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