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