File: thermal.c

package info (click to toggle)
lxpanel 0.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,588 kB
  • sloc: ansic: 31,210; sh: 4,369; makefile: 466; python: 20
file content (589 lines) | stat: -rw-r--r-- 17,219 bytes parent folder | download | duplicates (2)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
 * Thermal plugin to lxpanel
 *
 * Copyright (C) 2007 by Daniel Kesler <kesler.daniel@gmail.com>
 *               2014-2015 by Andriy Grytsenko <andrej@rep.kiev.ua>
 *
 * This file is a part of LXPanel project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib/gi18n.h>

#include <string.h>

#include "plugin.h"
#include "misc.h"

#include "dbg.h"

#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):"

#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"

#define MAX_NUM_SENSORS 10
#define MAX_AUTOMATIC_CRITICAL_TEMP 150 /* in degrees Celsius */

#if !GLIB_CHECK_VERSION(2, 40, 0)
# define g_info(...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
#endif

typedef gint (*GetTempFunc)(char const *);

typedef struct thermal {
    LXPanel *panel;
    config_setting_t *settings;
    GtkWidget *namew;
    GString *tip;
    int warning1;
    int warning2;
    int not_custom_levels, auto_sensor;
    char *sensor,
         *str_cl_normal,
         *str_cl_warning1,
         *str_cl_warning2;
    unsigned int timer;
    GdkColor cl_normal,
             cl_warning1,
             cl_warning2;
    int numsensors;
    char *sensor_array[MAX_NUM_SENSORS];
    char *sensor_name[MAX_NUM_SENSORS];
    GetTempFunc get_temperature[MAX_NUM_SENSORS];
    GetTempFunc get_critical[MAX_NUM_SENSORS];
    gint temperature[MAX_NUM_SENSORS];
    gint critical[MAX_NUM_SENSORS];
} thermal;


static gint
proc_get_critical(char const* sensor_path){
    FILE *state;
    char buf[ 256 ], sstmp [ 100 ];
    char* pstr;

    if(sensor_path == NULL) return -1;

    snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,PROC_THERMAL_TRIP);

    if (!(state = fopen( sstmp, "r"))) {
        g_warning("thermal: cannot open %s", sstmp);
        return -1;
    }

    while( fgets(buf, 256, state) &&
            ! ( pstr = strstr(buf, PROC_TRIP_CRITICAL) ) );
    if( pstr )
    {
        pstr += strlen(PROC_TRIP_CRITICAL);
        while( *pstr && *pstr == ' ' )
            ++pstr;

        pstr[strlen(pstr)-3] = '\0';
        fclose(state);
        return atoi(pstr);
    }

    fclose(state);
    return -1;
}

static gint
proc_get_temperature(char const* sensor_path){
    FILE *state;
    char buf[ 256 ], sstmp [ 100 ];
    char* pstr;

    if(sensor_path == NULL) return -1;

    snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,PROC_THERMAL_TEMPF);

    if (!(state = fopen( sstmp, "r"))) {
        g_warning("thermal: cannot open %s", sstmp);
        return -1;
    }

    while( fgets(buf, 256, state) &&
            ! ( pstr = strstr(buf, "temperature:") ) );
    if( pstr )
    {
        pstr += 12;
        while( *pstr && *pstr == ' ' )
            ++pstr;

        pstr[strlen(pstr)-3] = '\0';
        fclose(state);
        return atoi(pstr);
    }

    fclose(state);
    return -1;
}

