Debian Codegraph @

src/macosx/keybd.m

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * MacOS X keyboard module.
12 *
13 * By Angelo Mottola.
14 *
15 * Based on Unix/X11 version by Michael Bukin.
16 *
17 * See readme.txt for copyright information.
18 */
19
20
21 #include "allegro5/allegro.h"
22 #include "allegro5/internal/aintern.h"
23 #include "allegro5/internal/aintern_driver.h"
24 #include "allegro5/internal/aintern_events.h"
25 #include "allegro5/internal/aintern_keyboard.h"
26 #include "allegro5/platform/aintosx.h"
27
28 #import <Carbon/Carbon.h>
29
30 #ifndef ALLEGRO_MACOSX
31 #error Something is wrong with the makefile
32 #endif
33
34
35
36 /* Dictionary to translate OS X modifier codes to Allegro modifier codes
37 * and key codes.
38 */
39 static unsigned const int mod_info[5][3] = {
40 { NSAlphaShiftKeyMask, ALLEGRO_KEYMOD_CAPSLOCK, ALLEGRO_KEY_CAPSLOCK },
41 { NSShiftKeyMask, ALLEGRO_KEYMOD_SHIFT, ALLEGRO_KEY_LSHIFT },
42 { NSControlKeyMask, ALLEGRO_KEYMOD_CTRL, ALLEGRO_KEY_LCTRL },
43 { NSAlternateKeyMask, ALLEGRO_KEYMOD_ALT, ALLEGRO_KEY_ALT },
44 { NSCommandKeyMask, ALLEGRO_KEYMOD_COMMAND, ALLEGRO_KEY_COMMAND }
45 };
46
47
48
49 static bool osx_keyboard_init(void);
50 static void osx_keyboard_exit(void);
51 static ALLEGRO_KEYBOARD* osx_get_keyboard(void);
52 static ALLEGRO_KEYBOARD keyboard;
53 static ALLEGRO_KEYBOARD_STATE kbdstate;
54 static UInt32 dead_key_state;
55
56
57
58 /* translate_modifier_flags:
59 * Translate a bitmask of OS X modifier flags to Allegro's modifier flags
60 */
61 static int translate_modifier_flags(int osx_mods)
62 {
63 int allegro_mods = 0;
64 int i;
65
66 for (i = 0; i < 5; i++) {
67 if (osx_mods & mod_info[i][0])
68 allegro_mods |= mod_info[i][1];
69 }
70
71 return allegro_mods;
72 }
73
74
75
76 /* _al_osx_switch_keyboard_focus:
77 * Handle a focus switch event.
78 */
79 void _al_osx_switch_keyboard_focus(ALLEGRO_DISPLAY *dpy, bool switch_in)
80 {
81 _al_event_source_lock(&keyboard.es);
82
83 if (switch_in)
84 kbdstate.display = dpy;
85 else
86 kbdstate.display = NULL;
87
88 _al_event_source_unlock(&keyboard.es);
89 }
90
91
92
93 static void _handle_key_press(ALLEGRO_DISPLAY* dpy, int unicode, int scancode,
94 int modifiers, bool is_repeat)
95 {
96 _al_event_source_lock(&keyboard.es);
97 {
98 /* Generate the press event if necessary. */
99 if (_al_event_source_needs_to_generate_event(&keyboard.es)) {
100 ALLEGRO_EVENT event;
101 event.keyboard.type = ALLEGRO_EVENT_KEY_DOWN;
102 event.keyboard.timestamp = al_get_time();
103 event.keyboard.display = dpy;
104 event.keyboard.keycode = scancode;
105 event.keyboard.unichar = 0;
106 event.keyboard.modifiers = modifiers;
107 event.keyboard.repeat = false;
108 if (!is_repeat) {
109 _al_event_source_emit_event(&keyboard.es, &event);
110 }
111 if (unicode != 0) {
112 if (unicode < 0) {
113 unicode = 0;
114 }
115 event.keyboard.type = ALLEGRO_EVENT_KEY_CHAR;
116 event.keyboard.unichar = unicode;
117 event.keyboard.modifiers = modifiers;
118 event.keyboard.repeat = is_repeat;
119 _al_event_source_emit_event(&keyboard.es, &event);
120 }
121 }
122 }
123 /* Maintain the kbdstate array. */
124 _AL_KEYBOARD_STATE_SET_KEY_DOWN(kbdstate, scancode);
125 _al_event_source_unlock(&keyboard.es);
126 }
127
128
129
130 static void _handle_key_release(ALLEGRO_DISPLAY* dpy, int modifiers, int scancode)
131 {
132 _al_event_source_lock(&keyboard.es);
133 {
134 /* Generate the release event if necessary. */
135 if (_al_event_source_needs_to_generate_event(&keyboard.es)) {
136 ALLEGRO_EVENT event;
137 event.keyboard.type = ALLEGRO_EVENT_KEY_UP;
138 event.keyboard.timestamp = al_get_time();
139 event.keyboard.display = dpy;
140 event.keyboard.keycode = scancode;
141 event.keyboard.unichar = 0;
142 event.keyboard.modifiers = modifiers;
143 _al_event_source_emit_event(&keyboard.es, &event);
144 }
145 }
146 /* Maintain the kbdstate array. */
147 _AL_KEYBOARD_STATE_CLEAR_KEY_DOWN(kbdstate, scancode);
148 _al_event_source_unlock(&keyboard.es);
149 }
150
151
152
153 /* Mac keycode to Allegro scancode conversion table */
154 static const int mac_to_scancode[128] =
155 {
156 /* 0x00 */ ALLEGRO_KEY_A, ALLEGRO_KEY_S, ALLEGRO_KEY_D, ALLEGRO_KEY_F,
157 /* 0x04 */ ALLEGRO_KEY_H, ALLEGRO_KEY_G, ALLEGRO_KEY_Z, ALLEGRO_KEY_X,
158 /* 0x08 */ ALLEGRO_KEY_C, ALLEGRO_KEY_V, ALLEGRO_KEY_BACKSLASH2, ALLEGRO_KEY_B,
159 /* 0x0c */ ALLEGRO_KEY_Q, ALLEGRO_KEY_W, ALLEGRO_KEY_E, ALLEGRO_KEY_R,
160 /* 0x10 */ ALLEGRO_KEY_Y, ALLEGRO_KEY_T, ALLEGRO_KEY_1, ALLEGRO_KEY_2,
161 /* 0x14 */ ALLEGRO_KEY_3, ALLEGRO_KEY_4, ALLEGRO_KEY_6, ALLEGRO_KEY_5,
162 /* 0x18 */ ALLEGRO_KEY_EQUALS, ALLEGRO_KEY_9, ALLEGRO_KEY_7, ALLEGRO_KEY_MINUS,
163 /* 0x1c */ ALLEGRO_KEY_8, ALLEGRO_KEY_0, ALLEGRO_KEY_CLOSEBRACE, ALLEGRO_KEY_O,
164 /* 0x20 */ ALLEGRO_KEY_U, ALLEGRO_KEY_OPENBRACE, ALLEGRO_KEY_I, ALLEGRO_KEY_P,
165 /* 0x24 */ ALLEGRO_KEY_ENTER, ALLEGRO_KEY_L, ALLEGRO_KEY_J, ALLEGRO_KEY_QUOTE,
166 /* 0x28 */ ALLEGRO_KEY_K, ALLEGRO_KEY_SEMICOLON, ALLEGRO_KEY_BACKSLASH, ALLEGRO_KEY_COMMA,
167 /* 0x2c */ ALLEGRO_KEY_SLASH, ALLEGRO_KEY_N, ALLEGRO_KEY_M, ALLEGRO_KEY_FULLSTOP,
168 /* 0x30 */ ALLEGRO_KEY_TAB, ALLEGRO_KEY_SPACE, ALLEGRO_KEY_BACKQUOTE, ALLEGRO_KEY_BACKSPACE,
169 /* 0x34 */ ALLEGRO_KEY_ENTER, ALLEGRO_KEY_ESCAPE, 0, ALLEGRO_KEY_COMMAND,
170 /* 0x38 */ ALLEGRO_KEY_LSHIFT, ALLEGRO_KEY_CAPSLOCK, ALLEGRO_KEY_ALT, ALLEGRO_KEY_LEFT,
171 /* 0x3c */ ALLEGRO_KEY_RIGHT, ALLEGRO_KEY_DOWN, ALLEGRO_KEY_UP, 0,
172 /* 0x40 */ 0, ALLEGRO_KEY_PAD_DELETE, 0, ALLEGRO_KEY_PAD_ASTERISK,
173 /* 0x44 */ 0, ALLEGRO_KEY_PAD_PLUS, 0, ALLEGRO_KEY_NUMLOCK,
174 /* 0x48 */ 0, 0, 0, ALLEGRO_KEY_PAD_SLASH,
175 /* 0x4c */ ALLEGRO_KEY_PAD_ENTER,0, ALLEGRO_KEY_PAD_MINUS, 0,
176 /* 0x50 */ 0, ALLEGRO_KEY_PAD_EQUALS, ALLEGRO_KEY_PAD_0, ALLEGRO_KEY_PAD_1,
177 /* 0x54 */ ALLEGRO_KEY_PAD_2, ALLEGRO_KEY_PAD_3, ALLEGRO_KEY_PAD_4, ALLEGRO_KEY_PAD_5,
178 /* 0x58 */ ALLEGRO_KEY_PAD_6, ALLEGRO_KEY_PAD_7, 0, ALLEGRO_KEY_PAD_8,
179 /* 0x5c */ ALLEGRO_KEY_PAD_9, 0, 0, 0,
180 /* 0x60 */ ALLEGRO_KEY_F5, ALLEGRO_KEY_F6, ALLEGRO_KEY_F7, ALLEGRO_KEY_F3,
181 /* 0x64 */ ALLEGRO_KEY_F8, ALLEGRO_KEY_F9, 0, ALLEGRO_KEY_F11,
182 /* 0x68 */ 0, ALLEGRO_KEY_PRINTSCREEN,0, ALLEGRO_KEY_SCROLLLOCK,
183 /* 0x6c */ 0, ALLEGRO_KEY_F10, 0, ALLEGRO_KEY_F12,
184 /* 0x70 */ 0, ALLEGRO_KEY_PAUSE, ALLEGRO_KEY_INSERT, ALLEGRO_KEY_HOME,
185 /* 0x74 */ ALLEGRO_KEY_PGUP, ALLEGRO_KEY_DELETE, ALLEGRO_KEY_F4, ALLEGRO_KEY_END,
186 /* 0x78 */ ALLEGRO_KEY_F2, ALLEGRO_KEY_PGDN, ALLEGRO_KEY_F1, ALLEGRO_KEY_LEFT,
187 /* 0x7c */ ALLEGRO_KEY_RIGHT, ALLEGRO_KEY_DOWN, ALLEGRO_KEY_UP, 0
188 };
189
190
191
192 /* get_state:
193 * Copy a snapshot of the keyboard state into the user's structure
194 */
195 static void get_state(ALLEGRO_KEYBOARD_STATE *ret_state)
196 {
197 _al_event_source_lock(&keyboard.es);
198 {
199 memcpy(ret_state, &kbdstate, sizeof(ALLEGRO_KEYBOARD_STATE));
200 }
201 _al_event_source_unlock(&keyboard.es);
202 }
203
204
205
206 /* clear_state:
207 * Clear the keyboard state
208 */
209 static void clear_state(void)
210 {
211 _al_event_source_lock(&keyboard.es);
212 {
213 memset(&kbdstate, 0, sizeof(kbdstate));
214 }
215 _al_event_source_unlock(&keyboard.es);
216 }
217
218
219
220 static ALLEGRO_KEYBOARD_DRIVER keyboard_macosx =
221 {
222 KEYBOARD_MACOSX,
223 "",
224 "",
225 "MacOS X keyboard",
226 osx_keyboard_init,
227 osx_keyboard_exit,
228 osx_get_keyboard,
229 NULL, // ALLEGRO_METHOD(bool, set_leds, (int leds));
230 NULL, // ALLEGRO_METHOD(const char *, keycode_to_name, (int keycode));
231 get_state,
232 clear_state,
233 };
234
235
236
237 _AL_DRIVER_INFO _al_keyboard_driver_list[] =
238 {
239 { KEYBOARD_MACOSX, &keyboard_macosx, 1 },
240 { 0, NULL, 0 }
241 };
242
243
244
245 /* _al_osx_get_keyboard_driver:
246 * Returns the keyboard driver.
247 */
248 ALLEGRO_KEYBOARD_DRIVER* _al_osx_get_keyboard_driver(void)
249 {
250 return &keyboard_macosx;
251 }
252
253
254
255 /* _al_osx_keyboard_handler:
256 * Keyboard "interrupt" handler.
257 */
258 void _al_osx_keyboard_handler(int pressed, NSEvent *event, ALLEGRO_DISPLAY* dpy)
259 {
260 /* We need to distinguish between the raw character code (needed for
261 * ctrl and alt) and the "shifted" character code when neither of these
262 * is held down. This is needed to get the correct behavior when caps
263 * lock is on (ie, letters are upper case)
264 */
265 int scancode = mac_to_scancode[[event keyCode]];
266
267 /* Translate OS X modifier flags to Allegro modifier flags */
268 int key_shifts = translate_modifier_flags([event modifierFlags]);
269
270 if (pressed) {
271 int32_t unichar = 0;
272 bool new_input = _al_get_keyboard_compat_version() >= AL_ID(5, 2, 10, 0);
273 NSString *characters = [event characters];
274 UniChar character = ([characters length] > 0) ? [characters characterAtIndex: 0] : 0;
275
276 if (new_input) {
277 /* https://stackoverflow.com/a/22677690 */
278 TISInputSourceRef keyboard_input = TISCopyCurrentKeyboardInputSource();
279 CFDataRef layout_data = TISGetInputSourceProperty(keyboard_input, kTISPropertyUnicodeKeyLayoutData);
280 /* https://github.com/microsoft/vscode/issues/23833 */
281 if (!layout_data) {
282 /* TISGetInputSourceProperty returns null with a Japanese keyboard layout.
283 * Using TISCopyCurrentKeyboardLayoutInputSource to fix the NULL return.
284 */
285 keyboard_input = TISCopyCurrentKeyboardLayoutInputSource();
286 layout_data = TISGetInputSourceProperty(keyboard_input, kTISPropertyUnicodeKeyLayoutData);
287 }
288 const UCKeyboardLayout *layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);
289
290 CGEventFlags modifier_flags = [event modifierFlags];
291 UInt32 modifier_key_state = (modifier_flags >> 16) & 0xff;
292
293 UniChar unicode_string[5];
294 unicode_string[4] = 0;
295 UniCharCount unicode_length;
296
297 UCKeyTranslate(layout,
298 [event keyCode],
299 kUCKeyActionDown,
300 modifier_key_state,
301 LMGetKbdType(),
302 0,
303 &dead_key_state,
304 4,
305 &unicode_length,
306 unicode_string);
307 if (unicode_length > 0) {
308 ALLEGRO_USTR *ustr = al_ustr_new_from_utf16(unicode_string);
309 /* TODO: Possibly add an option to emit multiple events here.
310 * At the moment, we take the last key, because when an invalid
311 * dead key combination is entered, unicode string contains a character
312 * representaiton of the dead key + the last pressed key.
313 * We opt to return the last pressed key.
314 */
315 unichar = al_ustr_get(ustr, al_ustr_offset(ustr, (int)unicode_length - 1));
316 if (unichar < 0)
317 unichar = 0;
318 /* For some reason, pad enter sends a ^C. */
319 else if (scancode == ALLEGRO_KEY_PAD_ENTER && unichar == 3)
320 unichar = '\r';
321 /* This is here to override the Ctrl/Cmd fixes for backspace. */
322 else if (scancode == ALLEGRO_KEY_BACKSPACE)
323 unichar = '\b';
324 /* For some reason, Ctrl-<key> sends capital version of the character,
325 and not the correct invisible character. */
326 else if (key_shifts & ALLEGRO_KEYMOD_CTRL)
327 unichar = character;
328 /* For some reason, Cmd-<key> converts characters to Ctrl-<key>. */
329 else if (key_shifts & ALLEGRO_KEYMOD_COMMAND)
330 unichar = character;
331 al_ustr_free(ustr);
332 }
333 CFRelease(keyboard_input);
334 }
335 else
336 unichar = character;
337
338 /* Apple maps function, arrow, and other keys to Unicode points.
339 https://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CORPCHAR.TXT
340 We want to generate CHAR events for them, so we'll override the translation logic.
341 _handle_key_press will set the unichar back to 0 for these keys. */
342 if (character >= 0xF700 && character <= 0xF747) {
343 /* The old input did not handle this key (delete) correctly. We preserve the old behavior. */
344 if (new_input && character == 0xF728)
345 unichar = 127;
346 else
347 unichar = -1;
348 }
349 bool is_repeat = pressed ? ([event isARepeat] == YES) : false;
350 _handle_key_press(dpy, unichar, scancode, key_shifts, is_repeat);
351 }
352 else {
353 _handle_key_release(dpy, key_shifts, scancode);
354 }
355 }
356
357
358
359 /* _al_osx_keyboard_modifier:
360 * Handles keyboard modifiers changes.
361 */
362 void _al_osx_keyboard_modifiers(unsigned int modifiers, ALLEGRO_DISPLAY* dpy)
363 {
364 static unsigned int old_modifiers = 0;
365 int i, changed;
366 int key_shifts;
367
368 /* Translate OS X modifier flags to Allegro modifier flags */
369 key_shifts = translate_modifier_flags(modifiers);
370
371 for (i = 0; i < 5; i++) {
372 changed = (modifiers ^ old_modifiers) & mod_info[i][0];
373 if (changed) {
374 if (modifiers & mod_info[i][0]) {
375 _handle_key_press(dpy, 0, mod_info[i][2], key_shifts, false);
376 if (i == 0) {
377 /* Caps lock requires special handling */
378 _handle_key_release(dpy, key_shifts, mod_info[0][2]);
379 }
380 }
381 else {
382 if (i == 0) {
383 _handle_key_press(dpy, 0, mod_info[0][2], key_shifts, false);
384 }
385
386 _handle_key_release(dpy, key_shifts, mod_info[i][2]);
387 }
388 }
389 }
390 old_modifiers = modifiers;
391 }
392
393
394
395 /* osx_keyboard_init:
396 * Installs the keyboard handler.
397 */
398 static bool osx_keyboard_init(void)
399 {
400 memset(&keyboard, 0, sizeof keyboard);
401 _al_osx_keyboard_was_installed(YES);
402 _al_event_source_init(&keyboard.es);
403 return true;
404 }
405
406
407
408 /* osx_keyboard_exit:
409 * Removes the keyboard handler.
410 */
411 static void osx_keyboard_exit(void)
412 {
413 _al_event_source_free(&keyboard.es);
414 _al_osx_keyboard_was_installed(NO);
415 }
416
417
418
419 /* osx_get_keyboard:
420 * Returns the keyboard object.
421 */
422 static ALLEGRO_KEYBOARD* osx_get_keyboard(void)
423 {
424 return &keyboard;
425 }
426
427
428 /* vim: set sts=3 sw=3 et: */