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>
37 #include "ui_sensorlist.h"
40 #include "notify_cmd.h"
45 #include "ui_status.h"
59 #ifdef HAVE_REMOTE_SUPPORT
63 #include "ui_appindicator.h"
66 #include "ui_notify.h"
73 static const char *program_name;
75 static void print_version()
77 printf("psensor %s\n", VERSION);
78 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
79 "License GPLv2: GNU GPL version 2 or later "
80 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
81 "This is free software: you are free to change and"
83 "There is NO WARRANTY, to the extent permitted by law.\n"),
87 static void print_help()
89 printf(_("Usage: %s [OPTION]...\n"), program_name);
91 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
92 "including temperatures and fan speeds."));
96 puts(_(" -h, --help display this help and exit\n"
97 " -v, --version display version information and exit"));
102 " -u, --url=URL the URL of the psensor-server,\n"
103 " example: http://hostname:3131"));
105 " --use-libatasmart use atasmart library for disk monitoring instead of\n"
108 " -n, --new-instance force the creation of a new Psensor application"));
111 puts(_(" -d, --debug=LEVEL "
112 "set the debug level, integer between 0 and 3"));
116 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
118 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
122 * Updates the size of the sensor values if different than the
126 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
128 struct psensor **cur, *s;
130 for (cur = sensors; *cur; cur++) {
133 if (s->values_max_length != cfg->sensor_values_max_length)
134 psensor_values_resize(s,
135 cfg->sensor_values_max_length);
139 static void *update_measures(void *data)
141 struct psensor **sensors;
144 struct ui_psensor *ui;
146 ui = (struct ui_psensor *)data;
150 pmutex_lock(&ui->sensors_mutex);
152 sensors = ui->sensors;
156 update_psensor_values_size(sensors, cfg);
158 psensor_list_update_measures(sensors);
159 #ifdef HAVE_REMOTE_SUPPORT
160 remote_psensor_list_update(sensors);
163 nvidia_psensor_list_update(sensors);
165 #ifdef HAVE_LIBATIADL
166 amd_psensor_list_update(sensors);
169 psensor_log_measures(sensors);
171 period = cfg->sensor_update_interval;
173 pmutex_unlock(&ui->sensors_mutex);
179 static void indicators_update(struct ui_psensor *ui)
181 struct psensor **sensor_cur = ui->sensors;
182 unsigned int attention = 0;
184 while (*sensor_cur) {
185 struct psensor *s = *sensor_cur;
187 if (s->alarm_enabled && s->alarm_raised) {
195 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
196 if (is_appindicator_supported())
197 ui_appindicator_update(ui, attention);
200 if (is_status_supported())
201 ui_status_update(ui, attention);
204 gboolean ui_refresh_thread(gpointer data)
208 struct ui_psensor *ui = (struct ui_psensor *)data;
213 pmutex_lock(&ui->sensors_mutex);
215 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
217 ui_sensorlist_update(ui, 0);
219 if (is_appindicator_supported() || is_status_supported())
220 indicators_update(ui);
223 ui_unity_launcher_entry_update(ui->sensors,
224 !cfg->unity_launcher_count_disabled,
225 cfg->temperature_unit == CELSIUS);
228 if (ui->graph_update_interval != cfg->graph_update_interval) {
229 ui->graph_update_interval = cfg->graph_update_interval;
233 pmutex_unlock(&ui->sensors_mutex);
236 g_timeout_add(1000 * ui->graph_update_interval,
237 ui_refresh_thread, ui);
242 static void cb_alarm_raised(struct psensor *sensor, void *data)
244 #ifdef HAVE_LIBNOTIFY
245 if (sensor->alarm_enabled)
246 ui_notify(sensor, (struct ui_psensor *)data);
252 static void associate_colors(struct psensor **sensors)
254 /* number of uniq colors */
255 #define COLORS_COUNT 8
257 double colors[COLORS_COUNT][3] = {
258 {0, 0, 0}, /* black */
260 {0, 0, 1}, /* blue */
261 {0, 1, 0}, /* green */
263 {0.5, 0.5, 0.5},/* grey */
264 {0.5, 0, 0}, /* dark red */
265 {0, 0, 0.5}, /* dark blue */
266 {0, 0.5, 0} /* dark green */
268 struct psensor **cur;
272 for (cur = sensors, i = 0; *cur; cur++, i++) {
274 colors[i % COLORS_COUNT][0],
275 colors[i % COLORS_COUNT][1],
276 colors[i % COLORS_COUNT][2]);
278 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
283 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
285 struct psensor **sensor_cur = sensors;
286 while (*sensor_cur) {
287 struct psensor *s = *sensor_cur;
289 s->cb_alarm_raised = cb_alarm_raised;
290 s->cb_alarm_raised_data = ui;
292 s->alarm_high_threshold
293 = config_get_sensor_alarm_high_threshold(s->id);
294 s->alarm_low_threshold
295 = config_get_sensor_alarm_low_threshold(s->id);
298 = config_get_sensor_alarm_enabled(s->id);
304 static void associate_preferences(struct psensor **sensors)
306 struct psensor **sensor_cur = sensors;
307 while (*sensor_cur) {
309 struct psensor *s = *sensor_cur;
311 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
313 n = config_get_sensor_name(s->id);
320 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
326 static void log_init()
331 dir = get_psensor_user_dir();
336 path = malloc(strlen(dir)+1+strlen("log")+1);
337 sprintf(path, "%s/%s", dir, "log");
344 static struct option long_options[] = {
345 {"use-libatasmart", no_argument, 0, 0},
346 {"version", no_argument, 0, 'v'},
347 {"help", no_argument, 0, 'h'},
348 {"url", required_argument, 0, 'u'},
349 {"debug", required_argument, 0, 'd'},
350 {"new-instance", no_argument, 0, 'n'},
354 static gboolean initial_window_show(gpointer data)
356 struct ui_psensor *ui;
358 log_debug("initial_window_show()");
360 ui = (struct ui_psensor *)data;
362 log_debug("is_status_supported: %d", is_status_supported());
363 log_debug("is_appindicator_supported: %d",
364 is_appindicator_supported());
365 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
367 if (!ui->config->hide_on_startup
368 || (!is_appindicator_supported() && !is_status_supported()))
371 ui_window_update(ui);
376 static void log_glib_info()
378 log_debug("Compiled with GLib %d.%d.%d",
383 log_debug("Running with GLib %d.%d.%d",
389 static void cb_activate(GApplication *application,
392 ui_window_show((struct ui_psensor *)data);
396 * Release memory for Valgrind.
398 static void cleanup(struct ui_psensor *ui)
400 pmutex_lock(&ui->sensors_mutex);
402 log_debug("Cleanup...");
409 #ifdef HAVE_LIBATIADL
412 #ifdef HAVE_REMOTE_SUPPORT
416 psensor_list_free(ui->sensors);
419 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
420 ui_appindicator_cleanup();
425 pmutex_unlock(&ui->sensors_mutex);
429 log_debug("Cleanup done, closing log");
433 * Creates the list of sensors.
435 * 'url': remote psensor server url, null for local monitoring.
436 * 'use_libatasmart': whether the libatasmart must be used.
438 static struct psensor **create_sensors_list(const char *url,
439 unsigned int use_libatasmart)
441 struct psensor **sensors;
444 #ifdef HAVE_REMOTE_SUPPORT
446 sensors = get_remote_sensors(url, 600);
448 log_err(_("Psensor has not been compiled with remote "
453 sensors = get_all_sensors(use_libatasmart, 600);
455 sensors = nvidia_psensor_list_add(sensors, 600);
457 #ifdef HAVE_LIBATIADL
458 sensors = amd_psensor_list_add(sensors, 600);
461 sensors = cpu_psensor_list_add(sensors, 600);
465 associate_preferences(sensors);
466 associate_colors(sensors);
471 int main(int argc, char **argv)
473 struct ui_psensor ui;
475 int optc, cmdok, opti, use_libatasmart, new_instance, ret;
479 program_name = argv[0];
481 setlocale(LC_ALL, "");
484 bindtextdomain(PACKAGE, LOCALEDIR);
488 use_libatasmart = new_instance = 0;
491 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
495 if (!strcmp(long_options[opti].name, "use-libatasmart"))
500 url = strdup(optarg);
509 log_level = atoi(optarg);
510 log_info(_("Enables debug mode."));
521 if (!cmdok || optind != argc) {
522 fprintf(stderr, _("Try `%s --help' for more information.\n"),
529 app = g_application_new("wpitchoune.psensor", 0);
531 g_application_register(app, NULL, NULL);
533 if (!new_instance && g_application_get_is_remote(app)) {
534 g_application_activate(app);
535 log_warn(_("A Psensor instance already exists."));
539 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
542 #if !(GLIB_CHECK_VERSION(2, 31, 0))
544 * Since GLib 2.31 g_thread_init call is deprecated and not
547 log_debug("Calling g_thread_init(NULL)");
551 #ifdef HAVE_APPINDICATOR_029
552 /* gdk_thread_enter/leave only used to workaround mutex bug
553 * of appindicator < 0.2.9, so do not call gdk_threads_init
554 * if useless. Calling this function leads to
555 * crash "Attempt to unlock mutex that was not locked" with
556 * GLib 2.41.2 (new checking) probably due to bugs in GTK
562 gtk_init(NULL, NULL);
564 pmutex_init(&ui.sensors_mutex);
566 ui.config = config_load();
570 ui.sensors = create_sensors_list(url, use_libatasmart);
571 associate_cb_alarm_raised(ui.sensors, &ui);
573 if (ui.config->slog_enabled)
577 config_get_slog_interval());
579 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
581 ui_status_set_visible(1);
585 ui_window_create(&ui);
587 ui_enable_alpha_channel(&ui);
589 ret = pthread_create(&thread, NULL, update_measures, &ui);
592 log_err(_("Failed to create thread for monitoring sensors"));
594 ui.graph_update_interval = ui.config->graph_update_interval;
596 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
598 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
599 ui_appindicator_init(&ui);
602 gdk_notify_startup_complete();
605 * hack, did not find a cleaner solution.
606 * wait 30s to ensure that the status icon is attempted to be
607 * drawn before determining whether the main window must be
610 if (ui.config->hide_on_startup)
611 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
613 initial_window_show(&ui);
615 log_debug("translators: %s\n", _("translator-credits"));
623 log_debug("Quitting...");