static gint _get_reading(const char *path, gboolean quiet)
{
    FILE *state;
    char buf[256];
    char* pstr;

    if (!(state = fopen(path, "r"))) {
        if (!quiet)
            g_warning("thermal: cannot open %s", path);
        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_critical(char const* sensor_path){
    char sstmp [ 100 ];

    if(sensor_path == NULL) return -1;

    snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,SYSFS_THERMAL_TRIP);

    return _get_reading(sstmp, TRUE);
}

static gint
sysfs_get_temperature(char const* sensor_path){
    char sstmp [ 100 ];

    if(sensor_path == NULL) return -1;

    snprintf(sstmp,sizeof(sstmp),"%s%s",sensor_path,SYSFS_THERMAL_TEMPF);

    return _get_reading(sstmp, FALSE);
}

static gint
hwmon_get_critical(char const* sensor_path)
{
    char sstmp [ 100 ];
    int spl;

    if(sensor_path == NULL) return -1;

    spl = strlen(sensor_path) - 6;
    if (spl < 17 || spl > 94)
        return -1;

    snprintf(sstmp, sizeof(sstmp), "%.*s_crit", spl, sensor_path);

    return _get_reading(sstmp, TRUE);
}

static gint
hwmon_get_temperature(char const* sensor_path)
{
    if(sensor_path == NULL) return -1;

    return _get_reading(sensor_path, FALSE);
}

static gint get_temperature(thermal *th, gint *warn)
{
    gint max = -273;
    gint cur, i, w = 0;

    for(i = 0; i < th->numsensors; i++){
        cur = th->get_temperature[i](th->sensor_array[i]);
        if (w == 2) ; /* already warning2 */
        else if (th->not_custom_levels &&
                 th->critical[i] > 0 && cur >= th->critical[i] - 5)
            w = 2;
        else if ((!th->not_custom_levels || th->critical[i] < 0) &&
                 cur >= th->warning2)
            w = 2;
        else if (w == 1) ; /* already warning1 */
        else if (th->not_custom_levels &&
                 th->critical[i] > 0 && cur >= th->critical[i] - 10)
            w = 1;
        else if ((!th->not_custom_levels || th->critical[i] < 0) &&
                 cur >= th->warning1)
            w = 1;
        if (cur > max)
            max = cur;
        th->temperature[i] = cur;
    }
    *warn = w;

    return max;
}

static gint get_critical(thermal *th)
{
    gint min = MAX_AUTOMATIC_CRITICAL_TEMP;
    gint i;

    for(i = 0; i < th->numsensors; i++){
        th->critical[i] = th->get_critical[i](th->sensor_array[i]);
        if (th->critical[i] > 0 && th->critical[i] < min)
            min = th->critical[i];
    }

    return min;
}

static void
update_display(thermal *th)
{
    char buffer [60];
    int i;
    int temp;
    GdkColor color;
    gchar *separator;

    temp = get_temperature(th, &i);
    if (i >= 2)
        color = th->cl_warning2;
    else if (i >= 1)
        color = th->cl_warning1;
    else
        color = th->cl_normal;

    if(temp == -1)
        lxpanel_draw_label_text(th->panel, th->namew, "NA", TRUE, 1, TRUE);
    else
    {
        snprintf(buffer, sizeof(buffer), "%02d", temp);
        lxpanel_draw_label_text_with_color(th->panel, th->namew, buffer, TRUE, 1, &color);
    }

    g_string_truncate(th->tip, 0);
    separator = "";
    for (i = 0; i < th->numsensors; i++){
        g_string_append_printf(th->tip, "%s%s:\t%2d°C", separator, th->sensor_name[i], th->temperature[i]);
        separator = "\n";
    }
    gtk_widget_set_tooltip_text(th->namew, th->tip->str);
}

static gboolean update_display_timeout(gpointer user_data)
{
    if (g_source_is_destroyed(g_main_current_source()))
        return FALSE;
    update_display(user_data);
    return TRUE; /* repeat later */
}

static int
add_sensor(thermal* th, char const* sensor_path, const char *sensor_name,
           GetTempFunc get_temp, GetTempFunc get_crit)
{
    if (th->numsensors + 1 > MAX_NUM_SENSORS){
        g_warning("thermal: Too many sensors (max %d), ignoring '%s'",
                MAX_NUM_SENSORS, sensor_path);
        return -1;
    }

    th->sensor_array[th->numsensors] = g_strdup(sensor_path);
    th->sensor_name[th->numsensors] = g_strdup(sensor_name);
    th->get_critical[th->numsensors] = get_crit;
    th->get_temperature[th->numsensors] = get_temp;
    th->numsensors++;

    g_debug("thermal: Added sensor %s", sensor_path);

    return 0;
}

/* find_sensors():
 *      - 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
find_sensors(thermal* th, char const* directory, char const* subdir_prefix,
             GetTempFunc get_temp, GetTempFunc get_crit)
{
    GDir *sensorsDirectory;
    const char *sensor_name;
    char sensor_path[100];

    if (! (sensorsDirectory = g_dir_open(directory, 0, NULL)))
        return;

    /* Scan the thermal_zone directory for available sensors */
    while ((sensor_name = g_dir_read_name(sensorsDirectory))) {
        if (sensor_name[0] == '.')
            continue;
        if (subdir_prefix) {
            if (strncmp(sensor_name, subdir_prefix, strlen(subdir_prefix)) != 0)
                continue;
        }
        snprintf(sensor_path,sizeof(sensor_path),"%s%s/", directory, sensor_name);
        add_sensor(th, sensor_path, sensor_name, get_temp, get_crit);
    }
    g_dir_close(sensorsDirectory);
}

