2 * Copyright (C) 2010-2013 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"
43 #include "ui_status.h"
57 #ifdef HAVE_REMOTE_SUPPORT
61 #include "ui_appindicator.h"
64 #include "ui_notify.h"
73 static const char *program_name;
75 static void print_version()
77 printf("psensor %s\n", VERSION);
78 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
79 "License GPLv2: GNU GPL version 2 or later "
80 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
81 "This is free software: you are free to change and "
83 "There is NO WARRANTY, to the extent permitted by law.\n"),
87 static void print_help()
89 printf(_("Usage: %s [OPTION]...\n"), program_name);
91 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
92 "including temperatures and fan speeds."));
96 puts(_(" -h, --help display this help and exit\n"
97 " -v, --version display version information and exit"));
102 " -u, --url=URL the URL of the psensor-server,\n"
103 " example: http://hostname:3131"));
105 " --use-libatasmart use atasmart library for disk monitoring instead of\n"
108 " -n, --new-instance force the creation of a new Psensor application"));
111 puts(_(" -d, --debug=LEVEL "
112 "set the debug level, integer between 0 and 3"));
116 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
118 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
122 * Updates the size of the sensor values if different than the
126 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
128 struct psensor **cur, *s;
130 for (cur = sensors; *cur; cur++) {
133 if (s->values_max_length != cfg->sensor_values_max_length)
134 psensor_values_resize(s,
135 cfg->sensor_values_max_length);
139 static void update_measures(struct ui_psensor *ui)
141 struct psensor **sensors;
148 pthread_mutex_lock(&ui->sensors_mutex);
150 sensors = ui->sensors;
154 update_psensor_values_size(sensors, cfg);
156 psensor_list_update_measures(sensors);
157 #ifdef HAVE_REMOTE_SUPPORT
158 remote_psensor_list_update(sensors);
161 nvidia_psensor_list_update(sensors);
163 #ifdef HAVE_LIBATIADL
164 amd_psensor_list_update(sensors);
167 psensor_log_measures(sensors);
169 period = cfg->sensor_update_interval;
171 pthread_mutex_unlock(&ui->sensors_mutex);
177 static void indicators_update(struct ui_psensor *ui)
179 struct psensor **sensor_cur = ui->sensors;
180 unsigned int attention = 0;
182 while (*sensor_cur) {
183 struct psensor *s = *sensor_cur;
185 if (s->alarm_enabled && s->alarm_raised) {
193 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
194 if (is_appindicator_supported())
195 ui_appindicator_update(ui, attention);
198 if (is_status_supported())
199 ui_status_update(ui, attention);
202 gboolean ui_refresh_thread(gpointer data)
206 struct ui_psensor *ui = (struct ui_psensor *)data;
211 pthread_mutex_lock(&ui->sensors_mutex);
213 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
215 ui_sensorlist_update(ui);
217 if (is_appindicator_supported() || is_status_supported())
218 indicators_update(ui);
221 ui_unity_launcher_entry_update(ui->sensors,
222 !cfg->unity_launcher_count_disabled,
223 cfg->temperature_unit == CELCIUS);
226 if (ui->graph_update_interval != cfg->graph_update_interval) {
227 ui->graph_update_interval = cfg->graph_update_interval;
231 pthread_mutex_unlock(&ui->sensors_mutex);
234 g_timeout_add(1000 * ui->graph_update_interval,
235 ui_refresh_thread, ui);
240 static void cb_alarm_raised(struct psensor *sensor, void *data)
242 #ifdef HAVE_LIBNOTIFY
243 if (sensor->alarm_enabled)
244 ui_notify(sensor, (struct ui_psensor *)data);
248 static void associate_colors(struct psensor **sensors)
250 /* number of uniq colors */
251 #define COLORS_COUNT 8
253 unsigned int colors[COLORS_COUNT][3] = {
254 {0x0000, 0x0000, 0x0000}, /* black */
255 {0xffff, 0x0000, 0x0000}, /* red */
256 {0x0000, 0x0000, 0xffff}, /* blue */
257 {0x0000, 0xffff, 0x0000}, /* green */
259 {0x7fff, 0x7fff, 0x7fff}, /* grey */
260 {0x7fff, 0x0000, 0x0000}, /* dark red */
261 {0x0000, 0x0000, 0x7fff}, /* dark blue */
262 {0x0000, 0x7fff, 0x0000} /* dark green */
265 struct psensor **sensor_cur = sensors;
267 while (*sensor_cur) {
268 struct color default_color;
269 color_set(&default_color,
270 colors[i % COLORS_COUNT][0],
271 colors[i % COLORS_COUNT][1],
272 colors[i % COLORS_COUNT][2]);
275 = config_get_sensor_color((*sensor_cur)->id,
284 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
286 struct psensor **sensor_cur = sensors;
287 while (*sensor_cur) {
288 struct psensor *s = *sensor_cur;
290 s->cb_alarm_raised = cb_alarm_raised;
291 s->cb_alarm_raised_data = ui;
293 s->alarm_high_threshold
294 = config_get_sensor_alarm_high_threshold(s->id);
295 s->alarm_low_threshold
296 = config_get_sensor_alarm_low_threshold(s->id);
298 if (is_temp_type(s->type) || is_fan_type(s->type)) {
300 = config_get_sensor_alarm_enabled(s->id);
302 s->alarm_high_threshold = 0;
303 s->alarm_enabled = 0;
310 static void associate_preferences(struct psensor **sensors)
312 struct psensor **sensor_cur = sensors;
313 while (*sensor_cur) {
315 struct psensor *s = *sensor_cur;
317 s->enabled = config_is_sensor_enabled(s->id);
319 n = config_get_sensor_name(s->id);
326 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
332 static void log_init()
334 char *home, *path, *dir;
336 home = getenv("HOME");
341 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
342 sprintf(dir, "%s/%s", home, ".psensor");
345 path = malloc(strlen(dir)+1+strlen("log")+1);
346 sprintf(path, "%s/%s", dir, "log");
354 static struct option long_options[] = {
355 {"use-libatasmart", no_argument, 0, 0},
356 {"version", no_argument, 0, 'v'},
357 {"help", no_argument, 0, 'h'},
358 {"url", required_argument, 0, 'u'},
359 {"debug", required_argument, 0, 'd'},
360 {"new-instance", no_argument, 0, 'n'},
364 static gboolean initial_window_show(gpointer data)
366 struct ui_psensor *ui;
368 log_debug("initial_window_show()");
370 ui = (struct ui_psensor *)data;
372 log_debug("is_status_supported: %d", is_status_supported());
373 log_debug("is_appindicator_supported: %d",
374 is_appindicator_supported());
375 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
377 if (!ui->config->hide_on_startup
378 || (!is_appindicator_supported() && !is_status_supported()))
381 ui_window_update(ui);
386 static void log_glib_info()
388 log_debug("Compiled with GLib %d.%d.%d",
393 log_debug("Running with GLib %d.%d.%d",
399 static void cb_activate(GApplication *application,
402 ui_window_show((struct ui_psensor *)data);
406 * Release memory for Valgrind.
408 static void cleanup(struct ui_psensor *ui)
410 pthread_mutex_lock(&ui->sensors_mutex);
412 log_debug("Cleanup...");
419 #ifdef HAVE_LIBATIADL
422 #ifdef HAVE_REMOTE_SUPPORT
426 psensor_list_free(ui->sensors);
429 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
430 ui_appindicator_cleanup();
435 pthread_mutex_unlock(&ui->sensors_mutex);
439 log_debug("Cleanup done, closing log");
443 * Creates the list of sensors.
445 * 'url': remote psensor server url, null for local monitoring.
446 * 'use_libatasmart': whether the libatasmart must be used.
448 static struct psensor **create_sensors_list(const char *url,
449 unsigned int use_libatasmart)
451 struct psensor **sensors;
454 #ifdef HAVE_REMOTE_SUPPORT
456 sensors = get_remote_sensors(url, 600);
458 log_err(_("Psensor has not been compiled with remote "
463 sensors = get_all_sensors(use_libatasmart, 600);
465 sensors = nvidia_psensor_list_add(sensors, 600);
467 #ifdef HAVE_LIBATIADL
468 sensors = amd_psensor_list_add(sensors, 600);
471 sensors = cpu_psensor_list_add(sensors, 600);
475 associate_preferences(sensors);
476 associate_colors(sensors);
481 int main(int argc, char **argv)
483 struct ui_psensor ui;
486 int optc, cmdok, opti, use_libatasmart, new_instance;
490 program_name = argv[0];
492 setlocale(LC_ALL, "");
495 bindtextdomain(PACKAGE, LOCALEDIR);
499 use_libatasmart = new_instance = 0;
502 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
506 if (!strcmp(long_options[opti].name, "use-libatasmart"))
511 url = strdup(optarg);
520 log_level = atoi(optarg);
521 log_info(_("Enables debug mode."));
532 if (!cmdok || optind != argc) {
533 fprintf(stderr, _("Try `%s --help' for more information.\n"),
540 app = g_application_new("wpitchoune.psensor", 0);
542 g_application_register(app, NULL, NULL);
544 if (!new_instance && g_application_get_is_remote(app)) {
545 g_application_activate(app);
546 log_warn(_("A Psensor instance already exists."));
550 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
553 #if !(GLIB_CHECK_VERSION(2, 31, 0))
555 * Since GLib 2.31 g_thread_init call is deprecated and not
558 log_debug("Calling g_thread_init(NULL)");
564 gtk_init(NULL, NULL);
566 pthread_mutex_init(&ui.sensors_mutex, NULL);
568 ui.config = config_load();
572 ui.sensors = create_sensors_list(url, use_libatasmart);
573 associate_cb_alarm_raised(ui.sensors, &ui);
575 if (ui.config->slog_enabled)
579 config_get_slog_interval());
581 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
583 ui_status_set_visible(1);
587 ui_window_create(&ui);
589 ui_enable_alpha_channel(&ui);
591 thread = g_thread_create((GThreadFunc) update_measures,
597 ui.graph_update_interval = ui.config->graph_update_interval;
599 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
601 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
602 ui_appindicator_init(&ui);
605 gdk_notify_startup_complete();
608 * hack, did not find a cleaner solution.
609 * wait 30s to ensure that the status icon is attempted to be
610 * drawn before determining whether the main window must be
613 if (ui.config->hide_on_startup)
614 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
616 initial_window_show(&ui);
624 log_debug("Quitting...");