fixed LP971098 : fixed systray and application indicator both visible when unity...
[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 "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         puts(_("  --use-libatasmart   "
103                "use atasmart library for disk monitoring "
104                "instead of hddtemp daemon"));
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 void
122 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
123 {
124         struct psensor **cur;
125
126         cur = sensors;
127         while (*cur) {
128                 struct psensor *s = *cur;
129
130                 if (s->values_max_length != cfg->sensor_values_max_length)
131                         psensor_values_resize(s,
132                                               cfg->sensor_values_max_length);
133
134                 cur++;
135         }
136 }
137
138 static void log_measures(struct psensor **sensors)
139 {
140         if (log_level == LOG_DEBUG)
141                 while (*sensors) {
142                         log_debug("Measure: %s %.2f",
143                                    (*sensors)->name,
144                                    psensor_get_current_value(*sensors));
145
146                         sensors++;
147                 }
148 }
149
150 void update_psensor_measures(struct ui_psensor *ui)
151 {
152         struct psensor **sensors = ui->sensors;
153         struct config *cfg = ui->config;
154
155         while (1) {
156                 g_mutex_lock(ui->sensors_mutex);
157
158                 if (!sensors)
159                         return;
160
161                 update_psensor_values_size(sensors, ui->config);
162
163                 psensor_list_update_measures(sensors);
164 #ifdef HAVE_REMOTE_SUPPORT
165                 remote_psensor_list_update(sensors);
166 #endif
167 #ifdef HAVE_NVIDIA
168                 nvidia_psensor_list_update(sensors);
169 #endif
170 #ifdef HAVE_LIBATIADL
171                 amd_psensor_list_update(sensors);
172 #endif
173
174                 log_measures(sensors);
175
176                 g_mutex_unlock(ui->sensors_mutex);
177
178                 sleep(cfg->sensor_update_interval);
179         }
180 }
181
182 static void indicators_update(struct ui_psensor *ui)
183 {
184         struct psensor **sensor_cur = ui->sensors;
185         unsigned int attention = 0;
186
187         while (*sensor_cur) {
188                 struct psensor *s = *sensor_cur;
189
190                 if (s->alarm_enabled && s->alarm_raised) {
191                         attention = 1;
192                         break;
193                 }
194
195                 sensor_cur++;
196         }
197
198 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
199         if (is_appindicator_supported())
200                 ui_appindicator_update(ui, attention);
201 #endif
202
203         if (is_status_supported())
204                 ui_status_update(ui, attention);
205 }
206
207 gboolean ui_refresh_thread(gpointer data)
208 {
209         struct config *cfg;
210         gboolean ret;
211         struct ui_psensor *ui = (struct ui_psensor *)data;
212
213         ret = TRUE;
214         cfg = ui->config;
215
216         g_mutex_lock(ui->sensors_mutex);
217
218         graph_update(ui->sensors, ui->w_graph, ui->config);
219
220         ui_sensorlist_update(ui);
221
222         if (is_appindicator_supported() || is_status_supported())
223                 indicators_update(ui);
224
225 #ifdef HAVE_UNITY
226         ui_unity_launcher_entry_update(ui->sensors,
227                                        !cfg->unity_launcher_count_disabled);
228 #endif
229
230         if (ui->graph_update_interval != cfg->graph_update_interval) {
231                 ui->graph_update_interval = cfg->graph_update_interval;
232                 ret = FALSE;
233         }
234
235         g_mutex_unlock(ui->sensors_mutex);
236
237         if (ret == FALSE)
238                 g_timeout_add(1000 * ui->graph_update_interval,
239                               ui_refresh_thread, ui);
240
241         return ret;
242 }
243
244 void cb_alarm_raised(struct psensor *sensor, void *data)
245 {
246 #ifdef HAVE_LIBNOTIFY
247         if (sensor->enabled)
248                 ui_notify(sensor, (struct ui_psensor *)data);
249 #endif
250 }
251
252 static void associate_colors(struct psensor **sensors)
253 {
254         /* number of uniq colors */
255 #define COLORS_COUNT 8
256
257         unsigned int colors[COLORS_COUNT][3] = {
258                 {0x0000, 0x0000, 0x0000},       /* black */
259                 {0xffff, 0x0000, 0x0000},       /* red */
260                 {0x0000, 0.0000, 0xffff},       /* blue */
261                 {0x0000, 0xffff, 0x0000},       /* green */
262
263                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
264                 {0x7fff, 0x0000, 0x0000},       /* dark red */
265                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
266                 {0x0000, 0x7fff, 0x0000}        /* dark green */
267         };
268
269         struct psensor **sensor_cur = sensors;
270         int i = 0;
271         while (*sensor_cur) {
272                 struct color default_color;
273                 color_set(&default_color,
274                           colors[i % COLORS_COUNT][0],
275                           colors[i % COLORS_COUNT][1],
276                           colors[i % COLORS_COUNT][2]);
277
278                 (*sensor_cur)->color
279                     = config_get_sensor_color((*sensor_cur)->id,
280                                               &default_color);
281
282                 sensor_cur++;
283                 i++;
284         }
285 }
286
287 static void
288 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
289 {
290         struct psensor **sensor_cur = sensors;
291         while (*sensor_cur) {
292                 struct psensor *s = *sensor_cur;
293
294                 s->cb_alarm_raised = cb_alarm_raised;
295                 s->cb_alarm_raised_data = ui;
296
297                 if (is_temp_type(s->type)) {
298                         s->alarm_limit
299                             = config_get_sensor_alarm_limit(s->id, 60);
300                         s->alarm_enabled
301                             = config_get_sensor_alarm_enabled(s->id);
302                 } else {
303                         s->alarm_limit = 0;
304                         s->alarm_enabled = 0;
305                 }
306
307                 sensor_cur++;
308         }
309 }
310
311 static void associate_preferences(struct psensor **sensors)
312 {
313         struct psensor **sensor_cur = sensors;
314         while (*sensor_cur) {
315                 char *n;
316                 struct psensor *s = *sensor_cur;
317
318                 s->enabled = config_is_sensor_enabled(s->id);
319
320                 n = config_get_sensor_name(s->id);
321
322                 if (n)
323                         s->name = n;
324
325                 sensor_cur++;
326         }
327 }
328
329 static void log_init()
330 {
331         char *home, *path, *dir;
332
333         home = getenv("HOME");
334
335         if (!home)
336                 return ;
337
338         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
339         sprintf(dir, "%s/%s", home, ".psensor");
340         mkdir(dir, 0777);
341
342         path = malloc(strlen(dir)+1+strlen("log")+1);
343         sprintf(path, "%s/%s", dir, "log");
344
345         log_open(path);
346
347         free(dir);
348         free(path);
349 }
350
351 static struct option long_options[] = {
352         {"use-libatasmart", no_argument, 0, 0},
353         {"version", no_argument, 0, 'v'},
354         {"help", no_argument, 0, 'h'},
355         {"url", required_argument, 0, 'u'},
356         {"debug", required_argument, 0, 'd'},
357         {0, 0, 0, 0}
358 };
359
360 static gboolean initial_window_show(gpointer data)
361 {
362         struct ui_psensor *ui;
363
364         log_debug("initial_window_show()");
365
366         ui = (struct ui_psensor *)data;
367
368         log_debug("is_status_supported: %d", is_status_supported());
369         log_debug("is_appindicator_supported: %d",
370                    is_appindicator_supported());
371         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
372
373         if (!ui->config->hide_on_startup
374             || (!is_appindicator_supported() && !is_status_supported()))
375                 ui_window_show(ui);
376
377         ui_window_update(ui);
378
379         return FALSE;
380 }
381
382 static void log_glib_info()
383 {
384         log_debug("Compiled with GLib %d.%d.%d",
385                   GLIB_MAJOR_VERSION,
386                   GLIB_MINOR_VERSION,
387                   GLIB_MICRO_VERSION);
388
389         log_debug("Running with GLib %d.%d.%d",
390                   glib_major_version,
391                   glib_minor_version,
392                   glib_micro_version);
393 }
394
395 int main(int argc, char **argv)
396 {
397         struct ui_psensor ui;
398         GError *error;
399         GThread *thread;
400         int optc, cmdok, opti, use_libatasmart;
401         char *url = NULL;
402
403         program_name = argv[0];
404
405         setlocale(LC_ALL, "");
406
407 #if ENABLE_NLS
408         bindtextdomain(PACKAGE, LOCALEDIR);
409         textdomain(PACKAGE);
410 #endif
411
412         use_libatasmart = 0;
413
414         cmdok = 1;
415         while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
416                                    &opti)) != -1) {
417                 switch (optc) {
418                 case 0:
419                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
420                                 use_libatasmart = 1;
421                         break;
422                 case 'u':
423                         if (optarg)
424                                 url = strdup(optarg);
425                         break;
426                 case 'h':
427                         print_help();
428                         exit(EXIT_SUCCESS);
429                 case 'v':
430                         print_version();
431                         exit(EXIT_SUCCESS);
432                 case 'd':
433                         log_level = atoi(optarg);
434                         log_printf(LOG_INFO, _("Enables debug mode."));
435                         break;
436                 default:
437                         cmdok = 0;
438                         break;
439                 }
440         }
441
442         if (!cmdok || optind != argc) {
443                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
444                         program_name);
445                 exit(EXIT_FAILURE);
446         }
447
448         log_init();
449
450         log_glib_info();
451 #if !(GLIB_CHECK_VERSION(2, 31, 0))
452         /*
453          * Since GLib 2.31 g_thread_init call is deprecated and not
454          * needed.
455          */
456         log_debug("Calling g_thread_init(NULL)");
457         g_thread_init(NULL);
458 #endif
459
460         gdk_threads_init();
461
462         gtk_init(NULL, NULL);
463
464         ui.sensors_mutex = g_mutex_new();
465
466         config_init();
467
468         ui.config = config_load();
469
470         psensor_init();
471
472         if (url) {
473 #ifdef HAVE_REMOTE_SUPPORT
474                 rsensor_init();
475                 ui.sensors = get_remote_sensors(url, 600);
476 #else
477                 fprintf(stderr,
478                         _("ERROR: Not compiled with remote sensor support.\n"));
479                 exit(EXIT_FAILURE);
480 #endif
481         } else {
482                 ui.sensors = get_all_sensors(use_libatasmart, 600);
483 #ifdef HAVE_NVIDIA
484                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
485 #endif
486 #ifdef HAVE_LIBATIADL
487                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
488 #endif
489 #ifdef HAVE_GTOP
490                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
491 #endif
492         }
493
494         associate_preferences(ui.sensors);
495         associate_colors(ui.sensors);
496         associate_cb_alarm_raised(ui.sensors, &ui);
497
498 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
499         ui_status_init(&ui);
500         ui_status_set_visible(1);
501 #endif
502
503         /* main window */
504         ui_window_create(&ui);
505         ui.sensor_box = NULL;
506
507         /* drawing box */
508         ui.w_graph = ui_graph_create(&ui);
509
510         /* sensor list */
511         ui_sensorlist_create(&ui);
512
513         thread = g_thread_create((GThreadFunc) update_psensor_measures,
514                                  &ui, TRUE, &error);
515
516         if (!thread)
517                 g_error_free(error);
518
519         ui.graph_update_interval = ui.config->graph_update_interval;
520
521         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
522
523 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
524         ui_appindicator_init(&ui);
525 #endif
526
527         /*
528          * show the window as soon as all gtk events have been processed
529          * in order to ensure that the status icon is attempted to be
530          * drawn before. If not, there is no way to detect that it is
531          * visible.
532         */
533         g_idle_add((GSourceFunc)initial_window_show, &ui);
534
535         gdk_notify_startup_complete();
536
537         /* main loop */
538         gtk_main();
539
540         g_mutex_lock(ui.sensors_mutex);
541
542         psensor_cleanup();
543
544 #ifdef HAVE_NVIDIA
545         nvidia_cleanup();
546 #endif
547 #ifdef HAVE_LIBATIADL
548         amd_cleanup();
549 #endif
550 #ifdef HAVE_REMOTE_SUPPORT
551         rsensor_cleanup();
552 #endif
553
554         psensor_list_free(ui.sensors);
555         ui.sensors = NULL;
556
557 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
558         ui_appindicator_cleanup();
559 #endif
560
561         ui_status_cleanup();
562
563         g_mutex_unlock(ui.sensors_mutex);
564
565         config_cleanup();
566
567         log_close();
568
569         return 0;
570 }