2 Copyright 1985, 1987, 1990, 1998 The Open Group
3 Copyright 2008 Dan Nicholson
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
28 /************************************************************
29 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
31 Permission to use, copy, modify, and distribute this
32 software and its documentation for any purpose and without
33 fee is hereby granted, provided that the above copyright
34 notice appear in all copies and that both that copyright
35 notice and this permission notice appear in supporting
36 documentation, and that the name of Silicon Graphics not be
37 used in advertising or publicity pertaining to distribution
38 of the software without specific prior written permission.
39 Silicon Graphics makes no representation about the suitability
40 of this software for any purpose. It is provided "as is"
41 without any express or implied warranty.
43 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
44 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
45 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
46 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
47 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
48 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
49 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
50 THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 ********************************************************/
60 #include <X11/Xfuncproto.h>
61 #include <X11/extensions/XKB.h>
63 typedef uint32_t xkb_keycode_t;
64 typedef uint32_t xkb_keysym_t;
65 typedef uint32_t xkb_mod_index_t;
66 typedef uint32_t xkb_group_index_t;
67 typedef uint32_t xkb_led_index_t;
69 #define XKB_MOD_INVALID (0xffffffff)
70 #define XKB_GROUP_INVALID (0xffffffff)
71 #define XKB_KEYCODE_INVALID (0xffffffff)
72 #define XKB_LED_INVALID (0xffffffff)
74 #define XKB_KEYCODE_MAX (0xffffffff - 1)
75 #define xkb_keycode_is_legal_ext(kc) (kc <= XKB_KEYCODE_MAX)
76 #define xkb_keycode_is_legal_x11(kc) (kc <= XKB_KEYCODE_MAX)
77 #define xkb_keymap_keycode_range_is_legal(xkb) \
78 (xkb->max_key_code > 0 && \
79 xkb->max_key_code > xkb->min_key_code && \
80 xkb_keycode_is_legal_ext(xkb->min_key_code) && \
81 xkb_keycode_is_legal_ext(xkb->max_key_code))
83 /* Duplicate the modifier mask defines so libxkcommon can be used
85 #define XKB_COMMON_SHIFT_MASK (1 << 0)
86 #define XKB_COMMON_LOCK_MASK (1 << 1)
87 #define XKB_COMMON_CONTROL_MASK (1 << 2)
88 #define XKB_COMMON_MOD1_MASK (1 << 3)
89 #define XKB_COMMON_MOD2_MASK (1 << 4)
90 #define XKB_COMMON_MOD3_MASK (1 << 5)
91 #define XKB_COMMON_MOD4_MASK (1 << 6)
92 #define XKB_COMMON_MOD5_MASK (1 << 7)
95 struct xkb_rule_names {
100 const char * options;
103 struct xkb_any_action {
108 struct xkb_mod_action {
116 struct xkb_group_action {
122 struct xkb_iso_action {
132 struct xkb_controls_action {
138 struct xkb_device_button_action {
146 struct xkb_device_valuator_action {
157 struct xkb_pointer_default_action {
164 struct xkb_switch_screen_action {
170 struct xkb_redirect_key_action {
172 xkb_keycode_t new_key;
179 struct xkb_pointer_action {
186 struct xkb_message_action {
192 struct xkb_pointer_button_action {
200 struct xkb_any_action any;
201 struct xkb_mod_action mods;
202 struct xkb_group_action group;
203 struct xkb_iso_action iso;
204 struct xkb_controls_action ctrls;
205 struct xkb_device_button_action devbtn;
206 struct xkb_device_valuator_action devval;
207 struct xkb_pointer_default_action dflt;
208 struct xkb_switch_screen_action screen;
209 struct xkb_redirect_key_action redirect; /* XXX wholly unnecessary? */
210 struct xkb_pointer_action ptr; /* XXX delete for DeviceValuator */
211 struct xkb_pointer_button_action btn; /* XXX delete for DeviceBtn */
212 struct xkb_message_action msg; /* XXX just delete */
217 uint32_t mask; /* effective mods */
222 struct xkb_kt_map_entry {
225 struct xkb_mods mods;
228 struct xkb_key_type {
229 struct xkb_mods mods;
231 unsigned char map_count;
232 struct xkb_kt_map_entry * map;
233 struct xkb_mods * preserve;
235 const char **level_names;
238 struct xkb_sym_interpret {
243 uint32_t virtual_mod;
244 union xkb_action act;
247 struct xkb_compat_map {
248 struct xkb_sym_interpret * sym_interpret;
249 struct xkb_mods groups[XkbNumKbdGroups];
250 unsigned short num_si;
251 unsigned short size_si;
255 unsigned char kt_index[XkbNumKbdGroups];
256 unsigned char group_info;
258 unsigned short offset;
261 #define XkbNumGroups(g) ((g)&0x0f)
262 #define XkbOutOfRangeGroupInfo(g) ((g)&0xf0)
263 #define XkbOutOfRangeGroupAction(g) ((g)&0xc0)
264 #define XkbOutOfRangeGroupNumber(g) (((g)&0x30)>>4)
265 #define XkbSetGroupInfo(g,w,n) (((w)&0xc0)|(((n)&3)<<4)|((g)&0x0f))
266 #define XkbSetNumGroups(g,n) (((g)&0xf0)|((n)&0x0f))
268 struct xkb_client_map {
269 unsigned char size_types;
270 unsigned char num_types;
271 struct xkb_key_type * types;
276 struct xkb_sym_map * key_sym_map;
278 unsigned char *modmap;
281 struct xkb_behavior {
286 struct xkb_server_map {
287 unsigned short num_acts;
288 unsigned short size_acts;
290 #if defined(__cplusplus) || defined(c_plusplus)
291 /* explicit is a C++ reserved word */
292 unsigned char * c_explicit;
294 unsigned char * explicit;
297 union xkb_action *acts;
298 struct xkb_behavior *behaviors;
299 unsigned short *key_acts;
300 unsigned char *explicits;
301 uint32_t vmods[XkbNumVirtualMods]; /* vmod -> mod mapping */
302 uint32_t *vmodmap; /* key -> vmod mapping */
306 struct xkb_indicator_map {
308 unsigned char which_groups;
309 unsigned char groups;
310 unsigned char which_mods;
311 struct xkb_mods mods;
315 struct xkb_indicator {
316 unsigned long phys_indicators;
317 struct xkb_indicator_map maps[XkbNumIndicators];
320 struct xkb_key_name {
321 char name[XkbKeyNameLength];
324 struct xkb_key_alias {
325 char real[XkbKeyNameLength];
326 char alias[XkbKeyNameLength];
330 const char *vmods[XkbNumVirtualMods];
331 const char *indicators[XkbNumIndicators];
332 const char *groups[XkbNumKbdGroups];
333 struct xkb_key_name * keys;
334 struct xkb_key_alias * key_aliases;
336 xkb_keycode_t num_keys;
337 xkb_keycode_t num_key_aliases;
340 struct xkb_controls {
341 unsigned char num_groups;
342 unsigned char groups_wrap;
343 struct xkb_mods internal;
344 struct xkb_mods ignore_lock;
345 unsigned int enabled_ctrls;
346 unsigned short repeat_delay;
347 unsigned short repeat_interval;
348 unsigned short slow_keys_delay;
349 unsigned short debounce_delay;
350 unsigned short ax_options;
351 unsigned short ax_timeout;
352 unsigned short axt_opts_mask;
353 unsigned short axt_opts_values;
354 unsigned int axt_ctrls_mask;
355 unsigned int axt_ctrls_values;
356 unsigned char *per_key_repeat;
359 /* Common keyboard description structure */
361 unsigned int defined;
362 unsigned short flags;
363 unsigned short device_spec;
364 xkb_keycode_t min_key_code;
365 xkb_keycode_t max_key_code;
367 struct xkb_controls * ctrls;
368 struct xkb_server_map * server;
369 struct xkb_client_map * map;
370 struct xkb_indicator * indicators;
371 struct xkb_names * names;
372 struct xkb_compat_map * compat;
376 #define XkbKeyGroupInfo(d,k) ((d)->map->key_sym_map[k].group_info)
377 #define XkbKeyNumGroups(d,k) (XkbNumGroups((d)->map->key_sym_map[k].group_info))
378 #define XkbKeyGroupWidth(d,k,g) (XkbKeyType(d,k,g)->num_levels)
379 #define XkbKeyGroupsWidth(d,k) ((d)->map->key_sym_map[k].width)
380 #define XkbKeyTypeIndex(d,k,g) ((d)->map->key_sym_map[k].kt_index[g&0x3])
381 #define XkbKeyType(d,k,g) (&(d)->map->types[XkbKeyTypeIndex(d,k,g)])
382 #define XkbKeyNumSyms(d,k) (XkbKeyGroupsWidth(d,k)*XkbKeyNumGroups(d,k))
383 #define XkbKeySymsOffset(d,k) ((d)->map->key_sym_map[k].offset)
384 #define XkbKeySymsPtr(d,k) (&(d)->map->syms[XkbKeySymsOffset(d,k)])
385 #define XkbKeySym(d,k,n) (XkbKeySymsPtr(d,k)[n])
386 #define XkbKeySymEntry(d,k,sl,g) \
387 (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
388 #define XkbKeyHasActions(d,k) ((d)->server->key_acts[k]!=0)
389 #define XkbKeyNumActions(d,k) (XkbKeyHasActions(d,k)?XkbKeyNumSyms(d,k):1)
390 #define XkbKeyActionsPtr(d,k) (&(d)->server->acts[(d)->server->key_acts[k]])
391 #define XkbKeyAction(d,k,n) \
392 (XkbKeyHasActions(d,k)?&XkbKeyActionsPtr(d,k)[n]:NULL)
393 #define XkbKeyActionEntry(d,k,sl,g) \
394 (XkbKeyHasActions(d,k)?\
395 XkbKeyAction(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))):NULL)
397 #define XkbKeycodeInRange(d,k) (((k)>=(d)->min_key_code)&&\
398 ((k)<=(d)->max_key_code))
399 #define XkbNumKeys(d) ((d)->max_key_code-(d)->min_key_code+1)
401 struct xkb_map_changes {
402 unsigned short changed;
403 xkb_keycode_t min_key_code;
404 xkb_keycode_t max_key_code;
405 unsigned char first_type;
406 unsigned char num_types;
407 xkb_keycode_t first_key_sym;
408 xkb_keycode_t num_key_syms;
409 xkb_keycode_t first_key_act;
410 xkb_keycode_t num_key_acts;
411 xkb_keycode_t first_key_behavior;
412 xkb_keycode_t num_key_behaviors;
413 xkb_keycode_t first_key_explicit;
414 xkb_keycode_t num_key_explicit;
415 xkb_keycode_t first_modmap_key;
416 xkb_keycode_t num_modmap_keys;
417 xkb_keycode_t first_vmodmap_key;
418 xkb_keycode_t num_vmodmap_keys;
419 unsigned short vmods;
422 struct xkb_controls_changes {
423 unsigned int changed_ctrls;
424 unsigned int enabled_ctrls_changes;
425 int num_groups_changed;
428 struct xkb_indicator_changes {
429 unsigned int state_changes;
430 unsigned int map_changes;
433 struct xkb_name_changes {
434 unsigned int changed;
435 unsigned char first_type;
436 unsigned char num_types;
437 unsigned char first_lvl;
438 unsigned char num_lvls;
439 xkb_keycode_t num_aliases;
440 xkb_keycode_t first_key;
441 xkb_keycode_t num_keys;
442 unsigned short changed_vmods;
443 unsigned long changed_indicators;
444 unsigned char changed_groups;
447 struct xkb_compat_changes {
448 unsigned char changed_groups;
449 unsigned short first_si;
450 unsigned short num_si;
454 unsigned short device_spec;
455 unsigned short state_changes;
456 struct xkb_map_changes map;
457 struct xkb_controls_changes ctrls;
458 struct xkb_indicator_changes indicators;
459 struct xkb_name_changes names;
460 struct xkb_compat_changes compat;
463 struct xkb_component_names {
471 struct xkb_component_name {
472 unsigned short flags;
477 unsigned char group; /* base + latched + locked */
478 /* FIXME: Why are base + latched short and not char?? */
479 unsigned short base_group; /* physically ... down? */
480 unsigned short latched_group;
481 unsigned char locked_group;
483 unsigned char mods; /* base + latched + locked */
484 unsigned char base_mods; /* physically down */
485 unsigned char latched_mods;
486 unsigned char locked_mods;
488 unsigned char compat_state; /* mods + group for core state */
490 /* grab mods = all depressed and latched mods, _not_ locked mods */
491 unsigned char grab_mods; /* grab mods minus internal mods */
492 unsigned char compat_grab_mods; /* grab mods + group for core state,
493 but not locked groups if
494 IgnoreGroupLocks set */
496 /* effective mods = all mods (depressed, latched, locked) */
497 unsigned char lookup_mods; /* effective mods minus internal mods */
498 unsigned char compat_lookup_mods; /* effective mods + group */
500 unsigned short ptr_buttons; /* core pointer buttons */
507 struct xkb_desc *xkb;
510 #define XkbStateFieldFromRec(s) XkbBuildCoreState((s)->lookup_mods,(s)->group)
511 #define XkbGrabStateFromRec(s) XkbBuildCoreState((s)->grab_mods,(s)->group)
513 #define XkbNumGroups(g) ((g)&0x0f)
514 #define XkbOutOfRangeGroupInfo(g) ((g)&0xf0)
515 #define XkbOutOfRangeGroupAction(g) ((g)&0xc0)
516 #define XkbOutOfRangeGroupNumber(g) (((g)&0x30)>>4)
517 #define XkbSetNumGroups(g,n) (((g)&0xf0)|((n)&0x0f))
521 _X_EXPORT extern struct xkb_desc *
522 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rules);
524 _X_EXPORT extern struct xkb_desc *
525 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg);
527 _X_EXPORT extern struct xkb_desc *
528 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName);
530 _X_EXPORT extern struct xkb_desc *
531 xkb_compile_keymap_from_string(const char *string, const char *mapName);
533 _X_EXPORT extern void
534 xkb_free_keymap(struct xkb_desc *xkb);
537 * Canonicalises component names by prepending the relevant component from
538 * 'old' to the one in 'names' when the latter has a leading '+' or '|', and
539 * by replacing a '%' with the relevant component, e.g.:
542 * ------------------------------------------
545 * foo+%|baz bar foo+bar|baz
547 * If a component in names needs to be modified, the existing value will be
548 * free()d, and a new one allocated with malloc().
550 _X_EXPORT extern void
551 xkb_canonicalise_components(struct xkb_component_names * names,
552 const struct xkb_component_names * old);
555 * Converts a keysym to a string; will return unknown Unicode codepoints
556 * as "Ua1b2", and other unknown keysyms as "0xabcd1234".
558 _X_EXPORT extern void
559 xkb_keysym_to_string(xkb_keysym_t ks, char *buffer, size_t size);
562 * See xkb_keysym_to_string comments: this function will accept any string
563 * from that function.
565 _X_EXPORT extern xkb_keysym_t
566 xkb_string_to_keysym(const char *s);
568 _X_EXPORT unsigned int
569 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
570 xkb_keysym_t **syms_out);
573 * @defgroup components XKB state components
574 * Allows enumeration of state components such as modifiers and groups within
575 * the current keymap.
581 * Returns the number of modifiers active in the keymap.
583 _X_EXPORT xkb_mod_index_t
584 xkb_map_num_mods(struct xkb_desc *xkb);
587 * Returns the name of the modifier specified by 'idx', or NULL if invalid.
589 _X_EXPORT const char *
590 xkb_map_mod_get_name(struct xkb_desc *xkb, xkb_mod_index_t idx);
593 * Returns the index of the modifier specified by 'name', or XKB_MOD_INVALID.
595 _X_EXPORT xkb_mod_index_t
596 xkb_map_mod_get_index(struct xkb_desc *xkb, const char *name);
599 * Returns the number of groups active in the keymap.
601 _X_EXPORT xkb_group_index_t
602 xkb_map_num_groups(struct xkb_desc *xkb);
605 * Returns the name of the group specified by 'idx', or NULL if invalid.
607 _X_EXPORT const char *
608 xkb_map_group_get_name(struct xkb_desc *xkb, xkb_group_index_t idx);
611 * Returns the index of the group specified by 'name', or XKB_GROUP_INVALID.
613 _X_EXPORT xkb_group_index_t
614 xkb_map_group_get_index(struct xkb_desc *xkb, const char *name);
617 * Returns the number of groups active for the specified key.
619 _X_EXPORT xkb_group_index_t
620 xkb_key_num_groups(struct xkb_desc *xkb, xkb_keycode_t key);
623 * Returns the number of LEDs in the given map.
625 _X_EXPORT xkb_led_index_t
626 xkb_map_num_leds(struct xkb_desc *xkb);
629 * Returns the name of the LED specified by 'idx', or NULL if invalid.
631 _X_EXPORT const char *
632 xkb_map_led_get_name(struct xkb_desc *xkb, xkb_led_index_t idx);
635 * Returns the index of the LED specified by 'name', or XKB_LED_INVALID.
637 _X_EXPORT xkb_led_index_t
638 xkb_map_led_get_index(struct xkb_desc *xkb, const char *name);
643 * @defgroup state XKB state objects
644 * Creation, destruction and manipulation of keyboard state objects,
645 * representing modifier and group state.
651 * Allocates a new XKB state object for use with the given keymap.
653 _X_EXPORT struct xkb_state *
654 xkb_state_new(struct xkb_desc *xkb);
657 * Adds a reference to a state object, so it will not be freed until unref.
660 xkb_state_ref(struct xkb_state *state);
663 * Unrefs and potentially deallocates a state object; the caller must not
664 * use the state object after calling this.
667 xkb_state_unref(struct xkb_state *state);
670 * Updates a state object to reflect the given key being pressed or released.
673 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down);
676 * Modifier and group types for state objects. This enum is bitmaskable,
677 * e.g. (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED) is valid to exclude
680 enum xkb_state_component {
681 XKB_STATE_DEPRESSED = (1 << 0),
682 /**< A key holding this modifier or group is currently physically
683 * depressed; also known as 'base'. */
684 XKB_STATE_LATCHED = (1 << 1),
685 /**< Modifier or group is latched, i.e. will be unset after the next
686 * non-modifier key press. */
687 XKB_STATE_LOCKED = (1 << 2),
688 /**< Modifier or group is locked, i.e. will be unset after the key
689 * provoking the lock has been pressed again. */
690 XKB_STATE_EFFECTIVE =
691 (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED | XKB_STATE_LOCKED),
692 /**< Combinatination of depressed, latched, and locked. */
696 * Returns 1 if the modifier specified by 'name' is active in the manner
697 * specified by 'type', 0 if it is unset, or -1 if the modifier does not
698 * exist in the current map.
701 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
702 enum xkb_state_component type);
705 * Returns 1 if the modifier specified by 'idx' is active in the manner
706 * specified by 'type', 0 if it is unset, or -1 if the modifier does not
707 * exist in the current map.
710 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
711 enum xkb_state_component type);
714 * Returns 1 if the group specified by 'name' is active in the manner
715 * specified by 'type', 0 if it is unset, or -1 if the group does not
716 * exist in the current map.
719 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
720 enum xkb_state_component type);
723 * Returns 1 if the group specified by 'idx' is active in the manner
724 * specified by 'type', 0 if it is unset, or -1 if the group does not
725 * exist in the current map.
728 xkb_state_group_index_is_active(struct xkb_state *state, xkb_group_index_t idx,
729 enum xkb_state_component type);
732 * Returns 1 if the LED specified by 'name' is active, 0 if it is unset, or
733 * -1 if the LED does not exist in the current map.
736 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
739 * Returns 1 if the LED specified by 'idx' is active, 0 if it is unset, or
740 * -1 if the LED does not exist in the current map.
743 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
749 #endif /* _XKBCOMMON_H_ */