added support of enable/disable unity launcher count
[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 "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 jeanfi@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                                        !cfg->unity_launcher_count_disabled);
180 #endif
181
182         if (ui->graph_update_interval != cfg->graph_update_interval) {
183                 ui->graph_update_interval = cfg->graph_update_interval;
184                 ret = FALSE;
185         }
186
187         g_mutex_unlock(ui->sensors_mutex);
188         /*gdk_threads_leave();*/
189
190         if (ret == FALSE)
191                 g_timeout_add(1000 * ui->graph_update_interval,
192                               ui_refresh_thread, ui);
193
194         return ret;
195 }
196
197 void cb_alarm_raised(struct psensor *sensor, void *data)
198 {
199 #ifdef HAVE_LIBNOTIFY
200         if (sensor->enabled)
201                 ui_notify(sensor, (struct ui_psensor *)data);
202 #endif
203 }
204
205 void associate_colors(struct psensor **sensors)
206 {
207         /* number of uniq colors */
208 #define COLORS_COUNT 8
209
210         unsigned int colors[COLORS_COUNT][3] = {
211                 {0x0000, 0x0000, 0x0000},       /* black */
212                 {0xffff, 0x0000, 0x0000},       /* red */
213                 {0x0000, 0.0000, 0xffff},       /* blue */
214                 {0x0000, 0xffff, 0x0000},       /* green */
215
216                 {0x7fff, 0x7fff, 0x7fff},       /* grey */
217                 {0x7fff, 0x0000, 0x0000},       /* dark red */
218                 {0x0000, 0x0000, 0x7fff},       /* dark blue */
219                 {0x0000, 0x7fff, 0x0000}        /* dark green */
220         };
221
222         struct psensor **sensor_cur = sensors;
223         int i = 0;
224         while (*sensor_cur) {
225                 struct color default_color;
226                 color_set(&default_color,
227                           colors[i % COLORS_COUNT][0],
228                           colors[i % COLORS_COUNT][1],
229                           colors[i % COLORS_COUNT][2]);
230
231                 (*sensor_cur)->color
232                     = config_get_sensor_color((*sensor_cur)->id,
233                                               &default_color);
234
235                 sensor_cur++;
236                 i++;
237         }
238 }
239
240 void
241 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
242 {
243         struct psensor **sensor_cur = sensors;
244         while (*sensor_cur) {
245                 struct psensor *s = *sensor_cur;
246
247                 s->cb_alarm_raised = cb_alarm_raised;
248                 s->cb_alarm_raised_data = ui;
249
250                 if (is_temp_type(s->type)) {
251                         s->alarm_limit
252                             = config_get_sensor_alarm_limit(s->id, 60);
253                         s->alarm_enabled
254                             = config_get_sensor_alarm_enabled(s->id);
255                 } else {
256                         s->alarm_limit = 0;
257                         s->alarm_enabled = 0;
258                 }
259
260                 sensor_cur++;
261         }
262 }
263
264 void associate_preferences(struct psensor **sensors)
265 {
266         struct psensor **sensor_cur = sensors;
267         while (*sensor_cur) {
268                 char *n;
269                 struct psensor *s = *sensor_cur;
270
271                 s->enabled = config_is_sensor_enabled(s->id);
272
273                 n = config_get_sensor_name(s->id);
274
275                 if (n)
276                         s->name = n;
277
278                 sensor_cur++;
279         }
280 }
281
282
283 static struct option long_options[] = {
284         {"version", no_argument, 0, 'v'},
285         {"help", no_argument, 0, 'h'},
286         {"url", required_argument, 0, 'u'},
287         {0, 0, 0, 0}
288 };
289
290 int main(int argc, char **argv)
291 {
292         struct ui_psensor ui;
293         GError *error;
294         GThread *thread;
295         int err, optc;
296         char *url = NULL;
297         int cmdok = 1;
298
299         program_name = argv[0];
300
301         setlocale(LC_ALL, "");
302
303 #if ENABLE_NLS
304         bindtextdomain(PACKAGE, LOCALEDIR);
305         textdomain(PACKAGE);
306 #endif
307
308         while ((optc = getopt_long(argc, argv, "vhu:", long_options,
309                                    NULL)) != -1) {
310                 switch (optc) {
311                 case 'u':
312                         if (optarg)
313                                 url = strdup(optarg);
314                         break;
315                 case 'h':
316                         print_help();
317                         exit(EXIT_SUCCESS);
318                 case 'v':
319                         print_version();
320                         exit(EXIT_SUCCESS);
321                 default:
322                         cmdok = 0;
323                         break;
324                 }
325         }
326
327         if (!cmdok || optind != argc) {
328                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
329                         program_name);
330                 exit(EXIT_FAILURE);
331         }
332
333         g_thread_init(NULL);
334         gdk_threads_init();
335         /* gdk_threads_enter(); */
336
337         gtk_init(&argc, &argv);
338
339 #ifdef HAVE_LIBNOTIFY
340         ui.notification_last_time = NULL;
341 #endif
342
343         ui.sensors_mutex = g_mutex_new();
344
345         config_init();
346
347         ui.config = config_load();
348
349         err = lmsensor_init();
350         if (!err) {
351                 fprintf(stderr, _("ERROR: lmsensor init failure: %s\n"),
352                         sensors_strerror(err));
353                 exit(EXIT_FAILURE);
354         }
355
356         if (url) {
357 #ifdef HAVE_REMOTE_SUPPORT
358                 rsensor_init();
359                 ui.sensors = get_remote_sensors(url, 600);
360 #else
361                 fprintf(stderr,
362                         _("ERROR: Not compiled with remote sensor support.\n"));
363                 exit(EXIT_FAILURE);
364 #endif
365         } else {
366 #ifdef HAVE_NVIDIA
367                 struct psensor **tmp;
368
369                 tmp = get_all_sensors(600);
370                 ui.sensors = nvidia_psensor_list_add(tmp, 600);
371
372                 if (tmp != ui.sensors)
373                         free(tmp);
374 #else
375                 ui.sensors = get_all_sensors(600);
376 #endif
377         }
378
379         associate_preferences(ui.sensors);
380         associate_colors(ui.sensors);
381         associate_cb_alarm_raised(ui.sensors, &ui);
382
383         /* main window */
384         ui_window_create(&ui);
385         ui.sensor_box = NULL;
386
387         /* drawing box */
388         ui.w_graph = ui_graph_create(&ui);
389
390         /* sensor list */
391         ui.ui_sensorlist = ui_sensorlist_create(ui.sensors);
392
393         ui_window_update(&ui);
394
395         thread = g_thread_create((GThreadFunc) update_psensor_measures,
396                                  &ui, TRUE, &error);
397
398         if (!thread)
399                 g_error_free(error);
400
401         ui.graph_update_interval = ui.config->graph_update_interval;
402
403         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
404
405 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
406         ui_appindicator_init(&ui);
407 #endif
408
409         /* main loop */
410         gtk_main();
411
412         sensors_cleanup();
413
414         psensor_list_free(ui.sensors);
415
416         return 0;
417 }