4daa4b75e907763421ae13053dbf213838c7cb34
[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);
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         while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
350                                    NULL)) != -1) {
351                 switch (optc) {
352                 case 'u':
353                         if (optarg)
354                                 url = strdup(optarg);
355                         break;
356                 case 'h':
357                         print_help();
358                         exit(EXIT_SUCCESS);
359                 case 'v':
360                         print_version();
361                         exit(EXIT_SUCCESS);
362                 case 'd':
363                         printf(_("Enables debug mode.\n"));
364                         log_level = LOG_DEBUG;
365                         break;
366                 default:
367                         cmdok = 0;
368                         break;
369                 }
370         }
371
372         if (!cmdok || optind != argc) {
373                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
374                         program_name);
375                 exit(EXIT_FAILURE);
376         }
377
378         log_init();
379
380         g_thread_init(NULL);
381         gdk_threads_init();
382         /* gdk_threads_enter(); */
383
384         gtk_init(NULL, NULL);
385
386 #ifdef HAVE_LIBNOTIFY
387         ui.notification_last_time = NULL;
388 #endif
389
390         ui.sensors_mutex = g_mutex_new();
391
392         config_init();
393
394         ui.config = config_load();
395
396         psensor_init();
397
398         if (url) {
399 #ifdef HAVE_REMOTE_SUPPORT
400                 rsensor_init();
401                 ui.sensors = get_remote_sensors(url, 600);
402 #else
403                 fprintf(stderr,
404                         _("ERROR: Not compiled with remote sensor support.\n"));
405                 exit(EXIT_FAILURE);
406 #endif
407         } else {
408                 ui.sensors = get_all_sensors(600);
409 #ifdef HAVE_NVIDIA
410                 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
411 #endif
412 #ifdef HAVE_LIBATIADL
413                 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
414 #endif
415 #ifdef HAVE_GTOP
416                 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
417 #endif
418         }
419
420         associate_preferences(ui.sensors);
421         associate_colors(ui.sensors);
422         associate_cb_alarm_raised(ui.sensors, &ui);
423
424         /* main window */
425         ui_window_create(&ui);
426         ui.sensor_box = NULL;
427
428         /* drawing box */
429         ui.w_graph = ui_graph_create(&ui);
430
431         /* sensor list */
432         ui_sensorlist_create(&ui);
433
434         ui_window_update(&ui);
435
436         thread = g_thread_create((GThreadFunc) update_psensor_measures,
437                                  &ui, TRUE, &error);
438
439         if (!thread)
440                 g_error_free(error);
441
442         ui.graph_update_interval = ui.config->graph_update_interval;
443
444         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
445
446 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
447         ui_appindicator_init(&ui);
448 #endif
449
450         gdk_notify_startup_complete();
451
452         /* main loop */
453         gtk_main();
454
455         g_mutex_lock(ui.sensors_mutex);
456
457         psensor_cleanup();
458
459 #ifdef HAVE_NVIDIA
460         nvidia_cleanup();
461 #endif
462 #ifdef HAVE_LIBATIADL
463         amd_cleanup();
464 #endif
465
466         psensor_list_free(ui.sensors);
467         ui.sensors = NULL;
468
469         g_mutex_unlock(ui.sensors_mutex);
470
471         config_cleanup();
472
473         log_close();
474
475         return 0;
476 }