2 * Copyright (C) 2010-2012 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
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
27 #include <sys/types.h>
37 #include "ui_sensorlist.h"
42 #include "ui_status.h"
56 #ifdef HAVE_REMOTE_SUPPORT
60 #include "ui_appindicator.h"
63 #include "ui_notify.h"
72 static const char *program_name;
74 static void print_version()
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 "
82 "There is NO WARRANTY, to the extent permitted by law.\n"),
86 static void print_help()
88 printf(_("Usage: %s [OPTION]...\n"), program_name);
90 puts(_("psensor is a GTK application for monitoring hardware sensors, "
91 "including temperatures and fan speeds."));
95 puts(_(" -h, --help display this help and exit\n"
96 " -v, --version display version information and exit"));
100 puts(_(" -u, --url=URL "
101 "the URL of the psensor-server, example: http://hostname:3131"));
102 puts(_(" --use-libatasmart "
103 "use atasmart library for disk monitoring "
104 "instead of hddtemp daemon"));
107 puts(_(" -d, --debug=LEVEL "
108 "set the debug level, integer between 0 and 3"));
112 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
114 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
118 Updates the size of the sensor values if different than the
122 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
124 struct psensor **cur;
128 struct psensor *s = *cur;
130 if (s->values_max_length != cfg->sensor_values_max_length)
131 psensor_values_resize(s,
132 cfg->sensor_values_max_length);
138 static void log_measures(struct psensor **sensors)
140 if (log_level == LOG_DEBUG)
142 log_debug("Measure: %s %.2f",
144 psensor_get_current_value(*sensors));
150 void update_psensor_measures(struct ui_psensor *ui)
152 struct psensor **sensors = ui->sensors;
153 struct config *cfg = ui->config;
156 g_mutex_lock(ui->sensors_mutex);
161 update_psensor_values_size(sensors, ui->config);
163 psensor_list_update_measures(sensors);
164 #ifdef HAVE_REMOTE_SUPPORT
165 remote_psensor_list_update(sensors);
168 nvidia_psensor_list_update(sensors);
170 #ifdef HAVE_LIBATIADL
171 amd_psensor_list_update(sensors);
174 log_measures(sensors);
176 g_mutex_unlock(ui->sensors_mutex);
178 sleep(cfg->sensor_update_interval);
182 static void indicators_update(struct ui_psensor *ui)
184 struct psensor **sensor_cur = ui->sensors;
185 unsigned int attention = 0;
187 while (*sensor_cur) {
188 struct psensor *s = *sensor_cur;
190 if (s->alarm_enabled && s->alarm_raised) {
198 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
199 if (is_appindicator_supported())
200 ui_appindicator_update(ui, attention);
203 if (is_status_supported())
204 ui_status_update(ui, attention);
207 gboolean ui_refresh_thread(gpointer data)
211 struct ui_psensor *ui = (struct ui_psensor *)data;
216 g_mutex_lock(ui->sensors_mutex);
218 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
220 ui_sensorlist_update(ui);
222 if (is_appindicator_supported() || is_status_supported())
223 indicators_update(ui);
226 ui_unity_launcher_entry_update(ui->sensors,
227 !cfg->unity_launcher_count_disabled);
230 if (ui->graph_update_interval != cfg->graph_update_interval) {
231 ui->graph_update_interval = cfg->graph_update_interval;
235 g_mutex_unlock(ui->sensors_mutex);
238 g_timeout_add(1000 * ui->graph_update_interval,
239 ui_refresh_thread, ui);
244 void cb_alarm_raised(struct psensor *sensor, void *data)
246 #ifdef HAVE_LIBNOTIFY
248 ui_notify(sensor, (struct ui_psensor *)data);
252 static void associate_colors(struct psensor **sensors)
254 /* number of uniq colors */
255 #define COLORS_COUNT 8
257 unsigned int colors[COLORS_COUNT][3] = {
258 {0x0000, 0x0000, 0x0000}, /* black */
259 {0xffff, 0x0000, 0x0000}, /* red */
260 {0x0000, 0.0000, 0xffff}, /* blue */
261 {0x0000, 0xffff, 0x0000}, /* green */
263 {0x7fff, 0x7fff, 0x7fff}, /* grey */
264 {0x7fff, 0x0000, 0x0000}, /* dark red */
265 {0x0000, 0x0000, 0x7fff}, /* dark blue */
266 {0x0000, 0x7fff, 0x0000} /* dark green */
269 struct psensor **sensor_cur = sensors;
271 while (*sensor_cur) {
272 struct color default_color;
273 color_set(&default_color,
274 colors[i % COLORS_COUNT][0],
275 colors[i % COLORS_COUNT][1],
276 colors[i % COLORS_COUNT][2]);
279 = config_get_sensor_color((*sensor_cur)->id,
288 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
290 struct psensor **sensor_cur = sensors;
291 while (*sensor_cur) {
292 struct psensor *s = *sensor_cur;
294 s->cb_alarm_raised = cb_alarm_raised;
295 s->cb_alarm_raised_data = ui;
297 if (is_temp_type(s->type)) {
299 = config_get_sensor_alarm_limit(s->id, 60);
301 = config_get_sensor_alarm_enabled(s->id);
304 s->alarm_enabled = 0;
311 static void associate_preferences(struct psensor **sensors)
313 struct psensor **sensor_cur = sensors;
314 while (*sensor_cur) {
316 struct psensor *s = *sensor_cur;
318 s->enabled = config_is_sensor_enabled(s->id);
320 n = config_get_sensor_name(s->id);
329 static void log_init()
331 char *home, *path, *dir;
333 home = getenv("HOME");
338 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
339 sprintf(dir, "%s/%s", home, ".psensor");
342 path = malloc(strlen(dir)+1+strlen("log")+1);
343 sprintf(path, "%s/%s", dir, "log");
351 static struct option long_options[] = {
352 {"use-libatasmart", no_argument, 0, 0},
353 {"version", no_argument, 0, 'v'},
354 {"help", no_argument, 0, 'h'},
355 {"url", required_argument, 0, 'u'},
356 {"debug", required_argument, 0, 'd'},
360 static gboolean initial_window_show(gpointer data)
362 struct ui_psensor *ui;
364 log_debug("initial_window_show()");
366 ui = (struct ui_psensor *)data;
368 log_debug("is_status_supported: %d", is_status_supported());
369 log_debug("is_appindicator_supported: %d",
370 is_appindicator_supported());
371 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
373 if (!ui->config->hide_on_startup
374 || (!is_appindicator_supported() && !is_status_supported()))
377 ui_window_update(ui);
382 static void log_glib_info()
384 log_debug("Compiled with GLib %d.%d.%d",
389 log_debug("Running with GLib %d.%d.%d",
395 int main(int argc, char **argv)
397 struct ui_psensor ui;
400 int optc, cmdok, opti, use_libatasmart;
403 program_name = argv[0];
405 setlocale(LC_ALL, "");
408 bindtextdomain(PACKAGE, LOCALEDIR);
415 while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
419 if (!strcmp(long_options[opti].name, "use-libatasmart"))
424 url = strdup(optarg);
433 log_level = atoi(optarg);
434 log_printf(LOG_INFO, _("Enables debug mode."));
442 if (!cmdok || optind != argc) {
443 fprintf(stderr, _("Try `%s --help' for more information.\n"),
451 #if !(GLIB_CHECK_VERSION(2, 31, 0))
453 * Since GLib 2.31 g_thread_init call is deprecated and not
456 log_debug("Calling g_thread_init(NULL)");
462 gtk_init(NULL, NULL);
464 ui.sensors_mutex = g_mutex_new();
468 ui.config = config_load();
473 #ifdef HAVE_REMOTE_SUPPORT
475 ui.sensors = get_remote_sensors(url, 600);
478 _("ERROR: Not compiled with remote sensor support.\n"));
482 ui.sensors = get_all_sensors(use_libatasmart, 600);
484 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
486 #ifdef HAVE_LIBATIADL
487 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
490 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
494 associate_preferences(ui.sensors);
495 associate_colors(ui.sensors);
496 associate_cb_alarm_raised(ui.sensors, &ui);
498 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
500 ui_status_set_visible(1);
504 ui_window_create(&ui);
505 ui.sensor_box = NULL;
508 ui.w_graph = ui_graph_create(&ui);
511 ui_sensorlist_create(&ui);
513 thread = g_thread_create((GThreadFunc) update_psensor_measures,
519 ui.graph_update_interval = ui.config->graph_update_interval;
521 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
523 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
524 ui_appindicator_init(&ui);
528 * show the window as soon as all gtk events have been processed
529 * in order to ensure that the status icon is attempted to be
530 * drawn before. If not, there is no way to detect that it is
533 g_idle_add((GSourceFunc)initial_window_show, &ui);
535 gdk_notify_startup_complete();
540 g_mutex_lock(ui.sensors_mutex);
547 #ifdef HAVE_LIBATIADL
550 #ifdef HAVE_REMOTE_SUPPORT
554 psensor_list_free(ui.sensors);
557 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
558 ui_appindicator_cleanup();
563 g_mutex_unlock(ui.sensors_mutex);