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