]> Pileus Git - ~andy/gtk/blob - gdk/quartz/gdkkeys-quartz.c
Add more stubs.
[~andy/gtk] / gdk / quartz / gdkkeys-quartz.c
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 "gdk.h"
56 #include "gdkkeysyms.h"
57
58 #define NUM_KEYCODES 128
59 #define KEYVALS_PER_KEYCODE 4
60
61 static GdkKeymap *default_keymap = NULL;
62
63 static KeyboardLayoutRef current_layout = NULL;
64
65 /* This is a table of all keyvals. Each keycode gets KEYVALS_PER_KEYCODE entries.
66  * TThere is 1 keyval per modifier (Nothing, Shift, Alt, Shift+Alt);
67  */
68 static guint *keyval_array = NULL;
69
70 static inline UniChar
71 macroman2ucs (unsigned char c)
72 {
73   /* Precalculated table mapping MacRoman-128 to Unicode. Generated
74      by creating single element CFStringRefs then extracting the
75      first character. */
76   
77   static const unsigned short table[128] = {
78     0xc4, 0xc5, 0xc7, 0xc9, 0xd1, 0xd6, 0xdc, 0xe1,
79     0xe0, 0xe2, 0xe4, 0xe3, 0xe5, 0xe7, 0xe9, 0xe8,
80     0xea, 0xeb, 0xed, 0xec, 0xee, 0xef, 0xf1, 0xf3,
81     0xf2, 0xf4, 0xf6, 0xf5, 0xfa, 0xf9, 0xfb, 0xfc,
82     0x2020, 0xb0, 0xa2, 0xa3, 0xa7, 0x2022, 0xb6, 0xdf,
83     0xae, 0xa9, 0x2122, 0xb4, 0xa8, 0x2260, 0xc6, 0xd8,
84     0x221e, 0xb1, 0x2264, 0x2265, 0xa5, 0xb5, 0x2202, 0x2211,
85     0x220f, 0x3c0, 0x222b, 0xaa, 0xba, 0x3a9, 0xe6, 0xf8,
86     0xbf, 0xa1, 0xac, 0x221a, 0x192, 0x2248, 0x2206, 0xab,
87     0xbb, 0x2026, 0xa0, 0xc0, 0xc3, 0xd5, 0x152, 0x153,
88     0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0xf7, 0x25ca,
89     0xff, 0x178, 0x2044, 0x20ac, 0x2039, 0x203a, 0xfb01, 0xfb02,
90     0x2021, 0xb7, 0x201a, 0x201e, 0x2030, 0xc2, 0xca, 0xc1,
91     0xcb, 0xc8, 0xcd, 0xce, 0xcf, 0xcc, 0xd3, 0xd4,
92     0xf8ff, 0xd2, 0xda, 0xdb, 0xd9, 0x131, 0x2c6, 0x2dc,
93     0xaf, 0x2d8, 0x2d9, 0x2da, 0xb8, 0x2dd, 0x2db, 0x2c7,
94   };
95
96   if (c < 128)
97     return c;
98   else
99     return table[c - 128];
100 }
101
102 const static struct {
103   guint keycode;
104   guint keyval;
105 } known_keys[] = {
106   {  55, GDK_Meta_L },
107   {  56, GDK_Shift_L },
108   {  57, GDK_Caps_Lock },
109   {  58, GDK_Alt_L },
110   {  59, GDK_Control_L },
111   {  60, GDK_Shift_R },
112   {  61, GDK_Alt_R },
113   {  62, GDK_Control_R },
114   { 122, GDK_F1 },
115   { 120, GDK_F2 },
116   {  99, GDK_F3 },
117   { 118, GDK_F4 },
118   {  96, GDK_F5 },
119   {  97, GDK_F6 },
120   {  98, GDK_F7 },
121   { 100, GDK_F8 },
122   { 101, GDK_F9 },
123   { 109, GDK_F10 },
124   { 103, GDK_F11 },
125   { 111, GDK_F12 },
126   { 105, GDK_F13 },
127   { 107, GDK_F14 },
128   { 113, GDK_F15 },
129 };
130
131 const static struct {
132   guint keycode;
133   guint normal_keyval, keypad_keyval;
134 } known_numeric_keys[] = {
135   { 65, GDK_period, GDK_KP_Decimal },
136   { 67, GDK_asterisk, GDK_KP_Multiply },
137   { 69, GDK_plus, GDK_KP_Add },
138   { 75, GDK_slash, GDK_KP_Divide },
139   { 76, 0x01000003, GDK_KP_Enter },
140   { 78, GDK_minus, GDK_KP_Subtract },
141   { 81, GDK_equal, GDK_KP_Equal },
142   { 82, GDK_0, GDK_KP_0 },
143   { 83, GDK_1, GDK_KP_1 },
144   { 84, GDK_2, GDK_KP_2 },
145   { 85, GDK_3, GDK_KP_3 },
146   { 86, GDK_4, GDK_KP_4 },
147   { 87, GDK_5, GDK_KP_5 },
148   { 88, GDK_6, GDK_KP_6 },
149   { 89, GDK_7, GDK_KP_7 },
150   { 91, GDK_8, GDK_KP_8 },
151   { 92, GDK_9, GDK_KP_9 },
152 };
153
154 /* These values aren't covered by gdk_unicode_to_keyval */
155 const static struct {
156   gunichar ucs_value;
157   guint keyval;
158 } special_ucs_table [] = {
159   { 0x0001, GDK_Home },
160   { 0x0008, GDK_BackSpace },
161   { 0x0009, GDK_Tab },
162   { 0x000d, GDK_Return },
163   { 0x001c, GDK_Left },
164   { 0x001d, GDK_Right },
165   { 0x001e, GDK_Up },
166   { 0x001f, GDK_Down },
167 };
168
169 static void
170 maybe_update_keymap (void)
171 {
172   KeyboardLayoutRef new_layout;
173
174   KLGetCurrentKeyboardLayout (&new_layout);
175
176   if (new_layout != current_layout)
177     {
178       guint *p;
179       int i;
180
181       KeyboardLayoutKind layout_kind;
182       
183       g_free (keyval_array);
184       keyval_array = g_new0 (guint, NUM_KEYCODES * KEYVALS_PER_KEYCODE);
185
186       /* Get the layout kind */
187       KLGetKeyboardLayoutProperty (new_layout, kKLKind, (const void **)&layout_kind);
188
189       /* 8-bit-only keyabord layout */
190       if (layout_kind == kKLKCHRKind)
191         { 
192           const void *chr_data;
193           
194           /* Get chr data */
195           KLGetKeyboardLayoutProperty (new_layout, kKLKCHRData, (const void **)&chr_data);
196           
197           for (i = 0; i < NUM_KEYCODES; i++) 
198             {
199               int j;
200               UInt32 modifiers[] = {0, shiftKey, optionKey, shiftKey|optionKey};
201
202               p = keyval_array + i * KEYVALS_PER_KEYCODE;
203               
204               for (j = 0; j < KEYVALS_PER_KEYCODE; j++)
205                 {
206                   UInt32 c, state = 0;
207                   UInt16 key_code;
208                   UniChar uc;
209                   
210                   key_code = modifiers[j]|i;
211                   c = KeyTranslate (chr_data, key_code, &state);
212
213                   if (state != 0)
214                     {
215                       UInt32 state2 = 0;
216                       c = KeyTranslate (chr_data, key_code | 128, &state2);
217                     }
218
219                   if (c != 0 && c != 0x10)
220                     {
221                       int k;
222                       gboolean found = FALSE;
223
224                       /* FIXME: some keyboard layouts (e.g. Russian) use
225                        * a different 8-bit character set. We should
226                        * check for this. Not a serious problem, because
227                        * most (all?) of these layouts also have a
228                        * uchr version. 
229                        */
230                       uc = macroman2ucs (c);
231                       
232                       for (k = 0; k < G_N_ELEMENTS (special_ucs_table); k++) 
233                         {
234                           if (special_ucs_table[k].ucs_value == uc)
235                             {
236                               p[j] = special_ucs_table[k].keyval;
237                               found = TRUE;
238                               break;
239                             }
240                         }
241                       
242                       if (!found)
243                         p[j] = gdk_unicode_to_keyval (uc);
244                     }
245                 }
246               
247               if (p[3] == p[2])
248                 p[3] = 0;
249               if (p[2] == p[1])
250                 p[2] = 0;
251               if (p[1] == p[0])
252                 p[1] = 0;
253               if (p[0] == p[2] && 
254                   p[1] == p[3])
255                 p[2] = p[3] = 0;
256             }
257         }
258       /* unicode keyboard layout */
259       else if (layout_kind == kKLKCHRuchrKind || layout_kind == kKLuchrKind)
260         { 
261           const void *chr_data;
262           
263           /* Get chr data */
264           KLGetKeyboardLayoutProperty (new_layout, kKLuchrData, (const void **)&chr_data);
265           
266           for (i = 0; i < NUM_KEYCODES; i++) 
267             {
268               int j;
269               UInt32 modifiers[] = {0, shiftKey, optionKey, shiftKey|optionKey};
270               UniChar chars[4];
271               UniCharCount nChars;
272
273               p = keyval_array + i * KEYVALS_PER_KEYCODE;
274
275               for (j = 0; j < KEYVALS_PER_KEYCODE; j++)
276                 {
277                   UInt32 state = 0;
278                   OSStatus err;
279                   UInt16 key_code;
280                   UniChar uc;
281                   
282                   key_code = modifiers[j]|i;
283                   err = UCKeyTranslate (chr_data, i, kUCKeyActionDown,
284                                         (modifiers[j] >> 8) & 0xFF,
285                                         LMGetKbdType(),
286                                         kUCKeyTranslateNoDeadKeysMask,
287                                         &state, 4, &nChars, chars);
288
289
290                   /* FIXME: Theoretically, we can get multiple UTF-16 values;
291                    * we should convert them to proper unicode and figure
292                    * out whether there are really keyboard layouts that
293                    * give us more than one character for one keypress. */
294                   if (err == noErr && nChars == 1)
295                     {
296                       int k;
297                       gboolean found = FALSE;
298                       
299                       uc = chars[0];
300                       
301                       for (k = 0; k < G_N_ELEMENTS (special_ucs_table); k++) 
302                         {
303                           if (special_ucs_table[k].ucs_value == uc)
304                             {
305                               p[j] = special_ucs_table[k].keyval;
306                               found = TRUE;
307                               break;
308                             }
309                         }
310                       
311                       if (!found)
312                         p[j] = gdk_unicode_to_keyval (uc);
313                     }
314                 }
315               
316               if (p[3] == p[2])
317                 p[3] = 0;
318               if (p[2] == p[1])
319                 p[2] = 0;
320               if (p[1] == p[0])
321                 p[1] = 0;
322               if (p[0] == p[2] && 
323                   p[1] == p[3])
324                 p[2] = p[3] = 0;
325             }
326         }
327       else
328         {
329           g_error ("unknown type of keyboard layout (neither KCHR nor uchr)"
330                    " - not supported right now");
331         }
332
333       for (i = 0; i < G_N_ELEMENTS (known_keys); i++)
334         {
335           p = keyval_array + known_keys[i].keycode * KEYVALS_PER_KEYCODE;
336
337           if (p[0] == 0 && p[1] == 0 && 
338               p[2] == 0 && p[3] == 0)
339             p[0] = known_keys[i].keyval;
340         }
341
342       for (i = 0; i < G_N_ELEMENTS (known_numeric_keys); i++)
343         {
344           p = keyval_array + known_numeric_keys[i].keycode * KEYVALS_PER_KEYCODE;
345
346           if (p[0] == known_numeric_keys[i].normal_keyval);
347               p[0] = known_numeric_keys[i].keypad_keyval;
348         }
349       
350       if (current_layout)
351         g_signal_emit_by_name (default_keymap, "keys_changed");
352
353       current_layout = new_layout;
354     }
355 }
356
357 GdkKeymap *
358 gdk_keymap_get_for_display (GdkDisplay *display)
359 {
360   g_return_val_if_fail (display == gdk_display_get_default (), NULL);
361
362   if (default_keymap == NULL)
363     default_keymap = g_object_new (gdk_keymap_get_type (), NULL);
364
365   return default_keymap;
366 }
367
368 PangoDirection
369 gdk_keymap_get_direction (GdkKeymap *keymap)
370 {
371   return PANGO_DIRECTION_NEUTRAL;
372 }
373
374 gboolean
375 gdk_keymap_get_entries_for_keyval (GdkKeymap     *keymap,
376                                    guint          keyval,
377                                    GdkKeymapKey **keys,
378                                    gint          *n_keys)
379 {
380   GArray *keys_array;
381   int i;
382
383   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
384   g_return_val_if_fail (keys != NULL, FALSE);
385   g_return_val_if_fail (n_keys != NULL, FALSE);
386   g_return_val_if_fail (keyval != 0, FALSE);
387
388   maybe_update_keymap ();
389
390   *n_keys = 0;
391   keys_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
392
393   for (i = 0; i < NUM_KEYCODES * KEYVALS_PER_KEYCODE; i++)
394     {
395       GdkKeymapKey key;
396
397       if (keyval_array[i] != keyval)
398         continue;
399
400       (*n_keys)++;
401
402       key.keycode = i / KEYVALS_PER_KEYCODE;
403       key.group = 0;
404       key.level = i % KEYVALS_PER_KEYCODE;
405
406       g_array_append_val (keys_array, key);
407     }
408
409   *keys = (GdkKeymapKey *)g_array_free (keys_array, FALSE);
410   
411   return *n_keys > 0;;
412 }
413
414 gboolean
415 gdk_keymap_get_entries_for_keycode (GdkKeymap     *keymap,
416                                     guint          hardware_keycode,
417                                     GdkKeymapKey **keys,
418                                     guint        **keyvals,
419                                     gint          *n_entries)
420 {
421   GArray *keys_array, *keyvals_array;
422   int i;
423   guint *p;
424
425   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
426   g_return_val_if_fail (n_entries != NULL, FALSE);
427
428   maybe_update_keymap ();
429
430   *n_entries = 0;
431
432   if (hardware_keycode > NUM_KEYCODES)
433     return FALSE;
434
435   if (keys)
436     keys_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
437   else
438     keys_array = NULL;
439
440   if (keyvals)
441     keyvals_array = g_array_new (FALSE, FALSE, sizeof (guint));
442   else
443     keyvals_array = NULL;
444
445   p = keyval_array + hardware_keycode * KEYVALS_PER_KEYCODE;
446   
447   for (i = 0; i < KEYVALS_PER_KEYCODE; i++)
448     {
449       if (!p[i])
450         continue;
451
452       (*n_entries)++;
453       
454       if (keyvals_array)
455         g_array_append_val (keyvals_array, p[i]);
456
457       if (keys_array)
458         {
459           GdkKeymapKey key;
460
461           key.keycode = hardware_keycode;
462           key.group = i / 2;
463           key.level = i % 2;
464
465           g_array_append_val (keys_array, key);
466         }
467     }
468   
469   if (keys)
470     *keys = (GdkKeymapKey *)g_array_free (keys_array, FALSE);
471
472   if (keyvals)
473     *keyvals = (guint *)g_array_free (keyvals_array, FALSE);
474
475   return *n_entries > 0;
476 }
477
478 guint
479 gdk_keymap_lookup_key (GdkKeymap          *keymap,
480                        const GdkKeymapKey *key)
481 {
482   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
483   g_return_val_if_fail (key != NULL, 0);
484   g_return_val_if_fail (key->group < 4, 0);
485
486   /* FIXME: Implement */
487
488   return 0;
489 }
490
491
492 #define GET_KEYVAL(keycode, group, level) (keyval_array[(keycode * KEYVALS_PER_KEYCODE + group * 2 + level)])
493
494 static guint
495 translate_keysym (guint           hardware_keycode,
496                   gint            group,
497                   GdkModifierType state,
498                   guint          *effective_group,
499                   guint          *effective_level)
500 {
501   gint level;
502   guint tmp_keyval;
503
504   level = (state & GDK_SHIFT_MASK) ? 1 : 0;
505
506   if (!(GET_KEYVAL (hardware_keycode, group, 0) || GET_KEYVAL (hardware_keycode, group, 1)) &&
507       (GET_KEYVAL (hardware_keycode, 0, 0) || GET_KEYVAL (hardware_keycode, 0, 1)))
508     group = 0;
509
510   if (!GET_KEYVAL (hardware_keycode, group, level) &&
511       GET_KEYVAL (hardware_keycode, group, 0))
512     level = 0;
513
514   tmp_keyval = GET_KEYVAL (hardware_keycode, group, level);
515
516   if (state & GDK_LOCK_MASK)
517     {
518       guint upper = gdk_keyval_to_upper (tmp_keyval);
519         if (upper != tmp_keyval)
520           tmp_keyval = upper;
521     }
522
523   return tmp_keyval;
524 }
525
526 gboolean
527 gdk_keymap_translate_keyboard_state (GdkKeymap       *keymap,
528                                      guint            hardware_keycode,
529                                      GdkModifierType  state,
530                                      gint             group,
531                                      guint           *keyval,
532                                      gint            *effective_group,
533                                      gint            *level,
534                                      GdkModifierType *consumed_modifiers)
535 {
536   guint tmp_keyval;
537   GdkModifierType bit;
538   guint tmp_modifiers = 0;
539
540   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
541   g_return_val_if_fail (group >= 0 && group <= 1, FALSE);
542   
543   maybe_update_keymap ();
544
545   if (keyval)
546     *keyval = 0;
547   if (effective_group)
548     *effective_group = 0;
549   if (level)
550     *level = 0;
551   if (consumed_modifiers)
552     *consumed_modifiers = 0;
553
554   if (hardware_keycode < 0 || hardware_keycode >= NUM_KEYCODES)
555     return FALSE;
556   
557   /* Check if shift or capslock modify the keyval */
558   for (bit = GDK_SHIFT_MASK; bit < GDK_CONTROL_MASK; bit <<= 1)
559     {
560       if (translate_keysym (hardware_keycode, group, state & ~bit, NULL, NULL) !=
561           translate_keysym (hardware_keycode, group, state | bit, NULL, NULL))
562         tmp_modifiers |= bit;
563     }
564
565   tmp_keyval = translate_keysym (hardware_keycode, group, state, level, effective_group);
566
567   if (consumed_modifiers)
568     *consumed_modifiers = tmp_modifiers;
569
570   if (keyval)
571     *keyval = tmp_keyval; 
572
573   return TRUE;
574 }