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...");
438 #ifdef HAVE_LIBATIADL
441 #ifdef HAVE_REMOTE_SUPPORT
445 psensor_list_free(ui->sensors);
448 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
449 ui_appindicator_cleanup();
454 pmutex_unlock(&ui->sensors_mutex);
458 log_debug("Cleanup done, closing log");
462 * Creates the list of sensors.
464 * 'url': remote psensor server url, null for local monitoring.
466 static struct psensor **create_sensors_list(const char *url)
468 struct psensor **sensors;
471 #ifdef HAVE_REMOTE_SUPPORT
473 sensors = get_remote_sensors(url, 600);
475 log_err(_("Psensor has not been compiled with remote "
480 sensors = malloc(sizeof(struct psensor *));
483 if (config_is_lmsensor_enabled())
484 lmsensor_psensor_list_append(&sensors, 600);
486 if (config_is_hddtemp_enabled())
487 hddtemp_psensor_list_append(&sensors, 600);
490 if (config_is_libatasmart_enabled())
491 atasmart_psensor_list_append(&sensors, 600);
495 if (config_is_nvctrl_enabled())
496 nvidia_psensor_list_append(&sensors, 600);
498 #ifdef HAVE_LIBATIADL
499 if (config_is_atiadlsdk_enabled())
500 amd_psensor_list_append(&sensors, 600);
503 if (config_is_gtop2_enabled())
504 gtop2_psensor_list_append(&sensors, 600);
506 #ifdef HAVE_LIBUDISKS2
507 if (config_is_udisks2_enabled())
508 udisks2_psensor_list_append(&sensors, 600);
512 associate_preferences(sensors);
513 associate_colors(sensors);
518 int main(int argc, char **argv)
520 struct ui_psensor ui;
522 int optc, cmdok, opti, new_instance, ret;
526 program_name = argv[0];
528 setlocale(LC_ALL, "");
531 bindtextdomain(PACKAGE, LOCALEDIR);
538 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
543 url = strdup(optarg);
552 log_level = atoi(optarg);
553 log_info(_("Enables debug mode."));
564 if (!cmdok || optind != argc) {
565 fprintf(stderr, _("Try `%s --help' for more information.\n"),
572 app = g_application_new("wpitchoune.psensor", 0);
574 g_application_register(app, NULL, NULL);
576 if (!new_instance && g_application_get_is_remote(app)) {
577 g_application_activate(app);
578 log_warn(_("A Psensor instance already exists."));
582 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
585 #if !(GLIB_CHECK_VERSION(2, 31, 0))
587 * Since GLib 2.31 g_thread_init call is deprecated and not
590 log_debug("Calling g_thread_init(NULL)");
594 #ifdef HAVE_APPINDICATOR_029
595 /* gdk_thread_enter/leave only used to workaround mutex bug
596 * of appindicator < 0.2.9, so do not call gdk_threads_init
597 * if useless. Calling this function leads to
598 * crash "Attempt to unlock mutex that was not locked" with
599 * GLib 2.41.2 (new checking) probably due to bugs in GTK
605 gtk_init(NULL, NULL);
607 pmutex_init(&ui.sensors_mutex);
609 ui.config = config_load();
613 ui.sensors = create_sensors_list(url);
614 associate_cb_alarm_raised(ui.sensors, &ui);
616 if (ui.config->slog_enabled)
620 config_get_slog_interval());
622 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
624 ui_status_set_visible(1);
628 ui_window_create(&ui);
630 ui_enable_alpha_channel(&ui);
632 ret = pthread_create(&thread, NULL, update_measures, &ui);
635 log_err(_("Failed to create thread for monitoring sensors"));
637 ui.graph_update_interval = ui.config->graph_update_interval;
639 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
641 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
642 ui_appindicator_init(&ui);
645 gdk_notify_startup_complete();
648 * hack, did not find a cleaner solution.
649 * wait 30s to ensure that the status icon is attempted to be
650 * drawn before determining whether the main window must be
653 if (ui.config->hide_on_startup)
654 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
656 initial_window_show(&ui);
658 log_debug("translators: %s\n", _("translator-credits"));
666 log_debug("Quitting...");