2 * Copyright (C) 2010-2012 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"
42 #include "ui_status.h"
56 #ifdef HAVE_REMOTE_SUPPORT
60 #include "ui_appindicator.h"
63 #include "ui_notify.h"
72 static const char *program_name;
74 static void print_version()
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()
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"));
100 puts(_(" -u, --url=URL "
101 "the URL of the psensor-server, example: http://hostname:3131"));
102 puts(_(" --use-libatasmart "
103 "use atasmart library for disk monitoring "
104 "instead of hddtemp daemon"));
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;
128 struct psensor *s = *cur;
130 if (s->values_max_length != cfg->sensor_values_max_length)
131 psensor_values_resize(s,
132 cfg->sensor_values_max_length);
138 static void update_measures(struct ui_psensor *ui)
140 struct psensor **sensors;
147 g_mutex_lock(ui->sensors_mutex);
149 sensors = ui->sensors;
153 update_psensor_values_size(sensors, cfg);
155 psensor_list_update_measures(sensors);
156 #ifdef HAVE_REMOTE_SUPPORT
157 remote_psensor_list_update(sensors);
160 nvidia_psensor_list_update(sensors);
162 #ifdef HAVE_LIBATIADL
163 amd_psensor_list_update(sensors);
166 psensor_log_measures(sensors);
168 period = cfg->sensor_update_interval;
170 g_mutex_unlock(ui->sensors_mutex);
176 static void indicators_update(struct ui_psensor *ui)
178 struct psensor **sensor_cur = ui->sensors;
179 unsigned int attention = 0;
181 while (*sensor_cur) {
182 struct psensor *s = *sensor_cur;
184 if (s->alarm_enabled && s->alarm_raised) {
192 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
193 if (is_appindicator_supported())
194 ui_appindicator_update(ui, attention);
197 if (is_status_supported())
198 ui_status_update(ui, attention);
201 gboolean ui_refresh_thread(gpointer data)
205 struct ui_psensor *ui = (struct ui_psensor *)data;
210 g_mutex_lock(ui->sensors_mutex);
212 graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
214 ui_sensorlist_update(ui);
216 if (is_appindicator_supported() || is_status_supported())
217 indicators_update(ui);
220 ui_unity_launcher_entry_update(ui->sensors,
221 !cfg->unity_launcher_count_disabled,
222 cfg->temperature_unit == CELCIUS);
225 if (ui->graph_update_interval != cfg->graph_update_interval) {
226 ui->graph_update_interval = cfg->graph_update_interval;
230 g_mutex_unlock(ui->sensors_mutex);
233 g_timeout_add(1000 * ui->graph_update_interval,
234 ui_refresh_thread, ui);
239 static void cb_alarm_raised(struct psensor *sensor, void *data)
241 #ifdef HAVE_LIBNOTIFY
243 ui_notify(sensor, (struct ui_psensor *)data);
247 static void associate_colors(struct psensor **sensors)
249 /* number of uniq colors */
250 #define COLORS_COUNT 8
252 unsigned int colors[COLORS_COUNT][3] = {
253 {0x0000, 0x0000, 0x0000}, /* black */
254 {0xffff, 0x0000, 0x0000}, /* red */
255 {0x0000, 0x0000, 0xffff}, /* blue */
256 {0x0000, 0xffff, 0x0000}, /* green */
258 {0x7fff, 0x7fff, 0x7fff}, /* grey */
259 {0x7fff, 0x0000, 0x0000}, /* dark red */
260 {0x0000, 0x0000, 0x7fff}, /* dark blue */
261 {0x0000, 0x7fff, 0x0000} /* dark green */
264 struct psensor **sensor_cur = sensors;
266 while (*sensor_cur) {
267 struct color default_color;
268 color_set(&default_color,
269 colors[i % COLORS_COUNT][0],
270 colors[i % COLORS_COUNT][1],
271 colors[i % COLORS_COUNT][2]);
274 = config_get_sensor_color((*sensor_cur)->id,
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 if (is_temp_type(s->type)) {
294 = config_get_sensor_alarm_limit(s->id, 60);
296 = config_get_sensor_alarm_enabled(s->id);
299 s->alarm_enabled = 0;
306 static void associate_preferences(struct psensor **sensors)
308 struct psensor **sensor_cur = sensors;
309 while (*sensor_cur) {
311 struct psensor *s = *sensor_cur;
313 s->enabled = config_is_sensor_enabled(s->id);
315 n = config_get_sensor_name(s->id);
326 static void log_init()
328 char *home, *path, *dir;
330 home = getenv("HOME");
335 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
336 sprintf(dir, "%s/%s", home, ".psensor");
339 path = malloc(strlen(dir)+1+strlen("log")+1);
340 sprintf(path, "%s/%s", dir, "log");
348 static struct option long_options[] = {
349 {"use-libatasmart", no_argument, 0, 0},
350 {"version", no_argument, 0, 'v'},
351 {"help", no_argument, 0, 'h'},
352 {"url", required_argument, 0, 'u'},
353 {"debug", required_argument, 0, 'd'},
354 {"new-instance", no_argument, 0, 'n'},
358 static gboolean initial_window_show(gpointer data)
360 struct ui_psensor *ui;
362 log_debug("initial_window_show()");
364 ui = (struct ui_psensor *)data;
366 log_debug("is_status_supported: %d", is_status_supported());
367 log_debug("is_appindicator_supported: %d",
368 is_appindicator_supported());
369 log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
371 if (!ui->config->hide_on_startup
372 || (!is_appindicator_supported() && !is_status_supported()))
375 ui_window_update(ui);
380 static void log_glib_info()
382 log_debug("Compiled with GLib %d.%d.%d",
387 log_debug("Running with GLib %d.%d.%d",
393 static void cb_activate(GApplication *application,
396 ui_window_show((struct ui_psensor *)data);
400 * Release memory for Valgrind.
402 static void cleanup(struct ui_psensor *ui)
404 g_mutex_lock(ui->sensors_mutex);
406 log_debug("Cleanup...");
413 #ifdef HAVE_LIBATIADL
416 #ifdef HAVE_REMOTE_SUPPORT
420 psensor_list_free(ui->sensors);
423 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
424 ui_appindicator_cleanup();
429 g_mutex_unlock(ui->sensors_mutex);
433 log_debug("Cleanup done, closing log");
439 * Creates the list of sensors.
441 * 'url': remote psensor server url, null for local monitoring.
442 * 'use_libatasmart': whether the libatasmart must be used.
444 static struct psensor **create_sensors_list(const char *url,
445 unsigned int use_libatasmart)
447 struct psensor **sensors;
450 #ifdef HAVE_REMOTE_SUPPORT
452 sensors = get_remote_sensors(url, 600);
455 _("ERROR: Not compiled with remote sensor support.\n"));
459 sensors = get_all_sensors(use_libatasmart, 600);
461 sensors = nvidia_psensor_list_add(sensors, 600);
463 #ifdef HAVE_LIBATIADL
464 sensors = amd_psensor_list_add(sensors, 600);
467 sensors = cpu_psensor_list_add(sensors, 600);
471 associate_preferences(sensors);
472 associate_colors(sensors);
477 int main(int argc, char **argv)
479 struct ui_psensor ui;
482 int optc, cmdok, opti, use_libatasmart, new_instance;
486 program_name = argv[0];
488 setlocale(LC_ALL, "");
491 bindtextdomain(PACKAGE, LOCALEDIR);
495 use_libatasmart = new_instance = 0;
498 while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
502 if (!strcmp(long_options[opti].name, "use-libatasmart"))
507 url = strdup(optarg);
516 log_level = atoi(optarg);
517 log_printf(LOG_INFO, _("Enables debug mode."));
528 if (!cmdok || optind != argc) {
529 fprintf(stderr, _("Try `%s --help' for more information.\n"),
536 app = g_application_new("wpitchoune.psensor", 0);
538 g_application_register(app, NULL, NULL);
540 if (!new_instance && g_application_get_is_remote(app)) {
541 g_application_activate(app);
542 log_warn(_("Psensor instance already exists"));
546 g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
549 #if !(GLIB_CHECK_VERSION(2, 31, 0))
551 * Since GLib 2.31 g_thread_init call is deprecated and not
554 log_debug("Calling g_thread_init(NULL)");
560 gtk_init(NULL, NULL);
562 ui.sensors_mutex = g_mutex_new();
564 ui.config = config_load();
568 ui.sensors = create_sensors_list(url, use_libatasmart);
569 associate_cb_alarm_raised(ui.sensors, &ui);
571 #if !defined(HAVE_APPINDICATOR) && !defined(HAVE_APPINDICATOR_029)
573 ui_status_set_visible(1);
577 ui_window_create(&ui);
578 ui.sensor_box = NULL;
581 ui.w_graph = ui_graph_create(&ui);
583 ui_enable_alpha_channel(&ui);
586 ui_sensorlist_create(&ui);
588 thread = g_thread_create((GThreadFunc) update_measures,
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);
603 * show the window as soon as all gtk events have been processed
604 * in order to ensure that the status icon is attempted to be
605 * drawn before. If not, there is no way to detect that it is
608 g_idle_add((GSourceFunc)initial_window_show, &ui);
610 gdk_notify_startup_complete();
617 log_debug("Quitting...");