2 * Copyright (C) 2010-2017 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>
39 #include <notify_cmd.h>
48 #include <ui_appindicator.h>
51 #include <ui_notify.h>
53 #include <ui_sensorlist.h>
54 #include <ui_status.h>
57 static const char *program_name;
59 static void print_version(void)
61 printf("psensor %s\n", VERSION);
62 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
63 "License GPLv2: GNU GPL version 2 or later "
64 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
65 "This is free software: you are free to change and"
67 "There is NO WARRANTY, to the extent permitted by law.\n"),
71 static void print_help(void)
73 printf(_("Usage: %s [OPTION]...\n"), program_name);
75 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
76 "including temperatures and fan speeds."));
80 puts(_(" -h, --help display this help and exit\n"
81 " -v, --version display version information and exit"));
86 " -u, --url=URL the URL of the psensor-server,\n"
87 " example: http://hostname:3131"));
89 " -n, --new-instance force the creation of a new Psensor application"));
92 puts(_(" -d, --debug=LEVEL "
93 "set the debug level, integer between 0 and 3"));
97 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
99 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
103 * Updates the size of the sensor values if different than the
107 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
109 struct psensor **cur, *s;
111 for (cur = sensors; *cur; cur++) {
114 if (s->values_max_length != cfg->sensor_values_max_length)
115 psensor_values_resize(s,
116 cfg->sensor_values_max_length);
120 static void *update_measures(void *data)
122 struct psensor **sensors;
125 struct ui_psensor *ui;
127 ui = (struct ui_psensor *)data;
131 pmutex_lock(&ui->sensors_mutex);
133 sensors = ui->sensors;
137 update_psensor_values_size(sensors, cfg);
139 lmsensor_psensor_list_update(sensors);
141 remote_psensor_list_update(sensors);
142 nvidia_psensor_list_update(sensors);
143 amd_psensor_list_update(sensors);
144 udisks2_psensor_list_update(sensors);
145 gtop2_psensor_list_update(sensors);
146 atasmart_psensor_list_update(sensors);
147 hddtemp_psensor_list_update(sensors);
148 bcm2835_psensor_list_update(sensors);
150 psensor_log_measures(sensors);
152 period = cfg->sensor_update_interval;
154 pmutex_unlock(&ui->sensors_mutex);
160 static void indicators_update(struct ui_psensor *ui)
162 struct psensor **ss, *s;
170 if (s->alarm_raised && config_get_sensor_alarm_enabled(s->id)) {
178 if (is_appindicator_supported())
179 ui_appindicator_update(ui, attention);
181 if (is_status_supported())
182 ui_status_update(ui, attention);
185 static gboolean ui_refresh_thread(gpointer data)
189 struct ui_psensor *ui = (struct ui_psensor *)data;
194 pmutex_lock(&ui->sensors_mutex);
196 graph_update(ui->sensors, ui_get_graph(), ui->config, ui->main_window);
198 ui_sensorlist_update(ui, 0);
200 if (is_appindicator_supported() || is_status_supported())
201 indicators_update(ui);
203 ui_unity_launcher_entry_update(ui->sensors);
205 if (ui->graph_update_interval != cfg->graph_update_interval) {
206 ui->graph_update_interval = cfg->graph_update_interval;
210 pmutex_unlock(&ui->sensors_mutex);
213 g_timeout_add(1000 * ui->graph_update_interval,
214 ui_refresh_thread, ui);
219 static void cb_alarm_raised(struct psensor *sensor, void *data)
221 if (config_get_sensor_alarm_enabled(sensor->id)) {
222 ui_notify(sensor, (struct ui_psensor *)data);
228 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
232 double high_temp, high_default, low_default;
234 high_temp = config_get_default_high_threshold_temperature();
239 s->cb_alarm_raised = cb_alarm_raised;
240 s->cb_alarm_raised_data = ui;
242 if (s->max == UNKNOWN_DBL_VALUE) {
243 if (s->type & SENSOR_TYPE_TEMP)
244 high_default = high_temp;
246 high_default = UNKNOWN_DBL_VALUE;
248 high_default = s->max;
251 ret = config_get_sensor_alarm_high_threshold
252 (s->id, &s->alarm_high_threshold, high_default);
255 s->alarm_high_threshold = high_default;
257 if (s->min == UNKNOWN_DBL_VALUE)
260 low_default = s->min;
262 ret = config_get_sensor_alarm_low_threshold
263 (s->id, &s->alarm_low_threshold, low_default);
266 s->alarm_low_threshold = low_default;
272 static void associate_preferences(struct psensor **sensors)
274 struct psensor **sensor_cur = sensors;
276 while (*sensor_cur) {
278 struct psensor *s = *sensor_cur;
280 n = config_get_sensor_name(s->id);
291 static void log_init(void)
296 dir = get_psensor_user_dir();
301 path = malloc(strlen(dir)+1+strlen("log")+1);
302 sprintf(path, "%s/%s", dir, "log");
309 static struct option long_options[] = {
310 {"version", no_argument, NULL, 'v'},
311 {"help", no_argument, NULL, 'h'},
312 {"url", required_argument, NULL, 'u'},
313 {"debug", required_argument, NULL, 'd'},
314 {"new-instance", no_argument, NULL, 'n'},
319 * show the main window if there will be no way for the user to show
320 * it (no appindicator, no status icon).
322 static gboolean check_ui_visible(gpointer data)
324 struct ui_psensor *ui;
326 log_debug("check_ui_visible()");
328 ui = (struct ui_psensor *)data;
330 log_debug("is_status_supported: %d", is_status_supported());
331 log_debug("is_appindicator_supported: %d",
332 is_appindicator_supported());
334 if (!is_appindicator_supported() && !is_status_supported())
340 static void log_glib_info(void)
342 log_debug("Compiled with GLib %d.%d.%d",
347 log_debug("Running with GLib %d.%d.%d",
353 static void cb_activate(GApplication *application,
356 ui_window_show((struct ui_psensor *)data);
360 * Release memory for Valgrind.
362 static void cleanup(struct ui_psensor *ui)
364 pmutex_lock(&ui->sensors_mutex);
366 log_debug("Cleanup...");
372 psensor_list_free(ui->sensors);
375 ui_appindicator_cleanup();
379 pmutex_unlock(&ui->sensors_mutex);
383 log_debug("Cleanup done, closing log");
387 * Creates the list of sensors.
389 * 'url': remote psensor server url, null for local monitoring.
391 static struct psensor **create_sensors_list(const char *url)
393 struct psensor **sensors;
396 if (rsensor_is_supported()) {
398 sensors = get_remote_sensors(url, 600);
400 log_err(_("Psensor has not been compiled with remote "
405 sensors = malloc(sizeof(struct psensor *));
408 if (config_is_lmsensor_enabled())
409 lmsensor_psensor_list_append(&sensors, 600);
411 if (config_is_hddtemp_enabled())
412 hddtemp_psensor_list_append(&sensors, 600);
414 if (config_is_libatasmart_enabled())
415 atasmart_psensor_list_append(&sensors, 600);
417 if (config_is_nvctrl_enabled())
418 nvidia_psensor_list_append(&sensors, 600);
420 if (config_is_atiadlsdk_enabled())
421 amd_psensor_list_append(&sensors, 600);
423 if (config_is_gtop2_enabled())
424 gtop2_psensor_list_append(&sensors, 600);
426 if (config_is_udisks2_enabled())
427 udisks2_psensor_list_append(&sensors, 600);
429 bcm2835_psensor_list_append(&sensors, 600);
432 associate_preferences(sensors);
437 int main(int argc, char **argv)
439 struct ui_psensor ui;
441 int optc, cmdok, opti, new_instance, ret;
445 program_name = argv[0];
447 setlocale(LC_ALL, "");
450 bindtextdomain(PACKAGE, LOCALEDIR);
457 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
462 url = strdup(optarg);
471 log_level = atoi(optarg);
472 log_info(_("Enables debug mode."));
483 if (!cmdok || optind != argc) {
484 fprintf(stderr, _("Try `%s --help' for more information.\n"),
491 app = g_application_new("wpitchoune.psensor", 0);
493 g_application_register(app, NULL, NULL);
495 if (!new_instance && g_application_get_is_remote(app)) {
496 g_application_activate(app);
497 log_warn(_("A Psensor instance already exists."));
501 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
504 #if !(GLIB_CHECK_VERSION(2, 31, 0))
506 * Since GLib 2.31 g_thread_init call is deprecated and not
509 log_debug("Calling g_thread_init(NULL)");
513 gtk_init(NULL, NULL);
515 pmutex_init(&ui.sensors_mutex);
517 ui.config = config_load();
519 ui.sensors = create_sensors_list(url);
520 associate_cb_alarm_raised(ui.sensors, &ui);
522 if (ui.config->slog_enabled)
526 config_get_slog_interval());
529 ui_status_set_visible(1);
532 ui_window_create(&ui);
534 ui_enable_alpha_channel(&ui);
536 ret = pthread_create(&thread, NULL, update_measures, &ui);
539 log_err(_("Failed to create thread for monitoring sensors"));
541 ui.graph_update_interval = ui.config->graph_update_interval;
543 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
545 ui_appindicator_init(&ui);
548 gdk_notify_startup_complete();
550 log_debug("hide_on_startup: %d", ui.config->hide_on_startup);
551 if (ui.config->hide_on_startup)
552 /* Wait 30s to allow attempt to show status icon or other UI,
553 * then check whether any UI is accessible. If not, then main
554 * window must be shown despite hide_on_startup preference.
555 * There may be a cleaner way to deal with this.
557 g_timeout_add(30000, (GSourceFunc)check_ui_visible, &ui);
567 log_debug("Quitting...");