swift
macosinpututils.mm
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "macosinpututils.h"
5 
6 #include <IOKit/hid/IOHIDKeys.h>
7 #include <IOKit/hidsystem/IOHIDLib.h>
8 
9 namespace swift::input
10 {
11 
13  {
14  if (@available(macOS 10.15, *))
15  {
16  return IOHIDCheckAccess(kIOHIDRequestTypeListenEvent) == IOHIDAccessType::kIOHIDAccessTypeGranted;
17  }
18  else
19  {
20  return true;
21  }
22  }
23 
25  {
26  if (@available(macOS 10.15, *))
27  {
28  return IOHIDRequestAccess(kIOHIDRequestTypeListenEvent);
29  }
30  else
31  {
32  return true;
33  }
34  }
35 
36  CFMutableDictionaryRef CMacOSInputUtils::createDeviceMatchingDictionary(UInt32 usagePage, UInt32 usage)
37  {
38  CFMutableDictionaryRef result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
39  &kCFTypeDictionaryKeyCallBacks,
40  &kCFTypeDictionaryValueCallBacks);
41 
42  if (result)
43  {
44  if (usagePage)
45  {
46  CFNumberRef pageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usagePage);
47  if (pageCFNumberRef)
48  {
49  CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsagePageKey), pageCFNumberRef);
50  CFRelease(pageCFNumberRef);
51 
52  if (usage)
53  {
54  CFNumberRef usageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
55  if (usageCFNumberRef)
56  {
57  CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsageKey), usageCFNumberRef);
58  CFRelease(usageCFNumberRef);
59  }
60  }
61  }
62  }
63  }
64  return result;
65  }
66 }
static CFMutableDictionaryRef createDeviceMatchingDictionary(UInt32 usagePage, UInt32 usage)
Creates a new device matching dict using usagePage and usage.
static bool hasAccess()
Check OS permission for input monitoring access.
static bool requestAccess()
Request OS permission for input monitoring access.