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