5e008db8151ba03de8ccaed8936974601cce48f1
[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 modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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
20 #include <locale.h>
21
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include <gtk/gtk.h>
31
32 #include "config.h"
33
34 #include "cfg.h"
35 #include "psensor.h"
36 #include "graph.h"
37 #include "ui.h"
38 #include "ui_sensorlist.h"
39 #include "ui_color.h"
40 #include "lmsensor.h"
41 #include "ui_pref.h"
42 #include "ui_graph.h"
43 #include "ui_status.h"
44
45 #ifdef HAVE_UNITY
46 #include "ui_unity.h"
47 #endif
48
49 #ifdef HAVE_NVIDIA
50 #include "nvidia.h"
51 #endif
52
53 #ifdef HAVE_LIBATIADL
54 #include "amd.h"
55 #endif
56
57 #ifdef HAVE_REMOTE_SUPPORT
58 #include "rsensor.h"
59 #endif
60
61 #include "ui_appindicator.h"
62
63 #ifdef HAVE_LIBNOTIFY
64 #include "ui_notify.h"
65 #endif
66
67 #ifdef HAVE_GTOP
68 #include "cpu.h"
69 #endif
70
71 #include "compat.h"
72
73 static const char *program_name;
74
75 static void print_version()
76 {
77         printf("psensor %s\n", VERSION);
78         printf(_("Copyright (C) %s jeanfi@gmail.com\n\
79 License GPLv2: GNU GPL version 2 or later \
80 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
81 This is free software: you are free to change and 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(_("\
96   -h, --help          display this help and exit\n\
97   -v, --version       display version information and exit"));
98
99         puts("");
100
101         puts(_("\
102   -u, --url=URL       \
103 the URL of the psensor-server, example: http://hostname:3131"));
104
105         puts("");
106
107         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
108         puts("");
109         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
110 }
111
112 /*
113   Updates the size of the sensor values if different than the
114   configuration.
115  */
116 void
117 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
118 {
119         struct psensor **cur;
120
121         cur = sensors;
122         while (*cur) {
123                 struct psensor *s = *cur;
124
125                 if (s->values_max_length != cfg->sensor_values_max_length)
126                         psensor_values_resize(s,
127                                               cfg->sensor_values_max_length);
128
129                 cur++;
130         }
131 }
132
133 static void log_measures(struct psensor **sensors)
134 {
135         if (log_level == LOG_DEBUG)
136                 while (*sensors) {
137                         log_debug("Measure: %s %.2f",
138                                    (*sensors)->name,
139                                    psensor_get_current_value(*sensors));
140
141                         sensors++;
142                 }
143 }
144
145 void update_psensor_measures(struct ui_psensor *ui)
146 {
147         struct psensor **sensors = ui->sensors;
148         struct config *cfg = ui->config;
149
150         while (1) {
151                 g_mutex_lock(ui->sensors_mutex);
152
153                 if (!sensors)
154                         return;
155
156                 update_psensor_values_size(sensors, ui->config);
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                 log_measures(sensors);
170
171                 g_mutex_unlock(ui->sensors_mutex);
172
173                 sleep(cfg->sensor_update_interval);
174         }
175 }
176
177 static void indicators_update(struct ui_psensor *ui)
178 {
179         struct psensor **sensor_cur = ui->sensors;
180         unsigned int attention = 0;
181
182         while (*sensor_cur) {
183                 struct psensor *s = *sensor_cur;
184
185                 if (s->alarm_enabled && s->alarm_raised) {
186                         attention = 1;
187                         break;
188                 }
189
190                 sensor_cur++;
191         }
192
193 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
194         if (is_appindicator_supported())
195                 ui_appindicator_update(ui, attention);
196 #endif
197
198         if (is_status_supported())
199                 ui_status_update(ui, attention);
200 }
201
202 gboolean ui_refresh_thread(gpointer data)
203 {
204         struct config *cfg;
205         gboolean ret;
206         struct ui_psensor *ui = (struct ui_psensor *)data;
207
208         ret = TRUE;
209         cfg = ui->config;
210
211         g_mutex_lock(ui->sensors_mutex);
212
213         graph_update(ui->sensors, ui->w_graph, ui->config);
214
215         ui_sensorlist_update(ui);
216
217         if (is_appindicator_supported() || is_status_supported())
218                 indicators_update(ui);
219
220 #ifdef HAVE_UNITY
221         ui_unity_launcher_entry_update(ui->sensors,
222                                        !cfg->unity_launcher_count_disabled);
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 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                         s->name = n;
319
320                 sensor_cur++;
321         }
322 }
323
324 static void log_init()
325 {
326         char *home, *path, *dir;
327
328         home = getenv("HOME");
329
330         if (!home)
331                 return ;
332
333         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
334         sprintf(dir, "%s/%s", home, ".psensor");
335         mkdir(dir, 0777);
336
337         path = malloc(strlen(dir)+1+strlen("log")+1);
338         sprintf(path, "%s/%s", dir, "log");
339
340         log_open(path);
341
342         free(dir);
343         free(path);
344 }
345
346 static struct option long_options[] = {
347         {"version", no_argument, 0, 'v'},
348         {"help", no_argument, 0, 'h'},
349         {"url", required_argument, 0, 'u'},
350         {"debug", no_argument, 0, 'd'},
351         {0, 0, 0, 0}
352 };
353
354 static gboolean initial_window_show(gpointer data)
355 {
356         struct ui_psensor *ui;
357
358         log_debug("initial_window_show()");
359
360         ui = (struct ui_psensor *)data;
361
362         log_debug("is_status_supported: %d", is_status_supported());
363         log_debug("is_appindicator_supported: %d",
364                    is_appindicator_supported());
365         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
366
367         if (!ui->config->hide_on_startup
368             || (!is_appindicator_supported() && !is_status_supported()))
369                 ui_window_show(ui);
370
371         ui_window_update(ui);
372
373         return FALSE;
374 }
375
376 int main(int argc, char **argv)
377 {
378         struct ui_psensor ui;
379         GError *error;
380         GThread *thread;
381         int optc;
382         char *url = NULL;
383         int cmdok = 1;
384
385         program_name = argv[0];
386
387         setlocale(LC_ALL, "");
388
389 #if ENABLE_NLS
390         bindtextdomain(PACKAGE, LOCALEDIR);
391         textdomain(PACKAGE);
392 #endif
393
394         while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
395                                    NULL)) != -1) {
396                 switch (optc) {
397                 case 'u':
398                         if (optarg)
399                                 url = strdup(optarg);
400                         break;
401                 case 'h':
402                         print_help();
403                         exit(EXIT_SUCCESS);
404                 case 'v':
405                         print_version();
406                         exit(EXIT_SUCCESS);
407                 case 'd':
408                         printf(_("Enables debug mode.\n"));
409                         log_level = LOG_DEBUG;
410                         break;
411                 default:
412                         cmdok = 0;
413                         break;
414                 }
415         }
416
417         if (!cmdok || optind != argc) {
418                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
419                         program_name);
420                 exit(EXIT_FAILURE);
421         }
422
423         log_init();
424
425         g_thread_init(NULL);
426         gdk_threads_init();
427         /* gdk_threads_enter(); */
428
429         gtk_init(NULL, NULL);
430
431         ui.sensors_mutex = g_mutex_new();
432
433         config_init();
434
435         ui.config = config_load();
436
437         psensor_init();
438
439         if (url) {
440 #ifdef HAVE_REMOTE_SUPPORT
441                 rsensor_init();
442                 ui.sensors = get_remote_sensors(url, 600);
443 #else
444                 fprintf(stderr,
445                         _("ERROR: Not compiled with remote sensor support.\n"));
446                 exit(EXIT_FAILURE);
447 #endif
448         } else {
449                 ui.sensors = get_all_sensors(600);
450 #ifdef HAVE_NVIDIA
451                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
452 #endif
453 #ifdef HAVE_LIBATIADL
454                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
455 #endif
456 #ifdef HAVE_GTOP
457                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
458 #endif
459         }
460
461         associate_preferences(ui.sensors);
462         associate_colors(ui.sensors);
463         associate_cb_alarm_raised(ui.sensors, &ui);
464
465 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
466         ui_status_init(&ui);
467 #endif
468
469         /* main window */
470         ui_window_create(&ui);
471         ui.sensor_box = NULL;
472
473         /* drawing box */
474         ui.w_graph = ui_graph_create(&ui);
475
476         /* sensor list */
477         ui_sensorlist_create(&ui);
478
479         thread = g_thread_create((GThreadFunc) update_psensor_measures,
480                                  &ui, TRUE, &error);
481
482         if (!thread)
483                 g_error_free(error);
484
485         ui.graph_update_interval = ui.config->graph_update_interval;
486
487         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
488
489 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
490         ui_appindicator_init(&ui);
491 #endif
492
493         /*
494          * show the window as soon as all gtk events have been processed
495          * in order to ensure that the status icon is attempted to be
496          * drawn before. If not, there is no way to detect that it is
497          * visible.
498         */
499         g_idle_add((GSourceFunc)initial_window_show, &ui);
500
501         gdk_notify_startup_complete();
502
503         /* main loop */
504         gtk_main();
505
506         g_mutex_lock(ui.sensors_mutex);
507
508         psensor_cleanup();
509
510 #ifdef HAVE_NVIDIA
511         nvidia_cleanup();
512 #endif
513 #ifdef HAVE_LIBATIADL
514         amd_cleanup();
515 #endif
516 #ifdef HAVE_REMOTE_SUPPORT
517         rsensor_cleanup();
518 #endif
519
520         psensor_list_free(ui.sensors);
521         ui.sensors = NULL;
522
523 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
524         ui_appindicator_cleanup();
525 #endif
526
527         ui_status_cleanup();
528
529         g_mutex_unlock(ui.sensors_mutex);
530
531         config_cleanup();
532
533         log_close();
534
535         return 0;
536 }