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"));
105 puts(_(" -d, --debug=LEVEL "
106 "set the debug level, integer between 0 and 3"));
110 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
112 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
116 Updates the size of the sensor values if different than the
120 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
122 struct psensor **cur;
126 struct psensor *s = *cur;
128 if (s->values_max_length != cfg->sensor_values_max_length)
129 psensor_values_resize(s,
130 cfg->sensor_values_max_length);
136 static void log_measures(struct psensor **sensors)
138 if (log_level == LOG_DEBUG)
140 log_debug("Measure: %s %.2f",
142 psensor_get_current_value(*sensors));
148 void update_psensor_measures(struct ui_psensor *ui)
150 struct psensor **sensors = ui->sensors;
151 struct config *cfg = ui->config;
154 g_mutex_lock(ui->sensors_mutex);
159 update_psensor_values_size(sensors, ui->config);
161 psensor_list_update_measures(sensors);
162 #ifdef HAVE_REMOTE_SUPPORT
163 remote_psensor_list_update(sensors);
166 nvidia_psensor_list_update(sensors);
168 #ifdef HAVE_LIBATIADL
169 amd_psensor_list_update(sensors);
172 log_measures(sensors);
174 g_mutex_unlock(ui->sensors_mutex);
176 sleep(cfg->sensor_update_interval);
180 static void indicators_update(struct ui_psensor *ui)
182 struct psensor **sensor_cur = ui->sensors;
183 unsigned int attention = 0;
185 while (*sensor_cur) {
186 struct psensor *s = *sensor_cur;
188 if (s->alarm_enabled && s->alarm_raised) {
196 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
197 if (is_appindicator_supported())
198 ui_appindicator_update(ui, attention);
201 if (is_status_supported())
202 ui_status_update(ui, attention);
205 gboolean ui_refresh_thread(gpointer data)
209 struct ui_psensor *ui = (struct ui_psensor *)data;
214 g_mutex_lock(ui->sensors_mutex);
216 graph_update(ui->sensors, ui->w_graph, ui->config);
218 ui_sensorlist_update(ui);
220 if (is_appindicator_supported() || is_status_supported())
221 indicators_update(ui);
224 ui_unity_launcher_entry_update(ui->sensors,
225 !cfg->unity_launcher_count_disabled);
228 if (ui->graph_update_interval != cfg->graph_update_interval) {
229 ui->graph_update_interval = cfg->graph_update_interval;
233 g_mutex_unlock(ui->sensors_mutex);
236 g_timeout_add(1000 * ui->graph_update_interval,
237 ui_refresh_thread, ui);
242 void cb_alarm_raised(struct psensor *sensor, void *data)
244 #ifdef HAVE_LIBNOTIFY
246 ui_notify(sensor, (struct ui_psensor *)data);
250 static void associate_colors(struct psensor **sensors)
252 /* number of uniq colors */
253 #define COLORS_COUNT 8
255 unsigned int colors[COLORS_COUNT][3] = {
256 {0x0000, 0x0000, 0x0000}, /* black */
257 {0xffff, 0x0000, 0x0000}, /* red */
258 {0x0000, 0.0000, 0xffff}, /* blue */
259 {0x0000, 0xffff, 0x0000}, /* green */
261 {0x7fff, 0x7fff, 0x7fff}, /* grey */
262 {0x7fff, 0x0000, 0x0000}, /* dark red */
263 {0x0000, 0x0000, 0x7fff}, /* dark blue */
264 {0x0000, 0x7fff, 0x0000} /* dark green */
267 struct psensor **sensor_cur = sensors;
269 while (*sensor_cur) {
270 struct color default_color;
271 color_set(&default_color,
272 colors[i % COLORS_COUNT][0],
273 colors[i % COLORS_COUNT][1],
274 colors[i % COLORS_COUNT][2]);
277 = config_get_sensor_color((*sensor_cur)->id,
286 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
288 struct psensor **sensor_cur = sensors;
289 while (*sensor_cur) {
290 struct psensor *s = *sensor_cur;
292 s->cb_alarm_raised = cb_alarm_raised;
293 s->cb_alarm_raised_data = ui;
295 if (is_temp_type(s->type)) {
297 = config_get_sensor_alarm_limit(s->id, 60);
299 = config_get_sensor_alarm_enabled(s->id);
302 s->alarm_enabled = 0;
309 static void associate_preferences(struct psensor **sensors)
311 struct psensor **sensor_cur = sensors;
312 while (*sensor_cur) {
314 struct psensor *s = *sensor_cur;
316 s->enabled = config_is_sensor_enabled(s->id);
318 n = config_get_sensor_name(s->id);
327 static void log_init()
329 char *home, *path, *dir;
331 home = getenv("HOME");
336 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
337 sprintf(dir, "%s/%s", home, ".psensor");
340 path = malloc(strlen(dir)+1+strlen("log")+1);
341 sprintf(path, "%s/%s", dir, "log");
349 static struct option long_options[] = {
350 {"version", no_argument, 0, 'v'},
351 {"help", no_argument, 0, 'h'},
352 {"url", required_argument, 0, 'u'},
353 {"debug", required_argument, 0, 'd'},
357 static gboolean initial_window_show(gpointer data)
359 struct ui_psensor *ui;
361 log_debug("initial_window_show()");
363 ui = (struct ui_psensor *)data;
365 log_debug("is_status_supported: %d", is_status_supported());
366 log_debug("is_appindicator_supported: %d",
367 is_appindicator_supported());
368 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
370 if (!ui->config->hide_on_startup
371 || (!is_appindicator_supported() && !is_status_supported()))
374 ui_window_update(ui);
379 static void log_glib_info()
381 log_debug("Compiled with GLib %d.%d.%d",
386 log_debug("Running with GLib %d.%d.%d",
392 int main(int argc, char **argv)
394 struct ui_psensor ui;
400 program_name = argv[0];
402 setlocale(LC_ALL, "");
405 bindtextdomain(PACKAGE, LOCALEDIR);
410 while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
415 url = strdup(optarg);
424 log_level = atoi(optarg);
425 log_printf(LOG_INFO, _("Enables debug mode."));
433 if (!cmdok || optind != argc) {
434 fprintf(stderr, _("Try `%s --help' for more information.\n"),
442 #if !(GLIB_CHECK_VERSION(2, 31, 0))
444 * Since GLib 2.31 g_thread_init call is deprecated and not
447 log_debug("Calling g_thread_init(NULL)");
453 gtk_init(NULL, NULL);
455 ui.sensors_mutex = g_mutex_new();
459 ui.config = config_load();
464 #ifdef HAVE_REMOTE_SUPPORT
466 ui.sensors = get_remote_sensors(url, 600);
469 _("ERROR: Not compiled with remote sensor support.\n"));
473 ui.sensors = get_all_sensors(600);
475 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
477 #ifdef HAVE_LIBATIADL
478 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
481 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
485 associate_preferences(ui.sensors);
486 associate_colors(ui.sensors);
487 associate_cb_alarm_raised(ui.sensors, &ui);
489 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
494 ui_window_create(&ui);
495 ui.sensor_box = NULL;
498 ui.w_graph = ui_graph_create(&ui);
501 ui_sensorlist_create(&ui);
503 thread = g_thread_create((GThreadFunc) update_psensor_measures,
509 ui.graph_update_interval = ui.config->graph_update_interval;
511 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
513 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
514 ui_appindicator_init(&ui);
517 gdk_notify_startup_complete();
520 * hack, did not find a cleaner solution.
521 * wait 2s to ensure that the status icon is attempted to be
522 * drawn before determining whether the main window must be
525 g_timeout_add(2000,(GSourceFunc)initial_window_show, &ui);
530 g_mutex_lock(ui.sensors_mutex);
537 #ifdef HAVE_LIBATIADL
540 #ifdef HAVE_REMOTE_SUPPORT
544 psensor_list_free(ui.sensors);
547 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
548 ui_appindicator_cleanup();
553 g_mutex_unlock(ui.sensors_mutex);