File: middlelayer.c

package info (click to toggle)
xfce4-sensors-plugin 0.10.99.5~svn-r4998-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,032 kB
  • ctags: 307
  • sloc: sh: 9,815; ansic: 3,174; makefile: 100
file content (240 lines) | stat: -rw-r--r-- 5,971 bytes parent folder | download
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
/*
 *      middlelayer.c
 *
 *      Copyright 2006, 2007 Fabian Nowak <timytery@arcor.de>
 *
 *      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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif


#ifdef HAVE_LINUX
#include <sys/utsname.h>
#endif


#include "middlelayer.h"

#ifdef HAVE_LIBSENSORS
    #include "lmsensors.h"
#endif
#ifdef HAVE_HDDTEMP
    #include "hddtemp.h"
#endif
#ifdef HAVE_ACPI
    #include "acpi.h"
#endif

int
initialize_all (GPtrArray **chips, gboolean *suppressmessage)
{
    int res = 0;

    TRACE ("enters initialize_all");

    *chips = g_ptr_array_new();

     #ifdef HAVE_LIBSENSORS
    res += initialize_libsensors (*chips);
    #endif

    #ifdef HAVE_HDDTEMP
    res += initialize_hddtemp (*chips, suppressmessage);
    #endif

    #ifdef HAVE_ACPI
    res += initialize_ACPI (*chips);
    #endif

    TRACE ("leaves initialize_all, chips->len=%d", (*chips)->len);

    return res;
}


void
refresh_chip (gpointer chip, gpointer data)
{
    t_chip *c;

    g_assert (chip!=NULL);

    TRACE ("enters refresh_chip");

    c = (t_chip*) chip;

    #ifdef HAVE_ACPI
        if (c->type==ACPI) {
            g_ptr_array_foreach (c->chip_features, refresh_acpi, NULL );
            return;
        }
    #endif

    #ifdef HAVE_LIBSENSORS
        if (c->type==LMSENSOR) {
            g_ptr_array_foreach (c->chip_features, refresh_lmsensors, NULL );
        return;
        }
    #endif

    #ifdef HAVE_HDDTEMP
        if (c->type==HDD) {
            g_ptr_array_foreach (c->chip_features, refresh_hddtemp, NULL );
        return;
        }
    #endif

    TRACE ("leaves refresh_chip");
}


void
refresh_all_chips (GPtrArray *chips )
{
    TRACE ("enters refresh_all_chips");

    g_ptr_array_foreach (chips, refresh_chip, NULL );

    TRACE ("leaves refresh_all_chips");
}


void
categorize_sensor_type (t_chipfeature* chipfeature)
{
    TRACE ("enters categorize_sensor_type");

    if ( strstr(chipfeature->name, "Temp")!=NULL
    || strstr(chipfeature->name, "temp")!=NULL ) {
        chipfeature->class = TEMPERATURE;
        chipfeature->min_value = 0.0;
        chipfeature->max_value = 80.0;
    } else if ( strstr(chipfeature->name, "VCore")!=NULL
    || strstr(chipfeature->name, "3V")!=NULL
    || strstr(chipfeature->name, "5V")!=NULL
    || strstr(chipfeature->name, "12V")!=NULL ) {
        chipfeature->class = VOLTAGE;
        chipfeature->min_value = 1.0;
        chipfeature->max_value = 12.2;
    } else if ( strstr(chipfeature->name, "Fan")!=NULL
    || strstr(chipfeature->name, "fan")!=NULL ) {
        chipfeature->class = SPEED;
        chipfeature->min_value = 1000.0;
        chipfeature->max_value = 3500.0;
    } else if ( strstr(chipfeature->name, "alarm")!=NULL
    || strstr(chipfeature->name, "Alarm")!=NULL ) {
        chipfeature->class = STATE;
        chipfeature->min_value = 0.0;
        chipfeature->max_value = 1.0;
    } else {
        chipfeature->class = OTHER;
        chipfeature->min_value = 0.0;
        chipfeature->max_value = 7000.0;
    }

    TRACE ("leaves categorize_sensor_type");
}


int
sensor_get_value (t_chip *chip, int number, double *value)
{
    t_chipfeature *feature;
    /* TRACE ("enters sensor_get_value %d", number); */

    g_assert (chip!=NULL);

    if (chip->type==LMSENSOR ) {
        #ifdef HAVE_LIBSENSORS
            return sensors_get_feature_wrapper (chip->chip_name, number, value);
        #else
            return -1;
        #endif
    }
    if (chip->type==HDD ) {
        #ifdef HAVE_HDDTEMP
            g_assert (number<chip->num_features);
            feature = (t_chipfeature *) g_ptr_array_index (chip->chip_features, number);
            g_assert (feature!=NULL);
            *value = get_hddtemp_value (feature->devicename, NULL);
            if (*value==ZERO_KELVIN) {
                return NO_VALID_HDDTEMP;
            }
            return 0;
        #else
            return -1;
        #endif
    }
    if (chip->type==ACPI ) {
        #ifdef HAVE_ACPI
            g_assert (number<chip->num_features);
            feature = (t_chipfeature *) g_ptr_array_index (chip->chip_features, number);
            g_assert (feature!=NULL);
            refresh_acpi ((gpointer) feature, NULL);
            *value = feature->raw_value;
            return 0; /* HERE    I    AM,    I    WANNA    BE    FIXED    */
        #else
            return -1;
        #endif
    }
    else {
        feature = NULL;
        return -1;
    }
}


void
free_chipfeature (gpointer chipfeature, gpointer data)
{
    t_chipfeature *cf;
    cf = (t_chipfeature *) chipfeature;

    g_free (cf->name);
    g_free (cf->devicename);
    g_free (cf->formatted_value);
    g_free (cf->color);
}


void
free_chip (gpointer chip, gpointer data)
{
    t_chip *c;
    c = (t_chip *) chip;

    g_free (c->sensorId);
    g_free (c->description);
    #ifdef HAVE_LIBSENSORS
    if (c->type==LMSENSOR) {
        free_lmsensors_chip (chip);
    }
    #endif
    /* g_free (c->chip_name); */   /* is a _copied_ structure of libsensors */
    g_ptr_array_foreach (c->chip_features, free_chipfeature, NULL);
    g_ptr_array_free (c->chip_features, TRUE);
}


void
sensor_interface_cleanup()
{
    #ifdef HAVE_LIBSENSORS
        sensors_cleanup();
    #endif
}