github.com/Code-Hex/battery@v1.0.0/internal/macos/iokit.go (about) 1 //go:build darwin 2 3 package macOS 4 5 import "errors" 6 7 // IOKit linker flags for the external linker. See Issue 42459. 8 9 var IOPSCurrentCapacityKey = StringToCFString("Current Capacity") 10 var IOPSTimeToEmptyKey = StringToCFString("Time to Empty") 11 var IOPSPowerSourceStateKey = StringToCFString("Power Source State") 12 13 // Values for key IOPSPowerSourceStateKey. 14 var ( 15 // Power source is off-line or no longer connected. 16 IOPSOffLineValue = "Off Line" 17 // Power source is connected to external or AC power, and is not draining the internal battery. 18 IOPSACPowerValue = "AC Power" 19 // Power source is currently using the internal battery. 20 IOPSBatteryPowerValue = "Battery Power" 21 ) 22 23 //go:cgo_import_dynamic battery_IOPSCopyPowerSourcesInfo IOPSCopyPowerSourcesInfo "/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit" 24 25 func IOPSCopyPowerSourcesInfo() (CFRef, error) { 26 ret := syscall(FuncPC(battery_IOPSCopyPowerSourcesInfo_trampoline), 0, 0, 0, 0, 0, 0) 27 // https://developer.apple.com/documentation/iokit/1523839-iopscopypowersourcesinfo 28 // NULL if errors were encountered, a CFTypeRef otherwise. 29 // Caller must CFRelease() the return value when done accessing it. 30 if ret == 0 { 31 return 0, errors.New("IOPSCopyPowerSourcesInfo: errors were encountered") 32 } 33 return CFRef(ret), nil 34 } 35 func battery_IOPSCopyPowerSourcesInfo_trampoline() 36 37 //go:cgo_import_dynamic battery_IOPSCopyPowerSourcesList IOPSCopyPowerSourcesList "/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit" 38 39 func IOPSCopyPowerSourcesList(blob CFRef) (CFRef, error) { 40 ret := syscall(FuncPC(battery_IOPSCopyPowerSourcesList_trampoline), 41 uintptr(blob), 42 0, 0, 0, 0, 0) 43 // https://developer.apple.com/documentation/iokit/1523856-iopscopypowersourceslist 44 // Returns NULL if errors were encountered, otherwise a CFArray of CFTypeRefs. 45 // Caller must CFRelease() the returned CFArrayRef. 46 if ret == 0 { 47 return 0, errors.New("IOPSCopyPowerSourcesList: errors were encountered") 48 } 49 return CFRef(ret), nil 50 } 51 func battery_IOPSCopyPowerSourcesList_trampoline() 52 53 //go:cgo_import_dynamic battery_IOPSGetPowerSourceDescription IOPSGetPowerSourceDescription "/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit" 54 55 func IOPSGetPowerSourceDescription(blob CFRef, ps CFRef) (CFRef, error) { 56 ret := syscall(FuncPC(battery_IOPSGetPowerSourceDescription_trampoline), 57 uintptr(blob), uintptr(ps), 58 0, 0, 0, 0) 59 // https://developer.apple.com/documentation/iokit/1523867-iopsgetpowersourcedescription 60 // Returns NULL if an error was encountered, otherwise a CFDictionary. 61 // Caller should NOT release the returned CFDictionary - it will be 62 // released as part of the CFTypeRef returned by IOPSCopyPowerSourcesInfo(). 63 if ret == 0 { 64 return 0, errors.New("IOPSGetPowerSourceDescription: errors were encountered") 65 } 66 return CFRef(ret), nil 67 } 68 func battery_IOPSGetPowerSourceDescription_trampoline()