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