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