xmtop/widgets/temp_openbsd.go

75 lines
1.5 KiB
Go
Raw Normal View History

// +build openbsd
2019-02-18 17:54:17 +08:00
package widgets
2019-03-01 08:29:52 +08:00
// loosely based on https://github.com/openbsd/src/blob/master/sbin/sysctl/sysctl.c#L2517
2019-02-18 17:54:17 +08:00
// #include <sys/time.h>
// #include <sys/sysctl.h>
// #include <sys/sensors.h>
import "C"
import (
"strconv"
2019-02-18 21:09:36 +08:00
"syscall"
"unsafe"
2019-02-18 17:54:17 +08:00
2020-02-14 06:26:28 +08:00
"github.com/xxxserxxx/gotop/utils"
2019-02-18 17:54:17 +08:00
)
2019-03-01 08:29:52 +08:00
func (self *TempWidget) getTemp(mib []C.int, mlen int, snsrdev *C.struct_sensordev, index int) {
2019-02-22 12:12:17 +08:00
switch mlen {
case 4:
2019-02-18 17:54:17 +08:00
k := mib[3]
var numt C.int
2019-02-18 21:09:36 +08:00
for numt = 0; numt < snsrdev.maxnumt[k]; numt++ {
2019-02-18 17:54:17 +08:00
mib[4] = numt
2019-03-01 08:29:52 +08:00
self.getTemp(mib, mlen+1, snsrdev, int(numt))
2019-02-18 17:54:17 +08:00
}
2019-02-22 12:12:17 +08:00
case 5:
2019-02-18 17:54:17 +08:00
var snsr C.struct_sensor
var slen C.size_t = C.sizeof_struct_sensor
if v, _ := C.sysctl(&mib[0], 5, unsafe.Pointer(&snsr), &slen, nil, 0); v == -1 {
return
}
2019-02-18 21:09:36 +08:00
if slen > 0 && (snsr.flags&C.SENSOR_FINVALID) == 0 {
2019-02-18 17:54:17 +08:00
key := C.GoString(&snsrdev.xname[0]) + ".temp" + strconv.Itoa(index)
temp := int((snsr.value - 273150000.0) / 1000000.0)
2019-03-01 08:29:52 +08:00
switch self.TempScale {
case Fahrenheit:
self.Data[key] = utils.CelsiusToFahrenheit(temp)
2020-01-22 01:21:57 +08:00
case Celsius:
2019-03-01 08:29:52 +08:00
self.Data[key] = temp
2019-02-18 17:54:17 +08:00
}
}
}
}
2019-03-01 08:29:52 +08:00
func (self *TempWidget) update() {
2019-02-18 17:54:17 +08:00
mib := []C.int{0, 1, 2, 3, 4}
var snsrdev C.struct_sensordev
var len C.ulong = C.sizeof_struct_sensordev
mib[0] = C.CTL_HW
mib[1] = C.HW_SENSORS
mib[3] = C.SENSOR_TEMP
var i C.int
2019-02-18 21:09:36 +08:00
for i = 0; ; i++ {
2019-02-18 17:54:17 +08:00
mib[2] = i
if v, e := C.sysctl(&mib[0], 3, unsafe.Pointer(&snsrdev), &len, nil, 0); v == -1 {
if e == syscall.ENXIO {
continue
}
if e == syscall.ENOENT {
2019-02-18 21:09:36 +08:00
break
2019-02-18 17:54:17 +08:00
}
}
2019-03-01 08:29:52 +08:00
self.getTemp(mib, 4, &snsrdev, 0)
2019-02-18 17:54:17 +08:00
}
}