code style
[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(_(
101 "  -u, --url=URL       the URL of the psensor-server,\n"
102 "                      example: http://hostname:3131"));
103         puts(_(
104 "  --use-libatasmart   use atasmart library for disk monitoring instead of\n"
105 "                      hddtemp daemon"));
106         puts(_(
107 "  -n, --new-instance  force the creation of a new Psensor application"));
108         puts("");
109
110         puts(_("  -d, --debug=LEVEL   "
111                "set the debug level, integer between 0 and 3"));
112
113         puts("");
114
115         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
116         puts("");
117         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
118 }
119
120 /*
121  * Updates the size of the sensor values if different than the
122  * configuration.
123  */
124 static void
125 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
126 {
127         struct psensor **cur, *s;
128
129         for (cur = sensors; *cur; cur++) {
130                 s = *cur;
131
132                 if (s->values_max_length != cfg->sensor_values_max_length)
133                         psensor_values_resize(s,
134                                               cfg->sensor_values_max_length);
135         }
136 }
137
138 static void update_measures(struct ui_psensor *ui)
139 {
140         struct psensor **sensors;
141         struct config *cfg;
142         int period;
143
144         cfg = ui->config;
145
146         while (1) {
147                 g_mutex_lock(ui->sensors_mutex);
148
149                 sensors = ui->sensors;
150                 if (!sensors)
151                         return;
152
153                 update_psensor_values_size(sensors, cfg);
154
155                 psensor_list_update_measures(sensors);
156 #ifdef HAVE_REMOTE_SUPPORT
157                 remote_psensor_list_update(sensors);
158 #endif
159 #ifdef HAVE_NVIDIA
160                 nvidia_psensor_list_update(sensors);
161 #endif
162 #ifdef HAVE_LIBATIADL
163                 amd_psensor_list_update(sensors);
164 #endif
165
166                 psensor_log_measures(sensors);
167
168                 period = cfg->sensor_update_interval;
169
170                 g_mutex_unlock(ui->sensors_mutex);
171
172                 sleep(period);
173         }
174 }
175
176 static void indicators_update(struct ui_psensor *ui)
177 {
178         struct psensor **sensor_cur = ui->sensors;
179         unsigned int attention = 0;
180
181         while (*sensor_cur) {
182                 struct psensor *s = *sensor_cur;
183
184                 if (s->alarm_enabled && s->alarm_raised) {
185                         attention = 1;
186                         break;
187                 }
188
189                 sensor_cur++;
190         }
191
192 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
193         if (is_appindicator_supported())
194                 ui_appindicator_update(ui, attention);
195 #endif
196
197         if (is_status_supported())
198                 ui_status_update(ui, attention);
199 }
200
201 gboolean ui_refresh_thread(gpointer data)
202 {
203         struct config *cfg;
204         gboolean ret;
205         struct ui_psensor *ui = (struct ui_psensor *)data;
206
207         ret = TRUE;
208         cfg = ui->config;
209
210         g_mutex_lock(ui->sensors_mutex);
211
212         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
213
214         ui_sensorlist_update(ui);
215
216         if (is_appindicator_supported() || is_status_supported())
217                 indicators_update(ui);
218
219 #ifdef HAVE_UNITY
220         ui_unity_launcher_entry_update(ui->sensors,
221                                        !cfg->unity_launcher_count_disabled,
222                                        cfg->temperature_unit == CELCIUS);
223 #endif
224
225         if (ui->graph_update_interval != cfg->graph_update_interval) {
226                 ui->graph_update_interval = cfg->graph_update_interval;
227                 ret = FALSE;
228         }
229
230         g_mutex_unlock(ui->sensors_mutex);
231
232         if (ret == FALSE)
233                 g_timeout_add(1000 * ui->graph_update_interval,
234                               ui_refresh_thread, ui);
235
236         return ret;
237 }
238
239 static void cb_alarm_raised(struct psensor *sensor, void *data)
240 {
241 #ifdef HAVE_LIBNOTIFY
242         if (sensor->alarm_enabled)
243                 ui_notify(sensor, (struct ui_psensor *)data);
244 #endif
245 }
246
247 static void associate_colors(struct psensor **sensors)
248 {
249         /* number of uniq colors */
250 #define COLORS_COUNT 8
251
252         unsigned int colors[COLORS_COUNT][3] = {
253                 {0x0000, 0x0000, 0x0000},       /* black */
254                 {0xffff, 0x0000, 0x0000},       /* red */
255                 {0x0000, 0x0000, 0xffff},       /* blue */
256                 {0x0000, 0xffff, 0x0000},       /* green */
257
258                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
259                 {0x7fff, 0x0000, 0x0000},       /* dark red */
260                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
261                 {0x0000, 0x7fff, 0x0000}        /* dark green */
262         };
263
264         struct psensor **sensor_cur = sensors;
265         int i = 0;
266         while (*sensor_cur) {
267                 struct color default_color;
268                 color_set(&default_color,
269                           colors[i % COLORS_COUNT][0],
270                           colors[i % COLORS_COUNT][1],
271                           colors[i % COLORS_COUNT][2]);
272
273                 (*sensor_cur)->color
274                     = config_get_sensor_color((*sensor_cur)->id,
275                                               &default_color);
276
277                 sensor_cur++;
278                 i++;
279         }
280 }
281
282 static void
283 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
284 {
285         struct psensor **sensor_cur = sensors;
286         while (*sensor_cur) {
287                 struct psensor *s = *sensor_cur;
288
289                 s->cb_alarm_raised = cb_alarm_raised;
290                 s->cb_alarm_raised_data = ui;
291
292                 s->alarm_high_threshold
293                         = config_get_sensor_alarm_high_threshold(s->id);
294                 s->alarm_low_threshold
295                         = config_get_sensor_alarm_low_threshold(s->id);
296
297                 if (is_temp_type(s->type) || is_fan_type(s->type)) {
298                         s->alarm_enabled
299                             = config_get_sensor_alarm_enabled(s->id);
300                 } else {
301                         s->alarm_high_threshold = 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                         free(s->name);
322                         s->name = n;
323                 }
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         {"new-instance", no_argument, 0, 'n'},
358         {0, 0, 0, 0}
359 };
360
361 static gboolean initial_window_show(gpointer data)
362 {
363         struct ui_psensor *ui;
364
365         log_debug("initial_window_show()");
366
367         ui = (struct ui_psensor *)data;
368
369         log_debug("is_status_supported: %d", is_status_supported());
370         log_debug("is_appindicator_supported: %d",
371                    is_appindicator_supported());
372         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
373
374         if (!ui->config->hide_on_startup
375             || (!is_appindicator_supported() && !is_status_supported()))
376                 ui_window_show(ui);
377
378         ui_window_update(ui);
379
380         return FALSE;
381 }
382
383 static void log_glib_info()
384 {
385         log_debug("Compiled with GLib %d.%d.%d",
386                   GLIB_MAJOR_VERSION,
387                   GLIB_MINOR_VERSION,
388                   GLIB_MICRO_VERSION);
389
390         log_debug("Running with GLib %d.%d.%d",
391                   glib_major_version,
392                   glib_minor_version,
393                   glib_micro_version);
394 }
395
396 static void cb_activate(GApplication *application,
397                         gpointer data)
398 {
399         ui_window_show((struct ui_psensor *)data);
400 }
401
402 /*
403  * Release memory for Valgrind.
404  */
405 static void cleanup(struct ui_psensor *ui)
406 {
407         g_mutex_lock(ui->sensors_mutex);
408
409         log_debug("Cleanup...");
410
411         psensor_cleanup();
412
413 #ifdef HAVE_NVIDIA
414         nvidia_cleanup();
415 #endif
416 #ifdef HAVE_LIBATIADL
417         amd_cleanup();
418 #endif
419 #ifdef HAVE_REMOTE_SUPPORT
420         rsensor_cleanup();
421 #endif
422
423         psensor_list_free(ui->sensors);
424         ui->sensors = NULL;
425
426 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
427         ui_appindicator_cleanup();
428 #endif
429
430         ui_status_cleanup();
431
432         g_mutex_unlock(ui->sensors_mutex);
433
434         config_cleanup();
435
436         log_debug("Cleanup done, closing log");
437 }
438
439 /*
440  * Creates the list of sensors.
441  *
442  * 'url': remote psensor server url, null for local monitoring.
443  * 'use_libatasmart': whether the libatasmart must be used.
444  */
445 static struct psensor **create_sensors_list(const char *url,
446                                             unsigned int use_libatasmart)
447 {
448         struct psensor **sensors;
449
450         if (url) {
451 #ifdef HAVE_REMOTE_SUPPORT
452                 rsensor_init();
453                 sensors = get_remote_sensors(url, 600);
454 #else
455                 log_err(_("Psensor has not been compiled with remote "
456                           "sensor support."));
457                 exit(EXIT_FAILURE);
458 #endif
459         } else {
460                 sensors = get_all_sensors(use_libatasmart, 600);
461 #ifdef HAVE_NVIDIA
462                 sensors = nvidia_psensor_list_add(sensors, 600);
463 #endif
464 #ifdef HAVE_LIBATIADL
465                 sensors = amd_psensor_list_add(sensors, 600);
466 #endif
467 #ifdef HAVE_GTOP
468                 sensors = cpu_psensor_list_add(sensors, 600);
469 #endif
470         }
471
472         associate_preferences(sensors);
473         associate_colors(sensors);
474
475         return sensors;
476 }
477
478 int main(int argc, char **argv)
479 {
480         struct ui_psensor ui;
481         GError *error;
482         GThread *thread;
483         int optc, cmdok, opti, use_libatasmart, new_instance;
484         char *url = NULL;
485         GApplication *app;
486
487         program_name = argv[0];
488
489         setlocale(LC_ALL, "");
490
491 #if ENABLE_NLS
492         bindtextdomain(PACKAGE, LOCALEDIR);
493         textdomain(PACKAGE);
494 #endif
495
496         use_libatasmart = new_instance = 0;
497
498         cmdok = 1;
499         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
500                                    &opti)) != -1) {
501                 switch (optc) {
502                 case 0:
503                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
504                                 use_libatasmart = 1;
505                         break;
506                 case 'u':
507                         if (optarg)
508                                 url = strdup(optarg);
509                         break;
510                 case 'h':
511                         print_help();
512                         exit(EXIT_SUCCESS);
513                 case 'v':
514                         print_version();
515                         exit(EXIT_SUCCESS);
516                 case 'd':
517                         log_level = atoi(optarg);
518                         log_printf(LOG_INFO, _("Enables debug mode."));
519                         break;
520                 case 'n':
521                         new_instance = 1;
522                         break;
523                 default:
524                         cmdok = 0;
525                         break;
526                 }
527         }
528
529         if (!cmdok || optind != argc) {
530                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
531                         program_name);
532                 exit(EXIT_FAILURE);
533         }
534
535         log_init();
536
537         app = g_application_new("wpitchoune.psensor", 0);
538
539         g_application_register(app, NULL, NULL);
540
541         if (!new_instance && g_application_get_is_remote(app)) {
542                 g_application_activate(app);
543                 log_warn(_("A Psensor instance already exists."));
544                 exit(EXIT_SUCCESS);
545         }
546
547         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
548
549         log_glib_info();
550 #if !(GLIB_CHECK_VERSION(2, 31, 0))
551         /*
552          * Since GLib 2.31 g_thread_init call is deprecated and not
553          * needed.
554          */
555         log_debug("Calling g_thread_init(NULL)");
556         g_thread_init(NULL);
557 #endif
558
559         gdk_threads_init();
560
561         gtk_init(NULL, NULL);
562
563         ui.sensors_mutex = g_mutex_new();
564
565         ui.config = config_load();
566
567         psensor_init();
568
569         ui.sensors = create_sensors_list(url, use_libatasmart);
570         associate_cb_alarm_raised(ui.sensors, &ui);
571
572 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
573         ui_status_init(&ui);
574         ui_status_set_visible(1);
575 #endif
576
577         /* main window */
578         ui_window_create(&ui);
579         ui.sensor_box = NULL;
580
581         /* drawing box */
582         ui.w_graph = ui_graph_create(&ui);
583
584         ui_enable_alpha_channel(&ui);
585
586         /* sensor list */
587         ui_sensorlist_create(&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 2s to ensure that the status icon is attempted to be
608          * drawn before determining whether the main window must be
609          * show.
610          */
611         g_timeout_add(2000, (GSourceFunc)initial_window_show, &ui);
612
613         /* main loop */
614         gtk_main();
615
616         g_object_ref(app);
617         cleanup(&ui);
618
619         log_debug("Quitting...");
620         log_close();
621
622         return 0;
623 }