style
[psensor.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2011 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 redistribute it.\n\
81 There is NO WARRANTY, to the extent permitted by law.\n"),
82                "2010-2011");
83 }
84
85 static void print_help()
86 {
87         printf(_("Usage: %s [OPTION]...\n"), program_name);
88
89         puts(_("psensor is a GTK application for monitoring hardware sensors, "
90                "including temperatures and fan speeds."));
91
92         puts("");
93         puts(_("Options:"));
94         puts(_("\
95   -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       \
102 the URL of the psensor-server, example: http://hostname:3131"));
103
104         puts("");
105
106         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
107         puts("");
108         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
109 }
110
111 /*
112   Updates the size of the sensor values if different than the
113   configuration.
114  */
115 void
116 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
117 {
118         struct psensor **cur;
119
120         cur = sensors;
121         while (*cur) {
122                 struct psensor *s = *cur;
123
124                 if (s->values_max_length != cfg->sensor_values_max_length)
125                         psensor_values_resize(s,
126                                               cfg->sensor_values_max_length);
127
128                 cur++;
129         }
130 }
131
132 static void log_measures(struct psensor **sensors)
133 {
134         if (log_level == LOG_DEBUG)
135                 while (*sensors) {
136                         log_debug("Measure: %s %.2f",
137                                    (*sensors)->name,
138                                    psensor_get_current_value(*sensors));
139
140                         sensors++;
141                 }
142 }
143
144 void update_psensor_measures(struct ui_psensor *ui)
145 {
146         struct psensor **sensors = ui->sensors;
147         struct config *cfg = ui->config;
148
149         while (1) {
150                 g_mutex_lock(ui->sensors_mutex);
151
152                 if (!sensors)
153                         return;
154
155                 update_psensor_values_size(sensors, ui->config);
156
157                 psensor_list_update_measures(sensors);
158 #ifdef HAVE_REMOTE_SUPPORT
159                 remote_psensor_list_update(sensors);
160 #endif
161 #ifdef HAVE_NVIDIA
162                 nvidia_psensor_list_update(sensors);
163 #endif
164 #ifdef HAVE_LIBATIADL
165                 amd_psensor_list_update(sensors);
166 #endif
167
168                 log_measures(sensors);
169
170                 g_mutex_unlock(ui->sensors_mutex);
171
172                 sleep(cfg->sensor_update_interval);
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);
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 #endif
223
224         if (ui->graph_update_interval != cfg->graph_update_interval) {
225                 ui->graph_update_interval = cfg->graph_update_interval;
226                 ret = FALSE;
227         }
228
229         g_mutex_unlock(ui->sensors_mutex);
230
231         if (ret == FALSE)
232                 g_timeout_add(1000 * ui->graph_update_interval,
233                               ui_refresh_thread, ui);
234
235         return ret;
236 }
237
238 void cb_alarm_raised(struct psensor *sensor, void *data)
239 {
240 #ifdef HAVE_LIBNOTIFY
241         if (sensor->enabled)
242                 ui_notify(sensor, (struct ui_psensor *)data);
243 #endif
244 }
245
246 static void associate_colors(struct psensor **sensors)
247 {
248         /* number of uniq colors */
249 #define COLORS_COUNT 8
250
251         unsigned int colors[COLORS_COUNT][3] = {
252                 {0x0000, 0x0000, 0x0000},       /* black */
253                 {0xffff, 0x0000, 0x0000},       /* red */
254                 {0x0000, 0.0000, 0xffff},       /* blue */
255                 {0x0000, 0xffff, 0x0000},       /* green */
256
257                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
258                 {0x7fff, 0x0000, 0x0000},       /* dark red */
259                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
260                 {0x0000, 0x7fff, 0x0000}        /* dark green */
261         };
262
263         struct psensor **sensor_cur = sensors;
264         int i = 0;
265         while (*sensor_cur) {
266                 struct color default_color;
267                 color_set(&default_color,
268                           colors[i % COLORS_COUNT][0],
269                           colors[i % COLORS_COUNT][1],
270                           colors[i % COLORS_COUNT][2]);
271
272                 (*sensor_cur)->color
273                     = config_get_sensor_color((*sensor_cur)->id,
274                                               &default_color);
275
276                 sensor_cur++;
277                 i++;
278         }
279 }
280
281 static void
282 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
283 {
284         struct psensor **sensor_cur = sensors;
285         while (*sensor_cur) {
286                 struct psensor *s = *sensor_cur;
287
288                 s->cb_alarm_raised = cb_alarm_raised;
289                 s->cb_alarm_raised_data = ui;
290
291                 if (is_temp_type(s->type)) {
292                         s->alarm_limit
293                             = config_get_sensor_alarm_limit(s->id, 60);
294                         s->alarm_enabled
295                             = config_get_sensor_alarm_enabled(s->id);
296                 } else {
297                         s->alarm_limit = 0;
298                         s->alarm_enabled = 0;
299                 }
300
301                 sensor_cur++;
302         }
303 }
304
305 static void associate_preferences(struct psensor **sensors)
306 {
307         struct psensor **sensor_cur = sensors;
308         while (*sensor_cur) {
309                 char *n;
310                 struct psensor *s = *sensor_cur;
311
312                 s->enabled = config_is_sensor_enabled(s->id);
313
314                 n = config_get_sensor_name(s->id);
315
316                 if (n)
317                         s->name = n;
318
319                 sensor_cur++;
320         }
321 }
322
323 static void log_init()
324 {
325         char *home, *path, *dir;
326
327         home = getenv("HOME");
328
329         if (!home)
330                 return ;
331
332         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
333         sprintf(dir, "%s/%s", home, ".psensor");
334         mkdir(dir, 0777);
335
336         path = malloc(strlen(dir)+1+strlen("log")+1);
337         sprintf(path, "%s/%s", dir, "log");
338
339         log_open(path);
340
341         free(dir);
342         free(path);
343 }
344
345 static struct option long_options[] = {
346         {"version", no_argument, 0, 'v'},
347         {"help", no_argument, 0, 'h'},
348         {"url", required_argument, 0, 'u'},
349         {"debug", no_argument, 0, 'd'},
350         {0, 0, 0, 0}
351 };
352
353 static gboolean initial_window_show(gpointer data)
354 {
355         struct ui_psensor *ui;
356
357         log_debug("initial_window_show()");
358
359         ui = (struct ui_psensor *)data;
360
361         log_debug("is_status_supported: %d", is_status_supported());
362         log_debug("is_appindicator_supported: %d",
363                    is_appindicator_supported());
364         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
365
366         if (!ui->config->hide_on_startup
367             || (!is_appindicator_supported() && !is_status_supported()))
368                 ui_window_show(ui);
369
370         ui_window_update(ui);
371
372         return FALSE;
373 }
374
375 int main(int argc, char **argv)
376 {
377         struct ui_psensor ui;
378         GError *error;
379         GThread *thread;
380         int optc;
381         char *url = NULL;
382         int cmdok = 1;
383
384         program_name = argv[0];
385
386         setlocale(LC_ALL, "");
387
388 #if ENABLE_NLS
389         bindtextdomain(PACKAGE, LOCALEDIR);
390         textdomain(PACKAGE);
391 #endif
392
393         while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
394                                    NULL)) != -1) {
395                 switch (optc) {
396                 case 'u':
397                         if (optarg)
398                                 url = strdup(optarg);
399                         break;
400                 case 'h':
401                         print_help();
402                         exit(EXIT_SUCCESS);
403                 case 'v':
404                         print_version();
405                         exit(EXIT_SUCCESS);
406                 case 'd':
407                         printf(_("Enables debug mode.\n"));
408                         log_level = LOG_DEBUG;
409                         break;
410                 default:
411                         cmdok = 0;
412                         break;
413                 }
414         }
415
416         if (!cmdok || optind != argc) {
417                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
418                         program_name);
419                 exit(EXIT_FAILURE);
420         }
421
422         log_init();
423
424         g_thread_init(NULL);
425         gdk_threads_init();
426         /* gdk_threads_enter(); */
427
428         gtk_init(NULL, NULL);
429
430         ui.sensors_mutex = g_mutex_new();
431
432         config_init();
433
434         ui.config = config_load();
435
436         psensor_init();
437
438         if (url) {
439 #ifdef HAVE_REMOTE_SUPPORT
440                 rsensor_init();
441                 ui.sensors = get_remote_sensors(url, 600);
442 #else
443                 fprintf(stderr,
444                         _("ERROR: Not compiled with remote sensor support.\n"));
445                 exit(EXIT_FAILURE);
446 #endif
447         } else {
448                 ui.sensors = get_all_sensors(600);
449 #ifdef HAVE_NVIDIA
450                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
451 #endif
452 #ifdef HAVE_LIBATIADL
453                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
454 #endif
455 #ifdef HAVE_GTOP
456                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
457 #endif
458         }
459
460         associate_preferences(ui.sensors);
461         associate_colors(ui.sensors);
462         associate_cb_alarm_raised(ui.sensors, &ui);
463
464 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
465         ui_status_init(&ui);
466 #endif
467
468         /* main window */
469         ui_window_create(&ui);
470         ui.sensor_box = NULL;
471
472         /* drawing box */
473         ui.w_graph = ui_graph_create(&ui);
474
475         /* sensor list */
476         ui_sensorlist_create(&ui);
477
478         thread = g_thread_create((GThreadFunc) update_psensor_measures,
479                                  &ui, TRUE, &error);
480
481         if (!thread)
482                 g_error_free(error);
483
484         ui.graph_update_interval = ui.config->graph_update_interval;
485
486         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
487
488 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
489         ui_appindicator_init(&ui);
490 #endif
491
492         /*
493          * show the window as soon as all gtk events have been processed
494          * in order to ensure that the status icon is attempted to be
495          * drawn before. If not, there is no way to detect that it is
496          * visible.
497         */
498         g_idle_add((GSourceFunc)initial_window_show, &ui);
499
500         gdk_notify_startup_complete();
501
502         /* main loop */
503         gtk_main();
504
505         g_mutex_lock(ui.sensors_mutex);
506
507         psensor_cleanup();
508
509 #ifdef HAVE_NVIDIA
510         nvidia_cleanup();
511 #endif
512 #ifdef HAVE_LIBATIADL
513         amd_cleanup();
514 #endif
515 #ifdef HAVE_REMOTE_SUPPORT
516         rsensor_cleanup();
517 #endif
518
519         psensor_list_free(ui.sensors);
520         ui.sensors = NULL;
521
522 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
523         ui_appindicator_cleanup();
524 #endif
525
526         ui_status_cleanup();
527
528         g_mutex_unlock(ui.sensors_mutex);
529
530         config_cleanup();
531
532         log_close();
533
534         return 0;
535 }