status icon
[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_printf(LOG_DEBUG, "%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         if (!is_appindicator_supported() && !is_status_supported())
183                 return ;
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         indicators_update(ui);
221
222 #ifdef HAVE_UNITY
223         ui_unity_launcher_entry_update(ui->sensors,
224                                        !cfg->unity_launcher_count_disabled);
225 #endif
226
227         if (ui->graph_update_interval != cfg->graph_update_interval) {
228                 ui->graph_update_interval = cfg->graph_update_interval;
229                 ret = FALSE;
230         }
231
232         g_mutex_unlock(ui->sensors_mutex);
233
234         if (ret == FALSE)
235                 g_timeout_add(1000 * ui->graph_update_interval,
236                               ui_refresh_thread, ui);
237
238         return ret;
239 }
240
241 void cb_alarm_raised(struct psensor *sensor, void *data)
242 {
243 #ifdef HAVE_LIBNOTIFY
244         if (sensor->enabled)
245                 ui_notify(sensor, (struct ui_psensor *)data);
246 #endif
247 }
248
249 static void associate_colors(struct psensor **sensors)
250 {
251         /* number of uniq colors */
252 #define COLORS_COUNT 8
253
254         unsigned int colors[COLORS_COUNT][3] = {
255                 {0x0000, 0x0000, 0x0000},       /* black */
256                 {0xffff, 0x0000, 0x0000},       /* red */
257                 {0x0000, 0.0000, 0xffff},       /* blue */
258                 {0x0000, 0xffff, 0x0000},       /* green */
259
260                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
261                 {0x7fff, 0x0000, 0x0000},       /* dark red */
262                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
263                 {0x0000, 0x7fff, 0x0000}        /* dark green */
264         };
265
266         struct psensor **sensor_cur = sensors;
267         int i = 0;
268         while (*sensor_cur) {
269                 struct color default_color;
270                 color_set(&default_color,
271                           colors[i % COLORS_COUNT][0],
272                           colors[i % COLORS_COUNT][1],
273                           colors[i % COLORS_COUNT][2]);
274
275                 (*sensor_cur)->color
276                     = config_get_sensor_color((*sensor_cur)->id,
277                                               &default_color);
278
279                 sensor_cur++;
280                 i++;
281         }
282 }
283
284 static void
285 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
286 {
287         struct psensor **sensor_cur = sensors;
288         while (*sensor_cur) {
289                 struct psensor *s = *sensor_cur;
290
291                 s->cb_alarm_raised = cb_alarm_raised;
292                 s->cb_alarm_raised_data = ui;
293
294                 if (is_temp_type(s->type)) {
295                         s->alarm_limit
296                             = config_get_sensor_alarm_limit(s->id, 60);
297                         s->alarm_enabled
298                             = config_get_sensor_alarm_enabled(s->id);
299                 } else {
300                         s->alarm_limit = 0;
301                         s->alarm_enabled = 0;
302                 }
303
304                 sensor_cur++;
305         }
306 }
307
308 static void associate_preferences(struct psensor **sensors)
309 {
310         struct psensor **sensor_cur = sensors;
311         while (*sensor_cur) {
312                 char *n;
313                 struct psensor *s = *sensor_cur;
314
315                 s->enabled = config_is_sensor_enabled(s->id);
316
317                 n = config_get_sensor_name(s->id);
318
319                 if (n)
320                         s->name = n;
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         {"version", no_argument, 0, 'v'},
350         {"help", no_argument, 0, 'h'},
351         {"url", required_argument, 0, 'u'},
352         {"debug", no_argument, 0, 'd'},
353         {0, 0, 0, 0}
354 };
355
356 static gboolean initial_window_show(gpointer data)
357 {
358         struct ui_psensor *ui;
359
360         ui = (struct ui_psensor *)data;
361
362         log_printf(LOG_DEBUG,
363                    "is_status_supported: %d\n", is_status_supported());
364
365         if (!ui->config->hide_on_startup
366             || (!is_appindicator_supported() && !is_status_supported()))
367                 ui_window_show(ui);
368
369         ui_window_update(ui);
370
371         return FALSE;
372 }
373
374 int main(int argc, char **argv)
375 {
376         struct ui_psensor ui;
377         GError *error;
378         GThread *thread;
379         int optc;
380         char *url = NULL;
381         int cmdok = 1;
382
383         program_name = argv[0];
384
385         setlocale(LC_ALL, "");
386
387 #if ENABLE_NLS
388         bindtextdomain(PACKAGE, LOCALEDIR);
389         textdomain(PACKAGE);
390 #endif
391
392         while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
393                                    NULL)) != -1) {
394                 switch (optc) {
395                 case 'u':
396                         if (optarg)
397                                 url = strdup(optarg);
398                         break;
399                 case 'h':
400                         print_help();
401                         exit(EXIT_SUCCESS);
402                 case 'v':
403                         print_version();
404                         exit(EXIT_SUCCESS);
405                 case 'd':
406                         printf(_("Enables debug mode.\n"));
407                         log_level = LOG_DEBUG;
408                         break;
409                 default:
410                         cmdok = 0;
411                         break;
412                 }
413         }
414
415         if (!cmdok || optind != argc) {
416                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
417                         program_name);
418                 exit(EXIT_FAILURE);
419         }
420
421         log_init();
422
423         g_thread_init(NULL);
424         gdk_threads_init();
425         /* gdk_threads_enter(); */
426
427         gtk_init(NULL, NULL);
428
429 #ifdef HAVE_LIBNOTIFY
430         ui.notification_last_time = NULL;
431 #endif
432
433         ui.sensors_mutex = g_mutex_new();
434
435         config_init();
436
437         ui.config = config_load();
438
439         psensor_init();
440
441         if (url) {
442 #ifdef HAVE_REMOTE_SUPPORT
443                 rsensor_init();
444                 ui.sensors = get_remote_sensors(url, 600);
445 #else
446                 fprintf(stderr,
447                         _("ERROR: Not compiled with remote sensor support.\n"));
448                 exit(EXIT_FAILURE);
449 #endif
450         } else {
451                 ui.sensors = get_all_sensors(600);
452 #ifdef HAVE_NVIDIA
453                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
454 #endif
455 #ifdef HAVE_LIBATIADL
456                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
457 #endif
458 #ifdef HAVE_GTOP
459                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
460 #endif
461         }
462
463         associate_preferences(ui.sensors);
464         associate_colors(ui.sensors);
465         associate_cb_alarm_raised(ui.sensors, &ui);
466
467         ui_status_init(&ui);
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         /*
480          * show the window as soon as all gtk events have been processed
481          * in order to ensure that the status icon is attempted to be
482          * drawn before. If not, there is no way to detect that it is
483          * visible.
484         */
485         g_idle_add((GSourceFunc)initial_window_show, &ui);
486
487         thread = g_thread_create((GThreadFunc) update_psensor_measures,
488                                  &ui, TRUE, &error);
489
490         if (!thread)
491                 g_error_free(error);
492
493         ui.graph_update_interval = ui.config->graph_update_interval;
494
495         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
496
497 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
498         ui_appindicator_init(&ui);
499 #endif
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 }