tools: move the remaining tools from test to here
[platform/upstream/libxkbcommon.git] / xkbcommon / xkbcommon.h
1 /*
2  * Copyright 1985, 1987, 1990, 1998  The Open Group
3  * Copyright 2008  Dan Nicholson
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
21  *
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.
26  */
27
28 /************************************************************
29  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
30  *
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.
42  *
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.
51  *
52  ********************************************************/
53
54 /*
55  * Copyright © 2009-2012 Daniel Stone
56  * Copyright © 2012 Intel Corporation
57  * Copyright © 2012 Ran Benita
58  *
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:
65  *
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
68  * Software.
69  *
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.
77  *
78  * Author: Daniel Stone <daniel@fooishbar.org>
79  */
80
81 #ifndef _XKBCOMMON_H_
82 #define _XKBCOMMON_H_
83
84 #include <stdint.h>
85 #include <stdio.h>
86 #include <stdarg.h>
87
88 #include <xkbcommon/xkbcommon-names.h>
89 #include <xkbcommon/xkbcommon-keysyms.h>
90
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94
95 /**
96  * @file
97  * Main libxkbcommon API.
98  */
99
100 /**
101  * @struct xkb_context
102  * Opaque top level library context object.
103  *
104  * The context contains various general library data and state, like
105  * logging level and include paths.
106  *
107  * Objects are created in a specific context, and multiple contexts may
108  * coexist simultaneously.  Objects from different contexts are completely
109  * separated and do not share any memory or state.
110  */
111 struct xkb_context;
112
113 /**
114  * @struct xkb_keymap
115  * Opaque compiled keymap object.
116  *
117  * The keymap object holds all of the static keyboard information obtained
118  * from compiling XKB files.
119  *
120  * A keymap is immutable after it is created (besides reference counts, etc.);
121  * if you need to change it, you must create a new one.
122  */
123 struct xkb_keymap;
124
125 /**
126  * @struct xkb_state
127  * Opaque keyboard state object.
128  *
129  * State objects contain the active state of a keyboard (or keyboards), such
130  * as the currently effective layout and the active modifiers.  It acts as a
131  * simple state machine, wherein key presses and releases are the input, and
132  * key symbols (keysyms) are the output.
133  */
134 struct xkb_state;
135
136 /**
137  * A number used to represent a physical key on a keyboard.
138  *
139  * A standard PC-compatible keyboard might have 102 keys.  An appropriate
140  * keymap would assign each of them a keycode, by which the user should
141  * refer to the key throughout the library.
142  *
143  * Historically, the X11 protocol, and consequentially the XKB protocol,
144  * assign only 8 bits for keycodes.  This limits the number of different
145  * keys that can be used simultaneously in a single keymap to 256
146  * (disregarding other limitations).  This library does not share this limit;
147  * keycodes beyond 255 ('extended keycodes') are not treated specially.
148  * Keymaps and applications which are compatible with X11 should not use
149  * these keycodes.
150  *
151  * The values of specific keycodes are determined by the keymap and the
152  * underlying input system.  For example, with an X11-compatible keymap
153  * and Linux evdev scan codes (see linux/input.h), a fixed offset is used:
154  *
155  * The keymap defines a canonical name for each key, plus possible aliases.
156  * Historically, the XKB protocol restricts these names to at most 4 (ASCII)
157  * characters, but this library does not share this limit.
158  *
159  * @code
160  * xkb_keycode_t keycode_A = KEY_A + 8;
161  * @endcode
162  *
163  * @sa xkb_keycode_is_legal_ext() xkb_keycode_is_legal_x11()
164  */
165 typedef uint32_t xkb_keycode_t;
166
167 /**
168  * A number used to represent the symbols generated from a key on a keyboard.
169  *
170  * A key, represented by a keycode, may generate different symbols according
171  * to keyboard state.  For example, on a QWERTY keyboard, pressing the key
172  * labled \<A\> generates the symbol 'a'.  If the Shift key is held, it
173  * generates the symbol 'A'.  If a different layout is used, say Greek,
174  * it generates the symbol 'α'.  And so on.
175  *
176  * Each such symbol is represented by a keysym.  Note that keysyms are
177  * somewhat more general, in that they can also represent some "function",
178  * such as "Left" or "Right" for the arrow keys.  For more information,
179  * see:
180  * https://www.x.org/releases/current/doc/xproto/x11protocol.html#keysym_encoding
181  *
182  * Specifically named keysyms can be found in the
183  * xkbcommon/xkbcommon-keysyms.h header file.  Their name does not include
184  * the XKB_KEY_ prefix.
185  *
186  * Besides those, any Unicode/ISO 10646 character in the range U0100 to
187  * U10FFFF can be represented by a keysym value in the range 0x01000100 to
188  * 0x0110FFFF.  The name of Unicode keysyms is "U<codepoint>", e.g. "UA1B2".
189  *
190  * The name of other unnamed keysyms is the hexadecimal representation of
191  * their value, e.g. "0xabcd1234".
192  *
193  * Keysym names are case-sensitive.
194  */
195 typedef uint32_t xkb_keysym_t;
196
197 /**
198  * Index of a keyboard layout.
199  *
200  * The layout index is a state component which detemines which <em>keyboard
201  * layout</em> is active.  These may be different alphabets, different key
202  * arrangements, etc.
203  *
204  * Layout indices are consecutive.  The first layout has index 0.
205  *
206  * Each layout is not required to have a name, and the names are not
207  * guaranteed to be unique (though they are usually provided and unique).
208  * Therefore, it is not safe to use the name as a unique identifier for a
209  * layout.  Layout names are case-sensitive.
210  *
211  * Layouts are also called "groups" by XKB.
212  *
213  * @sa xkb_keymap_num_layouts() xkb_keymap_num_layouts_for_key()
214  */
215 typedef uint32_t xkb_layout_index_t;
216 /** A mask of layout indices. */
217 typedef uint32_t xkb_layout_mask_t;
218
219 /**
220  * Index of a shift level.
221  *
222  * Any key, in any layout, can have several <em>shift levels</em>.  Each
223  * shift level can assign different keysyms to the key.  The shift level
224  * to use is chosen according to the current keyboard state; for example,
225  * if no keys are pressed, the first level may be used; if the Left Shift
226  * key is pressed, the second; if Num Lock is pressed, the third; and
227  * many such combinations are possible (see xkb_mod_index_t).
228  *
229  * Level indices are consecutive.  The first level has index 0.
230  */
231 typedef uint32_t xkb_level_index_t;
232
233 /**
234  * Index of a modifier.
235  *
236  * A @e modifier is a state component which changes the way keys are
237  * interpreted.  A keymap defines a set of modifiers, such as Alt, Shift,
238  * Num Lock or Meta, and specifies which keys may @e activate which
239  * modifiers (in a many-to-many relationship, i.e. a key can activate
240  * several modifiers, and a modifier may be activated by several keys.
241  * Different keymaps do this differently).
242  *
243  * When retrieving the keysyms for a key, the active modifier set is
244  * consulted; this detemines the correct shift level to use within the
245  * currently active layout (see xkb_level_index_t).
246  *
247  * Modifier indices are consecutive.  The first modifier has index 0.
248  *
249  * Each modifier must have a name, and the names are unique.  Therefore, it
250  * is safe to use the name as a unique identifier for a modifier.  The names
251  * of some common modifiers are provided in the xkbcommon/xkbcommon-names.h
252  * header file.  Modifier names are case-sensitive.
253  *
254  * @sa xkb_keymap_num_mods()
255  */
256 typedef uint32_t xkb_mod_index_t;
257 /** A mask of modifier indices. */
258 typedef uint32_t xkb_mod_mask_t;
259
260 /**
261  * Index of a keyboard LED.
262  *
263  * LEDs are logical objects which may be @e active or @e inactive.  They
264  * typically correspond to the lights on the keyboard. Their state is
265  * determined by the current keyboard state.
266  *
267  * LED indices are non-consecutive.  The first LED has index 0.
268  *
269  * Each LED must have a name, and the names are unique. Therefore,
270  * it is safe to use the name as a unique identifier for a LED.  The names
271  * of some common LEDs are provided in the xkbcommon/xkbcommon-names.h
272  * header file.  LED names are case-sensitive.
273  *
274  * @warning A given keymap may specify an exact index for a given LED.
275  * Therefore, LED indexing is not necessarily sequential, as opposed to
276  * modifiers and layouts.  This means that when iterating over the LEDs
277  * in a keymap using e.g. xkb_keymap_num_leds(), some indices might be
278  * invalid.  Given such an index, functions like xkb_keymap_led_get_name()
279  * will return NULL, and xkb_state_led_index_is_active() will return -1.
280  *
281  * LEDs are also called "indicators" by XKB.
282  *
283  * @sa xkb_keymap_num_leds()
284  */
285 typedef uint32_t xkb_led_index_t;
286 /** A mask of LED indices. */
287 typedef uint32_t xkb_led_mask_t;
288
289 #define XKB_KEYCODE_INVALID (0xffffffff)
290 #define XKB_LAYOUT_INVALID  (0xffffffff)
291 #define XKB_LEVEL_INVALID   (0xffffffff)
292 #define XKB_MOD_INVALID     (0xffffffff)
293 #define XKB_LED_INVALID     (0xffffffff)
294
295 #define XKB_KEYCODE_MAX     (0xffffffff - 1)
296
297 /**
298  * Test whether a value is a valid extended keycode.
299  * @sa xkb_keycode_t
300  **/
301 #define xkb_keycode_is_legal_ext(key) (key <= XKB_KEYCODE_MAX)
302
303 /**
304  * Test whether a value is a valid X11 keycode.
305  * @sa xkb_keycode_t
306  */
307 #define xkb_keycode_is_legal_x11(key) (key >= 8 && key <= 255)
308
309 /**
310  * Names to compile a keymap with, also known as RMLVO.
311  *
312  * The names are the common configuration values by which a user picks
313  * a keymap.
314  *
315  * If the entire struct is NULL, then each field is taken to be NULL.
316  * You should prefer passing NULL instead of choosing your own defaults.
317  */
318 struct xkb_rule_names {
319     /**
320      * The rules file to use. The rules file describes how to interpret
321      * the values of the model, layout, variant and options fields.
322      *
323      * If NULL or the empty string "", a default value is used.
324      * If the XKB_DEFAULT_RULES environment variable is set, it is used
325      * as the default.  Otherwise the system default is used.
326      */
327     const char *rules;
328     /**
329      * The keyboard model by which to interpret keycodes and LEDs.
330      *
331      * If NULL or the empty string "", a default value is used.
332      * If the XKB_DEFAULT_MODEL environment variable is set, it is used
333      * as the default.  Otherwise the system default is used.
334      */
335     const char *model;
336     /**
337      * A comma separated list of layouts (languages) to include in the
338      * keymap.
339      *
340      * If NULL or the empty string "", a default value is used.
341      * If the XKB_DEFAULT_LAYOUT environment variable is set, it is used
342      * as the default.  Otherwise the system default is used.
343      */
344     const char *layout;
345     /**
346      * A comma separated list of variants, one per layout, which may
347      * modify or augment the respective layout in various ways.
348      *
349      * If NULL or the empty string "", and a default value is also used
350      * for the layout, a default value is used.  Otherwise no variant is
351      * used.
352      * If the XKB_DEFAULT_VARIANT environment variable is set, it is used
353      * as the default.  Otherwise the system default is used.
354      */
355     const char *variant;
356     /**
357      * A comma separated list of options, through which the user specifies
358      * non-layout related preferences, like which key combinations are used
359      * for switching layouts, or which key is the Compose key.
360      *
361      * If NULL, a default value is used.  If the empty string "", no
362      * options are used.
363      * If the XKB_DEFAULT_OPTIONS environment variable is set, it is used
364      * as the default.  Otherwise the system default is used.
365      */
366     const char *options;
367 };
368
369 /**
370  * @defgroup keysyms Keysyms
371  * Utility functions related to keysyms.
372  *
373  * @{
374  */
375
376 /**
377  * @page keysym-transformations Keysym Transformations
378  *
379  * Keysym translation is subject to several "keysym transformations",
380  * as described in the XKB specification.  These are:
381  *
382  * - Capitalization transformation.  If the Caps Lock modifier is
383  *   active and was not consumed by the translation process, a single
384  *   keysym is transformed to its upper-case form (if applicable).
385  *   Similarly, the UTF-8/UTF-32 string produced is capitalized.
386  *
387  *   This is described in:
388  *   https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
389  *
390  * - Control transformation.  If the Control modifier is active and
391  *   was not consumed by the translation process, the string produced
392  *   is transformed to its matching ASCII control character (if
393  *   applicable).  Keysyms are not affected.
394  *
395  *   This is described in:
396  *   https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
397  *
398  * Each relevant function discusses which transformations it performs.
399  *
400  * These transformations are not applicable when a key produces multiple
401  * keysyms.
402  */
403
404
405 /**
406  * Get the name of a keysym.
407  *
408  * For a description of how keysyms are named, see @ref xkb_keysym_t.
409  *
410  * @param[in]  keysym The keysym.
411  * @param[out] buffer A string buffer to write the name into.
412  * @param[in]  size   Size of the buffer.
413  *
414  * @warning If the buffer passed is too small, the string is truncated
415  * (though still NUL-terminated); a size of at least 64 bytes is recommended.
416  *
417  * @returns The number of bytes in the name, excluding the NUL byte. If
418  * the keysym is invalid, returns -1.
419  *
420  * You may check if truncation has occurred by comparing the return value
421  * with the length of buffer, similarly to the snprintf(3) function.
422  *
423  * @sa xkb_keysym_t
424  */
425 int
426 xkb_keysym_get_name(xkb_keysym_t keysym, char *buffer, size_t size);
427
428 /** Flags for xkb_keysym_from_name(). */
429 enum xkb_keysym_flags {
430     /** Do not apply any flags. */
431     XKB_KEYSYM_NO_FLAGS = 0,
432     /** Find keysym by case-insensitive search. */
433     XKB_KEYSYM_CASE_INSENSITIVE = (1 << 0)
434 };
435
436 /**
437  * Get a keysym from its name.
438  *
439  * @param name The name of a keysym. See remarks in xkb_keysym_get_name();
440  * this function will accept any name returned by that function.
441  * @param flags A set of flags controlling how the search is done. If
442  * invalid flags are passed, this will fail with XKB_KEY_NoSymbol.
443  *
444  * If you use the XKB_KEYSYM_CASE_INSENSITIVE flag and two keysym names
445  * differ only by case, then the lower-case keysym is returned.  For
446  * instance, for KEY_a and KEY_A, this function would return KEY_a for the
447  * case-insensitive search.  If this functionality is needed, it is
448  * recommended to first call this function without this flag; and if that
449  * fails, only then to try with this flag, while possibly warning the user
450  * he had misspelled the name, and might get wrong results.
451  *
452  * Case folding is done according to the C locale; the current locale is not
453  * consulted.
454  *
455  * @returns The keysym. If the name is invalid, returns XKB_KEY_NoSymbol.
456  *
457  * @sa xkb_keysym_t
458  */
459 xkb_keysym_t
460 xkb_keysym_from_name(const char *name, enum xkb_keysym_flags flags);
461
462 /**
463  * Get the Unicode/UTF-8 representation of a keysym.
464  *
465  * @param[in]  keysym The keysym.
466  * @param[out] buffer A buffer to write the UTF-8 string into.
467  * @param[in]  size   The size of buffer.  Must be at least 7.
468  *
469  * @returns The number of bytes written to the buffer (including the
470  * terminating byte).  If the keysym does not have a Unicode
471  * representation, returns 0.  If the buffer is too small, returns -1.
472  *
473  * This function does not perform any @ref keysym-transformations.
474  * Therefore, prefer to use xkb_state_key_get_utf8() if possible.
475  *
476  * @sa xkb_state_key_get_utf8()
477  */
478 int
479 xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size);
480
481 /**
482  * Get the Unicode/UTF-32 representation of a keysym.
483  *
484  * @returns The Unicode/UTF-32 representation of keysym, which is also
485  * compatible with UCS-4.  If the keysym does not have a Unicode
486  * representation, returns 0.
487  *
488  * This function does not perform any @ref keysym-transformations.
489  * Therefore, prefer to use xkb_state_key_get_utf32() if possible.
490  *
491  * @sa xkb_state_key_get_utf32()
492  */
493 uint32_t
494 xkb_keysym_to_utf32(xkb_keysym_t keysym);
495
496 /**
497  * Get the keysym corresponding to a Unicode/UTF-32 codepoint.
498  *
499  * @returns The keysym corresponding to the specified Unicode
500  * codepoint, or XKB_KEY_NoSymbol if there is none.
501  *
502  * This function is the inverse of @ref xkb_keysym_to_utf32. In cases
503  * where a single codepoint corresponds to multiple keysyms, returns
504  * the keysym with the lowest value.
505  *
506  * Unicode codepoints which do not have a special (legacy) keysym
507  * encoding use a direct encoding scheme. These keysyms don't usually
508  * have an associated keysym constant (XKB_KEY_*).
509  *
510  * For noncharacter Unicode codepoints and codepoints outside of the
511  * defined Unicode planes this function returns XKB_KEY_NoSymbol.
512  *
513  * @sa xkb_keysym_to_utf32()
514  * @since 0.11.0
515  */
516 xkb_keysym_t
517 xkb_utf32_to_keysym(uint32_t ucs);
518
519 /**
520  * Convert a keysym to its uppercase form.
521  *
522  * If there is no such form, the keysym is returned unchanged.
523  *
524  * The conversion rules may be incomplete; prefer to work with the Unicode
525  * representation instead, when possible.
526  */
527 xkb_keysym_t
528 xkb_keysym_to_upper(xkb_keysym_t ks);
529
530 /**
531  * Convert a keysym to its lowercase form.
532  *
533  * The conversion rules may be incomplete; prefer to work with the Unicode
534  * representation instead, when possible.
535  */
536 xkb_keysym_t
537 xkb_keysym_to_lower(xkb_keysym_t ks);
538
539 /** @} */
540
541 /**
542  * @defgroup context Library Context
543  * Creating, destroying and using library contexts.
544  *
545  * Every keymap compilation request must have a context associated with
546  * it.  The context keeps around state such as the include path.
547  *
548  * @{
549  */
550
551 /**
552  * @page envvars Environment Variables
553  *
554  * The user may set some environment variables which affect the library:
555  *
556  * - `XKB_CONFIG_ROOT`, `XDG_CONFIG_DIR`, `HOME` - see @ref include-path.
557  * - `XKB_LOG_LEVEL` - see xkb_context_set_log_level().
558  * - `XKB_LOG_VERBOSITY` - see xkb_context_set_log_verbosity().
559  * - `XKB_DEFAULT_RULES`, `XKB_DEFAULT_MODEL`, `XKB_DEFAULT_LAYOUT`,
560  *   `XKB_DEFAULT_VARIANT`, `XKB_DEFAULT_OPTIONS` - see xkb_rule_names.
561  */
562
563 /** Flags for context creation. */
564 enum xkb_context_flags {
565     /** Do not apply any context flags. */
566     XKB_CONTEXT_NO_FLAGS = 0,
567     /** Create this context with an empty include path. */
568     XKB_CONTEXT_NO_DEFAULT_INCLUDES = (1 << 0),
569     /**
570      * Don't take RMLVO names from the environment.
571      * @since 0.3.0
572      */
573     XKB_CONTEXT_NO_ENVIRONMENT_NAMES = (1 << 1)
574 };
575
576 /**
577  * Create a new context.
578  *
579  * @param flags Optional flags for the context, or 0.
580  *
581  * @returns A new context, or NULL on failure.
582  *
583  * @memberof xkb_context
584  */
585 struct xkb_context *
586 xkb_context_new(enum xkb_context_flags flags);
587
588 /**
589  * Take a new reference on a context.
590  *
591  * @returns The passed in context.
592  *
593  * @memberof xkb_context
594  */
595 struct xkb_context *
596 xkb_context_ref(struct xkb_context *context);
597
598 /**
599  * Release a reference on a context, and possibly free it.
600  *
601  * @param context The context.  If it is NULL, this function does nothing.
602  *
603  * @memberof xkb_context
604  */
605 void
606 xkb_context_unref(struct xkb_context *context);
607
608 /**
609  * Store custom user data in the context.
610  *
611  * This may be useful in conjunction with xkb_context_set_log_fn() or other
612  * callbacks.
613  *
614  * @memberof xkb_context
615  */
616 void
617 xkb_context_set_user_data(struct xkb_context *context, void *user_data);
618
619 /**
620  * Retrieves stored user data from the context.
621  *
622  * @returns The stored user data.  If the user data wasn't set, or the
623  * passed in context is NULL, returns NULL.
624  *
625  * This may be useful to access private user data from callbacks like a
626  * custom logging function.
627  *
628  * @memberof xkb_context
629  **/
630 void *
631 xkb_context_get_user_data(struct xkb_context *context);
632
633 /** @} */
634
635 /**
636  * @defgroup include-path Include Paths
637  * Manipulating the include paths in a context.
638  *
639  * The include paths are the file-system paths that are searched when an
640  * include statement is encountered during keymap compilation.
641  *
642  * The default include paths are, in that lookup order:
643  * - The path `$XDG_CONFIG_HOME/xkb`, with the usual `XDG_CONFIG_HOME`
644  *   fallback to `$HOME/.config/` if unset.
645  * - The path `$HOME/.xkb`, where $HOME is the value of the environment
646  *   variable `HOME`.
647  * - The `XKB_CONFIG_ROOT` environment variable, if defined, otherwise
648  *   the system XKB root, defined at library configuration time.
649  *
650  * @{
651  */
652
653 /**
654  * Append a new entry to the context's include path.
655  *
656  * @returns 1 on success, or 0 if the include path could not be added or is
657  * inaccessible.
658  *
659  * @memberof xkb_context
660  */
661 int
662 xkb_context_include_path_append(struct xkb_context *context, const char *path);
663
664 /**
665  * Append the default include paths to the context's include path.
666  *
667  * @returns 1 on success, or 0 if the primary include path could not be added.
668  *
669  * @memberof xkb_context
670  */
671 int
672 xkb_context_include_path_append_default(struct xkb_context *context);
673
674 /**
675  * Reset the context's include path to the default.
676  *
677  * Removes all entries from the context's include path, and inserts the
678  * default paths.
679  *
680  * @returns 1 on success, or 0 if the primary include path could not be added.
681  *
682  * @memberof xkb_context
683  */
684 int
685 xkb_context_include_path_reset_defaults(struct xkb_context *context);
686
687 /**
688  * Remove all entries from the context's include path.
689  *
690  * @memberof xkb_context
691  */
692 void
693 xkb_context_include_path_clear(struct xkb_context *context);
694
695 /**
696  * Get the number of paths in the context's include path.
697  *
698  * @memberof xkb_context
699  */
700 unsigned int
701 xkb_context_num_include_paths(struct xkb_context *context);
702
703 /**
704  * Get a specific include path from the context's include path.
705  *
706  * @returns The include path at the specified index.  If the index is
707  * invalid, returns NULL.
708  *
709  * @memberof xkb_context
710  */
711 const char *
712 xkb_context_include_path_get(struct xkb_context *context, unsigned int index);
713
714 /** @} */
715
716 /**
717  * @defgroup logging Logging Handling
718  * Manipulating how logging from this library is handled.
719  *
720  * @{
721  */
722
723 /** Specifies a logging level. */
724 enum xkb_log_level {
725     XKB_LOG_LEVEL_CRITICAL = 10, /**< Log critical internal errors only. */
726     XKB_LOG_LEVEL_ERROR = 20,    /**< Log all errors. */
727     XKB_LOG_LEVEL_WARNING = 30,  /**< Log warnings and errors. */
728     XKB_LOG_LEVEL_INFO = 40,     /**< Log information, warnings, and errors. */
729     XKB_LOG_LEVEL_DEBUG = 50     /**< Log everything. */
730 };
731
732 /**
733  * Set the current logging level.
734  *
735  * @param context The context in which to set the logging level.
736  * @param level   The logging level to use.  Only messages from this level
737  * and below will be logged.
738  *
739  * The default level is XKB_LOG_LEVEL_ERROR.  The environment variable
740  * XKB_LOG_LEVEL, if set in the time the context was created, overrides the
741  * default value.  It may be specified as a level number or name.
742  *
743  * @memberof xkb_context
744  */
745 void
746 xkb_context_set_log_level(struct xkb_context *context,
747                           enum xkb_log_level level);
748
749 /**
750  * Get the current logging level.
751  *
752  * @memberof xkb_context
753  */
754 enum xkb_log_level
755 xkb_context_get_log_level(struct xkb_context *context);
756
757 /**
758  * Sets the current logging verbosity.
759  *
760  * The library can generate a number of warnings which are not helpful to
761  * ordinary users of the library.  The verbosity may be increased if more
762  * information is desired (e.g. when developing a new keymap).
763  *
764  * The default verbosity is 0.  The environment variable XKB_LOG_VERBOSITY,
765  * if set in the time the context was created, overrides the default value.
766  *
767  * @param context   The context in which to use the set verbosity.
768  * @param verbosity The verbosity to use.  Currently used values are
769  * 1 to 10, higher values being more verbose.  0 would result in no verbose
770  * messages being logged.
771  *
772  * Most verbose messages are of level XKB_LOG_LEVEL_WARNING or lower.
773  *
774  * @memberof xkb_context
775  */
776 void
777 xkb_context_set_log_verbosity(struct xkb_context *context, int verbosity);
778
779 /**
780  * Get the current logging verbosity of the context.
781  *
782  * @memberof xkb_context
783  */
784 int
785 xkb_context_get_log_verbosity(struct xkb_context *context);
786
787 /**
788  * Set a custom function to handle logging messages.
789  *
790  * @param context The context in which to use the set logging function.
791  * @param log_fn  The function that will be called for logging messages.
792  * Passing NULL restores the default function, which logs to stderr.
793  *
794  * By default, log messages from this library are printed to stderr.  This
795  * function allows you to replace the default behavior with a custom
796  * handler.  The handler is only called with messages which match the
797  * current logging level and verbosity settings for the context.
798  * level is the logging level of the message.  @a format and @a args are
799  * the same as in the vprintf(3) function.
800  *
801  * You may use xkb_context_set_user_data() on the context, and then call
802  * xkb_context_get_user_data() from within the logging function to provide
803  * it with additional private context.
804  *
805  * @memberof xkb_context
806  */
807 void
808 xkb_context_set_log_fn(struct xkb_context *context,
809                        void (*log_fn)(struct xkb_context *context,
810                                       enum xkb_log_level level,
811                                       const char *format, va_list args));
812
813 /** @} */
814
815 /**
816  * @defgroup keymap Keymap Creation
817  * Creating and destroying keymaps.
818  *
819  * @{
820  */
821
822 /** Flags for keymap compilation. */
823 enum xkb_keymap_compile_flags {
824     /** Do not apply any flags. */
825     XKB_KEYMAP_COMPILE_NO_FLAGS = 0
826 };
827
828 /**
829  * Create a keymap from RMLVO names.
830  *
831  * The primary keymap entry point: creates a new XKB keymap from a set of
832  * RMLVO (Rules + Model + Layouts + Variants + Options) names.
833  *
834  * @param context The context in which to create the keymap.
835  * @param names   The RMLVO names to use.  See xkb_rule_names.
836  * @param flags   Optional flags for the keymap, or 0.
837  *
838  * @returns A keymap compiled according to the RMLVO names, or NULL if
839  * the compilation failed.
840  *
841  * @sa xkb_rule_names
842  * @memberof xkb_keymap
843  */
844 struct xkb_keymap *
845 xkb_keymap_new_from_names(struct xkb_context *context,
846                           const struct xkb_rule_names *names,
847                           enum xkb_keymap_compile_flags flags);
848
849 /** The possible keymap formats. */
850 enum xkb_keymap_format {
851     /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
852     XKB_KEYMAP_FORMAT_TEXT_V1 = 1
853 };
854
855 /**
856  * Create a keymap from a keymap file.
857  *
858  * @param context The context in which to create the keymap.
859  * @param file    The keymap file to compile.
860  * @param format  The text format of the keymap file to compile.
861  * @param flags   Optional flags for the keymap, or 0.
862  *
863  * @returns A keymap compiled from the given XKB keymap file, or NULL if
864  * the compilation failed.
865  *
866  * The file must contain a complete keymap.  For example, in the
867  * XKB_KEYMAP_FORMAT_TEXT_V1 format, this means the file must contain one
868  * top level '%xkb_keymap' section, which in turn contains other required
869  * sections.
870  *
871  * @memberof xkb_keymap
872  */
873 struct xkb_keymap *
874 xkb_keymap_new_from_file(struct xkb_context *context, FILE *file,
875                          enum xkb_keymap_format format,
876                          enum xkb_keymap_compile_flags flags);
877
878 /**
879  * Create a keymap from a keymap string.
880  *
881  * This is just like xkb_keymap_new_from_file(), but instead of a file, gets
882  * the keymap as one enormous string.
883  *
884  * @see xkb_keymap_new_from_file()
885  * @memberof xkb_keymap
886  */
887 struct xkb_keymap *
888 xkb_keymap_new_from_string(struct xkb_context *context, const char *string,
889                            enum xkb_keymap_format format,
890                            enum xkb_keymap_compile_flags flags);
891
892 /**
893  * Create a keymap from a memory buffer.
894  *
895  * This is just like xkb_keymap_new_from_string(), but takes a length argument
896  * so the input string does not have to be zero-terminated.
897  *
898  * @see xkb_keymap_new_from_string()
899  * @memberof xkb_keymap
900  * @since 0.3.0
901  */
902 struct xkb_keymap *
903 xkb_keymap_new_from_buffer(struct xkb_context *context, const char *buffer,
904                            size_t length, enum xkb_keymap_format format,
905                            enum xkb_keymap_compile_flags flags);
906
907 /**
908  * Take a new reference on a keymap.
909  *
910  * @returns The passed in keymap.
911  *
912  * @memberof xkb_keymap
913  */
914 struct xkb_keymap *
915 xkb_keymap_ref(struct xkb_keymap *keymap);
916
917 /**
918  * Release a reference on a keymap, and possibly free it.
919  *
920  * @param keymap The keymap.  If it is NULL, this function does nothing.
921  *
922  * @memberof xkb_keymap
923  */
924 void
925 xkb_keymap_unref(struct xkb_keymap *keymap);
926
927 /**
928  * Get the keymap as a string in the format from which it was created.
929  * @sa xkb_keymap_get_as_string()
930  **/
931 #define XKB_KEYMAP_USE_ORIGINAL_FORMAT ((enum xkb_keymap_format) -1)
932
933 /**
934  * Get the compiled keymap as a string.
935  *
936  * @param keymap The keymap to get as a string.
937  * @param format The keymap format to use for the string.  You can pass
938  * in the special value XKB_KEYMAP_USE_ORIGINAL_FORMAT to use the format
939  * from which the keymap was originally created.
940  *
941  * @returns The keymap as a NUL-terminated string, or NULL if unsuccessful.
942  *
943  * The returned string may be fed back into xkb_keymap_new_from_string() to get
944  * the exact same keymap (possibly in another process, etc.).
945  *
946  * The returned string is dynamically allocated and should be freed by the
947  * caller.
948  *
949  * @memberof xkb_keymap
950  */
951 char *
952 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
953                          enum xkb_keymap_format format);
954
955 /** @} */
956
957 /**
958  * @defgroup components Keymap Components
959  * Enumeration of state components in a keymap.
960  *
961  * @{
962  */
963
964 /**
965  * Get the minimum keycode in the keymap.
966  *
967  * @sa xkb_keycode_t
968  * @memberof xkb_keymap
969  * @since 0.3.1
970  */
971 xkb_keycode_t
972 xkb_keymap_min_keycode(struct xkb_keymap *keymap);
973
974 /**
975  * Get the maximum keycode in the keymap.
976  *
977  * @sa xkb_keycode_t
978  * @memberof xkb_keymap
979  * @since 0.3.1
980  */
981 xkb_keycode_t
982 xkb_keymap_max_keycode(struct xkb_keymap *keymap);
983
984 /**
985  * The iterator used by xkb_keymap_key_for_each().
986  *
987  * @sa xkb_keymap_key_for_each
988  * @memberof xkb_keymap
989  * @since 0.3.1
990  */
991 typedef void
992 (*xkb_keymap_key_iter_t)(struct xkb_keymap *keymap, xkb_keycode_t key,
993                          void *data);
994
995 /**
996  * Run a specified function for every valid keycode in the keymap.  If a
997  * keymap is sparse, this function may be called fewer than
998  * (max_keycode - min_keycode + 1) times.
999  *
1000  * @sa xkb_keymap_min_keycode() xkb_keymap_max_keycode() xkb_keycode_t
1001  * @memberof xkb_keymap
1002  * @since 0.3.1
1003  */
1004 void
1005 xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
1006                         void *data);
1007
1008 /**
1009  * Find the name of the key with the given keycode.
1010  *
1011  * This function always returns the canonical name of the key (see
1012  * description in xkb_keycode_t).
1013  *
1014  * @returns The key name. If no key with this keycode exists,
1015  * returns NULL.
1016  *
1017  * @sa xkb_keycode_t
1018  * @memberof xkb_keymap
1019  * @since 0.6.0
1020  */
1021 const char *
1022 xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t key);
1023
1024 /**
1025  * Find the keycode of the key with the given name.
1026  *
1027  * The name can be either a canonical name or an alias.
1028  *
1029  * @returns The keycode. If no key with this name exists,
1030  * returns XKB_KEYCODE_INVALID.
1031  *
1032  * @sa xkb_keycode_t
1033  * @memberof xkb_keymap
1034  * @since 0.6.0
1035  */
1036 xkb_keycode_t
1037 xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name);
1038
1039 /**
1040  * Get the number of modifiers in the keymap.
1041  *
1042  * @sa xkb_mod_index_t
1043  * @memberof xkb_keymap
1044  */
1045 xkb_mod_index_t
1046 xkb_keymap_num_mods(struct xkb_keymap *keymap);
1047
1048 /**
1049  * Get the name of a modifier by index.
1050  *
1051  * @returns The name.  If the index is invalid, returns NULL.
1052  *
1053  * @sa xkb_mod_index_t
1054  * @memberof xkb_keymap
1055  */
1056 const char *
1057 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx);
1058
1059 /**
1060  * Get the index of a modifier by name.
1061  *
1062  * @returns The index.  If no modifier with this name exists, returns
1063  * XKB_MOD_INVALID.
1064  *
1065  * @sa xkb_mod_index_t
1066  * @memberof xkb_keymap
1067  */
1068 xkb_mod_index_t
1069 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name);
1070
1071 /**
1072  * Get the number of layouts in the keymap.
1073  *
1074  * @sa xkb_layout_index_t xkb_rule_names xkb_keymap_num_layouts_for_key()
1075  * @memberof xkb_keymap
1076  */
1077 xkb_layout_index_t
1078 xkb_keymap_num_layouts(struct xkb_keymap *keymap);
1079
1080 /**
1081  * Get the name of a layout by index.
1082  *
1083  * @returns The name.  If the index is invalid, or the layout does not have
1084  * a name, returns NULL.
1085  *
1086  * @sa xkb_layout_index_t
1087  * @memberof xkb_keymap
1088  */
1089 const char *
1090 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx);
1091
1092 /**
1093  * Get the index of a layout by name.
1094  *
1095  * @returns The index.  If no layout exists with this name, returns
1096  * XKB_LAYOUT_INVALID.  If more than one layout in the keymap has this name,
1097  * returns the lowest index among them.
1098  *
1099  * @memberof xkb_keymap
1100  */
1101 xkb_layout_index_t
1102 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name);
1103
1104 /**
1105  * Get the number of LEDs in the keymap.
1106  *
1107  * @warning The range [ 0...xkb_keymap_num_leds() ) includes all of the LEDs
1108  * in the keymap, but may also contain inactive LEDs.  When iterating over
1109  * this range, you need the handle this case when calling functions such as
1110  * xkb_keymap_led_get_name() or xkb_state_led_index_is_active().
1111  *
1112  * @sa xkb_led_index_t
1113  * @memberof xkb_keymap
1114  */
1115 xkb_led_index_t
1116 xkb_keymap_num_leds(struct xkb_keymap *keymap);
1117
1118 /**
1119  * Get the name of a LED by index.
1120  *
1121  * @returns The name.  If the index is invalid, returns NULL.
1122  *
1123  * @memberof xkb_keymap
1124  */
1125 const char *
1126 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx);
1127
1128 /**
1129  * Get the index of a LED by name.
1130  *
1131  * @returns The index.  If no LED with this name exists, returns
1132  * XKB_LED_INVALID.
1133  *
1134  * @memberof xkb_keymap
1135  */
1136 xkb_led_index_t
1137 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name);
1138
1139 /**
1140  * Get the number of layouts for a specific key.
1141  *
1142  * This number can be different from xkb_keymap_num_layouts(), but is always
1143  * smaller.  It is the appropriate value to use when iterating over the
1144  * layouts of a key.
1145  *
1146  * @sa xkb_layout_index_t
1147  * @memberof xkb_keymap
1148  */
1149 xkb_layout_index_t
1150 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t key);
1151
1152 /**
1153  * Get the number of shift levels for a specific key and layout.
1154  *
1155  * If @c layout is out of range for this key (that is, larger or equal to
1156  * the value returned by xkb_keymap_num_layouts_for_key()), it is brought
1157  * back into range in a manner consistent with xkb_state_key_get_layout().
1158  *
1159  * @sa xkb_level_index_t
1160  * @memberof xkb_keymap
1161  */
1162 xkb_level_index_t
1163 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t key,
1164                               xkb_layout_index_t layout);
1165
1166 /**
1167  * Retrieves every possible modifier mask that produces the specified
1168  * shift level for a specific key and layout.
1169  *
1170  * This API is useful for inverse key transformation; i.e. finding out
1171  * which modifiers need to be active in order to be able to type the
1172  * keysym(s) corresponding to the specific key code, layout and level.
1173  *
1174  * @warning It returns only up to masks_size modifier masks. If the
1175  * buffer passed is too small, some of the possible modifier combinations
1176  * will not be returned.
1177  *
1178  * @param[in] keymap      The keymap.
1179  * @param[in] key         The keycode of the key.
1180  * @param[in] layout      The layout for which to get modifiers.
1181  * @param[in] level       The shift level in the layout for which to get the
1182  * modifiers. This should be smaller than:
1183  * @code xkb_keymap_num_levels_for_key(keymap, key) @endcode
1184  * @param[out] masks_out  A buffer in which the requested masks should be
1185  * stored.
1186  * @param[out] masks_size The size of the buffer pointed to by masks_out.
1187  *
1188  * If @c layout is out of range for this key (that is, larger or equal to
1189  * the value returned by xkb_keymap_num_layouts_for_key()), it is brought
1190  * back into range in a manner consistent with xkb_state_key_get_layout().
1191  *
1192  * @returns The number of modifier masks stored in the masks_out array.
1193  * If the key is not in the keymap or if the specified shift level cannot
1194  * be reached it returns 0 and does not modify the masks_out buffer.
1195  *
1196  * @sa xkb_level_index_t
1197  * @sa xkb_mod_mask_t
1198  * @memberof xkb_keymap
1199  * @since 0.11.0
1200  */
1201 size_t
1202 xkb_keymap_key_get_mods_for_level(struct xkb_keymap *keymap,
1203                                   xkb_keycode_t key,
1204                                   xkb_layout_index_t layout,
1205                                   xkb_level_index_t level,
1206                                   xkb_mod_mask_t *masks_out,
1207                                   size_t masks_size);
1208
1209 /**
1210  * Get the keysyms obtained from pressing a key in a given layout and
1211  * shift level.
1212  *
1213  * This function is like xkb_state_key_get_syms(), only the layout and
1214  * shift level are not derived from the keyboard state but are instead
1215  * specified explicitly.
1216  *
1217  * @param[in] keymap    The keymap.
1218  * @param[in] key       The keycode of the key.
1219  * @param[in] layout    The layout for which to get the keysyms.
1220  * @param[in] level     The shift level in the layout for which to get the
1221  * keysyms. This should be smaller than:
1222  * @code xkb_keymap_num_levels_for_key(keymap, key) @endcode
1223  * @param[out] syms_out An immutable array of keysyms corresponding to the
1224  * key in the given layout and shift level.
1225  *
1226  * If @c layout is out of range for this key (that is, larger or equal to
1227  * the value returned by xkb_keymap_num_layouts_for_key()), it is brought
1228  * back into range in a manner consistent with xkb_state_key_get_layout().
1229  *
1230  * @returns The number of keysyms in the syms_out array.  If no keysyms
1231  * are produced by the key in the given layout and shift level, returns 0
1232  * and sets syms_out to NULL.
1233  *
1234  * @sa xkb_state_key_get_syms()
1235  * @memberof xkb_keymap
1236  */
1237 int
1238 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
1239                                  xkb_keycode_t key,
1240                                  xkb_layout_index_t layout,
1241                                  xkb_level_index_t level,
1242                                  const xkb_keysym_t **syms_out);
1243
1244 /**
1245  * Determine whether a key should repeat or not.
1246  *
1247  * A keymap may specify different repeat behaviors for different keys.
1248  * Most keys should generally exhibit repeat behavior; for example, holding
1249  * the 'a' key down in a text editor should normally insert a single 'a'
1250  * character every few milliseconds, until the key is released.  However,
1251  * there are keys which should not or do not need to be repeated.  For
1252  * example, repeating modifier keys such as Left/Right Shift or Caps Lock
1253  * is not generally useful or desired.
1254  *
1255  * @returns 1 if the key should repeat, 0 otherwise.
1256  *
1257  * @memberof xkb_keymap
1258  */
1259 int
1260 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t key);
1261
1262 /** @} */
1263
1264 /**
1265  * @defgroup state Keyboard State
1266  * Creating, destroying and manipulating keyboard state objects.
1267  *
1268  * @{
1269  */
1270
1271 /**
1272  * Create a new keyboard state object.
1273  *
1274  * @param keymap The keymap which the state will use.
1275  *
1276  * @returns A new keyboard state object, or NULL on failure.
1277  *
1278  * @memberof xkb_state
1279  */
1280 struct xkb_state *
1281 xkb_state_new(struct xkb_keymap *keymap);
1282
1283 /**
1284  * Take a new reference on a keyboard state object.
1285  *
1286  * @returns The passed in object.
1287  *
1288  * @memberof xkb_state
1289  */
1290 struct xkb_state *
1291 xkb_state_ref(struct xkb_state *state);
1292
1293 /**
1294  * Release a reference on a keybaord state object, and possibly free it.
1295  *
1296  * @param state The state.  If it is NULL, this function does nothing.
1297  *
1298  * @memberof xkb_state
1299  */
1300 void
1301 xkb_state_unref(struct xkb_state *state);
1302
1303 /**
1304  * Get the keymap which a keyboard state object is using.
1305  *
1306  * @returns The keymap which was passed to xkb_state_new() when creating
1307  * this state object.
1308  *
1309  * This function does not take a new reference on the keymap; you must
1310  * explicitly reference it yourself if you plan to use it beyond the
1311  * lifetime of the state.
1312  *
1313  * @memberof xkb_state
1314  */
1315 struct xkb_keymap *
1316 xkb_state_get_keymap(struct xkb_state *state);
1317
1318 /** Specifies the direction of the key (press / release). */
1319 enum xkb_key_direction {
1320     XKB_KEY_UP,   /**< The key was released. */
1321     XKB_KEY_DOWN  /**< The key was pressed. */
1322 };
1323
1324 /**
1325  * Modifier and layout types for state objects.  This enum is bitmaskable,
1326  * e.g. (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED) is valid to
1327  * exclude locked modifiers.
1328  *
1329  * In XKB, the DEPRESSED components are also known as 'base'.
1330  */
1331 enum xkb_state_component {
1332     /** Depressed modifiers, i.e. a key is physically holding them. */
1333     XKB_STATE_MODS_DEPRESSED = (1 << 0),
1334     /** Latched modifiers, i.e. will be unset after the next non-modifier
1335      *  key press. */
1336     XKB_STATE_MODS_LATCHED = (1 << 1),
1337     /** Locked modifiers, i.e. will be unset after the key provoking the
1338      *  lock has been pressed again. */
1339     XKB_STATE_MODS_LOCKED = (1 << 2),
1340     /** Effective modifiers, i.e. currently active and affect key
1341      *  processing (derived from the other state components).
1342      *  Use this unless you explicitly care how the state came about. */
1343     XKB_STATE_MODS_EFFECTIVE = (1 << 3),
1344     /** Depressed layout, i.e. a key is physically holding it. */
1345     XKB_STATE_LAYOUT_DEPRESSED = (1 << 4),
1346     /** Latched layout, i.e. will be unset after the next non-modifier
1347      *  key press. */
1348     XKB_STATE_LAYOUT_LATCHED = (1 << 5),
1349     /** Locked layout, i.e. will be unset after the key provoking the lock
1350      *  has been pressed again. */
1351     XKB_STATE_LAYOUT_LOCKED = (1 << 6),
1352     /** Effective layout, i.e. currently active and affects key processing
1353      *  (derived from the other state components).
1354      *  Use this unless you explicitly care how the state came about. */
1355     XKB_STATE_LAYOUT_EFFECTIVE = (1 << 7),
1356     /** LEDs (derived from the other state components). */
1357     XKB_STATE_LEDS = (1 << 8)
1358 };
1359
1360 /**
1361  * Update the keyboard state to reflect a given key being pressed or
1362  * released.
1363  *
1364  * This entry point is intended for programs which track the keyboard state
1365  * explicitly (like an evdev client).  If the state is serialized to you by
1366  * a master process (like a Wayland compositor) using functions like
1367  * xkb_state_serialize_mods(), you should use xkb_state_update_mask() instead.
1368  * The two functions should not generally be used together.
1369  *
1370  * A series of calls to this function should be consistent; that is, a call
1371  * with XKB_KEY_DOWN for a key should be matched by an XKB_KEY_UP; if a key
1372  * is pressed twice, it should be released twice; etc. Otherwise (e.g. due
1373  * to missed input events), situations like "stuck modifiers" may occur.
1374  *
1375  * This function is often used in conjunction with the function
1376  * xkb_state_key_get_syms() (or xkb_state_key_get_one_sym()), for example,
1377  * when handling a key event.  In this case, you should prefer to get the
1378  * keysyms *before* updating the key, such that the keysyms reported for
1379  * the key event are not affected by the event itself.  This is the
1380  * conventional behavior.
1381  *
1382  * @returns A mask of state components that have changed as a result of
1383  * the update.  If nothing in the state has changed, returns 0.
1384  *
1385  * @memberof xkb_state
1386  *
1387  * @sa xkb_state_update_mask()
1388  */
1389 enum xkb_state_component
1390 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
1391                      enum xkb_key_direction direction);
1392
1393 /**
1394  * Update a keyboard state from a set of explicit masks.
1395  *
1396  * This entry point is intended for window systems and the like, where a
1397  * master process holds an xkb_state, then serializes it over a wire
1398  * protocol, and clients then use the serialization to feed in to their own
1399  * xkb_state.
1400  *
1401  * All parameters must always be passed, or the resulting state may be
1402  * incoherent.
1403  *
1404  * The serialization is lossy and will not survive round trips; it must only
1405  * be used to feed slave state objects, and must not be used to update the
1406  * master state.
1407  *
1408  * If you do not fit the description above, you should use
1409  * xkb_state_update_key() instead.  The two functions should not generally be
1410  * used together.
1411  *
1412  * @returns A mask of state components that have changed as a result of
1413  * the update.  If nothing in the state has changed, returns 0.
1414  *
1415  * @memberof xkb_state
1416  *
1417  * @sa xkb_state_component
1418  * @sa xkb_state_update_key
1419  */
1420 enum xkb_state_component
1421 xkb_state_update_mask(struct xkb_state *state,
1422                       xkb_mod_mask_t depressed_mods,
1423                       xkb_mod_mask_t latched_mods,
1424                       xkb_mod_mask_t locked_mods,
1425                       xkb_layout_index_t depressed_layout,
1426                       xkb_layout_index_t latched_layout,
1427                       xkb_layout_index_t locked_layout);
1428
1429 /**
1430  * Get the keysyms obtained from pressing a particular key in a given
1431  * keyboard state.
1432  *
1433  * Get the keysyms for a key according to the current active layout,
1434  * modifiers and shift level for the key, as determined by a keyboard
1435  * state.
1436  *
1437  * @param[in]  state    The keyboard state object.
1438  * @param[in]  key      The keycode of the key.
1439  * @param[out] syms_out An immutable array of keysyms corresponding the
1440  * key in the given keyboard state.
1441  *
1442  * As an extension to XKB, this function can return more than one keysym.
1443  * If you do not want to handle this case, you can use
1444  * xkb_state_key_get_one_sym() for a simpler interface.
1445  *
1446  * This function does not perform any @ref keysym-transformations.
1447  * (This might change).
1448  *
1449  * @returns The number of keysyms in the syms_out array.  If no keysyms
1450  * are produced by the key in the given keyboard state, returns 0 and sets
1451  * syms_out to NULL.
1452  *
1453  * @memberof xkb_state
1454  */
1455 int
1456 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
1457                        const xkb_keysym_t **syms_out);
1458
1459 /**
1460  * Get the Unicode/UTF-8 string obtained from pressing a particular key
1461  * in a given keyboard state.
1462  *
1463  * @param[in]  state  The keyboard state object.
1464  * @param[in]  key    The keycode of the key.
1465  * @param[out] buffer A buffer to write the string into.
1466  * @param[in]  size   Size of the buffer.
1467  *
1468  * @warning If the buffer passed is too small, the string is truncated
1469  * (though still NUL-terminated).
1470  *
1471  * @returns The number of bytes required for the string, excluding the
1472  * NUL byte.  If there is nothing to write, returns 0.
1473  *
1474  * You may check if truncation has occurred by comparing the return value
1475  * with the size of @p buffer, similarly to the snprintf(3) function.
1476  * You may safely pass NULL and 0 to @p buffer and @p size to find the
1477  * required size (without the NUL-byte).
1478  *
1479  * This function performs Capitalization and Control @ref
1480  * keysym-transformations.
1481  *
1482  * @memberof xkb_state
1483  * @since 0.4.1
1484  */
1485 int
1486 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t key,
1487                        char *buffer, size_t size);
1488
1489 /**
1490  * Get the Unicode/UTF-32 codepoint obtained from pressing a particular
1491  * key in a a given keyboard state.
1492  *
1493  * @returns The UTF-32 representation for the key, if it consists of only
1494  * a single codepoint.  Otherwise, returns 0.
1495  *
1496  * This function performs Capitalization and Control @ref
1497  * keysym-transformations.
1498  *
1499  * @memberof xkb_state
1500  * @since 0.4.1
1501  */
1502 uint32_t
1503 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t key);
1504
1505 /**
1506  * Get the single keysym obtained from pressing a particular key in a
1507  * given keyboard state.
1508  *
1509  * This function is similar to xkb_state_key_get_syms(), but intended
1510  * for users which cannot or do not want to handle the case where
1511  * multiple keysyms are returned (in which case this function is
1512  * preferred).
1513  *
1514  * @returns The keysym.  If the key does not have exactly one keysym,
1515  * returns XKB_KEY_NoSymbol
1516  *
1517  * This function performs Capitalization @ref keysym-transformations.
1518  *
1519  * @sa xkb_state_key_get_syms()
1520  * @memberof xkb_state
1521  */
1522 xkb_keysym_t
1523 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t key);
1524
1525 /**
1526  * Get the effective layout index for a key in a given keyboard state.
1527  *
1528  * @returns The layout index for the key in the given keyboard state.  If
1529  * the given keycode is invalid, or if the key is not included in any
1530  * layout at all, returns XKB_LAYOUT_INVALID.
1531  *
1532  * @invariant If the returned layout is valid, the following always holds:
1533  * @code
1534  * xkb_state_key_get_layout(state, key) < xkb_keymap_num_layouts_for_key(keymap, key)
1535  * @endcode
1536  *
1537  * @memberof xkb_state
1538  */
1539 xkb_layout_index_t
1540 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t key);
1541
1542 /**
1543  * Get the effective shift level for a key in a given keyboard state and
1544  * layout.
1545  *
1546  * @param state The keyboard state.
1547  * @param key The keycode of the key.
1548  * @param layout The layout for which to get the shift level.  This must be
1549  * smaller than:
1550  * @code xkb_keymap_num_layouts_for_key(keymap, key) @endcode
1551  * usually it would be:
1552  * @code xkb_state_key_get_layout(state, key) @endcode
1553  *
1554  * @return The shift level index.  If the key or layout are invalid,
1555  * returns XKB_LEVEL_INVALID.
1556  *
1557  * @invariant If the returned level is valid, the following always holds:
1558  * @code
1559  * xkb_state_key_get_level(state, key, layout) < xkb_keymap_num_levels_for_key(keymap, key, layout)
1560  * @endcode
1561  *
1562  * @memberof xkb_state
1563  */
1564 xkb_level_index_t
1565 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t key,
1566                         xkb_layout_index_t layout);
1567
1568 /**
1569  * Match flags for xkb_state_mod_indices_are_active() and
1570  * xkb_state_mod_names_are_active(), specifying the conditions for a
1571  * successful match.  XKB_STATE_MATCH_NON_EXCLUSIVE is bitmaskable with
1572  * the other modes.
1573  */
1574 enum xkb_state_match {
1575     /** Returns true if any of the modifiers are active. */
1576     XKB_STATE_MATCH_ANY = (1 << 0),
1577     /** Returns true if all of the modifiers are active. */
1578     XKB_STATE_MATCH_ALL = (1 << 1),
1579     /** Makes matching non-exclusive, i.e. will not return false if a
1580      *  modifier not specified in the arguments is active. */
1581     XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16)
1582 };
1583
1584 /**
1585  * The counterpart to xkb_state_update_mask for modifiers, to be used on
1586  * the server side of serialization.
1587  *
1588  * @param state      The keyboard state.
1589  * @param components A mask of the modifier state components to serialize.
1590  * State components other than XKB_STATE_MODS_* are ignored.
1591  * If XKB_STATE_MODS_EFFECTIVE is included, all other state components are
1592  * ignored.
1593  *
1594  * @returns A xkb_mod_mask_t representing the given components of the
1595  * modifier state.
1596  *
1597  * This function should not be used in regular clients; please use the
1598  * xkb_state_mod_*_is_active API instead.
1599  *
1600  * @memberof xkb_state
1601  */
1602 xkb_mod_mask_t
1603 xkb_state_serialize_mods(struct xkb_state *state,
1604                          enum xkb_state_component components);
1605
1606 /**
1607  * The counterpart to xkb_state_update_mask for layouts, to be used on
1608  * the server side of serialization.
1609  *
1610  * @param state      The keyboard state.
1611  * @param components A mask of the layout state components to serialize.
1612  * State components other than XKB_STATE_LAYOUT_* are ignored.
1613  * If XKB_STATE_LAYOUT_EFFECTIVE is included, all other state components are
1614  * ignored.
1615  *
1616  * @returns A layout index representing the given components of the
1617  * layout state.
1618  *
1619  * This function should not be used in regular clients; please use the
1620  * xkb_state_layout_*_is_active API instead.
1621  *
1622  * @memberof xkb_state
1623  */
1624 xkb_layout_index_t
1625 xkb_state_serialize_layout(struct xkb_state *state,
1626                            enum xkb_state_component components);
1627
1628 /**
1629  * Test whether a modifier is active in a given keyboard state by name.
1630  *
1631  * @returns 1 if the modifier is active, 0 if it is not.  If the modifier
1632  * name does not exist in the keymap, returns -1.
1633  *
1634  * @memberof xkb_state
1635  */
1636 int
1637 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1638                              enum xkb_state_component type);
1639
1640 /**
1641  * Test whether a set of modifiers are active in a given keyboard state by
1642  * name.
1643  *
1644  * @param state The keyboard state.
1645  * @param type  The component of the state against which to match the
1646  * given modifiers.
1647  * @param match The manner by which to match the state against the
1648  * given modifiers.
1649  * @param ...   The set of of modifier names to test, terminated by a NULL
1650  * argument (sentinel).
1651  *
1652  * @returns 1 if the modifiers are active, 0 if they are not.  If any of
1653  * the modifier names do not exist in the keymap, returns -1.
1654  *
1655  * @memberof xkb_state
1656  */
1657 int
1658 xkb_state_mod_names_are_active(struct xkb_state *state,
1659                                enum xkb_state_component type,
1660                                enum xkb_state_match match,
1661                                ...);
1662
1663 /**
1664  * Test whether a modifier is active in a given keyboard state by index.
1665  *
1666  * @returns 1 if the modifier is active, 0 if it is not.  If the modifier
1667  * index is invalid in the keymap, returns -1.
1668  *
1669  * @memberof xkb_state
1670  */
1671 int
1672 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
1673                               enum xkb_state_component type);
1674
1675 /**
1676  * Test whether a set of modifiers are active in a given keyboard state by
1677  * index.
1678  *
1679  * @param state The keyboard state.
1680  * @param type  The component of the state against which to match the
1681  * given modifiers.
1682  * @param match The manner by which to match the state against the
1683  * given modifiers.
1684  * @param ...   The set of of modifier indices to test, terminated by a
1685  * XKB_MOD_INVALID argument (sentinel).
1686  *
1687  * @returns 1 if the modifiers are active, 0 if they are not.  If any of
1688  * the modifier indices are invalid in the keymap, returns -1.
1689  *
1690  * @memberof xkb_state
1691  */
1692 int
1693 xkb_state_mod_indices_are_active(struct xkb_state *state,
1694                                  enum xkb_state_component type,
1695                                  enum xkb_state_match match,
1696                                  ...);
1697
1698 /**
1699  * @page consumed-modifiers Consumed Modifiers
1700  * @parblock
1701  *
1702  * Some functions, like xkb_state_key_get_syms(), look at the state of
1703  * the modifiers in the keymap and derive from it the correct shift level
1704  * to use for the key.  For example, in a US layout, pressing the key
1705  * labeled \<A\> while the Shift modifier is active, generates the keysym
1706  * 'A'.  In this case, the Shift modifier is said to be "consumed".
1707  * However, the Num Lock modifier does not affect this translation at all,
1708  * even if it is active, so it is not consumed by this translation.
1709  *
1710  * It may be desirable for some application to not reuse consumed modifiers
1711  * for further processing, e.g. for hotkeys or keyboard shortcuts.  To
1712  * understand why, consider some requirements from a standard shortcut
1713  * mechanism, and how they are implemented:
1714  *
1715  * 1. The shortcut's modifiers must match exactly to the state.  For
1716  *    example, it is possible to bind separate actions to \<Alt\>\<Tab\>
1717  *    and to \<Alt\>\<Shift\>\<Tab\>.  Further, if only \<Alt\>\<Tab\> is
1718  *    bound to an action, pressing \<Alt\>\<Shift\>\<Tab\> should not
1719  *    trigger the shortcut.
1720  *    Effectively, this means that the modifiers are compared using the
1721  *    equality operator (==).
1722  *
1723  * 2. Only relevant modifiers are considered for the matching.  For example,
1724  *    Caps Lock and Num Lock should not generally affect the matching, e.g.
1725  *    when matching \<Alt\>\<Tab\> against the state, it does not matter
1726  *    whether Num Lock is active or not.  These relevant, or "significant",
1727  *    modifiers usually include Alt, Control, Shift, Super and similar.
1728  *    Effectively, this means that non-significant modifiers are masked out,
1729  *    before doing the comparison as described above.
1730  *
1731  * 3. The matching must be independent of the layout/keymap.  For example,
1732  *    the \<Plus\> (+) symbol is found on the first level on some layouts,
1733  *    but requires holding Shift on others.  If you simply bind the action
1734  *    to the \<Plus\> keysym, it would work for the unshifted kind, but
1735  *    not for the others, because the match against Shift would fail.  If
1736  *    you bind the action to \<Shift\>\<Plus\>, only the shifted kind would
1737  *    work.  So what is needed is to recognize that Shift is used up in the
1738  *    translation of the keysym itself, and therefore should not be included
1739  *    in the matching.
1740  *    Effectively, this means that consumed modifiers (Shift in this example)
1741  *    are masked out as well, before doing the comparison.
1742  *
1743  * In summary, this is approximately how the matching would be performed:
1744  * @code
1745  *   (keysym == shortcut_keysym) &&
1746  *   ((state_mods & ~consumed_mods & significant_mods) == shortcut_mods)
1747  * @endcode
1748  *
1749  * @c state_mods are the modifiers reported by
1750  * xkb_state_mod_index_is_active() and similar functions.
1751  * @c consumed_mods are the modifiers reported by
1752  * xkb_state_mod_index_is_consumed() and similar functions.
1753  * @c significant_mods are decided upon by the application/toolkit/user;
1754  * it is up to them to decide whether these are configurable or hard-coded.
1755  *
1756  * @endparblock
1757  */
1758
1759 /**
1760  * Consumed modifiers mode.
1761  *
1762  * There are several possible methods for deciding which modifiers are
1763  * consumed and which are not, each applicable for different systems or
1764  * situations. The mode selects the method to use.
1765  *
1766  * Keep in mind that in all methods, the keymap may decide to "preserve"
1767  * a modifier, meaning it is not reported as consumed even if it would
1768  * have otherwise.
1769  */
1770 enum xkb_consumed_mode {
1771     /**
1772      * This is the mode defined in the XKB specification and used by libX11.
1773      *
1774      * A modifier is consumed if and only if it *may affect* key translation.
1775      *
1776      * For example, if `Control+Alt+<Backspace>` produces some assigned keysym,
1777      * then when pressing just `<Backspace>`, `Control` and `Alt` are consumed,
1778      * even though they are not active, since if they *were* active they would
1779      * have affected key translation.
1780      */
1781     XKB_CONSUMED_MODE_XKB,
1782     /**
1783      * This is the mode used by the GTK+ toolkit.
1784      *
1785      * The mode consists of the following two independent heuristics:
1786      *
1787      * - The currently active set of modifiers, excluding modifiers which do
1788      *   not affect the key (as described for @ref XKB_CONSUMED_MODE_XKB), are
1789      *   considered consumed, if the keysyms produced when all of them are
1790      *   active are different from the keysyms produced when no modifiers are
1791      *   active.
1792      *
1793      * - A single modifier is considered consumed if the keysyms produced for
1794      *   the key when it is the only active modifier are different from the
1795      *   keysyms produced when no modifiers are active.
1796      */
1797     XKB_CONSUMED_MODE_GTK
1798 };
1799
1800 /**
1801  * Get the mask of modifiers consumed by translating a given key.
1802  *
1803  * @param state The keyboard state.
1804  * @param key   The keycode of the key.
1805  * @param mode  The consumed modifiers mode to use; see enum description.
1806  *
1807  * @returns a mask of the consumed modifiers.
1808  *
1809  * @memberof xkb_state
1810  * @since 0.7.0
1811  */
1812 xkb_mod_mask_t
1813 xkb_state_key_get_consumed_mods2(struct xkb_state *state, xkb_keycode_t key,
1814                                  enum xkb_consumed_mode mode);
1815
1816 /**
1817  * Same as xkb_state_key_get_consumed_mods2() with mode XKB_CONSUMED_MODE_XKB.
1818  *
1819  * @memberof xkb_state
1820  * @since 0.4.1
1821  */
1822 xkb_mod_mask_t
1823 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t key);
1824
1825 /**
1826  * Test whether a modifier is consumed by keyboard state translation for
1827  * a key.
1828  *
1829  * @param state The keyboard state.
1830  * @param key   The keycode of the key.
1831  * @param idx   The index of the modifier to check.
1832  * @param mode  The consumed modifiers mode to use; see enum description.
1833  *
1834  * @returns 1 if the modifier is consumed, 0 if it is not.  If the modifier
1835  * index is not valid in the keymap, returns -1.
1836  *
1837  * @sa xkb_state_mod_mask_remove_consumed()
1838  * @sa xkb_state_key_get_consumed_mods()
1839  * @memberof xkb_state
1840  * @since 0.7.0
1841  */
1842 int
1843 xkb_state_mod_index_is_consumed2(struct xkb_state *state,
1844                                  xkb_keycode_t key,
1845                                  xkb_mod_index_t idx,
1846                                  enum xkb_consumed_mode mode);
1847
1848 /**
1849  * Same as xkb_state_mod_index_is_consumed2() with mode XKB_CONSUMED_MOD_XKB.
1850  *
1851  * @memberof xkb_state
1852  * @since 0.4.1
1853  */
1854 int
1855 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t key,
1856                                 xkb_mod_index_t idx);
1857
1858 /**
1859  * Remove consumed modifiers from a modifier mask for a key.
1860  *
1861  * @deprecated Use xkb_state_key_get_consumed_mods2() instead.
1862  *
1863  * Takes the given modifier mask, and removes all modifiers which are
1864  * consumed for that particular key (as in xkb_state_mod_index_is_consumed()).
1865  *
1866  * @sa xkb_state_mod_index_is_consumed()
1867  * @memberof xkb_state
1868  */
1869 xkb_mod_mask_t
1870 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t key,
1871                                    xkb_mod_mask_t mask);
1872
1873 /**
1874  * Test whether a layout is active in a given keyboard state by name.
1875  *
1876  * @returns 1 if the layout is active, 0 if it is not.  If no layout with
1877  * this name exists in the keymap, return -1.
1878  *
1879  * If multiple layouts in the keymap have this name, the one with the lowest
1880  * index is tested.
1881  *
1882  * @sa xkb_layout_index_t
1883  * @memberof xkb_state
1884  */
1885 int
1886 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1887                                 enum xkb_state_component type);
1888
1889 /**
1890  * Test whether a layout is active in a given keyboard state by index.
1891  *
1892  * @returns 1 if the layout is active, 0 if it is not.  If the layout index
1893  * is not valid in the keymap, returns -1.
1894  *
1895  * @sa xkb_layout_index_t
1896  * @memberof xkb_state
1897  */
1898 int
1899 xkb_state_layout_index_is_active(struct xkb_state *state,
1900                                  xkb_layout_index_t idx,
1901                                  enum xkb_state_component type);
1902
1903 /**
1904  * Test whether a LED is active in a given keyboard state by name.
1905  *
1906  * @returns 1 if the LED is active, 0 if it not.  If no LED with this name
1907  * exists in the keymap, returns -1.
1908  *
1909  * @sa xkb_led_index_t
1910  * @memberof xkb_state
1911  */
1912 int
1913 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
1914
1915 /**
1916  * Test whether a LED is active in a given keyboard state by index.
1917  *
1918  * @returns 1 if the LED is active, 0 if it not.  If the LED index is not
1919  * valid in the keymap, returns -1.
1920  *
1921  * @sa xkb_led_index_t
1922  * @memberof xkb_state
1923  */
1924 int
1925 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
1926
1927 /** @} */
1928
1929 /* Leave this include last, so it can pick up our types, etc. */
1930 #include <xkbcommon/xkbcommon-compat.h>
1931
1932 #ifdef __cplusplus
1933 } /* extern "C" */
1934 #endif
1935
1936 #endif /* _XKBCOMMON_H_ */