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 **ss, *s;
196 if (s->alarm_raised && config_get_sensor_alarm_enabled(s->id)) {
204 #if defined(HAVE_APPINDICATOR)
205 if (is_appindicator_supported())
206 ui_appindicator_update(ui, attention);
209 if (is_status_supported())
210 ui_status_update(ui, attention);
213 static gboolean ui_refresh_thread(gpointer data)
217 struct ui_psensor *ui = (struct ui_psensor *)data;
222 pmutex_lock(&ui->sensors_mutex);
224 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
226 ui_sensorlist_update(ui, 0);
228 if (is_appindicator_supported() || is_status_supported())
229 indicators_update(ui);
232 ui_unity_launcher_entry_update(ui->sensors,
233 !cfg->unity_launcher_count_disabled,
234 cfg->temperature_unit == CELSIUS);
237 if (ui->graph_update_interval != cfg->graph_update_interval) {
238 ui->graph_update_interval = cfg->graph_update_interval;
242 pmutex_unlock(&ui->sensors_mutex);
245 g_timeout_add(1000 * ui->graph_update_interval,
246 ui_refresh_thread, ui);
251 static void cb_alarm_raised(struct psensor *sensor, void *data)
253 if (config_get_sensor_alarm_enabled(sensor->id)) {
254 ui_notify(sensor, (struct ui_psensor *)data);
259 static void associate_colors(struct psensor **sensors)
262 /* copied from the default colors of the gtk color color
264 const char *default_colors[27] = {
265 "#ef2929", /* Scarlet Red */
266 "#fcaf3e", /* Orange */
267 "#fce94f", /* Butter */
268 "#8ae234", /* Chameleon */
269 "#729fcf", /* Sky Blue */
270 "#ad7fa8", /* Plum */
271 "#e9b96e", /* Chocolate */
272 "#888a85", /* Aluminum 1 */
273 "#eeeeec", /* Aluminum 2 */
294 struct psensor **cur;
297 for (cur = sensors, i = 0; *cur; cur++, i++) {
298 gdk_rgba_parse(&rgba, default_colors[i % 27]);
300 c.green = rgba.green;
303 (*cur)->color = config_get_sensor_color((*cur)->id, &c);
308 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
310 struct psensor **sensor_cur = sensors;
312 while (*sensor_cur) {
313 struct psensor *s = *sensor_cur;
315 s->cb_alarm_raised = cb_alarm_raised;
316 s->cb_alarm_raised_data = ui;
318 s->alarm_high_threshold
319 = config_get_sensor_alarm_high_threshold(s->id);
320 s->alarm_low_threshold
321 = config_get_sensor_alarm_low_threshold(s->id);
327 static void associate_preferences(struct psensor **sensors)
329 struct psensor **sensor_cur = sensors;
331 while (*sensor_cur) {
333 struct psensor *s = *sensor_cur;
335 s->graph_enabled = config_is_sensor_graph_enabled(s->id);
337 n = config_get_sensor_name(s->id);
348 static void log_init(void)
353 dir = get_psensor_user_dir();
358 path = malloc(strlen(dir)+1+strlen("log")+1);
359 sprintf(path, "%s/%s", dir, "log");
366 static struct option long_options[] = {
367 {"version", no_argument, NULL, 'v'},
368 {"help", no_argument, NULL, 'h'},
369 {"url", required_argument, NULL, 'u'},
370 {"debug", required_argument, NULL, 'd'},
371 {"new-instance", no_argument, NULL, 'n'},
375 static gboolean initial_window_show(gpointer data)
377 struct ui_psensor *ui;
379 log_debug("initial_window_show()");
381 ui = (struct ui_psensor *)data;
383 log_debug("is_status_supported: %d", is_status_supported());
384 log_debug("is_appindicator_supported: %d",
385 is_appindicator_supported());
386 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
388 if (!ui->config->hide_on_startup
389 || (!is_appindicator_supported() && !is_status_supported()))
392 ui_window_update(ui);
397 static void log_glib_info(void)
399 log_debug("Compiled with GLib %d.%d.%d",
404 log_debug("Running with GLib %d.%d.%d",
410 static void cb_activate(GApplication *application,
413 ui_window_show((struct ui_psensor *)data);
417 * Release memory for Valgrind.
419 static void cleanup(struct ui_psensor *ui)
421 pmutex_lock(&ui->sensors_mutex);
423 log_debug("Cleanup...");
428 #ifdef HAVE_LIBATIADL
431 #ifdef HAVE_REMOTE_SUPPORT
435 psensor_list_free(ui->sensors);
438 #if defined(HAVE_APPINDICATOR)
439 ui_appindicator_cleanup();
444 pmutex_unlock(&ui->sensors_mutex);
448 log_debug("Cleanup done, closing log");
452 * Creates the list of sensors.
454 * 'url': remote psensor server url, null for local monitoring.
456 static struct psensor **create_sensors_list(const char *url)
458 struct psensor **sensors;
461 #ifdef HAVE_REMOTE_SUPPORT
463 sensors = get_remote_sensors(url, 600);
465 log_err(_("Psensor has not been compiled with remote "
470 sensors = malloc(sizeof(struct psensor *));
473 if (config_is_lmsensor_enabled())
474 lmsensor_psensor_list_append(&sensors, 600);
476 if (config_is_hddtemp_enabled())
477 hddtemp_psensor_list_append(&sensors, 600);
480 if (config_is_libatasmart_enabled())
481 atasmart_psensor_list_append(&sensors, 600);
485 if (config_is_nvctrl_enabled())
486 nvidia_psensor_list_append(&sensors, 600);
488 #ifdef HAVE_LIBATIADL
489 if (config_is_atiadlsdk_enabled())
490 amd_psensor_list_append(&sensors, 600);
493 if (config_is_gtop2_enabled())
494 gtop2_psensor_list_append(&sensors, 600);
496 #ifdef HAVE_LIBUDISKS2
497 if (config_is_udisks2_enabled())
498 udisks2_psensor_list_append(&sensors, 600);
502 associate_preferences(sensors);
503 associate_colors(sensors);
508 int main(int argc, char **argv)
510 struct ui_psensor ui;
512 int optc, cmdok, opti, new_instance, ret;
516 program_name = argv[0];
518 setlocale(LC_ALL, "");
521 bindtextdomain(PACKAGE, LOCALEDIR);
528 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
533 url = strdup(optarg);
542 log_level = atoi(optarg);
543 log_info(_("Enables debug mode."));
554 if (!cmdok || optind != argc) {
555 fprintf(stderr, _("Try `%s --help' for more information.\n"),
562 app = g_application_new("wpitchoune.psensor", 0);
564 g_application_register(app, NULL, NULL);
566 if (!new_instance && g_application_get_is_remote(app)) {
567 g_application_activate(app);
568 log_warn(_("A Psensor instance already exists."));
572 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
575 #if !(GLIB_CHECK_VERSION(2, 31, 0))
577 * Since GLib 2.31 g_thread_init call is deprecated and not
580 log_debug("Calling g_thread_init(NULL)");
584 gtk_init(NULL, NULL);
586 pmutex_init(&ui.sensors_mutex);
588 ui.config = config_load();
590 ui.sensors = create_sensors_list(url);
591 associate_cb_alarm_raised(ui.sensors, &ui);
593 if (ui.config->slog_enabled)
597 config_get_slog_interval());
599 #if !defined(HAVE_APPINDICATOR)
601 ui_status_set_visible(1);
605 ui_window_create(&ui);
607 ui_enable_alpha_channel(&ui);
609 ret = pthread_create(&thread, NULL, update_measures, &ui);
612 log_err(_("Failed to create thread for monitoring sensors"));
614 ui.graph_update_interval = ui.config->graph_update_interval;
616 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
618 #if defined(HAVE_APPINDICATOR)
619 ui_appindicator_init(&ui);
622 gdk_notify_startup_complete();
625 * hack, did not find a cleaner solution.
626 * wait 30s to ensure that the status icon is attempted to be
627 * drawn before determining whether the main window must be
630 if (ui.config->hide_on_startup)
631 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
633 initial_window_show(&ui);
635 log_debug("translators: %s\n", _("translator-credits"));
643 log_debug("Quitting...");