added support of udisks2 (only init stage)
[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 "cpu.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 #ifdef HAVE_REMOTE_SUPPORT
161                 remote_psensor_list_update(sensors);
162 #endif
163 #ifdef HAVE_NVIDIA
164                 nvidia_psensor_list_update(sensors);
165 #endif
166 #ifdef HAVE_LIBATIADL
167                 amd_psensor_list_update(sensors);
168 #endif
169
170                 psensor_log_measures(sensors);
171
172                 period = cfg->sensor_update_interval;
173
174                 pmutex_unlock(&ui->sensors_mutex);
175
176                 sleep(period);
177         }
178 }
179
180 static void indicators_update(struct ui_psensor *ui)
181 {
182         struct psensor **sensor_cur = ui->sensors;
183         unsigned int attention = 0;
184
185         while (*sensor_cur) {
186                 struct psensor *s = *sensor_cur;
187
188                 if (s->alarm_enabled && s->alarm_raised) {
189                         attention = 1;
190                         break;
191                 }
192
193                 sensor_cur++;
194         }
195
196 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
197         if (is_appindicator_supported())
198                 ui_appindicator_update(ui, attention);
199 #endif
200
201         if (is_status_supported())
202                 ui_status_update(ui, attention);
203 }
204
205 gboolean ui_refresh_thread(gpointer data)
206 {
207         struct config *cfg;
208         gboolean ret;
209         struct ui_psensor *ui = (struct ui_psensor *)data;
210
211         ret = TRUE;
212         cfg = ui->config;
213
214         pmutex_lock(&ui->sensors_mutex);
215
216         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
217
218         ui_sensorlist_update(ui, 0);
219
220         if (is_appindicator_supported() || is_status_supported())
221                 indicators_update(ui);
222
223 #ifdef HAVE_UNITY
224         ui_unity_launcher_entry_update(ui->sensors,
225                                        !cfg->unity_launcher_count_disabled,
226                                        cfg->temperature_unit == CELSIUS);
227 #endif
228
229         if (ui->graph_update_interval != cfg->graph_update_interval) {
230                 ui->graph_update_interval = cfg->graph_update_interval;
231                 ret = FALSE;
232         }
233
234         pmutex_unlock(&ui->sensors_mutex);
235
236         if (ret == FALSE)
237                 g_timeout_add(1000 * ui->graph_update_interval,
238                               ui_refresh_thread, ui);
239
240         return ret;
241 }
242
243 static void cb_alarm_raised(struct psensor *sensor, void *data)
244 {
245 #ifdef HAVE_LIBNOTIFY
246         if (sensor->alarm_enabled)
247                 ui_notify(sensor, (struct ui_psensor *)data);
248 #endif
249
250         notify_cmd(sensor);
251 }
252
253 static void associate_colors(struct psensor **sensors)
254 {
255         /* number of uniq colors */
256 #define COLORS_COUNT 8
257
258         double colors[COLORS_COUNT][3] = {
259                 {0, 0, 0},      /* black */
260                 {1, 0, 0},      /* red */
261                 {0, 0, 1},      /* blue */
262                 {0, 1, 0},      /* green */
263
264                 {0.5, 0.5, 0.5},/* grey */
265                 {0.5, 0, 0},    /* dark red */
266                 {0, 0, 0.5},    /* dark blue */
267                 {0, 0.5, 0}     /* dark green */
268         };
269         struct psensor **cur;
270         int i;
271         struct color c;
272
273         for (cur = sensors, i = 0; *cur; cur++, i++) {
274                 color_set(&c,
275                           colors[i % COLORS_COUNT][0],
276                           colors[i % COLORS_COUNT][1],
277                           colors[i % COLORS_COUNT][2]);
278
279                 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
280         }
281 }
282
283 static void
284 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
285 {
286         struct psensor **sensor_cur = sensors;
287
288         while (*sensor_cur) {
289                 struct psensor *s = *sensor_cur;
290
291                 s->cb_alarm_raised = cb_alarm_raised;
292                 s->cb_alarm_raised_data = ui;
293
294                 s->alarm_high_threshold
295                         = config_get_sensor_alarm_high_threshold(s->id);
296                 s->alarm_low_threshold
297                         = config_get_sensor_alarm_low_threshold(s->id);
298
299                 s->alarm_enabled
300                             = config_get_sensor_alarm_enabled(s->id);
301
302                 sensor_cur++;
303         }
304 }
305
306 static void associate_preferences(struct psensor **sensors)
307 {
308         struct psensor **sensor_cur = sensors;
309
310         while (*sensor_cur) {
311                 char *n;
312                 struct psensor *s = *sensor_cur;
313
314                 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
315
316                 n = config_get_sensor_name(s->id);
317
318                 if (n) {
319                         free(s->name);
320                         s->name = n;
321                 }
322
323                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
324
325                 sensor_cur++;
326         }
327 }
328
329 static void log_init()
330 {
331         const char *dir;
332         char *path;
333
334         dir = get_psensor_user_dir();
335
336         if (!dir)
337                 return;
338
339         path = malloc(strlen(dir)+1+strlen("log")+1);
340         sprintf(path, "%s/%s", dir, "log");
341
342         log_open(path);
343
344         free(path);
345 }
346
347 static struct option long_options[] = {
348         {"use-libatasmart", no_argument, 0, 0},
349         {"version", no_argument, 0, 'v'},
350         {"help", no_argument, 0, 'h'},
351         {"url", required_argument, 0, 'u'},
352         {"debug", required_argument, 0, 'd'},
353         {"new-instance", no_argument, 0, 'n'},
354         {0, 0, 0, 0}
355 };
356
357 static gboolean initial_window_show(gpointer data)
358 {
359         struct ui_psensor *ui;
360
361         log_debug("initial_window_show()");
362
363         ui = (struct ui_psensor *)data;
364
365         log_debug("is_status_supported: %d", is_status_supported());
366         log_debug("is_appindicator_supported: %d",
367                    is_appindicator_supported());
368         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
369
370         if (!ui->config->hide_on_startup
371             || (!is_appindicator_supported() && !is_status_supported()))
372                 ui_window_show(ui);
373
374         ui_window_update(ui);
375
376         return FALSE;
377 }
378
379 static void log_glib_info()
380 {
381         log_debug("Compiled with GLib %d.%d.%d",
382                   GLIB_MAJOR_VERSION,
383                   GLIB_MINOR_VERSION,
384                   GLIB_MICRO_VERSION);
385
386         log_debug("Running with GLib %d.%d.%d",
387                   glib_major_version,
388                   glib_minor_version,
389                   glib_micro_version);
390 }
391
392 static void cb_activate(GApplication *application,
393                         gpointer data)
394 {
395         ui_window_show((struct ui_psensor *)data);
396 }
397
398 /*
399  * Release memory for Valgrind.
400  */
401 static void cleanup(struct ui_psensor *ui)
402 {
403         pmutex_lock(&ui->sensors_mutex);
404
405         log_debug("Cleanup...");
406
407         psensor_cleanup();
408
409 #ifdef HAVE_NVIDIA
410         nvidia_cleanup();
411 #endif
412 #ifdef HAVE_LIBATIADL
413         amd_cleanup();
414 #endif
415 #ifdef HAVE_REMOTE_SUPPORT
416         rsensor_cleanup();
417 #endif
418
419         psensor_list_free(ui->sensors);
420         ui->sensors = NULL;
421
422 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
423         ui_appindicator_cleanup();
424 #endif
425
426         ui_status_cleanup();
427
428         pmutex_unlock(&ui->sensors_mutex);
429
430         config_cleanup();
431
432         log_debug("Cleanup done, closing log");
433 }
434
435 /*
436  * Creates the list of sensors.
437  *
438  * 'url': remote psensor server url, null for local monitoring.
439  * 'use_libatasmart': whether the libatasmart must be used.
440  */
441 static struct psensor **create_sensors_list(const char *url,
442                                             unsigned int use_libatasmart)
443 {
444         struct psensor **sensors;
445
446         if (url) {
447 #ifdef HAVE_REMOTE_SUPPORT
448                 rsensor_init();
449                 sensors = get_remote_sensors(url, 600);
450 #else
451                 log_err(_("Psensor has not been compiled with remote "
452                           "sensor support."));
453                 exit(EXIT_FAILURE);
454 #endif
455         } else {
456                 sensors = get_all_sensors(use_libatasmart, 600);
457 #ifdef HAVE_NVIDIA
458                 sensors = nvidia_psensor_list_add(sensors, 600);
459 #endif
460 #ifdef HAVE_LIBATIADL
461                 sensors = amd_psensor_list_add(sensors, 600);
462 #endif
463 #ifdef HAVE_GTOP
464                 sensors = cpu_psensor_list_add(sensors, 600);
465 #endif
466                 udisks2_psensor_list_add(&sensors, 600);
467         }
468
469         associate_preferences(sensors);
470         associate_colors(sensors);
471
472         return sensors;
473 }
474
475 int main(int argc, char **argv)
476 {
477         struct ui_psensor ui;
478         pthread_t thread;
479         int optc, cmdok, opti, use_libatasmart, new_instance, ret;
480         char *url = NULL;
481         GApplication *app;
482
483         program_name = argv[0];
484
485         setlocale(LC_ALL, "");
486
487 #if ENABLE_NLS
488         bindtextdomain(PACKAGE, LOCALEDIR);
489         textdomain(PACKAGE);
490 #endif
491
492         use_libatasmart = new_instance = 0;
493
494         cmdok = 1;
495         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
496                                    &opti)) != -1) {
497                 switch (optc) {
498                 case 0:
499                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
500                                 use_libatasmart = 1;
501                         break;
502                 case 'u':
503                         if (optarg)
504                                 url = strdup(optarg);
505                         break;
506                 case 'h':
507                         print_help();
508                         exit(EXIT_SUCCESS);
509                 case 'v':
510                         print_version();
511                         exit(EXIT_SUCCESS);
512                 case 'd':
513                         log_level = atoi(optarg);
514                         log_info(_("Enables debug mode."));
515                         break;
516                 case 'n':
517                         new_instance = 1;
518                         break;
519                 default:
520                         cmdok = 0;
521                         break;
522                 }
523         }
524
525         if (!cmdok || optind != argc) {
526                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
527                         program_name);
528                 exit(EXIT_FAILURE);
529         }
530
531         log_init();
532
533         app = g_application_new("wpitchoune.psensor", 0);
534
535         g_application_register(app, NULL, NULL);
536
537         if (!new_instance && g_application_get_is_remote(app)) {
538                 g_application_activate(app);
539                 log_warn(_("A Psensor instance already exists."));
540                 exit(EXIT_SUCCESS);
541         }
542
543         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
544
545         log_glib_info();
546 #if !(GLIB_CHECK_VERSION(2, 31, 0))
547         /*
548          * Since GLib 2.31 g_thread_init call is deprecated and not
549          * needed.
550          */
551         log_debug("Calling g_thread_init(NULL)");
552         g_thread_init(NULL);
553 #endif
554
555 #ifdef HAVE_APPINDICATOR_029
556         /* gdk_thread_enter/leave only used to workaround mutex bug
557          * of appindicator < 0.2.9, so do not call gdk_threads_init
558          * if useless. Calling this function leads to
559          * crash "Attempt to unlock mutex that was not locked" with
560          * GLib 2.41.2 (new checking) probably due to bugs in GTK
561          * itself.
562          */
563         gdk_threads_init();
564 #endif
565
566         gtk_init(NULL, NULL);
567
568         pmutex_init(&ui.sensors_mutex);
569
570         ui.config = config_load();
571
572         psensor_init();
573
574         ui.sensors = create_sensors_list(url, use_libatasmart);
575         associate_cb_alarm_raised(ui.sensors, &ui);
576
577         if (ui.config->slog_enabled)
578                 slog_activate(NULL,
579                               ui.sensors,
580                               &ui.sensors_mutex,
581                               config_get_slog_interval());
582
583 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
584         ui_status_init(&ui);
585         ui_status_set_visible(1);
586 #endif
587
588         /* main window */
589         ui_window_create(&ui);
590
591         ui_enable_alpha_channel(&ui);
592
593         ret = pthread_create(&thread, NULL, update_measures, &ui);
594
595         if (ret)
596                 log_err(_("Failed to create thread for monitoring sensors"));
597
598         ui.graph_update_interval = ui.config->graph_update_interval;
599
600         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
601
602 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
603         ui_appindicator_init(&ui);
604 #endif
605
606         gdk_notify_startup_complete();
607
608         /*
609          * hack, did not find a cleaner solution.
610          * wait 30s to ensure that the status icon is attempted to be
611          * drawn before determining whether the main window must be
612          * show.
613          */
614         if  (ui.config->hide_on_startup)
615                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
616         else
617                 initial_window_show(&ui);
618
619         log_debug("translators: %s\n", _("translator-credits"));
620
621         /* main loop */
622         gtk_main();
623
624         g_object_unref(app);
625         cleanup(&ui);
626
627         log_debug("Quitting...");
628         log_close();
629
630         if (url)
631                 free(url);
632
633         return 0;
634 }