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