Improve API doxygen documentation
[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  *
58  * Permission is hereby granted, free of charge, to any person obtaining a
59  * copy of this software and associated documentation files (the "Software"),
60  * to deal in the Software without restriction, including without limitation
61  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
62  * and/or sell copies of the Software, and to permit persons to whom the
63  * Software is furnished to do so, subject to the following conditions:
64  *
65  * The above copyright notice and this permission notice (including the next
66  * paragraph) shall be included in all copies or substantial portions of the
67  * Software.
68  *
69  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
72  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
74  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
75  * DEALINGS IN THE SOFTWARE.
76  *
77  * Author: Daniel Stone <daniel@fooishbar.org>
78  */
79
80 #ifndef _XKBCOMMON_H_
81 #define _XKBCOMMON_H_
82
83 #include <stdint.h>
84 #include <stdio.h>
85 #include <stdarg.h>
86
87 #include <xkbcommon/xkbcommon-names.h>
88 #include <xkbcommon/xkbcommon-keysyms.h>
89
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93
94 /**
95  * @file
96  * @brief Main libxkbcommon API.
97  */
98
99 /**
100  * @struct xkb_context
101  * @brief Opaque top level library context object.
102  *
103  * The context contains various general library data and state, like
104  * logging level and include paths.
105  * Objects are created in a specific context, and multiple contexts may
106  * coexist simultaneously. Objects from different contexts are completely
107  * separated and do not share any memory or state.
108  * A context is created, accessed, manipulated and destroyed through the
109  * xkb_context_*() API.
110  */
111 struct xkb_context;
112
113 /**
114  * @struct xkb_keymap
115  * @brief Opaque compiled XKB keymap object.
116  *
117  * The keymap object holds all of the static keyboard information obtained
118  * from compiling XKB files.
119  * A keymap is immutable after it is created (besides reference counts, etc.);
120  * if you need to change it, you must create a new one.
121  * A keymap object is created, accessed and destroyed through the
122  * xkb_keymap_*() API.
123  */
124 struct xkb_keymap;
125
126 /**
127  * @struct xkb_state
128  * @brief Opaque XKB keyboard state object.
129  *
130  * State objects contain the active state of a keyboard (or keyboards), such
131  * as the currently effective layout and the active modifiers. It acts as a
132  * simple state machine, wherein key presses and releases are the input, and
133  * key symbols (keysyms) are the output.
134  * A state object is created, accessed, manipulated and destroyed through the
135  * xkb_state_*() API.
136  */
137 struct xkb_state;
138
139 typedef uint32_t xkb_keycode_t;
140 typedef uint32_t xkb_keysym_t;
141 typedef uint32_t xkb_mod_index_t;
142 typedef uint32_t xkb_mod_mask_t;
143 typedef uint32_t xkb_layout_index_t;
144 typedef uint32_t xkb_layout_mask_t;
145 typedef uint32_t xkb_level_index_t;
146 typedef uint32_t xkb_led_index_t;
147 typedef uint32_t xkb_led_mask_t;
148
149 #define XKB_MOD_INVALID     (0xffffffff)
150 #define XKB_LAYOUT_INVALID  (0xffffffff)
151 #define XKB_KEYCODE_INVALID (0xffffffff)
152 #define XKB_LEVEL_INVALID   (0xffffffff)
153 #define XKB_LED_INVALID     (0xffffffff)
154
155 #define XKB_KEYCODE_MAX     (0xffffffff - 1)
156 #define xkb_keycode_is_legal_ext(kc) (kc <= XKB_KEYCODE_MAX)
157 #define xkb_keycode_is_legal_x11(kc) (kc >= 8 && kc <= 255)
158
159 /**
160  * @brief Names to compile a keymap with, also known as RMLVO.
161  *
162  * These names together are the primary identifier for a keymap.
163  * If any of the members is NULL or an empty string (""), a default value is
164  * used.
165  */
166 struct xkb_rule_names {
167     /** The rules file to use. The rules file describes how to interpret
168      *  the values of the model, layout, variant and options fields. */
169     const char *rules;
170     /** The keyboard model by which to interpret keycodes and LEDs. */
171     const char *model;
172     /** A comma seperated list of layouts (languages) to include in the
173      *  keymap. */
174     const char *layout;
175     /** A comma seperated list of variants, one per layout, which may
176      *  modify or augment the respective layout in various ways. */
177     const char *variant;
178     /** A comma seprated list of options, through which the user specifies
179      *  non-layout related preferences, like which key combinations are used
180      *  for switching layouts, or which key is the Compose key. */
181     const char *options;
182 };
183
184 /**
185  * @defgroup keysyms Keysyms
186  * @brief Utility functions related to keysyms.
187  *
188  * @{
189  */
190
191 /**
192  * @brief Get the name of a keysym.
193  *
194  * @param[in]  keysym The keysym.
195  * @param[out] buffer A string buffer to write the name into.
196  * @param[in]  size   Size of the buffer.
197  *
198  * @remark Named keysyms are found in the xkbcommon/xkbcommon-keysyms.h
199  * header file. Their name does not include the XKB_KEY_ prefix.
200  * The name of Unicode keysyms is "U<codepoint>", e.g. "Ua1b2".
201  * The name of other unnamed keysyms is the hexadecimal representation of
202  * their value, e.g. "0xabcd1234".
203  * An invalid keysym is returned as "Invalid".
204  *
205  * @warning If the buffer passed is too small, the string is truncated
206  * (though still NUL-terminated); a size of at least 32 bytes is recommended.
207  */
208 int
209 xkb_keysym_get_name(xkb_keysym_t keysym, char *buffer, size_t size);
210
211 /**
212  * @brief Get a keysym from its name.
213  *
214  * @param name The name of a keysym. See remarks in
215  * xkb_keysym_get_name(); this function will accept any name returned by that
216  * function.
217  *
218  * @remark The lookup is case-sensitive.
219  *
220  * @returns The keysym, if name is valid.  Otherwise, XKB_KEY_NoSymbol is
221  * returned.
222  */
223 xkb_keysym_t
224 xkb_keysym_from_name(const char *name);
225
226 /**
227  * @brief Get the Unicode/UTF-8 representation of a keysym.
228  *
229  * @param[in]  keysym The keysym.
230  * @param[out] buffer A buffer to write the UTF-8 string into.
231  * @param[in]  size   The size of buffer.  Must be at least 7.
232  *
233  * @returns The number of bytes written to the buffer.  A return value of
234  * 0 means that the keysym does not have a known printable Unicode
235  * representation.  A return value of -1 means that the buffer was too small.
236  */
237 int
238 xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size);
239
240 /**
241  * @brief Get the Unicode/UTF-32 representation of a keysym.
242  *
243  * @param keysym The keysym.
244  *
245  * @returns The Unicode/UTF-32 representation of keysym, which is also
246  * compatible with UCS-4.  A return value of 0 means the keysym does not
247  * have a known printable Unicode representation.
248  */
249 uint32_t
250 xkb_keysym_to_utf32(xkb_keysym_t keysym);
251
252 /** @} */
253
254 /**
255  * @defgroup context Library Context
256  * @brief Creating, destroying and using library contexts.
257  *
258  * Every keymap compilation request must have a context associated with
259  * it.  The context keeps around state such as the include path.
260  *
261  * @{
262  */
263
264 /** @brief Flags for context creation. */
265 enum xkb_context_flags {
266     /** Create this context with an empty include path. */
267     XKB_CONTEXT_NO_DEFAULT_INCLUDES = (1 << 0),
268 };
269
270 /**
271  * @brief Create a new context.
272  *
273  * @param flags Optional flags for the context, or 0.
274  *
275  * @returns A new context, or NULL on failure.
276  *
277  * @remark If successful, the caller holds a reference on the context, and
278  * must call xkb_context_unref() when finished.
279  *
280  * @remark The user may set some environment variables to affect default
281  * values in the context. See e.g. xkb_context_set_log_level() and
282  * xkb_context_set_log_verbosity().
283  */
284 struct xkb_context *
285 xkb_context_new(enum xkb_context_flags flags);
286
287 /**
288  * @brief Take a new reference on a context.
289  * @param context The context to reference.
290  * @returns The passed in context.
291  */
292 struct xkb_context *
293 xkb_context_ref(struct xkb_context *context);
294
295 /**
296  * @brief Release a reference on a context, and possibly free it.
297  * @param context The context to unreference.
298  */
299 void
300 xkb_context_unref(struct xkb_context *context);
301
302 /**
303  * @brief Append a new entry to the context's include path.
304  * @returns 1 on success, or 0 if the include path could not be added or is
305  * inaccessible.
306  */
307 int
308 xkb_context_include_path_append(struct xkb_context *context, const char *path);
309
310 /**
311  * @brief Append the default include paths to the context's include path.
312  * @returns 1 on success, or 0 if the primary include path could not be added.
313  */
314 int
315 xkb_context_include_path_append_default(struct xkb_context *context);
316
317 /**
318  * @brief Reset the context's include path to the default.
319  *
320  * Removes all entries from the context's include path, and inserts the
321  * default paths.
322  *
323  * @returns 1 on success, or 0 if the primary include path could not be added.
324  */
325 int
326 xkb_context_include_path_reset_defaults(struct xkb_context *context);
327
328 /**
329  * @brief Remove all entries from the context's include path.
330  */
331 void
332 xkb_context_include_path_clear(struct xkb_context *context);
333
334 /**
335  * @brief Get the number of paths in the context's include path.
336  * @returns The number of paths in the context's include path.
337  */
338 unsigned int
339 xkb_context_num_include_paths(struct xkb_context *context);
340
341 /**
342  * @brief Get a specific include path from the context's include path.
343  * @returns The include path at the specified index within the context, or
344  * NULL if the index is invalid.
345  */
346 const char *
347 xkb_context_include_path_get(struct xkb_context *context, unsigned int index);
348
349 /**
350  * @brief Store custom user data in the context.
351  *
352  * This may be useful in conjuction with xkb_context_set_log_fn() or other
353  * callbacks.
354  */
355 void
356 xkb_context_set_user_data(struct xkb_context *context, void *user_data);
357
358 /**
359  * @brief Retrieves stored user data from the context.
360  *
361  * @returns The stored user data.  If the user data wasn't set, or the
362  * passed in context is NULL, returns NULL.
363  *
364  * This may be useful to access private user data from callbacks like a
365  * custom logging function.
366  **/
367 void *
368 xkb_context_get_user_data(struct xkb_context *context);
369
370 /** @} */
371
372 /**
373  * @defgroup logging Logging Handling
374  * @brief Manipulating how logging from this library is handled.
375  *
376  * @{
377  */
378
379 /** @brief Specifies a logging level. */
380 enum xkb_log_level {
381     XKB_LOG_LEVEL_CRITICAL = 10, /**< Log critical internal errors only. */
382     XKB_LOG_LEVEL_ERROR = 20,    /**< Log all errors. */
383     XKB_LOG_LEVEL_WARNING = 30,  /**< Log warnings and errors. */
384     XKB_LOG_LEVEL_INFO = 40,     /**< Log information, warnings, and errors. */
385     XKB_LOG_LEVEL_DEBUG = 50,    /**< Log everything. */
386 };
387
388 /**
389  * @brief Set the current logging level.
390  *
391  * @param context The context in which to set the logging level.
392  * @param level   The logging level to use.  Only messages from this level
393  * and below will be logged.
394  *
395  * The default level is XKB_LOG_LEVEL_ERROR.  The environment variable
396  * XKB_LOG, if set in the time the context was created, overrides the default
397  * value.  It may be specified as a level number or name.
398  */
399 void
400 xkb_context_set_log_level(struct xkb_context *context,
401                           enum xkb_log_level level);
402
403 /**
404  * @brief Get the current logging level.
405  * @returns The current logging level.
406  */
407 enum xkb_log_level
408 xkb_context_get_log_level(struct xkb_context *context);
409
410 /**
411  * @brief Sets the current logging verbosity.
412  *
413  * The library can generate a number of warnings which are not helpful to
414  * ordinary users of the library.  The verbosity may be increased if more
415  * information is desired (e.g. when developing a new keymap).
416  *
417  * The default verbosity is 0.  The environment variable XKB_VERBOSITY, if
418  * set in the time the context was created, overrdies the default value.
419  *
420  * @param context   The context in which to use the set verbosity.
421  * @param verbosity The verbosity to use.  Currently used values are
422  * 1 to 10, higher values being more verbose.  0 would result in no verbose
423  * messages being logged.
424  *
425  * @remark Most verbose messages are of level XKB_LOG_LEVEL_WARNING or lower.
426  */
427 void
428 xkb_context_set_log_verbosity(struct xkb_context *context, int verbosity);
429
430 /**
431  * @brief Get the current logging verbosity of the context.
432  * @returns The current logging verbosity.
433  */
434 int
435 xkb_context_get_log_verbosity(struct xkb_context *context);
436
437 /**
438  * @brief Set a custom function to handle logging messages.
439  *
440  * @param context The context in which to use the set logging function.
441  * @param log_fn  The function that will be called for logging messages.
442  * Passing NULL restores the default function, which logs to stderr.
443  *
444  * By default, log messages from this library are printed to stderr.  This
445  * function allows you to replace the default behavior with a custom
446  * handler.  The handler is only called with messages which match the
447  * current logging level and verbosity settings for the context.
448  * level is the logging level of the message.  format and args are the
449  * same as in the vprintf(3) function.
450  *
451  * You may use xkb_context_set_user_data() on the context, and then call
452  * xkb_context_get_user_data() from within the logging function to provide
453  * it with additional private context.
454  */
455 void
456 xkb_context_set_log_fn(struct xkb_context *context,
457                        void (*log_fn)(struct xkb_context *context,
458                                       enum xkb_log_level level,
459                                       const char *format, va_list args));
460
461 /** @} */
462
463 /**
464  * @defgroup keymap Keymap Creation
465  * @brief Creating and destroying XKB keymaps.
466  *
467  * @{
468  */
469
470 /** @brief Flags for keymap compilation. */
471 enum xkb_keymap_compile_flags {
472     /** Apparently you can't have empty enums.  What a drag. */
473     XKB_MAP_COMPILE_PLACEHOLDER = 0,
474 };
475
476 /**
477  * @brief Create a keymap from RMLVO names.
478  *
479  * The primary keymap entry point: creates a new XKB keymap from a set of
480  * RMLVO (Rules + Model + Layouts + Variants + Options) names.
481  *
482  * You should almost certainly be using this and nothing else to create
483  * keymaps.
484  *
485  * @param context The context in which to create the keymap.
486  * @param names   The RMLVO names to use.
487  * @param flags   Optional flags for the keymap, or 0.
488  *
489  * @returns A keymap compiled according to the RMLVO names, or NULL if
490  * the compilation failed.
491  *
492  * @sa xkb_rule_names
493  */
494 struct xkb_keymap *
495 xkb_keymap_new_from_names(struct xkb_context *context,
496                           const struct xkb_rule_names *names,
497                           enum xkb_keymap_compile_flags flags);
498
499 /** @brief The possible keymap text formats. */
500 enum xkb_keymap_format {
501     /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
502     XKB_KEYMAP_FORMAT_TEXT_V1 = 1,
503 };
504
505 /**
506  * @brief Create a keymap from an XKB keymap file.
507  *
508  * @param context The context in which to create the keymap.
509  * @param file    The XKB keymap file to compile.
510  * @param format  The text format of the XKB keymap file to compile.
511  * @param flags   Optional flags for the keymap, or 0.
512  *
513  * @returns A keymap compiled from the given XKB keymap file, or NULL of
514  * the compilation failed.
515  *
516  * @remark The file must contain an entire XKB keymap.  For example, in the
517  * XKB_KEYMAP_FORMAT_TEXT_V1 format, this means the file must contain one
518  * top level '%xkb_keymap' section, which in turn contains other required
519  * sections.
520  */
521 struct xkb_keymap *
522 xkb_keymap_new_from_file(struct xkb_context *context, FILE *file,
523                          enum xkb_keymap_format format,
524                          enum xkb_keymap_compile_flags flags);
525
526 /**
527  * @brief Create a keymap from an XKB keymap given as a string.
528  *
529  * This is just like xkb_keymap_new_from_file(), but instead of a file, gets
530  * the XKB keymap as one enormous string.
531  *
532  * @see xkb_keymap_new_from_string()
533  */
534 struct xkb_keymap *
535 xkb_keymap_new_from_string(struct xkb_context *context, const char *string,
536                            enum xkb_keymap_format format,
537                            enum xkb_keymap_compile_flags flags);
538
539 /**
540  * @brief Take a new reference on a keymap.
541  * @param keymap The keymap to reference.
542  * @returns The passed in keymap.
543  */
544 struct xkb_keymap *
545 xkb_keymap_ref(struct xkb_keymap *keymap);
546
547 /**
548  * @brief Release a reference on a keymap, and possibly free it.
549  * @param keymap The keymap to unreference. If the reference count reaches
550  * zero, the keymap is freed.
551  */
552 void
553 xkb_keymap_unref(struct xkb_keymap *keymap);
554
555 /**
556  * @brief Get the compiled keymap as a string.
557  *
558  * @returns The compiled keymap as a NUL-terminated string, or NULL if
559  * unsuccessful.
560  *
561  * The returned string may be fed back into xkb_map_new_from_string() to get
562  * the exact same keymap (possibly in another process, etc.).
563  *
564  * @remark The returned string is dynamically allocated and should be freed
565  * by the caller.
566  */
567 char *
568 xkb_keymap_get_as_string(struct xkb_keymap *keymap);
569
570 /** @} */
571
572 /**
573  * @defgroup components XKB State Components
574  * @brief Enumeration of state components in a keymap.
575  *
576  * @{
577  */
578
579 /**
580  * Returns the number of modifiers active in the keymap.
581  */
582 xkb_mod_index_t
583 xkb_keymap_num_mods(struct xkb_keymap *keymap);
584
585 /**
586  * Returns the name of the modifier specified by 'idx', or NULL if invalid.
587  */
588 const char *
589 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx);
590
591 /**
592  * Returns the index of the modifier specified by 'name', or XKB_MOD_INVALID.
593  */
594 xkb_mod_index_t
595 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name);
596
597 /**
598  * Returns the number of groups active in the keymap.
599  */
600 xkb_layout_index_t
601 xkb_keymap_num_layouts(struct xkb_keymap *keymap);
602
603 /**
604  * Returns the name of the group specified by 'idx', or NULL if invalid.
605  */
606 const char *
607 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx);
608
609 /**
610  * Returns the index of the layout specified by 'name', or XKB_LAYOUT_INVALID.
611  */
612 xkb_layout_index_t
613 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name);
614
615 /**
616  * Returns the number of layouts active for the specified key.
617  */
618 xkb_layout_index_t
619 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t key);
620
621 /**
622  * Returns the number of levels active for the specified key and layout.
623  */
624 xkb_level_index_t
625 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t key,
626                               xkb_layout_index_t layout);
627
628 /**
629  * Returns the number of LEDs in the given map.
630  */
631 xkb_led_index_t
632 xkb_keymap_num_leds(struct xkb_keymap *keymap);
633
634 /**
635  * Returns the name of the LED specified by 'idx', or NULL if invalid.
636  */
637 const char *
638 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx);
639
640 /**
641  * Returns the index of the LED specified by 'name', or XKB_LED_INVALID.
642  */
643 xkb_led_index_t
644 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name);
645
646 /**
647  * Returns 1 if the key should repeat, or 0 otherwise.
648  */
649 int
650 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t key);
651
652 /** @} */
653
654 /**
655  * @defgroup state XKB State Objects
656  * @brief Creating, destroying and manipulating keyboard state objects.
657  *
658  * @{
659  */
660
661 /**
662  * Returns a new XKB state object for use with the given keymap, or NULL on
663  * failure.
664  */
665 struct xkb_state *
666 xkb_state_new(struct xkb_keymap *keymap);
667
668 /**
669  * Takes a new reference on a state object.
670  */
671 struct xkb_state *
672 xkb_state_ref(struct xkb_state *state);
673
674 /**
675  * Unrefs and potentially deallocates a state object; the caller must not
676  * use the state object after calling this.
677  */
678 void
679 xkb_state_unref(struct xkb_state *state);
680
681 /**
682  * Get the keymap from which the state object was created.  Does not take
683  * a new reference on the map; you must explicitly reference it yourself
684  * if you plan to use it beyond the lifetime of the state.
685  */
686 struct xkb_keymap *
687 xkb_state_get_keymap(struct xkb_state *state);
688
689 /** @brief Specifies the direction of the key (press / release). */
690 enum xkb_key_direction {
691     XKB_KEY_UP,   /**< The key was released. */
692     XKB_KEY_DOWN, /**< The key was pressed. */
693 };
694
695 /**
696  * Updates a state object to reflect the given key being pressed or released.
697  */
698 void
699 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
700                      enum xkb_key_direction direction);
701
702 /**
703  * Gives the symbols obtained from pressing a particular key with the given
704  * state.  *syms_out will be set to point to an array of keysyms, with the
705  * return value being the number of symbols in *syms_out.  If the return
706  * value is 0, *syms_out will be set to NULL, as there are no symbols produced
707  * by this event.
708  */
709 int
710 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
711                        const xkb_keysym_t **syms_out);
712
713 /**
714  * Returns the layout number that would be active for a particular key with
715  * the given state.
716  */
717 xkb_layout_index_t
718 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t key);
719
720 /**
721  * Returns the level number that would be active for a particular key with
722  * the given state and layout number, usually obtained from
723  * xkb_state_key_get_layout.
724  */
725 xkb_level_index_t
726 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t key,
727                         xkb_layout_index_t layout);
728
729 /**
730  * Gives the symbols obtained from pressing a particular key with the given
731  * layout and level.  *syms_out will be set to point to an array of keysyms,
732  * with the return value being the number of symbols in *syms_out.  If the
733  * return value is 0, *syms_out will be set to NULL, as there are no symbols
734  * produced by this event.
735  */
736 int
737 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
738                                  xkb_keycode_t key,
739                                  xkb_layout_index_t layout,
740                                  xkb_level_index_t level,
741                                  const xkb_keysym_t **syms_out);
742
743 /**
744  * Modifier and group types for state objects.  This enum is bitmaskable,
745  * e.g. (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED) is valid to exclude
746  * locked modifiers.
747  */
748 enum xkb_state_component {
749     /** A key holding this modifier or group is currently physically
750      *  depressed; also known as 'base'. */
751     XKB_STATE_DEPRESSED = (1 << 0),
752     /** Modifier or group is latched, i.e. will be unset after the next
753      *  non-modifier key press. */
754     XKB_STATE_LATCHED = (1 << 1),
755     /** Modifier or group is locked, i.e. will be unset after the key
756      *  provoking the lock has been pressed again. */
757     XKB_STATE_LOCKED = (1 << 2),
758     /** Combinatination of depressed, latched, and locked. */
759     XKB_STATE_EFFECTIVE =
760         (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED | XKB_STATE_LOCKED),
761 };
762
763 /**
764  * Match flags for xkb_state_mod_indices_are_active and
765  * xkb_state_mod_names_are_active, specifying how the conditions for a
766  * successful match.  XKB_STATE_MATCH_NON_EXCLUSIVE is bitmaskable with
767  * the other modes.
768  */
769 enum xkb_state_match {
770     /** Returns true if any of the modifiers are active. */
771     XKB_STATE_MATCH_ANY = (1 << 0),
772     /** Returns true if all of the modifiers are active. */
773     XKB_STATE_MATCH_ALL = (1 << 1),
774     /** Makes matching non-exclusive, i.e. will not return false if a
775      *  modifier not specified in the arguments is active. */
776     XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16),
777 };
778
779 /**
780  * Updates a state object from a set of explicit masks.  This entrypoint is
781  * really only for window systems and the like, where a master process
782  * holds an xkb_state, then serializes it over a wire protocol, and clients
783  * then use the serialization to feed in to their own xkb_state.
784  *
785  * All parameters must always be passed, or the resulting state may be
786  * incoherent.
787  *
788  * The serialization is lossy and will not survive round trips; it must only
789  * be used to feed slave state objects, and must not be used to update the
790  * master state.
791  *
792  * Please do not use this unless you fit the description above.
793  */
794 void
795 xkb_state_update_mask(struct xkb_state *state, xkb_mod_mask_t base_mods,
796                       xkb_mod_mask_t latched_mods, xkb_mod_mask_t locked_mods,
797                       xkb_layout_index_t base_group,
798                       xkb_layout_index_t latched_group,
799                       xkb_layout_index_t locked_group);
800
801 /**
802  * The counterpart to xkb_state_update_mask, to be used on the server side
803  * of serialization.  Returns a xkb_mod_mask_t representing the given
804  * component(s) of the state.
805  *
806  * This function should not be used in regular clients; please use the
807  * xkb_state_mod_*_is_active or xkb_state_foreach_active_mod API instead.
808  *
809  * Can return NULL on failure.
810  */
811 xkb_mod_mask_t
812 xkb_state_serialize_mods(struct xkb_state *state,
813                          enum xkb_state_component component);
814
815 /**
816  * The group equivalent of xkb_state_serialize_mods: please see its
817  * documentation.
818  */
819 xkb_layout_index_t
820 xkb_state_serialize_layout(struct xkb_state *state,
821                            enum xkb_state_component component);
822
823 /**
824  * Returns 1 if the modifier specified by 'name' is active in the manner
825  * specified by 'type', 0 if it is unset, or -1 if the modifier does not
826  * exist in the map.
827  */
828 int
829 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
830                              enum xkb_state_component type);
831
832 /**
833  * Returns 1 if the modifiers specified by the varargs (NULL-terminated
834  * strings, with a NULL sentinel) are active in the manner specified by
835  * 'match', 0 otherwise, or -1 if any of the modifiers do not exist in
836  * the map.
837  */
838 int
839 xkb_state_mod_names_are_active(struct xkb_state *state,
840                                enum xkb_state_component type,
841                                enum xkb_state_match match,
842                                ...);
843
844 /**
845  * Returns 1 if the modifier specified by 'idx' is active in the manner
846  * specified by 'type', 0 if it is unset, or -1 if the modifier does not
847  * exist in the current map.
848  */
849 int
850 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
851                               enum xkb_state_component type);
852
853 /**
854  * Returns 1 if the modifiers specified by the varargs (of type
855  * xkb_mod_index_t, with a XKB_MOD_INVALID sentinel) are active in the
856  * manner specified by 'match' and 'type', 0 otherwise, or -1 if any of
857  * the modifiers do not exist in the map.
858  */
859 int
860 xkb_state_mod_indices_are_active(struct xkb_state *state,
861                                  enum xkb_state_component type,
862                                  enum xkb_state_match match,
863                                  ...);
864
865 /**
866  * Returns 1 if the modifier specified by 'idx' is used in the
867  * translation of the keycode 'key' to the key symbols obtained by
868  * pressing it (as in xkb_key_get_syms), given the current state.
869  * Returns 0 otherwise.
870  */
871 int
872 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t key,
873                                 xkb_mod_index_t idx);
874
875 /**
876  * Takes the given modifier mask, and removes all modifiers which are
877  * marked as 'consumed' (see xkb_key_mod_index_is_consumed definition)
878  * for that particular key.
879  */
880 xkb_mod_mask_t
881 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t key,
882                                    xkb_mod_mask_t mask);
883
884 /**
885  * Returns 1 if the group specified by 'name' is active in the manner
886  * specified by 'type', 0 if it is unset, or -1 if the group does not
887  * exist in the current map.
888  */
889 int
890 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
891                                 enum xkb_state_component type);
892
893 /**
894  * Returns 1 if the group specified by 'idx' is active in the manner
895  * specified by 'type', 0 if it is unset, or -1 if the group does not
896  * exist in the current map.
897  */
898 int
899 xkb_state_layout_index_is_active(struct xkb_state *state,
900                                  xkb_layout_index_t idx,
901                                  enum xkb_state_component type);
902
903 /**
904  * Returns 1 if the LED specified by 'name' is active, 0 if it is unset, or
905  * -1 if the LED does not exist in the current map.
906  */
907 int
908 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
909
910 /**
911  * Returns 1 if the LED specified by 'idx' is active, 0 if it is unset, or
912  * -1 if the LED does not exist in the current map.
913  */
914 int
915 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
916
917 /** @} */
918
919 /* Leave this include last, so it can pick up our types, etc. */
920 #include <xkbcommon/xkbcommon-compat.h>
921
922 #ifdef __cplusplus
923 } /* extern "C" */
924 #endif
925
926 #endif /* _XKBCOMMON_H_ */