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
57 * Copyright © 2012 Ran Benita
59 * Permission is hereby granted, free of charge, to any person obtaining a
60 * copy of this software and associated documentation files (the "Software"),
61 * to deal in the Software without restriction, including without limitation
62 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
63 * and/or sell copies of the Software, and to permit persons to whom the
64 * Software is furnished to do so, subject to the following conditions:
66 * The above copyright notice and this permission notice (including the next
67 * paragraph) shall be included in all copies or substantial portions of the
70 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
73 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
75 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
76 * DEALINGS IN THE SOFTWARE.
78 * Author: Daniel Stone <daniel@fooishbar.org>
88 #include <xkbcommon/xkbcommon-names.h>
89 #include <xkbcommon/xkbcommon-keysyms.h>
97 * Main libxkbcommon API.
101 * @struct xkb_context
102 * Opaque top level library context object.
104 * The context contains various general library data and state, like
105 * logging level and include paths.
106 * Objects are created in a specific context, and multiple contexts may
107 * coexist simultaneously. Objects from different contexts are completely
108 * separated and do not share any memory or state.
109 * A context is created, accessed, manipulated and destroyed through the
110 * xkb_context_*() API.
116 * Opaque compiled keymap object.
118 * The keymap object holds all of the static keyboard information obtained
119 * from compiling XKB files.
121 * A keymap is immutable after it is created (besides reference counts, etc.);
122 * if you need to change it, you must create a new one.
124 * A keymap object is created, accessed and destroyed through the
125 * xkb_keymap_*() API.
131 * Opaque keyboard state object.
133 * State objects contain the active state of a keyboard (or keyboards), such
134 * as the currently effective layout and the active modifiers. It acts as a
135 * simple state machine, wherein key presses and releases are the input, and
136 * key symbols (keysyms) are the output.
138 * A state object is created, accessed, manipulated and destroyed through the
144 * A number used to represent a physical key on a keyboard.
146 * A standard PC-compatible keyboard might have 102 keys. An appropriate
147 * keymap would assign each of them a keycode, by which the user should
148 * refer to the key throughout the library.
150 * Historically, the X11 protocol, and consequentially the XKB protocol,
151 * assign only 8 bits for keycodes. This limits the number of different
152 * keys that can be used simultaneously in a single keymap to 256
153 * (disregarding other limitations). This library does not share this limit;
154 * keycodes beyond 255 ('extended keycodes') are not treated specially.
155 * Keymaps and applications which are compatible with X11 should not use
158 * @todo Explain how keycodes are mapped to scancodes.
160 * @sa xkb_keycode_is_legal_ext() xkb_keycode_is_legal_x11()
162 typedef uint32_t xkb_keycode_t;
165 * A number used to represent the symbols visible on the keycaps of a
168 * A key, represented by a keycode, may generate different symbols according
169 * to keyboard state. For example, on a QWERTY keyboard, pressing the key
170 * labled \<A\> generates the symbol 'a'. If the Shift key is held, it
171 * generates the symbol 'A'. If a different layout is used, say Greek,
172 * it generates the symbol 'α'. And so on.
174 * Each such symbol is represented by a keysym. Note that keysyms are
175 * somewhat more general, in that they can also represent some "function",
176 * such as "Left" or "Right" for the arrow keys. For more information,
178 * http://www.x.org/releases/X11R7.7/doc/xproto/x11protocol.html#keysym_encoding
180 * Specifically named keysyms can be found in the
181 * xkbcommon/xkbcommon-keysyms.h header file. Their name does not include
182 * the XKB_KEY_ prefix.
184 * Besides those, any Unicode/ISO 10646 character in the range U0100 to
185 * U10FFFF can be represented by a keysym value in the range 0x01000100 to
186 * 0x0110FFFF. The name of Unicode keysyms is "U<codepoint>", e.g. "Ua1b2".
188 * The name of other unnamed keysyms is the hexadecimal representation of
189 * their value, e.g. "0xabcd1234".
191 * Keysym names are case-sensitive.
193 typedef uint32_t xkb_keysym_t;
196 * Index of a modifier.
198 * @todo Explain what are modifiers.
200 * Modifier indexes are consecutive. The first modifier has index 0.
202 * Each modifier must have a name, and the names are unique. Therefore, it
203 * is safe to use the name as a unique identifier for a modifier. Modifier
204 * names are case-sensitive.
206 * @sa xkb_keymap_num_mods()
208 typedef uint32_t xkb_mod_index_t;
209 /** A mask of modifier indexes. */
210 typedef uint32_t xkb_mod_mask_t;
213 * Index of a keyboard layout.
215 * @todo Explain what are layouts.
217 * Layout indexes are consecutive. The first layout has index 0.
219 * Each layout is not required to have a name, and the names are not
220 * guarenteed to be unique (though they are usually provided and unique).
221 * Therefore, it is not safe to use the name as a unique identifier for a
222 * layout. Layout names are case-sensitive.
224 * Layouts are also called "groups" by XKB.
226 * @sa xkb_keymap_num_layouts() xkb_keymap_num_layouts_for_key()
228 typedef uint32_t xkb_layout_index_t;
229 /** A mask of layout indexes. */
230 typedef uint32_t xkb_layout_mask_t;
233 * Index of a shift level.
235 * @todo Explain what are shift levels.
237 typedef uint32_t xkb_level_index_t;
240 * Index of a keyboard LED.
242 * @todo Explain what are LEDs.
244 * LED indexes are non-consecutive. The first LED has index 0.
246 * LED names are case-sensitive.
248 * Each LED must have a name, and the names are unique. Therefore,
249 * it is safe to use the name as a unique identifier for a LED.
251 * @warning A given keymap may specify an exact index for a given LED.
252 * Therefore, LED indexing is not necessarily sequential, as opposed to
253 * modifiers and layouts. This means that when iterating over the LEDs
254 * in a keymap using e.g. xkb_keymap_num_leds(), some indexes might be
255 * invalid. Given such an index, functions like xkb_keymap_led_get_name()
256 * will return NULL, and xkb_state_led_index_is_active() will return -1.
258 * LEDs are also called "indicators" by XKB.
260 * @sa xkb_keymap_num_leds()
262 typedef uint32_t xkb_led_index_t;
263 /** A mask of LED indexes. */
264 typedef uint32_t xkb_led_mask_t;
266 #define XKB_MOD_INVALID (0xffffffff)
267 #define XKB_LAYOUT_INVALID (0xffffffff)
268 #define XKB_KEYCODE_INVALID (0xffffffff)
269 #define XKB_LEVEL_INVALID (0xffffffff)
270 #define XKB_LED_INVALID (0xffffffff)
272 #define XKB_KEYCODE_MAX (0xffffffff - 1)
275 * Test whether a value is a valid extended keycode.
278 #define xkb_keycode_is_legal_ext(key) (key <= XKB_KEYCODE_MAX)
281 * Test whether a value is a valid X11 keycode.
284 #define xkb_keycode_is_legal_x11(key) (key >= 8 && key <= 255)
287 * Names to compile a keymap with, also known as RMLVO.
289 * These names together are the primary identifier for a keymap.
290 * If any of the members is NULL or an empty string (""), a default value is
291 * used. It is recommended to use the system default by passing NULL for
292 * unspecified values, instead of providing your own defaults.
294 struct xkb_rule_names {
295 /** The rules file to use. The rules file describes how to interpret
296 * the values of the model, layout, variant and options fields. */
298 /** The keyboard model by which to interpret keycodes and LEDs. */
300 /** A comma seperated list of layouts (languages) to include in the
303 /** A comma seperated list of variants, one per layout, which may
304 * modify or augment the respective layout in various ways. */
306 /** A comma seprated list of options, through which the user specifies
307 * non-layout related preferences, like which key combinations are used
308 * for switching layouts, or which key is the Compose key. */
313 * @defgroup keysyms Keysyms
314 * Utility functions related to keysyms.
320 * Get the name of a keysym.
322 * For a description of how keysyms are named, see @ref xkb_keysym_t.
324 * @param[in] keysym The keysym.
325 * @param[out] buffer A string buffer to write the name into.
326 * @param[in] size Size of the buffer.
328 * @warning If the buffer passed is too small, the string is truncated
329 * (though still NUL-terminated); a size of at least 64 bytes is recommended.
331 * @returns The number of bytes in the name, excluding the NUL byte. If
332 * the keysym is invalid, returns -1.
334 * You may check if truncation has occured by comparing the return value
335 * with the length of buffer, similarly to the snprintf(3) function.
340 xkb_keysym_get_name(xkb_keysym_t keysym, char *buffer, size_t size);
342 /** Flags for xkb_keysym_from_name(). */
343 enum xkb_keysym_flags {
344 /** Find keysym by case-insensitive search. */
345 XKB_KEYSYM_CASE_INSENSITIVE = (1 << 0)
349 * Get a keysym from its name.
351 * @param name The name of a keysym. See remarks in xkb_keysym_get_name();
352 * this function will accept any name returned by that function.
353 * @param flags A set of flags controlling how the search is done. If
354 * invalid flags are passed, this will fail with XKB_KEY_NoSymbol.
356 * If you use the XKB_KEYSYM_CASE_INSENSITIVE flag and two keysym names
357 * differ only by case, then the lower-case keysym is returned. For
358 * instance, for KEY_a and KEY_A, this function would return KEY_a for the
359 * case-insensitive search. If this functionality is needed, it is
360 * recommended to first call this function without this flag; and if that
361 * fails, only then to try with this flag, while possibly warning the user
362 * he had misspelled the name, and might get wrong results.
364 * @returns The keysym. If the name is invalid, returns XKB_KEY_NoSymbol.
369 xkb_keysym_from_name(const char *name, enum xkb_keysym_flags flags);
372 * Get the Unicode/UTF-8 representation of a keysym.
374 * @param[in] keysym The keysym.
375 * @param[out] buffer A buffer to write the UTF-8 string into.
376 * @param[in] size The size of buffer. Must be at least 7.
378 * @returns The number of bytes written to the buffer (including the
379 * terminating byte). If the keysym does not have a Unicode
380 * representation, returns 0. If the buffer is too small, returns -1.
383 xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size);
386 * Get the Unicode/UTF-32 representation of a keysym.
388 * @returns The Unicode/UTF-32 representation of keysym, which is also
389 * compatible with UCS-4. If the keysym does not have a Unicode
390 * representation, returns 0.
393 xkb_keysym_to_utf32(xkb_keysym_t keysym);
398 * @defgroup context Library Context
399 * Creating, destroying and using library contexts.
401 * Every keymap compilation request must have a context associated with
402 * it. The context keeps around state such as the include path.
407 /** Flags for context creation. */
408 enum xkb_context_flags {
409 /** Create this context with an empty include path. */
410 XKB_CONTEXT_NO_DEFAULT_INCLUDES = (1 << 0)
414 * Create a new context.
416 * @param flags Optional flags for the context, or 0.
418 * @returns A new context, or NULL on failure.
420 * The user may set some environment variables to affect default values in
421 * the context. See e.g. xkb_context_set_log_level() and
422 * xkb_context_set_log_verbosity().
424 * @memberof xkb_context
427 xkb_context_new(enum xkb_context_flags flags);
430 * Take a new reference on a context.
432 * @returns The passed in context.
434 * @memberof xkb_context
437 xkb_context_ref(struct xkb_context *context);
440 * Release a reference on a context, and possibly free it.
442 * @param context The context. If it is NULL, this function does nothing.
444 * @memberof xkb_context
447 xkb_context_unref(struct xkb_context *context);
450 * Store custom user data in the context.
452 * This may be useful in conjuction with xkb_context_set_log_fn() or other
455 * @memberof xkb_context
458 xkb_context_set_user_data(struct xkb_context *context, void *user_data);
461 * Retrieves stored user data from the context.
463 * @returns The stored user data. If the user data wasn't set, or the
464 * passed in context is NULL, returns NULL.
466 * This may be useful to access private user data from callbacks like a
467 * custom logging function.
469 * @memberof xkb_context
472 xkb_context_get_user_data(struct xkb_context *context);
477 * @defgroup include-path Include Paths
478 * Manipulating the include paths in a context.
480 * The include paths are the file-system paths that are searched when an
481 * include statement is encountered during keymap compilation.
482 * In most cases, the default include paths are sufficient.
488 * Append a new entry to the context's include path.
490 * @returns 1 on success, or 0 if the include path could not be added or is
493 * @memberof xkb_context
496 xkb_context_include_path_append(struct xkb_context *context, const char *path);
499 * Append the default include paths to the context's include path.
501 * @returns 1 on success, or 0 if the primary include path could not be added.
503 * @memberof xkb_context
506 xkb_context_include_path_append_default(struct xkb_context *context);
509 * Reset the context's include path to the default.
511 * Removes all entries from the context's include path, and inserts the
514 * @returns 1 on success, or 0 if the primary include path could not be added.
516 * @memberof xkb_context
519 xkb_context_include_path_reset_defaults(struct xkb_context *context);
522 * Remove all entries from the context's include path.
524 * @memberof xkb_context
527 xkb_context_include_path_clear(struct xkb_context *context);
530 * Get the number of paths in the context's include path.
532 * @memberof xkb_context
535 xkb_context_num_include_paths(struct xkb_context *context);
538 * Get a specific include path from the context's include path.
540 * @returns The include path at the specified index. If the index is
541 * invalid, returns NULL.
543 * @memberof xkb_context
546 xkb_context_include_path_get(struct xkb_context *context, unsigned int index);
551 * @defgroup logging Logging Handling
552 * Manipulating how logging from this library is handled.
557 /** Specifies a logging level. */
559 XKB_LOG_LEVEL_CRITICAL = 10, /**< Log critical internal errors only. */
560 XKB_LOG_LEVEL_ERROR = 20, /**< Log all errors. */
561 XKB_LOG_LEVEL_WARNING = 30, /**< Log warnings and errors. */
562 XKB_LOG_LEVEL_INFO = 40, /**< Log information, warnings, and errors. */
563 XKB_LOG_LEVEL_DEBUG = 50 /**< Log everything. */
567 * Set the current logging level.
569 * @param context The context in which to set the logging level.
570 * @param level The logging level to use. Only messages from this level
571 * and below will be logged.
573 * The default level is XKB_LOG_LEVEL_ERROR. The environment variable
574 * XKB_LOG_LEVEL, if set in the time the context was created, overrides the
575 * default value. It may be specified as a level number or name.
577 * @memberof xkb_context
580 xkb_context_set_log_level(struct xkb_context *context,
581 enum xkb_log_level level);
584 * Get the current logging level.
586 * @memberof xkb_context
589 xkb_context_get_log_level(struct xkb_context *context);
592 * Sets the current logging verbosity.
594 * The library can generate a number of warnings which are not helpful to
595 * ordinary users of the library. The verbosity may be increased if more
596 * information is desired (e.g. when developing a new keymap).
598 * The default verbosity is 0. The environment variable XKB_LOG_VERBOSITY,
599 * if set in the time the context was created, overrides the default value.
601 * @param context The context in which to use the set verbosity.
602 * @param verbosity The verbosity to use. Currently used values are
603 * 1 to 10, higher values being more verbose. 0 would result in no verbose
604 * messages being logged.
606 * Most verbose messages are of level XKB_LOG_LEVEL_WARNING or lower.
608 * @memberof xkb_context
611 xkb_context_set_log_verbosity(struct xkb_context *context, int verbosity);
614 * Get the current logging verbosity of the context.
616 * @memberof xkb_context
619 xkb_context_get_log_verbosity(struct xkb_context *context);
622 * Set a custom function to handle logging messages.
624 * @param context The context in which to use the set logging function.
625 * @param log_fn The function that will be called for logging messages.
626 * Passing NULL restores the default function, which logs to stderr.
628 * By default, log messages from this library are printed to stderr. This
629 * function allows you to replace the default behavior with a custom
630 * handler. The handler is only called with messages which match the
631 * current logging level and verbosity settings for the context.
632 * level is the logging level of the message. format and args are the
633 * same as in the vprintf(3) function.
635 * You may use xkb_context_set_user_data() on the context, and then call
636 * xkb_context_get_user_data() from within the logging function to provide
637 * it with additional private context.
639 * @memberof xkb_context
642 xkb_context_set_log_fn(struct xkb_context *context,
643 void (*log_fn)(struct xkb_context *context,
644 enum xkb_log_level level,
645 const char *format, va_list args));
650 * @defgroup keymap Keymap Creation
651 * Creating and destroying keymaps.
656 /** Flags for keymap compilation. */
657 enum xkb_keymap_compile_flags {
658 /** Apparently you can't have empty enums. What a drag. */
659 XKB_MAP_COMPILE_PLACEHOLDER = 0
663 * Create a keymap from RMLVO names.
665 * The primary keymap entry point: creates a new XKB keymap from a set of
666 * RMLVO (Rules + Model + Layouts + Variants + Options) names.
668 * You should almost certainly be using this and nothing else to create
671 * @param context The context in which to create the keymap.
672 * @param names The RMLVO names to use.
673 * @param flags Optional flags for the keymap, or 0.
675 * @returns A keymap compiled according to the RMLVO names, or NULL if
676 * the compilation failed.
679 * @memberof xkb_keymap
682 xkb_keymap_new_from_names(struct xkb_context *context,
683 const struct xkb_rule_names *names,
684 enum xkb_keymap_compile_flags flags);
686 /** The possible keymap text formats. */
687 enum xkb_keymap_format {
688 /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
689 XKB_KEYMAP_FORMAT_TEXT_V1 = 1
693 * Create a keymap from a keymap file.
695 * @param context The context in which to create the keymap.
696 * @param file The keymap file to compile.
697 * @param format The text format of the keymap file to compile.
698 * @param flags Optional flags for the keymap, or 0.
700 * @returns A keymap compiled from the given XKB keymap file, or NULL if
701 * the compilation failed.
703 * The file must contain a complete keymap. For example, in the
704 * XKB_KEYMAP_FORMAT_TEXT_V1 format, this means the file must contain one
705 * top level '%xkb_keymap' section, which in turn contains other required
708 * @memberof xkb_keymap
711 xkb_keymap_new_from_file(struct xkb_context *context, FILE *file,
712 enum xkb_keymap_format format,
713 enum xkb_keymap_compile_flags flags);
716 * Create a keymap from a keymap string.
718 * This is just like xkb_keymap_new_from_file(), but instead of a file, gets
719 * the keymap as one enormous string.
721 * @see xkb_keymap_new_from_string()
722 * @memberof xkb_keymap
725 xkb_keymap_new_from_string(struct xkb_context *context, const char *string,
726 enum xkb_keymap_format format,
727 enum xkb_keymap_compile_flags flags);
730 * Take a new reference on a keymap.
732 * @returns The passed in keymap.
734 * @memberof xkb_keymap
737 xkb_keymap_ref(struct xkb_keymap *keymap);
740 * Release a reference on a keymap, and possibly free it.
742 * @param keymap The keymap. If it is NULL, this function does nothing.
744 * @memberof xkb_keymap
747 xkb_keymap_unref(struct xkb_keymap *keymap);
750 * Get the keymap as a string in the format from which it was created.
751 * @sa xkb_keymap_get_as_string()
753 #define XKB_KEYMAP_USE_ORIGINAL_FORMAT ((enum xkb_keymap_format) -1)
756 * Get the compiled keymap as a string.
758 * @param keymap The keymap to get as a string.
759 * @param format The keymap format to use for the string. You can pass
760 * in the special value XKB_KEYMAP_USE_ORIGINAL_FORMAT to use the format
761 * from which the keymap was originally created.
763 * @returns The keymap as a NUL-terminated string, or NULL if unsuccessful.
765 * The returned string may be fed back into xkb_map_new_from_string() to get
766 * the exact same keymap (possibly in another process, etc.).
768 * The returned string is dynamically allocated and should be freed by the
771 * @memberof xkb_keymap
774 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
775 enum xkb_keymap_format format);
780 * @defgroup components Keymap Components
781 * Enumeration of state components in a keymap.
787 * Get the number of modifiers in the keymap.
789 * @sa xkb_mod_index_t
790 * @memberof xkb_keymap
793 xkb_keymap_num_mods(struct xkb_keymap *keymap);
796 * Get the name of a modifier by index.
798 * @returns The name. If the index is invalid, returns NULL.
800 * @sa xkb_mod_index_t
801 * @memberof xkb_keymap
804 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx);
807 * Get the index of a modifier by name.
809 * @returns The index. If no modifier with this name exists, returns
812 * @sa xkb_mod_index_t
813 * @memberof xkb_keymap
816 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name);
819 * Get the number of layouts in the keymap.
821 * @sa xkb_layout_index_t xkb_rule_names xkb_keymap_num_layouts_for_key()
822 * @memberof xkb_keymap
825 xkb_keymap_num_layouts(struct xkb_keymap *keymap);
828 * Get the name of a layout by index.
830 * @returns The name. If the index is invalid, or the layout does not have
831 * a name, returns NULL.
833 * @sa xkb_layout_index_t
834 * @memberof xkb_keymap
837 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx);
840 * Get the index of a layout by name.
842 * @returns The index. If no layout exists with this name, returns
843 * XKB_LAYOUT_INVALID. If more than one layout in the keymap has this name,
844 * returns the lowest index among them.
846 * @memberof xkb_keymap
849 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name);
852 * Get the number of layouts for a specific key.
854 * This number can be different from xkb_keymap_num_layouts(), but is always
855 * smaller. It is the appropriate value to use when iterating over the
858 * @sa xkb_layout_index_t
859 * @memberof xkb_keymap
862 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t key);
865 * Get the number of shift levels for a specific key and layout.
867 * If @c layout is out of range for this key (that is, larger or equal to
868 * the value returned by xkb_keymap_num_layouts_for_key()), it is brought
869 * back into range in a manner consistent with xkb_state_key_get_layout().
871 * @sa xkb_level_index_t
872 * @memberof xkb_keymap
875 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t key,
876 xkb_layout_index_t layout);
879 * Get the keysyms obtained from pressing a key in a given layout and
882 * This function is like xkb_state_key_get_syms(), only the layout and
883 * shift level are not derived from the keyboard state but are instead
884 * specified explicitly.
886 * @param[in] keymap The keymap.
887 * @param[in] key The keycode of the key.
888 * @param[in] layout The layout for which to get the keysyms.
889 * @param[in] level The shift level in the layout for which to get the
890 * keysyms. This must be smaller than:
891 * @code xkb_keymap_num_layouts_for_key(keymap, key) @endcode
892 * @param[out] syms_out An immutible array of keysyms corresponding to the
893 * key in the given layout and shift level.
895 * If @c layout is out of range for this key (that is, larger or equal to
896 * the value returned by xkb_keymap_num_layouts_for_key()), it is brought
897 * back into range in a manner consistent with xkb_state_key_get_layout().
899 * @returns The number of keysyms in the syms_out array. If no keysyms
900 * are produced by the key in the given layout and shift level, returns 0
901 * and sets syms_out to NULL.
903 * @sa xkb_state_key_get_syms()
904 * @memberof xkb_keymap
907 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
909 xkb_layout_index_t layout,
910 xkb_level_index_t level,
911 const xkb_keysym_t **syms_out);
914 * Get the number of LEDs in the keymap.
916 * @warning The range [ 0...xkb_keymap_num_leds() ) includes all of the LEDs
917 * in the keymap, but may also contain inactive LEDs. When iterating over
918 * this range, you need the handle this case when calling functions such as
919 * xkb_keymap_led_get_name() or xkb_state_led_index_is_active().
921 * @sa xkb_led_index_t
922 * @memberof xkb_keymap
925 xkb_keymap_num_leds(struct xkb_keymap *keymap);
928 * Get the name of a LED by index.
930 * @returns The name. If the index is invalid, returns NULL.
932 * @memberof xkb_keymap
935 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx);
938 * Get the index of a LED by name.
940 * @returns The index. If no LED with this name exists, returns
943 * @memberof xkb_keymap
946 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name);
949 * Determine whether a key should repeat or not.
951 * A keymap may specify different repeat behaviors for different keys.
952 * Most keys should generally exhibit repeat behavior; for example, holding
953 * the 'a' key down in a text editor should normally insert a single 'a'
954 * character every few milliseconds, until the key is released. However,
955 * there are keys which should not or do not need to be repeated. For
956 * example, repeating modifier keys such as Left/Right Shift or Caps Lock
957 * is not generally useful or desired.
959 * @returns 1 if the key should repeat, 0 otherwise.
961 * @memberof xkb_keymap
964 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t key);
969 * @defgroup state Keyboard State
970 * Creating, destroying and manipulating keyboard state objects.
976 * Create a new keyboard state object for a keymap.
978 * @param keymap The keymap for which to create the state.
980 * @returns A new keyboard state object, or NULL on failure.
982 * @memberof xkb_state
985 xkb_state_new(struct xkb_keymap *keymap);
988 * Take a new reference on a keyboard state object.
990 * @returns The passed in object.
992 * @memberof xkb_state
995 xkb_state_ref(struct xkb_state *state);
998 * Release a reference on a keybaord state object, and possibly free it.
1000 * @param state The state. If it is NULL, this function does nothing.
1002 * @memberof xkb_state
1005 xkb_state_unref(struct xkb_state *state);
1008 * Get the keymap from which a keyboard state object was created.
1010 * @returns The keymap which was used in xkb_state_new() to create this
1013 * This function does not take a new reference on the keymap; you must
1014 * explicitly reference it yourself if you plan to use it beyond the
1015 * lifetime of the state.
1017 * @memberof xkb_state
1020 xkb_state_get_keymap(struct xkb_state *state);
1022 /** Specifies the direction of the key (press / release). */
1023 enum xkb_key_direction {
1024 XKB_KEY_UP, /**< The key was released. */
1025 XKB_KEY_DOWN /**< The key was pressed. */
1029 * Modifier and layout types for state objects. This enum is bitmaskable,
1030 * e.g. (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED) is valid to
1031 * exclude locked modifiers.
1033 * In XKB, the DEPRESSED components are also known as 'base'.
1035 enum xkb_state_component {
1036 /** Depressed modifiers, i.e. a key is physically holding them. */
1037 XKB_STATE_MODS_DEPRESSED = (1 << 0),
1038 /** Latched modifiers, i.e. will be unset after the next non-modifier
1040 XKB_STATE_MODS_LATCHED = (1 << 1),
1041 /** Locked modifiers, i.e. will be unset after the key provoking the
1042 * lock has been pressed again. */
1043 XKB_STATE_MODS_LOCKED = (1 << 2),
1044 /** Effective modifiers, i.e. currently active and affect key
1045 * processing (derived from the other state components). */
1046 XKB_STATE_MODS_EFFECTIVE = (1 << 3),
1047 /** Depressed layout, i.e. a key is physically holding it. */
1048 XKB_STATE_LAYOUT_DEPRESSED = (1 << 4),
1049 /** Latched layout, i.e. will be unset after the next non-modifier
1051 XKB_STATE_LAYOUT_LATCHED = (1 << 5),
1052 /** Locked layout, i.e. will be unset after the key provoking the lock
1053 * has been pressed again. */
1054 XKB_STATE_LAYOUT_LOCKED = (1 << 6),
1055 /** Effective layout, i.e. currently active and affects key processing
1056 * (derived from the other state components). */
1057 XKB_STATE_LAYOUT_EFFECTIVE = (1 << 7),
1058 /** LEDs (derived from the other state components). */
1059 XKB_STATE_LEDS = (1 << 8)
1063 * Update the keyboard state to reflect a given key being pressed or
1066 * @returns A mask of state components that have changed as a result of
1067 * the update. If nothing in the state has changed, returns 0.
1069 * @memberof xkb_state
1071 enum xkb_state_component
1072 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
1073 enum xkb_key_direction direction);
1076 * Get the keysyms obtained from pressing a particular key in a given
1079 * Get the keysyms for a key according to the current active layout,
1080 * modifiers and shift level for the key, as determined by a keyboard
1083 * @param[in] state The keyboard state object.
1084 * @param[in] key The keycode of the key.
1085 * @param[out] syms_out An immutible array of keysyms corresponding the
1086 * key in the given keyboard state.
1088 * As an extension to XKB, this function can return more than one keysym.
1089 * If you do not want to handle this case, you can use
1090 * xkb_state_key_get_one_sym().
1092 * @returns The number of keysyms in the syms_out array. If no keysyms
1093 * are produced by the key in the given keyboard state, returns 0 and sets
1096 * @memberof xkb_state
1099 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
1100 const xkb_keysym_t **syms_out);
1103 * Get the single keysym obtained from pressing a particular key in a
1104 * given keyboard state.
1106 * This function is similar to xkb_state_key_get_syms(), but with a
1107 * simplified interface for users which cannot or do not want to handle
1108 * the case where multiple keysyms are returned.
1110 * @returns The keysym. If the key does not have exactly one keysym,
1111 * returns XKB_KEY_NoSymbol
1113 * @sa xkb_state_key_get_syms()
1114 * @memberof xkb_state
1117 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t key);
1120 * Get the effective layout index for a key in a given keyboard state.
1122 * @returns The layout index for the key in the given keyboard state. If
1123 * the given keycode is invalid, or if the key is not included in any
1124 * layout at all, returns XKB_LAYOUT_INVALID.
1126 * @invariant If the returned layout is valid, the following always holds:
1128 * xkb_state_key_get_layout(state, key) < xkb_keymap_num_layouts_for_key(keymap, key)
1131 * @memberof xkb_state
1134 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t key);
1137 * Get the effective shift level for a key in a given keyboard state and
1140 * @param state The keyboard state.
1141 * @param key The keycode of the key.
1142 * @param layout The layout for which to get the shift level. This must be
1144 * @code xkb_keymap_num_layouts_for_key(keymap, key) @endcode
1145 * usually it would be:
1146 * @code xkb_state_key_get_layout(state, key) @endcode
1148 * @return The shift level index. If the key or layout are invalid,
1149 * returns XKB_LEVEL_INVALID.
1151 * @invariant If the returned level is valid, the following always holds:
1153 * xkb_state_key_get_level(state, key, layout) < xkb_keymap_num_levels_for_key(keymap, key, layout)
1156 * @memberof xkb_state
1159 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t key,
1160 xkb_layout_index_t layout);
1163 * Match flags for xkb_state_mod_indices_are_active and
1164 * xkb_state_mod_names_are_active, specifying how the conditions for a
1165 * successful match. XKB_STATE_MATCH_NON_EXCLUSIVE is bitmaskable with
1168 enum xkb_state_match {
1169 /** Returns true if any of the modifiers are active. */
1170 XKB_STATE_MATCH_ANY = (1 << 0),
1171 /** Returns true if all of the modifiers are active. */
1172 XKB_STATE_MATCH_ALL = (1 << 1),
1173 /** Makes matching non-exclusive, i.e. will not return false if a
1174 * modifier not specified in the arguments is active. */
1175 XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16)
1179 * Update a keyboard state from a set of explicit masks.
1181 * This entrypoint is really only for window systems and the like, where a
1182 * master process holds an xkb_state, then serializes it over a wire
1183 * protocol, and clients then use the serialization to feed in to their own
1186 * All parameters must always be passed, or the resulting state may be
1189 * The serialization is lossy and will not survive round trips; it must only
1190 * be used to feed slave state objects, and must not be used to update the
1193 * Please do not use this unless you fit the description above.
1195 * @returns A mask of state components that have changed as a result of
1196 * the update. If nothing in the state has changed, returns 0.
1198 * @memberof xkb_state
1200 enum xkb_state_component
1201 xkb_state_update_mask(struct xkb_state *state,
1202 xkb_mod_mask_t depressed_mods,
1203 xkb_mod_mask_t latched_mods,
1204 xkb_mod_mask_t locked_mods,
1205 xkb_layout_index_t depressed_layout,
1206 xkb_layout_index_t latched_layout,
1207 xkb_layout_index_t locked_layout);
1210 * The counterpart to xkb_state_update_mask for modifiers, to be used on
1211 * the server side of serialization.
1213 * @param state The keyboard state.
1214 * @param components A mask of the modifier state components to serialize.
1215 * State components other than XKB_STATE_MODS_* are ignored.
1216 * If XKB_STATE_MODS_EFFECTIVE is included, all other state components are
1219 * @returns A xkb_mod_mask_t representing the given components of the
1222 * This function should not be used in regular clients; please use the
1223 * xkb_state_mod_*_is_active API instead.
1225 * @memberof xkb_state
1228 xkb_state_serialize_mods(struct xkb_state *state,
1229 enum xkb_state_component components);
1232 * The counterpart to xkb_state_update_mask for layouts, to be used on
1233 * the server side of serialization.
1235 * @param state The keyboard state.
1236 * @param components A mask of the layout state components to serialize.
1237 * State components other than XKB_STATE_LAYOUT_* are ignored.
1238 * If XKB_STATE_LAYOUT_EFFECTIVE is included, all other state components are
1241 * @returns A layout index representing the given components of the
1244 * This function should not be used in regular clients; please use the
1245 * xkb_state_layout_*_is_active API instead.
1247 * @memberof xkb_state
1250 xkb_state_serialize_layout(struct xkb_state *state,
1251 enum xkb_state_component components);
1254 * Test whether a modifier is active in a given keyboard state by name.
1256 * @returns 1 if the modifier is active, 0 if it is not. If the modifier
1257 * name does not exist in the keymap, returns -1.
1259 * @memberof xkb_state
1262 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1263 enum xkb_state_component type);
1266 * Test whether a set of modifiers are active in a given keyboard state by
1269 * @param state The keyboard state.
1270 * @param type The component of the state against which to match the
1272 * @param match The manner by which to match the state against the
1274 * @param ... The set of of modifier names to test, terminated by a NULL
1275 * argument (sentinel).
1277 * @returns 1 if the modifiers are active, 0 if they are not. If any of
1278 * the modifier names do not exist in the keymap, returns -1.
1280 * @memberof xkb_state
1283 xkb_state_mod_names_are_active(struct xkb_state *state,
1284 enum xkb_state_component type,
1285 enum xkb_state_match match,
1289 * Test whether a modifier is active in a given keyboard state by index.
1291 * @returns 1 if the modifier is active, 0 if it is not. If the modifier
1292 * index is invalid in the keymap, returns -1.
1294 * @memberof xkb_state
1297 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
1298 enum xkb_state_component type);
1301 * Test whether a set of modifiers are active in a given keyboard state by
1304 * @param state The keyboard state.
1305 * @param type The component of the state against which to match the
1307 * @param match The manner by which to match the state against the
1309 * @param ... The set of of modifier indexes to test, terminated by a
1310 * XKB_MOD_INVALID argument (sentinel).
1312 * @returns 1 if the modifiers are active, 0 if they are not. If any of
1313 * the modifier indexes are invalid in the keymap, returns -1.
1315 * @memberof xkb_state
1318 xkb_state_mod_indices_are_active(struct xkb_state *state,
1319 enum xkb_state_component type,
1320 enum xkb_state_match match,
1324 * Test whether a modifier is consumed by keyboard state translation for
1327 * Some functions, like xkb_state_key_get_syms(), look at the state of
1328 * the modifiers in the keymap and derive from it the correct shift level
1329 * to use for the key. For example, in a US layout, pressing the key
1330 * labeled \<A\> while the Shift modifier is active, generates the keysym 'A'.
1331 * In this case, the Shift modifier is said to be consumed. However, the
1332 * Num Lock modifier does not affect this translation at all, even if it
1333 * active, so it is not consumed by this translation.
1335 * It may be desireable for some application to not reuse consumed modifiers
1336 * for further processing, e.g. for hotkeys or keyboard shortcuts. To
1337 * understand why, consider some requirements from a standard shortcut
1338 * mechanism, and how they are implemented:
1340 * 1. The shortcut's modifiers must match exactly to the state. For example,
1341 * it is possible to bind separate actions to \<Alt\>\<Tab\> and to
1342 * \<Alt\>\<Shift\>\<Tab\>. Further, if only \<Alt\>\<Tab\> is bound to
1343 * an action, pressing \<Alt\>\<Shift\>\<Tab\> should not trigger the
1345 * Effectively, this means that the modifiers are compared using the
1346 * equality operator (==).
1347 * 2. Only relevant modifiers are considered for the matching. For example,
1348 * Caps Lock and Num Lock should not generally affect the matching, e.g.
1349 * when matching \<Alt\>\<Tab\> against the state, it does not matter
1350 * whether Num Lock is active or not. These relevant, or significant,
1351 * modifiers usually include Alt, Control, Shift, Super and similar.
1352 * Effectively, this means that non-significant modifiers are masked out,
1353 * before doing the comparison as described above.
1354 * 3. The matching must be independent of the layout/keymap. For example,
1355 * the \<Plus\> (+) symbol is found on the first level on some layouts,
1356 * and requires holding Shift on others. If you simply bind the action
1357 * to the \<Plus\> keysym, it would work for the unshifted kind, but
1358 * not for the others, because the match against Shift would fail. If
1359 * you bind the action to \<Shift\>\<Plus\>, only the shifted kind would
1360 * work. So what is needed is to recognize that Shift is used up in the
1361 * translation of the keysym itself, and therefore should not be included
1363 * Effectively, this means that consumed modifiers (Shift in this example)
1364 * are masked out as well, before doing the comparison.
1366 * To summarize, this is how the matching would be performed:
1368 * (keysym == shortcut_keysym) &&
1369 * ((state_modifiers & ~consumed_modifiers & significant_modifiers) == shortcut_modifiers)
1372 * @c state_modifiers are the modifiers reported by
1373 * xkb_state_mod_index_is_active() and similar functions.
1374 * @c consumed_modifiers are the modifiers reported by
1375 * xkb_state_mod_index_is_consumed().
1376 * @c significant_modifiers are decided upon by the application/toolkit/user;
1377 * it is up to them to decide whether these are configurable or hard-coded.
1379 * @returns 1 if the modifier is consumed, 0 if it is not. If the modifier
1380 * index is not valid in the keymap, returns -1.
1382 * @sa xkb_state_mod_mask_remove_consumed()
1383 * @memberof xkb_state
1386 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t key,
1387 xkb_mod_index_t idx);
1390 * Remove consumed modifiers from a modifier mask for a key.
1392 * Takes the given modifier mask, and removes all modifiers which are
1393 * consumed for that particular key (as in xkb_state_mod_index_is_consumed()).
1395 * @sa xkb_state_mod_index_is_consumed()
1396 * @memberof xkb_state
1399 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t key,
1400 xkb_mod_mask_t mask);
1403 * Test whether a layout is active in a given keyboard state by name.
1405 * @returns 1 if the layout is active, 0 if it is not. If no layout with
1406 * this name exists in the keymap, return -1.
1408 * If multiple layouts in the keymap have this name, the one with the lowest
1411 * @sa xkb_layout_index_t
1412 * @memberof xkb_state
1415 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1416 enum xkb_state_component type);
1419 * Test whether a layout is active in a given keyboard state by index.
1421 * @returns 1 if the layout is active, 0 if it is not. If the layout index
1422 * is not valid in the keymap, returns -1.
1424 * @sa xkb_layout_index_t
1425 * @memberof xkb_state
1428 xkb_state_layout_index_is_active(struct xkb_state *state,
1429 xkb_layout_index_t idx,
1430 enum xkb_state_component type);
1433 * Test whether a LED is active in a given keyboard state by name.
1435 * @returns 1 if the LED is active, 0 if it not. If no LED with this name
1436 * exists in the keymap, returns -1.
1438 * @sa xkb_led_index_t
1439 * @memberof xkb_state
1442 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
1445 * Test whether a LED is active in a given keyboard state by index.
1447 * @returns 1 if the LED is active, 0 if it not. If the LED index is not
1448 * valid in the keymap, returns -1.
1450 * @sa xkb_led_index_t
1451 * @memberof xkb_state
1454 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
1458 /* Leave this include last, so it can pick up our types, etc. */
1459 #include <xkbcommon/xkbcommon-compat.h>
1465 #endif /* _XKBCOMMON_H_ */