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>
38 #include "ui_sensorlist.h"
41 #include "notify_cmd.h"
47 #include "ui_status.h"
61 #ifdef HAVE_REMOTE_SUPPORT
65 #include "ui_appindicator.h"
68 #include "ui_notify.h"
75 static const char *program_name;
77 static void print_version()
79 printf("psensor %s\n", VERSION);
80 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
81 "License GPLv2: GNU GPL version 2 or later "
82 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
83 "This is free software: you are free to change and"
85 "There is NO WARRANTY, to the extent permitted by law.\n"),
89 static void print_help()
91 printf(_("Usage: %s [OPTION]...\n"), program_name);
93 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
94 "including temperatures and fan speeds."));
98 puts(_(" -h, --help display this help and exit\n"
99 " -v, --version display version information and exit"));
104 " -u, --url=URL the URL of the psensor-server,\n"
105 " example: http://hostname:3131"));
107 " -n, --new-instance force the creation of a new Psensor application"));
110 puts(_(" -d, --debug=LEVEL "
111 "set the debug level, integer between 0 and 3"));
115 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
117 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
121 * Updates the size of the sensor values if different than the
125 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
127 struct psensor **cur, *s;
129 for (cur = sensors; *cur; cur++) {
132 if (s->values_max_length != cfg->sensor_values_max_length)
133 psensor_values_resize(s,
134 cfg->sensor_values_max_length);
138 static void *update_measures(void *data)
140 struct psensor **sensors;
143 struct ui_psensor *ui;
145 ui = (struct ui_psensor *)data;
149 pmutex_lock(&ui->sensors_mutex);
151 sensors = ui->sensors;
155 update_psensor_values_size(sensors, cfg);
157 psensor_list_update_measures(sensors);
159 lmsensor_psensor_list_update(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 atasmart_psensor_list_update(sensors);
179 hddtemp_psensor_list_update(sensors);
181 psensor_log_measures(sensors);
183 period = cfg->sensor_update_interval;
185 pmutex_unlock(&ui->sensors_mutex);
191 static void indicators_update(struct ui_psensor *ui)
193 struct psensor **sensor_cur = ui->sensors;
194 unsigned int attention = 0;
196 while (*sensor_cur) {
197 struct psensor *s = *sensor_cur;
199 if (s->alarm_enabled && s->alarm_raised) {
207 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
208 if (is_appindicator_supported())
209 ui_appindicator_update(ui, attention);
212 if (is_status_supported())
213 ui_status_update(ui, attention);
216 gboolean ui_refresh_thread(gpointer data)
220 struct ui_psensor *ui = (struct ui_psensor *)data;
225 pmutex_lock(&ui->sensors_mutex);
227 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
229 ui_sensorlist_update(ui, 0);
231 if (is_appindicator_supported() || is_status_supported())
232 indicators_update(ui);
235 ui_unity_launcher_entry_update(ui->sensors,
236 !cfg->unity_launcher_count_disabled,
237 cfg->temperature_unit == CELSIUS);
240 if (ui->graph_update_interval != cfg->graph_update_interval) {
241 ui->graph_update_interval = cfg->graph_update_interval;
245 pmutex_unlock(&ui->sensors_mutex);
248 g_timeout_add(1000 * ui->graph_update_interval,
249 ui_refresh_thread, ui);
254 static void cb_alarm_raised(struct psensor *sensor, void *data)
256 #ifdef HAVE_LIBNOTIFY
257 if (sensor->alarm_enabled)
258 ui_notify(sensor, (struct ui_psensor *)data);
264 static void associate_colors(struct psensor **sensors)
266 /* number of uniq colors */
267 #define COLORS_COUNT 8
269 double colors[COLORS_COUNT][3] = {
270 {0, 0, 0}, /* black */
272 {0, 0, 1}, /* blue */
273 {0, 1, 0}, /* green */
275 {0.5, 0.5, 0.5},/* grey */
276 {0.5, 0, 0}, /* dark red */
277 {0, 0, 0.5}, /* dark blue */
278 {0, 0.5, 0} /* dark green */
280 struct psensor **cur;
284 for (cur = sensors, i = 0; *cur; cur++, i++) {
286 colors[i % COLORS_COUNT][0],
287 colors[i % COLORS_COUNT][1],
288 colors[i % COLORS_COUNT][2]);
290 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
295 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
297 struct psensor **sensor_cur = sensors;
299 while (*sensor_cur) {
300 struct psensor *s = *sensor_cur;
302 s->cb_alarm_raised = cb_alarm_raised;
303 s->cb_alarm_raised_data = ui;
305 s->alarm_high_threshold
306 = config_get_sensor_alarm_high_threshold(s->id);
307 s->alarm_low_threshold
308 = config_get_sensor_alarm_low_threshold(s->id);
311 = config_get_sensor_alarm_enabled(s->id);
317 static void associate_preferences(struct psensor **sensors)
319 struct psensor **sensor_cur = sensors;
321 while (*sensor_cur) {
323 struct psensor *s = *sensor_cur;
325 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
327 n = config_get_sensor_name(s->id);
334 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
340 static void log_init()
345 dir = get_psensor_user_dir();
350 path = malloc(strlen(dir)+1+strlen("log")+1);
351 sprintf(path, "%s/%s", dir, "log");
358 static struct option long_options[] = {
359 {"version", no_argument, 0, 'v'},
360 {"help", no_argument, 0, 'h'},
361 {"url", required_argument, 0, 'u'},
362 {"debug", required_argument, 0, 'd'},
363 {"new-instance", no_argument, 0, 'n'},
367 static gboolean initial_window_show(gpointer data)
369 struct ui_psensor *ui;
371 log_debug("initial_window_show()");
373 ui = (struct ui_psensor *)data;
375 log_debug("is_status_supported: %d", is_status_supported());
376 log_debug("is_appindicator_supported: %d",
377 is_appindicator_supported());
378 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
380 if (!ui->config->hide_on_startup
381 || (!is_appindicator_supported() && !is_status_supported()))
384 ui_window_update(ui);
389 static void log_glib_info()
391 log_debug("Compiled with GLib %d.%d.%d",
396 log_debug("Running with GLib %d.%d.%d",
402 static void cb_activate(GApplication *application,
405 ui_window_show((struct ui_psensor *)data);
409 * Release memory for Valgrind.
411 static void cleanup(struct ui_psensor *ui)
413 pmutex_lock(&ui->sensors_mutex);
415 log_debug("Cleanup...");
422 #ifdef HAVE_LIBATIADL
425 #ifdef HAVE_REMOTE_SUPPORT
429 psensor_list_free(ui->sensors);
432 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
433 ui_appindicator_cleanup();
438 pmutex_unlock(&ui->sensors_mutex);
442 log_debug("Cleanup done, closing log");
446 * Creates the list of sensors.
448 * 'url': remote psensor server url, null for local monitoring.
450 static struct psensor **create_sensors_list(const char *url)
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 = malloc(sizeof(struct psensor *));
467 if (config_is_lmsensor_enabled())
468 lmsensor_psensor_list_append(&sensors, 600);
470 if (config_is_hddtemp_enabled())
471 hddtemp_psensor_list_append(&sensors, 600);
474 if (config_is_libatasmart_enabled())
475 atasmart_psensor_list_append(&sensors, 600);
479 if (config_is_nvctrl_enabled())
480 nvidia_psensor_list_append(&sensors, 600);
482 #ifdef HAVE_LIBATIADL
483 if (config_is_atiadlsdk_enabled())
484 amd_psensor_list_append(&sensors, 600);
487 if (config_is_gtop2_enabled())
488 gtop2_psensor_list_append(&sensors, 600);
490 #ifdef HAVE_LIBUDISKS2
491 if (config_is_udisks2_enabled())
492 udisks2_psensor_list_append(&sensors, 600);
496 associate_preferences(sensors);
497 associate_colors(sensors);
502 int main(int argc, char **argv)
504 struct ui_psensor ui;
506 int optc, cmdok, opti, new_instance, ret;
510 program_name = argv[0];
512 setlocale(LC_ALL, "");
515 bindtextdomain(PACKAGE, LOCALEDIR);
522 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
527 url = strdup(optarg);
536 log_level = atoi(optarg);
537 log_info(_("Enables debug mode."));
548 if (!cmdok || optind != argc) {
549 fprintf(stderr, _("Try `%s --help' for more information.\n"),
556 app = g_application_new("wpitchoune.psensor", 0);
558 g_application_register(app, NULL, NULL);
560 if (!new_instance && g_application_get_is_remote(app)) {
561 g_application_activate(app);
562 log_warn(_("A Psensor instance already exists."));
566 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
569 #if !(GLIB_CHECK_VERSION(2, 31, 0))
571 * Since GLib 2.31 g_thread_init call is deprecated and not
574 log_debug("Calling g_thread_init(NULL)");
578 #ifdef HAVE_APPINDICATOR_029
579 /* gdk_thread_enter/leave only used to workaround mutex bug
580 * of appindicator < 0.2.9, so do not call gdk_threads_init
581 * if useless. Calling this function leads to
582 * crash "Attempt to unlock mutex that was not locked" with
583 * GLib 2.41.2 (new checking) probably due to bugs in GTK
589 gtk_init(NULL, NULL);
591 pmutex_init(&ui.sensors_mutex);
593 ui.config = config_load();
597 ui.sensors = create_sensors_list(url);
598 associate_cb_alarm_raised(ui.sensors, &ui);
600 if (ui.config->slog_enabled)
604 config_get_slog_interval());
606 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
608 ui_status_set_visible(1);
612 ui_window_create(&ui);
614 ui_enable_alpha_channel(&ui);
616 ret = pthread_create(&thread, NULL, update_measures, &ui);
619 log_err(_("Failed to create thread for monitoring sensors"));
621 ui.graph_update_interval = ui.config->graph_update_interval;
623 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
625 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
626 ui_appindicator_init(&ui);
629 gdk_notify_startup_complete();
632 * hack, did not find a cleaner solution.
633 * wait 30s to ensure that the status icon is attempted to be
634 * drawn before determining whether the main window must be
637 if (ui.config->hide_on_startup)
638 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
640 initial_window_show(&ui);
642 log_debug("translators: %s\n", _("translator-credits"));
650 log_debug("Quitting...");