slstatus

My fork of https://tools.suckless.org/slstatus/
git clone https://git.inz.fi/slstatus
Log | Files | Refs | README | LICENSE

temperature.c (1389B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stddef.h>
      3 
      4 #include "../util.h"
      5 
      6 
      7 #if defined(__linux__)
      8 	#include <stdint.h>
      9 
     10 	const char *
     11 	temp(const char *file)
     12 	{
     13 		uintmax_t temp;
     14 
     15 		if (pscanf(file, "%ju", &temp) != 1) {
     16 			return NULL;
     17 		}
     18 
     19 		return bprintf("%ju", temp / 1000);
     20 	}
     21 #elif defined(__OpenBSD__)
     22 	#include <stdio.h>
     23 	#include <sys/time.h> /* before <sys/sensors.h> for struct timeval */
     24 	#include <sys/sensors.h>
     25 	#include <sys/sysctl.h>
     26 
     27 	const char *
     28 	temp(const char *unused)
     29 	{
     30 		int mib[5];
     31 		size_t size;
     32 		struct sensor temp;
     33 
     34 		mib[0] = CTL_HW;
     35 		mib[1] = HW_SENSORS;
     36 		mib[2] = 0; /* cpu0 */
     37 		mib[3] = SENSOR_TEMP;
     38 		mib[4] = 0; /* temp0 */
     39 
     40 		size = sizeof(temp);
     41 
     42 		if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) {
     43 			warn("sysctl 'SENSOR_TEMP':");
     44 			return NULL;
     45 		}
     46 
     47 		/* kelvin to celsius */
     48 		return bprintf("%d", (int)((float)(temp.value-273150000) / 1E6));
     49 	}
     50 #elif defined(__FreeBSD__)
     51 	#include <stdio.h>
     52 	#include <stdlib.h>
     53 	#include <sys/sysctl.h>
     54 
     55 	const char *
     56 	temp(const char *zone)
     57 	{
     58 		char buf[256];
     59 		int temp;
     60 		size_t len;
     61 
     62 		len = sizeof(temp);
     63 		snprintf(buf, sizeof(buf), "hw.acpi.thermal.%s.temperature", zone);
     64 		if (sysctlbyname(buf, &temp, &len, NULL, 0) == -1
     65 				|| !len)
     66 			return NULL;
     67 
     68 		/* kelvin to decimal celcius */
     69 		return bprintf("%d.%d", (temp - 2731) / 10, abs((temp - 2731) % 10));
     70 	}
     71 #endif