removed compat.h because gtk2 is no more supported since a while
[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
263         struct psensor **sensor_cur = sensors;
264         int i = 0;
265         while (*sensor_cur) {
266                 struct color default_color;
267                 color_set(&default_color,
268                           colors[i % COLORS_COUNT][0],
269                           colors[i % COLORS_COUNT][1],
270                           colors[i % COLORS_COUNT][2]);
271
272                 (*sensor_cur)->color
273                     = config_get_sensor_color((*sensor_cur)->id,
274                                               &default_color);
275
276                 sensor_cur++;
277                 i++;
278         }
279 }
280
281 static void
282 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
283 {
284         struct psensor **sensor_cur = sensors;
285         while (*sensor_cur) {
286                 struct psensor *s = *sensor_cur;
287
288                 s->cb_alarm_raised = cb_alarm_raised;
289                 s->cb_alarm_raised_data = ui;
290
291                 s->alarm_high_threshold
292                         = config_get_sensor_alarm_high_threshold(s->id);
293                 s->alarm_low_threshold
294                         = config_get_sensor_alarm_low_threshold(s->id);
295
296                 if (is_temp_type(s->type) || is_fan_type(s->type)) {
297                         s->alarm_enabled
298                             = config_get_sensor_alarm_enabled(s->id);
299                 } else {
300                         s->alarm_high_threshold = 0;
301                         s->alarm_enabled = 0;
302                 }
303
304                 sensor_cur++;
305         }
306 }
307
308 static void associate_preferences(struct psensor **sensors)
309 {
310         struct psensor **sensor_cur = sensors;
311         while (*sensor_cur) {
312                 char *n;
313                 struct psensor *s = *sensor_cur;
314
315                 s->enabled = config_is_sensor_enabled(s->id);
316
317                 n = config_get_sensor_name(s->id);
318
319                 if (n) {
320                         free(s->name);
321                         s->name = n;
322                 }
323
324                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
325
326                 sensor_cur++;
327         }
328 }
329
330 static void log_init()
331 {
332         char *home, *path, *dir;
333
334         home = getenv("HOME");
335
336         if (!home)
337                 return ;
338
339         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
340         sprintf(dir, "%s/%s", home, ".psensor");
341         mkdir(dir, 0777);
342
343         path = malloc(strlen(dir)+1+strlen("log")+1);
344         sprintf(path, "%s/%s", dir, "log");
345
346         log_open(path);
347
348         free(dir);
349         free(path);
350 }
351
352 static struct option long_options[] = {
353         {"use-libatasmart", no_argument, 0, 0},
354         {"version", no_argument, 0, 'v'},
355         {"help", no_argument, 0, 'h'},
356         {"url", required_argument, 0, 'u'},
357         {"debug", required_argument, 0, 'd'},
358         {"new-instance", no_argument, 0, 'n'},
359         {0, 0, 0, 0}
360 };
361
362 static gboolean initial_window_show(gpointer data)
363 {
364         struct ui_psensor *ui;
365
366         log_debug("initial_window_show()");
367
368         ui = (struct ui_psensor *)data;
369
370         log_debug("is_status_supported: %d", is_status_supported());
371         log_debug("is_appindicator_supported: %d",
372                    is_appindicator_supported());
373         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
374
375         if (!ui->config->hide_on_startup
376             || (!is_appindicator_supported() && !is_status_supported()))
377                 ui_window_show(ui);
378
379         ui_window_update(ui);
380
381         return FALSE;
382 }
383
384 static void log_glib_info()
385 {
386         log_debug("Compiled with GLib %d.%d.%d",
387                   GLIB_MAJOR_VERSION,
388                   GLIB_MINOR_VERSION,
389                   GLIB_MICRO_VERSION);
390
391         log_debug("Running with GLib %d.%d.%d",
392                   glib_major_version,
393                   glib_minor_version,
394                   glib_micro_version);
395 }
396
397 static void cb_activate(GApplication *application,
398                         gpointer data)
399 {
400         ui_window_show((struct ui_psensor *)data);
401 }
402
403 /*
404  * Release memory for Valgrind.
405  */
406 static void cleanup(struct ui_psensor *ui)
407 {
408         pthread_mutex_lock(&ui->sensors_mutex);
409
410         log_debug("Cleanup...");
411
412         psensor_cleanup();
413
414 #ifdef HAVE_NVIDIA
415         nvidia_cleanup();
416 #endif
417 #ifdef HAVE_LIBATIADL
418         amd_cleanup();
419 #endif
420 #ifdef HAVE_REMOTE_SUPPORT
421         rsensor_cleanup();
422 #endif
423
424         psensor_list_free(ui->sensors);
425         ui->sensors = NULL;
426
427 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
428         ui_appindicator_cleanup();
429 #endif
430
431         ui_status_cleanup();
432
433         pthread_mutex_unlock(&ui->sensors_mutex);
434
435         config_cleanup();
436
437         log_debug("Cleanup done, closing log");
438 }
439
440 /*
441  * Creates the list of sensors.
442  *
443  * 'url': remote psensor server url, null for local monitoring.
444  * 'use_libatasmart': whether the libatasmart must be used.
445  */
446 static struct psensor **create_sensors_list(const char *url,
447                                             unsigned int use_libatasmart)
448 {
449         struct psensor **sensors;
450
451         if (url) {
452 #ifdef HAVE_REMOTE_SUPPORT
453                 rsensor_init();
454                 sensors = get_remote_sensors(url, 600);
455 #else
456                 log_err(_("Psensor has not been compiled with remote "
457                           "sensor support."));
458                 exit(EXIT_FAILURE);
459 #endif
460         } else {
461                 sensors = get_all_sensors(use_libatasmart, 600);
462 #ifdef HAVE_NVIDIA
463                 sensors = nvidia_psensor_list_add(sensors, 600);
464 #endif
465 #ifdef HAVE_LIBATIADL
466                 sensors = amd_psensor_list_add(sensors, 600);
467 #endif
468 #ifdef HAVE_GTOP
469                 sensors = cpu_psensor_list_add(sensors, 600);
470 #endif
471         }
472
473         associate_preferences(sensors);
474         associate_colors(sensors);
475
476         return sensors;
477 }
478
479 int main(int argc, char **argv)
480 {
481         struct ui_psensor ui;
482         GError *error;
483         GThread *thread;
484         int optc, cmdok, opti, use_libatasmart, new_instance;
485         char *url = NULL;
486         GApplication *app;
487
488         program_name = argv[0];
489
490         setlocale(LC_ALL, "");
491
492 #if ENABLE_NLS
493         bindtextdomain(PACKAGE, LOCALEDIR);
494         textdomain(PACKAGE);
495 #endif
496
497         use_libatasmart = new_instance = 0;
498
499         cmdok = 1;
500         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
501                                    &opti)) != -1) {
502                 switch (optc) {
503                 case 0:
504                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
505                                 use_libatasmart = 1;
506                         break;
507                 case 'u':
508                         if (optarg)
509                                 url = strdup(optarg);
510                         break;
511                 case 'h':
512                         print_help();
513                         exit(EXIT_SUCCESS);
514                 case 'v':
515                         print_version();
516                         exit(EXIT_SUCCESS);
517                 case 'd':
518                         log_level = atoi(optarg);
519                         log_info(_("Enables debug mode."));
520                         break;
521                 case 'n':
522                         new_instance = 1;
523                         break;
524                 default:
525                         cmdok = 0;
526                         break;
527                 }
528         }
529
530         if (!cmdok || optind != argc) {
531                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
532                         program_name);
533                 exit(EXIT_FAILURE);
534         }
535
536         log_init();
537
538         app = g_application_new("wpitchoune.psensor", 0);
539
540         g_application_register(app, NULL, NULL);
541
542         if (!new_instance && g_application_get_is_remote(app)) {
543                 g_application_activate(app);
544                 log_warn(_("A Psensor instance already exists."));
545                 exit(EXIT_SUCCESS);
546         }
547
548         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
549
550         log_glib_info();
551 #if !(GLIB_CHECK_VERSION(2, 31, 0))
552         /*
553          * Since GLib 2.31 g_thread_init call is deprecated and not
554          * needed.
555          */
556         log_debug("Calling g_thread_init(NULL)");
557         g_thread_init(NULL);
558 #endif
559
560         gdk_threads_init();
561
562         gtk_init(NULL, NULL);
563
564         pthread_mutex_init(&ui.sensors_mutex, NULL);
565
566         ui.config = config_load();
567
568         psensor_init();
569
570         ui.sensors = create_sensors_list(url, use_libatasmart);
571         associate_cb_alarm_raised(ui.sensors, &ui);
572
573         if (ui.config->slog_enabled)
574                 slog_activate(NULL,
575                               ui.sensors,
576                               &ui.sensors_mutex,
577                               config_get_slog_interval());
578
579 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
580         ui_status_init(&ui);
581         ui_status_set_visible(1);
582 #endif
583
584         /* main window */
585         ui_window_create(&ui);
586
587         ui_enable_alpha_channel(&ui);
588
589         thread = g_thread_create((GThreadFunc) update_measures,
590                                  &ui, TRUE, &error);
591
592         if (!thread)
593                 g_error_free(error);
594
595         ui.graph_update_interval = ui.config->graph_update_interval;
596
597         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
598
599 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
600         ui_appindicator_init(&ui);
601 #endif
602
603         gdk_notify_startup_complete();
604
605         /*
606          * hack, did not find a cleaner solution.
607          * wait 30s to ensure that the status icon is attempted to be
608          * drawn before determining whether the main window must be
609          * show.
610          */
611         if  (ui.config->hide_on_startup)
612                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
613         else
614                 initial_window_show(&ui);
615
616         /* main loop */
617         gtk_main();
618
619         g_object_unref(app);
620         cleanup(&ui);
621
622         log_debug("Quitting...");
623         log_close();
624
625         if (url)
626                 free(url);
627
628         return 0;
629 }