2 * Copyright (C) 2010-2014 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"
40 #include "notify_cmd.h"
46 #include "ui_status.h"
60 #ifdef HAVE_REMOTE_SUPPORT
64 #include "ui_appindicator.h"
67 #include "ui_notify.h"
74 static const char *program_name;
76 static void print_version()
78 printf("psensor %s\n", VERSION);
79 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
80 "License GPLv2: GNU GPL version 2 or later "
81 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
82 "This is free software: you are free to change and"
84 "There is NO WARRANTY, to the extent permitted by law.\n"),
88 static void print_help()
90 printf(_("Usage: %s [OPTION]...\n"), program_name);
92 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
93 "including temperatures and fan speeds."));
97 puts(_(" -h, --help display this help and exit\n"
98 " -v, --version display version information and exit"));
103 " -u, --url=URL the URL of the psensor-server,\n"
104 " example: http://hostname:3131"));
106 " --use-libatasmart use atasmart library for disk monitoring instead of\n"
109 " -n, --new-instance force the creation of a new Psensor application"));
112 puts(_(" -d, --debug=LEVEL "
113 "set the debug level, integer between 0 and 3"));
117 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
119 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
123 * Updates the size of the sensor values if different than the
127 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
129 struct psensor **cur, *s;
131 for (cur = sensors; *cur; cur++) {
134 if (s->values_max_length != cfg->sensor_values_max_length)
135 psensor_values_resize(s,
136 cfg->sensor_values_max_length);
140 static void *update_measures(void *data)
142 struct psensor **sensors;
145 struct ui_psensor *ui;
147 ui = (struct ui_psensor *)data;
151 pmutex_lock(&ui->sensors_mutex);
153 sensors = ui->sensors;
157 update_psensor_values_size(sensors, cfg);
159 psensor_list_update_measures(sensors);
160 #ifdef HAVE_REMOTE_SUPPORT
161 remote_psensor_list_update(sensors);
164 nvidia_psensor_list_update(sensors);
166 #ifdef HAVE_LIBATIADL
167 amd_psensor_list_update(sensors);
169 #ifdef HAVE_LIBUDISKS2
170 udisks2_psensor_list_update(sensors);
173 gtop2_psensor_list_update(sensors);
176 psensor_log_measures(sensors);
178 period = cfg->sensor_update_interval;
180 pmutex_unlock(&ui->sensors_mutex);
186 static void indicators_update(struct ui_psensor *ui)
188 struct psensor **sensor_cur = ui->sensors;
189 unsigned int attention = 0;
191 while (*sensor_cur) {
192 struct psensor *s = *sensor_cur;
194 if (s->alarm_enabled && s->alarm_raised) {
202 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
203 if (is_appindicator_supported())
204 ui_appindicator_update(ui, attention);
207 if (is_status_supported())
208 ui_status_update(ui, attention);
211 gboolean ui_refresh_thread(gpointer data)
215 struct ui_psensor *ui = (struct ui_psensor *)data;
220 pmutex_lock(&ui->sensors_mutex);
222 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
224 ui_sensorlist_update(ui, 0);
226 if (is_appindicator_supported() || is_status_supported())
227 indicators_update(ui);
230 ui_unity_launcher_entry_update(ui->sensors,
231 !cfg->unity_launcher_count_disabled,
232 cfg->temperature_unit == CELSIUS);
235 if (ui->graph_update_interval != cfg->graph_update_interval) {
236 ui->graph_update_interval = cfg->graph_update_interval;
240 pmutex_unlock(&ui->sensors_mutex);
243 g_timeout_add(1000 * ui->graph_update_interval,
244 ui_refresh_thread, ui);
249 static void cb_alarm_raised(struct psensor *sensor, void *data)
251 #ifdef HAVE_LIBNOTIFY
252 if (sensor->alarm_enabled)
253 ui_notify(sensor, (struct ui_psensor *)data);
259 static void associate_colors(struct psensor **sensors)
261 /* number of uniq colors */
262 #define COLORS_COUNT 8
264 double colors[COLORS_COUNT][3] = {
265 {0, 0, 0}, /* black */
267 {0, 0, 1}, /* blue */
268 {0, 1, 0}, /* green */
270 {0.5, 0.5, 0.5},/* grey */
271 {0.5, 0, 0}, /* dark red */
272 {0, 0, 0.5}, /* dark blue */
273 {0, 0.5, 0} /* dark green */
275 struct psensor **cur;
279 for (cur = sensors, i = 0; *cur; cur++, i++) {
281 colors[i % COLORS_COUNT][0],
282 colors[i % COLORS_COUNT][1],
283 colors[i % COLORS_COUNT][2]);
285 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
290 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
292 struct psensor **sensor_cur = sensors;
294 while (*sensor_cur) {
295 struct psensor *s = *sensor_cur;
297 s->cb_alarm_raised = cb_alarm_raised;
298 s->cb_alarm_raised_data = ui;
300 s->alarm_high_threshold
301 = config_get_sensor_alarm_high_threshold(s->id);
302 s->alarm_low_threshold
303 = config_get_sensor_alarm_low_threshold(s->id);
306 = config_get_sensor_alarm_enabled(s->id);
312 static void associate_preferences(struct psensor **sensors)
314 struct psensor **sensor_cur = sensors;
316 while (*sensor_cur) {
318 struct psensor *s = *sensor_cur;
320 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
322 n = config_get_sensor_name(s->id);
329 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
335 static void log_init()
340 dir = get_psensor_user_dir();
345 path = malloc(strlen(dir)+1+strlen("log")+1);
346 sprintf(path, "%s/%s", dir, "log");
353 static struct option long_options[] = {
354 {"use-libatasmart", no_argument, 0, 0},
355 {"version", no_argument, 0, 'v'},
356 {"help", no_argument, 0, 'h'},
357 {"url", required_argument, 0, 'u'},
358 {"debug", required_argument, 0, 'd'},
359 {"new-instance", no_argument, 0, 'n'},
363 static gboolean initial_window_show(gpointer data)
365 struct ui_psensor *ui;
367 log_debug("initial_window_show()");
369 ui = (struct ui_psensor *)data;
371 log_debug("is_status_supported: %d", is_status_supported());
372 log_debug("is_appindicator_supported: %d",
373 is_appindicator_supported());
374 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
376 if (!ui->config->hide_on_startup
377 || (!is_appindicator_supported() && !is_status_supported()))
380 ui_window_update(ui);
385 static void log_glib_info()
387 log_debug("Compiled with GLib %d.%d.%d",
392 log_debug("Running with GLib %d.%d.%d",
398 static void cb_activate(GApplication *application,
401 ui_window_show((struct ui_psensor *)data);
405 * Release memory for Valgrind.
407 static void cleanup(struct ui_psensor *ui)
409 pmutex_lock(&ui->sensors_mutex);
411 log_debug("Cleanup...");
418 #ifdef HAVE_LIBATIADL
421 #ifdef HAVE_REMOTE_SUPPORT
425 psensor_list_free(ui->sensors);
428 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
429 ui_appindicator_cleanup();
434 pmutex_unlock(&ui->sensors_mutex);
438 log_debug("Cleanup done, closing log");
442 * Creates the list of sensors.
444 * 'url': remote psensor server url, null for local monitoring.
445 * 'use_libatasmart': whether the libatasmart must be used.
447 static struct psensor **create_sensors_list(const char *url,
448 unsigned int use_libatasmart)
450 struct psensor **sensors;
453 #ifdef HAVE_REMOTE_SUPPORT
455 sensors = get_remote_sensors(url, 600);
457 log_err(_("Psensor has not been compiled with remote "
462 sensors = get_all_sensors(use_libatasmart, 600);
465 if (config_is_nvctrl_enabled())
466 sensors = nvidia_psensor_list_add(sensors, 600);
468 #ifdef HAVE_LIBATIADL
469 if (config_is_atiadlsdk_enabled())
470 sensors = amd_psensor_list_add(sensors, 600);
473 if (config_is_gtop2_enabled())
474 gtop2_psensor_list_append(&sensors, 600);
476 #ifdef HAVE_LIBUDISKS2
477 if (config_is_udisks2_enabled())
478 udisks2_psensor_list_append(&sensors, 600);
482 associate_preferences(sensors);
483 associate_colors(sensors);
488 int main(int argc, char **argv)
490 struct ui_psensor ui;
492 int optc, cmdok, opti, use_libatasmart, new_instance, ret;
496 program_name = argv[0];
498 setlocale(LC_ALL, "");
501 bindtextdomain(PACKAGE, LOCALEDIR);
505 use_libatasmart = new_instance = 0;
508 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
512 if (!strcmp(long_options[opti].name, "use-libatasmart"))
517 url = strdup(optarg);
526 log_level = atoi(optarg);
527 log_info(_("Enables debug mode."));
538 if (!cmdok || optind != argc) {
539 fprintf(stderr, _("Try `%s --help' for more information.\n"),
546 app = g_application_new("wpitchoune.psensor", 0);
548 g_application_register(app, NULL, NULL);
550 if (!new_instance && g_application_get_is_remote(app)) {
551 g_application_activate(app);
552 log_warn(_("A Psensor instance already exists."));
556 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
559 #if !(GLIB_CHECK_VERSION(2, 31, 0))
561 * Since GLib 2.31 g_thread_init call is deprecated and not
564 log_debug("Calling g_thread_init(NULL)");
568 #ifdef HAVE_APPINDICATOR_029
569 /* gdk_thread_enter/leave only used to workaround mutex bug
570 * of appindicator < 0.2.9, so do not call gdk_threads_init
571 * if useless. Calling this function leads to
572 * crash "Attempt to unlock mutex that was not locked" with
573 * GLib 2.41.2 (new checking) probably due to bugs in GTK
579 gtk_init(NULL, NULL);
581 pmutex_init(&ui.sensors_mutex);
583 ui.config = config_load();
587 ui.sensors = create_sensors_list(url, use_libatasmart);
588 associate_cb_alarm_raised(ui.sensors, &ui);
590 if (ui.config->slog_enabled)
594 config_get_slog_interval());
596 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
598 ui_status_set_visible(1);
602 ui_window_create(&ui);
604 ui_enable_alpha_channel(&ui);
606 ret = pthread_create(&thread, NULL, update_measures, &ui);
609 log_err(_("Failed to create thread for monitoring sensors"));
611 ui.graph_update_interval = ui.config->graph_update_interval;
613 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
615 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
616 ui_appindicator_init(&ui);
619 gdk_notify_startup_complete();
622 * hack, did not find a cleaner solution.
623 * wait 30s to ensure that the status icon is attempted to be
624 * drawn before determining whether the main window must be
627 if (ui.config->hide_on_startup)
628 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
630 initial_window_show(&ui);
632 log_debug("translators: %s\n", _("translator-credits"));
640 log_debug("Quitting...");