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