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