Debian Codegraph @

gdk/quartz/gdkkeys-quartz.c

raw
1 /* gdkkeys-quartz.c
2 *
3 * Copyright (C) 2000 Red Hat, Inc.
4 * Copyright (C) 2005 Imendio AB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21 /* Some parts of this code come from quartzKeyboard.c,
22 * from the Apple X11 Server.
23 *
24 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
25 *
26 * Permission is hereby granted, free of charge, to any person
27 * obtaining a copy of this software and associated documentation files
28 * (the "Software"), to deal in the Software without restriction,
29 * including without limitation the rights to use, copy, modify, merge,
30 * publish, distribute, sublicense, and/or sell copies of the Software,
31 * and to permit persons to whom the Software is furnished to do so,
32 * subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be
35 * included in all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40 * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
41 * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
42 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
44 * DEALINGS IN THE SOFTWARE.
45 *
46 * Except as contained in this notice, the name(s) of the above
47 * copyright holders shall not be used in advertising or otherwise to
48 * promote the sale, use or other dealings in this Software without
49 * prior written authorization.
50 */
51
52 #include "config.h"
53
54 #include <Carbon/Carbon.h>
55 #include <AppKit/NSEvent.h>
56 #include "gdk.h"
57 #include "gdkkeysyms.h"
58
59 #define NUM_KEYCODES 128
60 #define KEYVALS_PER_KEYCODE 4
61
62 static GdkKeymap *default_keymap = NULL;
63
64 /* This is a table of all keyvals. Each keycode gets KEYVALS_PER_KEYCODE entries.
65 * TThere is 1 keyval per modifier (Nothing, Shift, Alt, Shift+Alt);
66 */
67 static guint *keyval_array = NULL;
68
69 static inline UniChar
70 macroman2ucs (unsigned char c)
71 {
72 /* Precalculated table mapping MacRoman-128 to Unicode. Generated
73 by creating single element CFStringRefs then extracting the
74 first character. */
75
76 static const unsigned short table[128] = {
77 0xc4, 0xc5, 0xc7, 0xc9, 0xd1, 0xd6, 0xdc, 0xe1,
78 0xe0, 0xe2, 0xe4, 0xe3, 0xe5, 0xe7, 0xe9, 0xe8,
79 0xea, 0xeb, 0xed, 0xec, 0xee, 0xef, 0xf1, 0xf3,
80 0xf2, 0xf4, 0xf6, 0xf5, 0xfa, 0xf9, 0xfb, 0xfc,
81 0x2020, 0xb0, 0xa2, 0xa3, 0xa7, 0x2022, 0xb6, 0xdf,
82 0xae, 0xa9, 0x2122, 0xb4, 0xa8, 0x2260, 0xc6, 0xd8,
83 0x221e, 0xb1, 0x2264, 0x2265, 0xa5, 0xb5, 0x2202, 0x2211,
84 0x220f, 0x3c0, 0x222b, 0xaa, 0xba, 0x3a9, 0xe6, 0xf8,
85 0xbf, 0xa1, 0xac, 0x221a, 0x192, 0x2248, 0x2206, 0xab,
86 0xbb, 0x2026, 0xa0, 0xc0, 0xc3, 0xd5, 0x152, 0x153,
87 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0xf7, 0x25ca,
88 0xff, 0x178, 0x2044, 0x20ac, 0x2039, 0x203a, 0xfb01, 0xfb02,
89 0x2021, 0xb7, 0x201a, 0x201e, 0x2030, 0xc2, 0xca, 0xc1,
90 0xcb, 0xc8, 0xcd, 0xce, 0xcf, 0xcc, 0xd3, 0xd4,
91 0xf8ff, 0xd2, 0xda, 0xdb, 0xd9, 0x131, 0x2c6, 0x2dc,
92 0xaf, 0x2d8, 0x2d9, 0x2da, 0xb8, 0x2dd, 0x2db, 0x2c7
93 };
94
95 if (c < 128)
96 return c;
97 else
98 return table[c - 128];
99 }
100
101 const static struct {
102 guint keycode;
103 guint keyval;
104 unsigned int modmask; /* So we can tell when a mod key is pressed/released */
105 } modifier_keys[] = {
106 { 54, GDK_Meta_R, NSCommandKeyMask },
107 { 55, GDK_Meta_L, NSCommandKeyMask },
108 { 56, GDK_Shift_L, NSShiftKeyMask },
109 { 57, GDK_Caps_Lock, NSAlphaShiftKeyMask },
110 { 58, GDK_Alt_L, NSAlternateKeyMask },
111 { 59, GDK_Control_L, NSControlKeyMask },
112 { 60, GDK_Shift_R, NSShiftKeyMask },
113 { 61, GDK_Alt_R, NSAlternateKeyMask },
114 { 62, GDK_Control_R, NSControlKeyMask }
115 };
116
117 const static struct {
118 guint keycode;
119 guint keyval;
120 } function_keys[] = {
121 { 122, GDK_F1 },
122 { 120, GDK_F2 },
123 { 99, GDK_F3 },
124 { 118, GDK_F4 },
125 { 96, GDK_F5 },
126 { 97, GDK_F6 },
127 { 98, GDK_F7 },
128 { 100, GDK_F8 },
129 { 101, GDK_F9 },
130 { 109, GDK_F10 },
131 { 103, GDK_F11 },
132 { 111, GDK_F12 },
133 { 105, GDK_F13 },
134 { 107, GDK_F14 },
135 { 113, GDK_F15 },
136 { 106, GDK_F16 }
137 };
138
139 const static struct {
140 guint keycode;
141 guint normal_keyval, keypad_keyval;
142 } known_numeric_keys[] = {
143 { 65, GDK_period, GDK_KP_Decimal },
144 { 67, GDK_asterisk, GDK_KP_Multiply },
145 { 69, GDK_plus, GDK_KP_Add },
146 { 75, GDK_slash, GDK_KP_Divide },
147 { 76, GDK_Return, GDK_KP_Enter },
148 { 78, GDK_minus, GDK_KP_Subtract },
149 { 81, GDK_equal, GDK_KP_Equal },
150 { 82, GDK_0, GDK_KP_0 },
151 { 83, GDK_1, GDK_KP_1 },
152 { 84, GDK_2, GDK_KP_2 },
153 { 85, GDK_3, GDK_KP_3 },
154 { 86, GDK_4, GDK_KP_4 },
155 { 87, GDK_5, GDK_KP_5 },
156 { 88, GDK_6, GDK_KP_6 },
157 { 89, GDK_7, GDK_KP_7 },
158 { 91, GDK_8, GDK_KP_8 },
159 { 92, GDK_9, GDK_KP_9 }
160 };
161
162 /* These values aren't covered by gdk_unicode_to_keyval */
163 const static struct {
164 gunichar ucs_value;
165 guint keyval;
166 } special_ucs_table [] = {
167 { 0x0001, GDK_Home },
168 { 0x0003, GDK_Return },
169 { 0x0004, GDK_End },
170 { 0x0008, GDK_BackSpace },
171 { 0x0009, GDK_Tab },
172 { 0x000b, GDK_Page_Up },
173 { 0x000c, GDK_Page_Down },
174 { 0x000d, GDK_Return },
175 { 0x001b, GDK_Escape },
176 { 0x001c, GDK_Left },
177 { 0x001d, GDK_Right },
178 { 0x001e, GDK_Up },
179 { 0x001f, GDK_Down },
180 { 0x007f, GDK_Delete },
181 { 0xf027, GDK_dead_acute },
182 { 0xf060, GDK_dead_grave },
183 { 0xf300, GDK_dead_grave },
184 { 0xf0b4, GDK_dead_acute },
185 { 0xf301, GDK_dead_acute },
186 { 0xf385, GDK_dead_acute },
187 { 0xf05e, GDK_dead_circumflex },
188 { 0xf2c6, GDK_dead_circumflex },
189 { 0xf302, GDK_dead_circumflex },
190 { 0xf07e, GDK_dead_tilde },
191 { 0xf2dc, GDK_dead_tilde },
192 { 0xf303, GDK_dead_tilde },
193 { 0xf342, GDK_dead_perispomeni },
194 { 0xf0af, GDK_dead_macron },
195 { 0xf304, GDK_dead_macron },
196 { 0xf2d8, GDK_dead_breve },
197 { 0xf306, GDK_dead_breve },
198 { 0xf2d9, GDK_dead_abovedot },
199 { 0xf307, GDK_dead_abovedot },
200 { 0xf0a8, GDK_dead_diaeresis },
201 { 0xf308, GDK_dead_diaeresis },
202 { 0xf2da, GDK_dead_abovering },
203 { 0xf30A, GDK_dead_abovering },
204 { 0xf022, GDK_dead_doubleacute },
205 { 0xf2dd, GDK_dead_doubleacute },
206 { 0xf30B, GDK_dead_doubleacute },
207 { 0xf2c7, GDK_dead_caron },
208 { 0xf30C, GDK_dead_caron },
209 { 0xf0be, GDK_dead_cedilla },
210 { 0xf327, GDK_dead_cedilla },
211 { 0xf2db, GDK_dead_ogonek },
212 { 0xf328, GDK_dead_ogonek },
213 { 0xfe5d, GDK_dead_iota },
214 { 0xf323, GDK_dead_belowdot },
215 { 0xf309, GDK_dead_hook },
216 { 0xf31B, GDK_dead_horn },
217 { 0xf02d, GDK_dead_stroke },
218 { 0xf335, GDK_dead_stroke },
219 { 0xf336, GDK_dead_stroke },
220 { 0xf313, GDK_dead_abovecomma },
221 /* { 0xf313, GDK_dead_psili }, */
222 { 0xf314, GDK_dead_abovereversedcomma },
223 /* { 0xf314, GDK_dead_dasia }, */
224 { 0xf30F, GDK_dead_doublegrave },
225 { 0xf325, GDK_dead_belowring },
226 { 0xf2cd, GDK_dead_belowmacron },
227 { 0xf331, GDK_dead_belowmacron },
228 { 0xf32D, GDK_dead_belowcircumflex },
229 { 0xf330, GDK_dead_belowtilde },
230 { 0xf32E, GDK_dead_belowbreve },
231 { 0xf324, GDK_dead_belowdiaeresis },
232 { 0xf311, GDK_dead_invertedbreve },
233 { 0xf02c, GDK_dead_belowcomma },
234 { 0xf326, GDK_dead_belowcomma }
235 };
236
237 static void
238 update_keymap (void)
239 {
240 const void *chr_data = NULL;
241 guint *p;
242 int i;
243
244 /* Note: we could check only if building against the 10.5 SDK instead, but
245 * that would make non-xml layouts not work in 32-bit which would be a quite
246 * bad regression. This way, old unsupported layouts will just not work in
247 * 64-bit.
248 */
249 #ifdef __LP64__
250 TISInputSourceRef new_layout = TISCopyCurrentKeyboardLayoutInputSource ();
251 CFDataRef layout_data_ref;
252
253 #else
254 KeyboardLayoutRef new_layout;
255 KeyboardLayoutKind layout_kind;
256
257 KLGetCurrentKeyboardLayout (&new_layout);
258 #endif
259
260 g_free (keyval_array);
261 keyval_array = g_new0 (guint, NUM_KEYCODES * KEYVALS_PER_KEYCODE);
262
263 #ifdef __LP64__
264 layout_data_ref = (CFDataRef) TISGetInputSourceProperty
265 (new_layout, kTISPropertyUnicodeKeyLayoutData);
266
267 if (layout_data_ref)
268 chr_data = CFDataGetBytePtr (layout_data_ref);
269
270 if (chr_data == NULL)
271 {
272 g_error ("cannot get keyboard layout data");
273 return;
274 }
275 #else
276 /* Get the layout kind */
277 KLGetKeyboardLayoutProperty (new_layout, kKLKind, (const void **)&layout_kind);
278
279 /* 8-bit-only keyabord layout */
280 if (layout_kind == kKLKCHRKind)
281 {
282 /* Get chr data */
283 KLGetKeyboardLayoutProperty (new_layout, kKLKCHRData, (const void **)&chr_data);
284
285 for (i = 0; i < NUM_KEYCODES; i++)
286 {
287 int j;
288 UInt32 modifiers[] = {0, shiftKey, optionKey, shiftKey | optionKey};
289
290 p = keyval_array + i * KEYVALS_PER_KEYCODE;
291
292 for (j = 0; j < KEYVALS_PER_KEYCODE; j++)
293 {
294 UInt32 c, state = 0;
295 UInt16 key_code;
296 UniChar uc;
297
298 key_code = modifiers[j] | i;
299 c = KeyTranslate (chr_data, key_code, &state);
300
301 if (state != 0)
302 {
303 UInt32 state2 = 0;
304 c = KeyTranslate (chr_data, key_code | 128, &state2);
305 }
306
307 if (c != 0 && c != 0x10)
308 {
309 int k;
310 gboolean found = FALSE;
311
312 /* FIXME: some keyboard layouts (e.g. Russian) use a
313 * different 8-bit character set. We should check
314 * for this. Not a serious problem, because most
315 * (all?) of these layouts also have a uchr version.
316 */
317 uc = macroman2ucs (c);
318
319 for (k = 0; k < G_N_ELEMENTS (special_ucs_table); k++)
320 {
321 if (special_ucs_table[k].ucs_value == uc)
322 {
323 p[j] = special_ucs_table[k].keyval;
324 found = TRUE;
325 break;
326 }
327 }
328
329 /* Special-case shift-tab since GTK+ expects
330 * GDK_ISO_Left_Tab for that.
331 */
332 if (found && p[j] == GDK_Tab && modifiers[j] == shiftKey)
333 p[j] = GDK_ISO_Left_Tab;
334
335 if (!found)
336 p[j] = gdk_unicode_to_keyval (uc);
337 }
338 }
339
340 if (p[3] == p[2])
341 p[3] = 0;
342 if (p[2] == p[1])
343 p[2] = 0;
344 if (p[1] == p[0])
345 p[1] = 0;
346 if (p[0] == p[2] &&
347 p[1] == p[3])
348 p[2] = p[3] = 0;
349 }
350 }
351 /* unicode keyboard layout */
352 else if (layout_kind == kKLKCHRuchrKind || layout_kind == kKLuchrKind)
353 {
354 /* Get chr data */
355 KLGetKeyboardLayoutProperty (new_layout, kKLuchrData, (const void **)&chr_data);
356 #endif
357
358 for (i = 0; i < NUM_KEYCODES; i++)
359 {
360 int j;
361 UInt32 modifiers[] = {0, shiftKey, optionKey, shiftKey | optionKey};
362 UniChar chars[4];
363 UniCharCount nChars;
364
365 p = keyval_array + i * KEYVALS_PER_KEYCODE;
366
367 for (j = 0; j < KEYVALS_PER_KEYCODE; j++)
368 {
369 UInt32 state = 0;
370 OSStatus err;
371 UInt16 key_code;
372 UniChar uc;
373
374 key_code = modifiers[j] | i;
375 err = UCKeyTranslate (chr_data, i, kUCKeyActionDisplay,
376 (modifiers[j] >> 8) & 0xFF,
377 LMGetKbdType(),
378 0,
379 &state, 4, &nChars, chars);
380
381 /* FIXME: Theoretically, we can get multiple UTF-16
382 * values; we should convert them to proper unicode and
383 * figure out whether there are really keyboard layouts
384 * that give us more than one character for one
385 * keypress.
386 */
387 if (err == noErr && nChars == 1)
388 {
389 int k;
390 gboolean found = FALSE;
391
392 /* A few <Shift><Option>keys return two characters,
393 * the first of which is U+00a0, which isn't
394 * interesting; so we return the second. More
395 * sophisticated handling is the job of a
396 * GtkIMContext.
397 *
398 * If state isn't zero, it means that it's a dead
399 * key of some sort. Some of those are enumerated in
400 * the special_ucs_table with the high nibble set to
401 * f to push it into the private use range. Here we
402 * do the same.
403 */
404 if (state != 0)
405 chars[nChars - 1] |= 0xf000;
406 uc = chars[nChars - 1];
407
408 for (k = 0; k < G_N_ELEMENTS (special_ucs_table); k++)
409 {
410 if (special_ucs_table[k].ucs_value == uc)
411 {
412 p[j] = special_ucs_table[k].keyval;
413 found = TRUE;
414 break;
415 }
416 }
417
418 /* Special-case shift-tab since GTK+ expects
419 * GDK_ISO_Left_Tab for that.
420 */
421 if (found && p[j] == GDK_Tab && modifiers[j] == shiftKey)
422 p[j] = GDK_ISO_Left_Tab;
423
424 if (!found)
425 p[j] = gdk_unicode_to_keyval (uc);
426 }
427 }
428
429 if (p[3] == p[2])
430 p[3] = 0;
431 if (p[2] == p[1])
432 p[2] = 0;
433 if (p[1] == p[0])
434 p[1] = 0;
435 if (p[0] == p[2] &&
436 p[1] == p[3])
437 p[2] = p[3] = 0;
438 }
439 #ifndef __LP64__
440 }
441 else
442 {
443 g_error ("unknown type of keyboard layout (neither KCHR nor uchr)"
444 " - not supported right now");
445 }
446 #endif
447
448 for (i = 0; i < G_N_ELEMENTS (modifier_keys); i++)
449 {
450 p = keyval_array + modifier_keys[i].keycode * KEYVALS_PER_KEYCODE;
451
452 if (p[0] == 0 && p[1] == 0 &&
453 p[2] == 0 && p[3] == 0)
454 p[0] = modifier_keys[i].keyval;
455 }
456
457 for (i = 0; i < G_N_ELEMENTS (function_keys); i++)
458 {
459 p = keyval_array + function_keys[i].keycode * KEYVALS_PER_KEYCODE;
460
461 p[0] = function_keys[i].keyval;
462 p[1] = p[2] = p[3] = 0;
463 }
464
465 for (i = 0; i < G_N_ELEMENTS (known_numeric_keys); i++)
466 {
467 p = keyval_array + known_numeric_keys[i].keycode * KEYVALS_PER_KEYCODE;
468
469 if (p[0] == known_numeric_keys[i].normal_keyval)
470 p[0] = known_numeric_keys[i].keypad_keyval;
471 }
472
473 g_signal_emit_by_name (default_keymap, "keys-changed");
474 }
475
476 static void
477 input_sources_changed_notification (CFNotificationCenterRef center,
478 void *observer,
479 CFStringRef name,
480 const void *object,
481 CFDictionaryRef userInfo)
482 {
483 update_keymap ();
484 }
485
486 GdkKeymap *
487 gdk_keymap_get_for_display (GdkDisplay *display)
488 {
489 g_return_val_if_fail (display == gdk_display_get_default (), NULL);
490
491 if (default_keymap == NULL)
492 {
493 default_keymap = g_object_new (gdk_keymap_get_type (), NULL);
494 update_keymap ();
495 CFNotificationCenterAddObserver (CFNotificationCenterGetDistributedCenter (),
496 NULL,
497 input_sources_changed_notification,
498 CFSTR ("AppleSelectedInputSourcesChangedNotification"),
499 NULL,
500 CFNotificationSuspensionBehaviorDeliverImmediately);
501 }
502
503 return default_keymap;
504 }
505
506 PangoDirection
507 gdk_keymap_get_direction (GdkKeymap *keymap)
508 {
509 return PANGO_DIRECTION_NEUTRAL;
510 }
511
512 gboolean
513 gdk_keymap_have_bidi_layouts (GdkKeymap *keymap)
514 {
515 /* FIXME: Can we implement this? */
516 return FALSE;
517 }
518
519 gboolean
520 gdk_keymap_get_caps_lock_state (GdkKeymap *keymap)
521 {
522 /* FIXME: Implement this. */
523 return FALSE;
524 }
525
526 gboolean
527 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
528 guint keyval,
529 GdkKeymapKey **keys,
530 gint *n_keys)
531 {
532 GArray *keys_array;
533 int i;
534
535 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
536 g_return_val_if_fail (keys != NULL, FALSE);
537 g_return_val_if_fail (n_keys != NULL, FALSE);
538 g_return_val_if_fail (keyval != 0, FALSE);
539
540 if (!keymap)
541 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
542
543 *n_keys = 0;
544 keys_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
545
546 for (i = 0; i < NUM_KEYCODES * KEYVALS_PER_KEYCODE; i++)
547 {
548 GdkKeymapKey key;
549
550 if (keyval_array[i] != keyval)
551 continue;
552
553 (*n_keys)++;
554
555 key.keycode = i / KEYVALS_PER_KEYCODE;
556 key.group = (i % KEYVALS_PER_KEYCODE) >= 2;
557 key.level = i % 2;
558
559 g_array_append_val (keys_array, key);
560 }
561
562 *keys = (GdkKeymapKey *)g_array_free (keys_array, FALSE);
563
564 return *n_keys > 0;;
565 }
566
567 gboolean
568 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
569 guint hardware_keycode,
570 GdkKeymapKey **keys,
571 guint **keyvals,
572 gint *n_entries)
573 {
574 GArray *keys_array, *keyvals_array;
575 int i;
576 guint *p;
577
578 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
579 g_return_val_if_fail (n_entries != NULL, FALSE);
580
581 if (!keymap)
582 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
583
584 *n_entries = 0;
585
586 if (hardware_keycode > NUM_KEYCODES)
587 return FALSE;
588
589 if (keys)
590 keys_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
591 else
592 keys_array = NULL;
593
594 if (keyvals)
595 keyvals_array = g_array_new (FALSE, FALSE, sizeof (guint));
596 else
597 keyvals_array = NULL;
598
599 p = keyval_array + hardware_keycode * KEYVALS_PER_KEYCODE;
600
601 for (i = 0; i < KEYVALS_PER_KEYCODE; i++)
602 {
603 if (!p[i])
604 continue;
605
606 (*n_entries)++;
607
608 if (keyvals_array)
609 g_array_append_val (keyvals_array, p[i]);
610
611 if (keys_array)
612 {
613 GdkKeymapKey key;
614
615 key.keycode = hardware_keycode;
616 key.group = i >= 2;
617 key.level = i % 2;
618
619 g_array_append_val (keys_array, key);
620 }
621 }
622
623 if (keys)
624 *keys = (GdkKeymapKey *)g_array_free (keys_array, FALSE);
625
626 if (keyvals)
627 *keyvals = (guint *)g_array_free (keyvals_array, FALSE);
628
629 return *n_entries > 0;
630 }
631
632 guint
633 gdk_keymap_lookup_key (GdkKeymap *keymap,
634 const GdkKeymapKey *key)
635 {
636 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
637 g_return_val_if_fail (key != NULL, 0);
638 g_return_val_if_fail (key->group < 4, 0);
639
640 if (!keymap)
641 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
642
643 /* FIXME: Implement */
644
645 return 0;
646 }
647
648 #define GET_KEYVAL(keycode, group, level) (keyval_array[(keycode * KEYVALS_PER_KEYCODE + group * 2 + level)])
649
650 static guint
651 translate_keysym (guint hardware_keycode,
652 gint group,
653 GdkModifierType state,
654 gint *effective_group,
655 gint *effective_level)
656 {
657 gint level;
658 guint tmp_keyval;
659
660 level = (state & GDK_SHIFT_MASK) ? 1 : 0;
661
662 if (!(GET_KEYVAL (hardware_keycode, group, 0) || GET_KEYVAL (hardware_keycode, group, 1)) &&
663 (GET_KEYVAL (hardware_keycode, 0, 0) || GET_KEYVAL (hardware_keycode, 0, 1)))
664 group = 0;
665
666 if (!GET_KEYVAL (hardware_keycode, group, level) &&
667 GET_KEYVAL (hardware_keycode, group, 0))
668 level = 0;
669
670 tmp_keyval = GET_KEYVAL (hardware_keycode, group, level);
671
672 if (state & GDK_LOCK_MASK)
673 {
674 guint upper = gdk_keyval_to_upper (tmp_keyval);
675 if (upper != tmp_keyval)
676 tmp_keyval = upper;
677 }
678
679 if (effective_group)
680 *effective_group = group;
681 if (effective_level)
682 *effective_level = level;
683
684 return tmp_keyval;
685 }
686
687 gboolean
688 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
689 guint hardware_keycode,
690 GdkModifierType state,
691 gint group,
692 guint *keyval,
693 gint *effective_group,
694 gint *level,
695 GdkModifierType *consumed_modifiers)
696 {
697 guint tmp_keyval;
698 GdkModifierType bit;
699 guint tmp_modifiers = 0;
700
701 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
702 g_return_val_if_fail (group >= 0 && group <= 1, FALSE);
703
704 if (!keymap)
705 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
706
707 if (keyval)
708 *keyval = 0;
709 if (effective_group)
710 *effective_group = 0;
711 if (level)
712 *level = 0;
713 if (consumed_modifiers)
714 *consumed_modifiers = 0;
715
716 if (hardware_keycode < 0 || hardware_keycode >= NUM_KEYCODES)
717 return FALSE;
718
719 /* Check if modifiers modify the keyval */
720 for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
721 {
722 if (translate_keysym (hardware_keycode,
723 (bit == GDK_MOD1_MASK) ? 0 : group,
724 state & ~bit,
725 NULL, NULL) !=
726 translate_keysym (hardware_keycode,
727 (bit == GDK_MOD1_MASK) ? 1 : group,
728 state | bit,
729 NULL, NULL))
730 tmp_modifiers |= bit;
731 }
732
733 tmp_keyval = translate_keysym (hardware_keycode, group, state, level, effective_group);
734
735 if (consumed_modifiers)
736 *consumed_modifiers = tmp_modifiers;
737
738 if (keyval)
739 *keyval = tmp_keyval;
740
741 return TRUE;
742 }
743
744 void
745 gdk_keymap_add_virtual_modifiers (GdkKeymap *keymap,
746 GdkModifierType *state)
747 {
748 if (*state & GDK_MOD2_MASK)
749 *state |= GDK_META_MASK;
750 }
751
752 gboolean
753 gdk_keymap_map_virtual_modifiers (GdkKeymap *keymap,
754 GdkModifierType *state)
755 {
756 if (*state & GDK_META_MASK)
757 *state |= GDK_MOD2_MASK;
758
759 return TRUE;
760 }
761
762 /* What sort of key event is this? Returns one of
763 * GDK_KEY_PRESS, GDK_KEY_RELEASE, GDK_NOTHING (should be ignored)
764 */
765 GdkEventType
766 _gdk_quartz_keys_event_type (NSEvent *event)
767 {
768 unsigned short keycode;
769 unsigned int flags;
770 int i;
771
772 switch ([event type])
773 {
774 case NSKeyDown:
775 return GDK_KEY_PRESS;
776 case NSKeyUp:
777 return GDK_KEY_RELEASE;
778 case NSFlagsChanged:
779 break;
780 default:
781 g_assert_not_reached ();
782 }
783
784 /* For flags-changed events, we have to find the special key that caused the
785 * event, and see if it's in the modifier mask. */
786 keycode = [event keyCode];
787 flags = [event modifierFlags];
788
789 for (i = 0; i < G_N_ELEMENTS (modifier_keys); i++)
790 {
791 if (modifier_keys[i].keycode == keycode)
792 {
793 if (flags & modifier_keys[i].modmask)
794 return GDK_KEY_PRESS;
795 else
796 return GDK_KEY_RELEASE;
797 }
798 }
799
800 /* Some keypresses (eg: Expose' activations) seem to trigger flags-changed
801 * events for no good reason. Ignore them! */
802 return GDK_NOTHING;
803 }
804
805 gboolean
806 _gdk_quartz_keys_is_modifier (guint keycode)
807 {
808 gint i;
809
810 for (i = 0; i < G_N_ELEMENTS (modifier_keys); i++)
811 {
812 if (modifier_keys[i].modmask == 0)
813 break;
814
815 if (modifier_keys[i].keycode == keycode)
816 return TRUE;
817 }
818
819 return FALSE;
820 }