static gboolean try_hwmon_sensors(thermal* th, const char *path)
{
    GDir *sensorsDirectory;
    const char *sensor_name;
    char sensor_path[100], buf[256];
    FILE *fp;
    gboolean found = FALSE;

    if (!(sensorsDirectory = g_dir_open(path, 0, NULL)))
        return found;

    while ((sensor_name = g_dir_read_name(sensorsDirectory)))
    {
        if (strncmp(sensor_name, "temp", 4) == 0 &&
            strcmp(&sensor_name[5], "_input") == 0)
        {
            snprintf(sensor_path, sizeof(sensor_path), "%s/temp%c_label", path,
                     sensor_name[4]);
            fp = fopen(sensor_path, "r");
            buf[0] = '\0';
            if (fp)
            {
                if (fgets(buf, 256, fp))
                {
                    char *pp = strchr(buf, '\n');
                    if (pp)
                        *pp = '\0';
                }
                fclose(fp);
            }
            snprintf(sensor_path, sizeof(sensor_path), "%s/%s", path, sensor_name);
            add_sensor(th, sensor_path, buf[0] ? buf : sensor_name,
                       hwmon_get_temperature, hwmon_get_critical);
            found = TRUE;
        }
    }
    g_dir_close(sensorsDirectory);
    return found;
}

static void find_hwmon_sensors(thermal* th)
{
    char dir_path[100];
    char *c;
    int i; /* sensor type num, we'll try up to 4 */

    for (i = 0; i < 4; i++)
    {
        snprintf(dir_path, sizeof(dir_path), "/sys/class/hwmon/hwmon%d/device", i);
        if (try_hwmon_sensors(th, dir_path))
            continue;
        /* no sensors found under device/, try parent dir */
        c = strrchr(dir_path, '/');
        *c = '\0';
        try_hwmon_sensors(th, dir_path);
    }
}


static void
remove_all_sensors(thermal *th)
{
    int i;

    g_debug("thermal: Removing all sensors (%d)", th->numsensors);

    for (i = 0; i < th->numsensors; i++)
    {
        g_free(th->sensor_array[i]);
        g_free(th->sensor_name[i]);
    }

    th->numsensors = 0;
}

static void
check_sensors( thermal *th )
{
    // FIXME: scan in opposite order
    find_sensors(th, PROC_THERMAL_DIRECTORY, NULL, proc_get_temperature, proc_get_critical);
    find_sensors(th, SYSFS_THERMAL_DIRECTORY, SYSFS_THERMAL_SUBDIR_PREFIX, sysfs_get_temperature, sysfs_get_critical);
    if (th->numsensors == 0)
        find_hwmon_sensors(th);
    g_info("thermal: Found %d sensors", th->numsensors);
}


static gboolean applyConfig(gpointer p)
{
    thermal *th = lxpanel_plugin_get_data(p);
    int critical;
    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);

    remove_all_sensors(th);
    /* FIXME: support wildcards in th->sensor */
    if(th->sensor == NULL) th->auto_sensor = TRUE;
    if(th->auto_sensor) check_sensors(th);
    else if (strncmp(th->sensor, "/sys/", 5) != 0)
        add_sensor(th, th->sensor, th->sensor, proc_get_temperature, proc_get_critical);
    else if (strncmp(th->sensor, "/sys/class/hwmon/", 17) != 0)
        add_sensor(th, th->sensor, th->sensor, sysfs_get_temperature, sysfs_get_critical);
    else
        add_sensor(th, th->sensor, th->sensor, hwmon_get_temperature, hwmon_get_critical);

    critical = get_critical(th);

    if(th->not_custom_levels){
        th->warning1 = critical - 10;
        th->warning2 = critical - 5;
    }

    config_group_set_string(th->settings, "NormalColor", th->str_cl_normal);
    config_group_set_string(th->settings, "Warning1Color", th->str_cl_warning1);
    config_group_set_string(th->settings, "Warning2Color", th->str_cl_warning2);
    config_group_set_int(th->settings, "AutomaticLevels", th->not_custom_levels);
    /* TODO: clean obsolete setting
    config_setting_remove(th->settings, "CustomLevels"); */
    config_group_set_int(th->settings, "Warning1Temp", th->warning1);
    config_group_set_int(th->settings, "Warning2Temp", th->warning2);
    config_group_set_int(th->settings, "AutomaticSensor", th->auto_sensor);
    config_group_set_string(th->settings, "Sensor", th->sensor);
    RET(FALSE);
}

