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