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 " --use-libatasmart use atasmart library for disk monitoring instead of\n"
110 " -n, --new-instance force the creation of a new Psensor application"));
113 puts(_(" -d, --debug=LEVEL "
114 "set the debug level, integer between 0 and 3"));
118 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
120 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
124 * Updates the size of the sensor values if different than the
128 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
130 struct psensor **cur, *s;
132 for (cur = sensors; *cur; cur++) {
135 if (s->values_max_length != cfg->sensor_values_max_length)
136 psensor_values_resize(s,
137 cfg->sensor_values_max_length);
141 static void *update_measures(void *data)
143 struct psensor **sensors;
146 struct ui_psensor *ui;
148 ui = (struct ui_psensor *)data;
152 pmutex_lock(&ui->sensors_mutex);
154 sensors = ui->sensors;
158 update_psensor_values_size(sensors, cfg);
160 psensor_list_update_measures(sensors);
162 lmsensor_psensor_list_update(sensors);
163 #ifdef HAVE_REMOTE_SUPPORT
164 remote_psensor_list_update(sensors);
167 nvidia_psensor_list_update(sensors);
169 #ifdef HAVE_LIBATIADL
170 amd_psensor_list_update(sensors);
172 #ifdef HAVE_LIBUDISKS2
173 udisks2_psensor_list_update(sensors);
176 gtop2_psensor_list_update(sensors);
179 atasmart_psensor_list_update(sensors);
182 hddtemp_psensor_list_update(sensors);
184 psensor_log_measures(sensors);
186 period = cfg->sensor_update_interval;
188 pmutex_unlock(&ui->sensors_mutex);
194 static void indicators_update(struct ui_psensor *ui)
196 struct psensor **sensor_cur = ui->sensors;
197 unsigned int attention = 0;
199 while (*sensor_cur) {
200 struct psensor *s = *sensor_cur;
202 if (s->alarm_enabled && s->alarm_raised) {
210 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
211 if (is_appindicator_supported())
212 ui_appindicator_update(ui, attention);
215 if (is_status_supported())
216 ui_status_update(ui, attention);
219 gboolean ui_refresh_thread(gpointer data)
223 struct ui_psensor *ui = (struct ui_psensor *)data;
228 pmutex_lock(&ui->sensors_mutex);
230 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
232 ui_sensorlist_update(ui, 0);
234 if (is_appindicator_supported() || is_status_supported())
235 indicators_update(ui);
238 ui_unity_launcher_entry_update(ui->sensors,
239 !cfg->unity_launcher_count_disabled,
240 cfg->temperature_unit == CELSIUS);
243 if (ui->graph_update_interval != cfg->graph_update_interval) {
244 ui->graph_update_interval = cfg->graph_update_interval;
248 pmutex_unlock(&ui->sensors_mutex);
251 g_timeout_add(1000 * ui->graph_update_interval,
252 ui_refresh_thread, ui);
257 static void cb_alarm_raised(struct psensor *sensor, void *data)
259 #ifdef HAVE_LIBNOTIFY
260 if (sensor->alarm_enabled)
261 ui_notify(sensor, (struct ui_psensor *)data);
267 static void associate_colors(struct psensor **sensors)
269 /* number of uniq colors */
270 #define COLORS_COUNT 8
272 double colors[COLORS_COUNT][3] = {
273 {0, 0, 0}, /* black */
275 {0, 0, 1}, /* blue */
276 {0, 1, 0}, /* green */
278 {0.5, 0.5, 0.5},/* grey */
279 {0.5, 0, 0}, /* dark red */
280 {0, 0, 0.5}, /* dark blue */
281 {0, 0.5, 0} /* dark green */
283 struct psensor **cur;
287 for (cur = sensors, i = 0; *cur; cur++, i++) {
289 colors[i % COLORS_COUNT][0],
290 colors[i % COLORS_COUNT][1],
291 colors[i % COLORS_COUNT][2]);
293 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
298 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
300 struct psensor **sensor_cur = sensors;
302 while (*sensor_cur) {
303 struct psensor *s = *sensor_cur;
305 s->cb_alarm_raised = cb_alarm_raised;
306 s->cb_alarm_raised_data = ui;
308 s->alarm_high_threshold
309 = config_get_sensor_alarm_high_threshold(s->id);
310 s->alarm_low_threshold
311 = config_get_sensor_alarm_low_threshold(s->id);
314 = config_get_sensor_alarm_enabled(s->id);
320 static void associate_preferences(struct psensor **sensors)
322 struct psensor **sensor_cur = sensors;
324 while (*sensor_cur) {
326 struct psensor *s = *sensor_cur;
328 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
330 n = config_get_sensor_name(s->id);
337 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
343 static void log_init()
348 dir = get_psensor_user_dir();
353 path = malloc(strlen(dir)+1+strlen("log")+1);
354 sprintf(path, "%s/%s", dir, "log");
361 static struct option long_options[] = {
362 {"use-libatasmart", no_argument, 0, 0},
363 {"version", no_argument, 0, 'v'},
364 {"help", no_argument, 0, 'h'},
365 {"url", required_argument, 0, 'u'},
366 {"debug", required_argument, 0, 'd'},
367 {"new-instance", no_argument, 0, 'n'},
371 static gboolean initial_window_show(gpointer data)
373 struct ui_psensor *ui;
375 log_debug("initial_window_show()");
377 ui = (struct ui_psensor *)data;
379 log_debug("is_status_supported: %d", is_status_supported());
380 log_debug("is_appindicator_supported: %d",
381 is_appindicator_supported());
382 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
384 if (!ui->config->hide_on_startup
385 || (!is_appindicator_supported() && !is_status_supported()))
388 ui_window_update(ui);
393 static void log_glib_info()
395 log_debug("Compiled with GLib %d.%d.%d",
400 log_debug("Running with GLib %d.%d.%d",
406 static void cb_activate(GApplication *application,
409 ui_window_show((struct ui_psensor *)data);
413 * Release memory for Valgrind.
415 static void cleanup(struct ui_psensor *ui)
417 pmutex_lock(&ui->sensors_mutex);
419 log_debug("Cleanup...");
426 #ifdef HAVE_LIBATIADL
429 #ifdef HAVE_REMOTE_SUPPORT
433 psensor_list_free(ui->sensors);
436 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
437 ui_appindicator_cleanup();
442 pmutex_unlock(&ui->sensors_mutex);
446 log_debug("Cleanup done, closing log");
450 * Creates the list of sensors.
452 * 'url': remote psensor server url, null for local monitoring.
454 static struct psensor **create_sensors_list(const char *url)
456 struct psensor **sensors;
459 #ifdef HAVE_REMOTE_SUPPORT
461 sensors = get_remote_sensors(url, 600);
463 log_err(_("Psensor has not been compiled with remote "
468 sensors = malloc(sizeof(struct psensor *));
471 if (config_is_lmsensor_enabled())
472 lmsensor_psensor_list_append(&sensors, 600);
474 if (config_is_hddtemp_enabled())
475 hddtemp_psensor_list_append(&sensors, 600);
478 if (config_is_libatasmart_enabled())
479 atasmart_psensor_list_append(&sensors, 600);
483 if (config_is_nvctrl_enabled())
484 nvidia_psensor_list_append(&sensors, 600);
486 #ifdef HAVE_LIBATIADL
487 if (config_is_atiadlsdk_enabled())
488 amd_psensor_list_append(&sensors, 600);
491 if (config_is_gtop2_enabled())
492 gtop2_psensor_list_append(&sensors, 600);
494 #ifdef HAVE_LIBUDISKS2
495 if (config_is_udisks2_enabled())
496 udisks2_psensor_list_append(&sensors, 600);
500 associate_preferences(sensors);
501 associate_colors(sensors);
506 int main(int argc, char **argv)
508 struct ui_psensor ui;
510 int optc, cmdok, opti, new_instance, ret;
514 program_name = argv[0];
516 setlocale(LC_ALL, "");
519 bindtextdomain(PACKAGE, LOCALEDIR);
526 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
531 url = strdup(optarg);
540 log_level = atoi(optarg);
541 log_info(_("Enables debug mode."));
552 if (!cmdok || optind != argc) {
553 fprintf(stderr, _("Try `%s --help' for more information.\n"),
560 app = g_application_new("wpitchoune.psensor", 0);
562 g_application_register(app, NULL, NULL);
564 if (!new_instance && g_application_get_is_remote(app)) {
565 g_application_activate(app);
566 log_warn(_("A Psensor instance already exists."));
570 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
573 #if !(GLIB_CHECK_VERSION(2, 31, 0))
575 * Since GLib 2.31 g_thread_init call is deprecated and not
578 log_debug("Calling g_thread_init(NULL)");
582 #ifdef HAVE_APPINDICATOR_029
583 /* gdk_thread_enter/leave only used to workaround mutex bug
584 * of appindicator < 0.2.9, so do not call gdk_threads_init
585 * if useless. Calling this function leads to
586 * crash "Attempt to unlock mutex that was not locked" with
587 * GLib 2.41.2 (new checking) probably due to bugs in GTK
593 gtk_init(NULL, NULL);
595 pmutex_init(&ui.sensors_mutex);
597 ui.config = config_load();
601 ui.sensors = create_sensors_list(url);
602 associate_cb_alarm_raised(ui.sensors, &ui);
604 if (ui.config->slog_enabled)
608 config_get_slog_interval());
610 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
612 ui_status_set_visible(1);
616 ui_window_create(&ui);
618 ui_enable_alpha_channel(&ui);
620 ret = pthread_create(&thread, NULL, update_measures, &ui);
623 log_err(_("Failed to create thread for monitoring sensors"));
625 ui.graph_update_interval = ui.config->graph_update_interval;
627 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
629 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
630 ui_appindicator_init(&ui);
633 gdk_notify_startup_complete();
636 * hack, did not find a cleaner solution.
637 * wait 30s to ensure that the status icon is attempted to be
638 * drawn before determining whether the main window must be
641 if (ui.config->hide_on_startup)
642 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
644 initial_window_show(&ui);
646 log_debug("translators: %s\n", _("translator-credits"));
654 log_debug("Quitting...");