1 | /** @file
|
---|
2 | * Common GUI Library - Darwin Keyboard routines.
|
---|
3 | *
|
---|
4 | * @todo Move this up somewhere so that the two SDL GUIs can use parts of this code too (-HID crap).
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 | #define USE_HID_FOR_MODIFIERS
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #define LOG_GROUP LOG_GROUP_GUI
|
---|
32 | #include "DarwinKeyboard.h"
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/time.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #ifdef DEBUG_PRINTF
|
---|
38 | # include <iprt/stream.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
42 | # include <mach/mach.h>
|
---|
43 | # include <mach/mach_error.h>
|
---|
44 | # include <IOKit/IOKitLib.h>
|
---|
45 | # include <IOKit/IOCFPlugIn.h>
|
---|
46 | # include <IOKit/hid/IOHIDLib.h>
|
---|
47 | # include <IOKit/hid/IOHIDUsageTables.h>
|
---|
48 | # include <IOKit/usb/USB.h>
|
---|
49 | # include <CoreFoundation/CoreFoundation.h>
|
---|
50 | #endif
|
---|
51 | #include <Carbon/Carbon.h>
|
---|
52 |
|
---|
53 | __BEGIN_DECLS
|
---|
54 | /* Private interface in 10.3 and later. */
|
---|
55 | typedef int CGSConnection;
|
---|
56 | typedef enum
|
---|
57 | {
|
---|
58 | kCGSGlobalHotKeyEnable = 0,
|
---|
59 | kCGSGlobalHotKeyDisable,
|
---|
60 | kCGSGlobalHotKeyInvalid = -1 /* bird */
|
---|
61 | } CGSGlobalHotKeyOperatingMode;
|
---|
62 | extern CGSConnection _CGSDefaultConnection(void);
|
---|
63 | extern CGError CGSGetGlobalHotKeyOperatingMode(CGSConnection Connection, CGSGlobalHotKeyOperatingMode *enmMode);
|
---|
64 | extern CGError CGSSetGlobalHotKeyOperatingMode(CGSConnection Connection, CGSGlobalHotKeyOperatingMode enmMode);
|
---|
65 | __END_DECLS
|
---|
66 |
|
---|
67 |
|
---|
68 | /*******************************************************************************
|
---|
69 | * Defined Constants And Macros *
|
---|
70 | *******************************************************************************/
|
---|
71 |
|
---|
72 | #define QZ_RMETA 0x36
|
---|
73 | #define QZ_LMETA 0x37
|
---|
74 | #define QZ_LSHIFT 0x38
|
---|
75 | #define QZ_CAPSLOCK 0x39
|
---|
76 | #define QZ_LALT 0x3A
|
---|
77 | #define QZ_LCTRL 0x3B
|
---|
78 | #define QZ_RSHIFT 0x3C
|
---|
79 | #define QZ_RALT 0x3D
|
---|
80 | #define QZ_RCTRL 0x3E
|
---|
81 | #define QZ_NUMLOCK 0x47
|
---|
82 |
|
---|
83 | /** short hand for an extended key. */
|
---|
84 | #define K_EX VBOXKEY_EXTENDED
|
---|
85 | /** short hand for a modifier key. */
|
---|
86 | #define K_MOD VBOXKEY_MODIFIER
|
---|
87 | /** short hand for a lock key. */
|
---|
88 | #define K_LOCK VBOXKEY_LOCK
|
---|
89 |
|
---|
90 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
91 |
|
---|
92 | /** An attempt at catching reference leaks. */
|
---|
93 | #define MY_CHECK_CREFS(cRefs) do { AssertMsg(cRefs < 25, ("%ld\n", cRefs)); NOREF(cRefs); } while (0)
|
---|
94 |
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | /*******************************************************************************
|
---|
98 | * Global Variables *
|
---|
99 | *******************************************************************************/
|
---|
100 | /**
|
---|
101 | * This is derived partially from SDL_QuartzKeys.h and partially from testing.
|
---|
102 | *
|
---|
103 | * (The funny thing about the virtual scan codes on the mac is that they aren't
|
---|
104 | * offically documented, which is rather silly to say the least. Thus, the need
|
---|
105 | * for looking at SDL and other odd places for docs.)
|
---|
106 | */
|
---|
107 | static const uint16_t g_aDarwinToSet1[] =
|
---|
108 | {
|
---|
109 | /* set-1 SDL_QuartzKeys.h */
|
---|
110 | 0x1e, /* QZ_a 0x00 */
|
---|
111 | 0x1f, /* QZ_s 0x01 */
|
---|
112 | 0x20, /* QZ_d 0x02 */
|
---|
113 | 0x21, /* QZ_f 0x03 */
|
---|
114 | 0x23, /* QZ_h 0x04 */
|
---|
115 | 0x22, /* QZ_g 0x05 */
|
---|
116 | 0x2c, /* QZ_z 0x06 */
|
---|
117 | 0x2d, /* QZ_x 0x07 */
|
---|
118 | 0x2e, /* QZ_c 0x08 */
|
---|
119 | 0x2f, /* QZ_v 0x09 */
|
---|
120 | 0x56, /* between lshift and z. 'INT 1'? */
|
---|
121 | 0x30, /* QZ_b 0x0B */
|
---|
122 | 0x10, /* QZ_q 0x0C */
|
---|
123 | 0x11, /* QZ_w 0x0D */
|
---|
124 | 0x12, /* QZ_e 0x0E */
|
---|
125 | 0x13, /* QZ_r 0x0F */
|
---|
126 | 0x15, /* QZ_y 0x10 */
|
---|
127 | 0x14, /* QZ_t 0x11 */
|
---|
128 | 0x02, /* QZ_1 0x12 */
|
---|
129 | 0x03, /* QZ_2 0x13 */
|
---|
130 | 0x04, /* QZ_3 0x14 */
|
---|
131 | 0x05, /* QZ_4 0x15 */
|
---|
132 | 0x07, /* QZ_6 0x16 */
|
---|
133 | 0x06, /* QZ_5 0x17 */
|
---|
134 | 0x0d, /* QZ_EQUALS 0x18 */
|
---|
135 | 0x0a, /* QZ_9 0x19 */
|
---|
136 | 0x08, /* QZ_7 0x1A */
|
---|
137 | 0x0c, /* QZ_MINUS 0x1B */
|
---|
138 | 0x09, /* QZ_8 0x1C */
|
---|
139 | 0x0b, /* QZ_0 0x1D */
|
---|
140 | 0x1b, /* QZ_RIGHTBRACKET 0x1E */
|
---|
141 | 0x18, /* QZ_o 0x1F */
|
---|
142 | 0x16, /* QZ_u 0x20 */
|
---|
143 | 0x1a, /* QZ_LEFTBRACKET 0x21 */
|
---|
144 | 0x17, /* QZ_i 0x22 */
|
---|
145 | 0x19, /* QZ_p 0x23 */
|
---|
146 | 0x1c, /* QZ_RETURN 0x24 */
|
---|
147 | 0x26, /* QZ_l 0x25 */
|
---|
148 | 0x24, /* QZ_j 0x26 */
|
---|
149 | 0x28, /* QZ_QUOTE 0x27 */
|
---|
150 | 0x25, /* QZ_k 0x28 */
|
---|
151 | 0x27, /* QZ_SEMICOLON 0x29 */
|
---|
152 | 0x2b, /* QZ_BACKSLASH 0x2A */
|
---|
153 | 0x33, /* QZ_COMMA 0x2B */
|
---|
154 | 0x35, /* QZ_SLASH 0x2C */
|
---|
155 | 0x31, /* QZ_n 0x2D */
|
---|
156 | 0x32, /* QZ_m 0x2E */
|
---|
157 | 0x34, /* QZ_PERIOD 0x2F */
|
---|
158 | 0x0f, /* QZ_TAB 0x30 */
|
---|
159 | 0x39, /* QZ_SPACE 0x31 */
|
---|
160 | 0x29, /* QZ_BACKQUOTE 0x32 */
|
---|
161 | 0x0e, /* QZ_BACKSPACE 0x33 */
|
---|
162 | 0x9c, /* QZ_IBOOK_ENTER 0x34 */
|
---|
163 | 0x01, /* QZ_ESCAPE 0x35 */
|
---|
164 | 0x5c|K_EX|K_MOD, /* QZ_RMETA 0x36 */
|
---|
165 | 0x5b|K_EX|K_MOD, /* QZ_LMETA 0x37 */
|
---|
166 | 0x2a|K_MOD, /* QZ_LSHIFT 0x38 */
|
---|
167 | 0x3a|K_LOCK, /* QZ_CAPSLOCK 0x39 */
|
---|
168 | 0x38|K_MOD, /* QZ_LALT 0x3A */
|
---|
169 | 0x1d|K_MOD, /* QZ_LCTRL 0x3B */
|
---|
170 | 0x36|K_MOD, /* QZ_RSHIFT 0x3C */
|
---|
171 | 0x38|K_EX|K_MOD, /* QZ_RALT 0x3D */
|
---|
172 | 0x1d|K_EX|K_MOD, /* QZ_RCTRL 0x3E */
|
---|
173 | 0, /* */
|
---|
174 | 0, /* */
|
---|
175 | 0x53, /* QZ_KP_PERIOD 0x41 */
|
---|
176 | 0, /* */
|
---|
177 | 0x37, /* QZ_KP_MULTIPLY 0x43 */
|
---|
178 | 0, /* */
|
---|
179 | 0x4e, /* QZ_KP_PLUS 0x45 */
|
---|
180 | 0, /* */
|
---|
181 | 0x45|K_LOCK, /* QZ_NUMLOCK 0x47 */
|
---|
182 | 0, /* */
|
---|
183 | 0, /* */
|
---|
184 | 0, /* */
|
---|
185 | 0x35|K_EX, /* QZ_KP_DIVIDE 0x4B */
|
---|
186 | 0x1c|K_EX, /* QZ_KP_ENTER 0x4C */
|
---|
187 | 0, /* */
|
---|
188 | 0x4a, /* QZ_KP_MINUS 0x4E */
|
---|
189 | 0, /* */
|
---|
190 | 0, /* */
|
---|
191 | 0x0d/*?*/, /* QZ_KP_EQUALS 0x51 */
|
---|
192 | 0x52, /* QZ_KP0 0x52 */
|
---|
193 | 0x4f, /* QZ_KP1 0x53 */
|
---|
194 | 0x50, /* QZ_KP2 0x54 */
|
---|
195 | 0x51, /* QZ_KP3 0x55 */
|
---|
196 | 0x4b, /* QZ_KP4 0x56 */
|
---|
197 | 0x4c, /* QZ_KP5 0x57 */
|
---|
198 | 0x4d, /* QZ_KP6 0x58 */
|
---|
199 | 0x47, /* QZ_KP7 0x59 */
|
---|
200 | 0, /* */
|
---|
201 | 0x48, /* QZ_KP8 0x5B */
|
---|
202 | 0x49, /* QZ_KP9 0x5C */
|
---|
203 | 0, /* */
|
---|
204 | 0, /* */
|
---|
205 | 0, /* */
|
---|
206 | 0x3f, /* QZ_F5 0x60 */
|
---|
207 | 0x40, /* QZ_F6 0x61 */
|
---|
208 | 0x41, /* QZ_F7 0x62 */
|
---|
209 | 0x3d, /* QZ_F3 0x63 */
|
---|
210 | 0x42, /* QZ_F8 0x64 */
|
---|
211 | 0x43, /* QZ_F9 0x65 */
|
---|
212 | 0, /* */
|
---|
213 | 0x57, /* QZ_F11 0x67 */
|
---|
214 | 0, /* */
|
---|
215 | 0x37|K_EX, /* QZ_PRINT / F13 0x69 */
|
---|
216 | 0x63, /* QZ_F16 0x6A */
|
---|
217 | 0x46|K_LOCK, /* QZ_SCROLLOCK 0x6B */
|
---|
218 | 0, /* */
|
---|
219 | 0x44, /* QZ_F10 0x6D */
|
---|
220 | 0x5d|K_EX, /* */
|
---|
221 | 0x58, /* QZ_F12 0x6F */
|
---|
222 | 0, /* */
|
---|
223 | 0/* 0xe1,0x1d,0x45*/, /* QZ_PAUSE 0x71 */
|
---|
224 | 0x52|K_EX, /* QZ_INSERT / HELP 0x72 */
|
---|
225 | 0x47|K_EX, /* QZ_HOME 0x73 */
|
---|
226 | 0x49|K_EX, /* QZ_PAGEUP 0x74 */
|
---|
227 | 0x53|K_EX, /* QZ_DELETE 0x75 */
|
---|
228 | 0x3e, /* QZ_F4 0x76 */
|
---|
229 | 0x4f|K_EX, /* QZ_END 0x77 */
|
---|
230 | 0x3c, /* QZ_F2 0x78 */
|
---|
231 | 0x51|K_EX, /* QZ_PAGEDOWN 0x79 */
|
---|
232 | 0x3b, /* QZ_F1 0x7A */
|
---|
233 | 0x4b|K_EX, /* QZ_LEFT 0x7B */
|
---|
234 | 0x4d|K_EX, /* QZ_RIGHT 0x7C */
|
---|
235 | 0x50|K_EX, /* QZ_DOWN 0x7D */
|
---|
236 | 0x48|K_EX, /* QZ_UP 0x7E */
|
---|
237 | 0x5e|K_EX, /* QZ_POWER 0x7F */ /* have different break key! */
|
---|
238 | };
|
---|
239 |
|
---|
240 |
|
---|
241 | /** Keeping track of whether we disabled the hotkeys or not. */
|
---|
242 | static bool g_fHotKeysDisabled = false;
|
---|
243 | /** Whether we've connected or not. */
|
---|
244 | static bool g_fConnectedToCGS = false;
|
---|
245 | /** Cached connection. */
|
---|
246 | static CGSConnection g_CGSConnection;
|
---|
247 |
|
---|
248 |
|
---|
249 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
250 |
|
---|
251 | /** The IO Master Port. */
|
---|
252 | static mach_port_t g_MasterPort = NULL;
|
---|
253 |
|
---|
254 | /** Keyboards in the cache. */
|
---|
255 | static unsigned g_cKeyboards = 0;
|
---|
256 | /** Array of cached keyboard data. */
|
---|
257 | static struct KeyboardCacheData
|
---|
258 | {
|
---|
259 | /** The device interface. */
|
---|
260 | IOHIDDeviceInterface **ppHidDeviceInterface;
|
---|
261 | /** The queue interface. */
|
---|
262 | IOHIDQueueInterface **ppHidQueueInterface;
|
---|
263 |
|
---|
264 | /* cookie translation array. */
|
---|
265 | struct KeyboardCacheCookie
|
---|
266 | {
|
---|
267 | /** The cookie. */
|
---|
268 | IOHIDElementCookie Cookie;
|
---|
269 | /** The corresponding modifier mask. */
|
---|
270 | uint32_t fMask;
|
---|
271 | } aCookies[64];
|
---|
272 | /** Number of cookies in the array. */
|
---|
273 | unsigned cCookies;
|
---|
274 | } g_aKeyboards[128];
|
---|
275 | /** The keyboard cache creation timestamp. */
|
---|
276 | static uint64_t g_u64KeyboardTS = 0;
|
---|
277 |
|
---|
278 | /** HID queue status. */
|
---|
279 | static bool g_fHIDQueueEnabled;
|
---|
280 | /** The current modifier mask. */
|
---|
281 | static uint32_t g_fHIDModifierMask;
|
---|
282 | /** The old modifier mask. */
|
---|
283 | static uint32_t g_fOldHIDModifierMask;
|
---|
284 |
|
---|
285 | #endif /* USE_HID_FOR_MODIFIERS */
|
---|
286 |
|
---|
287 |
|
---|
288 | /*******************************************************************************
|
---|
289 | * Internal Functions *
|
---|
290 | *******************************************************************************/
|
---|
291 | static void darwinBruteForcePropertySearch(CFDictionaryRef DictRef, struct KeyboardCacheData *pKeyboardEntry);
|
---|
292 |
|
---|
293 |
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Converts a darwin (virtual) key code to a set 1 scan code.
|
---|
297 | *
|
---|
298 | * @returns set 1 scan code.
|
---|
299 | * @param uKeyCode The darwin key code.
|
---|
300 | */
|
---|
301 | unsigned DarwinKeycodeToSet1Scancode(unsigned uKeyCode)
|
---|
302 | {
|
---|
303 | if (uKeyCode >= RT_ELEMENTS(g_aDarwinToSet1))
|
---|
304 | return 0;
|
---|
305 | return g_aDarwinToSet1[uKeyCode];
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Converts a single modifier to a set 1 scan code.
|
---|
311 | *
|
---|
312 | * @returns Set 1 scan code.
|
---|
313 | * @returns ~0U if more than one modifier is set.
|
---|
314 | * @param fModifiers The darwin modifier mask.
|
---|
315 | */
|
---|
316 | unsigned DarwinModifierMaskToSet1Scancode(UInt32 fModifiers)
|
---|
317 | {
|
---|
318 | unsigned uScanCode = DarwinModifierMaskToDarwinKeycode(fModifiers);
|
---|
319 | if (uScanCode < RT_ELEMENTS(g_aDarwinToSet1))
|
---|
320 | uScanCode = g_aDarwinToSet1[uScanCode];
|
---|
321 | else
|
---|
322 | Assert(uScanCode == ~0U);
|
---|
323 | return uScanCode;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Converts a single modifier to a darwin keycode.
|
---|
329 | *
|
---|
330 | * @returns Darwin keycode.
|
---|
331 | * @returns 0 if none of the support masks were set.
|
---|
332 | * @returns ~0U if more than one modifier is set.
|
---|
333 | * @param fModifiers The darwin modifier mask.
|
---|
334 | */
|
---|
335 | unsigned DarwinModifierMaskToDarwinKeycode(UInt32 fModifiers)
|
---|
336 | {
|
---|
337 | unsigned uKeyCode;
|
---|
338 |
|
---|
339 | /** @todo find symbols for these keycodes... */
|
---|
340 | fModifiers &= shiftKey | rightShiftKey | controlKey | rightControlKey | optionKey | rightOptionKey | cmdKey
|
---|
341 | | kEventKeyModifierRightCmdKeyMask | kEventKeyModifierNumLockMask | alphaLock;
|
---|
342 | if (fModifiers == shiftKey)
|
---|
343 | uKeyCode = QZ_LSHIFT;
|
---|
344 | else if (fModifiers == rightShiftKey)
|
---|
345 | uKeyCode = QZ_RSHIFT;
|
---|
346 | else if (fModifiers == controlKey)
|
---|
347 | uKeyCode = QZ_LCTRL;
|
---|
348 | else if (fModifiers == rightControlKey)
|
---|
349 | uKeyCode = QZ_RCTRL;
|
---|
350 | else if (fModifiers == optionKey)
|
---|
351 | uKeyCode = QZ_LALT;
|
---|
352 | else if (fModifiers == rightOptionKey)
|
---|
353 | uKeyCode = QZ_RALT;
|
---|
354 | else if (fModifiers == cmdKey)
|
---|
355 | uKeyCode = QZ_LMETA;
|
---|
356 | else if (fModifiers == kEventKeyModifierRightCmdKeyMask /* hack */)
|
---|
357 | uKeyCode = QZ_RMETA;
|
---|
358 | else if (fModifiers == alphaLock)
|
---|
359 | uKeyCode = QZ_CAPSLOCK;
|
---|
360 | else if (fModifiers == kEventKeyModifierNumLockMask)
|
---|
361 | uKeyCode = QZ_NUMLOCK;
|
---|
362 | else if (fModifiers == 0)
|
---|
363 | uKeyCode = 0;
|
---|
364 | else
|
---|
365 | uKeyCode = ~0U; /* multiple */
|
---|
366 | return uKeyCode;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Converts a darwin keycode to a modifier mask.
|
---|
372 | *
|
---|
373 | * @returns Darwin modifier mask.
|
---|
374 | * @returns 0 if the keycode isn't a modifier we know.
|
---|
375 | * @param uKeyCode The darwin
|
---|
376 | */
|
---|
377 | UInt32 DarwinKeyCodeToDarwinModifierMask(unsigned uKeyCode)
|
---|
378 | {
|
---|
379 | UInt32 fModifiers;
|
---|
380 |
|
---|
381 | /** @todo find symbols for these keycodes... */
|
---|
382 | if (uKeyCode == QZ_LSHIFT)
|
---|
383 | fModifiers = shiftKey;
|
---|
384 | else if (uKeyCode == QZ_RSHIFT)
|
---|
385 | fModifiers = rightShiftKey;
|
---|
386 | else if (uKeyCode == QZ_LCTRL)
|
---|
387 | fModifiers = controlKey;
|
---|
388 | else if (uKeyCode == QZ_RCTRL)
|
---|
389 | fModifiers = rightControlKey;
|
---|
390 | else if (uKeyCode == QZ_LALT)
|
---|
391 | fModifiers = optionKey;
|
---|
392 | else if (uKeyCode == QZ_RALT)
|
---|
393 | fModifiers = rightOptionKey;
|
---|
394 | else if (uKeyCode == QZ_LMETA)
|
---|
395 | fModifiers = cmdKey;
|
---|
396 | else if (uKeyCode == QZ_RMETA)
|
---|
397 | fModifiers = kEventKeyModifierRightCmdKeyMask; /* hack */
|
---|
398 | else if (uKeyCode == QZ_CAPSLOCK)
|
---|
399 | fModifiers = alphaLock;
|
---|
400 | else if (uKeyCode == QZ_NUMLOCK)
|
---|
401 | fModifiers = kEventKeyModifierNumLockMask;
|
---|
402 | else
|
---|
403 | fModifiers = 0;
|
---|
404 | return fModifiers;
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * Disables or enabled global hot keys.
|
---|
410 | *
|
---|
411 | * @param fDisable Pass 'true' to disable the hot keys, pass 'false' to re-enable them.
|
---|
412 | */
|
---|
413 | void DarwinDisableGlobalHotKeys(bool fDisable)
|
---|
414 | {
|
---|
415 | static unsigned s_cComplaints = 0;
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * Lazy connect to the core graphics service.
|
---|
419 | */
|
---|
420 | if (!g_fConnectedToCGS)
|
---|
421 | {
|
---|
422 | g_CGSConnection = _CGSDefaultConnection();
|
---|
423 | g_fConnectedToCGS = true;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Get the current mode.
|
---|
428 | */
|
---|
429 | CGSGlobalHotKeyOperatingMode enmMode = kCGSGlobalHotKeyInvalid;
|
---|
430 | CGSGetGlobalHotKeyOperatingMode(g_CGSConnection, &enmMode);
|
---|
431 | if ( enmMode != kCGSGlobalHotKeyEnable
|
---|
432 | && enmMode != kCGSGlobalHotKeyDisable)
|
---|
433 | {
|
---|
434 | AssertMsgFailed(("%d\n", enmMode));
|
---|
435 | if (s_cComplaints++ < 32)
|
---|
436 | LogRel(("DarwinDisableGlobalHotKeys: Unexpected enmMode=%d\n", enmMode));
|
---|
437 | return;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /*
|
---|
441 | * Calc the new mode.
|
---|
442 | */
|
---|
443 | if (fDisable)
|
---|
444 | {
|
---|
445 | if (enmMode != kCGSGlobalHotKeyEnable)
|
---|
446 | return;
|
---|
447 | enmMode = kCGSGlobalHotKeyDisable;
|
---|
448 | }
|
---|
449 | else
|
---|
450 | {
|
---|
451 | if ( enmMode != kCGSGlobalHotKeyDisable
|
---|
452 | /*|| !g_fHotKeysDisabled*/)
|
---|
453 | return;
|
---|
454 | enmMode = kCGSGlobalHotKeyEnable;
|
---|
455 | }
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * Try set it and check the actual result.
|
---|
459 | */
|
---|
460 | CGSSetGlobalHotKeyOperatingMode(g_CGSConnection, enmMode);
|
---|
461 | CGSGlobalHotKeyOperatingMode enmNewMode = kCGSGlobalHotKeyInvalid;
|
---|
462 | CGSGetGlobalHotKeyOperatingMode(g_CGSConnection, &enmNewMode);
|
---|
463 | if (enmNewMode == enmMode)
|
---|
464 | g_fHotKeysDisabled = enmMode == kCGSGlobalHotKeyDisable;
|
---|
465 | else
|
---|
466 | {
|
---|
467 | AssertMsgFailed(("enmNewMode=%d enmMode=%d\n", enmNewMode, enmMode));
|
---|
468 | if (s_cComplaints++ < 32)
|
---|
469 | LogRel(("DarwinDisableGlobalHotKeys: Failed to change mode; enmNewMode=%d enmMode=%d\n", enmNewMode, enmMode));
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 |
|
---|
475 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
476 |
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * Callback function for consuming queued events.
|
---|
480 | *
|
---|
481 | * @param pvTarget queue?
|
---|
482 | * @param rcIn ?
|
---|
483 | * @param pvRefcon Pointer to the keyboard cache entry.
|
---|
484 | * @param pvSender ?
|
---|
485 | */
|
---|
486 | static void darwinQueueCallback(void *pvTarget, IOReturn rcIn, void *pvRefcon, void *pvSender)
|
---|
487 | {
|
---|
488 | struct KeyboardCacheData *pKeyboardEntry = (struct KeyboardCacheData *)pvRefcon;
|
---|
489 | if (!pKeyboardEntry->ppHidQueueInterface)
|
---|
490 | return;
|
---|
491 | NOREF(pvTarget);
|
---|
492 | NOREF(rcIn);
|
---|
493 | NOREF(pvSender);
|
---|
494 |
|
---|
495 | /*
|
---|
496 | * Consume the events.
|
---|
497 | */
|
---|
498 | g_fOldHIDModifierMask = g_fHIDModifierMask;
|
---|
499 | for (;;)
|
---|
500 | {
|
---|
501 | #ifdef DEBUG_PRINTF
|
---|
502 | RTPrintf("dbg-ev: "); RTStrmFlush(g_pStdOut);
|
---|
503 | #endif
|
---|
504 | IOHIDEventStruct Event;
|
---|
505 | AbsoluteTime ZeroTime = {0,0};
|
---|
506 | IOReturn rc = (*pKeyboardEntry->ppHidQueueInterface)->getNextEvent(pKeyboardEntry->ppHidQueueInterface,
|
---|
507 | &Event, ZeroTime, 0);
|
---|
508 | if (rc != kIOReturnSuccess)
|
---|
509 | break;
|
---|
510 |
|
---|
511 | /* Translate the cookie value to a modifier mask. */
|
---|
512 | uint32_t fMask = 0;
|
---|
513 | unsigned i = pKeyboardEntry->cCookies;
|
---|
514 | while (i-- > 0)
|
---|
515 | {
|
---|
516 | if (pKeyboardEntry->aCookies[i].Cookie == Event.elementCookie)
|
---|
517 | {
|
---|
518 | fMask = pKeyboardEntry->aCookies[i].fMask;
|
---|
519 | break;
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*
|
---|
524 | * Adjust the modifier mask.
|
---|
525 | *
|
---|
526 | * Note that we don't bother to deal with anyone pressing the same modifier
|
---|
527 | * on 2 or more keyboard. That's not worth the effort involved.
|
---|
528 | */
|
---|
529 | if (Event.value)
|
---|
530 | g_fHIDModifierMask |= fMask;
|
---|
531 | else
|
---|
532 | g_fHIDModifierMask &= ~fMask;
|
---|
533 | #ifdef DEBUG_PRINTF
|
---|
534 | RTPrintf("t=%d c=%#x v=%#x cblv=%d lv=%p m=%#X\n", Event.type, Event.elementCookie, Event.value, Event.longValueSize, Event.value, fMask); RTStrmFlush(g_pStdOut);
|
---|
535 | #endif
|
---|
536 | }
|
---|
537 | #ifdef DEBUG_PRINTF
|
---|
538 | RTPrintf("dbg-ev: done\n"); RTStrmFlush(g_pStdOut);
|
---|
539 | #endif
|
---|
540 | }
|
---|
541 |
|
---|
542 |
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * Element enumeration callback.
|
---|
546 | */
|
---|
547 | static void darwinBruteForcePropertySearchApplier(const void *pvValue, void *pvCacheEntry)
|
---|
548 | {
|
---|
549 | if (CFGetTypeID(pvValue) == CFDictionaryGetTypeID())
|
---|
550 | darwinBruteForcePropertySearch((CFMutableDictionaryRef)pvValue, (struct KeyboardCacheData *)pvCacheEntry);
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Recurses thru the keyboard properties looking for certain keys.
|
---|
556 | *
|
---|
557 | * @remark Yes, this can probably be done in a more efficient way. If you
|
---|
558 | * know how to do this, don't hesitate to let us know!
|
---|
559 | */
|
---|
560 | static void darwinBruteForcePropertySearch(CFDictionaryRef DictRef, struct KeyboardCacheData *pKeyboardEntry)
|
---|
561 | {
|
---|
562 | CFTypeRef ObjRef;
|
---|
563 |
|
---|
564 | /*
|
---|
565 | * Check for the usage page and usage key we want.
|
---|
566 | */
|
---|
567 | long lUsage;
|
---|
568 | ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementUsageKey));
|
---|
569 | if ( ObjRef
|
---|
570 | && CFGetTypeID(ObjRef) == CFNumberGetTypeID()
|
---|
571 | && CFNumberGetValue((CFNumberRef)ObjRef, kCFNumberLongType, &lUsage))
|
---|
572 | {
|
---|
573 | switch (lUsage)
|
---|
574 | {
|
---|
575 | case kHIDUsage_KeyboardLeftControl:
|
---|
576 | case kHIDUsage_KeyboardLeftShift:
|
---|
577 | case kHIDUsage_KeyboardLeftAlt:
|
---|
578 | case kHIDUsage_KeyboardLeftGUI:
|
---|
579 | case kHIDUsage_KeyboardRightControl:
|
---|
580 | case kHIDUsage_KeyboardRightShift:
|
---|
581 | case kHIDUsage_KeyboardRightAlt:
|
---|
582 | case kHIDUsage_KeyboardRightGUI:
|
---|
583 | {
|
---|
584 | long lPage;
|
---|
585 | ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementUsagePageKey));
|
---|
586 | if ( !ObjRef
|
---|
587 | || CFGetTypeID(ObjRef) != CFNumberGetTypeID()
|
---|
588 | || !CFNumberGetValue((CFNumberRef)ObjRef, kCFNumberLongType, &lPage)
|
---|
589 | || lPage != kHIDPage_KeyboardOrKeypad)
|
---|
590 | break;
|
---|
591 |
|
---|
592 | if (pKeyboardEntry->cCookies >= RT_ELEMENTS(pKeyboardEntry->aCookies))
|
---|
593 | {
|
---|
594 | AssertMsgFailed(("too many cookies!\n"));
|
---|
595 | break;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /*
|
---|
599 | * Get the cookie and modifier mask.
|
---|
600 | */
|
---|
601 | long lCookie;
|
---|
602 | ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementCookieKey));
|
---|
603 | if ( !ObjRef
|
---|
604 | || CFGetTypeID(ObjRef) != CFNumberGetTypeID()
|
---|
605 | || !CFNumberGetValue((CFNumberRef)ObjRef, kCFNumberLongType, &lCookie))
|
---|
606 | break;
|
---|
607 |
|
---|
608 | uint32_t fMask;
|
---|
609 | switch (lUsage)
|
---|
610 | {
|
---|
611 | case kHIDUsage_KeyboardLeftControl : fMask = controlKey; break;
|
---|
612 | case kHIDUsage_KeyboardLeftShift : fMask = shiftKey; break;
|
---|
613 | case kHIDUsage_KeyboardLeftAlt : fMask = optionKey; break;
|
---|
614 | case kHIDUsage_KeyboardLeftGUI : fMask = cmdKey; break;
|
---|
615 | case kHIDUsage_KeyboardRightControl: fMask = rightControlKey; break;
|
---|
616 | case kHIDUsage_KeyboardRightShift : fMask = rightShiftKey; break;
|
---|
617 | case kHIDUsage_KeyboardRightAlt : fMask = rightOptionKey; break;
|
---|
618 | case kHIDUsage_KeyboardRightGUI : fMask = kEventKeyModifierRightCmdKeyMask; break;
|
---|
619 | default: AssertMsgFailed(("%ld\n",lUsage)); fMask = 0; break;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * If we've got a queue, add the cookie to the queue.
|
---|
624 | */
|
---|
625 | if (pKeyboardEntry->ppHidQueueInterface)
|
---|
626 | {
|
---|
627 | IOReturn rc = (*pKeyboardEntry->ppHidQueueInterface)->addElement(pKeyboardEntry->ppHidQueueInterface, (IOHIDElementCookie)lCookie, 0);
|
---|
628 | AssertMsg(rc == kIOReturnSuccess, ("rc=%d\n", rc));
|
---|
629 | #ifdef DEBUG_PRINTF
|
---|
630 | RTPrintf("dbg-add: u=%#lx c=%#lx\n", lUsage, lCookie);
|
---|
631 | #endif
|
---|
632 | }
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Add the cookie to the keyboard entry.
|
---|
636 | */
|
---|
637 | pKeyboardEntry->aCookies[pKeyboardEntry->cCookies].Cookie = (IOHIDElementCookie)lCookie;
|
---|
638 | pKeyboardEntry->aCookies[pKeyboardEntry->cCookies].fMask = fMask;
|
---|
639 | ++pKeyboardEntry->cCookies;
|
---|
640 | break;
|
---|
641 | }
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Get the elements key and recursivly iterate the elements looking
|
---|
648 | * for they key cookies.
|
---|
649 | */
|
---|
650 | ObjRef = CFDictionaryGetValue(DictRef, CFSTR(kIOHIDElementKey));
|
---|
651 | if ( ObjRef
|
---|
652 | && CFGetTypeID(ObjRef) == CFArrayGetTypeID())
|
---|
653 | {
|
---|
654 | CFArrayRef ArrayObjRef = (CFArrayRef)ObjRef;
|
---|
655 | CFRange Range = {0, CFArrayGetCount(ArrayObjRef)};
|
---|
656 | CFArrayApplyFunction(ArrayObjRef, Range, darwinBruteForcePropertySearchApplier, pKeyboardEntry);
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Creates a keyboard cache entry.
|
---|
663 | *
|
---|
664 | * @returns true if the entry was created successfully, otherwise false.
|
---|
665 | * @param pKeyboardEntry Pointer to the entry.
|
---|
666 | * @param KeyboardDevice The keyboard device to create the entry for.
|
---|
667 | *
|
---|
668 | */
|
---|
669 | static bool darwinHIDKeyboardCacheCreateEntry(struct KeyboardCacheData *pKeyboardEntry, io_object_t KeyboardDevice)
|
---|
670 | {
|
---|
671 | unsigned long cRefs = 0;
|
---|
672 | memset(pKeyboardEntry, 0, sizeof(*pKeyboardEntry));
|
---|
673 |
|
---|
674 | /*
|
---|
675 | * Query the HIDDeviceInterface for this HID (keyboard) object.
|
---|
676 | */
|
---|
677 | SInt32 Score = 0;
|
---|
678 | IOCFPlugInInterface **ppPlugInInterface = NULL;
|
---|
679 | IOReturn rc = IOCreatePlugInInterfaceForService(KeyboardDevice, kIOHIDDeviceUserClientTypeID,
|
---|
680 | kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
|
---|
681 | if (rc == kIOReturnSuccess)
|
---|
682 | {
|
---|
683 | IOHIDDeviceInterface **ppHidDeviceInterface = NULL;
|
---|
684 | HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
|
---|
685 | CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
|
---|
686 | (LPVOID *)&ppHidDeviceInterface);
|
---|
687 | cRefs = (*ppPlugInInterface)->Release(ppPlugInInterface); MY_CHECK_CREFS(cRefs);
|
---|
688 | ppPlugInInterface = NULL;
|
---|
689 | if (hrc == S_OK)
|
---|
690 | {
|
---|
691 | rc = (*ppHidDeviceInterface)->open(ppHidDeviceInterface, 0);
|
---|
692 | if (rc == kIOReturnSuccess)
|
---|
693 | {
|
---|
694 | /*
|
---|
695 | * create a removal callback.
|
---|
696 | */
|
---|
697 | /** @todo */
|
---|
698 |
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Create the queue so we can insert elements while searching the properties.
|
---|
702 | */
|
---|
703 | IOHIDQueueInterface **ppHidQueueInterface = (*ppHidDeviceInterface)->allocQueue(ppHidDeviceInterface);
|
---|
704 | if (ppHidQueueInterface)
|
---|
705 | {
|
---|
706 | rc = (*ppHidQueueInterface)->create(ppHidQueueInterface, 0, 32);
|
---|
707 | if (rc != kIOReturnSuccess)
|
---|
708 | {
|
---|
709 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
710 | cRefs = (*ppHidQueueInterface)->Release(ppHidQueueInterface); MY_CHECK_CREFS(cRefs);
|
---|
711 | ppHidQueueInterface = NULL;
|
---|
712 | }
|
---|
713 | }
|
---|
714 | else
|
---|
715 | AssertFailed();
|
---|
716 | pKeyboardEntry->ppHidQueueInterface = ppHidQueueInterface;
|
---|
717 |
|
---|
718 | /*
|
---|
719 | * Brute force getting of attributes.
|
---|
720 | */
|
---|
721 | /** @todo read up on how to do this in a less resource intensive way! Suggestions are welcome! */
|
---|
722 | CFMutableDictionaryRef PropertiesRef = 0;
|
---|
723 | kern_return_t krc = IORegistryEntryCreateCFProperties(KeyboardDevice, &PropertiesRef, kCFAllocatorDefault, kNilOptions);
|
---|
724 | if (krc == KERN_SUCCESS)
|
---|
725 | {
|
---|
726 | darwinBruteForcePropertySearch(PropertiesRef, pKeyboardEntry);
|
---|
727 | CFRelease(PropertiesRef);
|
---|
728 | }
|
---|
729 | else
|
---|
730 | AssertMsgFailed(("krc=%#x\n", krc));
|
---|
731 |
|
---|
732 | if (ppHidQueueInterface)
|
---|
733 | {
|
---|
734 | /*
|
---|
735 | * Now install our queue callback.
|
---|
736 | */
|
---|
737 | CFRunLoopSourceRef RunLoopSrcRef = NULL;
|
---|
738 | rc = (*ppHidQueueInterface)->createAsyncEventSource(ppHidQueueInterface, &RunLoopSrcRef);
|
---|
739 | if (rc == kIOReturnSuccess)
|
---|
740 | {
|
---|
741 | CFRunLoopRef RunLoopRef = (CFRunLoopRef)GetCFRunLoopFromEventLoop(GetMainEventLoop());
|
---|
742 | CFRunLoopAddSource(RunLoopRef, RunLoopSrcRef, kCFRunLoopDefaultMode);
|
---|
743 | }
|
---|
744 |
|
---|
745 | /*
|
---|
746 | * Now install our queue callback.
|
---|
747 | */
|
---|
748 | rc = (*ppHidQueueInterface)->setEventCallout(ppHidQueueInterface, darwinQueueCallback, ppHidQueueInterface, pKeyboardEntry);
|
---|
749 | if (rc != kIOReturnSuccess)
|
---|
750 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
751 | }
|
---|
752 |
|
---|
753 | /*
|
---|
754 | * Complete the new keyboard cache entry.
|
---|
755 | */
|
---|
756 | pKeyboardEntry->ppHidDeviceInterface = ppHidDeviceInterface;
|
---|
757 | pKeyboardEntry->ppHidQueueInterface = ppHidQueueInterface;
|
---|
758 | return true;
|
---|
759 | }
|
---|
760 |
|
---|
761 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
762 | cRefs = (*ppHidDeviceInterface)->Release(ppHidDeviceInterface); MY_CHECK_CREFS(cRefs);
|
---|
763 | }
|
---|
764 | else
|
---|
765 | AssertMsgFailed(("hrc=%#x\n", hrc));
|
---|
766 | }
|
---|
767 | else
|
---|
768 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
769 |
|
---|
770 | return false;
|
---|
771 | }
|
---|
772 |
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * Destroys a keyboard cache entry.
|
---|
776 | *
|
---|
777 | * @param pKeyboardEntry The entry.
|
---|
778 | */
|
---|
779 | static void darwinHIDKeyboardCacheDestroyEntry(struct KeyboardCacheData *pKeyboardEntry)
|
---|
780 | {
|
---|
781 | unsigned long cRefs;
|
---|
782 |
|
---|
783 | /*
|
---|
784 | * Destroy the queue
|
---|
785 | */
|
---|
786 | if (pKeyboardEntry->ppHidQueueInterface)
|
---|
787 | {
|
---|
788 | IOHIDQueueInterface **ppHidQueueInterface = pKeyboardEntry->ppHidQueueInterface;
|
---|
789 | pKeyboardEntry->ppHidQueueInterface = NULL;
|
---|
790 |
|
---|
791 | /* stop it just in case we haven't done so. doesn't really matter I think. */
|
---|
792 | (*ppHidQueueInterface)->stop(ppHidQueueInterface);
|
---|
793 |
|
---|
794 | /* deal with the run loop source. */
|
---|
795 | CFRunLoopSourceRef RunLoopSrcRef = (*ppHidQueueInterface)->getAsyncEventSource(ppHidQueueInterface);
|
---|
796 | if (RunLoopSrcRef)
|
---|
797 | {
|
---|
798 | CFRunLoopRef RunLoopRef = (CFRunLoopRef)GetCFRunLoopFromEventLoop(GetMainEventLoop());
|
---|
799 | CFRunLoopRemoveSource(RunLoopRef, RunLoopSrcRef, kCFRunLoopDefaultMode);
|
---|
800 |
|
---|
801 | CFRelease(RunLoopSrcRef);
|
---|
802 | }
|
---|
803 |
|
---|
804 | /* dispose of and release the queue. */
|
---|
805 | (*ppHidQueueInterface)->dispose(ppHidQueueInterface);
|
---|
806 | cRefs = (*ppHidQueueInterface)->Release(ppHidQueueInterface); MY_CHECK_CREFS(cRefs);
|
---|
807 | }
|
---|
808 |
|
---|
809 | /*
|
---|
810 | * Release the removal hook?
|
---|
811 | */
|
---|
812 | /** @todo */
|
---|
813 |
|
---|
814 | /*
|
---|
815 | * Close and release the device interface.
|
---|
816 | */
|
---|
817 | if (pKeyboardEntry->ppHidDeviceInterface)
|
---|
818 | {
|
---|
819 | IOHIDDeviceInterface **ppHidDeviceInterface = pKeyboardEntry->ppHidDeviceInterface;
|
---|
820 | pKeyboardEntry->ppHidDeviceInterface = NULL;
|
---|
821 |
|
---|
822 | (*ppHidDeviceInterface)->close(ppHidDeviceInterface);
|
---|
823 | cRefs = (*ppHidDeviceInterface)->Release(ppHidDeviceInterface); MY_CHECK_CREFS(cRefs);
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 |
|
---|
828 | /**
|
---|
829 | * Zap the keyboard cache.
|
---|
830 | */
|
---|
831 | static void darwinHIDKeyboardCacheZap(void)
|
---|
832 | {
|
---|
833 | /*
|
---|
834 | * Release the old cache data first.
|
---|
835 | */
|
---|
836 | while (g_cKeyboards > 0)
|
---|
837 | {
|
---|
838 | unsigned i = --g_cKeyboards;
|
---|
839 | darwinHIDKeyboardCacheDestroyEntry(&g_aKeyboards[i]);
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Updates the cached keyboard data.
|
---|
846 | *
|
---|
847 | * @todo The current implementation is very brute force...
|
---|
848 | * Rewrite it so that it doesn't flush the cache completely but simply checks whether
|
---|
849 | * anything has changed in the HID config. With any luck, there might even be a callback
|
---|
850 | * or something we can poll for HID config changes...
|
---|
851 | * setRemovalCallback() is a start...
|
---|
852 | */
|
---|
853 | static void darwinHIDKeyboardCacheDoUpdate(void)
|
---|
854 | {
|
---|
855 | g_u64KeyboardTS = RTTimeMilliTS();
|
---|
856 |
|
---|
857 | /*
|
---|
858 | * Dispense with the old cache data.
|
---|
859 | */
|
---|
860 | darwinHIDKeyboardCacheZap();
|
---|
861 |
|
---|
862 | /*
|
---|
863 | * Open the master port on the first invocation.
|
---|
864 | */
|
---|
865 | if (!g_MasterPort)
|
---|
866 | {
|
---|
867 | kern_return_t krc = IOMasterPort(MACH_PORT_NULL, &g_MasterPort);
|
---|
868 | AssertReturnVoid(krc == KERN_SUCCESS);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /*
|
---|
872 | * Create a matching dictionary for searching for keyboards devices.
|
---|
873 | */
|
---|
874 | static const UInt32 s_Page = kHIDPage_GenericDesktop;
|
---|
875 | static const UInt32 s_Usage = kHIDUsage_GD_Keyboard;
|
---|
876 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOHIDDeviceKey);
|
---|
877 | AssertReturnVoid(RefMatchingDict);
|
---|
878 | CFDictionarySetValue(RefMatchingDict, CFSTR(kIOHIDPrimaryUsagePageKey),
|
---|
879 | CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &s_Page));
|
---|
880 | CFDictionarySetValue(RefMatchingDict, CFSTR(kIOHIDPrimaryUsageKey),
|
---|
881 | CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &s_Usage));
|
---|
882 |
|
---|
883 | /*
|
---|
884 | * Perform the search and get a collection of keyboard devices.
|
---|
885 | */
|
---|
886 | io_iterator_t Keyboards = NULL;
|
---|
887 | IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &Keyboards);
|
---|
888 | AssertMsgReturnVoid(rc == kIOReturnSuccess, ("rc=%d\n", rc));
|
---|
889 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * Enumerate the keyboards and query the cache data.
|
---|
893 | */
|
---|
894 | unsigned i = 0;
|
---|
895 | io_object_t KeyboardDevice;
|
---|
896 | while ( i < RT_ELEMENTS(g_aKeyboards)
|
---|
897 | && (KeyboardDevice = IOIteratorNext(Keyboards)) != 0)
|
---|
898 | {
|
---|
899 | if (darwinHIDKeyboardCacheCreateEntry(&g_aKeyboards[i], KeyboardDevice))
|
---|
900 | i++;
|
---|
901 | IOObjectRelease(KeyboardDevice);
|
---|
902 | }
|
---|
903 | g_cKeyboards = i;
|
---|
904 |
|
---|
905 | IOObjectRelease(Keyboards);
|
---|
906 | }
|
---|
907 |
|
---|
908 |
|
---|
909 | /**
|
---|
910 | * Updates the keyboard cache if it's time to do it again.
|
---|
911 | */
|
---|
912 | static void darwinHIDKeyboardCacheUpdate(void)
|
---|
913 | {
|
---|
914 | if ( !g_cKeyboards
|
---|
915 | /*|| g_u64KeyboardTS - RTTimeMilliTS() > 7500*/ /* 7.5sec */)
|
---|
916 | darwinHIDKeyboardCacheDoUpdate();
|
---|
917 | }
|
---|
918 |
|
---|
919 |
|
---|
920 | /**
|
---|
921 | * Queries the modifier keys from the (IOKit) HID Manager.
|
---|
922 | *
|
---|
923 | * @returns Carbon modifier mask with right/left set correctly.
|
---|
924 | */
|
---|
925 | static UInt32 darwinQueryHIDModifiers(void)
|
---|
926 | {
|
---|
927 | /*
|
---|
928 | * Iterate thru the keyboards collecting their modifier masks.
|
---|
929 | */
|
---|
930 | UInt32 fHIDModifiers = 0;
|
---|
931 | unsigned i = g_cKeyboards;
|
---|
932 | while (i-- > 0)
|
---|
933 | {
|
---|
934 | IOHIDDeviceInterface **ppHidDeviceInterface = g_aKeyboards[i].ppHidDeviceInterface;
|
---|
935 | if (!ppHidDeviceInterface)
|
---|
936 | continue;
|
---|
937 |
|
---|
938 | unsigned j = g_aKeyboards[i].cCookies;
|
---|
939 | while (j-- > 0)
|
---|
940 | {
|
---|
941 | IOHIDEventStruct HidEvent;
|
---|
942 | IOReturn rc = (*ppHidDeviceInterface)->getElementValue(ppHidDeviceInterface,
|
---|
943 | g_aKeyboards[i].aCookies[j].Cookie,
|
---|
944 | &HidEvent);
|
---|
945 | if (rc == kIOReturnSuccess)
|
---|
946 | {
|
---|
947 | if (HidEvent.value)
|
---|
948 | fHIDModifiers |= g_aKeyboards[i].aCookies[j].fMask;
|
---|
949 | }
|
---|
950 | else
|
---|
951 | AssertMsgFailed(("rc=%#x\n", rc));
|
---|
952 | }
|
---|
953 | }
|
---|
954 |
|
---|
955 | return fHIDModifiers;
|
---|
956 | }
|
---|
957 |
|
---|
958 | #endif /* USE_HID_FOR_MODIFIERS */
|
---|
959 |
|
---|
960 |
|
---|
961 |
|
---|
962 | /**
|
---|
963 | * Left / rigth adjust the modifier mask using the current
|
---|
964 | * keyboard state.
|
---|
965 | *
|
---|
966 | * @returns left/right adjusted fModifiers.
|
---|
967 | * @param fModifiers The mask to adjust.
|
---|
968 | */
|
---|
969 | UInt32 DarwinAdjustModifierMask(UInt32 fModifiers)
|
---|
970 | {
|
---|
971 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
972 | /*
|
---|
973 | * Update the keyboard cache.
|
---|
974 | */
|
---|
975 | darwinHIDKeyboardCacheUpdate();
|
---|
976 |
|
---|
977 | /*
|
---|
978 | * Check if there is anything to adjust and perform the adjustment.
|
---|
979 | */
|
---|
980 | if (fModifiers & (shiftKey | rightShiftKey | controlKey | rightControlKey | optionKey | rightOptionKey | cmdKey | kEventKeyModifierRightCmdKeyMask))
|
---|
981 | {
|
---|
982 | const UInt32 fHIDModifiers = g_fHIDModifierMask;
|
---|
983 | #ifdef DEBUG_PRINTF
|
---|
984 | RTPrintf("dbg-fHIDModifier=%#x fModifiers=%#x", fHIDModifiers, fModifiers);
|
---|
985 | #endif
|
---|
986 | if ( (fModifiers & (rightShiftKey | shiftKey))
|
---|
987 | && (fHIDModifiers & (rightShiftKey | shiftKey)))
|
---|
988 | {
|
---|
989 | fModifiers &= ~(rightShiftKey | shiftKey);
|
---|
990 | fModifiers |= fHIDModifiers & (rightShiftKey | shiftKey);
|
---|
991 | }
|
---|
992 |
|
---|
993 | if ( (fModifiers & (rightControlKey | controlKey))
|
---|
994 | && (fHIDModifiers & (rightControlKey | controlKey)))
|
---|
995 | {
|
---|
996 | fModifiers &= ~(rightControlKey | controlKey);
|
---|
997 | fModifiers |= fHIDModifiers & (rightControlKey | controlKey);
|
---|
998 | }
|
---|
999 |
|
---|
1000 | if ( (fModifiers & (optionKey | rightOptionKey))
|
---|
1001 | && (fHIDModifiers & (optionKey | rightOptionKey)))
|
---|
1002 | {
|
---|
1003 | fModifiers &= ~(optionKey | rightOptionKey);
|
---|
1004 | fModifiers |= fHIDModifiers & (optionKey | rightOptionKey);
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | if ( (fModifiers & (cmdKey | kEventKeyModifierRightCmdKeyMask))
|
---|
1008 | && (fHIDModifiers & (cmdKey | kEventKeyModifierRightCmdKeyMask)))
|
---|
1009 | {
|
---|
1010 | fModifiers &= ~(cmdKey | kEventKeyModifierRightCmdKeyMask);
|
---|
1011 | fModifiers |= fHIDModifiers & (cmdKey | kEventKeyModifierRightCmdKeyMask);
|
---|
1012 | }
|
---|
1013 | #ifdef DEBUG_PRINTF
|
---|
1014 | RTPrintf(" -> %#x", fModifiers);
|
---|
1015 | #endif
|
---|
1016 | }
|
---|
1017 | #endif /* USE_HID_FOR_MODIFIERS */
|
---|
1018 |
|
---|
1019 | return fModifiers;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 |
|
---|
1023 | /**
|
---|
1024 | * Start grabbing keyboard events.
|
---|
1025 | *
|
---|
1026 | * This only concerns itself with modifiers and disabling global hotkeys (if requested).
|
---|
1027 | *
|
---|
1028 | * @param fGlobalHotkeys Whether to disable global hotkeys or not.
|
---|
1029 | */
|
---|
1030 | void DarwinGrabKeyboard(bool fGlobalHotkeys)
|
---|
1031 | {
|
---|
1032 | LogFlow(("DarwinGrabKeyboard: fGlobalHotkeys=%RTbool\n", fGlobalHotkeys));
|
---|
1033 |
|
---|
1034 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
1035 | /*
|
---|
1036 | * Update the keyboard cache.
|
---|
1037 | */
|
---|
1038 | darwinHIDKeyboardCacheUpdate();
|
---|
1039 |
|
---|
1040 | /*
|
---|
1041 | * Start the keyboard queues and query the current mask.
|
---|
1042 | */
|
---|
1043 | g_fHIDQueueEnabled = true;
|
---|
1044 |
|
---|
1045 | unsigned i = g_cKeyboards;
|
---|
1046 | while (i-- > 0)
|
---|
1047 | {
|
---|
1048 | if (g_aKeyboards[i].ppHidQueueInterface)
|
---|
1049 | (*g_aKeyboards[i].ppHidQueueInterface)->start(g_aKeyboards[i].ppHidQueueInterface);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | g_fHIDModifierMask = darwinQueryHIDModifiers();
|
---|
1053 | #endif /* USE_HID_FOR_MODIFIERS */
|
---|
1054 |
|
---|
1055 | /*
|
---|
1056 | * Disable hotkeys if requested.
|
---|
1057 | */
|
---|
1058 | if (fGlobalHotkeys)
|
---|
1059 | DarwinDisableGlobalHotKeys(true);
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 |
|
---|
1063 | /**
|
---|
1064 | * Reverses the actions taken by DarwinGrabKeyboard.
|
---|
1065 | */
|
---|
1066 | void DarwinReleaseKeyboard(void)
|
---|
1067 | {
|
---|
1068 | LogFlow(("DarwinReleaseKeyboard\n"));
|
---|
1069 |
|
---|
1070 | /*
|
---|
1071 | * Re-enable hotkeys.
|
---|
1072 | */
|
---|
1073 | DarwinDisableGlobalHotKeys(false);
|
---|
1074 |
|
---|
1075 | #ifdef USE_HID_FOR_MODIFIERS
|
---|
1076 | /*
|
---|
1077 | * Stop and drain the keyboard queues.
|
---|
1078 | */
|
---|
1079 | g_fHIDQueueEnabled = false;
|
---|
1080 |
|
---|
1081 | #if 0
|
---|
1082 | unsigned i = g_cKeyboards;
|
---|
1083 | while (i-- > 0)
|
---|
1084 | {
|
---|
1085 | if (g_aKeyboards[i].ppHidQueueInterface)
|
---|
1086 | {
|
---|
1087 |
|
---|
1088 | (*g_aKeyboards[i].ppHidQueueInterface)->stop(g_aKeyboards[i].ppHidQueueInterface);
|
---|
1089 |
|
---|
1090 | /* drain it */
|
---|
1091 | IOReturn rc;
|
---|
1092 | unsigned c = 0;
|
---|
1093 | do
|
---|
1094 | {
|
---|
1095 | IOHIDEventStruct Event;
|
---|
1096 | AbsoluteTime MaxTime = {0,0};
|
---|
1097 | rc = (*g_aKeyboards[i].ppHidQueueInterface)->getNextEvent(g_aKeyboards[i].ppHidQueueInterface,
|
---|
1098 | &Event, MaxTime, 0);
|
---|
1099 | } while ( rc == kIOReturnSuccess
|
---|
1100 | && c++ < 32);
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | #else
|
---|
1104 |
|
---|
1105 | /*
|
---|
1106 | * Kill the keyboard cache.
|
---|
1107 | * This will hopefully fix the crash in getElementValue()/fillElementValue()...
|
---|
1108 | */
|
---|
1109 | darwinHIDKeyboardCacheZap();
|
---|
1110 | #endif
|
---|
1111 |
|
---|
1112 | /* Clear the modifier mask. */
|
---|
1113 | g_fHIDModifierMask = 0;
|
---|
1114 | #endif /* USE_HID_FOR_MODIFIERS */
|
---|
1115 | }
|
---|
1116 |
|
---|