ede84e6f104b94a551ebc64658c61d6296c68045
[psensor-pkg-ubuntu.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2013 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 "notify_cmd.h"
41 #include "slog.h"
42 #include "ui_pref.h"
43 #include "ui_graph.h"
44 #include "ui_status.h"
45
46 #ifdef HAVE_UNITY
47 #include "ui_unity.h"
48 #endif
49
50 #ifdef HAVE_NVIDIA
51 #include "nvidia.h"
52 #endif
53
54 #ifdef HAVE_LIBATIADL
55 #include "amd.h"
56 #endif
57
58 #ifdef HAVE_REMOTE_SUPPORT
59 #include "rsensor.h"
60 #endif
61
62 #include "ui_appindicator.h"
63
64 #ifdef HAVE_LIBNOTIFY
65 #include "ui_notify.h"
66 #endif
67
68 #ifdef HAVE_GTOP
69 #include "cpu.h"
70 #endif
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-2013");
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                 pthread_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                 pthread_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         pthread_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, 0);
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         pthread_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         notify_cmd(sensor);
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         struct psensor **cur;
266         int i;
267         struct color c;
268
269         for (cur = sensors, i = 0; *cur; cur++) {
270                 color_set(&c,
271                           colors[i % COLORS_COUNT][0],
272                           colors[i % COLORS_COUNT][1],
273                           colors[i % COLORS_COUNT][2]);
274
275                 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
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                 s->alarm_high_threshold
290                         = config_get_sensor_alarm_high_threshold(s->id);
291                 s->alarm_low_threshold
292                         = config_get_sensor_alarm_low_threshold(s->id);
293
294                 if (is_temp_type(s->type) || is_fan_type(s->type)) {
295                         s->alarm_enabled
296                             = config_get_sensor_alarm_enabled(s->id);
297                 } else {
298                         s->alarm_high_threshold = 0;
299                         s->alarm_enabled = 0;
300                 }
301
302                 sensor_cur++;
303         }
304 }
305
306 static void associate_preferences(struct psensor **sensors)
307 {
308         struct psensor **sensor_cur = sensors;
309         while (*sensor_cur) {
310                 char *n;
311                 struct psensor *s = *sensor_cur;
312
313                 s->graph_enabled = config_is_sensor_enabled(s->id);
314
315                 n = config_get_sensor_name(s->id);
316
317                 if (n) {
318                         free(s->name);
319                         s->name = n;
320                 }
321
322                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
323
324                 sensor_cur++;
325         }
326 }
327
328 static void log_init()
329 {
330         char *home, *path, *dir;
331
332         home = getenv("HOME");
333
334         if (!home)
335                 return ;
336
337         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
338         sprintf(dir, "%s/%s", home, ".psensor");
339         mkdir(dir, 0777);
340
341         path = malloc(strlen(dir)+1+strlen("log")+1);
342         sprintf(path, "%s/%s", dir, "log");
343
344         log_open(path);
345
346         free(dir);
347         free(path);
348 }
349
350 static struct option long_options[] = {
351         {"use-libatasmart", no_argument, 0, 0},
352         {"version", no_argument, 0, 'v'},
353         {"help", no_argument, 0, 'h'},
354         {"url", required_argument, 0, 'u'},
355         {"debug", required_argument, 0, 'd'},
356         {"new-instance", no_argument, 0, 'n'},
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 static void cb_activate(GApplication *application,
396                         gpointer data)
397 {
398         ui_window_show((struct ui_psensor *)data);
399 }
400
401 /*
402  * Release memory for Valgrind.
403  */
404 static void cleanup(struct ui_psensor *ui)
405 {
406         pthread_mutex_lock(&ui->sensors_mutex);
407
408         log_debug("Cleanup...");
409
410         psensor_cleanup();
411
412 #ifdef HAVE_NVIDIA
413         nvidia_cleanup();
414 #endif
415 #ifdef HAVE_LIBATIADL
416         amd_cleanup();
417 #endif
418 #ifdef HAVE_REMOTE_SUPPORT
419         rsensor_cleanup();
420 #endif
421
422         psensor_list_free(ui->sensors);
423         ui->sensors = NULL;
424
425 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
426         ui_appindicator_cleanup();
427 #endif
428
429         ui_status_cleanup();
430
431         pthread_mutex_unlock(&ui->sensors_mutex);
432
433         config_cleanup();
434
435         log_debug("Cleanup done, closing log");
436 }
437
438 /*
439  * Creates the list of sensors.
440  *
441  * 'url': remote psensor server url, null for local monitoring.
442  * 'use_libatasmart': whether the libatasmart must be used.
443  */
444 static struct psensor **create_sensors_list(const char *url,
445                                             unsigned int use_libatasmart)
446 {
447         struct psensor **sensors;
448
449         if (url) {
450 #ifdef HAVE_REMOTE_SUPPORT
451                 rsensor_init();
452                 sensors = get_remote_sensors(url, 600);
453 #else
454                 log_err(_("Psensor has not been compiled with remote "
455                           "sensor support."));
456                 exit(EXIT_FAILURE);
457 #endif
458         } else {
459                 sensors = get_all_sensors(use_libatasmart, 600);
460 #ifdef HAVE_NVIDIA
461                 sensors = nvidia_psensor_list_add(sensors, 600);
462 #endif
463 #ifdef HAVE_LIBATIADL
464                 sensors = amd_psensor_list_add(sensors, 600);
465 #endif
466 #ifdef HAVE_GTOP
467                 sensors = cpu_psensor_list_add(sensors, 600);
468 #endif
469         }
470
471         associate_preferences(sensors);
472         associate_colors(sensors);
473
474         return sensors;
475 }
476
477 int main(int argc, char **argv)
478 {
479         struct ui_psensor ui;
480         GError *error;
481         GThread *thread;
482         int optc, cmdok, opti, use_libatasmart, new_instance;
483         char *url = NULL;
484         GApplication *app;
485
486         program_name = argv[0];
487
488         setlocale(LC_ALL, "");
489
490 #if ENABLE_NLS
491         bindtextdomain(PACKAGE, LOCALEDIR);
492         textdomain(PACKAGE);
493 #endif
494
495         use_libatasmart = new_instance = 0;
496
497         cmdok = 1;
498         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
499                                    &opti)) != -1) {
500                 switch (optc) {
501                 case 0:
502                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
503                                 use_libatasmart = 1;
504                         break;
505                 case 'u':
506                         if (optarg)
507                                 url = strdup(optarg);
508                         break;
509                 case 'h':
510                         print_help();
511                         exit(EXIT_SUCCESS);
512                 case 'v':
513                         print_version();
514                         exit(EXIT_SUCCESS);
515                 case 'd':
516                         log_level = atoi(optarg);
517                         log_info(_("Enables debug mode."));
518                         break;
519                 case 'n':
520                         new_instance = 1;
521                         break;
522                 default:
523                         cmdok = 0;
524                         break;
525                 }
526         }
527
528         if (!cmdok || optind != argc) {
529                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
530                         program_name);
531                 exit(EXIT_FAILURE);
532         }
533
534         log_init();
535
536         app = g_application_new("wpitchoune.psensor", 0);
537
538         g_application_register(app, NULL, NULL);
539
540         if (!new_instance && g_application_get_is_remote(app)) {
541                 g_application_activate(app);
542                 log_warn(_("A Psensor instance already exists."));
543                 exit(EXIT_SUCCESS);
544         }
545
546         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
547
548         log_glib_info();
549 #if !(GLIB_CHECK_VERSION(2, 31, 0))
550         /*
551          * Since GLib 2.31 g_thread_init call is deprecated and not
552          * needed.
553          */
554         log_debug("Calling g_thread_init(NULL)");
555         g_thread_init(NULL);
556 #endif
557
558         gdk_threads_init();
559
560         gtk_init(NULL, NULL);
561
562         pthread_mutex_init(&ui.sensors_mutex, NULL);
563
564         ui.config = config_load();
565
566         psensor_init();
567
568         ui.sensors = create_sensors_list(url, use_libatasmart);
569         associate_cb_alarm_raised(ui.sensors, &ui);
570
571         if (ui.config->slog_enabled)
572                 slog_activate(NULL,
573                               ui.sensors,
574                               &ui.sensors_mutex,
575                               config_get_slog_interval());
576
577 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
578         ui_status_init(&ui);
579         ui_status_set_visible(1);
580 #endif
581
582         /* main window */
583         ui_window_create(&ui);
584
585         ui_enable_alpha_channel(&ui);
586
587         thread = g_thread_create((GThreadFunc) update_measures,
588                                  &ui, TRUE, &error);
589
590         if (!thread)
591                 g_error_free(error);
592
593         ui.graph_update_interval = ui.config->graph_update_interval;
594
595         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
596
597 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
598         ui_appindicator_init(&ui);
599 #endif
600
601         gdk_notify_startup_complete();
602
603         /*
604          * hack, did not find a cleaner solution.
605          * wait 30s to ensure that the status icon is attempted to be
606          * drawn before determining whether the main window must be
607          * show.
608          */
609         if  (ui.config->hide_on_startup)
610                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
611         else
612                 initial_window_show(&ui);
613
614         /* main loop */
615         gtk_main();
616
617         g_object_unref(app);
618         cleanup(&ui);
619
620         log_debug("Quitting...");
621         log_close();
622
623         if (url)
624                 free(url);
625
626         return 0;
627 }