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"
56 #ifdef HAVE_REMOTE_SUPPORT
60 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
61 #include "ui_appindicator.h"
65 #include "ui_notify.h"
74 static const char *program_name;
78 printf("psensor %s\n", VERSION);
79 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
80 License GPLv2: GNU GPL version 2 or later \
81 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
82 This is free software: you are free to change and redistribute it.\n\
83 There is NO WARRANTY, to the extent permitted by law.\n"),
89 printf(_("Usage: %s [OPTION]...\n"), program_name);
91 puts(_("psensor is a GTK application for monitoring hardware sensors, "
92 "including temperatures and fan speeds."));
97 -h, --help display this help and exit\n\
98 -v, --version display version information and exit"));
104 the URL of the psensor-server, example: http://hostname:3131"));
108 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
110 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
114 Updates the size of the sensor values if different than the
118 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
120 struct psensor **cur;
124 struct psensor *s = *cur;
126 if (s->values_max_length != cfg->sensor_values_max_length)
127 psensor_values_resize(s,
128 cfg->sensor_values_max_length);
134 static void log_measures(struct psensor **sensors)
136 if (log_level == LOG_DEBUG)
138 log_printf(LOG_DEBUG, "%s %.2f",
140 psensor_get_current_value(*sensors));
146 void update_psensor_measures(struct ui_psensor *ui)
148 struct psensor **sensors = ui->sensors;
149 struct config *cfg = ui->config;
152 g_mutex_lock(ui->sensors_mutex);
157 update_psensor_values_size(sensors, ui->config);
159 psensor_list_update_measures(sensors);
160 #ifdef HAVE_REMOTE_SUPPORT
161 remote_psensor_list_update(sensors);
164 nvidia_psensor_list_update(sensors);
166 #ifdef HAVE_LIBATIADL
167 amd_psensor_list_update(sensors);
170 log_measures(sensors);
172 g_mutex_unlock(ui->sensors_mutex);
174 sleep(cfg->sensor_update_interval);
178 gboolean ui_refresh_thread(gpointer data)
182 struct ui_psensor *ui = (struct ui_psensor *)data;
187 g_mutex_lock(ui->sensors_mutex);
189 graph_update(ui->sensors, ui->w_graph, ui->config);
191 ui_sensorlist_update(ui);
193 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
194 ui_appindicator_update(ui);
198 ui_unity_launcher_entry_update(ui->sensors,
199 !cfg->unity_launcher_count_disabled);
202 if (ui->graph_update_interval != cfg->graph_update_interval) {
203 ui->graph_update_interval = cfg->graph_update_interval;
207 g_mutex_unlock(ui->sensors_mutex);
210 g_timeout_add(1000 * ui->graph_update_interval,
211 ui_refresh_thread, ui);
216 void cb_alarm_raised(struct psensor *sensor, void *data)
218 #ifdef HAVE_LIBNOTIFY
220 ui_notify(sensor, (struct ui_psensor *)data);
224 static void associate_colors(struct psensor **sensors)
226 /* number of uniq colors */
227 #define COLORS_COUNT 8
229 unsigned int colors[COLORS_COUNT][3] = {
230 {0x0000, 0x0000, 0x0000}, /* black */
231 {0xffff, 0x0000, 0x0000}, /* red */
232 {0x0000, 0.0000, 0xffff}, /* blue */
233 {0x0000, 0xffff, 0x0000}, /* green */
235 {0x7fff, 0x7fff, 0x7fff}, /* grey */
236 {0x7fff, 0x0000, 0x0000}, /* dark red */
237 {0x0000, 0x0000, 0x7fff}, /* dark blue */
238 {0x0000, 0x7fff, 0x0000} /* dark green */
241 struct psensor **sensor_cur = sensors;
243 while (*sensor_cur) {
244 struct color default_color;
245 color_set(&default_color,
246 colors[i % COLORS_COUNT][0],
247 colors[i % COLORS_COUNT][1],
248 colors[i % COLORS_COUNT][2]);
251 = config_get_sensor_color((*sensor_cur)->id,
260 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
262 struct psensor **sensor_cur = sensors;
263 while (*sensor_cur) {
264 struct psensor *s = *sensor_cur;
266 s->cb_alarm_raised = cb_alarm_raised;
267 s->cb_alarm_raised_data = ui;
269 if (is_temp_type(s->type)) {
271 = config_get_sensor_alarm_limit(s->id, 60);
273 = config_get_sensor_alarm_enabled(s->id);
276 s->alarm_enabled = 0;
283 static void associate_preferences(struct psensor **sensors)
285 struct psensor **sensor_cur = sensors;
286 while (*sensor_cur) {
288 struct psensor *s = *sensor_cur;
290 s->enabled = config_is_sensor_enabled(s->id);
292 n = config_get_sensor_name(s->id);
301 static void log_init()
303 char *home, *path, *dir;
305 home = getenv("HOME");
310 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
311 sprintf(dir, "%s/%s", home, ".psensor");
314 path = malloc(strlen(dir)+1+strlen("log")+1);
315 sprintf(path, "%s/%s", dir, "log");
317 log_open(path, LOG_INFO);
323 static struct option long_options[] = {
324 {"version", no_argument, 0, 'v'},
325 {"help", no_argument, 0, 'h'},
326 {"url", required_argument, 0, 'u'},
327 {"debug", no_argument, 0, 'd'},
331 int main(int argc, char **argv)
333 struct ui_psensor ui;
340 program_name = argv[0];
342 setlocale(LC_ALL, "");
345 bindtextdomain(PACKAGE, LOCALEDIR);
349 while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
354 url = strdup(optarg);
363 printf(_("Enables debug mode.\n"));
364 log_level = LOG_DEBUG;
372 if (!cmdok || optind != argc) {
373 fprintf(stderr, _("Try `%s --help' for more information.\n"),
382 /* gdk_threads_enter(); */
384 gtk_init(NULL, NULL);
386 #ifdef HAVE_LIBNOTIFY
387 ui.notification_last_time = NULL;
390 ui.sensors_mutex = g_mutex_new();
394 ui.config = config_load();
399 #ifdef HAVE_REMOTE_SUPPORT
401 ui.sensors = get_remote_sensors(url, 600);
404 _("ERROR: Not compiled with remote sensor support.\n"));
408 ui.sensors = get_all_sensors(600);
410 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
412 #ifdef HAVE_LIBATIADL
413 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
416 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
420 associate_preferences(ui.sensors);
421 associate_colors(ui.sensors);
422 associate_cb_alarm_raised(ui.sensors, &ui);
425 ui_window_create(&ui);
426 ui.sensor_box = NULL;
429 ui.w_graph = ui_graph_create(&ui);
432 ui_sensorlist_create(&ui);
434 ui_window_update(&ui);
436 thread = g_thread_create((GThreadFunc) update_psensor_measures,
442 ui.graph_update_interval = ui.config->graph_update_interval;
444 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
446 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
447 ui_appindicator_init(&ui);
450 gdk_notify_startup_complete();
455 g_mutex_lock(ui.sensors_mutex);
462 #ifdef HAVE_LIBATIADL
466 psensor_list_free(ui.sensors);
469 g_mutex_unlock(ui.sensors_mutex);