internal/gamepad: use RegisterLibFunc (#2465)

RegisterLibFunction is typesafe and now supports float arguments.

Updates #1162
This commit is contained in:
TotallyGamerJet 2022-11-20 07:24:27 -08:00 committed by GitHub
parent fb612ab443
commit f09c4a624e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 149 deletions

View File

@ -50,19 +50,7 @@ const (
) )
var ( var (
corefoundation = purego.Dlopen("CoreFoundation.framework/CoreFoundation", purego.RTLD_LAZY|purego.RTLD_GLOBAL) corefoundation = purego.Dlopen("CoreFoundation.framework/CoreFoundation", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
procCFNumberCreate = purego.Dlsym(corefoundation, "CFNumberCreate")
procCFNumberGetValue = purego.Dlsym(corefoundation, "CFNumberGetValue")
procCFArrayCreate = purego.Dlsym(corefoundation, "CFArrayCreate")
procCFArrayGetCount = purego.Dlsym(corefoundation, "CFArrayGetCount")
procCFArrayGetValueAtIndex = purego.Dlsym(corefoundation, "CFArrayGetValueAtIndex")
procCFDictionaryCreate = purego.Dlsym(corefoundation, "CFDictionaryCreate")
procCFRelease = purego.Dlsym(corefoundation, "CFRelease")
procCFRunLoopGetMain = purego.Dlsym(corefoundation, "CFRunLoopGetMain")
procCFRunLoopRunInMode = purego.Dlsym(corefoundation, "CFRunLoopRunInMode")
procCFGetTypeID = purego.Dlsym(corefoundation, "CFGetTypeID")
procCFStringGetCString = purego.Dlsym(corefoundation, "CFStringGetCString")
procCFStringCreateWithCString = purego.Dlsym(corefoundation, "CFStringCreateWithCString")
kCFTypeDictionaryKeyCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryKeyCallBacks") kCFTypeDictionaryKeyCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryKeyCallBacks")
kCFTypeDictionaryValueCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryValueCallBacks") kCFTypeDictionaryValueCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryValueCallBacks")
@ -70,69 +58,41 @@ var (
kCFRunLoopDefaultMode = purego.Dlsym(corefoundation, "kCFRunLoopDefaultMode") kCFRunLoopDefaultMode = purego.Dlsym(corefoundation, "kCFRunLoopDefaultMode")
) )
func _CFNumberCreate(allocator _CFAllocatorRef, theType _CFNumberType, valuePtr unsafe.Pointer) _CFNumberRef { func init() {
number, _, _ := purego.SyscallN(procCFNumberCreate, uintptr(allocator), uintptr(theType), uintptr(valuePtr)) purego.RegisterLibFunc(&_CFNumberCreate, corefoundation, "CFNumberCreate")
return _CFNumberRef(number) purego.RegisterLibFunc(&_CFNumberGetValue, corefoundation, "CFNumberGetValue")
purego.RegisterLibFunc(&_CFArrayCreate, corefoundation, "CFArrayCreate")
purego.RegisterLibFunc(&_CFArrayGetValueAtIndex, corefoundation, "CFArrayGetValueAtIndex")
purego.RegisterLibFunc(&_CFArrayGetCount, corefoundation, "CFArrayGetCount")
purego.RegisterLibFunc(&_CFDictionaryCreate, corefoundation, "CFDictionaryCreate")
purego.RegisterLibFunc(&_CFRelease, corefoundation, "CFRelease")
purego.RegisterLibFunc(&_CFRunLoopGetMain, corefoundation, "CFRunLoopGetMain")
purego.RegisterLibFunc(&_CFRunLoopRunInMode, corefoundation, "CFRunLoopRunInMode")
purego.RegisterLibFunc(&_CFGetTypeID, corefoundation, "CFGetTypeID")
purego.RegisterLibFunc(&_CFStringGetCString, corefoundation, "CFStringGetCString")
purego.RegisterLibFunc(&_CFStringCreateWithCString, corefoundation, "CFStringCreateWithCString")
} }
func _CFNumberGetValue(number _CFNumberRef, theType _CFNumberType, valuePtr unsafe.Pointer) bool { var _CFNumberCreate func(allocator _CFAllocatorRef, theType _CFNumberType, valuePtr unsafe.Pointer) _CFNumberRef
ret, _, _ := purego.SyscallN(procCFNumberGetValue, uintptr(number), uintptr(theType), uintptr(valuePtr))
return ret != 0
}
func _CFArrayCreate(allocator _CFAllocatorRef, values *unsafe.Pointer, numValues _CFIndex, callbacks *_CFArrayCallBacks) _CFArrayRef { var _CFNumberGetValue func(number _CFNumberRef, theType _CFNumberType, valuePtr unsafe.Pointer) bool
ret, _, _ := purego.SyscallN(procCFArrayCreate, uintptr(allocator), uintptr(unsafe.Pointer(values)), uintptr(numValues), uintptr(unsafe.Pointer(callbacks)))
return _CFArrayRef(ret)
}
func _CFArrayGetValueAtIndex(array _CFArrayRef, index _CFIndex) uintptr { var _CFArrayCreate func(allocator _CFAllocatorRef, values *unsafe.Pointer, numValues _CFIndex, callbacks *_CFArrayCallBacks) _CFArrayRef
ret, _, _ := purego.SyscallN(procCFArrayGetValueAtIndex, uintptr(array), uintptr(index))
return ret
}
func _CFArrayGetCount(array _CFArrayRef) _CFIndex { var _CFArrayGetValueAtIndex func(array _CFArrayRef, index _CFIndex) uintptr
ret, _, _ := purego.SyscallN(procCFArrayGetCount, uintptr(array))
return _CFIndex(ret)
}
func _CFDictionaryCreate(allocator _CFAllocatorRef, keys *unsafe.Pointer, values *unsafe.Pointer, numValues _CFIndex, keyCallBacks *_CFDictionaryKeyCallBacks, valueCallBacks *_CFDictionaryValueCallBacks) _CFDictionaryRef { var _CFArrayGetCount func(array _CFArrayRef) _CFIndex
ret, _, _ := purego.SyscallN(procCFDictionaryCreate, uintptr(allocator), uintptr(unsafe.Pointer(keys)), uintptr(unsafe.Pointer(values)), uintptr(numValues), uintptr(unsafe.Pointer(keyCallBacks)), uintptr(unsafe.Pointer(valueCallBacks)))
return _CFDictionaryRef(ret)
}
func _CFRelease(cf _CFTypeRef) { var _CFDictionaryCreate func(allocator _CFAllocatorRef, keys *unsafe.Pointer, values *unsafe.Pointer, numValues _CFIndex, keyCallBacks *_CFDictionaryKeyCallBacks, valueCallBacks *_CFDictionaryValueCallBacks) _CFDictionaryRef
purego.SyscallN(procCFRelease, uintptr(cf))
}
func _CFRunLoopGetMain() _CFRunLoopRef { var _CFRelease func(cf _CFTypeRef)
ret, _, _ := purego.SyscallN(procCFRunLoopGetMain)
return _CFRunLoopRef(ret)
}
func _CFRunLoopRunInMode(mode _CFRunLoopMode, seconds _CFTimeInterval, returnAfterSourceHandled bool) _CFRunLoopRunResult { var _CFRunLoopGetMain func() _CFRunLoopRef
var b uintptr
if returnAfterSourceHandled {
b = 1
}
if seconds != 0 {
panic("corefoundation: seconds greater than 0 is not supported")
}
// TODO: support floats
ret, _, _ := purego.SyscallN(procCFRunLoopRunInMode, uintptr(mode), b)
return _CFRunLoopRunResult(ret)
}
func _CFGetTypeID(cf _CFTypeRef) _CFTypeID { var _CFRunLoopRunInMode func(mode _CFRunLoopMode, seconds _CFTimeInterval, returnAfterSourceHandled bool) _CFRunLoopRunResult
ret, _, _ := purego.SyscallN(procCFGetTypeID, uintptr(cf))
return _CFTypeID(ret)
}
func _CFStringGetCString(theString _CFStringRef, buffer []byte, encoding _CFStringEncoding) bool { var _CFGetTypeID func(cf _CFTypeRef) _CFTypeID
ret, _, _ := purego.SyscallN(procCFStringGetCString, uintptr(theString), uintptr(unsafe.Pointer(&buffer[0])), uintptr(len(buffer)), uintptr(encoding))
return ret != 0
}
func _CFStringCreateWithCString(alloc _CFAllocatorRef, cstr []byte, encoding _CFStringEncoding) _CFStringRef { var _CFStringGetCString func(theString _CFStringRef, buffer []byte, encoding _CFStringEncoding) bool
ret, _, _ := purego.SyscallN(procCFStringCreateWithCString, uintptr(alloc), uintptr(unsafe.Pointer(&cstr[0])), uintptr(encoding))
return _CFStringRef(ret) var _CFStringCreateWithCString func(alloc _CFAllocatorRef, cstr []byte, encoding _CFStringEncoding) _CFStringRef
}

