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)
267 /* copied from the default colors of the gtk color color
269 const char *default_colors[27] = {
270 "#ef2929", /* Scarlet Red */
271 "#fcaf3e", /* Orange */
272 "#fce94f", /* Butter */
273 "#8ae234", /* Chameleon */
274 "#729fcf", /* Sky Blue */
275 "#ad7fa8", /* Plum */
276 "#e9b96e", /* Chocolate */
277 "#888a85", /* Aluminum 1 */
278 "#eeeeec", /* Aluminum 2 */
299 struct psensor **cur;
302 for (cur = sensors, i = 0; *cur; cur++, i++) {
303 gdk_rgba_parse(&rgba, default_colors[i % 27]);
305 c.green = rgba.green;
308 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
313 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
315 struct psensor **sensor_cur = sensors;
317 while (*sensor_cur) {
318 struct psensor *s = *sensor_cur;
320 s->cb_alarm_raised = cb_alarm_raised;
321 s->cb_alarm_raised_data = ui;
323 s->alarm_high_threshold
324 = config_get_sensor_alarm_high_threshold(s->id);
325 s->alarm_low_threshold
326 = config_get_sensor_alarm_low_threshold(s->id);
329 = config_get_sensor_alarm_enabled(s->id);
335 static void associate_preferences(struct psensor **sensors)
337 struct psensor **sensor_cur = sensors;
339 while (*sensor_cur) {
341 struct psensor *s = *sensor_cur;
343 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
345 n = config_get_sensor_name(s->id);
352 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
358 static void log_init()
363 dir = get_psensor_user_dir();
368 path = malloc(strlen(dir)+1+strlen("log")+1);
369 sprintf(path, "%s/%s", dir, "log");
376 static struct option long_options[] = {
377 {"version", no_argument, 0, 'v'},
378 {"help", no_argument, 0, 'h'},
379 {"url", required_argument, 0, 'u'},
380 {"debug", required_argument, 0, 'd'},
381 {"new-instance", no_argument, 0, 'n'},
385 static gboolean initial_window_show(gpointer data)
387 struct ui_psensor *ui;
389 log_debug("initial_window_show()");
391 ui = (struct ui_psensor *)data;
393 log_debug("is_status_supported: %d", is_status_supported());
394 log_debug("is_appindicator_supported: %d",
395 is_appindicator_supported());
396 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
398 if (!ui->config->hide_on_startup
399 || (!is_appindicator_supported() && !is_status_supported()))
402 ui_window_update(ui);
407 static void log_glib_info()
409 log_debug("Compiled with GLib %d.%d.%d",
414 log_debug("Running with GLib %d.%d.%d",
420 static void cb_activate(GApplication *application,
423 ui_window_show((struct ui_psensor *)data);
427 * Release memory for Valgrind.
429 static void cleanup(struct ui_psensor *ui)
431 pmutex_lock(&ui->sensors_mutex);
433 log_debug("Cleanup...");
440 #ifdef HAVE_LIBATIADL
443 #ifdef HAVE_REMOTE_SUPPORT
447 psensor_list_free(ui->sensors);
450 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
451 ui_appindicator_cleanup();
456 pmutex_unlock(&ui->sensors_mutex);
460 log_debug("Cleanup done, closing log");
464 * Creates the list of sensors.
466 * 'url': remote psensor server url, null for local monitoring.
468 static struct psensor **create_sensors_list(const char *url)
470 struct psensor **sensors;
473 #ifdef HAVE_REMOTE_SUPPORT
475 sensors = get_remote_sensors(url, 600);
477 log_err(_("Psensor has not been compiled with remote "
482 sensors = malloc(sizeof(struct psensor *));
485 if (config_is_lmsensor_enabled())
486 lmsensor_psensor_list_append(&sensors, 600);
488 if (config_is_hddtemp_enabled())
489 hddtemp_psensor_list_append(&sensors, 600);
492 if (config_is_libatasmart_enabled())
493 atasmart_psensor_list_append(&sensors, 600);
497 if (config_is_nvctrl_enabled())
498 nvidia_psensor_list_append(&sensors, 600);
500 #ifdef HAVE_LIBATIADL
501 if (config_is_atiadlsdk_enabled())
502 amd_psensor_list_append(&sensors, 600);
505 if (config_is_gtop2_enabled())
506 gtop2_psensor_list_append(&sensors, 600);
508 #ifdef HAVE_LIBUDISKS2
509 if (config_is_udisks2_enabled())
510 udisks2_psensor_list_append(&sensors, 600);
514 associate_preferences(sensors);
515 associate_colors(sensors);
520 int main(int argc, char **argv)
522 struct ui_psensor ui;
524 int optc, cmdok, opti, new_instance, ret;
528 program_name = argv[0];
530 setlocale(LC_ALL, "");
533 bindtextdomain(PACKAGE, LOCALEDIR);
540 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
545 url = strdup(optarg);
554 log_level = atoi(optarg);
555 log_info(_("Enables debug mode."));
566 if (!cmdok || optind != argc) {
567 fprintf(stderr, _("Try `%s --help' for more information.\n"),
574 app = g_application_new("wpitchoune.psensor", 0);
576 g_application_register(app, NULL, NULL);
578 if (!new_instance && g_application_get_is_remote(app)) {
579 g_application_activate(app);
580 log_warn(_("A Psensor instance already exists."));
584 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
587 #if !(GLIB_CHECK_VERSION(2, 31, 0))
589 * Since GLib 2.31 g_thread_init call is deprecated and not
592 log_debug("Calling g_thread_init(NULL)");
596 #ifdef HAVE_APPINDICATOR_029
597 /* gdk_thread_enter/leave only used to workaround mutex bug
598 * of appindicator < 0.2.9, so do not call gdk_threads_init
599 * if useless. Calling this function leads to
600 * crash "Attempt to unlock mutex that was not locked" with
601 * GLib 2.41.2 (new checking) probably due to bugs in GTK
607 gtk_init(NULL, NULL);
609 pmutex_init(&ui.sensors_mutex);
611 ui.config = config_load();
615 ui.sensors = create_sensors_list(url);
616 associate_cb_alarm_raised(ui.sensors, &ui);
618 if (ui.config->slog_enabled)
622 config_get_slog_interval());
624 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
626 ui_status_set_visible(1);
630 ui_window_create(&ui);
632 ui_enable_alpha_channel(&ui);
634 ret = pthread_create(&thread, NULL, update_measures, &ui);
637 log_err(_("Failed to create thread for monitoring sensors"));
639 ui.graph_update_interval = ui.config->graph_update_interval;
641 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
643 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
644 ui_appindicator_init(&ui);
647 gdk_notify_startup_complete();
650 * hack, did not find a cleaner solution.
651 * wait 30s to ensure that the status icon is attempted to be
652 * drawn before determining whether the main window must be
655 if (ui.config->hide_on_startup)
656 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
658 initial_window_show(&ui);
660 log_debug("translators: %s\n", _("translator-credits"));
668 log_debug("Quitting...");