avoid to use cpp 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 "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                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
346
347                 sensor_cur++;
348         }
349 }
350
351 static void log_init(void)
352 {
353         const char *dir;
354         char *path;
355
356         dir = get_psensor_user_dir();
357
358         if (!dir)
359                 return;
360
361         path = malloc(strlen(dir)+1+strlen("log")+1);
362         sprintf(path, "%s/%s", dir, "log");
363
364         log_open(path);
365
366         free(path);
367 }
368
369 static struct option long_options[] = {
370         {"version", no_argument, NULL, 'v'},
371         {"help", no_argument, NULL, 'h'},
372         {"url", required_argument, NULL, 'u'},
373         {"debug", required_argument, NULL, 'd'},
374         {"new-instance", no_argument, NULL, 'n'},
375         {NULL, 0, NULL, 0}
376 };
377
378 static gboolean initial_window_show(gpointer data)
379 {
380         struct ui_psensor *ui;
381
382         log_debug("initial_window_show()");
383
384         ui = (struct ui_psensor *)data;
385
386         log_debug("is_status_supported: %d", is_status_supported());
387         log_debug("is_appindicator_supported: %d",
388                    is_appindicator_supported());
389         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
390
391         if (!ui->config->hide_on_startup
392             || (!is_appindicator_supported() && !is_status_supported()))
393                 ui_window_show(ui);
394
395         ui_window_update(ui);
396
397         return FALSE;
398 }
399
400 static void log_glib_info(void)
401 {
402         log_debug("Compiled with GLib %d.%d.%d",
403                   GLIB_MAJOR_VERSION,
404                   GLIB_MINOR_VERSION,
405                   GLIB_MICRO_VERSION);
406
407         log_debug("Running with GLib %d.%d.%d",
408                   glib_major_version,
409                   glib_minor_version,
410                   glib_micro_version);
411 }
412
413 static void cb_activate(GApplication *application,
414                         gpointer data)
415 {
416         ui_window_show((struct ui_psensor *)data);
417 }
418
419 /*
420  * Release memory for Valgrind.
421  */
422 static void cleanup(struct ui_psensor *ui)
423 {
424         pmutex_lock(&ui->sensors_mutex);
425
426         log_debug("Cleanup...");
427
428 #ifdef HAVE_NVIDIA
429         nvidia_cleanup();
430 #endif
431 #ifdef HAVE_LIBATIADL
432         amd_cleanup();
433 #endif
434 #ifdef HAVE_REMOTE_SUPPORT
435         rsensor_cleanup();
436 #endif
437
438         psensor_list_free(ui->sensors);
439         ui->sensors = NULL;
440
441 #if defined(HAVE_APPINDICATOR)
442         ui_appindicator_cleanup();
443 #endif
444
445         ui_status_cleanup();
446
447         pmutex_unlock(&ui->sensors_mutex);
448
449         config_cleanup();
450
451         log_debug("Cleanup done, closing log");
452 }
453
454 /*
455  * Creates the list of sensors.
456  *
457  * 'url': remote psensor server url, null for local monitoring.
458  */
459 static struct psensor **create_sensors_list(const char *url)
460 {
461         struct psensor **sensors;
462
463         if (url) {
464 #ifdef HAVE_REMOTE_SUPPORT
465                 rsensor_init();
466                 sensors = get_remote_sensors(url, 600);
467 #else
468                 log_err(_("Psensor has not been compiled with remote "
469                           "sensor support."));
470                 exit(EXIT_FAILURE);
471 #endif
472         } else {
473                 sensors = malloc(sizeof(struct psensor *));
474                 *sensors = NULL;
475
476                 if (config_is_lmsensor_enabled())
477                         lmsensor_psensor_list_append(&sensors, 600);
478
479                 if (config_is_hddtemp_enabled())
480                         hddtemp_psensor_list_append(&sensors, 600);
481
482 #ifdef HAVE_ATASMART
483                 if (config_is_libatasmart_enabled())
484                         atasmart_psensor_list_append(&sensors, 600);
485 #endif
486
487 #ifdef HAVE_NVIDIA
488                 if (config_is_nvctrl_enabled())
489                         nvidia_psensor_list_append(&sensors, 600);
490 #endif
491 #ifdef HAVE_LIBATIADL
492                 if (config_is_atiadlsdk_enabled())
493                         amd_psensor_list_append(&sensors, 600);
494 #endif
495 #ifdef HAVE_GTOP
496                 if (config_is_gtop2_enabled())
497                         gtop2_psensor_list_append(&sensors, 600);
498 #endif
499 #ifdef HAVE_LIBUDISKS2
500                 if (config_is_udisks2_enabled())
501                         udisks2_psensor_list_append(&sensors, 600);
502 #endif
503         }
504
505         associate_preferences(sensors);
506         associate_colors(sensors);
507
508         return sensors;
509 }
510
511 int main(int argc, char **argv)
512 {
513         struct ui_psensor ui;
514         pthread_t thread;
515         int optc, cmdok, opti, new_instance, ret;
516         char *url = NULL;
517         GApplication *app;
518
519         program_name = argv[0];
520
521         setlocale(LC_ALL, "");
522
523 #if ENABLE_NLS
524         bindtextdomain(PACKAGE, LOCALEDIR);
525         textdomain(PACKAGE);
526 #endif
527
528         new_instance = 0;
529
530         cmdok = 1;
531         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
532                                    &opti)) != -1) {
533                 switch (optc) {
534                 case 'u':
535                         if (optarg)
536                                 url = strdup(optarg);
537                         break;
538                 case 'h':
539                         print_help();
540                         exit(EXIT_SUCCESS);
541                 case 'v':
542                         print_version();
543                         exit(EXIT_SUCCESS);
544                 case 'd':
545                         log_level = atoi(optarg);
546                         log_info(_("Enables debug mode."));
547                         break;
548                 case 'n':
549                         new_instance = 1;
550                         break;
551                 default:
552                         cmdok = 0;
553                         break;
554                 }
555         }
556
557         if (!cmdok || optind != argc) {
558                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
559                         program_name);
560                 exit(EXIT_FAILURE);
561         }
562
563         log_init();
564
565         app = g_application_new("wpitchoune.psensor", 0);
566
567         g_application_register(app, NULL, NULL);
568
569         if (!new_instance && g_application_get_is_remote(app)) {
570                 g_application_activate(app);
571                 log_warn(_("A Psensor instance already exists."));
572                 exit(EXIT_SUCCESS);
573         }
574
575         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
576
577         log_glib_info();
578 #if !(GLIB_CHECK_VERSION(2, 31, 0))
579         /*
580          * Since GLib 2.31 g_thread_init call is deprecated and not
581          * needed.
582          */
583         log_debug("Calling g_thread_init(NULL)");
584         g_thread_init(NULL);
585 #endif
586
587         gtk_init(NULL, NULL);
588
589         pmutex_init(&ui.sensors_mutex);
590
591         ui.config = config_load();
592
593         ui.sensors = create_sensors_list(url);
594         associate_cb_alarm_raised(ui.sensors, &ui);
595
596         if (ui.config->slog_enabled)
597                 slog_activate(NULL,
598                               ui.sensors,
599                               &ui.sensors_mutex,
600                               config_get_slog_interval());
601
602 #if !defined(HAVE_APPINDICATOR)
603         ui_status_init(&ui);
604         ui_status_set_visible(1);
605 #endif
606
607         /* main window */
608         ui_window_create(&ui);
609
610         ui_enable_alpha_channel(&ui);
611
612         ret = pthread_create(&thread, NULL, update_measures, &ui);
613
614         if (ret)
615                 log_err(_("Failed to create thread for monitoring sensors"));
616
617         ui.graph_update_interval = ui.config->graph_update_interval;
618
619         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
620
621 #if defined(HAVE_APPINDICATOR)
622         ui_appindicator_init(&ui);
623 #endif
624
625         gdk_notify_startup_complete();
626
627         /*
628          * hack, did not find a cleaner solution.
629          * wait 30s to ensure that the status icon is attempted to be
630          * drawn before determining whether the main window must be
631          * show.
632          */
633         if  (ui.config->hide_on_startup)
634                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
635         else
636                 initial_window_show(&ui);
637
638         log_debug("translators: %s\n", _("translator-credits"));
639
640         /* main loop */
641         gtk_main();
642
643         g_object_unref(app);
644         cleanup(&ui);
645
646         log_debug("Quitting...");
647         log_close();
648
649         if (url)
650                 free(url);
651
652         return 0;
653 }