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
30 #include <sensors/sensors.h>
31 #include <sensors/error.h>
40 #include "ui_sensorlist.h"
54 #ifdef HAVE_REMOTE_SUPPORT
58 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
59 #include "ui_appindicator.h"
63 #include "ui_notify.h"
68 static const char *program_name;
72 printf("psensor %s\n", VERSION);
73 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
74 License GPLv2: GNU GPL version 2 or later \
75 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
76 This is free software: you are free to change and redistribute it.\n\
77 There is NO WARRANTY, to the extent permitted by law.\n"),
83 printf(_("Usage: %s [OPTION]...\n"), program_name);
85 puts(_("psensor is a GTK application for monitoring hardware sensors, "
86 "including temperatures and fan speeds."));
91 -h, --help display this help and exit\n\
92 -v, --version display version information and exit"));
98 the URL of the psensor-server, example: http://hostname:3131"));
102 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
104 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
108 Updates the size of the sensor values if different than the
112 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
114 struct psensor **cur;
118 struct psensor *s = *cur;
120 if (s->values_max_length != cfg->sensor_values_max_length)
121 psensor_values_resize(s,
122 cfg->sensor_values_max_length);
128 void update_psensor_measures(struct ui_psensor *ui)
130 struct psensor **sensors = ui->sensors;
131 struct config *cfg = ui->config;
134 /*gdk_threads_enter();*/
135 g_mutex_lock(ui->sensors_mutex);
140 update_psensor_values_size(sensors, ui->config);
142 psensor_list_update_measures(sensors);
143 #ifdef HAVE_REMOTE_SUPPORT
144 remote_psensor_list_update(sensors);
147 nvidia_psensor_list_update(sensors);
150 /*gdk_threads_leave();*/
151 g_mutex_unlock(ui->sensors_mutex);
153 sleep(cfg->sensor_update_interval);
157 gboolean ui_refresh_thread(gpointer data)
161 struct ui_psensor *ui = (struct ui_psensor *)data;
166 g_mutex_lock(ui->sensors_mutex);
167 /*gdk_threads_enter();*/
169 graph_update(ui->sensors, ui->w_graph, ui->config);
171 ui_sensorlist_update(ui->ui_sensorlist);
173 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
174 ui_appindicator_update(ui);
178 ui_unity_launcher_entry_update(ui->sensors);
181 if (ui->graph_update_interval != cfg->graph_update_interval) {
182 ui->graph_update_interval = cfg->graph_update_interval;
186 g_mutex_unlock(ui->sensors_mutex);
187 /*gdk_threads_leave();*/
190 g_timeout_add(1000 * ui->graph_update_interval,
191 ui_refresh_thread, ui);
196 void cb_alarm_raised(struct psensor *sensor, void *data)
198 #ifdef HAVE_LIBNOTIFY
200 ui_notify(sensor, (struct ui_psensor *)data);
204 void associate_colors(struct psensor **sensors)
206 /* number of uniq colors */
207 #define COLORS_COUNT 8
209 unsigned int colors[COLORS_COUNT][3] = {
210 {0x0000, 0x0000, 0x0000}, /* black */
211 {0xffff, 0x0000, 0x0000}, /* red */
212 {0x0000, 0.0000, 0xffff}, /* blue */
213 {0x0000, 0xffff, 0x0000}, /* green */
215 {0x7fff, 0x7fff, 0x7fff}, /* grey */
216 {0x7fff, 0x0000, 0x0000}, /* dark red */
217 {0x0000, 0x0000, 0x7fff}, /* dark blue */
218 {0x0000, 0x7fff, 0x0000} /* dark green */
221 struct psensor **sensor_cur = sensors;
223 while (*sensor_cur) {
224 struct color default_color;
225 color_set(&default_color,
226 colors[i % COLORS_COUNT][0],
227 colors[i % COLORS_COUNT][1],
228 colors[i % COLORS_COUNT][2]);
231 = config_get_sensor_color((*sensor_cur)->id,
240 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
242 struct psensor **sensor_cur = sensors;
243 while (*sensor_cur) {
244 struct psensor *s = *sensor_cur;
246 s->cb_alarm_raised = cb_alarm_raised;
247 s->cb_alarm_raised_data = ui;
249 if (is_temp_type(s->type)) {
251 = config_get_sensor_alarm_limit(s->id, 60);
253 = config_get_sensor_alarm_enabled(s->id);
256 s->alarm_enabled = 0;
263 void associate_preferences(struct psensor **sensors)
265 struct psensor **sensor_cur = sensors;
266 while (*sensor_cur) {
268 struct psensor *s = *sensor_cur;
270 s->enabled = config_is_sensor_enabled(s->id);
272 n = config_get_sensor_name(s->id);
282 static struct option long_options[] = {
283 {"version", no_argument, 0, 'v'},
284 {"help", no_argument, 0, 'h'},
285 {"url", required_argument, 0, 'u'},
289 int main(int argc, char **argv)
291 struct ui_psensor ui;
298 program_name = argv[0];
300 setlocale(LC_ALL, "");
303 bindtextdomain(PACKAGE, LOCALEDIR);
307 while ((optc = getopt_long(argc, argv, "vhu:", long_options,
312 url = strdup(optarg);
326 if (!cmdok || optind != argc) {
327 fprintf(stderr, _("Try `%s --help' for more information.\n"),
334 /* gdk_threads_enter(); */
336 gtk_init(&argc, &argv);
338 #ifdef HAVE_LIBNOTIFY
339 ui.notification_last_time = NULL;
342 ui.sensors_mutex = g_mutex_new();
346 ui.config = config_load();
348 err = lmsensor_init();
350 fprintf(stderr, _("ERROR: lmsensor init failure: %s\n"),
351 sensors_strerror(err));
356 #ifdef HAVE_REMOTE_SUPPORT
358 ui.sensors = get_remote_sensors(url, 600);
361 _("ERROR: Not compiled with remote sensor support.\n"));
366 struct psensor **tmp;
368 tmp = get_all_sensors(600);
369 ui.sensors = nvidia_psensor_list_add(tmp, 600);
371 if (tmp != ui.sensors)
374 ui.sensors = get_all_sensors(600);
378 associate_preferences(ui.sensors);
379 associate_colors(ui.sensors);
380 associate_cb_alarm_raised(ui.sensors, &ui);
383 ui_window_create(&ui);
384 ui.sensor_box = NULL;
387 ui.w_graph = ui_graph_create(&ui);
390 ui.ui_sensorlist = ui_sensorlist_create(ui.sensors);
392 ui_window_update(&ui);
394 thread = g_thread_create((GThreadFunc) update_psensor_measures,
400 ui.graph_update_interval = ui.config->graph_update_interval;
402 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
404 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
405 ui_appindicator_init(&ui);
413 psensor_list_free(ui.sensors);