updated checkpatch.pl
[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 "
81                  " redistribute it.\n"
82                  "There is NO WARRANTY, to the extent permitted by law.\n"),
83                "2010-2011");
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
103         puts("");
104
105         puts(_("  -d, --debug=LEVEL   "
106                "set the debug level, integer between 0 and 3"));
107
108         puts("");
109
110         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
111         puts("");
112         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
113 }
114
115 /*
116   Updates the size of the sensor values if different than the
117   configuration.
118  */
119 void
120 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
121 {
122         struct psensor **cur;
123
124         cur = sensors;
125         while (*cur) {
126                 struct psensor *s = *cur;
127
128                 if (s->values_max_length != cfg->sensor_values_max_length)
129                         psensor_values_resize(s,
130                                               cfg->sensor_values_max_length);
131
132                 cur++;
133         }
134 }
135
136 static void log_measures(struct psensor **sensors)
137 {
138         if (log_level == LOG_DEBUG)
139                 while (*sensors) {
140                         log_debug("Measure: %s %.2f",
141                                    (*sensors)->name,
142                                    psensor_get_current_value(*sensors));
143
144                         sensors++;
145                 }
146 }
147
148 void update_psensor_measures(struct ui_psensor *ui)
149 {
150         struct psensor **sensors = ui->sensors;
151         struct config *cfg = ui->config;
152
153         while (1) {
154                 g_mutex_lock(ui->sensors_mutex);
155
156                 if (!sensors)
157                         return;
158
159                 update_psensor_values_size(sensors, ui->config);
160
161                 psensor_list_update_measures(sensors);
162 #ifdef HAVE_REMOTE_SUPPORT
163                 remote_psensor_list_update(sensors);
164 #endif
165 #ifdef HAVE_NVIDIA
166                 nvidia_psensor_list_update(sensors);
167 #endif
168 #ifdef HAVE_LIBATIADL
169                 amd_psensor_list_update(sensors);
170 #endif
171
172                 log_measures(sensors);
173
174                 g_mutex_unlock(ui->sensors_mutex);
175
176                 sleep(cfg->sensor_update_interval);
177         }
178 }
179
180 static void indicators_update(struct ui_psensor *ui)
181 {
182         struct psensor **sensor_cur = ui->sensors;
183         unsigned int attention = 0;
184
185         while (*sensor_cur) {
186                 struct psensor *s = *sensor_cur;
187
188                 if (s->alarm_enabled && s->alarm_raised) {
189                         attention = 1;
190                         break;
191                 }
192
193                 sensor_cur++;
194         }
195
196 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
197         if (is_appindicator_supported())
198                 ui_appindicator_update(ui, attention);
199 #endif
200
201         if (is_status_supported())
202                 ui_status_update(ui, attention);
203 }
204
205 gboolean ui_refresh_thread(gpointer data)
206 {
207         struct config *cfg;
208         gboolean ret;
209         struct ui_psensor *ui = (struct ui_psensor *)data;
210
211         ret = TRUE;
212         cfg = ui->config;
213
214         g_mutex_lock(ui->sensors_mutex);
215
216         graph_update(ui->sensors, ui->w_graph, ui->config);
217
218         ui_sensorlist_update(ui);
219
220         if (is_appindicator_supported() || is_status_supported())
221                 indicators_update(ui);
222
223 #ifdef HAVE_UNITY
224         ui_unity_launcher_entry_update(ui->sensors,
225                                        !cfg->unity_launcher_count_disabled);
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 void cb_alarm_raised(struct psensor *sensor, void *data)
243 {
244 #ifdef HAVE_LIBNOTIFY
245         if (sensor->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, 0.0000, 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                 if (is_temp_type(s->type)) {
296                         s->alarm_limit
297                             = config_get_sensor_alarm_limit(s->id, 60);
298                         s->alarm_enabled
299                             = config_get_sensor_alarm_enabled(s->id);
300                 } else {
301                         s->alarm_limit = 0;
302                         s->alarm_enabled = 0;
303                 }
304
305                 sensor_cur++;
306         }
307 }
308
309 static void associate_preferences(struct psensor **sensors)
310 {
311         struct psensor **sensor_cur = sensors;
312         while (*sensor_cur) {
313                 char *n;
314                 struct psensor *s = *sensor_cur;
315
316                 s->enabled = config_is_sensor_enabled(s->id);
317
318                 n = config_get_sensor_name(s->id);
319
320                 if (n)
321                         s->name = n;
322
323                 sensor_cur++;
324         }
325 }
326
327 static void log_init()
328 {
329         char *home, *path, *dir;
330
331         home = getenv("HOME");
332
333         if (!home)
334                 return ;
335
336         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
337         sprintf(dir, "%s/%s", home, ".psensor");
338         mkdir(dir, 0777);
339
340         path = malloc(strlen(dir)+1+strlen("log")+1);
341         sprintf(path, "%s/%s", dir, "log");
342
343         log_open(path);
344
345         free(dir);
346         free(path);
347 }
348
349 static struct option long_options[] = {
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 int main(int argc, char **argv)
380 {
381         struct ui_psensor ui;
382         GError *error;
383         GThread *thread;
384         int optc;
385         char *url = NULL;
386         int cmdok = 1;
387
388         program_name = argv[0];
389
390         setlocale(LC_ALL, "");
391
392 #if ENABLE_NLS
393         bindtextdomain(PACKAGE, LOCALEDIR);
394         textdomain(PACKAGE);
395 #endif
396
397         while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
398                                    NULL)) != -1) {
399                 switch (optc) {
400                 case 'u':
401                         if (optarg)
402                                 url = strdup(optarg);
403                         break;
404                 case 'h':
405                         print_help();
406                         exit(EXIT_SUCCESS);
407                 case 'v':
408                         print_version();
409                         exit(EXIT_SUCCESS);
410                 case 'd':
411                         printf(_("Enables debug mode.\n"));
412                         log_level = atoi(optarg);
413                         break;
414                 default:
415                         cmdok = 0;
416                         break;
417                 }
418         }
419
420         if (!cmdok || optind != argc) {
421                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
422                         program_name);
423                 exit(EXIT_FAILURE);
424         }
425
426         log_init();
427
428         g_thread_init(NULL);
429         gdk_threads_init();
430         /* gdk_threads_enter(); */
431
432         gtk_init(NULL, NULL);
433
434         ui.sensors_mutex = g_mutex_new();
435
436         config_init();
437
438         ui.config = config_load();
439
440         psensor_init();
441
442         if (url) {
443 #ifdef HAVE_REMOTE_SUPPORT
444                 rsensor_init();
445                 ui.sensors = get_remote_sensors(url, 600);
446 #else
447                 fprintf(stderr,
448                         _("ERROR: Not compiled with remote sensor support.\n"));
449                 exit(EXIT_FAILURE);
450 #endif
451         } else {
452                 ui.sensors = get_all_sensors(600);
453 #ifdef HAVE_NVIDIA
454                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
455 #endif
456 #ifdef HAVE_LIBATIADL
457                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
458 #endif
459 #ifdef HAVE_GTOP
460                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
461 #endif
462         }
463
464         associate_preferences(ui.sensors);
465         associate_colors(ui.sensors);
466         associate_cb_alarm_raised(ui.sensors, &ui);
467
468 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
469         ui_status_init(&ui);
470 #endif
471
472         /* main window */
473         ui_window_create(&ui);
474         ui.sensor_box = NULL;
475
476         /* drawing box */
477         ui.w_graph = ui_graph_create(&ui);
478
479         /* sensor list */
480         ui_sensorlist_create(&ui);
481
482         thread = g_thread_create((GThreadFunc) update_psensor_measures,
483                                  &ui, TRUE, &error);
484
485         if (!thread)
486                 g_error_free(error);
487
488         ui.graph_update_interval = ui.config->graph_update_interval;
489
490         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
491
492 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
493         ui_appindicator_init(&ui);
494 #endif
495
496         /*
497          * show the window as soon as all gtk events have been processed
498          * in order to ensure that the status icon is attempted to be
499          * drawn before. If not, there is no way to detect that it is
500          * visible.
501         */
502         g_idle_add((GSourceFunc)initial_window_show, &ui);
503
504         gdk_notify_startup_complete();
505
506         /* main loop */
507         gtk_main();
508
509         g_mutex_lock(ui.sensors_mutex);
510
511         psensor_cleanup();
512
513 #ifdef HAVE_NVIDIA
514         nvidia_cleanup();
515 #endif
516 #ifdef HAVE_LIBATIADL
517         amd_cleanup();
518 #endif
519 #ifdef HAVE_REMOTE_SUPPORT
520         rsensor_cleanup();
521 #endif
522
523         psensor_list_free(ui.sensors);
524         ui.sensors = NULL;
525
526 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
527         ui_appindicator_cleanup();
528 #endif
529
530         ui_status_cleanup();
531
532         g_mutex_unlock(ui.sensors_mutex);
533
534         config_cleanup();
535
536         log_close();
537
538         return 0;
539 }