]> Pileus Git - ~andy/gtk/blob - gdk/gdkkeys.c
[GI] Fixes of existing incorrect annotations
[~andy/gtk] / gdk / gdkkeys.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gdkkeysprivate.h"
30 #include "gdkdisplay.h"
31 #include "gdkdisplaymanagerprivate.h"
32
33
34 /**
35  * SECTION:keys
36  * @Short_description: Functions for manipulating keyboard codes
37  * @Title: Key Values
38  *
39  * Key values are the codes which are sent whenever a key is pressed or released.
40  * They appear in the #GdkEventKey.keyval field of the
41  * #GdkEventKey structure, which is passed to signal handlers for the
42  * #GtkWidget::key-press-event and #GtkWidget::key-release-event signals.
43  * The complete list of key values can be found in the
44  * <filename>&lt;gdk/gdkkeysyms.h&gt;</filename> header file.
45  *
46  * Key values are regularly updated from the upstream X.org X11 implementation,
47  * so new values are added regularly. They will be prefixed with GDK_KEY_ rather
48  * than XF86XK_ or XK_ (for older symbols).
49  *
50  * Key values can be converted into a string representation using
51  * gdk_keyval_name(). The reverse function, converting a string to a key value,
52  * is provided by gdk_keyval_from_name().
53  *
54  * The case of key values can be determined using gdk_keyval_is_upper() and
55  * gdk_keyval_is_lower(). Key values can be converted to upper or lower case
56  * using gdk_keyval_to_upper() and gdk_keyval_to_lower().
57  *
58  * When it makes sense, key values can be converted to and from
59  * Unicode characters with gdk_keyval_to_unicode() and gdk_unicode_to_keyval().
60  *
61  * <para id="key-group-explanation">
62  * One #GdkKeymap object exists for each user display. gdk_keymap_get_default()
63  * returns the #GdkKeymap for the default display; to obtain keymaps for other
64  * displays, use gdk_keymap_get_for_display(). A keymap
65  * is a mapping from #GdkKeymapKey to key values. You can think of a #GdkKeymapKey
66  * as a representation of a symbol printed on a physical keyboard key. That is, it
67  * contains three pieces of information. First, it contains the hardware keycode;
68  * this is an identifying number for a physical key. Second, it contains the
69  * <firstterm>level</firstterm> of the key. The level indicates which symbol on the
70  * key will be used, in a vertical direction. So on a standard US keyboard, the key
71  * with the number "1" on it also has the exclamation point ("!") character on
72  * it. The level indicates whether to use the "1" or the "!" symbol.  The letter
73  * keys are considered to have a lowercase letter at level 0, and an uppercase
74  * letter at level 1, though only the uppercase letter is printed.  Third, the
75  * #GdkKeymapKey contains a group; groups are not used on standard US keyboards,
76  * but are used in many other countries. On a keyboard with groups, there can be 3
77  * or 4 symbols printed on a single key. The group indicates movement in a
78  * horizontal direction. Usually groups are used for two different languages.  In
79  * group 0, a key might have two English characters, and in group 1 it might have
80  * two Hebrew characters. The Hebrew characters will be printed on the key next to
81  * the English characters.
82  * </para>
83  *
84  * In order to use a keymap to interpret a key event, it's necessary to first
85  * convert the keyboard state into an effective group and level. This is done via a
86  * set of rules that varies widely according to type of keyboard and user
87  * configuration. The function gdk_keymap_translate_keyboard_state() accepts a
88  * keyboard state -- consisting of hardware keycode pressed, active modifiers, and
89  * active group -- applies the appropriate rules, and returns the group/level to be
90  * used to index the keymap, along with the modifiers which did not affect the
91  * group and level. i.e. it returns "unconsumed modifiers." The keyboard group may
92  * differ from the effective group used for keymap lookups because some keys don't
93  * have multiple groups - e.g. the Enter key is always in group 0 regardless of
94  * keyboard state.
95  *
96  * Note that gdk_keymap_translate_keyboard_state() also returns the keyval, i.e. it
97  * goes ahead and performs the keymap lookup in addition to telling you which
98  * effective group/level values were used for the lookup. #GdkEventKey already
99  * contains this keyval, however, so you don't normally need to call
100  * gdk_keymap_translate_keyboard_state() just to get the keyval.
101  */
102
103
104 enum {
105   DIRECTION_CHANGED,
106   KEYS_CHANGED,
107   STATE_CHANGED,
108   LAST_SIGNAL
109 };
110
111 static guint signals[LAST_SIGNAL] = { 0 };
112
113 G_DEFINE_TYPE (GdkKeymap, gdk_keymap, G_TYPE_OBJECT)
114
115 static void
116 gdk_keymap_class_init (GdkKeymapClass *klass)
117 {
118   GObjectClass *object_class = G_OBJECT_CLASS (klass);
119
120   /**
121    * GdkKeymap::direction-changed:
122    * @keymap: the object on which the signal is emitted
123    * 
124    * The ::direction-changed signal gets emitted when the direction of
125    * the keymap changes. 
126    *
127    * Since: 2.0
128    */
129   signals[DIRECTION_CHANGED] =
130     g_signal_new ("direction-changed",
131                   G_OBJECT_CLASS_TYPE (object_class),
132                   G_SIGNAL_RUN_LAST,
133                   G_STRUCT_OFFSET (GdkKeymapClass, direction_changed),
134                   NULL, NULL,
135                   g_cclosure_marshal_VOID__VOID,
136                   G_TYPE_NONE,
137                   0);
138   /**
139    * GdkKeymap::keys-changed:
140    * @keymap: the object on which the signal is emitted
141    *
142    * The ::keys-changed signal is emitted when the mapping represented by
143    * @keymap changes.
144    *
145    * Since: 2.2
146    */
147   signals[KEYS_CHANGED] =
148     g_signal_new ("keys-changed",
149                   G_OBJECT_CLASS_TYPE (object_class),
150                   G_SIGNAL_RUN_LAST,
151                   G_STRUCT_OFFSET (GdkKeymapClass, keys_changed),
152                   NULL, NULL,
153                   g_cclosure_marshal_VOID__VOID,
154                   G_TYPE_NONE,
155                   0);
156
157   /**
158    * GdkKeymap::state-changed:
159    * @keymap: the object on which the signal is emitted
160    *
161    * The ::state-changed signal is emitted when the state of the
162    * keyboard changes, e.g when Caps Lock is turned on or off.
163    * See gdk_keymap_get_caps_lock_state().
164    *
165    * Since: 2.16
166    */
167   signals[STATE_CHANGED] =
168     g_signal_new ("state_changed",
169                   G_OBJECT_CLASS_TYPE (object_class),
170                   G_SIGNAL_RUN_LAST,
171                   G_STRUCT_OFFSET (GdkKeymapClass, state_changed),
172                   NULL, NULL,
173                   g_cclosure_marshal_VOID__VOID,
174                   G_TYPE_NONE, 
175                   0);
176 }
177
178 static void
179 gdk_keymap_init (GdkKeymap *keymap)
180 {
181 }
182
183 /* Other key-handling stuff
184  */
185
186 /**
187  * gdk_keyval_convert_case:
188  * @symbol: a keyval
189  * @lower: (out): return location for lowercase version of @symbol
190  * @upper: (out): return location for uppercase version of @symbol
191  *
192  * Obtains the upper- and lower-case versions of the keyval @symbol.
193  * Examples of keyvals are #GDK_KEY_a, #GDK_KEY_Enter, #GDK_KEY_F1, etc.
194  */
195 void
196 gdk_keyval_convert_case (guint symbol,
197                          guint *lower,
198                          guint *upper)
199 {
200   GdkDisplayManager *manager = gdk_display_manager_get ();
201
202   GDK_DISPLAY_MANAGER_GET_CLASS (manager)->keyval_convert_case (manager, symbol, lower, upper);
203 }
204
205 /**
206  * gdk_keyval_to_upper:
207  * @keyval: a key value.
208  *
209  * Converts a key value to upper case, if applicable.
210  *
211  * Returns: the upper case form of @keyval, or @keyval itself if it is already
212  *   in upper case or it is not subject to case conversion.
213  */
214 guint
215 gdk_keyval_to_upper (guint keyval)
216 {
217   guint result;
218   
219   gdk_keyval_convert_case (keyval, NULL, &result);
220
221   return result;
222 }
223
224 /**
225  * gdk_keyval_to_lower:
226  * @keyval: a key value.
227  *
228  * Converts a key value to lower case, if applicable.
229  *
230  * Returns: the lower case form of @keyval, or @keyval itself if it is already
231  *  in lower case or it is not subject to case conversion.
232  */
233 guint
234 gdk_keyval_to_lower (guint keyval)
235 {
236   guint result;
237   
238   gdk_keyval_convert_case (keyval, &result, NULL);
239
240   return result;
241 }
242
243 /**
244  * gdk_keyval_is_upper:
245  * @keyval: a key value.
246  *
247  * Returns %TRUE if the given key value is in upper case.
248  *
249  * Returns: %TRUE if @keyval is in upper case, or if @keyval is not subject to
250  *  case conversion.
251  */
252 gboolean
253 gdk_keyval_is_upper (guint keyval)
254 {
255   if (keyval)
256     {
257       guint upper_val = 0;
258       
259       gdk_keyval_convert_case (keyval, NULL, &upper_val);
260       return upper_val == keyval;
261     }
262   return FALSE;
263 }
264
265 /**
266  * gdk_keyval_is_lower:
267  * @keyval: a key value.
268  *
269  * Returns %TRUE if the given key value is in lower case.
270  *
271  * Returns: %TRUE if @keyval is in lower case, or if @keyval is not
272  *   subject to case conversion.
273  */
274 gboolean
275 gdk_keyval_is_lower (guint keyval)
276 {
277   if (keyval)
278     {
279       guint lower_val = 0;
280       
281       gdk_keyval_convert_case (keyval, &lower_val, NULL);
282       return lower_val == keyval;
283     }
284   return FALSE;
285 }
286
287 /**
288  * gdk_keymap_get_default:
289  *
290  * Returns the #GdkKeymap attached to the default display.
291  *
292  * Returns: (transfer none): the #GdkKeymap attached to the default display.
293  */
294 GdkKeymap*
295 gdk_keymap_get_default (void)
296 {
297   return gdk_keymap_get_for_display (gdk_display_get_default ());
298 }
299
300 /**
301  * gdk_keymap_get_direction:
302  * @keymap: a #GdkKeymap
303  *
304  * Returns the direction of effective layout of the keymap.
305  *
306  * Returns: %PANGO_DIRECTION_LTR or %PANGO_DIRECTION_RTL
307  *   if it can determine the direction. %PANGO_DIRECTION_NEUTRAL
308  *   otherwise.
309  **/
310 PangoDirection
311 gdk_keymap_get_direction (GdkKeymap *keymap)
312 {
313   return GDK_KEYMAP_GET_CLASS(keymap)->get_direction (keymap);
314 }
315
316 /**
317  * gdk_keymap_have_bidi_layouts:
318  * @keymap: a #GdkKeymap
319  *
320  * Determines if keyboard layouts for both right-to-left and left-to-right
321  * languages are in use.
322  *
323  * Returns: %TRUE if there are layouts in both directions, %FALSE otherwise
324  *
325  * Since: 2.12
326  **/
327 gboolean
328 gdk_keymap_have_bidi_layouts (GdkKeymap *keymap)
329 {
330   return GDK_KEYMAP_GET_CLASS(keymap)->have_bidi_layouts (keymap);
331 }
332
333 /**
334  * gdk_keymap_get_caps_lock_state:
335  * @keymap: a #GdkKeymap
336  *
337  * Returns whether the Caps Lock modifer is locked.
338  *
339  * Returns: %TRUE if Caps Lock is on
340  *
341  * Since: 2.16
342  */
343 gboolean
344 gdk_keymap_get_caps_lock_state (GdkKeymap *keymap)
345 {
346   return GDK_KEYMAP_GET_CLASS(keymap)->get_caps_lock_state (keymap);
347 }
348
349 /**
350  * gdk_keymap_get_num_lock_state:
351  * @keymap: a #GdkKeymap
352  *
353  * Returns whether the Num Lock modifer is locked.
354  *
355  * Returns: %TRUE if Num Lock is on
356  *
357  * Since: 3.0
358  */
359 gboolean
360 gdk_keymap_get_num_lock_state (GdkKeymap *keymap)
361 {
362   return GDK_KEYMAP_GET_CLASS(keymap)->get_num_lock_state (keymap);
363 }
364
365 /**
366  * gdk_keymap_get_entries_for_keyval:
367  * @keymap: a #GdkKeymap
368  * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
369  * @keys: (out) (array length=n_keys) (transfer full): return location
370  *     for an array of #GdkKeymapKey
371  * @n_keys: return location for number of elements in returned array
372  *
373  * Obtains a list of keycode/group/level combinations that will
374  * generate @keyval. Groups and levels are two kinds of keyboard mode;
375  * in general, the level determines whether the top or bottom symbol
376  * on a key is used, and the group determines whether the left or
377  * right symbol is used. On US keyboards, the shift key changes the
378  * keyboard level, and there are no groups. A group switch key might
379  * convert a keyboard between Hebrew to English modes, for example.
380  * #GdkEventKey contains a %group field that indicates the active
381  * keyboard group. The level is computed from the modifier mask.
382  * The returned array should be freed
383  * with g_free().
384  *
385  * Return value: %TRUE if keys were found and returned
386  **/
387 gboolean
388 gdk_keymap_get_entries_for_keyval (GdkKeymap     *keymap,
389                                    guint          keyval,
390                                    GdkKeymapKey **keys,
391                                    gint          *n_keys)
392 {
393   return GDK_KEYMAP_GET_CLASS(keymap)->get_entries_for_keyval (keymap, keyval, keys, n_keys);
394 }
395
396 /**
397  * gdk_keymap_get_entries_for_keycode:
398  * @keymap: a #GdkKeymap
399  * @hardware_keycode: a keycode
400  * @keys: (out): return location for array of #GdkKeymapKey, or %NULL
401  * @keyvals: (out): return location for array of keyvals, or %NULL
402  * @n_entries: length of @keys and @keyvals
403  *
404  * Returns the keyvals bound to @hardware_keycode.
405  * The Nth #GdkKeymapKey in @keys is bound to the Nth
406  * keyval in @keyvals. Free the returned arrays with g_free().
407  * When a keycode is pressed by the user, the keyval from
408  * this list of entries is selected by considering the effective
409  * keyboard group and level. See gdk_keymap_translate_keyboard_state().
410  *
411  * Returns: %TRUE if there were any entries
412  **/
413 gboolean
414 gdk_keymap_get_entries_for_keycode (GdkKeymap     *keymap,
415                                     guint          hardware_keycode,
416                                     GdkKeymapKey **keys,
417                                     guint        **keyvals,
418                                     gint          *n_entries)
419 {
420   return GDK_KEYMAP_GET_CLASS(keymap)->get_entries_for_keycode (keymap, hardware_keycode, keys, keyvals, n_entries);
421 }
422
423 /**
424  * gdk_keymap_lookup_key:
425  * @keymap: a #GdkKeymap
426  * @key: a #GdkKeymapKey with keycode, group, and level initialized
427  *
428  * Looks up the keyval mapped to a keycode/group/level triplet.
429  * If no keyval is bound to @key, returns 0. For normal user input,
430  * you want to use gdk_keymap_translate_keyboard_state() instead of
431  * this function, since the effective group/level may not be
432  * the same as the current keyboard state.
433  *
434  * Return value: a keyval, or 0 if none was mapped to the given @key
435  **/
436 guint
437 gdk_keymap_lookup_key (GdkKeymap          *keymap,
438                        const GdkKeymapKey *key)
439 {
440   return GDK_KEYMAP_GET_CLASS(keymap)->lookup_key (keymap, key);
441 }
442
443 /**
444  * gdk_keymap_translate_keyboard_state:
445  * @keymap: a #GdkKeymap
446  * @hardware_keycode: a keycode
447  * @state: a modifier state
448  * @group: active keyboard group
449  * @keyval: (out) (allow-none): return location for keyval, or %NULL
450  * @effective_group: (out) (allow-none): return location for effective
451  *     group, or %NULL
452  * @level: (out) (allow-none): return location for level, or %NULL
453  * @consumed_modifiers: (out) (allow-none): return location for modifiers
454  *     that were used to determine the group or level, or %NULL
455  *
456  * Translates the contents of a #GdkEventKey into a keyval, effective
457  * group, and level. Modifiers that affected the translation and
458  * are thus unavailable for application use are returned in
459  * @consumed_modifiers.
460  * See <xref linkend="key-group-explanation"/> for an explanation of
461  * groups and levels. The @effective_group is the group that was
462  * actually used for the translation; some keys such as Enter are not
463  * affected by the active keyboard group. The @level is derived from
464  * @state. For convenience, #GdkEventKey already contains the translated
465  * keyval, so this function isn't as useful as you might think.
466  *
467  * <note><para>
468  * @consumed_modifiers gives modifiers that should be masked out
469  * from @state when comparing this key press to a hot key. For
470  * instance, on a US keyboard, the <literal>plus</literal>
471  * symbol is shifted, so when comparing a key press to a
472  * <literal>&lt;Control&gt;plus</literal> accelerator &lt;Shift&gt; should
473  * be masked out.
474  * </para>
475  * <informalexample><programlisting>
476  * &sol;* We want to ignore irrelevant modifiers like ScrollLock *&sol;
477  * &num;define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
478  * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
479  *                                      event->state, event->group,
480  *                                      &amp;keyval, NULL, NULL, &amp;consumed);
481  * if (keyval == GDK_PLUS &&
482  *     (event->state &amp; ~consumed &amp; ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
483  *   &sol;* Control was pressed *&sol;
484  * </programlisting></informalexample>
485  * <para>
486  * An older interpretation @consumed_modifiers was that it contained
487  * all modifiers that might affect the translation of the key;
488  * this allowed accelerators to be stored with irrelevant consumed
489  * modifiers, by doing:</para>
490  * <informalexample><programlisting>
491  * &sol;* XXX Don't do this XXX *&sol;
492  * if (keyval == accel_keyval &&
493  *     (event->state &amp; ~consumed &amp; ALL_ACCELS_MASK) == (accel_mods &amp; ~consumed))
494  *   &sol;* Accelerator was pressed *&sol;
495  * </programlisting></informalexample>
496  * <para>
497  * However, this did not work if multi-modifier combinations were
498  * used in the keymap, since, for instance, <literal>&lt;Control&gt;</literal>
499  * would be masked out even if only <literal>&lt;Control&gt;&lt;Alt&gt;</literal>
500  * was used in the keymap. To support this usage as well as well as
501  * possible, all <emphasis>single modifier</emphasis> combinations
502  * that could affect the key for any combination of modifiers will
503  * be returned in @consumed_modifiers; multi-modifier combinations
504  * are returned only when actually found in @state. When you store
505  * accelerators, you should always store them with consumed modifiers
506  * removed. Store <literal>&lt;Control&gt;plus</literal>,
507  * not <literal>&lt;Control&gt;&lt;Shift&gt;plus</literal>,
508  * </para></note>
509  *
510  * Return value: %TRUE if there was a keyval bound to the keycode/state/group
511  **/
512 gboolean
513 gdk_keymap_translate_keyboard_state (GdkKeymap       *keymap,
514                                      guint            hardware_keycode,
515                                      GdkModifierType  state,
516                                      gint             group,
517                                      guint           *keyval,
518                                      gint            *effective_group,
519                                      gint            *level,
520                                      GdkModifierType *consumed_modifiers)
521 {
522   return GDK_KEYMAP_GET_CLASS(keymap)->translate_keyboard_state (keymap,
523                                                                   hardware_keycode,
524                                                                   state,
525                                                                   group,
526                                                                   keyval,
527                                                                   effective_group,
528                                                                   level,
529                                                                   consumed_modifiers);
530 }
531
532 /**
533  * gdk_keymap_add_virtual_modifiers:
534  * @keymap: a #GdkKeymap
535  * @state: pointer to the modifier mask to change
536  *
537  * Adds virtual modifiers (i.e. Super, Hyper and Meta) which correspond
538  * to the real modifiers (i.e Mod2, Mod3, ...) in @modifiers.
539  * are set in @state to their non-virtual counterparts (i.e. Mod2,
540  * Mod3,...) and set the corresponding bits in @state.
541  *
542  * GDK already does this before delivering key events, but for
543  * compatibility reasons, it only sets the first virtual modifier
544  * it finds, whereas this function sets all matching virtual modifiers.
545  *
546  * This function is useful when matching key events against
547  * accelerators.
548  *
549  * Since: 2.20
550  */
551 void
552 gdk_keymap_add_virtual_modifiers (GdkKeymap       *keymap,
553                                   GdkModifierType *state)
554 {
555   GDK_KEYMAP_GET_CLASS(keymap)->add_virtual_modifiers (keymap, state);
556 }
557
558 /**
559  * gdk_keymap_map_virtual_modifiers:
560  * @keymap: a #GdkKeymap
561  * @state: pointer to the modifier state to map
562  *
563  * Maps the virtual modifiers (i.e. Super, Hyper and Meta) which
564  * are set in @state to their non-virtual counterparts (i.e. Mod2,
565  * Mod3,...) and set the corresponding bits in @state.
566  *
567  * This function is useful when matching key events against
568  * accelerators.
569  *
570  * Returns: %TRUE if no virtual modifiers were mapped to the
571  *     same non-virtual modifier. Note that %FALSE is also returned
572  *     if a virtual modifier is mapped to a non-virtual modifier that
573  *     was already set in @state.
574  *
575  * Since: 2.20
576  */
577 gboolean
578 gdk_keymap_map_virtual_modifiers (GdkKeymap       *keymap,
579                                   GdkModifierType *state)
580 {
581   return GDK_KEYMAP_GET_CLASS(keymap)->map_virtual_modifiers (keymap, state);
582 }
583
584 /**
585  * gdk_keyval_name:
586  * @keyval: a key value
587  *
588  * Converts a key value into a symbolic name.
589  *
590  * The names are the same as those in the
591  * <filename>&lt;gdk/gdkkeysyms.h&gt;</filename> header file
592  * but without the leading "GDK_KEY_".
593  *
594  * Return value: (transfer none): a string containing the name of the key,
595  *     or %NULL if @keyval is not a valid key. The string should not be
596  *     modified.
597  */
598 gchar*
599 gdk_keyval_name (guint keyval)
600 {
601   GdkDisplayManager *manager = gdk_display_manager_get ();
602
603   return GDK_DISPLAY_MANAGER_GET_CLASS (manager)->get_keyval_name (manager, keyval);
604 }
605
606 /**
607  * gdk_keyval_from_name:
608  * @keyval_name: a key name
609  *
610  * Converts a key name to a key value.
611  *
612  * The names are the same as those in the
613  * <filename>&lt;gdk/gdkkeysyms.h&gt;</filename> header file
614  * but without the leading "GDK_KEY_".
615  *
616  * Returns: the corresponding key value, or %GDK_KEY_VoidSymbol
617  *     if the key name is not a valid key
618  */
619 guint
620 gdk_keyval_from_name (const gchar *keyval_name)
621 {
622   GdkDisplayManager *manager = gdk_display_manager_get ();
623
624   return GDK_DISPLAY_MANAGER_GET_CLASS (manager)->lookup_keyval (manager, keyval_name);
625 }