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_notify.h>
48 #include "ui_status.h"
62 #ifdef HAVE_REMOTE_SUPPORT
66 #include "ui_appindicator.h"
72 static const char *program_name;
74 static void print_version(void)
76 printf("psensor %s\n", VERSION);
77 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
78 "License GPLv2: GNU GPL version 2 or later "
79 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
80 "This is free software: you are free to change and"
82 "There is NO WARRANTY, to the extent permitted by law.\n"),
86 static void print_help(void)
88 printf(_("Usage: %s [OPTION]...\n"), program_name);
90 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
91 "including temperatures and fan speeds."));
95 puts(_(" -h, --help display this help and exit\n"
96 " -v, --version display version information and exit"));
101 " -u, --url=URL the URL of the psensor-server,\n"
102 " example: http://hostname:3131"));
104 " -n, --new-instance force the creation of a new Psensor application"));
107 puts(_(" -d, --debug=LEVEL "
108 "set the debug level, integer between 0 and 3"));
112 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
114 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
118 * Updates the size of the sensor values if different than the
122 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
124 struct psensor **cur, *s;
126 for (cur = sensors; *cur; cur++) {
129 if (s->values_max_length != cfg->sensor_values_max_length)
130 psensor_values_resize(s,
131 cfg->sensor_values_max_length);
135 static void *update_measures(void *data)
137 struct psensor **sensors;
140 struct ui_psensor *ui;
142 ui = (struct ui_psensor *)data;
146 pmutex_lock(&ui->sensors_mutex);
148 sensors = ui->sensors;
152 update_psensor_values_size(sensors, cfg);
154 lmsensor_psensor_list_update(sensors);
155 #ifdef HAVE_REMOTE_SUPPORT
156 remote_psensor_list_update(sensors);
159 nvidia_psensor_list_update(sensors);
161 #ifdef HAVE_LIBATIADL
162 amd_psensor_list_update(sensors);
164 #ifdef HAVE_LIBUDISKS2
165 udisks2_psensor_list_update(sensors);
168 gtop2_psensor_list_update(sensors);
171 atasmart_psensor_list_update(sensors);
174 hddtemp_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)
203 if (is_appindicator_supported())
204 ui_appindicator_update(ui, attention);
207 if (is_status_supported())
208 ui_status_update(ui, attention);
211 static 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 if (sensor->alarm_enabled)
252 ui_notify(sensor, (struct ui_psensor *)data);
257 static void associate_colors(struct psensor **sensors)
260 /* copied from the default colors of the gtk color color
262 const char *default_colors[27] = {
263 "#ef2929", /* Scarlet Red */
264 "#fcaf3e", /* Orange */
265 "#fce94f", /* Butter */
266 "#8ae234", /* Chameleon */
267 "#729fcf", /* Sky Blue */
268 "#ad7fa8", /* Plum */
269 "#e9b96e", /* Chocolate */
270 "#888a85", /* Aluminum 1 */
271 "#eeeeec", /* Aluminum 2 */
292 struct psensor **cur;
295 for (cur = sensors, i = 0; *cur; cur++, i++) {
296 gdk_rgba_parse(&rgba, default_colors[i % 27]);
298 c.green = rgba.green;
301 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
306 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
308 struct psensor **sensor_cur = sensors;
310 while (*sensor_cur) {
311 struct psensor *s = *sensor_cur;
313 s->cb_alarm_raised = cb_alarm_raised;
314 s->cb_alarm_raised_data = ui;
316 s->alarm_high_threshold
317 = config_get_sensor_alarm_high_threshold(s->id);
318 s->alarm_low_threshold
319 = config_get_sensor_alarm_low_threshold(s->id);
322 = config_get_sensor_alarm_enabled(s->id);
328 static void associate_preferences(struct psensor **sensors)
330 struct psensor **sensor_cur = sensors;
332 while (*sensor_cur) {
334 struct psensor *s = *sensor_cur;
336 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
338 n = config_get_sensor_name(s->id);
349 static void log_init(void)
354 dir = get_psensor_user_dir();
359 path = malloc(strlen(dir)+1+strlen("log")+1);
360 sprintf(path, "%s/%s", dir, "log");
367 static struct option long_options[] = {
368 {"version", no_argument, NULL, 'v'},
369 {"help", no_argument, NULL, 'h'},
370 {"url", required_argument, NULL, 'u'},
371 {"debug", required_argument, NULL, 'd'},
372 {"new-instance", no_argument, NULL, 'n'},
376 static gboolean initial_window_show(gpointer data)
378 struct ui_psensor *ui;
380 log_debug("initial_window_show()");
382 ui = (struct ui_psensor *)data;
384 log_debug("is_status_supported: %d", is_status_supported());
385 log_debug("is_appindicator_supported: %d",
386 is_appindicator_supported());
387 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
389 if (!ui->config->hide_on_startup
390 || (!is_appindicator_supported() && !is_status_supported()))
393 ui_window_update(ui);
398 static void log_glib_info(void)
400 log_debug("Compiled with GLib %d.%d.%d",
405 log_debug("Running with GLib %d.%d.%d",
411 static void cb_activate(GApplication *application,
414 ui_window_show((struct ui_psensor *)data);
418 * Release memory for Valgrind.
420 static void cleanup(struct ui_psensor *ui)
422 pmutex_lock(&ui->sensors_mutex);
424 log_debug("Cleanup...");
429 #ifdef HAVE_LIBATIADL
432 #ifdef HAVE_REMOTE_SUPPORT
436 psensor_list_free(ui->sensors);
439 #if defined(HAVE_APPINDICATOR)
440 ui_appindicator_cleanup();
445 pmutex_unlock(&ui->sensors_mutex);
449 log_debug("Cleanup done, closing log");
453 * Creates the list of sensors.
455 * 'url': remote psensor server url, null for local monitoring.
457 static struct psensor **create_sensors_list(const char *url)
459 struct psensor **sensors;
462 #ifdef HAVE_REMOTE_SUPPORT
464 sensors = get_remote_sensors(url, 600);
466 log_err(_("Psensor has not been compiled with remote "
471 sensors = malloc(sizeof(struct psensor *));
474 if (config_is_lmsensor_enabled())
475 lmsensor_psensor_list_append(&sensors, 600);
477 if (config_is_hddtemp_enabled())
478 hddtemp_psensor_list_append(&sensors, 600);
481 if (config_is_libatasmart_enabled())
482 atasmart_psensor_list_append(&sensors, 600);
486 if (config_is_nvctrl_enabled())
487 nvidia_psensor_list_append(&sensors, 600);
489 #ifdef HAVE_LIBATIADL
490 if (config_is_atiadlsdk_enabled())
491 amd_psensor_list_append(&sensors, 600);
494 if (config_is_gtop2_enabled())
495 gtop2_psensor_list_append(&sensors, 600);
497 #ifdef HAVE_LIBUDISKS2
498 if (config_is_udisks2_enabled())
499 udisks2_psensor_list_append(&sensors, 600);
503 associate_preferences(sensors);
504 associate_colors(sensors);
509 int main(int argc, char **argv)
511 struct ui_psensor ui;
513 int optc, cmdok, opti, new_instance, ret;
517 program_name = argv[0];
519 setlocale(LC_ALL, "");
522 bindtextdomain(PACKAGE, LOCALEDIR);
529 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
534 url = strdup(optarg);
543 log_level = atoi(optarg);
544 log_info(_("Enables debug mode."));
555 if (!cmdok || optind != argc) {
556 fprintf(stderr, _("Try `%s --help' for more information.\n"),
563 app = g_application_new("wpitchoune.psensor", 0);
565 g_application_register(app, NULL, NULL);
567 if (!new_instance && g_application_get_is_remote(app)) {
568 g_application_activate(app);
569 log_warn(_("A Psensor instance already exists."));
573 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
576 #if !(GLIB_CHECK_VERSION(2, 31, 0))
578 * Since GLib 2.31 g_thread_init call is deprecated and not
581 log_debug("Calling g_thread_init(NULL)");
585 gtk_init(NULL, NULL);
587 pmutex_init(&ui.sensors_mutex);
589 ui.config = config_load();
591 ui.sensors = create_sensors_list(url);
592 associate_cb_alarm_raised(ui.sensors, &ui);
594 if (ui.config->slog_enabled)
598 config_get_slog_interval());
600 #if !defined(HAVE_APPINDICATOR)
602 ui_status_set_visible(1);
606 ui_window_create(&ui);
608 ui_enable_alpha_channel(&ui);
610 ret = pthread_create(&thread, NULL, update_measures, &ui);
613 log_err(_("Failed to create thread for monitoring sensors"));
615 ui.graph_update_interval = ui.config->graph_update_interval;
617 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
619 #if defined(HAVE_APPINDICATOR)
620 ui_appindicator_init(&ui);
623 gdk_notify_startup_complete();
626 * hack, did not find a cleaner solution.
627 * wait 30s to ensure that the status icon is attempted to be
628 * drawn before determining whether the main window must be
631 if (ui.config->hide_on_startup)
632 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
634 initial_window_show(&ui);
636 log_debug("translators: %s\n", _("translator-credits"));
644 log_debug("Quitting...");