status icon support
[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 gboolean ui_refresh_thread(gpointer data)
178 {
179         struct config *cfg;
180         gboolean ret;
181         struct ui_psensor *ui = (struct ui_psensor *)data;
182
183         ret = TRUE;
184         cfg = ui->config;
185
186         g_mutex_lock(ui->sensors_mutex);
187
188         graph_update(ui->sensors, ui->w_graph, ui->config);
189
190         ui_sensorlist_update(ui);
191
192 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
193         ui_appindicator_update(ui);
194 #endif
195
196 #ifdef HAVE_UNITY
197         ui_unity_launcher_entry_update(ui->sensors,
198                                        !cfg->unity_launcher_count_disabled);
199 #endif
200
201         if (ui->graph_update_interval != cfg->graph_update_interval) {
202                 ui->graph_update_interval = cfg->graph_update_interval;
203                 ret = FALSE;
204         }
205
206         g_mutex_unlock(ui->sensors_mutex);
207
208         if (ret == FALSE)
209                 g_timeout_add(1000 * ui->graph_update_interval,
210                               ui_refresh_thread, ui);
211
212         return ret;
213 }
214
215 void cb_alarm_raised(struct psensor *sensor, void *data)
216 {
217 #ifdef HAVE_LIBNOTIFY
218         if (sensor->enabled)
219                 ui_notify(sensor, (struct ui_psensor *)data);
220 #endif
221 }
222
223 static void associate_colors(struct psensor **sensors)
224 {
225         /* number of uniq colors */
226 #define COLORS_COUNT 8
227
228         unsigned int colors[COLORS_COUNT][3] = {
229                 {0x0000, 0x0000, 0x0000},       /* black */
230                 {0xffff, 0x0000, 0x0000},       /* red */
231                 {0x0000, 0.0000, 0xffff},       /* blue */
232                 {0x0000, 0xffff, 0x0000},       /* green */
233
234                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
235                 {0x7fff, 0x0000, 0x0000},       /* dark red */
236                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
237                 {0x0000, 0x7fff, 0x0000}        /* dark green */
238         };
239
240         struct psensor **sensor_cur = sensors;
241         int i = 0;
242         while (*sensor_cur) {
243                 struct color default_color;
244                 color_set(&default_color,
245                           colors[i % COLORS_COUNT][0],
246                           colors[i % COLORS_COUNT][1],
247                           colors[i % COLORS_COUNT][2]);
248
249                 (*sensor_cur)->color
250                     = config_get_sensor_color((*sensor_cur)->id,
251                                               &default_color);
252
253                 sensor_cur++;
254                 i++;
255         }
256 }
257
258 static void
259 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
260 {
261         struct psensor **sensor_cur = sensors;
262         while (*sensor_cur) {
263                 struct psensor *s = *sensor_cur;
264
265                 s->cb_alarm_raised = cb_alarm_raised;
266                 s->cb_alarm_raised_data = ui;
267
268                 if (is_temp_type(s->type)) {
269                         s->alarm_limit
270                             = config_get_sensor_alarm_limit(s->id, 60);
271                         s->alarm_enabled
272                             = config_get_sensor_alarm_enabled(s->id);
273                 } else {
274                         s->alarm_limit = 0;
275                         s->alarm_enabled = 0;
276                 }
277
278                 sensor_cur++;
279         }
280 }
281
282 static void associate_preferences(struct psensor **sensors)
283 {
284         struct psensor **sensor_cur = sensors;
285         while (*sensor_cur) {
286                 char *n;
287                 struct psensor *s = *sensor_cur;
288
289                 s->enabled = config_is_sensor_enabled(s->id);
290
291                 n = config_get_sensor_name(s->id);
292
293                 if (n)
294                         s->name = n;
295
296                 sensor_cur++;
297         }
298 }
299
300 static void log_init()
301 {
302         char *home, *path, *dir;
303
304         home = getenv("HOME");
305
306         if (!home)
307                 return ;
308
309         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
310         sprintf(dir, "%s/%s", home, ".psensor");
311         mkdir(dir, 0777);
312
313         path = malloc(strlen(dir)+1+strlen("log")+1);
314         sprintf(path, "%s/%s", dir, "log");
315
316         log_open(path);
317
318         free(dir);
319         free(path);
320 }
321
322 static struct option long_options[] = {
323         {"version", no_argument, 0, 'v'},
324         {"help", no_argument, 0, 'h'},
325         {"url", required_argument, 0, 'u'},
326         {"debug", no_argument, 0, 'd'},
327         {0, 0, 0, 0}
328 };
329
330 int main(int argc, char **argv)
331 {
332         struct ui_psensor ui;
333         GError *error;
334         GThread *thread;
335         int optc;
336         char *url = NULL;
337         int cmdok = 1;
338
339         program_name = argv[0];
340
341         setlocale(LC_ALL, "");
342
343 #if ENABLE_NLS
344         bindtextdomain(PACKAGE, LOCALEDIR);
345         textdomain(PACKAGE);
346 #endif
347
348         while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
349                                    NULL)) != -1) {
350                 switch (optc) {
351                 case 'u':
352                         if (optarg)
353                                 url = strdup(optarg);
354                         break;
355                 case 'h':
356                         print_help();
357                         exit(EXIT_SUCCESS);
358                 case 'v':
359                         print_version();
360                         exit(EXIT_SUCCESS);
361                 case 'd':
362                         printf(_("Enables debug mode.\n"));
363                         log_level = LOG_DEBUG;
364                         break;
365                 default:
366                         cmdok = 0;
367                         break;
368                 }
369         }
370
371         if (!cmdok || optind != argc) {
372                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
373                         program_name);
374                 exit(EXIT_FAILURE);
375         }
376
377         log_init();
378
379         g_thread_init(NULL);
380         gdk_threads_init();
381         /* gdk_threads_enter(); */
382
383         gtk_init(NULL, NULL);
384
385 #ifdef HAVE_LIBNOTIFY
386         ui.notification_last_time = NULL;
387 #endif
388
389         ui.sensors_mutex = g_mutex_new();
390
391         config_init();
392
393         ui.config = config_load();
394
395         psensor_init();
396
397         if (url) {
398 #ifdef HAVE_REMOTE_SUPPORT
399                 rsensor_init();
400                 ui.sensors = get_remote_sensors(url, 600);
401 #else
402                 fprintf(stderr,
403                         _("ERROR: Not compiled with remote sensor support.\n"));
404                 exit(EXIT_FAILURE);
405 #endif
406         } else {
407                 ui.sensors = get_all_sensors(600);
408 #ifdef HAVE_NVIDIA
409                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
410 #endif
411 #ifdef HAVE_LIBATIADL
412                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
413 #endif
414 #ifdef HAVE_GTOP
415                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
416 #endif
417         }
418
419         associate_preferences(ui.sensors);
420         associate_colors(ui.sensors);
421         associate_cb_alarm_raised(ui.sensors, &ui);
422
423         ui_status_init(&ui);
424
425         /* main window */
426         ui_window_create(&ui);
427         ui.sensor_box = NULL;
428
429         /* drawing box */
430         ui.w_graph = ui_graph_create(&ui);
431
432         /* sensor list */
433         ui_sensorlist_create(&ui);
434
435         ui_window_update(&ui);
436
437         thread = g_thread_create((GThreadFunc) update_psensor_measures,
438                                  &ui, TRUE, &error);
439
440         if (!thread)
441                 g_error_free(error);
442
443         ui.graph_update_interval = ui.config->graph_update_interval;
444
445         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
446
447 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
448         ui_appindicator_init(&ui);
449 #endif
450
451         gdk_notify_startup_complete();
452
453         /* main loop */
454         gtk_main();
455
456         g_mutex_lock(ui.sensors_mutex);
457
458         psensor_cleanup();
459
460 #ifdef HAVE_NVIDIA
461         nvidia_cleanup();
462 #endif
463 #ifdef HAVE_LIBATIADL
464         amd_cleanup();
465 #endif
466 #ifdef HAVE_REMOTE_SUPPORT
467         rsensor_cleanup();
468 #endif
469
470         psensor_list_free(ui.sensors);
471         ui.sensors = NULL;
472
473 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
474         ui_appindicator_cleanup();
475 #endif
476
477         ui_status_cleanup();
478
479         g_mutex_unlock(ui.sensors_mutex);
480
481         config_cleanup();
482
483         log_close();
484
485         return 0;
486 }