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