Issue #125 Explicitly request Input Monitoring and error if denied

This commit is contained in:
Mat Sutcliffe
2021-10-30 21:37:00 +01:00
parent b7b71b2245
commit 27f7d4d656
4 changed files with 34 additions and 1 deletions

View File

@@ -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));

View File

@@ -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;
}

View File

@@ -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);
};

View File

@@ -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,