10 # include <sys/types.h>
11 # include <sys/sysctl.h>
15 #include <sys/param.h>
16 #include <sys/sysctl.h>
17 #include <sys/sensors.h>
24 #include <Ecore_File.h>
26 #include "e_mod_main_private.h"
28 static int sensor_type = SENSOR_TYPE_NONE;
29 static char *sensor_name = NULL;
30 static int poll_interval = 32;
31 static int cur_poll_interval = 32;
33 static char *sensor_path = NULL;
34 #if defined (__FreeBSD__) || defined (__OpenBSD__)
40 static struct sensordev snsrdev;
41 static size_t sdlen = sizeof(snsrdev);
42 static struct sensor snsr;
43 static size_t slen = sizeof(snsr);
46 static Ecore_Poller *poller = NULL;
49 static void init(void);
50 static int check(void);
51 static Eina_Bool poll_cb(void *data);
54 temperature_get_bus_files(const char* bus)
59 char busdir[PATH_MAX];
64 snprintf(busdir, sizeof(busdir), "/sys/bus/%s/devices", bus);
65 /* Look through all the devices for the given bus. */
66 therms = ecore_file_ls(busdir);
68 EINA_LIST_FREE(therms, name)
73 /* Search each device for temp*_input, these should be
74 * temperature devices. */
75 snprintf(path, sizeof(path),"%s/%s", busdir, name);
76 files = ecore_file_ls(path);
77 EINA_LIST_FREE(files, file)
79 if ((!strncmp("temp", file, 4)) &&
80 (!strcmp("_input", &file[strlen(file) - 6])))
84 snprintf(path, sizeof(path),
85 "%s/%s/%s", busdir, name, file);
87 if (f) result = eina_list_append(result, f);
105 if ((!sensor_type) || ((!sensor_name) || (sensor_name[0] == 0)))
107 if (sensor_name) free(sensor_name);
108 if (sensor_path) free(sensor_path);
111 /* TODO: FreeBSD can also have more temperature sensors! */
112 sensor_type = SENSOR_TYPE_FREEBSD;
113 sensor_name = strdup("tz0");
118 for (dev = 0; ; dev++) {
120 if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1) {
121 if (errno == ENOENT) /* no further sensors */
126 if (strcmp(snsrdev.xname, "cpu0") == 0) {
127 sensor_type = SENSOR_TYPE_OPENBSD;
128 sensor_name = strdup("cpu0");
133 therms = ecore_file_ls("/proc/acpi/thermal_zone");
138 name = eina_list_data_get(therms);
139 sensor_type = SENSOR_TYPE_LINUX_ACPI;
140 sensor_name = strdup(name);
142 eina_list_free(therms);
146 eina_list_free(therms);
147 therms = ecore_file_ls("/sys/class/thermal");
153 EINA_LIST_FOREACH(therms, l, name)
155 if (!strncmp(name, "thermal", 7))
157 sensor_type = SENSOR_TYPE_LINUX_SYS;
158 sensor_name = strdup(name);
159 eina_list_free(therms);
164 if (therms) eina_list_free(therms);
168 if (ecore_file_exists("/proc/omnibook/temperature"))
170 sensor_type = SENSOR_TYPE_OMNIBOOK;
171 sensor_name = strdup("dummy");
173 else if (ecore_file_exists("/sys/devices/temperatures/sensor1_temperature"))
175 sensor_type = SENSOR_TYPE_LINUX_PBOOK;
176 sensor_name = strdup("dummy");
178 else if (ecore_file_exists("/sys/devices/temperatures/cpu_temperature"))
180 sensor_type = SENSOR_TYPE_LINUX_MACMINI;
181 sensor_name = strdup("dummy");
183 else if (ecore_file_exists("/sys/devices/platform/coretemp.0/temp1_input"))
185 sensor_type = SENSOR_TYPE_LINUX_INTELCORETEMP;
186 sensor_name = strdup("dummy");
188 else if (ecore_file_exists("/sys/devices/platform/thinkpad_hwmon/temp1_input"))
190 sensor_type = SENSOR_TYPE_LINUX_THINKPAD;
191 sensor_name = strdup("dummy");
196 therms = temperature_get_bus_files("i2c");
201 if ((name = eina_list_data_get(therms)))
203 if (ecore_file_exists(name))
207 snprintf(path, sizeof(path),
208 "%s", ecore_file_file_get(name));
210 if (len > 6) path[len - 6] = '\0';
211 sensor_type = SENSOR_TYPE_LINUX_I2C;
212 sensor_path = strdup(name);
213 sensor_name = strdup(path);
214 printf("sensor type = i2c\n"
216 "sensor name = %s\n",
217 sensor_path, sensor_name);
220 eina_list_free(therms);
225 therms = temperature_get_bus_files("pci");
230 if ((name = eina_list_data_get(therms)))
232 if (ecore_file_exists(name))
236 snprintf(path, sizeof(path),
237 "%s", ecore_file_file_get(name));
239 if (len > 6) path[len - 6] = '\0';
240 sensor_type = SENSOR_TYPE_LINUX_PCI;
241 sensor_path = strdup(name);
242 sensor_name = strdup(path);
243 printf("sensor type = pci\n"
245 "sensor name = %s\n",
246 sensor_path, sensor_name);
249 eina_list_free(therms);
257 if ((sensor_type) && (sensor_name) && (!sensor_path))
263 case SENSOR_TYPE_NONE:
265 case SENSOR_TYPE_FREEBSD:
267 snprintf(path, sizeof(path), "hw.acpi.thermal.%s.temperature",
269 sensor_path = strdup(path);
271 sysctlnametomib(sensor_path, mib, &len);
274 case SENSOR_TYPE_OPENBSD:
276 for (numt = 0; numt < snsrdev.maxnumt[SENSOR_TEMP]; numt++) {
279 if (sysctl(mib, 5, &snsr, &slen, NULL, 0) == -1)
281 if (slen > 0 && (snsr.flags & SENSOR_FINVALID) == 0) {
288 case SENSOR_TYPE_OMNIBOOK:
289 sensor_path = strdup("/proc/omnibook/temperature");
291 case SENSOR_TYPE_LINUX_MACMINI:
292 sensor_path = strdup("/sys/devices/temperatures/cpu_temperature");
294 case SENSOR_TYPE_LINUX_PBOOK:
295 sensor_path = strdup("/sys/devices/temperatures/sensor1_temperature");
297 case SENSOR_TYPE_LINUX_INTELCORETEMP:
298 sensor_path = strdup("/sys/devices/platform/coretemp.0/temp1_input");
300 case SENSOR_TYPE_LINUX_THINKPAD:
301 sensor_path = strdup("/sys/devices/platform/thinkpad_hwmon/temp1_input");
303 case SENSOR_TYPE_LINUX_I2C:
304 therms = ecore_file_ls("/sys/bus/i2c/devices");
306 EINA_LIST_FREE(therms, name)
308 snprintf(path, sizeof(path),
309 "/sys/bus/i2c/devices/%s/%s_input",
311 if (ecore_file_exists(path))
313 sensor_path = strdup(path);
314 /* We really only care about the first
315 * one for the default. */
321 case SENSOR_TYPE_LINUX_PCI:
322 therms = ecore_file_ls("/sys/bus/pci/devices");
324 EINA_LIST_FREE(therms, name)
326 snprintf(path, sizeof(path),
327 "/sys/bus/pci/devices/%s/%s_input",
329 if (ecore_file_exists(path))
331 sensor_path = strdup(path);
332 /* We really only care about the first
333 * one for the default. */
339 case SENSOR_TYPE_LINUX_ACPI:
340 snprintf(path, sizeof(path),
341 "/proc/acpi/thermal_zone/%s/temperature", sensor_name);
342 sensor_path = strdup(path);
344 case SENSOR_TYPE_LINUX_SYS:
345 snprintf(path, sizeof(path),
346 "/sys/class/thermal/thermal/%s/temp", sensor_name);
347 sensor_path = strdup(path);
367 /* TODO: Make standard parser. Seems to be two types of temperature string:
368 * - Somename: <temp> C
373 case SENSOR_TYPE_NONE:
374 /* TODO: Slow down poller? */
376 case SENSOR_TYPE_FREEBSD:
379 if (sysctl(mib, 5, &ftemp, &len, NULL, 0) != -1)
381 temp = (ftemp - 2732) / 10;
388 case SENSOR_TYPE_OPENBSD:
390 if (sysctl(mib, 5, &snsr, &slen, NULL, 0) != -1) {
391 temp = (snsr.value - 273150000) / 1000000.0;
398 case SENSOR_TYPE_OMNIBOOK:
399 f = fopen(sensor_path, "r");
404 fgets(buf, sizeof(buf), f);
405 buf[sizeof(buf) - 1] = 0;
406 if (sscanf(buf, "%s %s %i", dummy, dummy, &temp) == 3)
415 case SENSOR_TYPE_LINUX_MACMINI:
416 case SENSOR_TYPE_LINUX_PBOOK:
417 f = fopen(sensor_path, "rb");
420 fgets(buf, sizeof(buf), f);
422 buf[sizeof(buf) - 1] = 0;
423 if (sscanf(buf, "%i", &temp) == 1)
431 case SENSOR_TYPE_LINUX_INTELCORETEMP:
432 case SENSOR_TYPE_LINUX_I2C:
433 case SENSOR_TYPE_LINUX_THINKPAD:
434 f = fopen(sensor_path, "r");
437 fgets(buf, sizeof(buf), f);
438 buf[sizeof(buf) - 1] = 0;
440 /* actually read the temp */
441 if (sscanf(buf, "%i", &temp) == 1)
452 case SENSOR_TYPE_LINUX_PCI:
453 f = fopen(sensor_path, "r");
456 fgets(buf, sizeof(buf), f);
457 buf[sizeof(buf) - 1] = 0;
459 /* actually read the temp */
460 if (sscanf(buf, "%i", &temp) == 1)
471 case SENSOR_TYPE_LINUX_ACPI:
472 f = fopen(sensor_path, "r");
477 fgets(buf, sizeof(buf), f);
478 buf[sizeof(buf) - 1] = 0;
480 p = strchr(buf, ':');
484 while (*p == ' ') p++;
496 case SENSOR_TYPE_LINUX_SYS:
497 f = fopen(sensor_path, "r");
500 fgets(buf, sizeof(buf), f);
501 buf[sizeof(buf) - 1] = 0;
513 if (ret) return temp;
517 sensor_type = SENSOR_TYPE_NONE;
518 if (sensor_name) free(sensor_name);
520 if (sensor_path) free(sensor_path);
526 poll_cb(void *data __UNUSED__)
531 pp = cur_poll_interval;
534 cur_poll_interval /= 2;
535 if (cur_poll_interval < 4) cur_poll_interval = 4;
539 cur_poll_interval *= 2;
540 if (cur_poll_interval > 256) cur_poll_interval = 256;
542 /* adapt polling based on if temp changes - every time it changes,
543 * halve the time between polls, otherwise double it, between 4 and
545 if (pp != cur_poll_interval)
547 if (poller) ecore_poller_del(poller);
548 poller = ecore_poller_add(ECORE_POLLER_CORE, cur_poll_interval,
553 if (t == -999) printf("ERROR\n");
554 else printf("%i\n", t);
558 return ECORE_CALLBACK_RENEW;
562 main(int argc, char *argv[])
566 printf("ARGS INCORRECT!\n");
569 sensor_type = atoi(argv[1]);
570 sensor_name = strdup(argv[2]);
571 if (!strcmp(sensor_name, "-null-"))
576 poll_interval = atoi(argv[3]);
577 cur_poll_interval = poll_interval;
583 if (poller) ecore_poller_del(poller);
584 poller = ecore_poller_add(ECORE_POLLER_CORE, cur_poll_interval,
588 ecore_main_loop_begin();