7 # include <sys/types.h>
8 # include <sys/sysctl.h>
12 #include <Ecore_File.h>
14 #include "e_mod_main_private.h"
16 static int sensor_type = SENSOR_TYPE_NONE;
17 static char *sensor_name = NULL;
18 static int poll_interval = 32;
19 static int cur_poll_interval = 32;
21 static char *sensor_path = NULL;
25 static Ecore_Poller *poller = NULL;
28 static void init(void);
29 static int check(void);
30 static Eina_Bool poll_cb(void *data);
33 temperature_get_bus_files(const char* bus)
38 char busdir[PATH_MAX];
43 snprintf(busdir, sizeof(busdir), "/sys/bus/%s/devices", bus);
44 /* Look through all the devices for the given bus. */
45 therms = ecore_file_ls(busdir);
47 EINA_LIST_FREE(therms, name)
52 /* Search each device for temp*_input, these should be
53 * temperature devices. */
54 snprintf(path, sizeof(path),"%s/%s", busdir, name);
55 files = ecore_file_ls(path);
56 EINA_LIST_FREE(files, file)
58 if ((!strncmp("temp", file, 4)) &&
59 (!strcmp("_input", &file[strlen(file) - 6])))
63 snprintf(path, sizeof(path),
64 "%s/%s/%s", busdir, name, file);
66 if (f) result = eina_list_append(result, f);
84 if ((!sensor_type) || ((!sensor_name) || (sensor_name[0] == 0)))
86 if (sensor_name) free(sensor_name);
87 if (sensor_path) free(sensor_path);
90 /* TODO: FreeBSD can also have more temperature sensors! */
91 sensor_type = SENSOR_TYPE_FREEBSD;
92 sensor_name = strdup("tz0");
94 therms = ecore_file_ls("/proc/acpi/thermal_zone");
99 name = eina_list_data_get(therms);
100 sensor_type = SENSOR_TYPE_LINUX_ACPI;
101 sensor_name = strdup(name);
103 eina_list_free(therms);
107 eina_list_free(therms);
108 if (ecore_file_exists("/proc/omnibook/temperature"))
110 sensor_type = SENSOR_TYPE_OMNIBOOK;
111 sensor_name = strdup("dummy");
113 else if (ecore_file_exists("/sys/devices/temperatures/sensor1_temperature"))
115 sensor_type = SENSOR_TYPE_LINUX_PBOOK;
116 sensor_name = strdup("dummy");
118 else if (ecore_file_exists("/sys/devices/temperatures/cpu_temperature"))
120 sensor_type = SENSOR_TYPE_LINUX_MACMINI;
121 sensor_name = strdup("dummy");
123 else if (ecore_file_exists("/sys/devices/platform/coretemp.0/temp1_input"))
125 sensor_type = SENSOR_TYPE_LINUX_INTELCORETEMP;
126 sensor_name = strdup("dummy");
128 else if (ecore_file_exists("/sys/devices/platform/thinkpad_hwmon/temp1_input"))
130 sensor_type = SENSOR_TYPE_LINUX_THINKPAD;
131 sensor_name = strdup("dummy");
136 therms = temperature_get_bus_files("i2c");
141 if ((name = eina_list_data_get(therms)))
143 if (ecore_file_exists(name))
147 snprintf(path, sizeof(path),
148 "%s", ecore_file_file_get(name));
150 if (len > 6) path[len - 6] = '\0';
151 sensor_type = SENSOR_TYPE_LINUX_I2C;
152 sensor_path = strdup(name);
153 sensor_name = strdup(path);
154 printf("sensor type = i2c\n"
156 "sensor name = %s\n",
157 sensor_path, sensor_name);
160 eina_list_free(therms);
165 therms = temperature_get_bus_files("pci");
170 if ((name = eina_list_data_get(therms)))
172 if (ecore_file_exists(name))
176 snprintf(path, sizeof(path),
177 "%s", ecore_file_file_get(name));
179 if (len > 6) path[len - 6] = '\0';
180 sensor_type = SENSOR_TYPE_LINUX_PCI;
181 sensor_path = strdup(name);
182 sensor_name = strdup(path);
183 printf("sensor type = pci\n"
185 "sensor name = %s\n",
186 sensor_path, sensor_name);
189 eina_list_free(therms);
196 if ((sensor_type) && (sensor_name) && (!sensor_path))
202 case SENSOR_TYPE_NONE:
204 case SENSOR_TYPE_FREEBSD:
206 snprintf(path, sizeof(path), "hw.acpi.thermal.%s.temperature",
208 sensor_path = strdup(path);
210 sysctlnametomib(sensor_path, mib, &len);
213 case SENSOR_TYPE_OMNIBOOK:
214 sensor_path = strdup("/proc/omnibook/temperature");
216 case SENSOR_TYPE_LINUX_MACMINI:
217 sensor_path = strdup("/sys/devices/temperatures/cpu_temperature");
219 case SENSOR_TYPE_LINUX_PBOOK:
220 sensor_path = strdup("/sys/devices/temperatures/sensor1_temperature");
222 case SENSOR_TYPE_LINUX_INTELCORETEMP:
223 sensor_path = strdup("/sys/devices/platform/coretemp.0/temp1_input");
225 case SENSOR_TYPE_LINUX_THINKPAD:
226 sensor_path = strdup("/sys/devices/platform/thinkpad_hwmon/temp1_input");
228 case SENSOR_TYPE_LINUX_I2C:
229 therms = ecore_file_ls("/sys/bus/i2c/devices");
231 EINA_LIST_FREE(therms, name)
233 snprintf(path, sizeof(path),
234 "/sys/bus/i2c/devices/%s/%s_input",
236 if (ecore_file_exists(path))
238 sensor_path = strdup(path);
239 /* We really only care about the first
240 * one for the default. */
246 case SENSOR_TYPE_LINUX_PCI:
247 therms = ecore_file_ls("/sys/bus/pci/devices");
249 EINA_LIST_FREE(therms, name)
251 snprintf(path, sizeof(path),
252 "/sys/bus/pci/devices/%s/%s_input",
254 if (ecore_file_exists(path))
256 sensor_path = strdup(path);
257 /* We really only care about the first
258 * one for the default. */
264 case SENSOR_TYPE_LINUX_ACPI:
265 snprintf(path, sizeof(path),
266 "/proc/acpi/thermal_zone/%s/temperature", sensor_name);
267 sensor_path = strdup(path);
285 /* TODO: Make standard parser. Seems to be two types of temperature string:
286 * - Somename: <temp> C
291 case SENSOR_TYPE_NONE:
292 /* TODO: Slow down poller? */
294 case SENSOR_TYPE_FREEBSD:
297 if (sysctl(mib, 5, &ftemp, &len, NULL, 0) != -1)
299 temp = (ftemp - 2732) / 10;
306 case SENSOR_TYPE_OMNIBOOK:
307 f = fopen(sensor_path, "r");
312 fgets(buf, sizeof(buf), f);
313 buf[sizeof(buf) - 1] = 0;
314 if (sscanf(buf, "%s %s %i", dummy, dummy, &temp) == 3)
323 case SENSOR_TYPE_LINUX_MACMINI:
324 case SENSOR_TYPE_LINUX_PBOOK:
325 f = fopen(sensor_path, "rb");
328 fgets(buf, sizeof(buf), f);
330 buf[sizeof(buf) - 1] = 0;
331 if (sscanf(buf, "%i", &temp) == 1)
339 case SENSOR_TYPE_LINUX_INTELCORETEMP:
340 case SENSOR_TYPE_LINUX_I2C:
341 case SENSOR_TYPE_LINUX_THINKPAD:
342 f = fopen(sensor_path, "r");
345 fgets(buf, sizeof(buf), f);
346 buf[sizeof(buf) - 1] = 0;
348 /* actually read the temp */
349 if (sscanf(buf, "%i", &temp) == 1)
360 case SENSOR_TYPE_LINUX_PCI:
361 f = fopen(sensor_path, "r");
364 fgets(buf, sizeof(buf), f);
365 buf[sizeof(buf) - 1] = 0;
367 /* actually read the temp */
368 if (sscanf(buf, "%i", &temp) == 1)
379 case SENSOR_TYPE_LINUX_ACPI:
380 f = fopen(sensor_path, "r");
385 fgets(buf, sizeof(buf), f);
386 buf[sizeof(buf) - 1] = 0;
388 p = strchr(buf, ':');
392 while (*p == ' ') p++;
406 if (ret) return temp;
410 sensor_type = SENSOR_TYPE_NONE;
411 if (sensor_name) free(sensor_name);
413 if (sensor_path) free(sensor_path);
424 pp = cur_poll_interval;
427 cur_poll_interval /= 2;
428 if (cur_poll_interval < 4) cur_poll_interval = 4;
432 cur_poll_interval *= 2;
433 if (cur_poll_interval > 256) cur_poll_interval = 256;
435 /* adapt polling based on if temp changes - every time it changes,
436 * halve the time between polls, otherwise double it, between 4 and
438 if (pp != cur_poll_interval)
440 if (poller) ecore_poller_del(poller);
441 poller = ecore_poller_add(ECORE_POLLER_CORE, cur_poll_interval,
446 if (t == -999) printf("ERROR\n");
447 else printf("%i\n", t);
451 return ECORE_CALLBACK_RENEW;
455 main(int argc, char *argv[])
459 printf("ARGS INCORRECT!\n");
462 sensor_type = atoi(argv[1]);
463 sensor_name = strdup(argv[2]);
464 if (!strcmp(sensor_name, "-null-"))
469 poll_interval = atoi(argv[3]);
470 cur_poll_interval = poll_interval;
476 if (poller) ecore_poller_del(poller);
477 poller = ecore_poller_add(ECORE_POLLER_CORE, cur_poll_interval,
481 ecore_main_loop_begin();