ba3eafb970683e153cdb402965e52aae138adf70
[psensor.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2014 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 "notify_cmd.h"
41 #include <pmutex.h>
42 #include <pudisks2.h>
43 #include "slog.h"
44 #include "ui_pref.h"
45 #include "ui_graph.h"
46 #include "ui_status.h"
47
48 #ifdef HAVE_UNITY
49 #include "ui_unity.h"
50 #endif
51
52 #ifdef HAVE_NVIDIA
53 #include "nvidia.h"
54 #endif
55
56 #ifdef HAVE_LIBATIADL
57 #include "amd.h"
58 #endif
59
60 #ifdef HAVE_REMOTE_SUPPORT
61 #include "rsensor.h"
62 #endif
63
64 #include "ui_appindicator.h"
65
66 #ifdef HAVE_LIBNOTIFY
67 #include "ui_notify.h"
68 #endif
69
70 #ifdef HAVE_GTOP
71 #include <pgtop2.h>
72 #endif
73
74 static const char *program_name;
75
76 static void print_version()
77 {
78         printf("psensor %s\n", VERSION);
79         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
80                  "License GPLv2: GNU GPL version 2 or later "
81                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
82                  "This is free software: you are free to change and"
83                  " redistribute it.\n"
84                  "There is NO WARRANTY, to the extent permitted by law.\n"),
85                "2010-2014");
86 }
87
88 static void print_help()
89 {
90         printf(_("Usage: %s [OPTION]...\n"), program_name);
91
92         puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
93                "including temperatures and fan speeds."));
94
95         puts("");
96         puts(_("Options:"));
97         puts(_("  -h, --help          display this help and exit\n"
98                "  -v, --version       display version information and exit"));
99
100         puts("");
101
102         puts(_(
103 "  -u, --url=URL       the URL of the psensor-server,\n"
104 "                      example: http://hostname:3131"));
105         puts(_(
106 "  --use-libatasmart   use atasmart library for disk monitoring instead of\n"
107 "                      hddtemp daemon"));
108         puts(_(
109 "  -n, --new-instance  force the creation of a new Psensor application"));
110         puts("");
111
112         puts(_("  -d, --debug=LEVEL   "
113                "set the debug level, integer between 0 and 3"));
114
115         puts("");
116
117         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
118         puts("");
119         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
120 }
121
122 /*
123  * Updates the size of the sensor values if different than the
124  * configuration.
125  */
126 static void
127 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
128 {
129         struct psensor **cur, *s;
130
131         for (cur = sensors; *cur; cur++) {
132                 s = *cur;
133
134                 if (s->values_max_length != cfg->sensor_values_max_length)
135                         psensor_values_resize(s,
136                                               cfg->sensor_values_max_length);
137         }
138 }
139
140 static void *update_measures(void *data)
141 {
142         struct psensor **sensors;
143         struct config *cfg;
144         int period;
145         struct ui_psensor *ui;
146
147         ui = (struct ui_psensor *)data;
148         cfg = ui->config;
149
150         while (1) {
151                 pmutex_lock(&ui->sensors_mutex);
152
153                 sensors = ui->sensors;
154                 if (!sensors)
155                         pthread_exit(NULL);
156
157                 update_psensor_values_size(sensors, cfg);
158
159                 psensor_list_update_measures(sensors);
160
161                 lmsensor_psensor_list_update(sensors);
162 #ifdef HAVE_REMOTE_SUPPORT
163                 remote_psensor_list_update(sensors);
164 #endif
165 #ifdef HAVE_NVIDIA
166                 nvidia_psensor_list_update(sensors);
167 #endif
168 #ifdef HAVE_LIBATIADL
169                 amd_psensor_list_update(sensors);
170 #endif
171 #ifdef HAVE_LIBUDISKS2
172                 udisks2_psensor_list_update(sensors);
173 #endif
174 #ifdef HAVE_GTOP
175                 gtop2_psensor_list_update(sensors);
176 #endif
177
178                 psensor_log_measures(sensors);
179
180                 period = cfg->sensor_update_interval;
181
182                 pmutex_unlock(&ui->sensors_mutex);
183
184                 sleep(period);
185         }
186 }
187
188 static void indicators_update(struct ui_psensor *ui)
189 {
190         struct psensor **sensor_cur = ui->sensors;
191         unsigned int attention = 0;
192
193         while (*sensor_cur) {
194                 struct psensor *s = *sensor_cur;
195
196                 if (s->alarm_enabled && s->alarm_raised) {
197                         attention = 1;
198                         break;
199                 }
200
201                 sensor_cur++;
202         }
203
204 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
205         if (is_appindicator_supported())
206                 ui_appindicator_update(ui, attention);
207 #endif
208
209         if (is_status_supported())
210                 ui_status_update(ui, attention);
211 }
212
213 gboolean ui_refresh_thread(gpointer data)
214 {
215         struct config *cfg;
216         gboolean ret;
217         struct ui_psensor *ui = (struct ui_psensor *)data;
218
219         ret = TRUE;
220         cfg = ui->config;
221
222         pmutex_lock(&ui->sensors_mutex);
223
224         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
225
226         ui_sensorlist_update(ui, 0);
227
228         if (is_appindicator_supported() || is_status_supported())
229                 indicators_update(ui);
230
231 #ifdef HAVE_UNITY
232         ui_unity_launcher_entry_update(ui->sensors,
233                                        !cfg->unity_launcher_count_disabled,
234                                        cfg->temperature_unit == CELSIUS);
235 #endif
236
237         if (ui->graph_update_interval != cfg->graph_update_interval) {
238                 ui->graph_update_interval = cfg->graph_update_interval;
239                 ret = FALSE;
240         }
241
242         pmutex_unlock(&ui->sensors_mutex);
243
244         if (ret == FALSE)
245                 g_timeout_add(1000 * ui->graph_update_interval,
246                               ui_refresh_thread, ui);
247
248         return ret;
249 }
250
251 static void cb_alarm_raised(struct psensor *sensor, void *data)
252 {
253 #ifdef HAVE_LIBNOTIFY
254         if (sensor->alarm_enabled)
255                 ui_notify(sensor, (struct ui_psensor *)data);
256 #endif
257
258         notify_cmd(sensor);
259 }
260
261 static void associate_colors(struct psensor **sensors)
262 {
263         /* number of uniq colors */
264 #define COLORS_COUNT 8
265
266         double colors[COLORS_COUNT][3] = {
267                 {0, 0, 0},      /* black */
268                 {1, 0, 0},      /* red */
269                 {0, 0, 1},      /* blue */
270                 {0, 1, 0},      /* green */
271
272                 {0.5, 0.5, 0.5},/* grey */
273                 {0.5, 0, 0},    /* dark red */
274                 {0, 0, 0.5},    /* dark blue */
275                 {0, 0.5, 0}     /* dark green */
276         };
277         struct psensor **cur;
278         int i;
279         struct color c;
280
281         for (cur = sensors, i = 0; *cur; cur++, i++) {
282                 color_set(&c,
283                           colors[i % COLORS_COUNT][0],
284                           colors[i % COLORS_COUNT][1],
285                           colors[i % COLORS_COUNT][2]);
286
287                 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
288         }
289 }
290
291 static void
292 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
293 {
294         struct psensor **sensor_cur = sensors;
295
296         while (*sensor_cur) {
297                 struct psensor *s = *sensor_cur;
298
299                 s->cb_alarm_raised = cb_alarm_raised;
300                 s->cb_alarm_raised_data = ui;
301
302                 s->alarm_high_threshold
303                         = config_get_sensor_alarm_high_threshold(s->id);
304                 s->alarm_low_threshold
305                         = config_get_sensor_alarm_low_threshold(s->id);
306
307                 s->alarm_enabled
308                             = config_get_sensor_alarm_enabled(s->id);
309
310                 sensor_cur++;
311         }
312 }
313
314 static void associate_preferences(struct psensor **sensors)
315 {
316         struct psensor **sensor_cur = sensors;
317
318         while (*sensor_cur) {
319                 char *n;
320                 struct psensor *s = *sensor_cur;
321
322                 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
323
324                 n = config_get_sensor_name(s->id);
325
326                 if (n) {
327                         free(s->name);
328                         s->name = n;
329                 }
330
331                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
332
333                 sensor_cur++;
334         }
335 }
336
337 static void log_init()
338 {
339         const char *dir;
340         char *path;
341
342         dir = get_psensor_user_dir();
343
344         if (!dir)
345                 return;
346
347         path = malloc(strlen(dir)+1+strlen("log")+1);
348         sprintf(path, "%s/%s", dir, "log");
349
350         log_open(path);
351
352         free(path);
353 }
354
355 static struct option long_options[] = {
356         {"use-libatasmart", no_argument, 0, 0},
357         {"version", no_argument, 0, 'v'},
358         {"help", no_argument, 0, 'h'},
359         {"url", required_argument, 0, 'u'},
360         {"debug", required_argument, 0, 'd'},
361         {"new-instance", no_argument, 0, 'n'},
362         {0, 0, 0, 0}
363 };
364
365 static gboolean initial_window_show(gpointer data)
366 {
367         struct ui_psensor *ui;
368
369         log_debug("initial_window_show()");
370
371         ui = (struct ui_psensor *)data;
372
373         log_debug("is_status_supported: %d", is_status_supported());
374         log_debug("is_appindicator_supported: %d",
375                    is_appindicator_supported());
376         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
377
378         if (!ui->config->hide_on_startup
379             || (!is_appindicator_supported() && !is_status_supported()))
380                 ui_window_show(ui);
381
382         ui_window_update(ui);
383
384         return FALSE;
385 }
386
387 static void log_glib_info()
388 {
389         log_debug("Compiled with GLib %d.%d.%d",
390                   GLIB_MAJOR_VERSION,
391                   GLIB_MINOR_VERSION,
392                   GLIB_MICRO_VERSION);
393
394         log_debug("Running with GLib %d.%d.%d",
395                   glib_major_version,
396                   glib_minor_version,
397                   glib_micro_version);
398 }
399
400 static void cb_activate(GApplication *application,
401                         gpointer data)
402 {
403         ui_window_show((struct ui_psensor *)data);
404 }
405
406 /*
407  * Release memory for Valgrind.
408  */
409 static void cleanup(struct ui_psensor *ui)
410 {
411         pmutex_lock(&ui->sensors_mutex);
412
413         log_debug("Cleanup...");
414
415         psensor_cleanup();
416
417 #ifdef HAVE_NVIDIA
418         nvidia_cleanup();
419 #endif
420 #ifdef HAVE_LIBATIADL
421         amd_cleanup();
422 #endif
423 #ifdef HAVE_REMOTE_SUPPORT
424         rsensor_cleanup();
425 #endif
426
427         psensor_list_free(ui->sensors);
428         ui->sensors = NULL;
429
430 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
431         ui_appindicator_cleanup();
432 #endif
433
434         ui_status_cleanup();
435
436         pmutex_unlock(&ui->sensors_mutex);
437
438         config_cleanup();
439
440         log_debug("Cleanup done, closing log");
441 }
442
443 /*
444  * Creates the list of sensors.
445  *
446  * 'url': remote psensor server url, null for local monitoring.
447  * 'use_libatasmart': whether the libatasmart must be used.
448  */
449 static struct psensor **create_sensors_list(const char *url,
450                                             unsigned int use_libatasmart)
451 {
452         struct psensor **sensors;
453
454         if (url) {
455 #ifdef HAVE_REMOTE_SUPPORT
456                 rsensor_init();
457                 sensors = get_remote_sensors(url, 600);
458 #else
459                 log_err(_("Psensor has not been compiled with remote "
460                           "sensor support."));
461                 exit(EXIT_FAILURE);
462 #endif
463         } else {
464                 sensors = get_all_sensors(use_libatasmart, 600);
465
466                 if (config_is_lmsensor_enabled())
467                         lmsensor_psensor_list_append(&sensors, 600);
468
469 #ifdef HAVE_NVIDIA
470                 if (config_is_nvctrl_enabled())
471                         nvidia_psensor_list_append(&sensors, 600);
472 #endif
473 #ifdef HAVE_LIBATIADL
474                 if (config_is_atiadlsdk_enabled())
475                         amd_psensor_list_append(&sensors, 600);
476 #endif
477 #ifdef HAVE_GTOP
478                 if (config_is_gtop2_enabled())
479                         gtop2_psensor_list_append(&sensors, 600);
480 #endif
481 #ifdef HAVE_LIBUDISKS2
482                 if (config_is_udisks2_enabled())
483                         udisks2_psensor_list_append(&sensors, 600);
484 #endif
485         }
486
487         associate_preferences(sensors);
488         associate_colors(sensors);
489
490         return sensors;
491 }
492
493 int main(int argc, char **argv)
494 {
495         struct ui_psensor ui;
496         pthread_t thread;
497         int optc, cmdok, opti, use_libatasmart, new_instance, ret;
498         char *url = NULL;
499         GApplication *app;
500
501         program_name = argv[0];
502
503         setlocale(LC_ALL, "");
504
505 #if ENABLE_NLS
506         bindtextdomain(PACKAGE, LOCALEDIR);
507         textdomain(PACKAGE);
508 #endif
509
510         use_libatasmart = new_instance = 0;
511
512         cmdok = 1;
513         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
514                                    &opti)) != -1) {
515                 switch (optc) {
516                 case 0:
517                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
518                                 use_libatasmart = 1;
519                         break;
520                 case 'u':
521                         if (optarg)
522                                 url = strdup(optarg);
523                         break;
524                 case 'h':
525                         print_help();
526                         exit(EXIT_SUCCESS);
527                 case 'v':
528                         print_version();
529                         exit(EXIT_SUCCESS);
530                 case 'd':
531                         log_level = atoi(optarg);
532                         log_info(_("Enables debug mode."));
533                         break;
534                 case 'n':
535                         new_instance = 1;
536                         break;
537                 default:
538                         cmdok = 0;
539                         break;
540                 }
541         }
542
543         if (!cmdok || optind != argc) {
544                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
545                         program_name);
546                 exit(EXIT_FAILURE);
547         }
548
549         log_init();
550
551         app = g_application_new("wpitchoune.psensor", 0);
552
553         g_application_register(app, NULL, NULL);
554
555         if (!new_instance && g_application_get_is_remote(app)) {
556                 g_application_activate(app);
557                 log_warn(_("A Psensor instance already exists."));
558                 exit(EXIT_SUCCESS);
559         }
560
561         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
562
563         log_glib_info();
564 #if !(GLIB_CHECK_VERSION(2, 31, 0))
565         /*
566          * Since GLib 2.31 g_thread_init call is deprecated and not
567          * needed.
568          */
569         log_debug("Calling g_thread_init(NULL)");
570         g_thread_init(NULL);
571 #endif
572
573 #ifdef HAVE_APPINDICATOR_029
574         /* gdk_thread_enter/leave only used to workaround mutex bug
575          * of appindicator < 0.2.9, so do not call gdk_threads_init
576          * if useless. Calling this function leads to
577          * crash "Attempt to unlock mutex that was not locked" with
578          * GLib 2.41.2 (new checking) probably due to bugs in GTK
579          * itself.
580          */
581         gdk_threads_init();
582 #endif
583
584         gtk_init(NULL, NULL);
585
586         pmutex_init(&ui.sensors_mutex);
587
588         ui.config = config_load();
589
590         psensor_init();
591
592         ui.sensors = create_sensors_list(url, use_libatasmart);
593         associate_cb_alarm_raised(ui.sensors, &ui);
594
595         if (ui.config->slog_enabled)
596                 slog_activate(NULL,
597                               ui.sensors,
598                               &ui.sensors_mutex,
599                               config_get_slog_interval());
600
601 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
602         ui_status_init(&ui);
603         ui_status_set_visible(1);
604 #endif
605
606         /* main window */
607         ui_window_create(&ui);
608
609         ui_enable_alpha_channel(&ui);
610
611         ret = pthread_create(&thread, NULL, update_measures, &ui);
612
613         if (ret)
614                 log_err(_("Failed to create thread for monitoring sensors"));
615
616         ui.graph_update_interval = ui.config->graph_update_interval;
617
618         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
619
620 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
621         ui_appindicator_init(&ui);
622 #endif
623
624         gdk_notify_startup_complete();
625
626         /*
627          * hack, did not find a cleaner solution.
628          * wait 30s to ensure that the status icon is attempted to be
629          * drawn before determining whether the main window must be
630          * show.
631          */
632         if  (ui.config->hide_on_startup)
633                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
634         else
635                 initial_window_show(&ui);
636
637         log_debug("translators: %s\n", _("translator-credits"));
638
639         /* main loop */
640         gtk_main();
641
642         g_object_unref(app);
643         cleanup(&ui);
644
645         log_debug("Quitting...");
646         log_close();
647
648         if (url)
649                 free(url);
650
651         return 0;
652 }