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