Imported Upstream version 0.6.2.17
[psensor-pkg-debian.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 "ui_pref.h"
41 #include "ui_graph.h"
42 #include "ui_status.h"
43
44 #ifdef HAVE_UNITY
45 #include "ui_unity.h"
46 #endif
47
48 #ifdef HAVE_NVIDIA
49 #include "nvidia.h"
50 #endif
51
52 #ifdef HAVE_LIBATIADL
53 #include "amd.h"
54 #endif
55
56 #ifdef HAVE_REMOTE_SUPPORT
57 #include "rsensor.h"
58 #endif
59
60 #include "ui_appindicator.h"
61
62 #ifdef HAVE_LIBNOTIFY
63 #include "ui_notify.h"
64 #endif
65
66 #ifdef HAVE_GTOP
67 #include "cpu.h"
68 #endif
69
70 #include "compat.h"
71
72 static const char *program_name;
73
74 static void print_version()
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-2012");
84 }
85
86 static void print_help()
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(_("  -u, --url=URL       "
101                "the URL of the psensor-server, example: http://hostname:3131"));
102
103         puts("");
104
105         puts(_("  -d, --debug=LEVEL   "
106                "set the debug level, integer between 0 and 3"));
107
108         puts("");
109
110         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
111         puts("");
112         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
113 }
114
115 /*
116   Updates the size of the sensor values if different than the
117   configuration.
118  */
119 void
120 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
121 {
122         struct psensor **cur;
123
124         cur = sensors;
125         while (*cur) {
126                 struct psensor *s = *cur;
127
128                 if (s->values_max_length != cfg->sensor_values_max_length)
129                         psensor_values_resize(s,
130                                               cfg->sensor_values_max_length);
131
132                 cur++;
133         }
134 }
135
136 static void log_measures(struct psensor **sensors)
137 {
138         if (log_level == LOG_DEBUG)
139                 while (*sensors) {
140                         log_debug("Measure: %s %.2f",
141                                    (*sensors)->name,
142                                    psensor_get_current_value(*sensors));
143
144                         sensors++;
145                 }
146 }
147
148 void update_psensor_measures(struct ui_psensor *ui)
149 {
150         struct psensor **sensors = ui->sensors;
151         struct config *cfg = ui->config;
152
153         while (1) {
154                 g_mutex_lock(ui->sensors_mutex);
155
156                 if (!sensors)
157                         return;
158
159                 update_psensor_values_size(sensors, ui->config);
160
161                 psensor_list_update_measures(sensors);
162 #ifdef HAVE_REMOTE_SUPPORT
163                 remote_psensor_list_update(sensors);
164 #endif
165 #ifdef HAVE_NVIDIA
166                 nvidia_psensor_list_update(sensors);
167 #endif
168 #ifdef HAVE_LIBATIADL
169                 amd_psensor_list_update(sensors);
170 #endif
171
172                 log_measures(sensors);
173
174                 g_mutex_unlock(ui->sensors_mutex);
175
176                 sleep(cfg->sensor_update_interval);
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);
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 #endif
227
228         if (ui->graph_update_interval != cfg->graph_update_interval) {
229                 ui->graph_update_interval = cfg->graph_update_interval;
230                 ret = FALSE;
231         }
232
233         g_mutex_unlock(ui->sensors_mutex);
234
235         if (ret == FALSE)
236                 g_timeout_add(1000 * ui->graph_update_interval,
237                               ui_refresh_thread, ui);
238
239         return ret;
240 }
241
242 void cb_alarm_raised(struct psensor *sensor, void *data)
243 {
244 #ifdef HAVE_LIBNOTIFY
245         if (sensor->enabled)
246                 ui_notify(sensor, (struct ui_psensor *)data);
247 #endif
248 }
249
250 static void associate_colors(struct psensor **sensors)
251 {
252         /* number of uniq colors */
253 #define COLORS_COUNT 8
254
255         unsigned int colors[COLORS_COUNT][3] = {
256                 {0x0000, 0x0000, 0x0000},       /* black */
257                 {0xffff, 0x0000, 0x0000},       /* red */
258                 {0x0000, 0.0000, 0xffff},       /* blue */
259                 {0x0000, 0xffff, 0x0000},       /* green */
260
261                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
262                 {0x7fff, 0x0000, 0x0000},       /* dark red */
263                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
264                 {0x0000, 0x7fff, 0x0000}        /* dark green */
265         };
266
267         struct psensor **sensor_cur = sensors;
268         int i = 0;
269         while (*sensor_cur) {
270                 struct color default_color;
271                 color_set(&default_color,
272                           colors[i % COLORS_COUNT][0],
273                           colors[i % COLORS_COUNT][1],
274                           colors[i % COLORS_COUNT][2]);
275
276                 (*sensor_cur)->color
277                     = config_get_sensor_color((*sensor_cur)->id,
278                                               &default_color);
279
280                 sensor_cur++;
281                 i++;
282         }
283 }
284
285 static void
286 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
287 {
288         struct psensor **sensor_cur = sensors;
289         while (*sensor_cur) {
290                 struct psensor *s = *sensor_cur;
291
292                 s->cb_alarm_raised = cb_alarm_raised;
293                 s->cb_alarm_raised_data = ui;
294
295                 if (is_temp_type(s->type)) {
296                         s->alarm_limit
297                             = config_get_sensor_alarm_limit(s->id, 60);
298                         s->alarm_enabled
299                             = config_get_sensor_alarm_enabled(s->id);
300                 } else {
301                         s->alarm_limit = 0;
302                         s->alarm_enabled = 0;
303                 }
304
305                 sensor_cur++;
306         }
307 }
308
309 static void associate_preferences(struct psensor **sensors)
310 {
311         struct psensor **sensor_cur = sensors;
312         while (*sensor_cur) {
313                 char *n;
314                 struct psensor *s = *sensor_cur;
315
316                 s->enabled = config_is_sensor_enabled(s->id);
317
318                 n = config_get_sensor_name(s->id);
319
320                 if (n)
321                         s->name = n;
322
323                 sensor_cur++;
324         }
325 }
326
327 static void log_init()
328 {
329         char *home, *path, *dir;
330
331         home = getenv("HOME");
332
333         if (!home)
334                 return ;
335
336         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
337         sprintf(dir, "%s/%s", home, ".psensor");
338         mkdir(dir, 0777);
339
340         path = malloc(strlen(dir)+1+strlen("log")+1);
341         sprintf(path, "%s/%s", dir, "log");
342
343         log_open(path);
344
345         free(dir);
346         free(path);
347 }
348
349 static struct option long_options[] = {
350         {"version", no_argument, 0, 'v'},
351         {"help", no_argument, 0, 'h'},
352         {"url", required_argument, 0, 'u'},
353         {"debug", required_argument, 0, 'd'},
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 int main(int argc, char **argv)
393 {
394         struct ui_psensor ui;
395         GError *error;
396         GThread *thread;
397         int optc, cmdok;
398         char *url = NULL;
399
400         program_name = argv[0];
401
402         setlocale(LC_ALL, "");
403
404 #if ENABLE_NLS
405         bindtextdomain(PACKAGE, LOCALEDIR);
406         textdomain(PACKAGE);
407 #endif
408
409         cmdok = 1;
410         while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
411                                    NULL)) != -1) {
412                 switch (optc) {
413                 case 'u':
414                         if (optarg)
415                                 url = strdup(optarg);
416                         break;
417                 case 'h':
418                         print_help();
419                         exit(EXIT_SUCCESS);
420                 case 'v':
421                         print_version();
422                         exit(EXIT_SUCCESS);
423                 case 'd':
424                         log_level = atoi(optarg);
425                         log_printf(LOG_INFO, _("Enables debug mode."));
426                         break;
427                 default:
428                         cmdok = 0;
429                         break;
430                 }
431         }
432
433         if (!cmdok || optind != argc) {
434                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
435                         program_name);
436                 exit(EXIT_FAILURE);
437         }
438
439         log_init();
440
441         log_glib_info();
442 #if !(GLIB_CHECK_VERSION(2, 31, 0))
443         /*
444          * Since GLib 2.31 g_thread_init call is deprecated and not
445          * needed.
446          */
447         log_debug("Calling g_thread_init(NULL)");
448         g_thread_init(NULL);
449 #endif
450
451         gdk_threads_init();
452
453         gtk_init(NULL, NULL);
454
455         ui.sensors_mutex = g_mutex_new();
456
457         config_init();
458
459         ui.config = config_load();
460
461         psensor_init();
462
463         if (url) {
464 #ifdef HAVE_REMOTE_SUPPORT
465                 rsensor_init();
466                 ui.sensors = get_remote_sensors(url, 600);
467 #else
468                 fprintf(stderr,
469                         _("ERROR: Not compiled with remote sensor support.\n"));
470                 exit(EXIT_FAILURE);
471 #endif
472         } else {
473                 ui.sensors = get_all_sensors(600);
474 #ifdef HAVE_NVIDIA
475                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
476 #endif
477 #ifdef HAVE_LIBATIADL
478                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
479 #endif
480 #ifdef HAVE_GTOP
481                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
482 #endif
483         }
484
485         associate_preferences(ui.sensors);
486         associate_colors(ui.sensors);
487         associate_cb_alarm_raised(ui.sensors, &ui);
488
489 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
490         ui_status_init(&ui);
491 #endif
492
493         /* main window */
494         ui_window_create(&ui);
495         ui.sensor_box = NULL;
496
497         /* drawing box */
498         ui.w_graph = ui_graph_create(&ui);
499
500         /* sensor list */
501         ui_sensorlist_create(&ui);
502
503         thread = g_thread_create((GThreadFunc) update_psensor_measures,
504                                  &ui, TRUE, &error);
505
506         if (!thread)
507                 g_error_free(error);
508
509         ui.graph_update_interval = ui.config->graph_update_interval;
510
511         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
512
513 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
514         ui_appindicator_init(&ui);
515 #endif
516
517         /*
518          * show the window as soon as all gtk events have been processed
519          * in order to ensure that the status icon is attempted to be
520          * drawn before. If not, there is no way to detect that it is
521          * visible.
522         */
523         g_idle_add((GSourceFunc)initial_window_show, &ui);
524
525         gdk_notify_startup_complete();
526
527         /* main loop */
528         gtk_main();
529
530         g_mutex_lock(ui.sensors_mutex);
531
532         psensor_cleanup();
533
534 #ifdef HAVE_NVIDIA
535         nvidia_cleanup();
536 #endif
537 #ifdef HAVE_LIBATIADL
538         amd_cleanup();
539 #endif
540 #ifdef HAVE_REMOTE_SUPPORT
541         rsensor_cleanup();
542 #endif
543
544         psensor_list_free(ui.sensors);
545         ui.sensors = NULL;
546
547 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
548         ui_appindicator_cleanup();
549 #endif
550
551         ui_status_cleanup();
552
553         g_mutex_unlock(ui.sensors_mutex);
554
555         config_cleanup();
556
557         log_close();
558
559         return 0;
560 }