summaryrefslogtreecommitdiff
path: root/plugins/ibm-acpi/ibm-acpi-plugin.c
blob: 8bf26839df82e512923a9cd2dcc7ba1ee64c39e1 (plain)
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
/*
 * Copyright (C) 2005-2009 Alex Murray <murray.alex@gmail.com>
 *
 * 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
 */

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

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif /* HAVE_STDIO_H */

#include <glib.h>
#include <glib/gi18n.h>
#include "ibm-acpi-plugin.h"

const gchar *plugin_name = "ibm-acpi";

#define IBM_ACPI_TEMPERATURE_FILE "/proc/acpi/ibm/thermal"
#define IBM_ACPI_FAN_FILE "/proc/acpi/ibm/fan"

/* sensor id's */
#define CPU "CPU"
#define MPCI "mPCI"
#define HDD "HDD"
#define GPU "GPU"
#define BAT0 "BAT0"
#define BAT1 "BAT1"
#define FAN "FAN"


enum {
        IBM_ACPI_FILE_OPEN_ERROR,
        IBM_ACPI_FILE_READ_ERROR
};


static void ibm_acpi_plugin_setup_manually(GList **sensors) {
	gchar *label;

        if (g_file_test(IBM_ACPI_TEMPERATURE_FILE, G_FILE_TEST_EXISTS)) {
                /* with Ibm_Acpi have 8 fixed sensors, all accessed
                   from the IBM_ACPI_TEMPERATURE_FILE */
                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_TEMPERATURE_FILE,
                                                 CPU,
                                                 _(CPU),
                                                 TEMP_SENSOR,
                                                 TRUE,
                                                 CPU_ICON,
                                                 DEFAULT_GRAPH_COLOR);
                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_TEMPERATURE_FILE,
                                                 MPCI,
                                                 _("MiniPCI"),
                                                 TEMP_SENSOR,
                                                 FALSE,
                                                 GENERIC_ICON,
                                                 DEFAULT_GRAPH_COLOR);

                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_TEMPERATURE_FILE,
                                                 HDD,
                                                 _(HDD),
                                                 TEMP_SENSOR,	
                                                 FALSE,
                                                 HDD_ICON,
                                                 DEFAULT_GRAPH_COLOR);
                
                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_TEMPERATURE_FILE,
                                                 GPU,
                                                 _(GPU),
                                                 TEMP_SENSOR,
                                                 FALSE,
                                                 GPU_ICON,
                                                 DEFAULT_GRAPH_COLOR);
                
		label = g_strdup_printf("%s %d", _("Battery"), 0);
                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_TEMPERATURE_FILE,
                                                 BAT0,
                                                 label,
                                                 TEMP_SENSOR,
                                                 FALSE,
                                                 BATTERY_ICON,
                                                 DEFAULT_GRAPH_COLOR);
                
		g_free(label);
		label = g_strdup_printf("%s %d", _("Battery"), 0);
                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_TEMPERATURE_FILE,
                                                 BAT1,
                                                 label,
                                                 TEMP_SENSOR,
                                                 FALSE,
                                                 BATTERY_ICON,
                                                 DEFAULT_GRAPH_COLOR);

		g_free(label);
	}

        if (g_file_test(IBM_ACPI_FAN_FILE, G_FILE_TEST_EXISTS)) {
                sensors_applet_plugin_add_sensor(sensors,
                                                 IBM_ACPI_FAN_FILE,
                                                 FAN,
                                                 _("Fan"),
                                                 FAN_SENSOR,
                                                 FALSE,
                                                 FAN_ICON,
                                                 DEFAULT_GRAPH_COLOR);

        }
}
/* to be called externally to setup for ibm_acpi sensors */
GList *ibm_acpi_plugin_init(void) {
        GList *sensors = NULL;
        
       ibm_acpi_plugin_setup_manually(&sensors);

       return sensors;
                
}

gdouble ibm_acpi_plugin_get_sensor_value(const gchar *path, 
                                         const gchar *id, 
                                         SensorType type,
                                         GError **error) {

        /* to open and access the value of each sensor */
        FILE *fp;
        gint sensor_value;
        gint cpu_temp, minipci_temp, hdd_temp, gpu_temp, bat0_temp, bat1_temp, unknown0, unknown1;
        gint fan_speed;

        if (NULL == (fp = fopen(path, "r"))) {
                g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, IBM_ACPI_FILE_OPEN_ERROR, "Error opening sensor device file %s", path);
                return -1.0;
        }

        switch (type) {
        case TEMP_SENSOR:
                if (fscanf(fp, "temperatures:   %d %d %d %d %d %d %d %d", &cpu_temp, &minipci_temp, &hdd_temp, &gpu_temp, &bat0_temp, &unknown0, &bat1_temp, &unknown1) != 8) {
                        g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, IBM_ACPI_FILE_READ_ERROR, "Error reading from sensor device file %s", path);
                        fclose(fp);
                        return -1.0;
                }

                if (g_ascii_strcasecmp(id, CPU) == 0) {
                        sensor_value = cpu_temp;
                } else if (g_ascii_strcasecmp(id, MPCI) == 0) {
                        sensor_value = minipci_temp;
                } else if (g_ascii_strcasecmp(id, HDD) == 0) {
                        sensor_value = hdd_temp;
                } else if (g_ascii_strcasecmp(id, GPU) == 0) {
                        sensor_value = gpu_temp;
                } else if (g_ascii_strcasecmp(id, BAT0) == 0) {
                        sensor_value = bat0_temp;
                } else if (g_ascii_strcasecmp(id, BAT1) == 0) {
                        sensor_value = bat1_temp;
                }
                break;
        case FAN_SENSOR:
                fscanf(fp, "%*[^\n]");   /* Skip to the End of the Line */
                fscanf(fp, "%*1[\n]");   /* Skip One Newline */
                if (fscanf(fp, "speed:   %d", &fan_speed) != 1) {
                        g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, IBM_ACPI_FILE_READ_ERROR, "Error reading from sensor device file %s", path);
                        fclose(fp);
                        return -1.0;
                }

                if (g_ascii_strcasecmp(id, "FAN") == 0) {
                        sensor_value = fan_speed;
                }
                break;

        default:
                g_error("Unknown sensor type passed as parameter to ibm-acpi sensor interface, cannot get value for this sensor");
                g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, IBM_ACPI_FILE_READ_ERROR, "Error reading from sensor device file %s", path);
                fclose(fp);
                return -1.0;
        }

        fclose(fp);

        return (gdouble)sensor_value;

}

const gchar *sensors_applet_plugin_name(void) 
{
        return plugin_name;
}

GList *sensors_applet_plugin_init(void) 
{
        return ibm_acpi_plugin_init();
}

gdouble sensors_applet_plugin_get_sensor_value(const gchar *path, 
                                                const gchar *id, 
                                                SensorType type,
                                                GError **error) {
        return ibm_acpi_plugin_get_sensor_value(path, id, type, error);
}