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 ********************************************************/
55 * Copyright © 2009-2012 Daniel Stone
56 * Copyright © 2012 Intel Corporation
58 * Permission is hereby granted, free of charge, to any person obtaining a
59 * copy of this software and associated documentation files (the "Software"),
60 * to deal in the Software without restriction, including without limitation
61 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
62 * and/or sell copies of the Software, and to permit persons to whom the
63 * Software is furnished to do so, subject to the following conditions:
65 * The above copyright notice and this permission notice (including the next
66 * paragraph) shall be included in all copies or substantial portions of the
69 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
72 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
74 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
75 * DEALINGS IN THE SOFTWARE.
77 * Author: Daniel Stone <daniel@fooishbar.org>
87 #include <xkbcommon/xkbcommon-names.h>
88 #include <xkbcommon/xkbcommon-keysyms.h>
90 typedef uint32_t xkb_keycode_t;
91 typedef uint32_t xkb_keysym_t;
92 typedef uint32_t xkb_mod_index_t;
93 typedef uint32_t xkb_mod_mask_t;
94 typedef uint32_t xkb_layout_index_t;
95 typedef uint32_t xkb_layout_mask_t;
96 typedef uint32_t xkb_level_index_t;
97 typedef uint32_t xkb_led_index_t;
98 typedef uint32_t xkb_led_mask_t;
100 #define XKB_MOD_INVALID (0xffffffff)
101 #define XKB_LAYOUT_INVALID (0xffffffff)
102 #define XKB_KEYCODE_INVALID (0xffffffff)
103 #define XKB_LEVEL_INVALID (0xffffffff)
104 #define XKB_LED_INVALID (0xffffffff)
106 #define XKB_KEYCODE_MAX (0xffffffff - 1)
107 #define xkb_keycode_is_legal_ext(kc) (kc <= XKB_KEYCODE_MAX)
108 #define xkb_keycode_is_legal_x11(kc) (kc >= 8 && kc <= 255)
111 * Names to compile a keymap with, also known as RMLVO. These names together
112 * should be the primary identifier for a keymap.
114 struct xkb_rule_names {
123 * Opaque context object; may only be created, accessed, manipulated and
124 * destroyed through the xkb_context_*() API.
129 * Opaque keymap object; may only be created, accessed, manipulated and
130 * destroyed through the xkb_state_*() API.
135 * Opaque state object; may only be created, accessed, manipulated and
136 * destroyed through the xkb_state_*() API.
145 * Returns the name for a keysym as a string; will return unknown Unicode
146 * codepoints as "Ua1b2", and other unknown keysyms as "0xabcd1234".
147 * If the buffer passed is too small, the string is truncated; a size of
148 * at least 32 bytes is recommended.
151 xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size);
154 * See xkb_keysym_get_name comments: this function will accept any string
155 * from that function.
158 xkb_keysym_from_name(const char *s);
161 * Return the printable representation of the keystring in Unicode/UTF-8.
162 * The buffer passed must be at least 7 bytes long. The return value
163 * is the number of bytes written to the buffer. A return value of zero
164 * means that the keysym does not have a known printable Unicode
165 * representation, and a return value of -1 means that the buffer was too
166 * small to contain the return.
169 xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size);
172 * Returns the Unicode/UTF-32 representation of the provided keysym, which is
173 * also compatible with UCS-4. A return value of zero means the keysym does
174 * not have a known printable Unicode representation.
177 xkb_keysym_to_utf32(xkb_keysym_t keysym);
180 * @defgroup context XKB contexts
181 * Every keymap compilation request must have an XKB context associated with
182 * it. The context keeps around state such as the include path.
187 enum xkb_context_flags {
188 /** Create this context with an empty include path. */
189 XKB_CONTEXT_NO_DEFAULT_INCLUDES = 1,
193 * Returns a new XKB context, or NULL on failure. If successful, the caller
194 * holds a reference on the context, and must free it when finished with
195 * xkb_context_unref().
198 xkb_context_new(enum xkb_context_flags flags);
201 * Appends a new entry to the include path used for keymap compilation.
202 * Returns 1 on success, or 0 if the include path could not be added or is
206 xkb_context_include_path_append(struct xkb_context *context, const char *path);
209 * Appends the default include paths to the context's current include path.
210 * Returns 1 on success, or 0 if the primary include path could not be
214 xkb_context_include_path_append_default(struct xkb_context *context);
217 * Removes all entries from the context's include path, and inserts the
218 * default paths. Returns 1 on success, or 0 if the primary include path
219 * could not be added.
222 xkb_context_include_path_reset_defaults(struct xkb_context *context);
225 * Removes all entries from the context's include path.
228 xkb_context_include_path_clear(struct xkb_context *context);
231 * Returns the number of include paths currently active in the context.
234 xkb_context_num_include_paths(struct xkb_context *context);
237 * Returns the include path at the specified index within the context.
240 xkb_context_include_path_get(struct xkb_context *context, unsigned int index);
243 * Takes a new reference on an XKB context.
246 xkb_context_ref(struct xkb_context *context);
249 * Releases a reference on an XKB context, and possibly frees it.
252 xkb_context_unref(struct xkb_context *context);
257 * @defgroup logging Logging handling
258 * These functions allow you to manipulate how logging from this library
265 /** Log critical internal errors only */
266 XKB_LOG_LEVEL_CRITICAL = 10,
267 /** Log all errors */
268 XKB_LOG_LEVEL_ERROR = 20,
269 /** Log warnings and errors */
270 XKB_LOG_LEVEL_WARNING = 30,
271 /** Log information, warnings, and errors */
272 XKB_LOG_LEVEL_INFO = 40,
273 /** Log all the things */
274 XKB_LOG_LEVEL_DEBUG = 50,
278 * Sets the function to be called for logging messages.
279 * Passing NULL restores the default function, which logs to stderr.
282 xkb_context_set_log_fn(struct xkb_context *context,
283 void (*log_fn)(struct xkb_context *context,
284 enum xkb_log_level level,
285 const char *format, va_list args));
287 * Sets the current logging level. The value controls which messages
288 * are logged. The default level is XKB_LOG_LEVEL_ERROR.
290 * The environment variable XKB_LOG, if set, overrides the default value
291 * and may be specified as a level number or name.
294 xkb_context_set_log_level(struct xkb_context *context,
295 enum xkb_log_level level);
298 * Returns the current logging level.
301 xkb_context_get_log_level(struct xkb_context *context);
304 * Sets the current logging verbosity, a value from 0 to 10.
306 * The library can generate a number of warnings which are not helpful to
307 * ordinary users of the library. The verbosity may be increased if more
308 * information is desired (e.g. when developing a keymap). Defaults to 0.
309 * The environment variable XKB_VERBOSITY, if set, overrdies the default
312 * Note that most verbose messages are of level XKB_LOG_LEVEL_WARNING
316 xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity);
319 * Returns the current logging verbosity.
322 xkb_context_get_log_verbosity(struct xkb_context *ctx);
325 * Retrieves stored data pointer from the context. This might be useful
326 * to access from callbacks like a custom logging function.
328 * If context is NULL, returns NULL.
331 xkb_context_get_user_data(struct xkb_context *context);
334 * Store custom user data in the context.
337 xkb_context_set_user_data(struct xkb_context *context, void *user_data);
342 * @defgroup map Keymap management
343 * These utility functions allow you to create and deallocate XKB keymaps.
348 enum xkb_keymap_compile_flags {
349 /** Apparently you can't have empty enums. What a drag. */
350 XKB_MAP_COMPILE_PLACEHOLDER = 0,
354 * The primary keymap entry point: creates a new XKB keymap from a set of
355 * RMLVO (Rules + Model + Layout + Variant + Option) names.
357 * You should almost certainly be using this and nothing else to create
361 xkb_keymap_new_from_names(struct xkb_context *context,
362 const struct xkb_rule_names *names,
363 enum xkb_keymap_compile_flags flags);
365 enum xkb_keymap_format {
366 /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
367 XKB_KEYMAP_FORMAT_TEXT_V1 = 1,
371 * Creates an XKB keymap from a full text XKB keymap passed into the
375 xkb_keymap_new_from_file(struct xkb_context *context, FILE *file,
376 enum xkb_keymap_format format,
377 enum xkb_keymap_compile_flags flags);
380 * Creates an XKB keymap from a full text XKB keymap serialized into one
384 xkb_keymap_new_from_string(struct xkb_context *context, const char *string,
385 enum xkb_keymap_format format,
386 enum xkb_keymap_compile_flags flags);
389 * Returns the compiled XKB map as a string which can later be fed back into
390 * xkb_map_new_from_string to return the exact same keymap.
393 xkb_keymap_get_as_string(struct xkb_keymap *keymap);
396 * Takes a new reference on a keymap.
399 xkb_keymap_ref(struct xkb_keymap *keymap);
402 * Releases a reference on a keymap.
405 xkb_keymap_unref(struct xkb_keymap *keymap);
410 * @defgroup components XKB state components
411 * Allows enumeration of state components such as modifiers and groups within
412 * the current keymap.
418 * Returns the number of modifiers active in the keymap.
421 xkb_keymap_num_mods(struct xkb_keymap *keymap);
424 * Returns the name of the modifier specified by 'idx', or NULL if invalid.
427 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx);
430 * Returns the index of the modifier specified by 'name', or XKB_MOD_INVALID.
433 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name);
436 * Returns the number of groups active in the keymap.
439 xkb_keymap_num_layouts(struct xkb_keymap *keymap);
442 * Returns the name of the group specified by 'idx', or NULL if invalid.
445 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx);
448 * Returns the index of the layout specified by 'name', or XKB_LAYOUT_INVALID.
451 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name);
454 * Returns the number of layouts active for the specified key.
457 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t key);
460 * Returns the number of levels active for the specified key and layout.
463 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t key,
464 xkb_layout_index_t layout);
467 * Returns 1 if the key should repeat, or 0 otherwise.
470 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t key);
473 * Returns the number of LEDs in the given map.
476 xkb_keymap_num_leds(struct xkb_keymap *keymap);
479 * Returns the name of the LED specified by 'idx', or NULL if invalid.
482 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx);
485 * Returns the index of the LED specified by 'name', or XKB_LED_INVALID.
488 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name);
493 * @defgroup state XKB state objects
494 * Creation, destruction and manipulation of keyboard state objects,
495 * representing modifier and group state.
501 * Returns a new XKB state object for use with the given keymap, or NULL on
505 xkb_state_new(struct xkb_keymap *keymap);
508 * Takes a new reference on a state object.
511 xkb_state_ref(struct xkb_state *state);
514 * Unrefs and potentially deallocates a state object; the caller must not
515 * use the state object after calling this.
518 xkb_state_unref(struct xkb_state *state);
521 * Get the keymap from which the state object was created. Does not take
522 * a new reference on the map; you must explicitly reference it yourself
523 * if you plan to use it beyond the lifetime of the state.
526 xkb_state_get_keymap(struct xkb_state *state);
528 enum xkb_key_direction {
534 * Updates a state object to reflect the given key being pressed or released.
537 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
538 enum xkb_key_direction direction);
541 * Gives the symbols obtained from pressing a particular key with the given
542 * state. *syms_out will be set to point to an array of keysyms, with the
543 * return value being the number of symbols in *syms_out. If the return
544 * value is 0, *syms_out will be set to NULL, as there are no symbols produced
548 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
549 const xkb_keysym_t **syms_out);
552 * Returns the layout number that would be active for a particular key with
556 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t key);
559 * Returns the level number that would be active for a particular key with
560 * the given state and layout number, usually obtained from
561 * xkb_state_key_get_layout.
564 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t key,
565 xkb_layout_index_t layout);
568 * Gives the symbols obtained from pressing a particular key with the given
569 * layout and level. *syms_out will be set to point to an array of keysyms,
570 * with the return value being the number of symbols in *syms_out. If the
571 * return value is 0, *syms_out will be set to NULL, as there are no symbols
572 * produced by this event.
575 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
577 xkb_layout_index_t layout,
578 xkb_level_index_t level,
579 const xkb_keysym_t **syms_out);
582 * Modifier and group types for state objects. This enum is bitmaskable,
583 * e.g. (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED) is valid to exclude
586 enum xkb_state_component {
587 /** A key holding this modifier or group is currently physically
588 * depressed; also known as 'base'. */
589 XKB_STATE_DEPRESSED = (1 << 0),
590 /** Modifier or group is latched, i.e. will be unset after the next
591 * non-modifier key press. */
592 XKB_STATE_LATCHED = (1 << 1),
593 /** Modifier or group is locked, i.e. will be unset after the key
594 * provoking the lock has been pressed again. */
595 XKB_STATE_LOCKED = (1 << 2),
596 /** Combinatination of depressed, latched, and locked. */
597 XKB_STATE_EFFECTIVE =
598 (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED | XKB_STATE_LOCKED),
602 * Match flags for xkb_state_mod_indices_are_active and
603 * xkb_state_mod_names_are_active, specifying how the conditions for a
604 * successful match. XKB_STATE_MATCH_NON_EXCLUSIVE is bitmaskable with
607 enum xkb_state_match {
608 /** Returns true if any of the modifiers are active. */
609 XKB_STATE_MATCH_ANY = (1 << 0),
610 /** Returns true if all of the modifiers are active. */
611 XKB_STATE_MATCH_ALL = (1 << 1),
612 /** Makes matching non-exclusive, i.e. will not return false if a
613 * modifier not specified in the arguments is active. */
614 XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16),
618 * Updates a state object from a set of explicit masks. This entrypoint is
619 * really only for window systems and the like, where a master process
620 * holds an xkb_state, then serializes it over a wire protocol, and clients
621 * then use the serialization to feed in to their own xkb_state.
623 * All parameters must always be passed, or the resulting state may be
626 * The serialization is lossy and will not survive round trips; it must only
627 * be used to feed slave state objects, and must not be used to update the
630 * Please do not use this unless you fit the description above.
633 xkb_state_update_mask(struct xkb_state *state, xkb_mod_mask_t base_mods,
634 xkb_mod_mask_t latched_mods, xkb_mod_mask_t locked_mods,
635 xkb_layout_index_t base_group,
636 xkb_layout_index_t latched_group,
637 xkb_layout_index_t locked_group);
640 * The counterpart to xkb_state_update_mask, to be used on the server side
641 * of serialization. Returns a xkb_mod_mask_t representing the given
642 * component(s) of the state.
644 * This function should not be used in regular clients; please use the
645 * xkb_state_mod_*_is_active or xkb_state_foreach_active_mod API instead.
647 * Can return NULL on failure.
650 xkb_state_serialize_mods(struct xkb_state *state,
651 enum xkb_state_component component);
654 * The group equivalent of xkb_state_serialize_mods: please see its
658 xkb_state_serialize_layout(struct xkb_state *state,
659 enum xkb_state_component component);
662 * Returns 1 if the modifier specified by 'name' is active in the manner
663 * specified by 'type', 0 if it is unset, or -1 if the modifier does not
667 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
668 enum xkb_state_component type);
671 * Returns 1 if the modifiers specified by the varargs (NULL-terminated
672 * strings, with a NULL sentinel) are active in the manner specified by
673 * 'match', 0 otherwise, or -1 if any of the modifiers do not exist in
677 xkb_state_mod_names_are_active(struct xkb_state *state,
678 enum xkb_state_component type,
679 enum xkb_state_match match,
683 * Returns 1 if the modifier specified by 'idx' is active in the manner
684 * specified by 'type', 0 if it is unset, or -1 if the modifier does not
685 * exist in the current map.
688 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
689 enum xkb_state_component type);
692 * Returns 1 if the modifiers specified by the varargs (of type
693 * xkb_mod_index_t, with a XKB_MOD_INVALID sentinel) are active in the
694 * manner specified by 'match' and 'type', 0 otherwise, or -1 if any of
695 * the modifiers do not exist in the map.
698 xkb_state_mod_indices_are_active(struct xkb_state *state,
699 enum xkb_state_component type,
700 enum xkb_state_match match,
704 * Returns 1 if the modifier specified by 'idx' is used in the
705 * translation of the keycode 'key' to the key symbols obtained by
706 * pressing it (as in xkb_key_get_syms), given the current state.
707 * Returns 0 otherwise.
710 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t key,
711 xkb_mod_index_t idx);
714 * Takes the given modifier mask, and removes all modifiers which are
715 * marked as 'consumed' (see xkb_key_mod_index_is_consumed definition)
716 * for that particular key.
719 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t key,
720 xkb_mod_mask_t mask);
723 * Returns 1 if the group specified by 'name' 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_layout_name_is_active(struct xkb_state *state, const char *name,
729 enum xkb_state_component type);
732 * Returns 1 if the group specified by 'idx' is active in the manner
733 * specified by 'type', 0 if it is unset, or -1 if the group does not
734 * exist in the current map.
737 xkb_state_layout_index_is_active(struct xkb_state *state,
738 xkb_layout_index_t idx,
739 enum xkb_state_component type);
742 * Returns 1 if the LED specified by 'name' is active, 0 if it is unset, or
743 * -1 if the LED does not exist in the current map.
746 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
749 * Returns 1 if the LED specified by 'idx' is active, 0 if it is unset, or
750 * -1 if the LED does not exist in the current map.
753 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
757 /* Leave this include last, so it can pick up our types, etc. */
758 #include <xkbcommon/xkbcommon-compat.h>
764 #endif /* _XKBCOMMON_H_ */