Merging upstream version 0.7.0 (Closes: #493243, #510888, #567617, #699414, #709777...
[debian/lxpanel.git] / plugins / thermal / thermal.c
1 /**
2 * Thermal plugin to lxpanel
3 *
4 * Copyright (C) 2007 by Daniel Kesler <kesler.daniel@gmail.com>
5 * 2014 by Andriy Grytsenko <andrej@rep.kiev.ua>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #include <sys/types.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <glib/gi18n.h>
27
28 #include <string.h>
29
30 #include "plugin.h"
31 #include "misc.h"
32
33 #include "dbg.h"
34
35 #define PROC_THERMAL_DIRECTORY "/proc/acpi/thermal_zone/" /* must be slash-terminated */
36 #define PROC_THERMAL_TEMPF "temperature"
37 #define PROC_THERMAL_TRIP "trip_points"
38 #define PROC_TRIP_CRITICAL "critical (S5):"
39
40 #define SYSFS_THERMAL_DIRECTORY "/sys/class/thermal/" /* must be slash-terminated */
41 #define SYSFS_THERMAL_SUBDIR_PREFIX "thermal_zone"
42 #define SYSFS_THERMAL_TEMPF "temp"
43 #define SYSFS_THERMAL_TRIP "trip_point_0_temp"
44
45 #define MAX_NUM_SENSORS 10
46 #define MAX_AUTOMATIC_CRITICAL_TEMP 150 /* in degrees Celsius */
47
48 #if !GLIB_CHECK_VERSION(2, 40, 0)
49 # define g_info(...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
50 #endif
51
52 typedef gint (*GetTempFunc)(char const *);
53
54 typedef struct thermal {
55 LXPanel *panel;
56 config_setting_t *settings;
57 GtkWidget *namew;
58 GString *tip;
59 int critical;
60 int warning1;
61 int warning2;
62 int not_custom_levels, auto_sensor;
63 char *sensor,
64 *str_cl_normal,
65 *str_cl_warning1,
66 *str_cl_warning2;
67 unsigned int timer;
68 GdkColor cl_normal,
69 cl_warning1,
70 cl_warning2;
71 int numsensors;
72 char *sensor_array[MAX_NUM_SENSORS];
73 char *sensor_name[MAX_NUM_SENSORS];
74 GetTempFunc get_temperature[MAX_NUM_SENSORS];
75 GetTempFunc get_critical[MAX_NUM_SENSORS];
76 gint temperature[MAX_NUM_SENSORS];
77 } thermal;
78
79
80 static gint
81 proc_get_critical(char const* sensor_path){
82 FILE *state;
83 char buf[ 256 ], sstmp [ 100 ];
84 char* pstr;
85
86 if(sensor_path == NULL) return -1;
87
88 snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,PROC_THERMAL_TRIP);
89
90 if (!(state = fopen( sstmp, "r"))) {
91 g_warning("thermal: cannot open %s", sstmp);
92 return -1;
93 }
94
95 while( fgets(buf, 256, state) &&
96 ! ( pstr = strstr(buf, PROC_TRIP_CRITICAL) ) );
97 if( pstr )
98 {
99 pstr += strlen(PROC_TRIP_CRITICAL);
100 while( *pstr && *pstr == ' ' )
101 ++pstr;
102
103 pstr[strlen(pstr)-3] = '\0';
104 fclose(state);
105 return atoi(pstr);
106 }
107
108 fclose(state);
109 return -1;
110 }
111
112 static gint
113 proc_get_temperature(char const* sensor_path){
114 FILE *state;
115 char buf[ 256 ], sstmp [ 100 ];
116 char* pstr;
117
118 if(sensor_path == NULL) return -1;
119
120 snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,PROC_THERMAL_TEMPF);
121
122 if (!(state = fopen( sstmp, "r"))) {
123 g_warning("thermal: cannot open %s", sstmp);
124 return -1;
125 }
126
127 while( fgets(buf, 256, state) &&
128 ! ( pstr = strstr(buf, "temperature:") ) );
129 if( pstr )
130 {
131 pstr += 12;
132 while( *pstr && *pstr == ' ' )
133 ++pstr;
134
135 pstr[strlen(pstr)-3] = '\0';
136 fclose(state);
137 return atoi(pstr);
138 }
139
140 fclose(state);
141 return -1;
142 }
143
144 static gint _get_reading(const char *path)
145 {
146 FILE *state;
147 char buf[256];
148 char* pstr;
149
150 if (!(state = fopen(path, "r"))) {
151 g_warning("thermal: cannot open %s", path);
152 return -1;
153 }
154
155 while( fgets(buf, 256, state) &&
156 ! ( pstr = buf ) );
157 if( pstr )
158 {
159 fclose(state);
160 return atoi(pstr)/1000;
161 }
162
163 fclose(state);
164 return -1;
165 }
166
167 static gint
168 sysfs_get_critical(char const* sensor_path){
169 char sstmp [ 100 ];
170
171 if(sensor_path == NULL) return -1;
172
173 snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,SYSFS_THERMAL_TRIP);
174
175 return _get_reading(sstmp);
176 }
177
178 static gint
179 sysfs_get_temperature(char const* sensor_path){
180 char sstmp [ 100 ];
181
182 if(sensor_path == NULL) return -1;
183
184 snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,SYSFS_THERMAL_TEMPF);
185
186 return _get_reading(sstmp);
187 }
188
189 static gint
190 hwmon_get_critical(char const* sensor_path)
191 {
192 char sstmp [ 100 ];
193 int spl;
194
195 if(sensor_path == NULL) return -1;
196
197 spl = strlen(sensor_path) - 6;
198 if (spl < 17 || spl > 94)
199 return -1;
200
201 snprintf(sstmp, sizeof(sstmp), "%.*s_crit", spl, sensor_path);
202
203 return _get_reading(sstmp);
204 }
205
206 static gint
207 hwmon_get_temperature(char const* sensor_path)
208 {
209 if(sensor_path == NULL) return -1;
210
211 return _get_reading(sensor_path);
212 }
213
214 static gint get_temperature(thermal *th)
215 {
216 gint max = -273;
217 gint cur, i;
218
219 for(i = 0; i < th->numsensors; i++){
220 cur = th->get_temperature[i](th->sensor_array[i]);
221 if (cur > max)
222 max = cur;
223 th->temperature[i] = cur;
224 }
225
226 return max;
227 }
228
229 static gint get_critical(thermal *th)
230 {
231 gint min = MAX_AUTOMATIC_CRITICAL_TEMP;
232 gint cur, i;
233
234 for(i = 0; i < th->numsensors; i++){
235 cur = th->get_critical[i](th->sensor_array[i]);
236 if (cur < min)
237 min = cur;
238 }
239
240 return min;
241 }
242
243 static void
244 update_display(thermal *th)
245 {
246 char buffer [60];
247 int i;
248 int temp;
249 GdkColor color;
250 gchar *separator;
251
252 temp = get_temperature(th);
253 if(temp >= th->warning2)
254 color = th->cl_warning2;
255 else if(temp >= th->warning1)
256 color = th->cl_warning1;
257 else
258 color = th->cl_normal;
259
260 if(temp == -1)
261 lxpanel_draw_label_text(th->panel, th->namew, "NA", TRUE, 1, TRUE);
262 else
263 {
264 snprintf(buffer, sizeof(buffer), "<span color=\"#%06x\"><b>%02d</b></span>",
265 gcolor2rgb24(&color), temp);
266 gtk_label_set_markup (GTK_LABEL(th->namew), buffer) ;
267 }
268
269 g_string_truncate(th->tip, 0);
270 separator = "";
271 for (i = 0; i < th->numsensors; i++){
272 g_string_append_printf(th->tip, "%s%s:\t%2d°C", separator, th->sensor_name[i], th->temperature[i]);
273 separator = "\n";
274 }
275 gtk_widget_set_tooltip_text(th->namew, th->tip->str);
276 }
277
278 static gboolean update_display_timeout(gpointer user_data)
279 {
280 if (g_source_is_destroyed(g_main_current_source()))
281 return FALSE;
282 update_display(user_data);
283 return TRUE; /* repeat later */
284 }
285
286 static int
287 add_sensor(thermal* th, char const* sensor_path, const char *sensor_name,
288 GetTempFunc get_temp, GetTempFunc get_crit)
289 {
290 if (th->numsensors + 1 > MAX_NUM_SENSORS){
291 g_warning("thermal: Too many sensors (max %d), ignoring '%s'",
292 MAX_NUM_SENSORS, sensor_path);
293 return -1;
294 }
295
296 th->sensor_array[th->numsensors] = g_strdup(sensor_path);
297 th->sensor_name[th->numsensors] = g_strdup(sensor_name);
298 th->get_critical[th->numsensors] = get_crit;
299 th->get_temperature[th->numsensors] = get_temp;
300 th->numsensors++;
301
302 g_debug("thermal: Added sensor %s", sensor_path);
303
304 return 0;
305 }
306
307 /* find_sensors():
308 * - Get the sensor directory, and store it in '*sensor'.
309 * - It is searched for in 'directory'.
310 * - Only the subdirectories starting with 'subdir_prefix' are accepted as sensors.
311 * - 'subdir_prefix' may be NULL, in which case any subdir is considered a sensor. */
312 static void
313 find_sensors(thermal* th, char const* directory, char const* subdir_prefix,
314 GetTempFunc get_temp, GetTempFunc get_crit)
315 {
316 GDir *sensorsDirectory;
317 const char *sensor_name;
318 char sensor_path[100];
319
320 if (! (sensorsDirectory = g_dir_open(directory, 0, NULL)))
321 return;
322
323 /* Scan the thermal_zone directory for available sensors */
324 while ((sensor_name = g_dir_read_name(sensorsDirectory))) {
325 if (sensor_name[0] == '.')
326 continue;
327 if (subdir_prefix) {
328 if (strncmp(sensor_name, subdir_prefix, strlen(subdir_prefix)) != 0)
329 continue;
330 }
331 snprintf(sensor_path,sizeof(sensor_path),"%s%s/", directory, sensor_name);
332 add_sensor(th, sensor_path, sensor_name, get_temp, get_crit);
333 }
334 g_dir_close(sensorsDirectory);
335 }
336
337 static void find_hwmon_sensors(thermal* th)
338 {
339 GDir *sensorsDirectory;
340 const char *sensor_name;
341 char sensor_path[100], buf[256];
342 FILE *fp;
343
344 if (!(sensorsDirectory = g_dir_open("/sys/class/hwmon/hwmon0/device/", 0, NULL)))
345 return;
346 /* FIXME: do scanning hwmonX other than 0 */
347
348 while ((sensor_name = g_dir_read_name(sensorsDirectory)))
349 {
350 if (strncmp(sensor_name, "temp", 4) == 0 &&
351 strcmp(&sensor_name[5], "_input") == 0)
352 {
353 snprintf(sensor_path, sizeof(sensor_path),
354 "/sys/class/hwmon/hwmon0/device/temp%c_label", sensor_name[4]);
355 fp = fopen(sensor_path, "r");
356 buf[0] = '\0';
357 if (fp)
358 {
359 if (fgets(buf, 256, fp))
360 {
361 char *pp = strchr(buf, '\n');
362 if (pp)
363 *pp = '\0';
364 }
365 fclose(fp);
366 }
367 snprintf(sensor_path, sizeof(sensor_path),
368 "/sys/class/hwmon/hwmon0/device/%s", sensor_name);
369 add_sensor(th, sensor_path, buf[0] ? buf : sensor_name,
370 hwmon_get_temperature, hwmon_get_critical);
371 }
372 }
373 g_dir_close(sensorsDirectory);
374 }
375
376
377 static void
378 remove_all_sensors(thermal *th)
379 {
380 int i;
381
382 g_debug("thermal: Removing all sensors (%d)", th->numsensors);
383
384 for (i = 0; i < th->numsensors; i++)
385 {
386 g_free(th->sensor_array[i]);
387 g_free(th->sensor_name[i]);
388 }
389
390 th->numsensors = 0;
391 }
392
393 static void
394 check_sensors( thermal *th )
395 {
396 find_sensors(th, PROC_THERMAL_DIRECTORY, NULL, proc_get_temperature, proc_get_critical);
397 find_sensors(th, SYSFS_THERMAL_DIRECTORY, SYSFS_THERMAL_SUBDIR_PREFIX, sysfs_get_temperature, sysfs_get_critical);
398 if (th->numsensors == 0)
399 find_hwmon_sensors(th);
400 g_info("thermal: Found %d sensors", th->numsensors);
401 }
402
403
404 static gboolean applyConfig(gpointer p)
405 {
406 thermal *th = lxpanel_plugin_get_data(p);
407 ENTER;
408
409 if (th->str_cl_normal) gdk_color_parse(th->str_cl_normal, &th->cl_normal);
410 if (th->str_cl_warning1) gdk_color_parse(th->str_cl_warning1, &th->cl_warning1);
411 if (th->str_cl_warning2) gdk_color_parse(th->str_cl_warning2, &th->cl_warning2);
412
413 remove_all_sensors(th);
414 if(th->sensor == NULL) th->auto_sensor = TRUE;
415 if(th->auto_sensor) check_sensors(th);
416 else if (strncmp(th->sensor, "/sys/", 5) != 0)
417 add_sensor(th, th->sensor, th->sensor, proc_get_temperature, proc_get_critical);
418 else if (strncmp(th->sensor, "/sys/class/hwmon/", 17) != 0)
419 add_sensor(th, th->sensor, th->sensor, sysfs_get_temperature, sysfs_get_critical);
420 else
421 add_sensor(th, th->sensor, th->sensor, hwmon_get_temperature, hwmon_get_critical);
422
423 th->critical = get_critical(th);
424
425 if(th->not_custom_levels){
426 th->warning1 = th->critical - 10;
427 th->warning2 = th->critical - 5;
428 }
429
430 config_group_set_string(th->settings, "NormalColor", th->str_cl_normal);
431 config_group_set_string(th->settings, "Warning1Color", th->str_cl_warning1);
432 config_group_set_string(th->settings, "Warning2Color", th->str_cl_warning2);
433 config_group_set_int(th->settings, "CustomLevels", th->not_custom_levels);
434 config_group_set_int(th->settings, "Warning1Temp", th->warning1);
435 config_group_set_int(th->settings, "Warning2Temp", th->warning2);
436 config_group_set_int(th->settings, "AutomaticSensor", th->auto_sensor);
437 config_group_set_string(th->settings, "Sensor", th->sensor);
438 RET(FALSE);
439 }
440
441 static void
442 thermal_destructor(gpointer user_data)
443 {
444 thermal *th = (thermal *)user_data;
445
446 ENTER;
447 remove_all_sensors(th);
448 g_string_free(th->tip, TRUE);
449 g_free(th->sensor);
450 g_free(th->str_cl_normal);
451 g_free(th->str_cl_warning1);
452 g_free(th->str_cl_warning2);
453 g_source_remove(th->timer);
454 g_free(th);
455 RET();
456 }
457
458 static GtkWidget *
459 thermal_constructor(LXPanel *panel, config_setting_t *settings)
460 {
461 thermal *th;
462 GtkWidget *p;
463 const char *tmp;
464
465 ENTER;
466 th = g_new0(thermal, 1);
467 th->panel = panel;
468 th->settings = settings;
469
470 p = gtk_event_box_new();
471 lxpanel_plugin_set_data(p, th, thermal_destructor);
472 gtk_widget_set_has_window(p, FALSE);
473 gtk_container_set_border_width( GTK_CONTAINER(p), 2 );
474
475 th->namew = gtk_label_new("ww");
476 gtk_container_add(GTK_CONTAINER(p), th->namew);
477
478 th->tip = g_string_new(NULL);
479
480 /* By default, use automatic, that is, "not custom" temperature levels. If
481 * we were using custom levels, they would be 0°C at startup, so we would
482 * display in warning colors by default. */
483 th->not_custom_levels = TRUE;
484
485 if (config_setting_lookup_string(settings, "NormalColor", &tmp))
486 th->str_cl_normal = g_strdup(tmp);
487 if (config_setting_lookup_string(settings, "Warning1Color", &tmp))
488 th->str_cl_warning1 = g_strdup(tmp);
489 if (config_setting_lookup_string(settings, "Warning2Color", &tmp))
490 th->str_cl_warning2 = g_strdup(tmp);
491 config_setting_lookup_int(settings, "AutomaticSensor", &th->auto_sensor);
492 config_setting_lookup_int(settings, "CustomLevels", &th->not_custom_levels);
493 if (config_setting_lookup_string(settings, "Sensor", &tmp))
494 th->sensor = g_strdup(tmp);
495 config_setting_lookup_int(settings, "Warning1Temp", &th->warning1);
496 config_setting_lookup_int(settings, "Warning2Temp", &th->warning2);
497
498 if(!th->str_cl_normal)
499 th->str_cl_normal = g_strdup("#00ff00");
500 if(!th->str_cl_warning1)
501 th->str_cl_warning1 = g_strdup("#fff000");
502 if(!th->str_cl_warning2)
503 th->str_cl_warning2 = g_strdup("#ff0000");
504
505 applyConfig(p);
506
507 gtk_widget_show(th->namew);
508
509 update_display(th);
510 th->timer = g_timeout_add_seconds(3, (GSourceFunc) update_display_timeout, (gpointer)th);
511
512 RET(p);
513 }
514
515 static GtkWidget *config(LXPanel *panel, GtkWidget *p)
516 {
517 ENTER;
518
519 GtkWidget *dialog;
520 thermal *th = lxpanel_plugin_get_data(p);
521 dialog = lxpanel_generic_config_dlg(_("Temperature Monitor"),
522 panel, applyConfig, p,
523 _("Normal color"), &th->str_cl_normal, CONF_TYPE_STR,
524 _("Warning1 color"), &th->str_cl_warning1, CONF_TYPE_STR,
525 _("Warning2 color"), &th->str_cl_warning2, CONF_TYPE_STR,
526 _("Automatic sensor location"), &th->auto_sensor, CONF_TYPE_BOOL,
527 _("Sensor"), &th->sensor, CONF_TYPE_STR,
528 _("Automatic temperature levels"), &th->not_custom_levels, CONF_TYPE_BOOL,
529 _("Warning1 temperature"), &th->warning1, CONF_TYPE_INT,
530 _("Warning2 temperature"), &th->warning2, CONF_TYPE_INT,
531 NULL);
532
533 RET(dialog);
534 }
535
536 FM_DEFINE_MODULE(lxpanel_gtk, thermal)
537
538 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
539 .name = N_("Temperature Monitor"),
540 .description = N_("Display system temperature"),
541
542 .new_instance = thermal_constructor,
543 .config = config,
544 };
545
546
547 /* vim: set sw=4 sts=4 et : */