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