d981976535a4ffcdff40955e2c4731a1d315674a
[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 #include <libintl.h>
22 #define _(str) gettext(str)
23
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include <gtk/gtk.h>
31
32 #include <sensors/sensors.h>
33 #include <sensors/error.h>
34
35 #include "config.h"
36
37 #include "cfg.h"
38 #include "hdd.h"
39 #include "psensor.h"
40 #include "graph.h"
41 #include "ui.h"
42 #include "ui_sensorlist.h"
43 #include "ui_color.h"
44 #include "lmsensor.h"
45 #include "ui_pref.h"
46
47 #ifdef HAVE_NVIDIA
48 #include "nvidia.h"
49 #endif
50
51 #ifdef HAVE_REMOTE_SUPPORT
52 #include "rsensor.h"
53 #endif
54
55 #ifdef HAVE_APPINDICATOR
56 #include "ui_appindicator.h"
57 #endif
58
59 #ifdef HAVE_LIBNOTIFY
60 #include "ui_notify.h"
61 #endif
62
63 #include "compat.h"
64
65 static const char *program_name;
66
67 void print_version()
68 {
69         printf("psensor %s\n", VERSION);
70         printf(_("Copyright (C) %s wpitchoune@gmail.com\n\
71 License GPLv2: GNU GPL version 2 or later \
72 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
73 This is free software: you are free to change and redistribute it.\n\
74 There is NO WARRANTY, to the extent permitted by law.\n"),
75                "2010-2011");
76 }
77
78 void print_help()
79 {
80         printf(_("Usage: %s [OPTION]...\n"), program_name);
81
82         puts(_("psensor is a GTK application for monitoring hardware sensors, "
83                "including temperatures and fan speeds."));
84
85         puts("");
86         puts("Options:");
87         puts(_("\
88   -h, --help          display this help and exit\n\
89   -v, --version       display version information and exit"));
90
91         puts("");
92
93         puts(_("\
94   -u, --url=URL       \
95 the URL of the psensor-server, example: http://hostname:3131"));
96
97         puts("");
98
99         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
100         puts("");
101         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
102 }
103
104 static void
105 cb_preferences(gpointer data, guint callback_action, GtkWidget *item)
106 {
107         ui_pref_dialog_run((struct ui_psensor *)data);
108 }
109
110 static GtkItemFactoryEntry menu_items[] = {
111         {"/Preferences",
112          NULL, cb_preferences, 0, "<Item>"},
113
114         {"/sep1",
115          NULL, NULL, 0, "<Separator>"},
116
117         {"/Quit",
118          "", ui_psensor_exit, 0, "<StockItem>", GTK_STOCK_QUIT},
119 };
120
121 static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
122
123 /*
124   Updates the size of the sensor values if different than the
125   configuration.
126  */
127 void
128 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
129 {
130         struct psensor **cur;
131
132         cur = sensors;
133         while (*cur) {
134                 struct psensor *s = *cur;
135
136                 if (s->values_max_length != cfg->sensor_values_max_length)
137                         psensor_values_resize(s,
138                                               cfg->sensor_values_max_length);
139
140                 cur++;
141         }
142 }
143
144 void update_psensor_measures(struct ui_psensor *ui)
145 {
146         struct psensor **sensors = ui->sensors;
147         struct config *cfg = ui->config;
148
149         while (1) {
150                 gdk_threads_enter();
151
152                 if (!sensors)
153                         return;
154
155                 update_psensor_values_size(sensors, ui->config);
156
157                 psensor_list_update_measures(sensors);
158 #ifdef HAVE_REMOTE_SUPPORT
159                 remote_psensor_list_update(sensors);
160 #endif
161 #ifdef HAVE_NVIDIA
162                 nvidia_psensor_list_update(sensors);
163 #endif
164
165                 gdk_threads_leave();
166
167                 sleep(cfg->sensor_update_interval);
168         }
169 }
170
171 gboolean ui_refresh_thread(gpointer data)
172 {
173         struct config *cfg;
174         gboolean ret;
175         struct ui_psensor *ui = (struct ui_psensor *)data;
176
177         ret = TRUE;
178         cfg = ui->config;
179
180         gdk_threads_enter();
181
182         graph_update(ui->sensors, ui->w_graph, ui->config);
183
184         ui_sensorlist_update(ui->ui_sensorlist);
185
186 #ifdef HAVE_APPINDICATOR
187         ui_appindicator_update(ui);
188 #endif
189
190         if (ui->graph_update_interval != cfg->graph_update_interval) {
191                 ui->graph_update_interval = cfg->graph_update_interval;
192                 ret = FALSE;
193         }
194
195         gdk_threads_leave();
196
197         if (ret == FALSE)
198                 g_timeout_add(1000 * ui->graph_update_interval,
199                               ui_refresh_thread, ui);
200
201         return ret;
202 }
203
204 gboolean
205 on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
206 {
207         struct ui_psensor *ui_psensor = (struct ui_psensor *)data;
208
209         graph_update(ui_psensor->sensors,
210                      ui_psensor->w_graph, ui_psensor->config);
211
212         return FALSE;
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 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 void associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
259 {
260         struct psensor **sensor_cur = sensors;
261         while (*sensor_cur) {
262                 struct psensor *s = *sensor_cur;
263
264                 s->cb_alarm_raised = cb_alarm_raised;
265                 s->cb_alarm_raised_data = ui;
266
267                 if (is_temp_type(s->type)) {
268                         s->alarm_limit
269                             = config_get_sensor_alarm_limit(s->id, 60);
270                         s->alarm_enabled
271                             = config_get_sensor_alarm_enabled(s->id);
272                 } else {
273                         s->alarm_limit = 0;
274                         s->alarm_enabled = 0;
275                 }
276
277                 sensor_cur++;
278         }
279 }
280
281 void associate_preferences(struct psensor **sensors)
282 {
283         struct psensor **sensor_cur = sensors;
284         while (*sensor_cur) {
285                 char *n;
286                 struct psensor *s = *sensor_cur;
287
288                 s->enabled = config_is_sensor_enabled(s->id);
289
290                 n = config_get_sensor_name(s->id);
291
292                 if (n)
293                         s->name = n;
294
295                 sensor_cur++;
296         }
297 }
298
299 GtkWidget *ui_get_popupmenu(gpointer data)
300 {
301         GtkItemFactory *item_factory;
302         GtkWidget *menu;
303
304         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
305         gtk_item_factory_create_items(item_factory,
306                                       nmenu_items, menu_items, data);
307         menu = gtk_item_factory_get_widget(item_factory, "<main>");
308
309         return menu;
310 }
311
312 int on_graph_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
313 {
314         GtkWidget *menu;
315
316         if (event->type != GDK_BUTTON_PRESS)
317                 return FALSE;
318
319         menu = ui_get_popupmenu(data);
320
321         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
322                        event->button, event->time);
323
324         return TRUE;
325 }
326
327
328 GtkWidget *create_graph_widget(struct ui_psensor * ui)
329 {
330         GtkWidget *w_graph;
331
332         w_graph = gtk_drawing_area_new();
333
334         g_signal_connect(G_OBJECT(w_graph),
335                          "expose-event", G_CALLBACK(on_expose_event), ui);
336
337         gtk_widget_add_events(w_graph, GDK_BUTTON_PRESS_MASK);
338         gtk_signal_connect(GTK_OBJECT(w_graph),
339                            "button_press_event",
340                            (GCallback) on_graph_clicked, ui);
341
342         return w_graph;
343 }
344
345 void ui_main_box_create(struct ui_psensor *ui)
346 {
347         struct config *cfg;
348         GtkWidget *w_sensorlist;
349
350         cfg = ui->config;
351
352         if (ui->main_box) {
353                 ui_sensorlist_create_widget(ui->ui_sensorlist);
354
355                 gtk_container_remove(GTK_CONTAINER(ui->main_window),
356                                      ui->main_box);
357
358                 ui->w_graph = create_graph_widget(ui);
359                 ui->w_sensorlist = ui->ui_sensorlist->widget;
360         }
361
362         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
363             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
364                 ui->main_box = gtk_hpaned_new();
365         else
366                 ui->main_box = gtk_vpaned_new();
367
368         w_sensorlist = gtk_scrolled_window_new(NULL, NULL);
369         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w_sensorlist),
370                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
371         gtk_container_add(GTK_CONTAINER(w_sensorlist),
372                           ui->ui_sensorlist->widget);
373
374         gtk_container_add(GTK_CONTAINER(ui->main_window), ui->main_box);
375
376         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
377             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
378                 gtk_paned_pack1(GTK_PANED(ui->main_box),
379                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
380                 gtk_paned_pack2(GTK_PANED(ui->main_box),
381                                 w_sensorlist, FALSE, TRUE);
382         } else {
383                 gtk_paned_pack1(GTK_PANED(ui->main_box),
384                                 w_sensorlist, FALSE, TRUE);
385                 gtk_paned_pack2(GTK_PANED(ui->main_box),
386                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
387         }
388
389         gtk_widget_show_all(ui->main_box);
390 }
391
392 static struct option long_options[] = {
393         {"version", no_argument, 0, 'v'},
394         {"help", no_argument, 0, 'h'},
395         {"url", required_argument, 0, 'u'},
396         {0, 0, 0, 0}
397 };
398
399 int main(int argc, char **argv)
400 {
401         struct ui_psensor ui;
402         GError *error;
403         GThread *thread;
404         int err, optc;
405         char *url = NULL;
406         int cmdok = 1;
407
408         program_name = argv[0];
409
410         setlocale(LC_ALL, "");
411
412 #if ENABLE_NLS
413         bindtextdomain(PACKAGE, LOCALEDIR);
414         textdomain(PACKAGE);
415 #endif
416
417         while ((optc = getopt_long(argc, argv, "vhu:", long_options,
418                                    NULL)) != -1) {
419                 switch (optc) {
420                 case 'u':
421                         if (optarg)
422                                 url = strdup(optarg);
423                         break;
424                 case 'h':
425                         print_help();
426                         exit(EXIT_SUCCESS);
427                 case 'v':
428                         print_version();
429                         exit(EXIT_SUCCESS);
430                 default:
431                         cmdok = 0;
432                         break;
433                 }
434         }
435
436         if (!cmdok || optind != argc) {
437                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
438                         program_name);
439                 exit(EXIT_FAILURE);
440         }
441
442         g_thread_init(NULL);
443         gdk_threads_init();
444         gdk_threads_enter();
445
446         gtk_init(&argc, &argv);
447
448 #ifdef HAVE_LIBNOTIFY
449         ui.notification_last_time = NULL;
450 #endif
451
452         config_init();
453
454         ui.config = config_load();
455
456         err = lmsensor_init();
457         if (!err) {
458                 fprintf(stderr, _("ERROR: lmsensor init failure: %s\n"),
459                         sensors_strerror(err));
460                 exit(EXIT_FAILURE);
461         }
462
463         if (url) {
464 #ifdef HAVE_REMOTE_SUPPORT
465                 rsensor_init();
466                 ui.sensors = get_remote_sensors(url, 600);
467 #else
468                 fprintf(stderr,
469                         _("ERROR: Not compiled with remote sensor support.\n"));
470                 exit(EXIT_FAILURE);
471 #endif
472         } else {
473 #ifdef HAVE_NVIDIA
474                 struct psensor **tmp;
475
476                 tmp = get_all_sensors(600);
477                 ui.sensors = nvidia_psensor_list_add(tmp, 600);
478
479                 if (tmp != ui.sensors)
480                         free(tmp);
481 #else
482                 ui.sensors = get_all_sensors(600);
483 #endif
484         }
485
486         associate_preferences(ui.sensors);
487         associate_colors(ui.sensors);
488         associate_cb_alarm_raised(ui.sensors, &ui);
489
490         /* main window */
491         ui.main_window = ui_window_create(&ui);
492         ui.main_box = NULL;
493
494         /* drawing box */
495         ui.w_graph = create_graph_widget(&ui);
496
497         /* sensor list */
498         ui.ui_sensorlist = ui_sensorlist_create(ui.sensors);
499
500         ui_main_box_create(&ui);
501
502         gtk_widget_show_all(ui.main_window);
503
504         thread = g_thread_create((GThreadFunc) update_psensor_measures,
505                                  &ui, TRUE, &error);
506
507         if (!thread)
508                 g_error_free(error);
509
510         ui.graph_update_interval = ui.config->graph_update_interval;
511
512         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
513
514 #ifdef HAVE_APPINDICATOR
515         ui_appindicator_init(&ui);
516 #endif
517
518         /* main loop */
519         gtk_main();
520
521         sensors_cleanup();
522
523         psensor_list_free(ui.sensors);
524
525         gdk_threads_leave();
526
527         return 0;
528 }