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