View File

@ -84,102 +84,55 @@ type _IOHIDElementType uint32
type _IOHIDDeviceCallback func(context unsafe.Pointer, result _IOReturn, sender unsafe.Pointer, device _IOHIDDeviceRef) type _IOHIDDeviceCallback func(context unsafe.Pointer, result _IOReturn, sender unsafe.Pointer, device _IOHIDDeviceRef)
var ( func init() {
iokit = purego.Dlopen("IOKit.framework/IOKit", purego.RTLD_LAZY|purego.RTLD_GLOBAL) iokit := purego.Dlopen("IOKit.framework/IOKit", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
procIOHIDElementGetTypeID = purego.Dlsym(iokit, "IOHIDElementGetTypeID") purego.RegisterLibFunc(&_IOHIDElementGetTypeID, iokit, "IOHIDElementGetTypeID")
procIOHIDManagerCreate = purego.Dlsym(iokit, "IOHIDManagerCreate") purego.RegisterLibFunc(&_IOHIDManagerCreate, iokit, "IOHIDManagerCreate")
procIOHIDDeviceGetProperty = purego.Dlsym(iokit, "IOHIDDeviceGetProperty") purego.RegisterLibFunc(&_IOHIDDeviceGetProperty, iokit, "IOHIDDeviceGetProperty")
procIOHIDManagerOpen = purego.Dlsym(iokit, "IOHIDManagerOpen") purego.RegisterLibFunc(&_IOHIDManagerOpen, iokit, "IOHIDManagerOpen")
procIOHIDManagerSetDeviceMatchingMultiple = purego.Dlsym(iokit, "IOHIDManagerSetDeviceMatchingMultiple") purego.RegisterLibFunc(&_IOHIDManagerSetDeviceMatchingMultiple, iokit, "IOHIDManagerSetDeviceMatchingMultiple")
procIOHIDManagerRegisterDeviceMatchingCallback = purego.Dlsym(iokit, "IOHIDManagerRegisterDeviceMatchingCallback") purego.RegisterLibFunc(&_IOHIDManagerRegisterDeviceMatchingCallback, iokit, "IOHIDManagerRegisterDeviceMatchingCallback")
procIOHIDManagerRegisterDeviceRemovalCallback = purego.Dlsym(iokit, "IOHIDManagerRegisterDeviceRemovalCallback") purego.RegisterLibFunc(&_IOHIDManagerRegisterDeviceRemovalCallback, iokit, "IOHIDManagerRegisterDeviceRemovalCallback")
procIOHIDManagerScheduleWithRunLoop = purego.Dlsym(iokit, "IOHIDManagerScheduleWithRunLoop") purego.RegisterLibFunc(&_IOHIDManagerScheduleWithRunLoop, iokit, "IOHIDManagerScheduleWithRunLoop")
procIOHIDElementGetType = purego.Dlsym(iokit, "IOHIDElementGetType") purego.RegisterLibFunc(&_IOHIDElementGetType, iokit, "IOHIDElementGetType")
procIOHIDElementGetUsage = purego.Dlsym(iokit, "IOHIDElementGetUsage") purego.RegisterLibFunc(&_IOHIDElementGetUsage, iokit, "IOHIDElementGetUsage")
procIOHIDElementGetUsagePage = purego.Dlsym(iokit, "IOHIDElementGetUsagePage") purego.RegisterLibFunc(&_IOHIDElementGetUsagePage, iokit, "IOHIDElementGetUsagePage")
procIOHIDElementGetLogicalMin = purego.Dlsym(iokit, "IOHIDElementGetLogicalMin") purego.RegisterLibFunc(&_IOHIDElementGetLogicalMin, iokit, "IOHIDElementGetLogicalMin")
procIOHIDElementGetLogicalMax = purego.Dlsym(iokit, "IOHIDElementGetLogicalMax") purego.RegisterLibFunc(&_IOHIDElementGetLogicalMax, iokit, "IOHIDElementGetLogicalMax")
procIOHIDDeviceGetValue = purego.Dlsym(iokit, "IOHIDDeviceGetValue") purego.RegisterLibFunc(&_IOHIDDeviceGetValue, iokit, "IOHIDDeviceGetValue")
procIOHIDValueGetIntegerValue = purego.Dlsym(iokit, "IOHIDValueGetIntegerValue") purego.RegisterLibFunc(&_IOHIDValueGetIntegerValue, iokit, "IOHIDValueGetIntegerValue")
procIOHIDDeviceCopyMatchingElements = purego.Dlsym(iokit, "IOHIDDeviceCopyMatchingElements") purego.RegisterLibFunc(&_IOHIDDeviceCopyMatchingElements, iokit, "IOHIDDeviceCopyMatchingElements")
)
func _IOHIDElementGetTypeID() _CFTypeID {
ret, _, _ := purego.SyscallN(procIOHIDElementGetTypeID)
return _CFTypeID(ret)
} }
func _IOHIDManagerCreate(allocator _CFAllocatorRef, options _IOOptionBits) _IOHIDManagerRef { var _IOHIDElementGetTypeID func() _CFTypeID
ret, _, _ := purego.SyscallN(procIOHIDManagerCreate, uintptr(allocator), uintptr(options))
return _IOHIDManagerRef(ret)
}
func _IOHIDDeviceGetProperty(device _IOHIDDeviceRef, key _CFStringRef) _CFTypeRef { var _IOHIDManagerCreate func(allocator _CFAllocatorRef, options _IOOptionBits) _IOHIDManagerRef
ret, _, _ := purego.SyscallN(procIOHIDDeviceGetProperty, uintptr(device), uintptr(key))
return _CFTypeRef(ret)
}
func _IOHIDManagerOpen(manager _IOHIDManagerRef, options _IOOptionBits) _IOReturn { var _IOHIDDeviceGetProperty func(device _IOHIDDeviceRef, key _CFStringRef) _CFTypeRef
ret, _, _ := purego.SyscallN(procIOHIDManagerOpen, uintptr(manager), uintptr(options))
return _IOReturn(ret)
}
func _IOHIDManagerSetDeviceMatchingMultiple(manager _IOHIDManagerRef, multiple _CFArrayRef) { var _IOHIDManagerOpen func(manager _IOHIDManagerRef, options _IOOptionBits) _IOReturn
purego.SyscallN(procIOHIDManagerSetDeviceMatchingMultiple, uintptr(manager), uintptr(multiple))
}
func _IOHIDManagerRegisterDeviceMatchingCallback(manager _IOHIDManagerRef, callback _IOHIDDeviceCallback, context unsafe.Pointer) { var _IOHIDManagerSetDeviceMatchingMultiple func(manager _IOHIDManagerRef, multiple _CFArrayRef)
purego.SyscallN(procIOHIDManagerRegisterDeviceMatchingCallback, uintptr(manager), purego.NewCallback(callback), uintptr(context))
}
func _IOHIDManagerRegisterDeviceRemovalCallback(manager _IOHIDManagerRef, callback _IOHIDDeviceCallback, context unsafe.Pointer) { var _IOHIDManagerRegisterDeviceMatchingCallback func(manager _IOHIDManagerRef, callback _IOHIDDeviceCallback, context unsafe.Pointer)
purego.SyscallN(procIOHIDManagerRegisterDeviceRemovalCallback, uintptr(manager), purego.NewCallback(callback), uintptr(context))
}
func _IOHIDManagerScheduleWithRunLoop(manager _IOHIDManagerRef, runLoop _CFRunLoopRef, runLoopMode _CFStringRef) { var _IOHIDManagerRegisterDeviceRemovalCallback func(manager _IOHIDManagerRef, callback _IOHIDDeviceCallback, context unsafe.Pointer)
purego.SyscallN(procIOHIDManagerScheduleWithRunLoop, uintptr(manager), uintptr(runLoop), uintptr(runLoopMode))
}
func _IOHIDElementGetType(element _IOHIDElementRef) _IOHIDElementType { var _IOHIDManagerScheduleWithRunLoop func(manager _IOHIDManagerRef, runLoop _CFRunLoopRef, runLoopMode _CFStringRef)
ret, _, _ := purego.SyscallN(procIOHIDElementGetType, uintptr(element))
return _IOHIDElementType(ret)
}
func _IOHIDElementGetUsage(element _IOHIDElementRef) uint32 { var _IOHIDElementGetType func(element _IOHIDElementRef) _IOHIDElementType
ret, _, _ := purego.SyscallN(procIOHIDElementGetUsage, uintptr(element))
return uint32(ret)
}
func _IOHIDElementGetUsagePage(element _IOHIDElementRef) uint32 { var _IOHIDElementGetUsage func(element _IOHIDElementRef) uint32
ret, _, _ := purego.SyscallN(procIOHIDElementGetUsagePage, uintptr(element))
return uint32(ret)
}
func _IOHIDElementGetLogicalMin(element _IOHIDElementRef) _CFIndex { var _IOHIDElementGetUsagePage func(element _IOHIDElementRef) uint32
ret, _, _ := purego.SyscallN(procIOHIDElementGetLogicalMin, uintptr(element))
return _CFIndex(ret)
}
func _IOHIDElementGetLogicalMax(element _IOHIDElementRef) _CFIndex { var _IOHIDElementGetLogicalMin func(element _IOHIDElementRef) _CFIndex
ret, _, _ := purego.SyscallN(procIOHIDElementGetLogicalMax, uintptr(element))
return _CFIndex(ret)
}
func _IOHIDDeviceGetValue(device _IOHIDDeviceRef, element _IOHIDElementRef, pValue *_IOHIDValueRef) _IOReturn { var _IOHIDElementGetLogicalMax func(element _IOHIDElementRef) _CFIndex
if pValue == nil {
panic("IOHID: pValue cannot be nil")
}
ret, _, _ := purego.SyscallN(procIOHIDDeviceGetValue, uintptr(device), uintptr(element), uintptr(unsafe.Pointer(pValue)))
return _IOReturn(ret)
}
func _IOHIDValueGetIntegerValue(value _IOHIDValueRef) _CFIndex { var _IOHIDDeviceGetValue func(device _IOHIDDeviceRef, element _IOHIDElementRef, pValue *_IOHIDValueRef) _IOReturn
ret, _, _ := purego.SyscallN(procIOHIDValueGetIntegerValue, uintptr(value))
return _CFIndex(ret)
}
func _IOHIDDeviceCopyMatchingElements(device _IOHIDDeviceRef, matching _CFDictionaryRef, options _IOOptionBits) _CFArrayRef { var _IOHIDValueGetIntegerValue func(value _IOHIDValueRef) _CFIndex
ret, _, _ := purego.SyscallN(procIOHIDDeviceCopyMatchingElements, uintptr(device), uintptr(matching), uintptr(options))
return _CFArrayRef(ret) var _IOHIDDeviceCopyMatchingElements func(device _IOHIDDeviceRef, matching _CFDictionaryRef, options _IOOptionBits) _CFArrayRef
}