From a1c99bb10f75fd8623aa1570c5dedfb0ad80a05e Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Mon, 18 Feb 2019 10:51:34 +0100 Subject: [PATCH 1/4] add openbsd to build tag --- src/widgets/proc_other.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widgets/proc_other.go b/src/widgets/proc_other.go index da0123c..c5f42d5 100644 --- a/src/widgets/proc_other.go +++ b/src/widgets/proc_other.go @@ -1,4 +1,4 @@ -// +build freebsd darwin +// +build freebsd darwin openbsd package widgets From 175cfef2e18f9271b6faa881eff5e91ad4bf9ae3 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Mon, 18 Feb 2019 10:54:17 +0100 Subject: [PATCH 2/4] implemented Temp.update for openbsd --- src/widgets/temp_openbsd.go | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/widgets/temp_openbsd.go diff --git a/src/widgets/temp_openbsd.go b/src/widgets/temp_openbsd.go new file mode 100644 index 0000000..1267b98 --- /dev/null +++ b/src/widgets/temp_openbsd.go @@ -0,0 +1,76 @@ +package widgets + +// #include +// #include +// #include +import "C" + +import ( + "unsafe" + "syscall" + "strconv" + + "github.com/cjbassi/gotop/src/utils" +) + +func gettemp(t *Temp, mib []C.int, mlen int, snsrdev *C.struct_sensordev, index int) { + if mlen == 4 { + k := mib[3] + var numt C.int + for numt = 0; numt < snsrdev.maxnumt[k]; numt += 1 { + mib[4] = numt + gettemp(t, mib, mlen+1, snsrdev, int(numt)) + } + return + } + + if mlen == 5 { + 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 + } + + if slen > 0 && (snsr.flags & C.SENSOR_FINVALID) == 0 { + key := C.GoString(&snsrdev.xname[0]) + ".temp" + strconv.Itoa(index) + temp := int((snsr.value - 273150000.0) / 1000000.0) + + if t.Fahrenheit { + t.Data[key] = utils.CelsiusToFahrenheit(temp) + } else { + t.Data[key] = temp + } + } + + return + } +} + +func (self *Temp) update() { + 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 + for i = 0; ; i += 1 { + 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 { + break; + } + } + + gettemp(self, mib, 4, &snsrdev, 0) + } +} From 584157012355665191e1bf9b1c6c85ba85938a4d Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Mon, 18 Feb 2019 14:09:36 +0100 Subject: [PATCH 3/4] followed golint & gofmt advices --- src/widgets/temp_openbsd.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/widgets/temp_openbsd.go b/src/widgets/temp_openbsd.go index 1267b98..7795d6d 100644 --- a/src/widgets/temp_openbsd.go +++ b/src/widgets/temp_openbsd.go @@ -6,9 +6,9 @@ package widgets import "C" import ( - "unsafe" - "syscall" "strconv" + "syscall" + "unsafe" "github.com/cjbassi/gotop/src/utils" ) @@ -17,7 +17,7 @@ func gettemp(t *Temp, mib []C.int, mlen int, snsrdev *C.struct_sensordev, index if mlen == 4 { k := mib[3] var numt C.int - for numt = 0; numt < snsrdev.maxnumt[k]; numt += 1 { + for numt = 0; numt < snsrdev.maxnumt[k]; numt++ { mib[4] = numt gettemp(t, mib, mlen+1, snsrdev, int(numt)) } @@ -32,7 +32,7 @@ func gettemp(t *Temp, mib []C.int, mlen int, snsrdev *C.struct_sensordev, index return } - if slen > 0 && (snsr.flags & C.SENSOR_FINVALID) == 0 { + if slen > 0 && (snsr.flags&C.SENSOR_FINVALID) == 0 { key := C.GoString(&snsrdev.xname[0]) + ".temp" + strconv.Itoa(index) temp := int((snsr.value - 273150000.0) / 1000000.0) @@ -58,7 +58,7 @@ func (self *Temp) update() { mib[3] = C.SENSOR_TEMP var i C.int - for i = 0; ; i += 1 { + for i = 0; ; i++ { mib[2] = i if v, e := C.sysctl(&mib[0], 3, unsafe.Pointer(&snsrdev), &len, nil, 0); v == -1 { @@ -67,7 +67,7 @@ func (self *Temp) update() { } if e == syscall.ENOENT { - break; + break } } From 8c5982f81c64be12d1fe5bfee72fb033d3ab9c42 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Thu, 21 Feb 2019 20:12:17 -0800 Subject: [PATCH 4/4] Minor openbsd refactor --- src/widgets/temp_openbsd.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/widgets/temp_openbsd.go b/src/widgets/temp_openbsd.go index 7795d6d..646cae9 100644 --- a/src/widgets/temp_openbsd.go +++ b/src/widgets/temp_openbsd.go @@ -13,18 +13,16 @@ import ( "github.com/cjbassi/gotop/src/utils" ) -func gettemp(t *Temp, mib []C.int, mlen int, snsrdev *C.struct_sensordev, index int) { - if mlen == 4 { +func getTemp(t *Temp, mib []C.int, mlen int, snsrdev *C.struct_sensordev, index int) { + switch mlen { + case 4: k := mib[3] var numt C.int for numt = 0; numt < snsrdev.maxnumt[k]; numt++ { mib[4] = numt - gettemp(t, mib, mlen+1, snsrdev, int(numt)) + getTemp(t, mib, mlen+1, snsrdev, int(numt)) } - return - } - - if mlen == 5 { + case 5: var snsr C.struct_sensor var slen C.size_t = C.sizeof_struct_sensor @@ -42,8 +40,6 @@ func gettemp(t *Temp, mib []C.int, mlen int, snsrdev *C.struct_sensordev, index t.Data[key] = temp } } - - return } } @@ -71,6 +67,6 @@ func (self *Temp) update() { } } - gettemp(self, mib, 4, &snsrdev, 0) + getTemp(self, mib, 4, &snsrdev, 0) } }