fahrenheit support for unity launcher
[psensor.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include <gtk/gtk.h>
30
31 #include "config.h"
32
33 #include "cfg.h"
34 #include "psensor.h"
35 #include "graph.h"
36 #include "ui.h"
37 #include "ui_sensorlist.h"
38 #include "ui_color.h"
39 #include "lmsensor.h"
40 #include "ui_pref.h"
41 #include "ui_graph.h"
42 #include "ui_status.h"
43
44 #ifdef HAVE_UNITY
45 #include "ui_unity.h"
46 #endif
47
48 #ifdef HAVE_NVIDIA
49 #include "nvidia.h"
50 #endif
51
52 #ifdef HAVE_LIBATIADL
53 #include "amd.h"
54 #endif
55
56 #ifdef HAVE_REMOTE_SUPPORT
57 #include "rsensor.h"
58 #endif
59
60 #include "ui_appindicator.h"
61
62 #ifdef HAVE_LIBNOTIFY
63 #include "ui_notify.h"
64 #endif
65
66 #ifdef HAVE_GTOP
67 #include "cpu.h"
68 #endif
69
70 #include "compat.h"
71
72 static const char *program_name;
73
74 static void print_version()
75 {
76         printf("psensor %s\n", VERSION);
77         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
78                  "License GPLv2: GNU GPL version 2 or later "
79                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
80                  "This is free software: you are free to change and "
81                  " redistribute it.\n"
82                  "There is NO WARRANTY, to the extent permitted by law.\n"),
83                "2010-2012");
84 }
85
86 static void print_help()
87 {
88         printf(_("Usage: %s [OPTION]...\n"), program_name);
89
90         puts(_("psensor is a GTK application for monitoring hardware sensors, "
91                "including temperatures and fan speeds."));
92
93         puts("");
94         puts(_("Options:"));
95         puts(_("  -h, --help          display this help and exit\n"
96                "  -v, --version       display version information and exit"));
97
98         puts("");
99
100         puts(_("  -u, --url=URL       "
101                "the URL of the psensor-server, example: http://hostname:3131"));
102         puts(_("  --use-libatasmart   "
103                "use atasmart library for disk monitoring "
104                "instead of hddtemp daemon"));
105         puts("");
106
107         puts(_("  -d, --debug=LEVEL   "
108                "set the debug level, integer between 0 and 3"));
109
110         puts("");
111
112         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
113         puts("");
114         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
115 }
116
117 /*
118   Updates the size of the sensor values if different than the
119   configuration.
120  */
121 void
122 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
123 {
124         struct psensor **cur;
125
126         cur = sensors;
127         while (*cur) {
128                 struct psensor *s = *cur;
129
130                 if (s->values_max_length != cfg->sensor_values_max_length)
131                         psensor_values_resize(s,
132                                               cfg->sensor_values_max_length);
133
134                 cur++;
135         }
136 }
137
138 static void log_measures(struct psensor **sensors)
139 {
140         if (log_level == LOG_DEBUG)
141                 while (*sensors) {
142                         log_debug("Measure: %s %.2f",
143                                    (*sensors)->name,
144                                    psensor_get_current_value(*sensors));
145
146                         sensors++;
147                 }
148 }
149
150 void update_psensor_measures(struct ui_psensor *ui)
151 {
152         struct psensor **sensors = ui->sensors;
153         struct config *cfg = ui->config;
154
155         while (1) {
156                 g_mutex_lock(ui->sensors_mutex);
157
158                 if (!sensors)
159                         return;
160
161                 update_psensor_values_size(sensors, ui->config);
162
163                 psensor_list_update_measures(sensors);
164 #ifdef HAVE_REMOTE_SUPPORT
165                 remote_psensor_list_update(sensors);
166 #endif
167 #ifdef HAVE_NVIDIA
168                 nvidia_psensor_list_update(sensors);
169 #endif
170 #ifdef HAVE_LIBATIADL
171                 amd_psensor_list_update(sensors);
172 #endif
173
174                 log_measures(sensors);
175
176                 g_mutex_unlock(ui->sensors_mutex);
177
178                 sleep(cfg->sensor_update_interval);
179         }
180 }
181
182 static void indicators_update(struct ui_psensor *ui)
183 {
184         struct psensor **sensor_cur = ui->sensors;
185         unsigned int attention = 0;
186
187         while (*sensor_cur) {
188                 struct psensor *s = *sensor_cur;
189
190                 if (s->alarm_enabled && s->alarm_raised) {
191                         attention = 1;
192                         break;
193                 }
194
195                 sensor_cur++;
196         }
197
198 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
199         if (is_appindicator_supported())
200                 ui_appindicator_update(ui, attention);
201 #endif
202
203         if (is_status_supported())
204                 ui_status_update(ui, attention);
205 }
206
207 gboolean ui_refresh_thread(gpointer data)
208 {
209         struct config *cfg;
210         gboolean ret;
211         struct ui_psensor *ui = (struct ui_psensor *)data;
212
213         ret = TRUE;
214         cfg = ui->config;
215
216         g_mutex_lock(ui->sensors_mutex);
217
218         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
219
220         ui_sensorlist_update(ui);
221
222         if (is_appindicator_supported() || is_status_supported())
223                 indicators_update(ui);
224
225 #ifdef HAVE_UNITY
226         ui_unity_launcher_entry_update(ui->sensors,
227                                        !cfg->unity_launcher_count_disabled,
228                                        cfg->temperature_unit == CELCIUS);
229 #endif
230
231         if (ui->graph_update_interval != cfg->graph_update_interval) {
232                 ui->graph_update_interval = cfg->graph_update_interval;
233                 ret = FALSE;
234         }
235
236         g_mutex_unlock(ui->sensors_mutex);
237
238         if (ret == FALSE)
239                 g_timeout_add(1000 * ui->graph_update_interval,
240                               ui_refresh_thread, ui);
241
242         return ret;
243 }
244
245 void cb_alarm_raised(struct psensor *sensor, void *data)
246 {
247 #ifdef HAVE_LIBNOTIFY
248         if (sensor->enabled)
249                 ui_notify(sensor, (struct ui_psensor *)data);
250 #endif
251 }
252
253 static void associate_colors(struct psensor **sensors)
254 {
255         /* number of uniq colors */
256 #define COLORS_COUNT 8
257
258         unsigned int colors[COLORS_COUNT][3] = {
259                 {0x0000, 0x0000, 0x0000},       /* black */
260                 {0xffff, 0x0000, 0x0000},       /* red */
261                 {0x0000, 0.0000, 0xffff},       /* blue */
262                 {0x0000, 0xffff, 0x0000},       /* green */
263
264                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
265                 {0x7fff, 0x0000, 0x0000},       /* dark red */
266                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
267                 {0x0000, 0x7fff, 0x0000}        /* dark green */
268         };
269
270         struct psensor **sensor_cur = sensors;
271         int i = 0;
272         while (*sensor_cur) {
273                 struct color default_color;
274                 color_set(&default_color,
275                           colors[i % COLORS_COUNT][0],
276                           colors[i % COLORS_COUNT][1],
277                           colors[i % COLORS_COUNT][2]);
278
279                 (*sensor_cur)->color
280                     = config_get_sensor_color((*sensor_cur)->id,
281                                               &default_color);
282
283                 sensor_cur++;
284                 i++;
285         }
286 }
287
288 static void
289 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
290 {
291         struct psensor **sensor_cur = sensors;
292         while (*sensor_cur) {
293                 struct psensor *s = *sensor_cur;
294
295                 s->cb_alarm_raised = cb_alarm_raised;
296                 s->cb_alarm_raised_data = ui;
297
298                 if (is_temp_type(s->type)) {
299                         s->alarm_limit
300                             = config_get_sensor_alarm_limit(s->id, 60);
301                         s->alarm_enabled
302                             = config_get_sensor_alarm_enabled(s->id);
303                 } else {
304                         s->alarm_limit = 0;
305                         s->alarm_enabled = 0;
306                 }
307
308                 sensor_cur++;
309         }
310 }
311
312 static void associate_preferences(struct psensor **sensors)
313 {
314         struct psensor **sensor_cur = sensors;
315         while (*sensor_cur) {
316                 char *n;
317                 struct psensor *s = *sensor_cur;
318
319                 s->enabled = config_is_sensor_enabled(s->id);
320
321                 n = config_get_sensor_name(s->id);
322
323                 if (n)
324                         s->name = n;
325
326                 sensor_cur++;
327         }
328 }
329
330 static void log_init()
331 {
332         char *home, *path, *dir;
333
334         home = getenv("HOME");
335
336         if (!home)
337                 return ;
338
339         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
340         sprintf(dir, "%s/%s", home, ".psensor");
341         mkdir(dir, 0777);
342
343         path = malloc(strlen(dir)+1+strlen("log")+1);
344         sprintf(path, "%s/%s", dir, "log");
345
346         log_open(path);
347
348         free(dir);
349         free(path);
350 }
351
352 static struct option long_options[] = {
353         {"use-libatasmart", no_argument, 0, 0},
354         {"version", no_argument, 0, 'v'},
355         {"help", no_argument, 0, 'h'},
356         {"url", required_argument, 0, 'u'},
357         {"debug", required_argument, 0, 'd'},
358         {0, 0, 0, 0}
359 };
360
361 static gboolean initial_window_show(gpointer data)
362 {
363         struct ui_psensor *ui;
364
365         log_debug("initial_window_show()");
366
367         ui = (struct ui_psensor *)data;
368
369         log_debug("is_status_supported: %d", is_status_supported());
370         log_debug("is_appindicator_supported: %d",
371                    is_appindicator_supported());
372         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
373
374         if (!ui->config->hide_on_startup
375             || (!is_appindicator_supported() && !is_status_supported()))
376                 ui_window_show(ui);
377
378         ui_window_update(ui);
379
380         return FALSE;
381 }
382
383 static void log_glib_info()
384 {
385         log_debug("Compiled with GLib %d.%d.%d",
386                   GLIB_MAJOR_VERSION,
387                   GLIB_MINOR_VERSION,
388                   GLIB_MICRO_VERSION);
389
390         log_debug("Running with GLib %d.%d.%d",
391                   glib_major_version,
392                   glib_minor_version,
393                   glib_micro_version);
394 }
395
396 int main(int argc, char **argv)
397 {
398         struct ui_psensor ui;
399         GError *error;
400         GThread *thread;
401         int optc, cmdok, opti, use_libatasmart;
402         char *url = NULL;
403
404         program_name = argv[0];
405
406         setlocale(LC_ALL, "");
407
408 #if ENABLE_NLS
409         bindtextdomain(PACKAGE, LOCALEDIR);
410         textdomain(PACKAGE);
411 #endif
412
413         use_libatasmart = 0;
414
415         cmdok = 1;
416         while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
417                                    &opti)) != -1) {
418                 switch (optc) {
419                 case 0:
420                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
421                                 use_libatasmart = 1;
422                         break;
423                 case 'u':
424                         if (optarg)
425                                 url = strdup(optarg);
426                         break;
427                 case 'h':
428                         print_help();
429                         exit(EXIT_SUCCESS);
430                 case 'v':
431                         print_version();
432                         exit(EXIT_SUCCESS);
433                 case 'd':
434                         log_level = atoi(optarg);
435                         log_printf(LOG_INFO, _("Enables debug mode."));
436                         break;
437                 default:
438                         cmdok = 0;
439                         break;
440                 }
441         }
442
443         if (!cmdok || optind != argc) {
444                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
445                         program_name);
446                 exit(EXIT_FAILURE);
447         }
448
449         log_init();
450
451         log_glib_info();
452 #if !(GLIB_CHECK_VERSION(2, 31, 0))
453         /*
454          * Since GLib 2.31 g_thread_init call is deprecated and not
455          * needed.
456          */
457         log_debug("Calling g_thread_init(NULL)");
458         g_thread_init(NULL);
459 #endif
460
461         gdk_threads_init();
462
463         gtk_init(NULL, NULL);
464
465         ui.sensors_mutex = g_mutex_new();
466
467         config_init();
468
469         ui.config = config_load();
470
471         psensor_init();
472
473         if (url) {
474 #ifdef HAVE_REMOTE_SUPPORT
475                 rsensor_init();
476                 ui.sensors = get_remote_sensors(url, 600);
477 #else
478                 fprintf(stderr,
479                         _("ERROR: Not compiled with remote sensor support.\n"));
480                 exit(EXIT_FAILURE);
481 #endif
482         } else {
483                 ui.sensors = get_all_sensors(use_libatasmart, 600);
484 #ifdef HAVE_NVIDIA
485                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
486 #endif
487 #ifdef HAVE_LIBATIADL
488                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
489 #endif
490 #ifdef HAVE_GTOP
491                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
492 #endif
493         }
494
495         associate_preferences(ui.sensors);
496         associate_colors(ui.sensors);
497         associate_cb_alarm_raised(ui.sensors, &ui);
498
499 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
500         ui_status_init(&ui);
501         ui_status_set_visible(1);
502 #endif
503
504         /* main window */
505         ui_window_create(&ui);
506         ui.sensor_box = NULL;
507
508         /* drawing box */
509         ui.w_graph = ui_graph_create(&ui);
510
511         ui_enable_alpha_channel(&ui);
512
513         /* sensor list */
514         ui_sensorlist_create(&ui);
515
516         thread = g_thread_create((GThreadFunc) update_psensor_measures,
517                                  &ui, TRUE, &error);
518
519         if (!thread)
520                 g_error_free(error);
521
522         ui.graph_update_interval = ui.config->graph_update_interval;
523
524         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
525
526 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
527         ui_appindicator_init(&ui);
528 #endif
529
530         /*
531          * show the window as soon as all gtk events have been processed
532          * in order to ensure that the status icon is attempted to be
533          * drawn before. If not, there is no way to detect that it is
534          * visible.
535         */
536         g_idle_add((GSourceFunc)initial_window_show, &ui);
537
538         gdk_notify_startup_complete();
539
540         /* main loop */
541         gtk_main();
542
543         g_mutex_lock(ui.sensors_mutex);
544
545         psensor_cleanup();
546
547 #ifdef HAVE_NVIDIA
548         nvidia_cleanup();
549 #endif
550 #ifdef HAVE_LIBATIADL
551         amd_cleanup();
552 #endif
553 #ifdef HAVE_REMOTE_SUPPORT
554         rsensor_cleanup();
555 #endif
556
557         psensor_list_free(ui.sensors);
558         ui.sensors = NULL;
559
560 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
561         ui_appindicator_cleanup();
562 #endif
563
564         ui_status_cleanup();
565
566         g_mutex_unlock(ui.sensors_mutex);
567
568         config_cleanup();
569
570         log_close();
571
572         return 0;
573 }