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>
39 #include "ui_sensorlist.h"
53 #ifdef HAVE_REMOTE_SUPPORT
57 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
58 #include "ui_appindicator.h"
62 #include "ui_notify.h"
67 static const char *program_name;
71 printf("psensor %s\n", VERSION);
72 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
73 License GPLv2: GNU GPL version 2 or later \
74 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
75 This is free software: you are free to change and redistribute it.\n\
76 There is NO WARRANTY, to the extent permitted by law.\n"),
82 printf(_("Usage: %s [OPTION]...\n"), program_name);
84 puts(_("psensor is a GTK application for monitoring hardware sensors, "
85 "including temperatures and fan speeds."));
90 -h, --help display this help and exit\n\
91 -v, --version display version information and exit"));
97 the URL of the psensor-server, example: http://hostname:3131"));
101 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
103 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
107 Updates the size of the sensor values if different than the
111 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
113 struct psensor **cur;
117 struct psensor *s = *cur;
119 if (s->values_max_length != cfg->sensor_values_max_length)
120 psensor_values_resize(s,
121 cfg->sensor_values_max_length);
127 void update_psensor_measures(struct ui_psensor *ui)
129 struct psensor **sensors = ui->sensors;
130 struct config *cfg = ui->config;
133 g_mutex_lock(ui->sensors_mutex);
138 update_psensor_values_size(sensors, ui->config);
140 psensor_list_update_measures(sensors);
141 #ifdef HAVE_REMOTE_SUPPORT
142 remote_psensor_list_update(sensors);
145 nvidia_psensor_list_update(sensors);
148 g_mutex_unlock(ui->sensors_mutex);
150 sleep(cfg->sensor_update_interval);
154 gboolean ui_refresh_thread(gpointer data)
158 struct ui_psensor *ui = (struct ui_psensor *)data;
163 g_mutex_lock(ui->sensors_mutex);
165 graph_update(ui->sensors, ui->w_graph, ui->config);
167 ui_sensorlist_update(ui);
169 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
170 ui_appindicator_update(ui);
174 ui_unity_launcher_entry_update(ui->sensors,
175 !cfg->unity_launcher_count_disabled);
178 if (ui->graph_update_interval != cfg->graph_update_interval) {
179 ui->graph_update_interval = cfg->graph_update_interval;
183 g_mutex_unlock(ui->sensors_mutex);
186 g_timeout_add(1000 * ui->graph_update_interval,
187 ui_refresh_thread, ui);
192 void cb_alarm_raised(struct psensor *sensor, void *data)
194 #ifdef HAVE_LIBNOTIFY
196 ui_notify(sensor, (struct ui_psensor *)data);
200 void associate_colors(struct psensor **sensors)
202 /* number of uniq colors */
203 #define COLORS_COUNT 8
205 unsigned int colors[COLORS_COUNT][3] = {
206 {0x0000, 0x0000, 0x0000}, /* black */
207 {0xffff, 0x0000, 0x0000}, /* red */
208 {0x0000, 0.0000, 0xffff}, /* blue */
209 {0x0000, 0xffff, 0x0000}, /* green */
211 {0x7fff, 0x7fff, 0x7fff}, /* grey */
212 {0x7fff, 0x0000, 0x0000}, /* dark red */
213 {0x0000, 0x0000, 0x7fff}, /* dark blue */
214 {0x0000, 0x7fff, 0x0000} /* dark green */
217 struct psensor **sensor_cur = sensors;
219 while (*sensor_cur) {
220 struct color default_color;
221 color_set(&default_color,
222 colors[i % COLORS_COUNT][0],
223 colors[i % COLORS_COUNT][1],
224 colors[i % COLORS_COUNT][2]);
227 = config_get_sensor_color((*sensor_cur)->id,
236 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
238 struct psensor **sensor_cur = sensors;
239 while (*sensor_cur) {
240 struct psensor *s = *sensor_cur;
242 s->cb_alarm_raised = cb_alarm_raised;
243 s->cb_alarm_raised_data = ui;
245 if (is_temp_type(s->type)) {
247 = config_get_sensor_alarm_limit(s->id, 60);
249 = config_get_sensor_alarm_enabled(s->id);
252 s->alarm_enabled = 0;
259 void associate_preferences(struct psensor **sensors)
261 struct psensor **sensor_cur = sensors;
262 while (*sensor_cur) {
264 struct psensor *s = *sensor_cur;
266 s->enabled = config_is_sensor_enabled(s->id);
268 n = config_get_sensor_name(s->id);
278 static struct option long_options[] = {
279 {"version", no_argument, 0, 'v'},
280 {"help", no_argument, 0, 'h'},
281 {"url", required_argument, 0, 'u'},
285 int main(int argc, char **argv)
287 struct ui_psensor ui;
294 program_name = argv[0];
296 setlocale(LC_ALL, "");
299 bindtextdomain(PACKAGE, LOCALEDIR);
303 while ((optc = getopt_long(argc, argv, "vhu:", long_options,
308 url = strdup(optarg);
322 if (!cmdok || optind != argc) {
323 fprintf(stderr, _("Try `%s --help' for more information.\n"),
330 /* gdk_threads_enter(); */
332 gtk_init(&argc, &argv);
334 #ifdef HAVE_LIBNOTIFY
335 ui.notification_last_time = NULL;
338 ui.sensors_mutex = g_mutex_new();
342 ui.config = config_load();
344 err = lmsensor_init();
346 fprintf(stderr, _("ERROR: lmsensor init failure: %s\n"),
347 sensors_strerror(err));
352 #ifdef HAVE_REMOTE_SUPPORT
354 ui.sensors = get_remote_sensors(url, 600);
357 _("ERROR: Not compiled with remote sensor support.\n"));
362 struct psensor **tmp;
364 tmp = get_all_sensors(600);
365 ui.sensors = nvidia_psensor_list_add(tmp, 600);
367 if (tmp != ui.sensors)
370 ui.sensors = get_all_sensors(600);
374 associate_preferences(ui.sensors);
375 associate_colors(ui.sensors);
376 associate_cb_alarm_raised(ui.sensors, &ui);
379 ui_window_create(&ui);
380 ui.sensor_box = NULL;
383 ui.w_graph = ui_graph_create(&ui);
386 ui_sensorlist_create(&ui);
388 ui_window_update(&ui);
390 thread = g_thread_create((GThreadFunc) update_psensor_measures,
396 ui.graph_update_interval = ui.config->graph_update_interval;
398 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
400 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
401 ui_appindicator_init(&ui);
409 psensor_list_free(ui.sensors);