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