removed atasmart option from help
[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 "  -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(void *data)
139 {
140         struct psensor **sensors;
141         struct config *cfg;
142         int period;
143         struct ui_psensor *ui;
144
145         ui = (struct ui_psensor *)data;
146         cfg = ui->config;
147
148         while (1) {
149                 pmutex_lock(&ui->sensors_mutex);
150
151                 sensors = ui->sensors;
152                 if (!sensors)
153                         pthread_exit(NULL);
154
155                 update_psensor_values_size(sensors, cfg);
156
157                 psensor_list_update_measures(sensors);
158
159                 lmsensor_psensor_list_update(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 #ifdef HAVE_ATASMART
176                 atasmart_psensor_list_update(sensors);
177 #endif
178
179                 hddtemp_psensor_list_update(sensors);
180
181                 psensor_log_measures(sensors);
182
183                 period = cfg->sensor_update_interval;
184
185                 pmutex_unlock(&ui->sensors_mutex);
186
187                 sleep(period);
188         }
189 }
190
191 static void indicators_update(struct ui_psensor *ui)
192 {
193         struct psensor **sensor_cur = ui->sensors;
194         unsigned int attention = 0;
195
196         while (*sensor_cur) {
197                 struct psensor *s = *sensor_cur;
198
199                 if (s->alarm_enabled && s->alarm_raised) {
200                         attention = 1;
201                         break;
202                 }
203
204                 sensor_cur++;
205         }
206
207 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
208         if (is_appindicator_supported())
209                 ui_appindicator_update(ui, attention);
210 #endif
211
212         if (is_status_supported())
213                 ui_status_update(ui, attention);
214 }
215
216 gboolean ui_refresh_thread(gpointer data)
217 {
218         struct config *cfg;
219         gboolean ret;
220         struct ui_psensor *ui = (struct ui_psensor *)data;
221
222         ret = TRUE;
223         cfg = ui->config;
224
225         pmutex_lock(&ui->sensors_mutex);
226
227         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
228
229         ui_sensorlist_update(ui, 0);
230
231         if (is_appindicator_supported() || is_status_supported())
232                 indicators_update(ui);
233
234 #ifdef HAVE_UNITY
235         ui_unity_launcher_entry_update(ui->sensors,
236                                        !cfg->unity_launcher_count_disabled,
237                                        cfg->temperature_unit == CELSIUS);
238 #endif
239
240         if (ui->graph_update_interval != cfg->graph_update_interval) {
241                 ui->graph_update_interval = cfg->graph_update_interval;
242                 ret = FALSE;
243         }
244
245         pmutex_unlock(&ui->sensors_mutex);
246
247         if (ret == FALSE)
248                 g_timeout_add(1000 * ui->graph_update_interval,
249                               ui_refresh_thread, ui);
250
251         return ret;
252 }
253
254 static void cb_alarm_raised(struct psensor *sensor, void *data)
255 {
256 #ifdef HAVE_LIBNOTIFY
257         if (sensor->alarm_enabled)
258                 ui_notify(sensor, (struct ui_psensor *)data);
259 #endif
260
261         notify_cmd(sensor);
262 }
263
264 static void associate_colors(struct psensor **sensors)
265 {
266         /* number of uniq colors */
267 #define COLORS_COUNT 8
268
269         double colors[COLORS_COUNT][3] = {
270                 {0, 0, 0},      /* black */
271                 {1, 0, 0},      /* red */
272                 {0, 0, 1},      /* blue */
273                 {0, 1, 0},      /* green */
274
275                 {0.5, 0.5, 0.5},/* grey */
276                 {0.5, 0, 0},    /* dark red */
277                 {0, 0, 0.5},    /* dark blue */
278                 {0, 0.5, 0}     /* dark green */
279         };
280         struct psensor **cur;
281         int i;
282         struct color c;
283
284         for (cur = sensors, i = 0; *cur; cur++, i++) {
285                 color_set(&c,
286                           colors[i % COLORS_COUNT][0],
287                           colors[i % COLORS_COUNT][1],
288                           colors[i % COLORS_COUNT][2]);
289
290                 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
291         }
292 }
293
294 static void
295 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
296 {
297         struct psensor **sensor_cur = sensors;
298
299         while (*sensor_cur) {
300                 struct psensor *s = *sensor_cur;
301
302                 s->cb_alarm_raised = cb_alarm_raised;
303                 s->cb_alarm_raised_data = ui;
304
305                 s->alarm_high_threshold
306                         = config_get_sensor_alarm_high_threshold(s->id);
307                 s->alarm_low_threshold
308                         = config_get_sensor_alarm_low_threshold(s->id);
309
310                 s->alarm_enabled
311                             = config_get_sensor_alarm_enabled(s->id);
312
313                 sensor_cur++;
314         }
315 }
316
317 static void associate_preferences(struct psensor **sensors)
318 {
319         struct psensor **sensor_cur = sensors;
320
321         while (*sensor_cur) {
322                 char *n;
323                 struct psensor *s = *sensor_cur;
324
325                 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
326
327                 n = config_get_sensor_name(s->id);
328
329                 if (n) {
330                         free(s->name);
331                         s->name = n;
332                 }
333
334                 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
335
336                 sensor_cur++;
337         }
338 }
339
340 static void log_init()
341 {
342         const char *dir;
343         char *path;
344
345         dir = get_psensor_user_dir();
346
347         if (!dir)
348                 return;
349
350         path = malloc(strlen(dir)+1+strlen("log")+1);
351         sprintf(path, "%s/%s", dir, "log");
352
353         log_open(path);
354
355         free(path);
356 }
357
358 static struct option long_options[] = {
359         {"version", no_argument, 0, 'v'},
360         {"help", no_argument, 0, 'h'},
361         {"url", required_argument, 0, 'u'},
362         {"debug", required_argument, 0, 'd'},
363         {"new-instance", no_argument, 0, 'n'},
364         {0, 0, 0, 0}
365 };
366
367 static gboolean initial_window_show(gpointer data)
368 {
369         struct ui_psensor *ui;
370
371         log_debug("initial_window_show()");
372
373         ui = (struct ui_psensor *)data;
374
375         log_debug("is_status_supported: %d", is_status_supported());
376         log_debug("is_appindicator_supported: %d",
377                    is_appindicator_supported());
378         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
379
380         if (!ui->config->hide_on_startup
381             || (!is_appindicator_supported() && !is_status_supported()))
382                 ui_window_show(ui);
383
384         ui_window_update(ui);
385
386         return FALSE;
387 }
388
389 static void log_glib_info()
390 {
391         log_debug("Compiled with GLib %d.%d.%d",
392                   GLIB_MAJOR_VERSION,
393                   GLIB_MINOR_VERSION,
394                   GLIB_MICRO_VERSION);
395
396         log_debug("Running with GLib %d.%d.%d",
397                   glib_major_version,
398                   glib_minor_version,
399                   glib_micro_version);
400 }
401
402 static void cb_activate(GApplication *application,
403                         gpointer data)
404 {
405         ui_window_show((struct ui_psensor *)data);
406 }
407
408 /*
409  * Release memory for Valgrind.
410  */
411 static void cleanup(struct ui_psensor *ui)
412 {
413         pmutex_lock(&ui->sensors_mutex);
414
415         log_debug("Cleanup...");
416
417         psensor_cleanup();
418
419 #ifdef HAVE_NVIDIA
420         nvidia_cleanup();
421 #endif
422 #ifdef HAVE_LIBATIADL
423         amd_cleanup();
424 #endif
425 #ifdef HAVE_REMOTE_SUPPORT
426         rsensor_cleanup();
427 #endif
428
429         psensor_list_free(ui->sensors);
430         ui->sensors = NULL;
431
432 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
433         ui_appindicator_cleanup();
434 #endif
435
436         ui_status_cleanup();
437
438         pmutex_unlock(&ui->sensors_mutex);
439
440         config_cleanup();
441
442         log_debug("Cleanup done, closing log");
443 }
444
445 /*
446  * Creates the list of sensors.
447  *
448  * 'url': remote psensor server url, null for local monitoring.
449  */
450 static struct psensor **create_sensors_list(const char *url)
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                 log_err(_("Psensor has not been compiled with remote "
460                           "sensor support."));
461                 exit(EXIT_FAILURE);
462 #endif
463         } else {
464                 sensors = malloc(sizeof(struct psensor *));
465                 *sensors = NULL;
466
467                 if (config_is_lmsensor_enabled())
468                         lmsensor_psensor_list_append(&sensors, 600);
469
470                 if (config_is_hddtemp_enabled())
471                         hddtemp_psensor_list_append(&sensors, 600);
472
473 #ifdef HAVE_ATASMART
474                 if (config_is_libatasmart_enabled())
475                         atasmart_psensor_list_append(&sensors, 600);
476 #endif
477
478 #ifdef HAVE_NVIDIA
479                 if (config_is_nvctrl_enabled())
480                         nvidia_psensor_list_append(&sensors, 600);
481 #endif
482 #ifdef HAVE_LIBATIADL
483                 if (config_is_atiadlsdk_enabled())
484                         amd_psensor_list_append(&sensors, 600);
485 #endif
486 #ifdef HAVE_GTOP
487                 if (config_is_gtop2_enabled())
488                         gtop2_psensor_list_append(&sensors, 600);
489 #endif
490 #ifdef HAVE_LIBUDISKS2
491                 if (config_is_udisks2_enabled())
492                         udisks2_psensor_list_append(&sensors, 600);
493 #endif
494         }
495
496         associate_preferences(sensors);
497         associate_colors(sensors);
498
499         return sensors;
500 }
501
502 int main(int argc, char **argv)
503 {
504         struct ui_psensor ui;
505         pthread_t thread;
506         int optc, cmdok, opti, new_instance, ret;
507         char *url = NULL;
508         GApplication *app;
509
510         program_name = argv[0];
511
512         setlocale(LC_ALL, "");
513
514 #if ENABLE_NLS
515         bindtextdomain(PACKAGE, LOCALEDIR);
516         textdomain(PACKAGE);
517 #endif
518
519         new_instance = 0;
520
521         cmdok = 1;
522         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
523                                    &opti)) != -1) {
524                 switch (optc) {
525                 case 'u':
526                         if (optarg)
527                                 url = strdup(optarg);
528                         break;
529                 case 'h':
530                         print_help();
531                         exit(EXIT_SUCCESS);
532                 case 'v':
533                         print_version();
534                         exit(EXIT_SUCCESS);
535                 case 'd':
536                         log_level = atoi(optarg);
537                         log_info(_("Enables debug mode."));
538                         break;
539                 case 'n':
540                         new_instance = 1;
541                         break;
542                 default:
543                         cmdok = 0;
544                         break;
545                 }
546         }
547
548         if (!cmdok || optind != argc) {
549                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
550                         program_name);
551                 exit(EXIT_FAILURE);
552         }
553
554         log_init();
555
556         app = g_application_new("wpitchoune.psensor", 0);
557
558         g_application_register(app, NULL, NULL);
559
560         if (!new_instance && g_application_get_is_remote(app)) {
561                 g_application_activate(app);
562                 log_warn(_("A Psensor instance already exists."));
563                 exit(EXIT_SUCCESS);
564         }
565
566         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
567
568         log_glib_info();
569 #if !(GLIB_CHECK_VERSION(2, 31, 0))
570         /*
571          * Since GLib 2.31 g_thread_init call is deprecated and not
572          * needed.
573          */
574         log_debug("Calling g_thread_init(NULL)");
575         g_thread_init(NULL);
576 #endif
577
578 #ifdef HAVE_APPINDICATOR_029
579         /* gdk_thread_enter/leave only used to workaround mutex bug
580          * of appindicator < 0.2.9, so do not call gdk_threads_init
581          * if useless. Calling this function leads to
582          * crash "Attempt to unlock mutex that was not locked" with
583          * GLib 2.41.2 (new checking) probably due to bugs in GTK
584          * itself.
585          */
586         gdk_threads_init();
587 #endif
588
589         gtk_init(NULL, NULL);
590
591         pmutex_init(&ui.sensors_mutex);
592
593         ui.config = config_load();
594
595         psensor_init();
596
597         ui.sensors = create_sensors_list(url);
598         associate_cb_alarm_raised(ui.sensors, &ui);
599
600         if (ui.config->slog_enabled)
601                 slog_activate(NULL,
602                               ui.sensors,
603                               &ui.sensors_mutex,
604                               config_get_slog_interval());
605
606 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
607         ui_status_init(&ui);
608         ui_status_set_visible(1);
609 #endif
610
611         /* main window */
612         ui_window_create(&ui);
613
614         ui_enable_alpha_channel(&ui);
615
616         ret = pthread_create(&thread, NULL, update_measures, &ui);
617
618         if (ret)
619                 log_err(_("Failed to create thread for monitoring sensors"));
620
621         ui.graph_update_interval = ui.config->graph_update_interval;
622
623         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
624
625 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
626         ui_appindicator_init(&ui);
627 #endif
628
629         gdk_notify_startup_complete();
630
631         /*
632          * hack, did not find a cleaner solution.
633          * wait 30s to ensure that the status icon is attempted to be
634          * drawn before determining whether the main window must be
635          * show.
636          */
637         if  (ui.config->hide_on_startup)
638                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
639         else
640                 initial_window_show(&ui);
641
642         log_debug("translators: %s\n", _("translator-credits"));
643
644         /* main loop */
645         gtk_main();
646
647         g_object_unref(app);
648         cleanup(&ui);
649
650         log_debug("Quitting...");
651         log_close();
652
653         if (url)
654                 free(url);
655
656         return 0;
657 }