merged cpu and mem sensors code
[psensor.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2014 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 <pmutex.h>
42 #include <pudisks2.h>
43 #include "slog.h"
44 #include "ui_pref.h"
45 #include "ui_graph.h"
46 #include "ui_status.h"
47
48 #ifdef HAVE_UNITY
49 #include "ui_unity.h"
50 #endif
51
52 #ifdef HAVE_NVIDIA
53 #include "nvidia.h"
54 #endif
55
56 #ifdef HAVE_LIBATIADL
57 #include "amd.h"
58 #endif
59
60 #ifdef HAVE_REMOTE_SUPPORT
61 #include "rsensor.h"
62 #endif
63
64 #include "ui_appindicator.h"
65
66 #ifdef HAVE_LIBNOTIFY
67 #include "ui_notify.h"
68 #endif
69
70 #ifdef HAVE_GTOP
71 #include <pgtop2.h>
72 #endif
73
74 static const char *program_name;
75
76 static void print_version()
77 {
78         printf("psensor %s\n", VERSION);
79         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
80                  "License GPLv2: GNU GPL version 2 or later "
81                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
82                  "This is free software: you are free to change and"
83                  " redistribute it.\n"
84                  "There is NO WARRANTY, to the extent permitted by law.\n"),
85                "2010-2014");
86 }
87
88 static void print_help()
89 {
90         printf(_("Usage: %s [OPTION]...\n"), program_name);
91
92         puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
93                "including temperatures and fan speeds."));
94
95         puts("");
96         puts(_("Options:"));
97         puts(_("  -h, --help          display this help and exit\n"
98                "  -v, --version       display version information and exit"));
99
100         puts("");
101
102         puts(_(
103 "  -u, --url=URL       the URL of the psensor-server,\n"
104 "                      example: http://hostname:3131"));
105         puts(_(
106 "  --use-libatasmart   use atasmart library for disk monitoring instead of\n"
107 "                      hddtemp daemon"));
108         puts(_(
109 "  -n, --new-instance  force the creation of a new Psensor application"));
110         puts("");
111
112         puts(_("  -d, --debug=LEVEL   "
113                "set the debug level, integer between 0 and 3"));
114
115         puts("");
116
117         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
118         puts("");
119         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
120 }
121
122 /*
123  * Updates the size of the sensor values if different than the
124  * configuration.
125  */
126 static void
127 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
128 {
129         struct psensor **cur, *s;
130
131         for (cur = sensors; *cur; cur++) {
132                 s = *cur;
133
134                 if (s->values_max_length != cfg->sensor_values_max_length)
135                         psensor_values_resize(s,
136                                               cfg->sensor_values_max_length);
137         }
138 }
139
140 static void *update_measures(void *data)
141 {
142         struct psensor **sensors;
143         struct config *cfg;
144         int period;
145         struct ui_psensor *ui;
146
147         ui = (struct ui_psensor *)data;
148         cfg = ui->config;
149
150         while (1) {
151                 pmutex_lock(&ui->sensors_mutex);
152
153                 sensors = ui->sensors;
154                 if (!sensors)
155                         pthread_exit(NULL);
156
157                 update_psensor_values_size(sensors, cfg);
158
159                 psensor_list_update_measures(sensors);
160 #ifdef HAVE_REMOTE_SUPPORT
161                 remote_psensor_list_update(sensors);
162 #endif
163 #ifdef HAVE_NVIDIA
164                 nvidia_psensor_list_update(sensors);
165 #endif
166 #ifdef HAVE_LIBATIADL
167                 amd_psensor_list_update(sensors);
168 #endif
169 #ifdef HAVE_LIBUDISKS2
170                 udisks2_psensor_list_update(sensors);
171 #endif
172 #ifdef HAVE_GTOP
173                 gtop2_psensor_list_update(sensors);
174 #endif
175
176                 psensor_log_measures(sensors);
177
178                 period = cfg->sensor_update_interval;
179
180                 pmutex_unlock(&ui->sensors_mutex);
181
182                 sleep(period);
183         }
184 }
185
186 static void indicators_update(struct ui_psensor *ui)
187 {
188         struct psensor **sensor_cur = ui->sensors;
189         unsigned int attention = 0;
190
191         while (*sensor_cur) {
192                 struct psensor *s = *sensor_cur;
193
194                 if (s->alarm_enabled && s->alarm_raised) {
195                         attention = 1;
196                         break;
197                 }
198
199                 sensor_cur++;
200         }
201
202 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
203         if (is_appindicator_supported())
204                 ui_appindicator_update(ui, attention);
205 #endif
206
207         if (is_status_supported())
208                 ui_status_update(ui, attention);
209 }
210
211 gboolean ui_refresh_thread(gpointer data)
212 {
213         struct config *cfg;
214         gboolean ret;
215         struct ui_psensor *ui = (struct ui_psensor *)data;
216
217         ret = TRUE;
218         cfg = ui->config;
219
220         pmutex_lock(&ui->sensors_mutex);
221
222         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
223
224         ui_sensorlist_update(ui, 0);
225
226         if (is_appindicator_supported() || is_status_supported())
227                 indicators_update(ui);
228
229 #ifdef HAVE_UNITY
230         ui_unity_launcher_entry_update(ui->sensors,
231                                        !cfg->unity_launcher_count_disabled,
232                                        cfg->temperature_unit == CELSIUS);
233 #endif
234
235         if (ui->graph_update_interval != cfg->graph_update_interval) {
236                 ui->graph_update_interval = cfg->graph_update_interval;
237                 ret = FALSE;
238         }
239
240         pmutex_unlock(&ui->sensors_mutex);
241
242         if (ret == FALSE)
243                 g_timeout_add(1000 * ui->graph_update_interval,
244                               ui_refresh_thread, ui);
245
246         return ret;
247 }
248
249 static void cb_alarm_raised(struct psensor *sensor, void *data)
250 {
251 #ifdef HAVE_LIBNOTIFY
252         if (sensor->alarm_enabled)
253                 ui_notify(sensor, (struct ui_psensor *)data);
254 #endif
255
256         notify_cmd(sensor);
257 }
258
259 static void associate_colors(struct psensor **sensors)
260 {
261         /* number of uniq colors */
262 #define COLORS_COUNT 8
263
264         double colors[COLORS_COUNT][3] = {
265                 {0, 0, 0},      /* black */
266                 {1, 0, 0},      /* red */
267                 {0, 0, 1},      /* blue */
268                 {0, 1, 0},      /* green */
269
270                 {0.5, 0.5, 0.5},/* grey */
271                 {0.5, 0, 0},    /* dark red */
272                 {0, 0, 0.5},    /* dark blue */
273                 {0, 0.5, 0}     /* dark green */
274         };
275         struct psensor **cur;
276         int i;
277         struct color c;
278
279         for (cur = sensors, i = 0; *cur; cur++, i++) {
280                 color_set(&c,
281                           colors[i % COLORS_COUNT][0],
282                           colors[i % COLORS_COUNT][1],
283                           colors[i % COLORS_COUNT][2]);
284
285                 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
286         }
287 }
288
289 static void
290 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
291 {
292         struct psensor **sensor_cur = sensors;
293
294         while (*sensor_cur) {
295                 struct psensor *s = *sensor_cur;
296
297                 s->cb_alarm_raised = cb_alarm_raised;
298                 s->cb_alarm_raised_data = ui;
299
300                 s->alarm_high_threshold
301                         = config_get_sensor_alarm_high_threshold(s->id);
302                 s->alarm_low_threshold
303                         = config_get_sensor_alarm_low_threshold(s->id);
304
305                 s->alarm_enabled
306                             = config_get_sensor_alarm_enabled(s->id);
307
308                 sensor_cur++;
309         }
310 }
311
312 static void associate_preferences(struct psensor **sensors)
313 {
314         struct psensor **sensor_cur = sensors;
315
316         while (*sensor_cur) {
317                 char *n;
318                 struct psensor *s = *sensor_cur;
319
320                 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
321
322                 n = config_get_sensor_name(s->id);
323
324                 if (n) {
325                         free(s->name);
326                         s->name = n;
327                 }
328
329                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
330
331                 sensor_cur++;
332         }
333 }
334
335 static void log_init()
336 {
337         const char *dir;
338         char *path;
339
340         dir = get_psensor_user_dir();
341
342         if (!dir)
343                 return;
344
345         path = malloc(strlen(dir)+1+strlen("log")+1);
346         sprintf(path, "%s/%s", dir, "log");
347
348         log_open(path);
349
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         pmutex_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         pmutex_unlock(&ui->sensors_mutex);
435
436         config_cleanup();
437
438         log_debug("Cleanup done, closing log");
439 }
440
441 /*
442  * Creates the list of sensors.
443  *
444  * 'url': remote psensor server url, null for local monitoring.
445  * 'use_libatasmart': whether the libatasmart must be used.
446  */
447 static struct psensor **create_sensors_list(const char *url,
448                                             unsigned int use_libatasmart)
449 {
450         struct psensor **sensors;
451
452         if (url) {
453 #ifdef HAVE_REMOTE_SUPPORT
454                 rsensor_init();
455                 sensors = get_remote_sensors(url, 600);
456 #else
457                 log_err(_("Psensor has not been compiled with remote "
458                           "sensor support."));
459                 exit(EXIT_FAILURE);
460 #endif
461         } else {
462                 sensors = get_all_sensors(use_libatasmart, 600);
463
464 #ifdef HAVE_NVIDIA
465                 if (config_is_nvctrl_enabled())
466                         sensors = nvidia_psensor_list_add(sensors, 600);
467 #endif
468 #ifdef HAVE_LIBATIADL
469                 if (config_is_atiadlsdk_enabled())
470                         sensors = amd_psensor_list_add(sensors, 600);
471 #endif
472 #ifdef HAVE_GTOP
473                 if (config_is_gtop2_enabled())
474                         gtop2_psensor_list_append(&sensors, 600);
475 #endif
476 #ifdef HAVE_LIBUDISKS2
477                 if (config_is_udisks2_enabled())
478                         udisks2_psensor_list_append(&sensors, 600);
479 #endif
480         }
481
482         associate_preferences(sensors);
483         associate_colors(sensors);
484
485         return sensors;
486 }
487
488 int main(int argc, char **argv)
489 {
490         struct ui_psensor ui;
491         pthread_t thread;
492         int optc, cmdok, opti, use_libatasmart, new_instance, ret;
493         char *url = NULL;
494         GApplication *app;
495
496         program_name = argv[0];
497
498         setlocale(LC_ALL, "");
499
500 #if ENABLE_NLS
501         bindtextdomain(PACKAGE, LOCALEDIR);
502         textdomain(PACKAGE);
503 #endif
504
505         use_libatasmart = new_instance = 0;
506
507         cmdok = 1;
508         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
509                                    &opti)) != -1) {
510                 switch (optc) {
511                 case 0:
512                         if (!strcmp(long_options[opti].name, "use-libatasmart"))
513                                 use_libatasmart = 1;
514                         break;
515                 case 'u':
516                         if (optarg)
517                                 url = strdup(optarg);
518                         break;
519                 case 'h':
520                         print_help();
521                         exit(EXIT_SUCCESS);
522                 case 'v':
523                         print_version();
524                         exit(EXIT_SUCCESS);
525                 case 'd':
526                         log_level = atoi(optarg);
527                         log_info(_("Enables debug mode."));
528                         break;
529                 case 'n':
530                         new_instance = 1;
531                         break;
532                 default:
533                         cmdok = 0;
534                         break;
535                 }
536         }
537
538         if (!cmdok || optind != argc) {
539                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
540                         program_name);
541                 exit(EXIT_FAILURE);
542         }
543
544         log_init();
545
546         app = g_application_new("wpitchoune.psensor", 0);
547
548         g_application_register(app, NULL, NULL);
549
550         if (!new_instance && g_application_get_is_remote(app)) {
551                 g_application_activate(app);
552                 log_warn(_("A Psensor instance already exists."));
553                 exit(EXIT_SUCCESS);
554         }
555
556         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
557
558         log_glib_info();
559 #if !(GLIB_CHECK_VERSION(2, 31, 0))
560         /*
561          * Since GLib 2.31 g_thread_init call is deprecated and not
562          * needed.
563          */
564         log_debug("Calling g_thread_init(NULL)");
565         g_thread_init(NULL);
566 #endif
567
568 #ifdef HAVE_APPINDICATOR_029
569         /* gdk_thread_enter/leave only used to workaround mutex bug
570          * of appindicator < 0.2.9, so do not call gdk_threads_init
571          * if useless. Calling this function leads to
572          * crash "Attempt to unlock mutex that was not locked" with
573          * GLib 2.41.2 (new checking) probably due to bugs in GTK
574          * itself.
575          */
576         gdk_threads_init();
577 #endif
578
579         gtk_init(NULL, NULL);
580
581         pmutex_init(&ui.sensors_mutex);
582
583         ui.config = config_load();
584
585         psensor_init();
586
587         ui.sensors = create_sensors_list(url, use_libatasmart);
588         associate_cb_alarm_raised(ui.sensors, &ui);
589
590         if (ui.config->slog_enabled)
591                 slog_activate(NULL,
592                               ui.sensors,
593                               &ui.sensors_mutex,
594                               config_get_slog_interval());
595
596 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
597         ui_status_init(&ui);
598         ui_status_set_visible(1);
599 #endif
600
601         /* main window */
602         ui_window_create(&ui);
603
604         ui_enable_alpha_channel(&ui);
605
606         ret = pthread_create(&thread, NULL, update_measures, &ui);
607
608         if (ret)
609                 log_err(_("Failed to create thread for monitoring sensors"));
610
611         ui.graph_update_interval = ui.config->graph_update_interval;
612
613         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
614
615 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
616         ui_appindicator_init(&ui);
617 #endif
618
619         gdk_notify_startup_complete();
620
621         /*
622          * hack, did not find a cleaner solution.
623          * wait 30s to ensure that the status icon is attempted to be
624          * drawn before determining whether the main window must be
625          * show.
626          */
627         if  (ui.config->hide_on_startup)
628                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
629         else
630                 initial_window_show(&ui);
631
632         log_debug("translators: %s\n", _("translator-credits"));
633
634         /* main loop */
635         gtk_main();
636
637         g_object_unref(app);
638         cleanup(&ui);
639
640         log_debug("Quitting...");
641         log_close();
642
643         if (url)
644                 free(url);
645
646         return 0;
647 }