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 void update_psensor_measures(struct ui_psensor *ui)
136 struct psensor **sensors = ui->sensors;
137 struct config *cfg = ui->config;
140 g_mutex_lock(ui->sensors_mutex);
145 update_psensor_values_size(sensors, ui->config);
147 psensor_list_update_measures(sensors);
148 #ifdef HAVE_REMOTE_SUPPORT
149 remote_psensor_list_update(sensors);
152 nvidia_psensor_list_update(sensors);
154 #ifdef HAVE_LIBATIADL
155 amd_psensor_list_update(sensors);
157 g_mutex_unlock(ui->sensors_mutex);
159 sleep(cfg->sensor_update_interval);
163 gboolean ui_refresh_thread(gpointer data)
167 struct ui_psensor *ui = (struct ui_psensor *)data;
172 g_mutex_lock(ui->sensors_mutex);
174 graph_update(ui->sensors, ui->w_graph, ui->config);
176 ui_sensorlist_update(ui);
178 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
179 ui_appindicator_update(ui);
183 ui_unity_launcher_entry_update(ui->sensors,
184 !cfg->unity_launcher_count_disabled);
187 if (ui->graph_update_interval != cfg->graph_update_interval) {
188 ui->graph_update_interval = cfg->graph_update_interval;
192 g_mutex_unlock(ui->sensors_mutex);
195 g_timeout_add(1000 * ui->graph_update_interval,
196 ui_refresh_thread, ui);
201 void cb_alarm_raised(struct psensor *sensor, void *data)
203 #ifdef HAVE_LIBNOTIFY
205 ui_notify(sensor, (struct ui_psensor *)data);
209 static void associate_colors(struct psensor **sensors)
211 /* number of uniq colors */
212 #define COLORS_COUNT 8
214 unsigned int colors[COLORS_COUNT][3] = {
215 {0x0000, 0x0000, 0x0000}, /* black */
216 {0xffff, 0x0000, 0x0000}, /* red */
217 {0x0000, 0.0000, 0xffff}, /* blue */
218 {0x0000, 0xffff, 0x0000}, /* green */
220 {0x7fff, 0x7fff, 0x7fff}, /* grey */
221 {0x7fff, 0x0000, 0x0000}, /* dark red */
222 {0x0000, 0x0000, 0x7fff}, /* dark blue */
223 {0x0000, 0x7fff, 0x0000} /* dark green */
226 struct psensor **sensor_cur = sensors;
228 while (*sensor_cur) {
229 struct color default_color;
230 color_set(&default_color,
231 colors[i % COLORS_COUNT][0],
232 colors[i % COLORS_COUNT][1],
233 colors[i % COLORS_COUNT][2]);
236 = config_get_sensor_color((*sensor_cur)->id,
245 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
247 struct psensor **sensor_cur = sensors;
248 while (*sensor_cur) {
249 struct psensor *s = *sensor_cur;
251 s->cb_alarm_raised = cb_alarm_raised;
252 s->cb_alarm_raised_data = ui;
254 if (is_temp_type(s->type)) {
256 = config_get_sensor_alarm_limit(s->id, 60);
258 = config_get_sensor_alarm_enabled(s->id);
261 s->alarm_enabled = 0;
268 static void associate_preferences(struct psensor **sensors)
270 struct psensor **sensor_cur = sensors;
271 while (*sensor_cur) {
273 struct psensor *s = *sensor_cur;
275 s->enabled = config_is_sensor_enabled(s->id);
277 n = config_get_sensor_name(s->id);
286 static void log_init()
288 char *home, *path, *dir;
290 home = getenv("HOME");
295 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
296 sprintf(dir, "%s/%s", home, ".psensor");
299 path = malloc(strlen(dir)+1+strlen("log")+1);
300 sprintf(path, "%s/%s", dir, "log");
302 log_open(path, LOG_INFO);
308 static struct option long_options[] = {
309 {"version", no_argument, 0, 'v'},
310 {"help", no_argument, 0, 'h'},
311 {"url", required_argument, 0, 'u'},
315 int main(int argc, char **argv)
317 struct ui_psensor ui;
324 program_name = argv[0];
326 setlocale(LC_ALL, "");
329 bindtextdomain(PACKAGE, LOCALEDIR);
333 while ((optc = getopt_long(argc, argv, "vhu:", long_options,
338 url = strdup(optarg);
352 if (!cmdok || optind != argc) {
353 fprintf(stderr, _("Try `%s --help' for more information.\n"),
362 /* gdk_threads_enter(); */
364 gtk_init(NULL, NULL);
366 #ifdef HAVE_LIBNOTIFY
367 ui.notification_last_time = NULL;
370 ui.sensors_mutex = g_mutex_new();
374 ui.config = config_load();
379 #ifdef HAVE_REMOTE_SUPPORT
381 ui.sensors = get_remote_sensors(url, 600);
384 _("ERROR: Not compiled with remote sensor support.\n"));
388 ui.sensors = get_all_sensors(600);
390 ui.sensors = nvidia_psensor_list_add(ui.sensors, 600);
392 #ifdef HAVE_LIBATIADL
393 ui.sensors = amd_psensor_list_add(ui.sensors, 600);
396 ui.sensors = cpu_psensor_list_add(ui.sensors, 600);
400 associate_preferences(ui.sensors);
401 associate_colors(ui.sensors);
402 associate_cb_alarm_raised(ui.sensors, &ui);
405 ui_window_create(&ui);
406 ui.sensor_box = NULL;
409 ui.w_graph = ui_graph_create(&ui);
412 ui_sensorlist_create(&ui);
414 ui_window_update(&ui);
416 thread = g_thread_create((GThreadFunc) update_psensor_measures,
422 ui.graph_update_interval = ui.config->graph_update_interval;
424 g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
426 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
427 ui_appindicator_init(&ui);
433 g_mutex_lock(ui.sensors_mutex);
440 #ifdef HAVE_LIBATIADL
444 psensor_list_free(ui.sensors);
447 g_mutex_unlock(ui.sensors_mutex);