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"
63 #include "ui_status.h"
67 #include "ui_notify.h"
76 static const char *program_name;
78 static void print_version()
80 printf("psensor %s\n", VERSION);
81 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
82 License GPLv2: GNU GPL version 2 or later \
83 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
84 This is free software: you are free to change and redistribute it.\n\
85 There is NO WARRANTY, to the extent permitted by law.\n"),
89 static void print_help()
91 printf(_("Usage: %s [OPTION]...\n"), program_name);
93 puts(_("psensor is a GTK application for monitoring hardware sensors, "
94 "including temperatures and fan speeds."));
99 -h, --help display this help and exit\n\
100 -v, --version display version information and exit"));
106 the URL of the psensor-server, example: http://hostname:3131"));
110 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
112 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
116 Updates the size of the sensor values if different than the
120 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
122 struct psensor **cur;
126 struct psensor *s = *cur;
128 if (s->values_max_length != cfg->sensor_values_max_length)
129 psensor_values_resize(s,
130 cfg->sensor_values_max_length);
136 static void log_measures(struct psensor **sensors)
138 if (log_level == LOG_DEBUG)
140 log_printf(LOG_DEBUG, "%s %.2f",
142 psensor_get_current_value(*sensors));
148 void update_psensor_measures(struct ui_psensor *ui)
150 struct psensor **sensors = ui->sensors;
151 struct config *cfg = ui->config;
154 g_mutex_lock(ui->sensors_mutex);
159 update_psensor_values_size(sensors, ui->config);
161 psensor_list_update_measures(sensors);
162 #ifdef HAVE_REMOTE_SUPPORT
163 remote_psensor_list_update(sensors);
166 nvidia_psensor_list_update(sensors);
168 #ifdef HAVE_LIBATIADL
169 amd_psensor_list_update(sensors);
172 log_measures(sensors);
174 g_mutex_unlock(ui->sensors_mutex);
176 sleep(cfg->sensor_update_interval);
180 gboolean ui_refresh_thread(gpointer data)
184 struct ui_psensor *ui = (struct ui_psensor *)data;
189 g_mutex_lock(ui->sensors_mutex);
191 graph_update(ui->sensors, ui->w_graph, ui->config);
193 ui_sensorlist_update(ui);
195 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
196 ui_appindicator_update(ui);
200 ui_unity_launcher_entry_update(ui->sensors,
201 !cfg->unity_launcher_count_disabled);
204 if (ui->graph_update_interval != cfg->graph_update_interval) {
205 ui->graph_update_interval = cfg->graph_update_interval;
209 g_mutex_unlock(ui->sensors_mutex);
212 g_timeout_add(1000 * ui->graph_update_interval,
213 ui_refresh_thread, ui);
218 void cb_alarm_raised(struct psensor *sensor, void *data)
220 #ifdef HAVE_LIBNOTIFY
222 ui_notify(sensor, (struct ui_psensor *)data);
226 static void associate_colors(struct psensor **sensors)
228 /* number of uniq colors */
229 #define COLORS_COUNT 8
231 unsigned int colors[COLORS_COUNT][3] = {
232 {0x0000, 0x0000, 0x0000}, /* black */
233 {0xffff, 0x0000, 0x0000}, /* red */
234 {0x0000, 0.0000, 0xffff}, /* blue */
235 {0x0000, 0xffff, 0x0000}, /* green */
237 {0x7fff, 0x7fff, 0x7fff}, /* grey */
238 {0x7fff, 0x0000, 0x0000}, /* dark red */
239 {0x0000, 0x0000, 0x7fff}, /* dark blue */
240 {0x0000, 0x7fff, 0x0000} /* dark green */
243 struct psensor **sensor_cur = sensors;
245 while (*sensor_cur) {
246 struct color default_color;
247 color_set(&default_color,
248 colors[i % COLORS_COUNT][0],
249 colors[i % COLORS_COUNT][1],
250 colors[i % COLORS_COUNT][2]);
253 = config_get_sensor_color((*sensor_cur)->id,
262 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
264 struct psensor **sensor_cur = sensors;
265 while (*sensor_cur) {
266 struct psensor *s = *sensor_cur;
268 s->cb_alarm_raised = cb_alarm_raised;
269 s->cb_alarm_raised_data = ui;
271 if (is_temp_type(s->type)) {
273 = config_get_sensor_alarm_limit(s->id, 60);
275 = config_get_sensor_alarm_enabled(s->id);
278 s->alarm_enabled = 0;
285 static void associate_preferences(struct psensor **sensors)
287 struct psensor **sensor_cur = sensors;
288 while (*sensor_cur) {
290 struct psensor *s = *sensor_cur;
292 s->enabled = config_is_sensor_enabled(s->id);
294 n = config_get_sensor_name(s->id);
303 static void log_init()
305 char *home, *path, *dir;
307 home = getenv("HOME");
312 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
313 sprintf(dir, "%s/%s", home, ".psensor");
316 path = malloc(strlen(dir)+1+strlen("log")+1);
317 sprintf(path, "%s/%s", dir, "log");
325 static struct option long_options[] = {
326 {"version", no_argument, 0, 'v'},
327 {"help", no_argument, 0, 'h'},
328 {"url", required_argument, 0, 'u'},
329 {"debug", no_argument, 0, 'd'},
333 int main(int argc, char **argv)
335 struct ui_psensor ui;
342 program_name = argv[0];
344 setlocale(LC_ALL, "");
347 bindtextdomain(PACKAGE, LOCALEDIR);
351 while ((optc = getopt_long(argc, argv, "vhdu:", long_options,
356 url = strdup(optarg);
365 printf(_("Enables debug mode.\n"));
366 log_level = LOG_DEBUG;
374 if (!cmdok || optind != argc) {
375 fprintf(stderr, _("Try `%s --help' for more information.\n"),
384 /* gdk_threads_enter(); */
386 gtk_init(NULL, NULL);
388 #ifdef HAVE_LIBNOTIFY
389 ui.notification_last_time = NULL;
392 ui.sensors_mutex = g_mutex_new();
396 ui.config = config_load();
401 #ifdef HAVE_REMOTE_SUPPORT
403 ui.sensors = get_remote_sensors(url, 600);
406 _("ERROR: Not compiled with remote sensor support.\n"));
410 ui.sensors = get_all_sensors(600);
412 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
414 #ifdef HAVE_LIBATIADL
415 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
418 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
422 associate_preferences(ui.sensors);
423 associate_colors(ui.sensors);
424 associate_cb_alarm_raised(ui.sensors, &ui);
427 ui_window_create(&ui);
428 ui.sensor_box = NULL;
431 ui.w_graph = ui_graph_create(&ui);
434 ui_sensorlist_create(&ui);
436 ui_window_update(&ui);
438 thread = g_thread_create((GThreadFunc) update_psensor_measures,
444 ui.graph_update_interval = ui.config->graph_update_interval;
446 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
448 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
449 ui_appindicator_init(&ui);
454 gdk_notify_startup_complete();
459 g_mutex_lock(ui.sensors_mutex);
466 #ifdef HAVE_LIBATIADL
469 #ifdef HAVE_REMOTE_SUPPORT
473 psensor_list_free(ui.sensors);
476 g_mutex_unlock(ui.sensors_mutex);