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