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