-
Notifications
You must be signed in to change notification settings - Fork 240
/
darwin.py
277 lines (222 loc) · 8.45 KB
/
darwin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# coding=utf-8
# pynput
# Copyright (C) 2015-2022 Moses Palmér
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Utility functions and classes for the *Darwin* backend.
"""
# pylint: disable=C0103
# pylint: disable=R0903
# This module contains wrapper classes
import contextlib
import ctypes
import ctypes.util
import six
import objc
import CoreFoundation
import HIServices
import Quartz
from . import AbstractListener
def _wrap_value(value):
"""Converts a pointer to a *Python objc* value.
:param value: The pointer to convert.
:return: a wrapped value
"""
return objc.objc_object(c_void_p=value) if value is not None else None
@contextlib.contextmanager
def _wrapped(value):
"""A context manager that converts a raw pointer to a *Python objc* value.
When the block is exited, the value is released.
:param value: The raw value to wrap.
"""
wrapped_value = _wrap_value(value)
try:
yield value
finally:
CoreFoundation.CFRelease(wrapped_value)
class CarbonExtra(object):
"""A class exposing some missing functionality from *Carbon* as class
attributes.
"""
_Carbon = ctypes.cdll.LoadLibrary(ctypes.util.find_library('Carbon'))
_Carbon.TISCopyCurrentKeyboardInputSource.argtypes = []
_Carbon.TISCopyCurrentKeyboardInputSource.restype = ctypes.c_void_p
_Carbon.TISCopyCurrentASCIICapableKeyboardLayoutInputSource.argtypes = []
_Carbon.TISCopyCurrentASCIICapableKeyboardLayoutInputSource.restype = \
ctypes.c_void_p
_Carbon.TISGetInputSourceProperty.argtypes = [
ctypes.c_void_p, ctypes.c_void_p]
_Carbon.TISGetInputSourceProperty.restype = ctypes.c_void_p
_Carbon.LMGetKbdType.argtypes = []
_Carbon.LMGetKbdType.restype = ctypes.c_uint32
_Carbon.UCKeyTranslate.argtypes = [
ctypes.c_void_p,
ctypes.c_uint16,
ctypes.c_uint16,
ctypes.c_uint32,
ctypes.c_uint32,
ctypes.c_uint32,
ctypes.POINTER(ctypes.c_uint32),
ctypes.c_uint8,
ctypes.POINTER(ctypes.c_uint8),
ctypes.c_uint16 * 4]
_Carbon.UCKeyTranslate.restype = ctypes.c_uint32
TISCopyCurrentKeyboardInputSource = \
_Carbon.TISCopyCurrentKeyboardInputSource
TISCopyCurrentASCIICapableKeyboardLayoutInputSource = \
_Carbon.TISCopyCurrentASCIICapableKeyboardLayoutInputSource
kTISPropertyUnicodeKeyLayoutData = ctypes.c_void_p.in_dll(
_Carbon, 'kTISPropertyUnicodeKeyLayoutData')
TISGetInputSourceProperty = \
_Carbon.TISGetInputSourceProperty
LMGetKbdType = \
_Carbon.LMGetKbdType
kUCKeyActionDisplay = 3
kUCKeyTranslateNoDeadKeysBit = 0
UCKeyTranslate = \
_Carbon.UCKeyTranslate
@contextlib.contextmanager
def keycode_context():
"""Returns an opaque value representing a context for translating keycodes
to strings.
"""
keyboard_type, layout_data = None, None
for source in [
CarbonExtra.TISCopyCurrentKeyboardInputSource,
CarbonExtra.TISCopyCurrentASCIICapableKeyboardLayoutInputSource]:
with _wrapped(source()) as keyboard:
keyboard_type = CarbonExtra.LMGetKbdType()
layout = _wrap_value(CarbonExtra.TISGetInputSourceProperty(
keyboard,
CarbonExtra.kTISPropertyUnicodeKeyLayoutData))
layout_data = layout.bytes().tobytes() if layout else None
if keyboard is not None and layout_data is not None:
break
yield (keyboard_type, layout_data)
def keycode_to_string(context, keycode, modifier_state=0):
"""Converts a keycode to a string.
"""
LENGTH = 4
keyboard_type, layout_data = context
dead_key_state = ctypes.c_uint32()
length = ctypes.c_uint8()
unicode_string = (ctypes.c_uint16 * LENGTH)()
CarbonExtra.UCKeyTranslate(
layout_data,
keycode,
CarbonExtra.kUCKeyActionDisplay,
modifier_state,
keyboard_type,
CarbonExtra.kUCKeyTranslateNoDeadKeysBit,
ctypes.byref(dead_key_state),
LENGTH,
ctypes.byref(length),
unicode_string)
return u''.join(
six.unichr(unicode_string[i])
for i in range(length.value))
def get_unicode_to_keycode_map():
"""Returns a mapping from unicode strings to virtual key codes.
:return: a dict mapping key codes to strings
"""
with keycode_context() as context:
return {
keycode_to_string(context, keycode): keycode
for keycode in range(128)}
class ListenerMixin(object):
"""A mixin for *Quartz* event listeners.
Subclasses should set a value for :attr:`_EVENTS` and implement
:meth:`_handle`.
"""
#: The events that we listen to
_EVENTS = tuple()
#: Whether this process is trusted to monitor input events.
IS_TRUSTED = False
def _run(self):
self.IS_TRUSTED = HIServices.AXIsProcessTrusted()
if not self.IS_TRUSTED:
self._log.warning(
'This process is not trusted! Input event monitoring will not '
'be possible until it is added to accessibility clients.')
self._loop = None
try:
tap = self._create_event_tap()
if tap is None:
self._mark_ready()
return
loop_source = Quartz.CFMachPortCreateRunLoopSource(
None, tap, 0)
self._loop = Quartz.CFRunLoopGetCurrent()
Quartz.CFRunLoopAddSource(
self._loop, loop_source, Quartz.kCFRunLoopDefaultMode)
Quartz.CGEventTapEnable(tap, True)
self._mark_ready()
# pylint: disable=W0702; we want to silence errors
try:
while self.running:
result = Quartz.CFRunLoopRunInMode(
Quartz.kCFRunLoopDefaultMode, 1, False)
try:
if result != Quartz.kCFRunLoopRunTimedOut:
break
except AttributeError:
# This happens during teardown of the virtual machine
break
except:
# This exception will have been passed to the main thread
pass
# pylint: enable=W0702
finally:
self._loop = None
def _stop_platform(self):
# The base class sets the running flag to False; this will cause the
# loop around run loop invocations to terminate and set this event
try:
if self._loop is not None:
Quartz.CFRunLoopStop(self._loop)
except AttributeError:
# The loop may not have been created
pass
def _create_event_tap(self):
"""Creates the event tap used by the listener.
:return: an event tap
"""
return Quartz.CGEventTapCreate(
Quartz.kCGSessionEventTap,
Quartz.kCGHeadInsertEventTap,
Quartz.kCGEventTapOptionListenOnly if (
True
and not self.suppress
and self._intercept is None)
else Quartz.kCGEventTapOptionDefault,
self._EVENTS,
self._handler,
None)
@AbstractListener._emitter
def _handler(self, proxy, event_type, event, refcon):
"""The callback registered with *macOS* for mouse events.
This method will call the callbacks registered on initialisation.
"""
self._handle(proxy, event_type, event, refcon)
if self._intercept is not None:
return self._intercept(event_type, event)
elif self.suppress:
return None
def _handle(self, proxy, event_type, event, refcon):
"""The device specific callback handler.
This method calls the appropriate callback registered when this
listener was created based on the event.
"""
raise NotImplementedError()