2 Copyright (C) 2010-2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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
28 #include <sys/types.h>
38 #include "ui_sensorlist.h"
43 #include "ui_status.h"
57 #ifdef HAVE_REMOTE_SUPPORT
61 #include "ui_appindicator.h"
64 #include "ui_notify.h"
73 static const char *program_name;
75 static void print_version()
77 printf("psensor %s\n", VERSION);
78 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
79 License GPLv2: GNU GPL version 2 or later \
80 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
81 This is free software: you are free to change and redistribute it.\n\
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."));
96 -h, --help display this help and exit\n\
97 -v, --version display version information and exit"));
103 the URL of the psensor-server, example: http://hostname:3131"));
107 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
109 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
113 Updates the size of the sensor values if different than the
117 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
119 struct psensor **cur;
123 struct psensor *s = *cur;
125 if (s->values_max_length != cfg->sensor_values_max_length)
126 psensor_values_resize(s,
127 cfg->sensor_values_max_length);
133 static void log_measures(struct psensor **sensors)
135 if (log_level == LOG_DEBUG)
137 log_printf(LOG_DEBUG, "%s %.2f",
139 psensor_get_current_value(*sensors));
145 void update_psensor_measures(struct ui_psensor *ui)
147 struct psensor **sensors = ui->sensors;
148 struct config *cfg = ui->config;
151 g_mutex_lock(ui->sensors_mutex);
156 update_psensor_values_size(sensors, ui->config);
158 psensor_list_update_measures(sensors);
159 #ifdef HAVE_REMOTE_SUPPORT
160 remote_psensor_list_update(sensors);
163 nvidia_psensor_list_update(sensors);
165 #ifdef HAVE_LIBATIADL
166 amd_psensor_list_update(sensors);
169 log_measures(sensors);
171 g_mutex_unlock(ui->sensors_mutex);
173 sleep(cfg->sensor_update_interval);
177 static void indicators_update(struct ui_psensor *ui)
179 struct psensor **sensor_cur = ui->sensors;
180 unsigned int attention = 0;
182 if (!is_appindicator_supported() && !is_status_supported())
185 while (*sensor_cur) {
186 struct psensor *s = *sensor_cur;
188 if (s->alarm_enabled && s->alarm_raised) {
196 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
197 if (is_appindicator_supported())
198 ui_appindicator_update(ui, attention);
201 if (is_status_supported())
202 ui_status_update(ui, attention);
205 gboolean ui_refresh_thread(gpointer data)
209 struct ui_psensor *ui = (struct ui_psensor *)data;
214 g_mutex_lock(ui->sensors_mutex);
216 graph_update(ui->sensors, ui->w_graph, ui->config);
218 ui_sensorlist_update(ui);
220 indicators_update(ui);
223 ui_unity_launcher_entry_update(ui->sensors,
224 !cfg->unity_launcher_count_disabled);
227 if (ui->graph_update_interval != cfg->graph_update_interval) {
228 ui->graph_update_interval = cfg->graph_update_interval;
232 g_mutex_unlock(ui->sensors_mutex);
235 g_timeout_add(1000 * ui->graph_update_interval,
236 ui_refresh_thread, ui);
241 void cb_alarm_raised(struct psensor *sensor, void *data)
243 #ifdef HAVE_LIBNOTIFY
245 ui_notify(sensor, (struct ui_psensor *)data);
249 static void associate_colors(struct psensor **sensors)
251 /* number of uniq colors */
252 #define COLORS_COUNT 8
254 unsigned int colors[COLORS_COUNT][3] = {
255 {0x0000, 0x0000, 0x0000}, /* black */
256 {0xffff, 0x0000, 0x0000}, /* red */
257 {0x0000, 0.0000, 0xffff}, /* blue */
258 {0x0000, 0xffff, 0x0000}, /* green */
260 {0x7fff, 0x7fff, 0x7fff}, /* grey */
261 {0x7fff, 0x0000, 0x0000}, /* dark red */
262 {0x0000, 0x0000, 0x7fff}, /* dark blue */
263 {0x0000, 0x7fff, 0x0000} /* dark green */
266 struct psensor **sensor_cur = sensors;
268 while (*sensor_cur) {
269 struct color default_color;
270 color_set(&default_color,
271 colors[i % COLORS_COUNT][0],
272 colors[i % COLORS_COUNT][1],
273 colors[i % COLORS_COUNT][2]);
276 = config_get_sensor_color((*sensor_cur)->id,
285 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
287 struct psensor **sensor_cur = sensors;
288 while (*sensor_cur) {
289 struct psensor *s = *sensor_cur;
291 s->cb_alarm_raised = cb_alarm_raised;
292 s->cb_alarm_raised_data = ui;
294 if (is_temp_type(s->type)) {
296 = config_get_sensor_alarm_limit(s->id, 60);
298 = config_get_sensor_alarm_enabled(s->id);
301 s->alarm_enabled = 0;
308 static void associate_preferences(struct psensor **sensors)
310 struct psensor **sensor_cur = sensors;
311 while (*sensor_cur) {
313 struct psensor *s = *sensor_cur;
315 s->enabled = config_is_sensor_enabled(s->id);
317 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 {"version", no_argument, 0, 'v'},
350 {"help", no_argument, 0, 'h'},
351 {"url", required_argument, 0, 'u'},
352 {"debug", no_argument, 0, 'd'},
356 static gboolean initial_window_show(gpointer data)
358 struct ui_psensor *ui;
360 ui = (struct ui_psensor *)data;
362 log_printf(LOG_DEBUG,
363 "is_status_supported: %d\n", is_status_supported());
365 if (!ui->config->hide_on_startup
366 || (!is_appindicator_supported() && !is_status_supported()))
369 ui_window_update(ui);
374 int main(int argc, char **argv)
376 struct ui_psensor ui;
383 program_name = argv[0];
385 setlocale(LC_ALL, "");
388 bindtextdomain(PACKAGE, LOCALEDIR);
392 while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
397 url = strdup(optarg);
406 printf(_("Enables debug mode.\n"));
407 log_level = LOG_DEBUG;
415 if (!cmdok || optind != argc) {
416 fprintf(stderr, _("Try `%s --help' for more information.\n"),
425 /* gdk_threads_enter(); */
427 gtk_init(NULL, NULL);
429 #ifdef HAVE_LIBNOTIFY
430 ui.notification_last_time = NULL;
433 ui.sensors_mutex = g_mutex_new();
437 ui.config = config_load();
442 #ifdef HAVE_REMOTE_SUPPORT
444 ui.sensors = get_remote_sensors(url, 600);
447 _("ERROR: Not compiled with remote sensor support.\n"));
451 ui.sensors = get_all_sensors(600);
453 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
455 #ifdef HAVE_LIBATIADL
456 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
459 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
463 associate_preferences(ui.sensors);
464 associate_colors(ui.sensors);
465 associate_cb_alarm_raised(ui.sensors, &ui);
470 ui_window_create(&ui);
471 ui.sensor_box = NULL;
474 ui.w_graph = ui_graph_create(&ui);
477 ui_sensorlist_create(&ui);
480 * show the window as soon as all gtk events have been processed
481 * in order to ensure that the status icon is attempted to be
482 * drawn before. If not, there is no way to detect that it is
485 g_idle_add((GSourceFunc)initial_window_show, &ui);
487 thread = g_thread_create((GThreadFunc) update_psensor_measures,
493 ui.graph_update_interval = ui.config->graph_update_interval;
495 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
497 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
498 ui_appindicator_init(&ui);
501 gdk_notify_startup_complete();
506 g_mutex_lock(ui.sensors_mutex);
513 #ifdef HAVE_LIBATIADL
516 #ifdef HAVE_REMOTE_SUPPORT
520 psensor_list_free(ui.sensors);
523 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
524 ui_appindicator_cleanup();
529 g_mutex_unlock(ui.sensors_mutex);