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