import psensor trunk from private svn
[psensor.git] / src / main.c
1 /*
2     Copyright (C) 2010-2011 wpitchoune@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
28 #include <gtk/gtk.h>
29
30 #include <sensors/sensors.h>
31 #include <sensors/error.h>
32
33 #include "config.h"
34
35 #include "cfg.h"
36 #include "hdd.h"
37 #include "psensor.h"
38 #include "graph.h"
39 #include "ui.h"
40 #include "ui_sensorlist.h"
41 #include "ui_color.h"
42 #include "lmsensor.h"
43 #include "ui_pref.h"
44 #include "ui_graph.h"
45
46 #ifdef HAVE_UNITY
47 #include "ui_unity.h"
48 #endif
49
50 #ifdef HAVE_NVIDIA
51 #include "nvidia.h"
52 #endif
53
54 #ifdef HAVE_REMOTE_SUPPORT
55 #include "rsensor.h"
56 #endif
57
58 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
59 #include "ui_appindicator.h"
60 #endif
61
62 #ifdef HAVE_LIBNOTIFY
63 #include "ui_notify.h"
64 #endif
65
66 #include "compat.h"
67
68 static const char *program_name;
69
70 void print_version()
71 {
72         printf("psensor %s\n", VERSION);
73         printf(_("Copyright (C) %s wpitchoune@gmail.com\n\
74 License GPLv2: GNU GPL version 2 or later \
75 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
76 This is free software: you are free to change and redistribute it.\n\
77 There is NO WARRANTY, to the extent permitted by law.\n"),
78                "2010-2011");
79 }
80
81 void print_help()
82 {
83         printf(_("Usage: %s [OPTION]...\n"), program_name);
84
85         puts(_("psensor is a GTK application for monitoring hardware sensors, "
86                "including temperatures and fan speeds."));
87
88         puts("");
89         puts(_("Options:"));
90         puts(_("\
91   -h, --help          display this help and exit\n\
92   -v, --version       display version information and exit"));
93
94         puts("");
95
96         puts(_("\
97   -u, --url=URL       \
98 the URL of the psensor-server, example: http://hostname:3131"));
99
100         puts("");
101
102         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
103         puts("");
104         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
105 }
106
107 /*
108   Updates the size of the sensor values if different than the
109   configuration.
110  */
111 void
112 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
113 {
114         struct psensor **cur;
115
116         cur = sensors;
117         while (*cur) {
118                 struct psensor *s = *cur;
119
120                 if (s->values_max_length != cfg->sensor_values_max_length)
121                         psensor_values_resize(s,
122                                               cfg->sensor_values_max_length);
123
124                 cur++;
125         }
126 }
127
128 void update_psensor_measures(struct ui_psensor *ui)
129 {
130         struct psensor **sensors = ui->sensors;
131         struct config *cfg = ui->config;
132
133         while (1) {
134                 /*gdk_threads_enter();*/
135                 g_mutex_lock(ui->sensors_mutex);
136
137                 if (!sensors)
138                         return;
139
140                 update_psensor_values_size(sensors, ui->config);
141
142                 psensor_list_update_measures(sensors);
143 #ifdef HAVE_REMOTE_SUPPORT
144                 remote_psensor_list_update(sensors);
145 #endif
146 #ifdef HAVE_NVIDIA
147                 nvidia_psensor_list_update(sensors);
148 #endif
149
150                 /*gdk_threads_leave();*/
151                 g_mutex_unlock(ui->sensors_mutex);
152
153                 sleep(cfg->sensor_update_interval);
154         }
155 }
156
157 gboolean ui_refresh_thread(gpointer data)
158 {
159         struct config *cfg;
160         gboolean ret;
161         struct ui_psensor *ui = (struct ui_psensor *)data;
162
163         ret = TRUE;
164         cfg = ui->config;
165
166         g_mutex_lock(ui->sensors_mutex);
167         /*gdk_threads_enter();*/
168
169         graph_update(ui->sensors, ui->w_graph, ui->config);
170
171         ui_sensorlist_update(ui->ui_sensorlist);
172
173 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
174         ui_appindicator_update(ui);
175 #endif
176
177 #ifdef HAVE_UNITY
178         ui_unity_launcher_entry_update(ui->sensors);
179 #endif
180
181         if (ui->graph_update_interval != cfg->graph_update_interval) {
182                 ui->graph_update_interval = cfg->graph_update_interval;
183                 ret = FALSE;
184         }
185
186         g_mutex_unlock(ui->sensors_mutex);
187         /*gdk_threads_leave();*/
188
189         if (ret == FALSE)
190                 g_timeout_add(1000 * ui->graph_update_interval,
191                               ui_refresh_thread, ui);
192
193         return ret;
194 }
195
196 void cb_alarm_raised(struct psensor *sensor, void *data)
197 {
198 #ifdef HAVE_LIBNOTIFY
199         if (sensor->enabled)
200                 ui_notify(sensor, (struct ui_psensor *)data);
201 #endif
202 }
203
204 void associate_colors(struct psensor **sensors)
205 {
206         /* number of uniq colors */
207 #define COLORS_COUNT 8
208
209         unsigned int colors[COLORS_COUNT][3] = {
210                 {0x0000, 0x0000, 0x0000},       /* black */
211                 {0xffff, 0x0000, 0x0000},       /* red */
212                 {0x0000, 0.0000, 0xffff},       /* blue */
213                 {0x0000, 0xffff, 0x0000},       /* green */
214
215                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
216                 {0x7fff, 0x0000, 0x0000},       /* dark red */
217                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
218                 {0x0000, 0x7fff, 0x0000}        /* dark green */
219         };
220
221         struct psensor **sensor_cur = sensors;
222         int i = 0;
223         while (*sensor_cur) {
224                 struct color default_color;
225                 color_set(&default_color,
226                           colors[i % COLORS_COUNT][0],
227                           colors[i % COLORS_COUNT][1],
228                           colors[i % COLORS_COUNT][2]);
229
230                 (*sensor_cur)->color
231                     = config_get_sensor_color((*sensor_cur)->id,
232                                               &default_color);
233
234                 sensor_cur++;
235                 i++;
236         }
237 }
238
239 void
240 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
241 {
242         struct psensor **sensor_cur = sensors;
243         while (*sensor_cur) {
244                 struct psensor *s = *sensor_cur;
245
246                 s->cb_alarm_raised = cb_alarm_raised;
247                 s->cb_alarm_raised_data = ui;
248
249                 if (is_temp_type(s->type)) {
250                         s->alarm_limit
251                             = config_get_sensor_alarm_limit(s->id, 60);
252                         s->alarm_enabled
253                             = config_get_sensor_alarm_enabled(s->id);
254                 } else {
255                         s->alarm_limit = 0;
256                         s->alarm_enabled = 0;
257                 }
258
259                 sensor_cur++;
260         }
261 }
262
263 void associate_preferences(struct psensor **sensors)
264 {
265         struct psensor **sensor_cur = sensors;
266         while (*sensor_cur) {
267                 char *n;
268                 struct psensor *s = *sensor_cur;
269
270                 s->enabled = config_is_sensor_enabled(s->id);
271
272                 n = config_get_sensor_name(s->id);
273
274                 if (n)
275                         s->name = n;
276
277                 sensor_cur++;
278         }
279 }
280
281
282 static struct option long_options[] = {
283         {"version", no_argument, 0, 'v'},
284         {"help", no_argument, 0, 'h'},
285         {"url", required_argument, 0, 'u'},
286         {0, 0, 0, 0}
287 };
288
289 int main(int argc, char **argv)
290 {
291         struct ui_psensor ui;
292         GError *error;
293         GThread *thread;
294         int err, optc;
295         char *url = NULL;
296         int cmdok = 1;
297
298         program_name = argv[0];
299
300         setlocale(LC_ALL, "");
301
302 #if ENABLE_NLS
303         bindtextdomain(PACKAGE, LOCALEDIR);
304         textdomain(PACKAGE);
305 #endif
306
307         while ((optc = getopt_long(argc, argv, "vhu:", long_options,
308                                    NULL)) != -1) {
309                 switch (optc) {
310                 case 'u':
311                         if (optarg)
312                                 url = strdup(optarg);
313                         break;
314                 case 'h':
315                         print_help();
316                         exit(EXIT_SUCCESS);
317                 case 'v':
318                         print_version();
319                         exit(EXIT_SUCCESS);
320                 default:
321                         cmdok = 0;
322                         break;
323                 }
324         }
325
326         if (!cmdok || optind != argc) {
327                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
328                         program_name);
329                 exit(EXIT_FAILURE);
330         }
331
332         g_thread_init(NULL);
333         gdk_threads_init();
334         /* gdk_threads_enter(); */
335
336         gtk_init(&argc, &argv);
337
338 #ifdef HAVE_LIBNOTIFY
339         ui.notification_last_time = NULL;
340 #endif
341
342         ui.sensors_mutex = g_mutex_new();
343
344         config_init();
345
346         ui.config = config_load();
347
348         err = lmsensor_init();
349         if (!err) {
350                 fprintf(stderr, _("ERROR: lmsensor init failure: %s\n"),
351                         sensors_strerror(err));
352                 exit(EXIT_FAILURE);
353         }
354
355         if (url) {
356 #ifdef HAVE_REMOTE_SUPPORT
357                 rsensor_init();
358                 ui.sensors = get_remote_sensors(url, 600);
359 #else
360                 fprintf(stderr,
361                         _("ERROR: Not compiled with remote sensor support.\n"));
362                 exit(EXIT_FAILURE);
363 #endif
364         } else {
365 #ifdef HAVE_NVIDIA
366                 struct psensor **tmp;
367
368                 tmp = get_all_sensors(600);
369                 ui.sensors = nvidia_psensor_list_add(tmp, 600);
370
371                 if (tmp != ui.sensors)
372                         free(tmp);
373 #else
374                 ui.sensors = get_all_sensors(600);
375 #endif
376         }
377
378         associate_preferences(ui.sensors);
379         associate_colors(ui.sensors);
380         associate_cb_alarm_raised(ui.sensors, &ui);
381
382         /* main window */
383         ui.main_window = ui_window_create(&ui);
384         ui.main_box = NULL;
385
386         /* drawing box */
387         ui.w_graph = ui_graph_create(&ui);
388
389         /* sensor list */
390         ui.ui_sensorlist = ui_sensorlist_create(ui.sensors);
391
392         ui_main_box_create(&ui);
393
394         gtk_widget_show_all(ui.main_window);
395
396         thread = g_thread_create((GThreadFunc) update_psensor_measures,
397                                  &ui, TRUE, &error);
398
399         if (!thread)
400                 g_error_free(error);
401
402         ui.graph_update_interval = ui.config->graph_update_interval;
403
404         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
405
406 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
407         ui_appindicator_init(&ui);
408 #endif
409
410         /* main loop */
411         gtk_main();
412
413         sensors_cleanup();
414
415         psensor_list_free(ui.sensors);
416
417         /* gdk_threads_leave(); */
418
419         return 0;
420 }