764355d5b5e49f45710631b1f5058a7b8859c74b
[psensor.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2017 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 <bcm2835.h>
35 #include <cfg.h>
36 #include <graph.h>
37 #include <hdd.h>
38 #include <lmsensor.h>
39 #include <notify_cmd.h>
40 #include <nvidia.h>
41 #include <pgtop2.h>
42 #include <pmutex.h>
43 #include <psensor.h>
44 #include <pudisks2.h>
45 #include <rsensor.h>
46 #include <slog.h>
47 #include <ui.h>
48 #include <ui_appindicator.h>
49 #include <ui_color.h>
50 #include <ui_graph.h>
51 #include <ui_notify.h>
52 #include <ui_pref.h>
53 #include <ui_sensorlist.h>
54 #include <ui_status.h>
55 #include <ui_unity.h>
56
57 static const char *program_name;
58
59 static void print_version(void)
60 {
61         printf("psensor %s\n", VERSION);
62         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
63                  "License GPLv2: GNU GPL version 2 or later "
64                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
65                  "This is free software: you are free to change and"
66                  " redistribute it.\n"
67                  "There is NO WARRANTY, to the extent permitted by law.\n"),
68                "2010-2014");
69 }
70
71 static void print_help(void)
72 {
73         printf(_("Usage: %s [OPTION]...\n"), program_name);
74
75         puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
76                "including temperatures and fan speeds."));
77
78         puts("");
79         puts(_("Options:"));
80         puts(_("  -h, --help          display this help and exit\n"
81                "  -v, --version       display version information and exit"));
82
83         puts("");
84
85         puts(_(
86 "  -u, --url=URL       the URL of the psensor-server,\n"
87 "                      example: http://hostname:3131"));
88         puts(_(
89 "  -n, --new-instance  force the creation of a new Psensor application"));
90         puts("");
91
92         puts(_("  -d, --debug=LEVEL   "
93                "set the debug level, integer between 0 and 3"));
94
95         puts("");
96
97         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
98         puts("");
99         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
100 }
101
102 /*
103  * Updates the size of the sensor values if different than the
104  * configuration.
105  */
106 static void
107 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
108 {
109         struct psensor **cur, *s;
110
111         for (cur = sensors; *cur; cur++) {
112                 s = *cur;
113
114                 if (s->values_max_length != cfg->sensor_values_max_length)
115                         psensor_values_resize(s,
116                                               cfg->sensor_values_max_length);
117         }
118 }
119
120 static void *update_measures(void *data)
121 {
122         struct psensor **sensors;
123         struct config *cfg;
124         int period;
125         struct ui_psensor *ui;
126
127         ui = (struct ui_psensor *)data;
128         cfg = ui->config;
129
130         while (1) {
131                 pmutex_lock(&ui->sensors_mutex);
132
133                 sensors = ui->sensors;
134                 if (!sensors)
135                         pthread_exit(NULL);
136
137                 update_psensor_values_size(sensors, cfg);
138
139                 lmsensor_psensor_list_update(sensors);
140
141                 remote_psensor_list_update(sensors);
142                 nvidia_psensor_list_update(sensors);
143                 amd_psensor_list_update(sensors);
144                 udisks2_psensor_list_update(sensors);
145                 gtop2_psensor_list_update(sensors);
146                 atasmart_psensor_list_update(sensors);
147                 hddtemp_psensor_list_update(sensors);
148                 bcm2835_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_get_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
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
228 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
229 {
230         bool ret;
231         struct psensor *s;
232         double high_temp, high_default, low_default;
233
234         high_temp = config_get_default_high_threshold_temperature();
235
236         while (*sensors) {
237                 s = *sensors;
238
239                 s->cb_alarm_raised = cb_alarm_raised;
240                 s->cb_alarm_raised_data = ui;
241
242                 if (s->max == UNKNOWN_DBL_VALUE) {
243                         if (s->type & SENSOR_TYPE_TEMP)
244                                 high_default = high_temp;
245                         else
246                                 high_default = UNKNOWN_DBL_VALUE;
247                 } else {
248                         high_default = s->max;
249                 }
250
251                 ret = config_get_sensor_alarm_high_threshold
252                         (s->id, &s->alarm_high_threshold, high_default);
253
254                 if (!ret)
255                         s->alarm_high_threshold = high_default;
256
257                 if (s->min == UNKNOWN_DBL_VALUE)
258                         low_default = 0;
259                 else
260                         low_default = s->min;
261
262                 ret = config_get_sensor_alarm_low_threshold
263                         (s->id, &s->alarm_low_threshold, low_default);
264
265                 if (!ret)
266                         s->alarm_low_threshold = low_default;
267
268                 sensors++;
269         }
270 }
271
272 static void associate_preferences(struct psensor **sensors)
273 {
274         struct psensor **sensor_cur = sensors;
275
276         while (*sensor_cur) {
277                 char *n;
278                 struct psensor *s = *sensor_cur;
279
280                 n = config_get_sensor_name(s->id);
281
282                 if (n) {
283                         free(s->name);
284                         s->name = n;
285                 }
286
287                 sensor_cur++;
288         }
289 }
290
291 static void log_init(void)
292 {
293         const char *dir;
294         char *path;
295
296         dir = get_psensor_user_dir();
297
298         if (!dir)
299                 return;
300
301         path = malloc(strlen(dir)+1+strlen("log")+1);
302         sprintf(path, "%s/%s", dir, "log");
303
304         log_open(path);
305
306         free(path);
307 }
308
309 static struct option long_options[] = {
310         {"version", no_argument, NULL, 'v'},
311         {"help", no_argument, NULL, 'h'},
312         {"url", required_argument, NULL, 'u'},
313         {"debug", required_argument, NULL, 'd'},
314         {"new-instance", no_argument, NULL, 'n'},
315         {NULL, 0, NULL, 0}
316 };
317
318 /*
319  * show the main window if there will be no way for the user to show
320  * it (no appindicator, no status icon).
321  */
322 static gboolean check_ui_visible(gpointer data)
323 {
324         struct ui_psensor *ui;
325
326         log_debug("check_ui_visible()");
327
328         ui = (struct ui_psensor *)data;
329
330         log_debug("is_status_supported: %d", is_status_supported());
331         log_debug("is_appindicator_supported: %d",
332                    is_appindicator_supported());
333
334         if (!is_appindicator_supported() && !is_status_supported())
335                 ui_window_show(ui);
336
337         ui_window_update(ui);
338
339         return FALSE;
340 }
341
342 static void log_glib_info(void)
343 {
344         log_debug("Compiled with GLib %d.%d.%d",
345                   GLIB_MAJOR_VERSION,
346                   GLIB_MINOR_VERSION,
347                   GLIB_MICRO_VERSION);
348
349         log_debug("Running with GLib %d.%d.%d",
350                   glib_major_version,
351                   glib_minor_version,
352                   glib_micro_version);
353 }
354
355 static void cb_activate(GApplication *application,
356                         gpointer data)
357 {
358         ui_window_show((struct ui_psensor *)data);
359 }
360
361 /*
362  * Release memory for Valgrind.
363  */
364 static void cleanup(struct ui_psensor *ui)
365 {
366         pmutex_lock(&ui->sensors_mutex);
367
368         log_debug("Cleanup...");
369
370         nvidia_cleanup();
371         amd_cleanup();
372         rsensor_cleanup();
373
374         psensor_list_free(ui->sensors);
375         ui->sensors = NULL;
376
377         ui_appindicator_cleanup();
378
379         ui_status_cleanup();
380
381         pmutex_unlock(&ui->sensors_mutex);
382
383         config_cleanup();
384
385         log_debug("Cleanup done, closing log");
386 }
387
388 /*
389  * Creates the list of sensors.
390  *
391  * 'url': remote psensor server url, null for local monitoring.
392  */
393 static struct psensor **create_sensors_list(const char *url)
394 {
395         struct psensor **sensors;
396
397         if (url) {
398                 if (rsensor_is_supported()) {
399                         rsensor_init();
400                         sensors = get_remote_sensors(url, 600);
401                 } else {
402                         log_err(_("Psensor has not been compiled with remote "
403                                   "sensor support."));
404                         exit(EXIT_FAILURE);
405                 }
406         } else {
407                 sensors = malloc(sizeof(struct psensor *));
408                 *sensors = NULL;
409
410                 if (config_is_lmsensor_enabled())
411                         lmsensor_psensor_list_append(&sensors, 600);
412
413                 if (config_is_hddtemp_enabled())
414                         hddtemp_psensor_list_append(&sensors, 600);
415
416                 if (config_is_libatasmart_enabled())
417                         atasmart_psensor_list_append(&sensors, 600);
418
419                 if (config_is_nvctrl_enabled())
420                         nvidia_psensor_list_append(&sensors, 600);
421
422                 if (config_is_atiadlsdk_enabled())
423                         amd_psensor_list_append(&sensors, 600);
424
425                 if (config_is_gtop2_enabled())
426                         gtop2_psensor_list_append(&sensors, 600);
427
428                 if (config_is_udisks2_enabled())
429                         udisks2_psensor_list_append(&sensors, 600);
430
431                 bcm2835_psensor_list_append(&sensors, 600);
432         }
433
434         associate_preferences(sensors);
435
436         return sensors;
437 }
438
439 int main(int argc, char **argv)
440 {
441         struct ui_psensor ui;
442         pthread_t thread;
443         int optc, cmdok, opti, new_instance, ret;
444         char *url = NULL;
445         GApplication *app;
446
447         program_name = argv[0];
448
449         setlocale(LC_ALL, "");
450
451 #if ENABLE_NLS
452         bindtextdomain(PACKAGE, LOCALEDIR);
453         textdomain(PACKAGE);
454 #endif
455
456         new_instance = 0;
457
458         cmdok = 1;
459         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
460                                    &opti)) != -1) {
461                 switch (optc) {
462                 case 'u':
463                         if (optarg)
464                                 url = strdup(optarg);
465                         break;
466                 case 'h':
467                         print_help();
468                         exit(EXIT_SUCCESS);
469                 case 'v':
470                         print_version();
471                         exit(EXIT_SUCCESS);
472                 case 'd':
473                         log_level = atoi(optarg);
474                         log_info(_("Enables debug mode."));
475                         break;
476                 case 'n':
477                         new_instance = 1;
478                         break;
479                 default:
480                         cmdok = 0;
481                         break;
482                 }
483         }
484
485         if (!cmdok || optind != argc) {
486                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
487                         program_name);
488                 exit(EXIT_FAILURE);
489         }
490
491         log_init();
492
493         app = g_application_new("wpitchoune.psensor", 0);
494
495         g_application_register(app, NULL, NULL);
496
497         if (!new_instance && g_application_get_is_remote(app)) {
498                 g_application_activate(app);
499                 log_warn(_("A Psensor instance already exists."));
500                 exit(EXIT_SUCCESS);
501         }
502
503         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
504
505         log_glib_info();
506 #if !(GLIB_CHECK_VERSION(2, 31, 0))
507         /*
508          * Since GLib 2.31 g_thread_init call is deprecated and not
509          * needed.
510          */
511         log_debug("Calling g_thread_init(NULL)");
512         g_thread_init(NULL);
513 #endif
514
515         gtk_init(NULL, NULL);
516
517         pmutex_init(&ui.sensors_mutex);
518
519         ui.config = config_load();
520
521         ui.sensors = create_sensors_list(url);
522         associate_cb_alarm_raised(ui.sensors, &ui);
523
524         if (ui.config->slog_enabled)
525                 slog_activate(NULL,
526                               ui.sensors,
527                               &ui.sensors_mutex,
528                               config_get_slog_interval());
529
530         ui_status_init(&ui);
531         ui_status_set_visible(1);
532
533         /* main window */
534         ui_window_create(&ui);
535
536         ui_enable_alpha_channel(&ui);
537
538         ret = pthread_create(&thread, NULL, update_measures, &ui);
539
540         if (ret)
541                 log_err(_("Failed to create thread for monitoring sensors"));
542
543         ui.graph_update_interval = ui.config->graph_update_interval;
544
545         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
546
547         ui_appindicator_init(&ui);
548         ui_unity_init();
549
550         gdk_notify_startup_complete();
551
552         log_debug("hide_on_startup: %d", ui.config->hide_on_startup);
553         if (ui.config->hide_on_startup)
554                 /* Wait 30s to allow attempt to show status icon or other UI,
555                  * then check whether any UI is accessible. If not, then main
556                  * window must be shown despite hide_on_startup preference.
557                  * There may be a cleaner way to deal with this.
558                  */
559                 g_timeout_add(30000, (GSourceFunc)check_ui_visible, &ui);
560         else
561                 ui_window_show(&ui);
562
563         /* main loop */
564         gtk_main();
565
566         g_object_unref(app);
567         cleanup(&ui);
568
569         log_debug("Quitting...");
570         log_close();
571
572         if (url)
573                 free(url);
574
575         return 0;
576 }