avoid configuration data in psensor struct
[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 **ss, *s;
189         bool attention;
190
191         attention = false;
192         ss = ui->sensors;
193         while (*ss) {
194                 s = *ss;
195
196                 if (s->alarm_raised && config_get_sensor_alarm_enabled(s->id)) {
197                         attention = true;
198                         break;
199                 }
200
201                 ss++;
202         }
203
204 #if defined(HAVE_APPINDICATOR)
205         if (is_appindicator_supported())
206                 ui_appindicator_update(ui, attention);
207 #endif
208
209         if (is_status_supported())
210                 ui_status_update(ui, attention);
211 }
212
213 static gboolean ui_refresh_thread(gpointer data)
214 {
215         struct config *cfg;
216         gboolean ret;
217         struct ui_psensor *ui = (struct ui_psensor *)data;
218
219         ret = TRUE;
220         cfg = ui->config;
221
222         pmutex_lock(&ui->sensors_mutex);
223
224         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
225
226         ui_sensorlist_update(ui, 0);
227
228         if (is_appindicator_supported() || is_status_supported())
229                 indicators_update(ui);
230
231 #ifdef HAVE_UNITY
232         ui_unity_launcher_entry_update(ui->sensors,
233                                        !cfg->unity_launcher_count_disabled,
234                                        cfg->temperature_unit == CELSIUS);
235 #endif
236
237         if (ui->graph_update_interval != cfg->graph_update_interval) {
238                 ui->graph_update_interval = cfg->graph_update_interval;
239                 ret = FALSE;
240         }
241
242         pmutex_unlock(&ui->sensors_mutex);
243
244         if (ret == FALSE)
245                 g_timeout_add(1000 * ui->graph_update_interval,
246                               ui_refresh_thread, ui);
247
248         return ret;
249 }
250
251 static void cb_alarm_raised(struct psensor *sensor, void *data)
252 {
253         if (config_get_sensor_alarm_enabled(sensor->id)) {
254                 ui_notify(sensor, (struct ui_psensor *)data);
255                 notify_cmd(sensor);
256         }
257 }
258
259 static void associate_colors(struct psensor **sensors)
260 {
261         GdkRGBA rgba;
262         /* copied from the default colors of the gtk color color
263          * chooser. */
264         const char *default_colors[27] = {
265                 "#ef2929",  /* Scarlet Red */
266                 "#fcaf3e",  /* Orange */
267                 "#fce94f",  /* Butter */
268                 "#8ae234",  /* Chameleon */
269                 "#729fcf",  /* Sky Blue */
270                 "#ad7fa8",  /* Plum */
271                 "#e9b96e",  /* Chocolate */
272                 "#888a85",  /* Aluminum 1 */
273                 "#eeeeec",  /* Aluminum 2 */
274                 "#cc0000",
275                 "#f57900",
276                 "#edd400",
277                 "#73d216",
278                 "#3465a4",
279                 "#75507b",
280                 "#c17d11",
281                 "#555753",
282                 "#d3d7cf",
283                 "#a40000",
284                 "#ce5c00",
285                 "#c4a000",
286                 "#4e9a06",
287                 "#204a87",
288                 "#5c3566",
289                 "#8f5902",
290                 "#2e3436",
291                 "#babdb6"
292         };
293         int i;
294         struct psensor **cur;
295         struct color c;
296
297         for (cur = sensors, i = 0; *cur; cur++, i++) {
298                 gdk_rgba_parse(&rgba, default_colors[i % 27]);
299                 c.red = rgba.red;
300                 c.green = rgba.green;
301                 c.blue = rgba.blue;
302
303                 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
304         }
305 }
306
307 static void
308 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
309 {
310         struct psensor **sensor_cur = sensors;
311
312         while (*sensor_cur) {
313                 struct psensor *s = *sensor_cur;
314
315                 s->cb_alarm_raised = cb_alarm_raised;
316                 s->cb_alarm_raised_data = ui;
317
318                 s->alarm_high_threshold
319                         = config_get_sensor_alarm_high_threshold(s->id);
320                 s->alarm_low_threshold
321                         = config_get_sensor_alarm_low_threshold(s->id);
322
323                 sensor_cur++;
324         }
325 }
326
327 static void associate_preferences(struct psensor **sensors)
328 {
329         struct psensor **sensor_cur = sensors;
330
331         while (*sensor_cur) {
332                 char *n;
333                 struct psensor *s = *sensor_cur;
334
335                 n = config_get_sensor_name(s->id);
336
337                 if (n) {
338                         free(s->name);
339                         s->name = n;
340                 }
341
342                 sensor_cur++;
343         }
344 }
345
346 static void log_init(void)
347 {
348         const char *dir;
349         char *path;
350
351         dir = get_psensor_user_dir();
352
353         if (!dir)
354                 return;
355
356         path = malloc(strlen(dir)+1+strlen("log")+1);
357         sprintf(path, "%s/%s", dir, "log");
358
359         log_open(path);
360
361         free(path);
362 }
363
364 static struct option long_options[] = {
365         {"version", no_argument, NULL, 'v'},
366         {"help", no_argument, NULL, 'h'},
367         {"url", required_argument, NULL, 'u'},
368         {"debug", required_argument, NULL, 'd'},
369         {"new-instance", no_argument, NULL, 'n'},
370         {NULL, 0, NULL, 0}
371 };
372
373 static gboolean initial_window_show(gpointer data)
374 {
375         struct ui_psensor *ui;
376
377         log_debug("initial_window_show()");
378
379         ui = (struct ui_psensor *)data;
380
381         log_debug("is_status_supported: %d", is_status_supported());
382         log_debug("is_appindicator_supported: %d",
383                    is_appindicator_supported());
384         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
385
386         if (!ui->config->hide_on_startup
387             || (!is_appindicator_supported() && !is_status_supported()))
388                 ui_window_show(ui);
389
390         ui_window_update(ui);
391
392         return FALSE;
393 }
394
395 static void log_glib_info(void)
396 {
397         log_debug("Compiled with GLib %d.%d.%d",
398                   GLIB_MAJOR_VERSION,
399                   GLIB_MINOR_VERSION,
400                   GLIB_MICRO_VERSION);
401
402         log_debug("Running with GLib %d.%d.%d",
403                   glib_major_version,
404                   glib_minor_version,
405                   glib_micro_version);
406 }
407
408 static void cb_activate(GApplication *application,
409                         gpointer data)
410 {
411         ui_window_show((struct ui_psensor *)data);
412 }
413
414 /*
415  * Release memory for Valgrind.
416  */
417 static void cleanup(struct ui_psensor *ui)
418 {
419         pmutex_lock(&ui->sensors_mutex);
420
421         log_debug("Cleanup...");
422
423 #ifdef HAVE_NVIDIA
424         nvidia_cleanup();
425 #endif
426 #ifdef HAVE_LIBATIADL
427         amd_cleanup();
428 #endif
429 #ifdef HAVE_REMOTE_SUPPORT
430         rsensor_cleanup();
431 #endif
432
433         psensor_list_free(ui->sensors);
434         ui->sensors = NULL;
435
436 #if defined(HAVE_APPINDICATOR)
437         ui_appindicator_cleanup();
438 #endif
439
440         ui_status_cleanup();
441
442         pmutex_unlock(&ui->sensors_mutex);
443
444         config_cleanup();
445
446         log_debug("Cleanup done, closing log");
447 }
448
449 /*
450  * Creates the list of sensors.
451  *
452  * 'url': remote psensor server url, null for local monitoring.
453  */
454 static struct psensor **create_sensors_list(const char *url)
455 {
456         struct psensor **sensors;
457
458         if (url) {
459 #ifdef HAVE_REMOTE_SUPPORT
460                 rsensor_init();
461                 sensors = get_remote_sensors(url, 600);
462 #else
463                 log_err(_("Psensor has not been compiled with remote "
464                           "sensor support."));
465                 exit(EXIT_FAILURE);
466 #endif
467         } else {
468                 sensors = malloc(sizeof(struct psensor *));
469                 *sensors = NULL;
470
471                 if (config_is_lmsensor_enabled())
472                         lmsensor_psensor_list_append(&sensors, 600);
473
474                 if (config_is_hddtemp_enabled())
475                         hddtemp_psensor_list_append(&sensors, 600);
476
477 #ifdef HAVE_ATASMART
478                 if (config_is_libatasmart_enabled())
479                         atasmart_psensor_list_append(&sensors, 600);
480 #endif
481
482 #ifdef HAVE_NVIDIA
483                 if (config_is_nvctrl_enabled())
484                         nvidia_psensor_list_append(&sensors, 600);
485 #endif
486 #ifdef HAVE_LIBATIADL
487                 if (config_is_atiadlsdk_enabled())
488                         amd_psensor_list_append(&sensors, 600);
489 #endif
490 #ifdef HAVE_GTOP
491                 if (config_is_gtop2_enabled())
492                         gtop2_psensor_list_append(&sensors, 600);
493 #endif
494 #ifdef HAVE_LIBUDISKS2
495                 if (config_is_udisks2_enabled())
496                         udisks2_psensor_list_append(&sensors, 600);
497 #endif
498         }
499
500         associate_preferences(sensors);
501         associate_colors(sensors);
502
503         return sensors;
504 }
505
506 int main(int argc, char **argv)
507 {
508         struct ui_psensor ui;
509         pthread_t thread;
510         int optc, cmdok, opti, new_instance, ret;
511         char *url = NULL;
512         GApplication *app;
513
514         program_name = argv[0];
515
516         setlocale(LC_ALL, "");
517
518 #if ENABLE_NLS
519         bindtextdomain(PACKAGE, LOCALEDIR);
520         textdomain(PACKAGE);
521 #endif
522
523         new_instance = 0;
524
525         cmdok = 1;
526         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
527                                    &opti)) != -1) {
528                 switch (optc) {
529                 case 'u':
530                         if (optarg)
531                                 url = strdup(optarg);
532                         break;
533                 case 'h':
534                         print_help();
535                         exit(EXIT_SUCCESS);
536                 case 'v':
537                         print_version();
538                         exit(EXIT_SUCCESS);
539                 case 'd':
540                         log_level = atoi(optarg);
541                         log_info(_("Enables debug mode."));
542                         break;
543                 case 'n':
544                         new_instance = 1;
545                         break;
546                 default:
547                         cmdok = 0;
548                         break;
549                 }
550         }
551
552         if (!cmdok || optind != argc) {
553                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
554                         program_name);
555                 exit(EXIT_FAILURE);
556         }
557
558         log_init();
559
560         app = g_application_new("wpitchoune.psensor", 0);
561
562         g_application_register(app, NULL, NULL);
563
564         if (!new_instance && g_application_get_is_remote(app)) {
565                 g_application_activate(app);
566                 log_warn(_("A Psensor instance already exists."));
567                 exit(EXIT_SUCCESS);
568         }
569
570         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
571
572         log_glib_info();
573 #if !(GLIB_CHECK_VERSION(2, 31, 0))
574         /*
575          * Since GLib 2.31 g_thread_init call is deprecated and not
576          * needed.
577          */
578         log_debug("Calling g_thread_init(NULL)");
579         g_thread_init(NULL);
580 #endif
581
582         gtk_init(NULL, NULL);
583
584         pmutex_init(&ui.sensors_mutex);
585
586         ui.config = config_load();
587
588         ui.sensors = create_sensors_list(url);
589         associate_cb_alarm_raised(ui.sensors, &ui);
590
591         if (ui.config->slog_enabled)
592                 slog_activate(NULL,
593                               ui.sensors,
594                               &ui.sensors_mutex,
595                               config_get_slog_interval());
596
597 #if !defined(HAVE_APPINDICATOR)
598         ui_status_init(&ui);
599         ui_status_set_visible(1);
600 #endif
601
602         /* main window */
603         ui_window_create(&ui);
604
605         ui_enable_alpha_channel(&ui);
606
607         ret = pthread_create(&thread, NULL, update_measures, &ui);
608
609         if (ret)
610                 log_err(_("Failed to create thread for monitoring sensors"));
611
612         ui.graph_update_interval = ui.config->graph_update_interval;
613
614         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
615
616 #if defined(HAVE_APPINDICATOR)
617         ui_appindicator_init(&ui);
618 #endif
619
620         gdk_notify_startup_complete();
621
622         /*
623          * hack, did not find a cleaner solution.
624          * wait 30s to ensure that the status icon is attempted to be
625          * drawn before determining whether the main window must be
626          * show.
627          */
628         if  (ui.config->hide_on_startup)
629                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
630         else
631                 initial_window_show(&ui);
632
633         log_debug("translators: %s\n", _("translator-credits"));
634
635         /* main loop */
636         gtk_main();
637
638         g_object_unref(app);
639         cleanup(&ui);
640
641         log_debug("Quitting...");
642         log_close();
643
644         if (url)
645                 free(url);
646
647         return 0;
648 }