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,
179 !cfg->unity_launcher_count_disabled);
182 if (ui->graph_update_interval != cfg->graph_update_interval) {
183 ui->graph_update_interval = cfg->graph_update_interval;
187 g_mutex_unlock(ui->sensors_mutex);
188 /*gdk_threads_leave();*/
191 g_timeout_add(1000 * ui->graph_update_interval,
192 ui_refresh_thread, ui);
197 void cb_alarm_raised(struct psensor *sensor, void *data)
199 #ifdef HAVE_LIBNOTIFY
201 ui_notify(sensor, (struct ui_psensor *)data);
205 void associate_colors(struct psensor **sensors)
207 /* number of uniq colors */
208 #define COLORS_COUNT 8
210 unsigned int colors[COLORS_COUNT][3] = {
211 {0x0000, 0x0000, 0x0000}, /* black */
212 {0xffff, 0x0000, 0x0000}, /* red */
213 {0x0000, 0.0000, 0xffff}, /* blue */
214 {0x0000, 0xffff, 0x0000}, /* green */
216 {0x7fff, 0x7fff, 0x7fff}, /* grey */
217 {0x7fff, 0x0000, 0x0000}, /* dark red */
218 {0x0000, 0x0000, 0x7fff}, /* dark blue */
219 {0x0000, 0x7fff, 0x0000} /* dark green */
222 struct psensor **sensor_cur = sensors;
224 while (*sensor_cur) {
225 struct color default_color;
226 color_set(&default_color,
227 colors[i % COLORS_COUNT][0],
228 colors[i % COLORS_COUNT][1],
229 colors[i % COLORS_COUNT][2]);
232 = config_get_sensor_color((*sensor_cur)->id,
241 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
243 struct psensor **sensor_cur = sensors;
244 while (*sensor_cur) {
245 struct psensor *s = *sensor_cur;
247 s->cb_alarm_raised = cb_alarm_raised;
248 s->cb_alarm_raised_data = ui;
250 if (is_temp_type(s->type)) {
252 = config_get_sensor_alarm_limit(s->id, 60);
254 = config_get_sensor_alarm_enabled(s->id);
257 s->alarm_enabled = 0;
264 void associate_preferences(struct psensor **sensors)
266 struct psensor **sensor_cur = sensors;
267 while (*sensor_cur) {
269 struct psensor *s = *sensor_cur;
271 s->enabled = config_is_sensor_enabled(s->id);
273 n = config_get_sensor_name(s->id);
283 static struct option long_options[] = {
284 {"version", no_argument, 0, 'v'},
285 {"help", no_argument, 0, 'h'},
286 {"url", required_argument, 0, 'u'},
290 int main(int argc, char **argv)
292 struct ui_psensor ui;
299 program_name = argv[0];
301 setlocale(LC_ALL, "");
304 bindtextdomain(PACKAGE, LOCALEDIR);
308 while ((optc = getopt_long(argc, argv, "vhu:", long_options,
313 url = strdup(optarg);
327 if (!cmdok || optind != argc) {
328 fprintf(stderr, _("Try `%s --help' for more information.\n"),
335 /* gdk_threads_enter(); */
337 gtk_init(&argc, &argv);
339 #ifdef HAVE_LIBNOTIFY
340 ui.notification_last_time = NULL;
343 ui.sensors_mutex = g_mutex_new();
347 ui.config = config_load();
349 err = lmsensor_init();
351 fprintf(stderr, _("ERROR: lmsensor init failure: %s\n"),
352 sensors_strerror(err));
357 #ifdef HAVE_REMOTE_SUPPORT
359 ui.sensors = get_remote_sensors(url, 600);
362 _("ERROR: Not compiled with remote sensor support.\n"));
367 struct psensor **tmp;
369 tmp = get_all_sensors(600);
370 ui.sensors = nvidia_psensor_list_add(tmp, 600);
372 if (tmp != ui.sensors)
375 ui.sensors = get_all_sensors(600);
379 associate_preferences(ui.sensors);
380 associate_colors(ui.sensors);
381 associate_cb_alarm_raised(ui.sensors, &ui);
384 ui_window_create(&ui);
385 ui.sensor_box = NULL;
388 ui.w_graph = ui_graph_create(&ui);
391 ui.ui_sensorlist = ui_sensorlist_create(ui.sensors);
393 ui_window_update(&ui);
395 thread = g_thread_create((GThreadFunc) update_psensor_measures,
401 ui.graph_update_interval = ui.config->graph_update_interval;
403 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
405 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
406 ui_appindicator_init(&ui);
414 psensor_list_free(ui.sensors);