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