mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
Issue #125 Explicitly request Input Monitoring and error if denied
This commit is contained in:
@@ -30,6 +30,10 @@ namespace BlackInput
|
||||
|
||||
bool CJoystickDevice::init(const IOHIDDeviceRef device)
|
||||
{
|
||||
if (!CMacOSInputUtils::requestAccess())
|
||||
{
|
||||
CLogMessage(this).error(u"Access denied for joystick input monitoring");
|
||||
}
|
||||
m_deviceRef = device;
|
||||
|
||||
CFTypeRef property = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
|
||||
|
||||
@@ -146,6 +146,10 @@ namespace BlackInput
|
||||
|
||||
bool CKeyboardMacOS::init()
|
||||
{
|
||||
if (!CMacOSInputUtils::requestAccess())
|
||||
{
|
||||
CLogMessage(this).error(u"Access denied for keyboard input monitoring");
|
||||
}
|
||||
m_hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
|
||||
|
||||
CFMutableArrayRef matchingArray = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
|
||||
@@ -174,7 +178,16 @@ namespace BlackInput
|
||||
|
||||
IOHIDManagerRegisterInputValueCallback(m_hidManager, valueCallback, this);
|
||||
IOHIDManagerScheduleWithRunLoop(m_hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||
IOHIDManagerOpen(m_hidManager, kIOHIDOptionsTypeNone);
|
||||
const auto result = IOHIDManagerOpen(m_hidManager, kIOHIDOptionsTypeNone);
|
||||
|
||||
if (result == kIOReturnSuccess)
|
||||
{
|
||||
CLogMessage(this).debug(u"Initialized");
|
||||
}
|
||||
else
|
||||
{
|
||||
CLogMessage(this).error(u"Failed to open HID manager for keyboard monitoring");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,9 @@ namespace BlackInput
|
||||
public:
|
||||
CMacOSInputUtils() = delete;
|
||||
|
||||
//! Request OS permission for input monitoring access
|
||||
static bool requestAccess();
|
||||
|
||||
//! Creates a new device matching dict using usagePage and usage
|
||||
static CFMutableDictionaryRef createDeviceMatchingDictionary(UInt32 usagePage, UInt32 usage);
|
||||
};
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
#include "macosinpututils.h"
|
||||
|
||||
#include <IOKit/hid/IOHIDKeys.h>
|
||||
#include <IOKit/hidsystem/IOHIDLib.h>
|
||||
|
||||
namespace BlackInput
|
||||
{
|
||||
bool CMacOSInputUtils::requestAccess()
|
||||
{
|
||||
if (@available(macOS 10.15, *))
|
||||
{
|
||||
return IOHIDRequestAccess(kIOHIDRequestTypeListenEvent);
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
CFMutableDictionaryRef CMacOSInputUtils::createDeviceMatchingDictionary(UInt32 usagePage, UInt32 usage)
|
||||
{
|
||||
CFMutableDictionaryRef result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
|
||||
|
||||
Reference in New Issue
Block a user