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