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);
161 lmsensor_psensor_list_update(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);
171 #ifdef HAVE_LIBUDISKS2
172 udisks2_psensor_list_update(sensors);
175 gtop2_psensor_list_update(sensors);
178 psensor_log_measures(sensors);
180 period = cfg->sensor_update_interval;
182 pmutex_unlock(&ui->sensors_mutex);
188 static void indicators_update(struct ui_psensor *ui)
190 struct psensor **sensor_cur = ui->sensors;
191 unsigned int attention = 0;
193 while (*sensor_cur) {
194 struct psensor *s = *sensor_cur;
196 if (s->alarm_enabled && s->alarm_raised) {
204 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
205 if (is_appindicator_supported())
206 ui_appindicator_update(ui, attention);
209 if (is_status_supported())
210 ui_status_update(ui, attention);
213 gboolean ui_refresh_thread(gpointer data)
217 struct ui_psensor *ui = (struct ui_psensor *)data;
222 pmutex_lock(&ui->sensors_mutex);
224 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
226 ui_sensorlist_update(ui, 0);
228 if (is_appindicator_supported() || is_status_supported())
229 indicators_update(ui);
232 ui_unity_launcher_entry_update(ui->sensors,
233 !cfg->unity_launcher_count_disabled,
234 cfg->temperature_unit == CELSIUS);
237 if (ui->graph_update_interval != cfg->graph_update_interval) {
238 ui->graph_update_interval = cfg->graph_update_interval;
242 pmutex_unlock(&ui->sensors_mutex);
245 g_timeout_add(1000 * ui->graph_update_interval,
246 ui_refresh_thread, ui);
251 static void cb_alarm_raised(struct psensor *sensor, void *data)
253 #ifdef HAVE_LIBNOTIFY
254 if (sensor->alarm_enabled)
255 ui_notify(sensor, (struct ui_psensor *)data);
261 static void associate_colors(struct psensor **sensors)
263 /* number of uniq colors */
264 #define COLORS_COUNT 8
266 double colors[COLORS_COUNT][3] = {
267 {0, 0, 0}, /* black */
269 {0, 0, 1}, /* blue */
270 {0, 1, 0}, /* green */
272 {0.5, 0.5, 0.5},/* grey */
273 {0.5, 0, 0}, /* dark red */
274 {0, 0, 0.5}, /* dark blue */
275 {0, 0.5, 0} /* dark green */
277 struct psensor **cur;
281 for (cur = sensors, i = 0; *cur; cur++, i++) {
283 colors[i % COLORS_COUNT][0],
284 colors[i % COLORS_COUNT][1],
285 colors[i % COLORS_COUNT][2]);
287 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
292 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
294 struct psensor **sensor_cur = sensors;
296 while (*sensor_cur) {
297 struct psensor *s = *sensor_cur;
299 s->cb_alarm_raised = cb_alarm_raised;
300 s->cb_alarm_raised_data = ui;
302 s->alarm_high_threshold
303 = config_get_sensor_alarm_high_threshold(s->id);
304 s->alarm_low_threshold
305 = config_get_sensor_alarm_low_threshold(s->id);
308 = config_get_sensor_alarm_enabled(s->id);
314 static void associate_preferences(struct psensor **sensors)
316 struct psensor **sensor_cur = sensors;
318 while (*sensor_cur) {
320 struct psensor *s = *sensor_cur;
322 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
324 n = config_get_sensor_name(s->id);
331 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
337 static void log_init()
342 dir = get_psensor_user_dir();
347 path = malloc(strlen(dir)+1+strlen("log")+1);
348 sprintf(path, "%s/%s", dir, "log");
355 static struct option long_options[] = {
356 {"use-libatasmart", no_argument, 0, 0},
357 {"version", no_argument, 0, 'v'},
358 {"help", no_argument, 0, 'h'},
359 {"url", required_argument, 0, 'u'},
360 {"debug", required_argument, 0, 'd'},
361 {"new-instance", no_argument, 0, 'n'},
365 static gboolean initial_window_show(gpointer data)
367 struct ui_psensor *ui;
369 log_debug("initial_window_show()");
371 ui = (struct ui_psensor *)data;
373 log_debug("is_status_supported: %d", is_status_supported());
374 log_debug("is_appindicator_supported: %d",
375 is_appindicator_supported());
376 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
378 if (!ui->config->hide_on_startup
379 || (!is_appindicator_supported() && !is_status_supported()))
382 ui_window_update(ui);
387 static void log_glib_info()
389 log_debug("Compiled with GLib %d.%d.%d",
394 log_debug("Running with GLib %d.%d.%d",
400 static void cb_activate(GApplication *application,
403 ui_window_show((struct ui_psensor *)data);
407 * Release memory for Valgrind.
409 static void cleanup(struct ui_psensor *ui)
411 pmutex_lock(&ui->sensors_mutex);
413 log_debug("Cleanup...");
420 #ifdef HAVE_LIBATIADL
423 #ifdef HAVE_REMOTE_SUPPORT
427 psensor_list_free(ui->sensors);
430 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
431 ui_appindicator_cleanup();
436 pmutex_unlock(&ui->sensors_mutex);
440 log_debug("Cleanup done, closing log");
444 * Creates the list of sensors.
446 * 'url': remote psensor server url, null for local monitoring.
447 * 'use_libatasmart': whether the libatasmart must be used.
449 static struct psensor **create_sensors_list(const char *url,
450 unsigned int use_libatasmart)
452 struct psensor **sensors;
455 #ifdef HAVE_REMOTE_SUPPORT
457 sensors = get_remote_sensors(url, 600);
459 log_err(_("Psensor has not been compiled with remote "
464 sensors = get_all_sensors(use_libatasmart, 600);
466 if (config_is_lmsensor_enabled())
467 lmsensor_psensor_list_append(&sensors, 600);
470 if (config_is_nvctrl_enabled())
471 nvidia_psensor_list_append(&sensors, 600);
473 #ifdef HAVE_LIBATIADL
474 if (config_is_atiadlsdk_enabled())
475 sensors = amd_psensor_list_add(sensors, 600);
478 if (config_is_gtop2_enabled())
479 gtop2_psensor_list_append(&sensors, 600);
481 #ifdef HAVE_LIBUDISKS2
482 if (config_is_udisks2_enabled())
483 udisks2_psensor_list_append(&sensors, 600);
487 associate_preferences(sensors);
488 associate_colors(sensors);
493 int main(int argc, char **argv)
495 struct ui_psensor ui;
497 int optc, cmdok, opti, use_libatasmart, new_instance, ret;
501 program_name = argv[0];
503 setlocale(LC_ALL, "");
506 bindtextdomain(PACKAGE, LOCALEDIR);
510 use_libatasmart = new_instance = 0;
513 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
517 if (!strcmp(long_options[opti].name, "use-libatasmart"))
522 url = strdup(optarg);
531 log_level = atoi(optarg);
532 log_info(_("Enables debug mode."));
543 if (!cmdok || optind != argc) {
544 fprintf(stderr, _("Try `%s --help' for more information.\n"),
551 app = g_application_new("wpitchoune.psensor", 0);
553 g_application_register(app, NULL, NULL);
555 if (!new_instance && g_application_get_is_remote(app)) {
556 g_application_activate(app);
557 log_warn(_("A Psensor instance already exists."));
561 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
564 #if !(GLIB_CHECK_VERSION(2, 31, 0))
566 * Since GLib 2.31 g_thread_init call is deprecated and not
569 log_debug("Calling g_thread_init(NULL)");
573 #ifdef HAVE_APPINDICATOR_029
574 /* gdk_thread_enter/leave only used to workaround mutex bug
575 * of appindicator < 0.2.9, so do not call gdk_threads_init
576 * if useless. Calling this function leads to
577 * crash "Attempt to unlock mutex that was not locked" with
578 * GLib 2.41.2 (new checking) probably due to bugs in GTK
584 gtk_init(NULL, NULL);
586 pmutex_init(&ui.sensors_mutex);
588 ui.config = config_load();
592 ui.sensors = create_sensors_list(url, use_libatasmart);
593 associate_cb_alarm_raised(ui.sensors, &ui);
595 if (ui.config->slog_enabled)
599 config_get_slog_interval());
601 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
603 ui_status_set_visible(1);
607 ui_window_create(&ui);
609 ui_enable_alpha_channel(&ui);
611 ret = pthread_create(&thread, NULL, update_measures, &ui);
614 log_err(_("Failed to create thread for monitoring sensors"));
616 ui.graph_update_interval = ui.config->graph_update_interval;
618 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
620 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
621 ui_appindicator_init(&ui);
624 gdk_notify_startup_complete();
627 * hack, did not find a cleaner solution.
628 * wait 30s to ensure that the status icon is attempted to be
629 * drawn before determining whether the main window must be
632 if (ui.config->hide_on_startup)
633 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
635 initial_window_show(&ui);
637 log_debug("translators: %s\n", _("translator-credits"));
645 log_debug("Quitting...");