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 <notify_cmd.h>
47 #include <ui_appindicator.h>
50 #include <ui_notify.h>
52 #include <ui_sensorlist.h>
53 #include <ui_status.h>
56 static const char *program_name;
58 static void print_version(void)
60 printf("psensor %s\n", VERSION);
61 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
62 "License GPLv2: GNU GPL version 2 or later "
63 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
64 "This is free software: you are free to change and"
66 "There is NO WARRANTY, to the extent permitted by law.\n"),
70 static void print_help(void)
72 printf(_("Usage: %s [OPTION]...\n"), program_name);
74 puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
75 "including temperatures and fan speeds."));
79 puts(_(" -h, --help display this help and exit\n"
80 " -v, --version display version information and exit"));
85 " -u, --url=URL the URL of the psensor-server,\n"
86 " example: http://hostname:3131"));
88 " -n, --new-instance force the creation of a new Psensor application"));
91 puts(_(" -d, --debug=LEVEL "
92 "set the debug level, integer between 0 and 3"));
96 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
98 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
102 * Updates the size of the sensor values if different than the
106 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
108 struct psensor **cur, *s;
110 for (cur = sensors; *cur; cur++) {
113 if (s->values_max_length != cfg->sensor_values_max_length)
114 psensor_values_resize(s,
115 cfg->sensor_values_max_length);
119 static void *update_measures(void *data)
121 struct psensor **sensors;
124 struct ui_psensor *ui;
126 ui = (struct ui_psensor *)data;
130 pmutex_lock(&ui->sensors_mutex);
132 sensors = ui->sensors;
136 update_psensor_values_size(sensors, cfg);
138 lmsensor_psensor_list_update(sensors);
140 remote_psensor_list_update(sensors);
141 nvidia_psensor_list_update(sensors);
142 amd_psensor_list_update(sensors);
143 udisks2_psensor_list_update(sensors);
144 gtop2_psensor_list_update(sensors);
145 atasmart_psensor_list_update(sensors);
146 hddtemp_psensor_list_update(sensors);
148 psensor_log_measures(sensors);
150 period = cfg->sensor_update_interval;
152 pmutex_unlock(&ui->sensors_mutex);
158 static void indicators_update(struct ui_psensor *ui)
160 struct psensor **ss, *s;
168 if (s->alarm_raised && config_get_sensor_alarm_enabled(s->id)) {
176 if (is_appindicator_supported())
177 ui_appindicator_update(ui, attention);
179 if (is_status_supported())
180 ui_status_update(ui, attention);
183 static gboolean ui_refresh_thread(gpointer data)
187 struct ui_psensor *ui = (struct ui_psensor *)data;
193 pmutex_lock(&ui->sensors_mutex);
195 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
197 ui_sensorlist_update(ui, 0);
199 if (is_appindicator_supported() || is_status_supported())
200 indicators_update(ui);
202 if (config_get_temperature_unit() == CELSIUS)
206 ui_unity_launcher_entry_update(ui->sensors);
208 if (ui->graph_update_interval != cfg->graph_update_interval) {
209 ui->graph_update_interval = cfg->graph_update_interval;
213 pmutex_unlock(&ui->sensors_mutex);
216 g_timeout_add(1000 * ui->graph_update_interval,
217 ui_refresh_thread, ui);
222 static void cb_alarm_raised(struct psensor *sensor, void *data)
224 if (config_get_sensor_alarm_enabled(sensor->id)) {
225 ui_notify(sensor, (struct ui_psensor *)data);
231 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
237 high_temp = config_get_default_high_threshold_temperature();
242 s->cb_alarm_raised = cb_alarm_raised;
243 s->cb_alarm_raised_data = ui;
245 ret = config_get_sensor_alarm_high_threshold
246 (s->id, &s->alarm_high_threshold);
249 if (s->max == UNKNOWN_DBL_VALUE) {
250 if (s->type & SENSOR_TYPE_TEMP)
251 s->alarm_high_threshold = high_temp;
253 s->alarm_high_threshold = s->max;
257 ret = config_get_sensor_alarm_low_threshold
258 (s->id, &s->alarm_low_threshold);
260 if (!ret && s->min != UNKNOWN_DBL_VALUE)
261 s->alarm_low_threshold = s->min;
267 static void associate_preferences(struct psensor **sensors)
269 struct psensor **sensor_cur = sensors;
271 while (*sensor_cur) {
273 struct psensor *s = *sensor_cur;
275 n = config_get_sensor_name(s->id);
286 static void log_init(void)
291 dir = get_psensor_user_dir();
296 path = malloc(strlen(dir)+1+strlen("log")+1);
297 sprintf(path, "%s/%s", dir, "log");
304 static struct option long_options[] = {
305 {"version", no_argument, NULL, 'v'},
306 {"help", no_argument, NULL, 'h'},
307 {"url", required_argument, NULL, 'u'},
308 {"debug", required_argument, NULL, 'd'},
309 {"new-instance", no_argument, NULL, 'n'},
313 static gboolean initial_window_show(gpointer data)
315 struct ui_psensor *ui;
317 log_debug("initial_window_show()");
319 ui = (struct ui_psensor *)data;
321 log_debug("is_status_supported: %d", is_status_supported());
322 log_debug("is_appindicator_supported: %d",
323 is_appindicator_supported());
324 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
326 if (!ui->config->hide_on_startup
327 || (!is_appindicator_supported() && !is_status_supported()))
330 ui_window_update(ui);
335 static void log_glib_info(void)
337 log_debug("Compiled with GLib %d.%d.%d",
342 log_debug("Running with GLib %d.%d.%d",
348 static void cb_activate(GApplication *application,
351 ui_window_show((struct ui_psensor *)data);
355 * Release memory for Valgrind.
357 static void cleanup(struct ui_psensor *ui)
359 pmutex_lock(&ui->sensors_mutex);
361 log_debug("Cleanup...");
367 psensor_list_free(ui->sensors);
370 ui_appindicator_cleanup();
374 pmutex_unlock(&ui->sensors_mutex);
378 log_debug("Cleanup done, closing log");
382 * Creates the list of sensors.
384 * 'url': remote psensor server url, null for local monitoring.
386 static struct psensor **create_sensors_list(const char *url)
388 struct psensor **sensors;
391 if (rsensor_is_supported()) {
393 sensors = get_remote_sensors(url, 600);
395 log_err(_("Psensor has not been compiled with remote "
400 sensors = malloc(sizeof(struct psensor *));
403 if (config_is_lmsensor_enabled())
404 lmsensor_psensor_list_append(&sensors, 600);
406 if (config_is_hddtemp_enabled())
407 hddtemp_psensor_list_append(&sensors, 600);
409 if (config_is_libatasmart_enabled())
410 atasmart_psensor_list_append(&sensors, 600);
412 if (config_is_nvctrl_enabled())
413 nvidia_psensor_list_append(&sensors, 600);
415 if (config_is_atiadlsdk_enabled())
416 amd_psensor_list_append(&sensors, 600);
418 if (config_is_gtop2_enabled())
419 gtop2_psensor_list_append(&sensors, 600);
421 if (config_is_udisks2_enabled())
422 udisks2_psensor_list_append(&sensors, 600);
425 associate_preferences(sensors);
430 int main(int argc, char **argv)
432 struct ui_psensor ui;
434 int optc, cmdok, opti, new_instance, ret;
438 program_name = argv[0];
440 setlocale(LC_ALL, "");
443 bindtextdomain(PACKAGE, LOCALEDIR);
450 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
455 url = strdup(optarg);
464 log_level = atoi(optarg);
465 log_info(_("Enables debug mode."));
476 if (!cmdok || optind != argc) {
477 fprintf(stderr, _("Try `%s --help' for more information.\n"),
484 app = g_application_new("wpitchoune.psensor", 0);
486 g_application_register(app, NULL, NULL);
488 if (!new_instance && g_application_get_is_remote(app)) {
489 g_application_activate(app);
490 log_warn(_("A Psensor instance already exists."));
494 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
497 #if !(GLIB_CHECK_VERSION(2, 31, 0))
499 * Since GLib 2.31 g_thread_init call is deprecated and not
502 log_debug("Calling g_thread_init(NULL)");
506 gtk_init(NULL, NULL);
508 pmutex_init(&ui.sensors_mutex);
510 ui.config = config_load();
512 ui.sensors = create_sensors_list(url);
513 associate_cb_alarm_raised(ui.sensors, &ui);
515 if (ui.config->slog_enabled)
519 config_get_slog_interval());
522 ui_status_set_visible(1);
525 ui_window_create(&ui);
527 ui_enable_alpha_channel(&ui);
529 ret = pthread_create(&thread, NULL, update_measures, &ui);
532 log_err(_("Failed to create thread for monitoring sensors"));
534 ui.graph_update_interval = ui.config->graph_update_interval;
536 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
538 ui_appindicator_init(&ui);
541 gdk_notify_startup_complete();
544 * hack, did not find a cleaner solution.
545 * wait 30s to ensure that the status icon is attempted to be
546 * drawn before determining whether the main window must be
549 if (ui.config->hide_on_startup)
550 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
552 initial_window_show(&ui);
560 log_debug("Quitting...");