From 27f7d4d65660b763557d8cb18a9eb5e20283ac53 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Sat, 30 Oct 2021 21:37:00 +0100 Subject: [PATCH] Issue #125 Explicitly request Input Monitoring and error if denied --- src/blackinput/macos/joystickmacos.mm | 4 ++++ src/blackinput/macos/keyboardmacos.mm | 15 ++++++++++++++- src/blackinput/macos/macosinpututils.h | 3 +++ src/blackinput/macos/macosinpututils.mm | 13 +++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/blackinput/macos/joystickmacos.mm b/src/blackinput/macos/joystickmacos.mm index c4620459a..045f032bb 100644 --- a/src/blackinput/macos/joystickmacos.mm +++ b/src/blackinput/macos/joystickmacos.mm @@ -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)); diff --git a/src/blackinput/macos/keyboardmacos.mm b/src/blackinput/macos/keyboardmacos.mm index 54464f632..fc479f284 100644 --- a/src/blackinput/macos/keyboardmacos.mm +++ b/src/blackinput/macos/keyboardmacos.mm @@ -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; } diff --git a/src/blackinput/macos/macosinpututils.h b/src/blackinput/macos/macosinpututils.h index 7ac927a5e..4a1e81525 100644 --- a/src/blackinput/macos/macosinpututils.h +++ b/src/blackinput/macos/macosinpututils.h @@ -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); }; diff --git a/src/blackinput/macos/macosinpututils.mm b/src/blackinput/macos/macosinpututils.mm index a7ffc5dcc..96f50b6ea 100644 --- a/src/blackinput/macos/macosinpututils.mm +++ b/src/blackinput/macos/macosinpututils.mm @@ -1,9 +1,22 @@ #include "macosinpututils.h" #include +#include 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,