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 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
62 #include "ui_appindicator.h"
66 #include "ui_notify.h"
75 static const char *program_name;
77 static void print_version()
79 printf("psensor %s\n", VERSION);
80 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
81 License GPLv2: GNU GPL version 2 or later \
82 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
83 This is free software: you are free to change and redistribute it.\n\
84 There is NO WARRANTY, to the extent permitted by law.\n"),
88 static void print_help()
90 printf(_("Usage: %s [OPTION]...\n"), program_name);
92 puts(_("psensor is a GTK application for monitoring hardware sensors, "
93 "including temperatures and fan speeds."));
98 -h, --help display this help and exit\n\
99 -v, --version display version information and exit"));
105 the URL of the psensor-server, example: http://hostname:3131"));
109 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
111 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
115 Updates the size of the sensor values if different than the
119 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
121 struct psensor **cur;
125 struct psensor *s = *cur;
127 if (s->values_max_length != cfg->sensor_values_max_length)
128 psensor_values_resize(s,
129 cfg->sensor_values_max_length);
135 static void log_measures(struct psensor **sensors)
137 if (log_level == LOG_DEBUG)
139 log_printf(LOG_DEBUG, "%s %.2f",
141 psensor_get_current_value(*sensors));
147 void update_psensor_measures(struct ui_psensor *ui)
149 struct psensor **sensors = ui->sensors;
150 struct config *cfg = ui->config;
153 g_mutex_lock(ui->sensors_mutex);
158 update_psensor_values_size(sensors, ui->config);
160 psensor_list_update_measures(sensors);
161 #ifdef HAVE_REMOTE_SUPPORT
162 remote_psensor_list_update(sensors);
165 nvidia_psensor_list_update(sensors);
167 #ifdef HAVE_LIBATIADL
168 amd_psensor_list_update(sensors);
171 log_measures(sensors);
173 g_mutex_unlock(ui->sensors_mutex);
175 sleep(cfg->sensor_update_interval);
179 gboolean ui_refresh_thread(gpointer data)
183 struct ui_psensor *ui = (struct ui_psensor *)data;
188 g_mutex_lock(ui->sensors_mutex);
190 graph_update(ui->sensors, ui->w_graph, ui->config);
192 ui_sensorlist_update(ui);
194 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
195 ui_appindicator_update(ui);
199 ui_unity_launcher_entry_update(ui->sensors,
200 !cfg->unity_launcher_count_disabled);
203 if (ui->graph_update_interval != cfg->graph_update_interval) {
204 ui->graph_update_interval = cfg->graph_update_interval;
208 g_mutex_unlock(ui->sensors_mutex);
211 g_timeout_add(1000 * ui->graph_update_interval,
212 ui_refresh_thread, ui);
217 void cb_alarm_raised(struct psensor *sensor, void *data)
219 #ifdef HAVE_LIBNOTIFY
221 ui_notify(sensor, (struct ui_psensor *)data);
225 static void associate_colors(struct psensor **sensors)
227 /* number of uniq colors */
228 #define COLORS_COUNT 8
230 unsigned int colors[COLORS_COUNT][3] = {
231 {0x0000, 0x0000, 0x0000}, /* black */
232 {0xffff, 0x0000, 0x0000}, /* red */
233 {0x0000, 0.0000, 0xffff}, /* blue */
234 {0x0000, 0xffff, 0x0000}, /* green */
236 {0x7fff, 0x7fff, 0x7fff}, /* grey */
237 {0x7fff, 0x0000, 0x0000}, /* dark red */
238 {0x0000, 0x0000, 0x7fff}, /* dark blue */
239 {0x0000, 0x7fff, 0x0000} /* dark green */
242 struct psensor **sensor_cur = sensors;
244 while (*sensor_cur) {
245 struct color default_color;
246 color_set(&default_color,
247 colors[i % COLORS_COUNT][0],
248 colors[i % COLORS_COUNT][1],
249 colors[i % COLORS_COUNT][2]);
252 = config_get_sensor_color((*sensor_cur)->id,
261 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
263 struct psensor **sensor_cur = sensors;
264 while (*sensor_cur) {
265 struct psensor *s = *sensor_cur;
267 s->cb_alarm_raised = cb_alarm_raised;
268 s->cb_alarm_raised_data = ui;
270 if (is_temp_type(s->type)) {
272 = config_get_sensor_alarm_limit(s->id, 60);
274 = config_get_sensor_alarm_enabled(s->id);
277 s->alarm_enabled = 0;
284 static void associate_preferences(struct psensor **sensors)
286 struct psensor **sensor_cur = sensors;
287 while (*sensor_cur) {
289 struct psensor *s = *sensor_cur;
291 s->enabled = config_is_sensor_enabled(s->id);
293 n = config_get_sensor_name(s->id);
302 static void log_init()
304 char *home, *path, *dir;
306 home = getenv("HOME");
311 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
312 sprintf(dir, "%s/%s", home, ".psensor");
315 path = malloc(strlen(dir)+1+strlen("log")+1);
316 sprintf(path, "%s/%s", dir, "log");
324 static struct option long_options[] = {
325 {"version", no_argument, 0, 'v'},
326 {"help", no_argument, 0, 'h'},
327 {"url", required_argument, 0, 'u'},
328 {"debug", no_argument, 0, 'd'},
332 int main(int argc, char **argv)
334 struct ui_psensor ui;
341 program_name = argv[0];
343 setlocale(LC_ALL, "");
346 bindtextdomain(PACKAGE, LOCALEDIR);
350 while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
355 url = strdup(optarg);
364 printf(_("Enables debug mode.\n"));
365 log_level = LOG_DEBUG;
373 if (!cmdok || optind != argc) {
374 fprintf(stderr, _("Try `%s --help' for more information.\n"),
383 /* gdk_threads_enter(); */
385 gtk_init(NULL, NULL);
387 #ifdef HAVE_LIBNOTIFY
388 ui.notification_last_time = NULL;
391 ui.sensors_mutex = g_mutex_new();
395 ui.config = config_load();
400 #ifdef HAVE_REMOTE_SUPPORT
402 ui.sensors = get_remote_sensors(url, 600);
405 _("ERROR: Not compiled with remote sensor support.\n"));
409 ui.sensors = get_all_sensors(600);
411 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
413 #ifdef HAVE_LIBATIADL
414 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
417 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
421 associate_preferences(ui.sensors);
422 associate_colors(ui.sensors);
423 associate_cb_alarm_raised(ui.sensors, &ui);
426 ui_window_create(&ui);
427 ui.sensor_box = NULL;
430 ui.w_graph = ui_graph_create(&ui);
433 ui_sensorlist_create(&ui);
435 ui_window_update(&ui);
437 thread = g_thread_create((GThreadFunc) update_psensor_measures,
443 ui.graph_update_interval = ui.config->graph_update_interval;
445 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
447 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
448 ui_appindicator_init(&ui);
453 gdk_notify_startup_complete();
458 g_mutex_lock(ui.sensors_mutex);
465 #ifdef HAVE_LIBATIADL
468 #ifdef HAVE_REMOTE_SUPPORT
472 psensor_list_free(ui.sensors);
475 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
476 ui_appindicator_cleanup();
481 g_mutex_unlock(ui.sensors_mutex);