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