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 void update_psensor_measures(struct ui_psensor *ui)
140 struct psensor **sensors = ui->sensors;
141 struct config *cfg = ui->config;
144 g_mutex_lock(ui->sensors_mutex);
149 update_psensor_values_size(sensors, ui->config);
151 psensor_list_update_measures(sensors);
152 #ifdef HAVE_REMOTE_SUPPORT
153 remote_psensor_list_update(sensors);
156 nvidia_psensor_list_update(sensors);
158 #ifdef HAVE_LIBATIADL
159 amd_psensor_list_update(sensors);
162 psensor_log_measures(sensors);
164 g_mutex_unlock(ui->sensors_mutex);
166 sleep(cfg->sensor_update_interval);
170 static void indicators_update(struct ui_psensor *ui)
172 struct psensor **sensor_cur = ui->sensors;
173 unsigned int attention = 0;
175 while (*sensor_cur) {
176 struct psensor *s = *sensor_cur;
178 if (s->alarm_enabled && s->alarm_raised) {
186 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
187 if (is_appindicator_supported())
188 ui_appindicator_update(ui, attention);
191 if (is_status_supported())
192 ui_status_update(ui, attention);
195 gboolean ui_refresh_thread(gpointer data)
199 struct ui_psensor *ui = (struct ui_psensor *)data;
204 g_mutex_lock(ui->sensors_mutex);
206 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
208 ui_sensorlist_update(ui);
210 if (is_appindicator_supported() || is_status_supported())
211 indicators_update(ui);
214 ui_unity_launcher_entry_update(ui->sensors,
215 !cfg->unity_launcher_count_disabled,
216 cfg->temperature_unit == CELCIUS);
219 if (ui->graph_update_interval != cfg->graph_update_interval) {
220 ui->graph_update_interval = cfg->graph_update_interval;
224 g_mutex_unlock(ui->sensors_mutex);
227 g_timeout_add(1000 * ui->graph_update_interval,
228 ui_refresh_thread, ui);
233 void cb_alarm_raised(struct psensor *sensor, void *data)
235 #ifdef HAVE_LIBNOTIFY
237 ui_notify(sensor, (struct ui_psensor *)data);
241 static void associate_colors(struct psensor **sensors)
243 /* number of uniq colors */
244 #define COLORS_COUNT 8
246 unsigned int colors[COLORS_COUNT][3] = {
247 {0x0000, 0x0000, 0x0000}, /* black */
248 {0xffff, 0x0000, 0x0000}, /* red */
249 {0x0000, 0.0000, 0xffff}, /* blue */
250 {0x0000, 0xffff, 0x0000}, /* green */
252 {0x7fff, 0x7fff, 0x7fff}, /* grey */
253 {0x7fff, 0x0000, 0x0000}, /* dark red */
254 {0x0000, 0x0000, 0x7fff}, /* dark blue */
255 {0x0000, 0x7fff, 0x0000} /* dark green */
258 struct psensor **sensor_cur = sensors;
260 while (*sensor_cur) {
261 struct color default_color;
262 color_set(&default_color,
263 colors[i % COLORS_COUNT][0],
264 colors[i % COLORS_COUNT][1],
265 colors[i % COLORS_COUNT][2]);
268 = config_get_sensor_color((*sensor_cur)->id,
277 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
279 struct psensor **sensor_cur = sensors;
280 while (*sensor_cur) {
281 struct psensor *s = *sensor_cur;
283 s->cb_alarm_raised = cb_alarm_raised;
284 s->cb_alarm_raised_data = ui;
286 if (is_temp_type(s->type)) {
288 = config_get_sensor_alarm_limit(s->id, 60);
290 = config_get_sensor_alarm_enabled(s->id);
293 s->alarm_enabled = 0;
300 static void associate_preferences(struct psensor **sensors)
302 struct psensor **sensor_cur = sensors;
303 while (*sensor_cur) {
305 struct psensor *s = *sensor_cur;
307 s->enabled = config_is_sensor_enabled(s->id);
309 n = config_get_sensor_name(s->id);
318 static void log_init()
320 char *home, *path, *dir;
322 home = getenv("HOME");
327 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
328 sprintf(dir, "%s/%s", home, ".psensor");
331 path = malloc(strlen(dir)+1+strlen("log")+1);
332 sprintf(path, "%s/%s", dir, "log");
340 static struct option long_options[] = {
341 {"use-libatasmart", no_argument, 0, 0},
342 {"version", no_argument, 0, 'v'},
343 {"help", no_argument, 0, 'h'},
344 {"url", required_argument, 0, 'u'},
345 {"debug", required_argument, 0, 'd'},
349 static gboolean initial_window_show(gpointer data)
351 struct ui_psensor *ui;
353 log_debug("initial_window_show()");
355 ui = (struct ui_psensor *)data;
357 log_debug("is_status_supported: %d", is_status_supported());
358 log_debug("is_appindicator_supported: %d",
359 is_appindicator_supported());
360 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
362 if (!ui->config->hide_on_startup
363 || (!is_appindicator_supported() && !is_status_supported()))
366 ui_window_update(ui);
371 static void log_glib_info()
373 log_debug("Compiled with GLib %d.%d.%d",
378 log_debug("Running with GLib %d.%d.%d",
384 int main(int argc, char **argv)
386 struct ui_psensor ui;
389 int optc, cmdok, opti, use_libatasmart;
392 program_name = argv[0];
394 setlocale(LC_ALL, "");
397 bindtextdomain(PACKAGE, LOCALEDIR);
404 while ((optc = getopt_long(argc, argv, "vhd:u:", long_options,
408 if (!strcmp(long_options[opti].name, "use-libatasmart"))
413 url = strdup(optarg);
422 log_level = atoi(optarg);
423 log_printf(LOG_INFO, _("Enables debug mode."));
431 if (!cmdok || optind != argc) {
432 fprintf(stderr, _("Try `%s --help' for more information.\n"),
440 #if !(GLIB_CHECK_VERSION(2, 31, 0))
442 * Since GLib 2.31 g_thread_init call is deprecated and not
445 log_debug("Calling g_thread_init(NULL)");
451 gtk_init(NULL, NULL);
453 ui.sensors_mutex = g_mutex_new();
457 ui.config = config_load();
462 #ifdef HAVE_REMOTE_SUPPORT
464 ui.sensors = get_remote_sensors(url, 600);
467 _("ERROR: Not compiled with remote sensor support.\n"));
471 ui.sensors = get_all_sensors(use_libatasmart, 600);
473 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
475 #ifdef HAVE_LIBATIADL
476 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
479 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
483 associate_preferences(ui.sensors);
484 associate_colors(ui.sensors);
485 associate_cb_alarm_raised(ui.sensors, &ui);
487 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
489 ui_status_set_visible(1);
493 ui_window_create(&ui);
494 ui.sensor_box = NULL;
497 ui.w_graph = ui_graph_create(&ui);
499 ui_enable_alpha_channel(&ui);
502 ui_sensorlist_create(&ui);
504 thread = g_thread_create((GThreadFunc) update_psensor_measures,
510 ui.graph_update_interval = ui.config->graph_update_interval;
512 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
514 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
515 ui_appindicator_init(&ui);
519 * show the window as soon as all gtk events have been processed
520 * in order to ensure that the status icon is attempted to be
521 * drawn before. If not, there is no way to detect that it is
524 g_idle_add((GSourceFunc)initial_window_show, &ui);
526 gdk_notify_startup_complete();
531 g_mutex_lock(ui.sensors_mutex);
538 #ifdef HAVE_LIBATIADL
541 #ifdef HAVE_REMOTE_SUPPORT
545 psensor_list_free(ui.sensors);
548 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
549 ui_appindicator_cleanup();
554 g_mutex_unlock(ui.sensors_mutex);