1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
X-Git-Url: http://lxde.git.sourceforge.net/git/gitweb.cgi?p=lxde%2Flxpanel;a=blobdiff_plain;f=src%2Fplugins%2Fthermal%2Fthermal.c;h=1ac80939630e4c26b376a45dc7474ddca8246189;hp=afe5e89135fa28d72f7aa625f70214c3adb3239e;hb=HEAD;hpb=8e33d60b30bb9236d7a16c06e8f079936c8dd9b0
diff --git a/src/plugins/thermal/thermal.c b/src/plugins/thermal/thermal.c
index afe5e89..1ac8093 100644
--- a/src/plugins/thermal/thermal.c
+++ b/src/plugins/thermal/thermal.c
@@ -32,12 +32,18 @@
#include "dbg.h"
-#define THERMAL_DIRECTORY "/proc/acpi/thermal_zone/" /* must be slash-terminated */
-#define THERMAL_TEMPF "temperature"
-#define THERMAL_TRIP "trip_points"
-#define TRIP_CRITICAL "critical (S5):"
+#define PROC_THERMAL_DIRECTORY "/proc/acpi/thermal_zone/" /* must be slash-terminated */
+#define PROC_THERMAL_TEMPF "temperature"
+#define PROC_THERMAL_TRIP "trip_points"
+#define PROC_TRIP_CRITICAL "critical (S5):"
-typedef struct {
+#define SYSFS_THERMAL_DIRECTORY "/sys/class/thermal/" /* must be slash-terminated */
+#define SYSFS_THERMAL_SUBDIR_PREFIX "thermal_zone"
+#define SYSFS_THERMAL_TEMPF "temp"
+#define SYSFS_THERMAL_TRIP "trip_point_0_temp"
+
+
+typedef struct thermal {
Plugin * plugin;
GtkWidget *main;
GtkWidget *namew;
@@ -45,7 +51,7 @@ typedef struct {
int critical;
int warning1;
int warning2;
- int custom_levels, auto_sensor;
+ int not_custom_levels, auto_sensor;
char *sensor,
*str_cl_normal,
*str_cl_warning1,
@@ -54,17 +60,20 @@ typedef struct {
GdkColor cl_normal,
cl_warning1,
cl_warning2;
+ gint (*get_temperature)(struct thermal *th);
+ gint (*get_critical)(struct thermal *th);
} thermal;
+
static gint
-get_critical(thermal *th){
+proc_get_critical(thermal *th){
FILE *state;
char buf[ 256 ], sstmp [ 100 ];
char* pstr;
if(th->sensor == NULL) return -1;
- sprintf(sstmp,"%s%s",th->sensor,THERMAL_TRIP);
+ sprintf(sstmp,"%s%s",th->sensor,PROC_THERMAL_TRIP);
if (!(state = fopen( sstmp, "r"))) {
//printf("cannot open %s\n",sstmp);
@@ -72,15 +81,15 @@ get_critical(thermal *th){
}
while( fgets(buf, 256, state) &&
- ! ( pstr = strstr(buf, TRIP_CRITICAL) ) );
+ ! ( pstr = strstr(buf, PROC_TRIP_CRITICAL) ) );
if( pstr )
{
- pstr += strlen(TRIP_CRITICAL);
+ pstr += strlen(PROC_TRIP_CRITICAL);
while( *pstr && *pstr == ' ' )
++pstr;
pstr[strlen(pstr)-3] = '\0';
- printf("Critical: [%s]\n",pstr);
+ //printf("Critical: [%s]\n",pstr);
fclose(state);
return atoi(pstr);
}
@@ -90,14 +99,14 @@ get_critical(thermal *th){
}
static gint
-get_temperature(thermal *th){
+proc_get_temperature(thermal *th){
FILE *state;
char buf[ 256 ], sstmp [ 100 ];
char* pstr;
if(th->sensor == NULL) return -1;
- sprintf(sstmp,"%s%s",th->sensor,THERMAL_TEMPF);
+ sprintf(sstmp,"%s%s",th->sensor,PROC_THERMAL_TEMPF);
if (!(state = fopen( sstmp, "r"))) {
//printf("cannot open %s\n",sstmp);
@@ -122,11 +131,77 @@ get_temperature(thermal *th){
}
static gint
+sysfs_get_critical(thermal *th){
+ FILE *state;
+ char buf[ 256 ], sstmp [ 100 ];
+ char* pstr;
+
+ if(th->sensor == NULL) return -1;
+
+ sprintf(sstmp,"%s%s",th->sensor,SYSFS_THERMAL_TRIP);
+
+ if (!(state = fopen( sstmp, "r"))) {
+ //printf("cannot open %s\n",sstmp);
+ return -1;
+ }
+
+ while( fgets(buf, 256, state) &&
+ ! ( pstr = buf ) );
+ if( pstr )
+ {
+ fclose(state);
+ return atoi(pstr)/1000;
+ }
+
+ fclose(state);
+ return -1;
+}
+
+static gint
+sysfs_get_temperature(thermal *th){
+ FILE *state;
+ char buf[ 256 ], sstmp [ 100 ];
+ char* pstr;
+
+ if(th->sensor == NULL) return -1;
+
+ sprintf(sstmp,"%s%s",th->sensor,SYSFS_THERMAL_TEMPF);
+
+ if (!(state = fopen( sstmp, "r"))) {
+ //printf("cannot open %s\n",sstmp);
+ return -1;
+ }
+
+ while (fgets(buf, 256, state) &&
+ ! ( pstr = buf ) );
+ if( pstr )
+ {
+ fclose(state);
+ return atoi(pstr)/1000;
+ }
+
+ fclose(state);
+ return -1;
+}
+
+
+static void
+set_get_functions(thermal *th)
+{
+ if (th->sensor && strncmp(th->sensor, "/sys/", 5) == 0){
+ th->get_temperature = sysfs_get_temperature;
+ th->get_critical = sysfs_get_critical;
+ } else {
+ th->get_temperature = proc_get_temperature;
+ th->get_critical = proc_get_critical;
+ }
+}
+
+static gint
update_display(thermal *th)
{
char buffer [60];
- int n;
- int temp = get_temperature(th);
+ int temp = th->get_temperature(th);
GdkColor color;
if(temp >= th->warning2)
@@ -141,42 +216,92 @@ update_display(thermal *th)
panel_draw_label_text(th->plugin->panel, th->namew, "NA", TRUE, TRUE);
else
{
- n = sprintf(buffer, "<span color=\"#%06x\"><b>%02d</b></span>", gcolor2rgb24(&color), temp);
+ sprintf(buffer, "<span color=\"#%06x\"><b>%02d</b></span>", gcolor2rgb24(&color), temp);
gtk_label_set_markup (GTK_LABEL(th->namew), buffer) ;
}
RET(TRUE);
}
+
+/* get_sensor():
+ * - Get the sensor directory, and store it in '*sensor'.
+ * - It is searched for in 'directory'.
+ * - Only the subdirectories starting with 'subdir_prefix' are accepted as sensors.
+ * - 'subdir_prefix' may be NULL, in which case any subdir is considered a sensor. */
static void
-check_sensors( thermal* th )
+get_sensor(char** sensor, char const* directory, char const* subdir_prefix)
{
GDir *sensorsDirectory;
const char *sensor_name;
char sensor_path[100];
- if (! (sensorsDirectory = g_dir_open(THERMAL_DIRECTORY, 0, NULL)))
+ if (! (sensorsDirectory = g_dir_open(directory, 0, NULL)))
{
- th->sensor = NULL;
+ *sensor = NULL;
return;
}
/* Scan the thermal_zone directory for available sensors */
while ((sensor_name = g_dir_read_name(sensorsDirectory))) {
if (sensor_name[0] != '.') {
- sprintf(sensor_path,"%s%s/",THERMAL_DIRECTORY, sensor_name);
- if(th->sensor) {
- g_free(th->sensor);
- th->sensor = NULL;
+ if (subdir_prefix) {
+ if (strncmp(sensor_name, subdir_prefix, strlen(subdir_prefix)) != 0)
+ continue;
}
- th->sensor = strdup(sensor_path);
- //printf("sensor: %s\n", b->sensor);
+ sprintf(sensor_path,"%s%s/", directory, sensor_name);
+ if(*sensor) {
+ g_free(*sensor);
+ *sensor = NULL;
+ }
+ *sensor = strdup(sensor_path);
break;
}
}
g_dir_close(sensorsDirectory);
}
+static void
+check_sensors( thermal *th )
+{
+ if(th->sensor) {
+ g_free(th->sensor);
+ th->sensor = NULL;
+ }
+
+ get_sensor(&th->sensor, PROC_THERMAL_DIRECTORY, NULL);
+
+ if (!th->sensor)
+ get_sensor(&th->sensor, SYSFS_THERMAL_DIRECTORY, SYSFS_THERMAL_SUBDIR_PREFIX);
+
+ //printf("thermal sensor: %s\n", th->sensor);
+}
+
+
+static void applyConfig(Plugin* p)
+{
+ thermal *th = p->priv;
+ ENTER;
+
+ if (th->str_cl_normal) gdk_color_parse(th->str_cl_normal, &th->cl_normal);
+ if (th->str_cl_warning1) gdk_color_parse(th->str_cl_warning1, &th->cl_warning1);
+ if (th->str_cl_warning2) gdk_color_parse(th->str_cl_warning2, &th->cl_warning2);
+
+ if(th->sensor == NULL) th->auto_sensor = TRUE;
+ if(th->auto_sensor) check_sensors(th);
+
+ set_get_functions(th);
+
+ th->critical = th->get_critical(th);
+
+ if(th->not_custom_levels){
+ th->warning1 = th->critical - 10;
+ th->warning2 = th->critical - 5;
+ }
+
+ RET();
+}
+
static int
thermal_constructor(Plugin *p, char** fp)
{
@@ -197,6 +322,11 @@ thermal_constructor(Plugin *p, char** fp)
th->main = p->pwid;
th->tip = gtk_tooltips_new();
+ /* By default, use automatic, that is, "not custom" temperature levels. If
+ * we were using custom levels, they would be 0°C at startup, so we would
+ * display in warning colors by default. */
+ th->not_custom_levels = TRUE;
+
g_signal_connect (G_OBJECT (p->pwid), "button_press_event",
G_CALLBACK (plugin_button_press_event), (gpointer) p);
@@ -220,7 +350,7 @@ thermal_constructor(Plugin *p, char** fp)
}else if (!g_ascii_strcasecmp(s.t[0], "AutomaticSensor")){
th->auto_sensor= atoi(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "CustomLevels")){
- th->custom_levels= atoi(s.t[1]);
+ th->not_custom_levels= atoi(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "Sensor")){
th->sensor= g_strdup(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "Warning1Temp")){
@@ -236,7 +366,6 @@ thermal_constructor(Plugin *p, char** fp)
goto error;
}
}
-
}
if(!th->str_cl_normal)
@@ -246,20 +375,7 @@ thermal_constructor(Plugin *p, char** fp)
if(!th->str_cl_warning2)
th->str_cl_warning2 = g_strdup("#ff0000");
- gdk_color_parse(th->str_cl_normal, &(th->cl_normal));
- gdk_color_parse(th->str_cl_warning1, &(th->cl_warning1));
- gdk_color_parse(th->str_cl_warning2, &(th->cl_warning2));
-
-
- if(th->sensor == NULL) th->auto_sensor = TRUE;
- if(th->auto_sensor) check_sensors(th);
-
- th->critical = get_critical(th);
-
- if(!th->custom_levels){
- th->warning1 = th->critical - 10;
- th->warning2 = th->critical - 5;
- }
+ applyConfig(p);
gtk_widget_show(th->namew);
@@ -272,27 +388,6 @@ error:
RET(FALSE);
}
-static void applyConfig(Plugin* p)
-{
-
- thermal *th = (thermal *)p->priv;
-
- ENTER;
-
- if (th->str_cl_normal) gdk_color_parse(th->str_cl_normal, &th->cl_normal);
- if (th->str_cl_warning1) gdk_color_parse(th->str_cl_warning1, &th->cl_warning1);
- if (th->str_cl_warning2) gdk_color_parse(th->str_cl_warning2, &th->cl_warning2);
-
- if(th->auto_sensor) check_sensors(th);
-
- if(th->custom_levels){
- th->warning1 = th->critical - 10;
- th->warning2 = th->critical - 5;
- }
-
- RET();
-}
-
static void config(Plugin *p, GtkWindow* parent) {
ENTER;
@@ -306,7 +401,7 @@ static void config(Plugin *p, GtkWindow* parent) {
_("Warning2"), &th->str_cl_warning2, CONF_TYPE_STR,
_("Automatic sensor location"), &th->auto_sensor, CONF_TYPE_BOOL,
_("Sensor"), &th->sensor, CONF_TYPE_STR,
- _("Automatic temperature levels"), &th->custom_levels, CONF_TYPE_BOOL,
+ _("Automatic temperature levels"), &th->not_custom_levels, CONF_TYPE_BOOL,
_("Warning1 Temperature"), &th->warning1, CONF_TYPE_INT,
_("Warning2 Temperature"), &th->warning2, CONF_TYPE_INT,
NULL);
@@ -338,7 +433,7 @@ static void save_config( Plugin* p, FILE* fp )
lxpanel_put_str( fp, "NormalColor", th->str_cl_normal );
lxpanel_put_str( fp, "Warning1Color", th->str_cl_warning1 );
lxpanel_put_str( fp, "Warning2Color", th->str_cl_warning2 );
- lxpanel_put_int( fp, "CustomLevels", th->custom_levels );
+ lxpanel_put_int( fp, "CustomLevels", th->not_custom_levels );
lxpanel_put_int( fp, "Warning1Temp", th->warning1 );
lxpanel_put_int( fp, "Warning2Temp", th->warning2 );
lxpanel_put_int( fp, "AutomaticSensor", th->auto_sensor );
|