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 lmsensor_psensor_list_update(sensors);
158 #ifdef HAVE_REMOTE_SUPPORT
159 remote_psensor_list_update(sensors);
162 nvidia_psensor_list_update(sensors);
164 #ifdef HAVE_LIBATIADL
165 amd_psensor_list_update(sensors);
167 #ifdef HAVE_LIBUDISKS2
168 udisks2_psensor_list_update(sensors);
171 gtop2_psensor_list_update(sensors);
174 atasmart_psensor_list_update(sensors);
177 hddtemp_psensor_list_update(sensors);
179 psensor_log_measures(sensors);
181 period = cfg->sensor_update_interval;
183 pmutex_unlock(&ui->sensors_mutex);
189 static void indicators_update(struct ui_psensor *ui)
191 struct psensor **sensor_cur = ui->sensors;
192 unsigned int attention = 0;
194 while (*sensor_cur) {
195 struct psensor *s = *sensor_cur;
197 if (s->alarm_enabled && s->alarm_raised) {
205 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
206 if (is_appindicator_supported())
207 ui_appindicator_update(ui, attention);
210 if (is_status_supported())
211 ui_status_update(ui, attention);
214 gboolean ui_refresh_thread(gpointer data)
218 struct ui_psensor *ui = (struct ui_psensor *)data;
223 pmutex_lock(&ui->sensors_mutex);
225 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
227 ui_sensorlist_update(ui, 0);
229 if (is_appindicator_supported() || is_status_supported())
230 indicators_update(ui);
233 ui_unity_launcher_entry_update(ui->sensors,
234 !cfg->unity_launcher_count_disabled,
235 cfg->temperature_unit == CELSIUS);
238 if (ui->graph_update_interval != cfg->graph_update_interval) {
239 ui->graph_update_interval = cfg->graph_update_interval;
243 pmutex_unlock(&ui->sensors_mutex);
246 g_timeout_add(1000 * ui->graph_update_interval,
247 ui_refresh_thread, ui);
252 static void cb_alarm_raised(struct psensor *sensor, void *data)
254 #ifdef HAVE_LIBNOTIFY
255 if (sensor->alarm_enabled)
256 ui_notify(sensor, (struct ui_psensor *)data);
262 static void associate_colors(struct psensor **sensors)
265 /* copied from the default colors of the gtk color color
267 const char *default_colors[27] = {
268 "#ef2929", /* Scarlet Red */
269 "#fcaf3e", /* Orange */
270 "#fce94f", /* Butter */
271 "#8ae234", /* Chameleon */
272 "#729fcf", /* Sky Blue */
273 "#ad7fa8", /* Plum */
274 "#e9b96e", /* Chocolate */
275 "#888a85", /* Aluminum 1 */
276 "#eeeeec", /* Aluminum 2 */
297 struct psensor **cur;
300 for (cur = sensors, i = 0; *cur; cur++, i++) {
301 gdk_rgba_parse(&rgba, default_colors[i % 27]);
303 c.green = rgba.green;
306 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
311 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
313 struct psensor **sensor_cur = sensors;
315 while (*sensor_cur) {
316 struct psensor *s = *sensor_cur;
318 s->cb_alarm_raised = cb_alarm_raised;
319 s->cb_alarm_raised_data = ui;
321 s->alarm_high_threshold
322 = config_get_sensor_alarm_high_threshold(s->id);
323 s->alarm_low_threshold
324 = config_get_sensor_alarm_low_threshold(s->id);
327 = config_get_sensor_alarm_enabled(s->id);
333 static void associate_preferences(struct psensor **sensors)
335 struct psensor **sensor_cur = sensors;
337 while (*sensor_cur) {
339 struct psensor *s = *sensor_cur;
341 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
343 n = config_get_sensor_name(s->id);
350 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
356 static void log_init()
361 dir = get_psensor_user_dir();
366 path = malloc(strlen(dir)+1+strlen("log")+1);
367 sprintf(path, "%s/%s", dir, "log");
374 static struct option long_options[] = {
375 {"version", no_argument, 0, 'v'},
376 {"help", no_argument, 0, 'h'},
377 {"url", required_argument, 0, 'u'},
378 {"debug", required_argument, 0, 'd'},
379 {"new-instance", no_argument, 0, 'n'},
383 static gboolean initial_window_show(gpointer data)
385 struct ui_psensor *ui;
387 log_debug("initial_window_show()");
389 ui = (struct ui_psensor *)data;
391 log_debug("is_status_supported: %d", is_status_supported());
392 log_debug("is_appindicator_supported: %d",
393 is_appindicator_supported());
394 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
396 if (!ui->config->hide_on_startup
397 || (!is_appindicator_supported() && !is_status_supported()))
400 ui_window_update(ui);
405 static void log_glib_info()
407 log_debug("Compiled with GLib %d.%d.%d",
412 log_debug("Running with GLib %d.%d.%d",
418 static void cb_activate(GApplication *application,
421 ui_window_show((struct ui_psensor *)data);
425 * Release memory for Valgrind.
427 static void cleanup(struct ui_psensor *ui)
429 pmutex_lock(&ui->sensors_mutex);
431 log_debug("Cleanup...");
436 #ifdef HAVE_LIBATIADL
439 #ifdef HAVE_REMOTE_SUPPORT
443 psensor_list_free(ui->sensors);
446 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
447 ui_appindicator_cleanup();
452 pmutex_unlock(&ui->sensors_mutex);
456 log_debug("Cleanup done, closing log");
460 * Creates the list of sensors.
462 * 'url': remote psensor server url, null for local monitoring.
464 static struct psensor **create_sensors_list(const char *url)
466 struct psensor **sensors;
469 #ifdef HAVE_REMOTE_SUPPORT
471 sensors = get_remote_sensors(url, 600);
473 log_err(_("Psensor has not been compiled with remote "
478 sensors = malloc(sizeof(struct psensor *));
481 if (config_is_lmsensor_enabled())
482 lmsensor_psensor_list_append(&sensors, 600);
484 if (config_is_hddtemp_enabled())
485 hddtemp_psensor_list_append(&sensors, 600);
488 if (config_is_libatasmart_enabled())
489 atasmart_psensor_list_append(&sensors, 600);
493 if (config_is_nvctrl_enabled())
494 nvidia_psensor_list_append(&sensors, 600);
496 #ifdef HAVE_LIBATIADL
497 if (config_is_atiadlsdk_enabled())
498 amd_psensor_list_append(&sensors, 600);
501 if (config_is_gtop2_enabled())
502 gtop2_psensor_list_append(&sensors, 600);
504 #ifdef HAVE_LIBUDISKS2
505 if (config_is_udisks2_enabled())
506 udisks2_psensor_list_append(&sensors, 600);
510 associate_preferences(sensors);
511 associate_colors(sensors);
516 int main(int argc, char **argv)
518 struct ui_psensor ui;
520 int optc, cmdok, opti, new_instance, ret;
524 program_name = argv[0];
526 setlocale(LC_ALL, "");
529 bindtextdomain(PACKAGE, LOCALEDIR);
536 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
541 url = strdup(optarg);
550 log_level = atoi(optarg);
551 log_info(_("Enables debug mode."));
562 if (!cmdok || optind != argc) {
563 fprintf(stderr, _("Try `%s --help' for more information.\n"),
570 app = g_application_new("wpitchoune.psensor", 0);
572 g_application_register(app, NULL, NULL);
574 if (!new_instance && g_application_get_is_remote(app)) {
575 g_application_activate(app);
576 log_warn(_("A Psensor instance already exists."));
580 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
583 #if !(GLIB_CHECK_VERSION(2, 31, 0))
585 * Since GLib 2.31 g_thread_init call is deprecated and not
588 log_debug("Calling g_thread_init(NULL)");
592 #ifdef HAVE_APPINDICATOR_029
593 /* gdk_thread_enter/leave only used to workaround mutex bug
594 * of appindicator < 0.2.9, so do not call gdk_threads_init
595 * if useless. Calling this function leads to
596 * crash "Attempt to unlock mutex that was not locked" with
597 * GLib 2.41.2 (new checking) probably due to bugs in GTK
603 gtk_init(NULL, NULL);
605 pmutex_init(&ui.sensors_mutex);
607 ui.config = config_load();
609 ui.sensors = create_sensors_list(url);
610 associate_cb_alarm_raised(ui.sensors, &ui);
612 if (ui.config->slog_enabled)
616 config_get_slog_interval());
618 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
620 ui_status_set_visible(1);
624 ui_window_create(&ui);
626 ui_enable_alpha_channel(&ui);
628 ret = pthread_create(&thread, NULL, update_measures, &ui);
631 log_err(_("Failed to create thread for monitoring sensors"));
633 ui.graph_update_interval = ui.config->graph_update_interval;
635 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
637 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
638 ui_appindicator_init(&ui);
641 gdk_notify_startup_complete();
644 * hack, did not find a cleaner solution.
645 * wait 30s to ensure that the status icon is attempted to be
646 * drawn before determining whether the main window must be
649 if (ui.config->hide_on_startup)
650 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
652 initial_window_show(&ui);
654 log_debug("translators: %s\n", _("translator-credits"));
662 log_debug("Quitting...");