fixed compilation with libnotify 0.5.2 (submitted by Lubos
[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
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 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
61 #include "ui_appindicator.h"
62 #endif
63
64 #ifdef HAVE_LIBNOTIFY
65 #include "ui_notify.h"
66 #endif
67
68 #ifdef HAVE_GTOP
69 #include "cpu.h"
70 #endif
71
72 #include "compat.h"
73
74 static const char *program_name;
75
76 static void print_version()
77 {
78         printf("psensor %s\n", VERSION);
79         printf(_("Copyright (C) %s jeanfi@gmail.com\n\
80 License GPLv2: GNU GPL version 2 or later \
81 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
82 This is free software: you are free to change and redistribute it.\n\
83 There is NO WARRANTY, to the extent permitted by law.\n"),
84                "2010-2011");
85 }
86
87 static void print_help()
88 {
89         printf(_("Usage: %s [OPTION]...\n"), program_name);
90
91         puts(_("psensor is a GTK application for monitoring hardware sensors, "
92                "including temperatures and fan speeds."));
93
94         puts("");
95         puts(_("Options:"));
96         puts(_("\
97   -h, --help          display this help and exit\n\
98   -v, --version       display version information and exit"));
99
100         puts("");
101
102         puts(_("\
103   -u, --url=URL       \
104 the URL of the psensor-server, example: http://hostname:3131"));
105
106         puts("");
107
108         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
109         puts("");
110         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
111 }
112
113 /*
114   Updates the size of the sensor values if different than the
115   configuration.
116  */
117 void
118 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
119 {
120         struct psensor **cur;
121
122         cur = sensors;
123         while (*cur) {
124                 struct psensor *s = *cur;
125
126                 if (s->values_max_length != cfg->sensor_values_max_length)
127                         psensor_values_resize(s,
128                                               cfg->sensor_values_max_length);
129
130                 cur++;
131         }
132 }
133
134 static void log_measures(struct psensor **sensors)
135 {
136         if (log_level == LOG_DEBUG)
137                 while (*sensors) {
138                         log_printf(LOG_DEBUG, "%s %.2f",
139                                    (*sensors)->name,
140                                    psensor_get_current_value(*sensors));
141
142                         sensors++;
143                 }
144 }
145
146 void update_psensor_measures(struct ui_psensor *ui)
147 {
148         struct psensor **sensors = ui->sensors;
149         struct config *cfg = ui->config;
150
151         while (1) {
152                 g_mutex_lock(ui->sensors_mutex);
153
154                 if (!sensors)
155                         return;
156
157                 update_psensor_values_size(sensors, ui->config);
158
159                 psensor_list_update_measures(sensors);
160 #ifdef HAVE_REMOTE_SUPPORT
161                 remote_psensor_list_update(sensors);
162 #endif
163 #ifdef HAVE_NVIDIA
164                 nvidia_psensor_list_update(sensors);
165 #endif
166 #ifdef HAVE_LIBATIADL
167                 amd_psensor_list_update(sensors);
168 #endif
169
170                 log_measures(sensors);
171
172                 g_mutex_unlock(ui->sensors_mutex);
173
174                 sleep(cfg->sensor_update_interval);
175         }
176 }
177
178 gboolean ui_refresh_thread(gpointer data)
179 {
180         struct config *cfg;
181         gboolean ret;
182         struct ui_psensor *ui = (struct ui_psensor *)data;
183
184         ret = TRUE;
185         cfg = ui->config;
186
187         g_mutex_lock(ui->sensors_mutex);
188
189         graph_update(ui->sensors, ui->w_graph, ui->config);
190
191         ui_sensorlist_update(ui);
192
193 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
194         ui_appindicator_update(ui);
195 #endif
196
197 #ifdef HAVE_UNITY
198         ui_unity_launcher_entry_update(ui->sensors,
199                                        !cfg->unity_launcher_count_disabled);
200 #endif
201
202         if (ui->graph_update_interval != cfg->graph_update_interval) {
203                 ui->graph_update_interval = cfg->graph_update_interval;
204                 ret = FALSE;
205         }
206
207         g_mutex_unlock(ui->sensors_mutex);
208
209         if (ret == FALSE)
210                 g_timeout_add(1000 * ui->graph_update_interval,
211                               ui_refresh_thread, ui);
212
213         return ret;
214 }
215
216 void cb_alarm_raised(struct psensor *sensor, void *data)
217 {
218 #ifdef HAVE_LIBNOTIFY
219         if (sensor->enabled)
220                 ui_notify(sensor, (struct ui_psensor *)data);
221 #endif
222 }
223
224 static void associate_colors(struct psensor **sensors)
225 {
226         /* number of uniq colors */
227 #define COLORS_COUNT 8
228
229         unsigned int colors[COLORS_COUNT][3] = {
230                 {0x0000, 0x0000, 0x0000},       /* black */
231                 {0xffff, 0x0000, 0x0000},       /* red */
232                 {0x0000, 0.0000, 0xffff},       /* blue */
233                 {0x0000, 0xffff, 0x0000},       /* green */
234
235                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
236                 {0x7fff, 0x0000, 0x0000},       /* dark red */
237                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
238                 {0x0000, 0x7fff, 0x0000}        /* dark green */
239         };
240
241         struct psensor **sensor_cur = sensors;
242         int i = 0;
243         while (*sensor_cur) {
244                 struct color default_color;
245                 color_set(&default_color,
246                           colors[i % COLORS_COUNT][0],
247                           colors[i % COLORS_COUNT][1],
248                           colors[i % COLORS_COUNT][2]);
249
250                 (*sensor_cur)->color
251                     = config_get_sensor_color((*sensor_cur)->id,
252                                               &default_color);
253
254                 sensor_cur++;
255                 i++;
256         }
257 }
258
259 static void
260 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
261 {
262         struct psensor **sensor_cur = sensors;
263         while (*sensor_cur) {
264                 struct psensor *s = *sensor_cur;
265
266                 s->cb_alarm_raised = cb_alarm_raised;
267                 s->cb_alarm_raised_data = ui;
268
269                 if (is_temp_type(s->type)) {
270                         s->alarm_limit
271                             = config_get_sensor_alarm_limit(s->id, 60);
272                         s->alarm_enabled
273                             = config_get_sensor_alarm_enabled(s->id);
274                 } else {
275                         s->alarm_limit = 0;
276                         s->alarm_enabled = 0;
277                 }
278
279                 sensor_cur++;
280         }
281 }
282
283 static void associate_preferences(struct psensor **sensors)
284 {
285         struct psensor **sensor_cur = sensors;
286         while (*sensor_cur) {
287                 char *n;
288                 struct psensor *s = *sensor_cur;
289
290                 s->enabled = config_is_sensor_enabled(s->id);
291
292                 n = config_get_sensor_name(s->id);
293
294                 if (n)
295                         s->name = n;
296
297                 sensor_cur++;
298         }
299 }
300
301 static void log_init()
302 {
303         char *home, *path, *dir;
304
305         home = getenv("HOME");
306
307         if (!home)
308                 return ;
309
310         dir = malloc(strlen(home)+1+strlen(".psensor")+1);
311         sprintf(dir, "%s/%s", home, ".psensor");
312         mkdir(dir, 0777);
313
314         path = malloc(strlen(dir)+1+strlen("log")+1);
315         sprintf(path, "%s/%s", dir, "log");
316
317         log_open(path, LOG_INFO);
318
319         free(dir);
320         free(path);
321 }
322
323 static struct option long_options[] = {
324         {"version", no_argument, 0, 'v'},
325         {"help", no_argument, 0, 'h'},
326         {"url", required_argument, 0, 'u'},
327         {"debug", no_argument, 0, 'd'},
328         {0, 0, 0, 0}
329 };
330
331 int main(int argc, char **argv)
332 {
333         struct ui_psensor ui;
334         GError *error;
335         GThread *thread;
336         int optc;
337         char *url = NULL;
338         int cmdok = 1;
339
340         program_name = argv[0];
341
342         setlocale(LC_ALL, "");
343
344 #if ENABLE_NLS
345         bindtextdomain(PACKAGE, LOCALEDIR);
346         textdomain(PACKAGE);
347 #endif
348
349 #if TUTU
350         printf("coucou\n");
351 #endif
352
353         while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
354                                    NULL)) != -1) {
355                 switch (optc) {
356                 case 'u':
357                         if (optarg)
358                                 url = strdup(optarg);
359                         break;
360                 case 'h':
361                         print_help();
362                         exit(EXIT_SUCCESS);
363                 case 'v':
364                         print_version();
365                         exit(EXIT_SUCCESS);
366                 case 'd':
367                         printf(_("Enables debug mode.\n"));
368                         log_level = LOG_DEBUG;
369                         break;
370                 default:
371                         cmdok = 0;
372                         break;
373                 }
374         }
375
376         if (!cmdok || optind != argc) {
377                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
378                         program_name);
379                 exit(EXIT_FAILURE);
380         }
381
382         log_init();
383
384         g_thread_init(NULL);
385         gdk_threads_init();
386         /* gdk_threads_enter(); */
387
388         gtk_init(NULL, NULL);
389
390 #ifdef HAVE_LIBNOTIFY
391         ui.notification_last_time = NULL;
392 #endif
393
394         ui.sensors_mutex = g_mutex_new();
395
396         config_init();
397
398         ui.config = config_load();
399
400         psensor_init();
401
402         if (url) {
403 #ifdef HAVE_REMOTE_SUPPORT
404                 rsensor_init();
405                 ui.sensors = get_remote_sensors(url, 600);
406 #else
407                 fprintf(stderr,
408                         _("ERROR: Not compiled with remote sensor support.\n"));
409                 exit(EXIT_FAILURE);
410 #endif
411         } else {
412                 ui.sensors = get_all_sensors(600);
413 #ifdef HAVE_NVIDIA
414                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
415 #endif
416 #ifdef HAVE_LIBATIADL
417                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
418 #endif
419 #ifdef HAVE_GTOP
420                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
421 #endif
422         }
423
424         associate_preferences(ui.sensors);
425         associate_colors(ui.sensors);
426         associate_cb_alarm_raised(ui.sensors, &ui);
427
428         /* main window */
429         ui_window_create(&ui);
430         ui.sensor_box = NULL;
431
432         /* drawing box */
433         ui.w_graph = ui_graph_create(&ui);
434
435         /* sensor list */
436         ui_sensorlist_create(&ui);
437
438         ui_window_update(&ui);
439
440         thread = g_thread_create((GThreadFunc) update_psensor_measures,
441                                  &ui, TRUE, &error);
442
443         if (!thread)
444                 g_error_free(error);
445
446         ui.graph_update_interval = ui.config->graph_update_interval;
447
448         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
449
450 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
451         ui_appindicator_init(&ui);
452 #endif
453
454         gdk_notify_startup_complete();
455
456         /* main loop */
457         gtk_main();
458
459         g_mutex_lock(ui.sensors_mutex);
460
461         psensor_cleanup();
462
463 #ifdef HAVE_NVIDIA
464         nvidia_cleanup();
465 #endif
466 #ifdef HAVE_LIBATIADL
467         amd_cleanup();
468 #endif
469
470         psensor_list_free(ui.sensors);
471         ui.sensors = NULL;
472
473         g_mutex_unlock(ui.sensors_mutex);
474
475         config_cleanup();
476
477         log_close();
478
479         return 0;
480 }