2 * Copyright © 2020 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #ifndef _XKBREGISTRY_H_
26 #define _XKBREGISTRY_H_
33 * @brief Query for available RMLVO
42 * @defgroup registry Query for available RMLVO
44 * The libxkbregistry API to query for available rules, models, layouts,
45 * variants and options (RMLVO). libxkbregistry is a separate library to
48 * This library is the replacement for clients currently parsing evdev.xml
49 * directly. The library is intended to provide easy access to the set of
50 * **possible** MLVO configurations for a given ruleset. It is not a library to
51 * apply these configurations, merely to enumerate them. The intended users of
52 * this library are the configuration UIs that allow a user to select their
53 * keyboard layout of choice.
59 * @struct rxkb_context
61 * Opaque top level library context object.
63 * The context contains general library state, like include paths and parsed
64 * data. Objects are created in a specific context, and multiple contexts
65 * may coexist simultaneously. Objects from different contexts are
66 * completely separated and do not share any memory or state.
73 * Opaque struct representing an XKB model.
80 * Opaque struct representing an XKB layout, including an optional variant.
81 * Where the variant is NULL, the layout is the base layout.
83 * For example, "us" is the base layout, "us(intl)" is the "intl" variant of the
89 * @struct rxkb_option_group
91 * Opaque struct representing an option group. Option groups divide the
92 * individual options into logical groups. Their main purpose is to indicate
93 * whether some options are mutually exclusive or not.
95 struct rxkb_option_group;
100 * Opaque struct representing an XKB option. Options are grouped inside an @ref
107 * @struct rxkb_iso639_code
109 * Opaque struct representing an ISO 639-3 code (e.g. "eng", "fra"). There
110 * is no guarantee that two identical ISO codes share the same struct. You
111 * must not rely on the pointer value of this struct.
113 * See https://iso639-3.sil.org/code_tables/639/data for a list of codes.
115 struct rxkb_iso639_code;
119 * @struct rxkb_iso3166_code
121 * Opaque struct representing an ISO 3166 Alpha 2 code (e.g. "US", "FR").
122 * There is no guarantee that two identical ISO codes share the same struct.
123 * You must not rely on the pointer value of this struct.
125 * See https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes for a list
128 struct rxkb_iso3166_code;
131 * Describes the popularity of an item. Historically, some highly specialized or
132 * experimental definitions are excluded from the default list and shipped in
133 * separate files. If these extra definitions are loaded (see @ref
134 * RXKB_CONTEXT_LOAD_EXOTIC_RULES), the popularity of the item is set
137 * If the exotic items are not loaded, all items will have the standard
140 enum rxkb_popularity {
141 RXKB_POPULARITY_STANDARD = 1,
142 RXKB_POPULARITY_EXOTIC,
146 * Flags for context creation.
148 enum rxkb_context_flags {
149 RXKB_CONTEXT_NO_FLAGS = 0,
151 * Skip the default include paths. This requires the caller to call
152 * rxkb_context_include_path_append() or
153 * rxkb_context_include_path_append_default().
155 RXKB_CONTEXT_NO_DEFAULT_INCLUDES = (1 << 0),
157 * Load the extra items that are considered too exotic for the default list.
159 * For historical reasons, xkeyboard-config ships those exotic rules in a
160 * separate file (e.g. `evdev.extras.xml`). Where the exotic rules are
161 * requested, libxkbregistry will look for and load `$ruleset.extras.xml`
162 * in the include paths, see rxkb_context_include_path_append() for details
163 * on the lookup behavior.
165 RXKB_CONTEXT_LOAD_EXOTIC_RULES = (1 << 1),
167 * Disable the use of secure_getenv for this context, so that privileged
168 * processes can use environment variables. Client uses at their own risk.
172 RXKB_CONTEXT_NO_SECURE_GETENV = (1 << 2)
176 * Create a new xkb registry context.
178 * The context has an initial refcount of 1. Use rxkb_context_unref() to release
179 * memory associated with this context.
181 * Creating a context does not parse the files yet, use
182 * rxkb_context_parse().
184 * @param flags Flags affecting context behavior
185 * @return A new xkb registry context or NULL on failure
187 struct rxkb_context *
188 rxkb_context_new(enum rxkb_context_flags flags);
190 /** Specifies a logging level. */
191 enum rxkb_log_level {
192 RXKB_LOG_LEVEL_CRITICAL = 10, /**< Log critical internal errors only. */
193 RXKB_LOG_LEVEL_ERROR = 20, /**< Log all errors. */
194 RXKB_LOG_LEVEL_WARNING = 30, /**< Log warnings and errors. */
195 RXKB_LOG_LEVEL_INFO = 40, /**< Log information, warnings, and errors. */
196 RXKB_LOG_LEVEL_DEBUG = 50 /**< Log everything. */
200 * Set the current logging level.
202 * @param ctx The context in which to set the logging level.
203 * @param level The logging level to use. Only messages from this level
204 * and below will be logged.
206 * The default level is RXKB_LOG_LEVEL_ERROR. The environment variable
207 * RXKB_LOG_LEVEL, if set at the time the context was created, overrides the
208 * default value. It may be specified as a level number or name.
211 rxkb_context_set_log_level(struct rxkb_context *ctx,
212 enum rxkb_log_level level);
215 * Get the current logging level.
218 rxkb_context_get_log_level(struct rxkb_context *ctx);
221 * Set a custom function to handle logging messages.
223 * @param ctx The context in which to use the set logging function.
224 * @param log_fn The function that will be called for logging messages.
225 * Passing NULL restores the default function, which logs to stderr.
227 * By default, log messages from this library are printed to stderr. This
228 * function allows you to replace the default behavior with a custom
229 * handler. The handler is only called with messages which match the
230 * current logging level and verbosity settings for the context.
231 * level is the logging level of the message. @a format and @a args are
232 * the same as in the vprintf(3) function.
234 * You may use rxkb_context_set_user_data() on the context, and then call
235 * rxkb_context_get_user_data() from within the logging function to provide
236 * it with additional private context.
239 rxkb_context_set_log_fn(struct rxkb_context *ctx,
240 void (*log_fn)(struct rxkb_context *ctx,
241 enum rxkb_log_level level,
242 const char *format, va_list args));
246 * Parse the given ruleset. This can only be called once per context and once
247 * parsed the data in the context is considered constant and will never
250 * This function parses all files with the given ruleset name. See
251 * rxkb_context_include_path_append() for details.
253 * If this function returns false, libxkbregistry failed to parse the xml files.
254 * This is usually caused by invalid files on the host and should be debugged by
255 * the host's administrator using external tools. Callers should reduce the
256 * include paths to known good paths and/or fall back to a default RMLVO set.
258 * If this function returns false, the context should be be considered dead and
259 * must be released with rxkb_context_unref().
261 * @param ctx The xkb registry context
262 * @param ruleset The ruleset to parse, e.g. "evdev"
263 * @return true on success or false on failure
266 rxkb_context_parse(struct rxkb_context *ctx, const char *ruleset);
269 * Parse the default ruleset as configured at build time. See
270 * rxkb_context_parse() for details.
273 rxkb_context_parse_default_ruleset(struct rxkb_context *ctx);
276 * Increases the refcount of this object by one and returns the object.
278 * @param ctx The xkb registry context
279 * @return The passed in object
282 rxkb_context_ref(struct rxkb_context *ctx);
285 * Decreases the refcount of this object by one. Where the refcount of an
286 * object hits zero, associated resources will be freed.
288 * @param ctx The xkb registry context
289 * @return always NULL
292 rxkb_context_unref(struct rxkb_context *ctx);
295 * Assign user-specific data. libxkbregistry will not look at or modify the
296 * data, it will merely return the same pointer in
297 * rxkb_context_get_user_data().
299 * @param ctx The xkb registry context
300 * @param user_data User-specific data pointer
303 rxkb_context_set_user_data(struct rxkb_context *ctx, void *user_data);
306 * Return the pointer passed into rxkb_context_get_user_data().
308 * @param ctx The xkb registry context
309 * @return User-specific data pointer
312 rxkb_context_get_user_data(struct rxkb_context *ctx);
315 * Append a new entry to the context's include path.
317 * The include path handling is optimized for the most common use-case: a set of
318 * system files that provide a complete set of MLVO and some
319 * custom MLVO provided by a user **in addition** to the system set.
321 * The include paths should be given so that the least complete path is
322 * specified first and the most complete path is appended last. For example:
325 * ctx = rxkb_context_new(RXKB_CONTEXT_NO_DEFAULT_INCLUDES);
326 * rxkb_context_include_path_append(ctx, "/home/user/.config/xkb");
327 * rxkb_context_include_path_append(ctx, "/usr/share/X11/xkb");
328 * rxkb_context_parse(ctx, "evdev");
331 * The above example reflects the default behavior unless @ref
332 * RXKB_CONTEXT_NO_DEFAULT_INCLUDES is provided.
334 * Loading of the files is in **reverse order**, i.e. the last path appended is
335 * loaded first - in this case the ``/usr/share/X11/xkb`` path.
336 * Any models, layouts, variants and options defined in the "evdev" ruleset
337 * are loaded into the context. Then, any RMLVO found in the "evdev" ruleset of
338 * the user's path (``/home/user/.config/xkb`` in this example) are **appended**
339 * to the existing set.
341 * Note that data from previously loaded include paths is never overwritten,
342 * only appended to. It is not not possible to change the system-provided data,
343 * only to append new models, layouts, variants and options to it.
345 * In other words, to define a new variant of the "us" layout called "banana",
346 * the following XML is sufficient.
349 * <xkbConfigRegistry version="1.1">
358 * <name>banana</name>
359 * <description>English (Banana)</description>
364 * </xkbConfigRegistry>
367 * The list of models, options and all other layouts (including "us" and its
368 * variants) is taken from the system files. The resulting list of layouts will
369 * thus have a "us" keyboard layout with the variant "banana" and all other
370 * system-provided variants (dvorak, colemak, intl, etc.)
372 * This function must be called before rxkb_context_parse() or
373 * rxkb_context_parse_default_ruleset().
375 * @returns true on success, or false if the include path could not be added
376 * or is inaccessible.
379 rxkb_context_include_path_append(struct rxkb_context *ctx, const char *path);
382 * Append the default include paths to the context's include path.
383 * See rxkb_context_include_path_append() for details about the merge order.
385 * This function must be called before rxkb_context_parse() or
386 * rxkb_context_parse_default_ruleset().
388 * @returns true on success, or false if the include path could not be added
389 * or is inaccessible.
392 rxkb_context_include_path_append_default(struct rxkb_context *ctx);
395 * Return the first model for this context. Use this to start iterating over
396 * the models, followed by calls to rxkb_model_next(). Models are not sorted.
398 * The refcount of the returned model is not increased. Use rxkb_model_ref() if
399 * you need to keep this struct outside the immediate scope.
401 * @return The first model in the model list.
404 rxkb_model_first(struct rxkb_context *ctx);
407 * Return the next model for this context. Returns NULL when no more models
410 * The refcount of the returned model is not increased. Use rxkb_model_ref() if
411 * you need to keep this struct outside the immediate scope.
413 * @return the next model or NULL at the end of the list
416 rxkb_model_next(struct rxkb_model *m);
419 * Increase the refcount of the argument by one.
421 * @returns The argument passed in to this function.
424 rxkb_model_ref(struct rxkb_model *m);
427 * Decrease the refcount of the argument by one. When the refcount hits zero,
428 * all memory associated with this struct is freed.
430 * @returns always NULL
433 rxkb_model_unref(struct rxkb_model *m);
436 * Return the name of this model. This is the value for M in RMLVO, to be used
440 rxkb_model_get_name(struct rxkb_model *m);
443 * Return a human-readable description of this model. This function may return
447 rxkb_model_get_description(struct rxkb_model *m);
450 * Return the vendor name for this model. This function may return NULL.
453 rxkb_model_get_vendor(struct rxkb_model *m);
456 * Return the popularity for this model.
459 rxkb_model_get_popularity(struct rxkb_model *m);
462 * Return the first layout for this context. Use this to start iterating over
463 * the layouts, followed by calls to rxkb_layout_next(). Layouts are not sorted.
465 * The refcount of the returned layout is not increased. Use rxkb_layout_ref() if
466 * you need to keep this struct outside the immediate scope.
468 * @return The first layout in the layout list.
471 rxkb_layout_first(struct rxkb_context *ctx);
474 * Return the next layout for this context. Returns NULL when no more layouts
477 * The refcount of the returned layout is not increased. Use rxkb_layout_ref()
478 * if you need to keep this struct outside the immediate scope.
480 * @return the next layout or NULL at the end of the list
483 rxkb_layout_next(struct rxkb_layout *l);
486 * Increase the refcount of the argument by one.
488 * @returns The argument passed in to this function.
491 rxkb_layout_ref(struct rxkb_layout *l);
494 * Decrease the refcount of the argument by one. When the refcount hits zero,
495 * all memory associated with this struct is freed.
497 * @returns always NULL
500 rxkb_layout_unref(struct rxkb_layout *l);
503 * Return the name of this layout. This is the value for L in RMLVO, to be used
507 rxkb_layout_get_name(struct rxkb_layout *l);
510 * Return the variant of this layout. This is the value for V in RMLVO, to be
511 * used with libxkbcommon.
513 * A variant does not stand on its own, it always depends on the base layout.
514 * e.g. there may be multiple variants called "intl" but there is only one
517 * Where the variant is NULL, the layout is the base layout (e.g. "us").
520 rxkb_layout_get_variant(struct rxkb_layout *l);
523 * Return a short (one-word) description of this layout. This function may
527 rxkb_layout_get_brief(struct rxkb_layout *l);
530 * Return a human-readable description of this layout. This function may return
534 rxkb_layout_get_description(struct rxkb_layout *l);
537 * Return the popularity for this layout.
540 rxkb_layout_get_popularity(struct rxkb_layout *l);
543 * Return the first option group for this context. Use this to start iterating
544 * over the option groups, followed by calls to rxkb_option_group_next().
545 * Option groups are not sorted.
547 * The refcount of the returned option group is not increased. Use
548 * rxkb_option_group_ref() if you need to keep this struct outside the immediate
551 * @return The first option group in the option group list.
553 struct rxkb_option_group *
554 rxkb_option_group_first(struct rxkb_context *ctx);
557 * Return the next option group for this context. Returns NULL when no more
558 * option groups are available.
560 * The refcount of the returned option group is not increased. Use
561 * rxkb_option_group_ref() if you need to keep this struct outside the immediate
564 * @return the next option group or NULL at the end of the list
566 struct rxkb_option_group *
567 rxkb_option_group_next(struct rxkb_option_group *g);
570 * Increase the refcount of the argument by one.
572 * @returns The argument passed in to this function.
574 struct rxkb_option_group *
575 rxkb_option_group_ref(struct rxkb_option_group *g);
578 * Decrease the refcount of the argument by one. When the refcount hits zero,
579 * all memory associated with this struct is freed.
581 * @returns always NULL
583 struct rxkb_option_group *
584 rxkb_option_group_unref(struct rxkb_option_group *g);
587 * Return the name of this option group. This is **not** the value for O in
588 * RMLVO, the name can be used for internal sorting in the caller. This function
592 rxkb_option_group_get_name(struct rxkb_option_group *m);
595 * Return a human-readable description of this option group. This function may
599 rxkb_option_group_get_description(struct rxkb_option_group *m);
602 * @return true if multiple options within this option group can be selected
603 * simultaneously, false if all options within this option group
604 * are mutually exclusive.
607 rxkb_option_group_allows_multiple(struct rxkb_option_group *g);
610 * Return the popularity for this option group.
613 rxkb_option_group_get_popularity(struct rxkb_option_group *g);
616 * Return the first option for this option group. Use this to start iterating
617 * over the options, followed by calls to rxkb_option_next(). Options are not
620 * The refcount of the returned option is not increased. Use rxkb_option_ref()
621 * if you need to keep this struct outside the immediate scope.
623 * @return The first option in the option list.
626 rxkb_option_first(struct rxkb_option_group *group);
629 * Return the next option for this option group. Returns NULL when no more
630 * options are available.
632 * The refcount of the returned options is not increased. Use rxkb_option_ref()
633 * if you need to keep this struct outside the immediate scope.
635 * @returns The next option or NULL at the end of the list
638 rxkb_option_next(struct rxkb_option *o);
641 * Increase the refcount of the argument by one.
643 * @returns The argument passed in to this function.
646 rxkb_option_ref(struct rxkb_option *o);
649 * Decrease the refcount of the argument by one. When the refcount hits zero,
650 * all memory associated with this struct is freed.
652 * @returns always NULL
655 rxkb_option_unref(struct rxkb_option *o);
658 * Return the name of this option. This is the value for O in RMLVO, to be used
662 rxkb_option_get_name(struct rxkb_option *o);
665 * Return a short (one-word) description of this option. This function may
669 rxkb_option_get_brief(struct rxkb_option *o);
672 * Return a human-readable description of this option. This function may return
676 rxkb_option_get_description(struct rxkb_option *o);
679 * Return the popularity for this option.
682 rxkb_option_get_popularity(struct rxkb_option *o);
685 * Increase the refcount of the argument by one.
687 * @returns The argument passed in to this function.
689 struct rxkb_iso639_code *
690 rxkb_iso639_code_ref(struct rxkb_iso639_code *iso639);
693 * Decrease the refcount of the argument by one. When the refcount hits zero,
694 * all memory associated with this struct is freed.
696 * @returns always NULL
698 struct rxkb_iso639_code *
699 rxkb_iso639_code_unref(struct rxkb_iso639_code *iso639);
702 * Return the ISO 639-3 code for this code (e.g. "eng", "fra").
705 rxkb_iso639_code_get_code(struct rxkb_iso639_code *iso639);
708 * Return the first ISO 639 for this layout. Use this to start iterating over
709 * the codes, followed by calls to rxkb_iso639_code_next(). Codes are not
712 * The refcount of the returned code is not increased. Use rxkb_iso639_code_ref()
713 * if you need to keep this struct outside the immediate scope.
715 * @return The first code in the code list.
717 struct rxkb_iso639_code *
718 rxkb_layout_get_iso639_first(struct rxkb_layout *layout);
721 * Return the next code in the list. Returns NULL when no more codes
724 * The refcount of the returned codes is not increased. Use
725 * rxkb_iso639_code_ref() if you need to keep this struct outside the immediate
728 * @returns The next code or NULL at the end of the list
730 struct rxkb_iso639_code *
731 rxkb_iso639_code_next(struct rxkb_iso639_code *iso639);
734 * Increase the refcount of the argument by one.
736 * @returns The argument passed in to this function.
738 struct rxkb_iso3166_code *
739 rxkb_iso3166_code_ref(struct rxkb_iso3166_code *iso3166);
742 * Decrease the refcount of the argument by one. When the refcount hits zero,
743 * all memory associated with this struct is freed.
745 * @returns always NULL
747 struct rxkb_iso3166_code *
748 rxkb_iso3166_code_unref(struct rxkb_iso3166_code *iso3166);
751 * Return the ISO 3166 Alpha 2 code for this code (e.g. "US", "FR").
754 rxkb_iso3166_code_get_code(struct rxkb_iso3166_code *iso3166);
757 * Return the first ISO 3166 for this layout. Use this to start iterating over
758 * the codes, followed by calls to rxkb_iso3166_code_next(). Codes are not
761 * The refcount of the returned code is not increased. Use
762 * rxkb_iso3166_code_ref() if you need to keep this struct outside the immediate
765 * @return The first code in the code list.
767 struct rxkb_iso3166_code *
768 rxkb_layout_get_iso3166_first(struct rxkb_layout *layout);
771 * Return the next code in the list. Returns NULL when no more codes
774 * The refcount of the returned codes is not increased. Use
775 * rxkb_iso3166_code_ref() if you need to keep this struct outside the immediate
778 * @returns The next code or NULL at the end of the list
780 struct rxkb_iso3166_code *
781 rxkb_iso3166_code_next(struct rxkb_iso3166_code *iso3166);
789 #endif /* _XKBREGISTRY_H_ */