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