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