refactor(battery): use syscall.SyscallN calls

This commit is contained in:
L. Yeung 2024-08-17 14:02:27 +08:00 committed by Jan De Dobbeleer
parent fb45e8a498
commit 2450bb0d6c
2 changed files with 6 additions and 12 deletions

View file

@ -271,7 +271,7 @@ func (e *Engine) patchPowerShellBleed() {
// when in PowerShell, we need to clear the line after the prompt // when in PowerShell, we need to clear the line after the prompt
// to avoid the background being printed on the next line // to avoid the background being printed on the next line
// when at the end of the buffer. // when at the end of the buffer.
// See https://githue.com/JanDeDobbeleer/oh-my-posh/issues/65 // See https://github.com/JanDeDobbeleer/oh-my-posh/issues/65
if e.Env.Shell() != shell.PWSH && e.Env.Shell() != shell.PWSH5 { if e.Env.Shell() != shell.PWSH && e.Env.Shell() != shell.PWSH5 {
return return
} }

View file

@ -91,8 +91,8 @@ func uint32ToFloat64(num uint32) (float64, error) {
return float64(num), nil return float64(num), nil
} }
func setupDiSetup(proc *windows.LazyProc, nargs, a1, a2, a3, a4, a5, a6 uintptr) (uintptr, error) { func setupDiSetup(proc *windows.LazyProc, args ...uintptr) (uintptr, error) {
r1, _, errno := syscall.Syscall6(proc.Addr(), nargs, a1, a2, a3, a4, a5, a6) //nolint:staticcheck r1, _, errno := syscall.SyscallN(proc.Addr(), args...)
if windows.Handle(r1) == windows.InvalidHandle { if windows.Handle(r1) == windows.InvalidHandle {
if errno != 0 { if errno != 0 {
return 0, error(errno) return 0, error(errno)
@ -102,8 +102,8 @@ func setupDiSetup(proc *windows.LazyProc, nargs, a1, a2, a3, a4, a5, a6 uintptr)
return r1, nil return r1, nil
} }
func setupDiCall(proc *windows.LazyProc, nargs, a1, a2, a3, a4, a5, a6 uintptr) syscall.Errno { func setupDiCall(proc *windows.LazyProc, args ...uintptr) syscall.Errno {
r1, _, errno := syscall.Syscall6(proc.Addr(), nargs, a1, a2, a3, a4, a5, a6) //nolint:staticcheck r1, _, errno := syscall.SyscallN(proc.Addr(), args...)
if r1 == 0 { if r1 == 0 {
if errno != 0 { if errno != 0 {
return errno return errno
@ -137,12 +137,10 @@ func readState(powerState uint32) State {
func systemGet(idx int) (*battery, error) { func systemGet(idx int) (*battery, error) {
hdev, err := setupDiSetup( hdev, err := setupDiSetup(
setupDiGetClassDevsW, setupDiGetClassDevsW,
4,
uintptr(unsafe.Pointer(&guidDeviceBattery)), uintptr(unsafe.Pointer(&guidDeviceBattery)),
0, 0,
0, 0,
2|16, // DIGCF_PRESENT|DIGCF_DEVICEINTERFACE 2|16, // DIGCF_PRESENT|DIGCF_DEVICEINTERFACE
0, 0,
) )
if err != nil { if err != nil {
@ -150,20 +148,18 @@ func systemGet(idx int) (*battery, error) {
} }
defer func() { defer func() {
_, _, _ = syscall.Syscall(setupDiDestroyDeviceInfoList.Addr(), 1, hdev, 0, 0) //nolint:staticcheck _, _, _ = syscall.SyscallN(setupDiDestroyDeviceInfoList.Addr(), hdev)
}() }()
var did spDeviceInterfaceData var did spDeviceInterfaceData
did.cbSize = uint32(unsafe.Sizeof(did)) did.cbSize = uint32(unsafe.Sizeof(did))
errno := setupDiCall( errno := setupDiCall(
setupDiEnumDeviceInterfaces, setupDiEnumDeviceInterfaces,
5,
hdev, hdev,
0, 0,
uintptr(unsafe.Pointer(&guidDeviceBattery)), uintptr(unsafe.Pointer(&guidDeviceBattery)),
uintptr(idx), uintptr(idx),
uintptr(unsafe.Pointer(&did)), uintptr(unsafe.Pointer(&did)),
0,
) )
if errno == 259 { // ERROR_NO_MORE_ITEMS if errno == 259 { // ERROR_NO_MORE_ITEMS
@ -177,7 +173,6 @@ func systemGet(idx int) (*battery, error) {
var cbRequired uint32 var cbRequired uint32
errno = setupDiCall( errno = setupDiCall(
setupDiGetDeviceInterfaceDetailW, setupDiGetDeviceInterfaceDetailW,
6,
hdev, hdev,
uintptr(unsafe.Pointer(&did)), uintptr(unsafe.Pointer(&did)),
0, 0,
@ -203,7 +198,6 @@ func systemGet(idx int) (*battery, error) {
errno = setupDiCall( errno = setupDiCall(
setupDiGetDeviceInterfaceDetailW, setupDiGetDeviceInterfaceDetailW,
6,
hdev, hdev,
uintptr(unsafe.Pointer(&did)), uintptr(unsafe.Pointer(&did)),
uintptr(unsafe.Pointer(&didd[0])), uintptr(unsafe.Pointer(&didd[0])),