2 * Copyright (C) 2010-2013 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"
43 #include "ui_status.h"
57 #ifdef HAVE_REMOTE_SUPPORT
61 #include "ui_appindicator.h"
64 #include "ui_notify.h"
71 static const char *program_name;
73 static void print_version()
75 printf("psensor %s\n", VERSION);
76 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
77 "License GPLv2: GNU GPL version 2 or later "
78 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
79 "This is free software: you are free to change and "
81 "There is NO WARRANTY, to the extent permitted by law.\n"),
85 static void print_help()
87 printf(_("Usage: %s [OPTION]...\n"), program_name);
89 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
90 "including temperatures and fan speeds."));
94 puts(_(" -h, --help display this help and exit\n"
95 " -v, --version display version information and exit"));
100 " -u, --url=URL the URL of the psensor-server,\n"
101 " example: http://hostname:3131"));
103 " --use-libatasmart use atasmart library for disk monitoring instead of\n"
106 " -n, --new-instance force the creation of a new Psensor application"));
109 puts(_(" -d, --debug=LEVEL "
110 "set the debug level, integer between 0 and 3"));
114 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
116 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
120 * Updates the size of the sensor values if different than the
124 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
126 struct psensor **cur, *s;
128 for (cur = sensors; *cur; cur++) {
131 if (s->values_max_length != cfg->sensor_values_max_length)
132 psensor_values_resize(s,
133 cfg->sensor_values_max_length);
137 static void update_measures(struct ui_psensor *ui)
139 struct psensor **sensors;
146 pthread_mutex_lock(&ui->sensors_mutex);
148 sensors = ui->sensors;
152 update_psensor_values_size(sensors, cfg);
154 psensor_list_update_measures(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);
165 psensor_log_measures(sensors);
167 period = cfg->sensor_update_interval;
169 pthread_mutex_unlock(&ui->sensors_mutex);
175 static void indicators_update(struct ui_psensor *ui)
177 struct psensor **sensor_cur = ui->sensors;
178 unsigned int attention = 0;
180 while (*sensor_cur) {
181 struct psensor *s = *sensor_cur;
183 if (s->alarm_enabled && s->alarm_raised) {
191 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
192 if (is_appindicator_supported())
193 ui_appindicator_update(ui, attention);
196 if (is_status_supported())
197 ui_status_update(ui, attention);
200 gboolean ui_refresh_thread(gpointer data)
204 struct ui_psensor *ui = (struct ui_psensor *)data;
209 pthread_mutex_lock(&ui->sensors_mutex);
211 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
213 ui_sensorlist_update(ui, 0);
215 if (is_appindicator_supported() || is_status_supported())
216 indicators_update(ui);
219 ui_unity_launcher_entry_update(ui->sensors,
220 !cfg->unity_launcher_count_disabled,
221 cfg->temperature_unit == CELCIUS);
224 if (ui->graph_update_interval != cfg->graph_update_interval) {
225 ui->graph_update_interval = cfg->graph_update_interval;
229 pthread_mutex_unlock(&ui->sensors_mutex);
232 g_timeout_add(1000 * ui->graph_update_interval,
233 ui_refresh_thread, ui);
238 static void cb_alarm_raised(struct psensor *sensor, void *data)
240 #ifdef HAVE_LIBNOTIFY
241 if (sensor->alarm_enabled)
242 ui_notify(sensor, (struct ui_psensor *)data);
246 static void associate_colors(struct psensor **sensors)
248 /* number of uniq colors */
249 #define COLORS_COUNT 8
251 unsigned int colors[COLORS_COUNT][3] = {
252 {0x0000, 0x0000, 0x0000}, /* black */
253 {0xffff, 0x0000, 0x0000}, /* red */
254 {0x0000, 0x0000, 0xffff}, /* blue */
255 {0x0000, 0xffff, 0x0000}, /* green */
257 {0x7fff, 0x7fff, 0x7fff}, /* grey */
258 {0x7fff, 0x0000, 0x0000}, /* dark red */
259 {0x0000, 0x0000, 0x7fff}, /* dark blue */
260 {0x0000, 0x7fff, 0x0000} /* dark green */
262 struct psensor **cur;
266 for (cur = sensors, i = 0; *cur; cur++) {
268 colors[i % COLORS_COUNT][0],
269 colors[i % COLORS_COUNT][1],
270 colors[i % COLORS_COUNT][2]);
272 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
277 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
279 struct psensor **sensor_cur = sensors;
280 while (*sensor_cur) {
281 struct psensor *s = *sensor_cur;
283 s->cb_alarm_raised = cb_alarm_raised;
284 s->cb_alarm_raised_data = ui;
286 s->alarm_high_threshold
287 = config_get_sensor_alarm_high_threshold(s->id);
288 s->alarm_low_threshold
289 = config_get_sensor_alarm_low_threshold(s->id);
291 if (is_temp_type(s->type) || is_fan_type(s->type)) {
293 = config_get_sensor_alarm_enabled(s->id);
295 s->alarm_high_threshold = 0;
296 s->alarm_enabled = 0;
303 static void associate_preferences(struct psensor **sensors)
305 struct psensor **sensor_cur = sensors;
306 while (*sensor_cur) {
308 struct psensor *s = *sensor_cur;
310 s->graph_enabled = config_is_sensor_enabled(s->id);
312 n = config_get_sensor_name(s->id);
319 s->appindicator_enabled = config_is_appindicator_enabled(s->id);
325 static void log_init()
327 char *home, *path, *dir;
329 home = getenv("HOME");
334 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
335 sprintf(dir, "%s/%s", home, ".psensor");
338 path = malloc(strlen(dir)+1+strlen("log")+1);
339 sprintf(path, "%s/%s", dir, "log");
347 static struct option long_options[] = {
348 {"use-libatasmart", no_argument, 0, 0},
349 {"version", no_argument, 0, 'v'},
350 {"help", no_argument, 0, 'h'},
351 {"url", required_argument, 0, 'u'},
352 {"debug", required_argument, 0, 'd'},
353 {"new-instance", no_argument, 0, 'n'},
357 static gboolean initial_window_show(gpointer data)
359 struct ui_psensor *ui;
361 log_debug("initial_window_show()");
363 ui = (struct ui_psensor *)data;
365 log_debug("is_status_supported: %d", is_status_supported());
366 log_debug("is_appindicator_supported: %d",
367 is_appindicator_supported());
368 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
370 if (!ui->config->hide_on_startup
371 || (!is_appindicator_supported() && !is_status_supported()))
374 ui_window_update(ui);
379 static void log_glib_info()
381 log_debug("Compiled with GLib %d.%d.%d",
386 log_debug("Running with GLib %d.%d.%d",
392 static void cb_activate(GApplication *application,
395 ui_window_show((struct ui_psensor *)data);
399 * Release memory for Valgrind.
401 static void cleanup(struct ui_psensor *ui)
403 pthread_mutex_lock(&ui->sensors_mutex);
405 log_debug("Cleanup...");
412 #ifdef HAVE_LIBATIADL
415 #ifdef HAVE_REMOTE_SUPPORT
419 psensor_list_free(ui->sensors);
422 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
423 ui_appindicator_cleanup();
428 pthread_mutex_unlock(&ui->sensors_mutex);
432 log_debug("Cleanup done, closing log");
436 * Creates the list of sensors.
438 * 'url': remote psensor server url, null for local monitoring.
439 * 'use_libatasmart': whether the libatasmart must be used.
441 static struct psensor **create_sensors_list(const char *url,
442 unsigned int use_libatasmart)
444 struct psensor **sensors;
447 #ifdef HAVE_REMOTE_SUPPORT
449 sensors = get_remote_sensors(url, 600);
451 log_err(_("Psensor has not been compiled with remote "
456 sensors = get_all_sensors(use_libatasmart, 600);
458 sensors = nvidia_psensor_list_add(sensors, 600);
460 #ifdef HAVE_LIBATIADL
461 sensors = amd_psensor_list_add(sensors, 600);
464 sensors = cpu_psensor_list_add(sensors, 600);
468 associate_preferences(sensors);
469 associate_colors(sensors);
474 int main(int argc, char **argv)
476 struct ui_psensor ui;
479 int optc, cmdok, opti, use_libatasmart, new_instance;
483 program_name = argv[0];
485 setlocale(LC_ALL, "");
488 bindtextdomain(PACKAGE, LOCALEDIR);
492 use_libatasmart = new_instance = 0;
495 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
499 if (!strcmp(long_options[opti].name, "use-libatasmart"))
504 url = strdup(optarg);
513 log_level = atoi(optarg);
514 log_info(_("Enables debug mode."));
525 if (!cmdok || optind != argc) {
526 fprintf(stderr, _("Try `%s --help' for more information.\n"),
533 app = g_application_new("wpitchoune.psensor", 0);
535 g_application_register(app, NULL, NULL);
537 if (!new_instance && g_application_get_is_remote(app)) {
538 g_application_activate(app);
539 log_warn(_("A Psensor instance already exists."));
543 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
546 #if !(GLIB_CHECK_VERSION(2, 31, 0))
548 * Since GLib 2.31 g_thread_init call is deprecated and not
551 log_debug("Calling g_thread_init(NULL)");
557 gtk_init(NULL, NULL);
559 pthread_mutex_init(&ui.sensors_mutex, NULL);
561 ui.config = config_load();
565 ui.sensors = create_sensors_list(url, use_libatasmart);
566 associate_cb_alarm_raised(ui.sensors, &ui);
568 if (ui.config->slog_enabled)
572 config_get_slog_interval());
574 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
576 ui_status_set_visible(1);
580 ui_window_create(&ui);
582 ui_enable_alpha_channel(&ui);
584 thread = g_thread_create((GThreadFunc) update_measures,
590 ui.graph_update_interval = ui.config->graph_update_interval;
592 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
594 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
595 ui_appindicator_init(&ui);
598 gdk_notify_startup_complete();
601 * hack, did not find a cleaner solution.
602 * wait 30s to ensure that the status icon is attempted to be
603 * drawn before determining whether the main window must be
606 if (ui.config->hide_on_startup)
607 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
609 initial_window_show(&ui);
617 log_debug("Quitting...");