static void
thermal_destructor(gpointer user_data)
{
  thermal *th = (thermal *)user_data;

  ENTER;
  remove_all_sensors(th);
  g_string_free(th->tip, TRUE);
  g_free(th->sensor);
  g_free(th->str_cl_normal);
  g_free(th->str_cl_warning1);
  g_free(th->str_cl_warning2);
  g_source_remove(th->timer);
  g_free(th);
  RET();
}

static GtkWidget *
thermal_constructor(LXPanel *panel, config_setting_t *settings)
{
    thermal *th;
    GtkWidget *p;
    const char *tmp;

    ENTER;
    th = g_new0(thermal, 1);
    th->panel = panel;
    th->settings = settings;

    p = gtk_event_box_new();
    lxpanel_plugin_set_data(p, th, thermal_destructor);
    gtk_widget_set_has_window(p, FALSE);

    th->namew = gtk_label_new("ww");
    gtk_container_add(GTK_CONTAINER(p), th->namew);

    th->tip = g_string_new(NULL);

    /* 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;

    if (config_setting_lookup_string(settings, "NormalColor", &tmp))
        th->str_cl_normal = g_strdup(tmp);
    if (config_setting_lookup_string(settings, "Warning1Color", &tmp))
        th->str_cl_warning1 = g_strdup(tmp);
    if (config_setting_lookup_string(settings, "Warning2Color", &tmp))
        th->str_cl_warning2 = g_strdup(tmp);
    config_setting_lookup_int(settings, "AutomaticSensor", &th->auto_sensor);
    /* backward compatibility for wrong variable */
    config_setting_lookup_int(settings, "CustomLevels", &th->not_custom_levels);
    config_setting_lookup_int(settings, "AutomaticLevels", &th->not_custom_levels);
    if (config_setting_lookup_string(settings, "Sensor", &tmp))
        th->sensor = g_strdup(tmp);
    config_setting_lookup_int(settings, "Warning1Temp", &th->warning1);
    config_setting_lookup_int(settings, "Warning2Temp", &th->warning2);

    if(!th->str_cl_normal)
        th->str_cl_normal = g_strdup("#00ff00");
    if(!th->str_cl_warning1)
        th->str_cl_warning1 = g_strdup("#fff000");
    if(!th->str_cl_warning2)
        th->str_cl_warning2 = g_strdup("#ff0000");

    applyConfig(p);

    gtk_widget_show(th->namew);

    update_display(th);
    th->timer = g_timeout_add_seconds(3, (GSourceFunc) update_display_timeout, (gpointer)th);

    RET(p);
}

static GtkWidget *config(LXPanel *panel, GtkWidget *p)
{
    ENTER;

    GtkWidget *dialog;
    thermal *th = lxpanel_plugin_get_data(p);
    dialog = lxpanel_generic_config_dlg(_("Temperature Monitor"),
            panel, applyConfig, p,
            _("Normal color"), &th->str_cl_normal, CONF_TYPE_STR,
            _("Warning1 color"), &th->str_cl_warning1, CONF_TYPE_STR,
            _("Warning2 color"), &th->str_cl_warning2, CONF_TYPE_STR,
            _("Automatic sensor location"), &th->auto_sensor, CONF_TYPE_BOOL, // FIXME: if off, disable next one
            _("Sensor"), &th->sensor, CONF_TYPE_STR, // FIXME: create a list to select instead
            _("Automatic temperature levels"), &th->not_custom_levels, CONF_TYPE_BOOL, // FIXME: if off, disable two below
            _("Warning1 temperature"), &th->warning1, CONF_TYPE_INT,
            _("Warning2 temperature"), &th->warning2, CONF_TYPE_INT,
            NULL);

    RET(dialog);
}

FM_DEFINE_MODULE(lxpanel_gtk, thermal)

LXPanelPluginInit fm_module_init_lxpanel_gtk = {
    .name = N_("Temperature Monitor"),
    .description = N_("Display system temperature"),

    .new_instance = thermal_constructor,
    .config = config,
};


/* vim: set sw=4 sts=4 et : */