Path: blob/master/thirdparty/linuxbsd_headers/xkbcommon/xkbcommon-compose.h
9903 views
/*1* Copyright © 2013 Ran Benita2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#ifndef _XKBCOMMON_COMPOSE_H24#define _XKBCOMMON_COMPOSE_H2526#include <xkbcommon/xkbcommon.h>2728#ifdef __cplusplus29extern "C" {30#endif3132/**33* @file34* libxkbcommon Compose API - support for Compose and dead-keys.35*/3637/**38* @defgroup compose Compose and dead-keys support39* Support for Compose and dead-keys.40* @since 0.5.041*42* @{43*/4445/**46* @page compose-overview Overview47* @parblock48*49* Compose and dead-keys are a common feature of many keyboard input50* systems. They extend the range of the keysysm that can be produced51* directly from a keyboard by using a sequence of key strokes, instead52* of just one.53*54* Here are some example sequences, in the libX11 Compose file format:55*56* <dead_acute> <a> : "á" aacute # LATIN SMALL LETTER A WITH ACUTE57* <Multi_key> <A> <T> : "@" at # COMMERCIAL AT58*59* When the user presses a key which produces the `<dead_acute>` keysym,60* nothing initially happens (thus the key is dubbed a "dead-key"). But61* when the user enters `<a>`, "á" is "composed", in place of "a". If62* instead the user had entered a keysym which does not follow63* `<dead_acute>` in any compose sequence, the sequence is said to be64* "cancelled".65*66* Compose files define many such sequences. For a description of the67* common file format for Compose files, see the Compose(5) man page.68*69* A successfuly-composed sequence has two results: a keysym and a UTF-870* string. At least one of the two is defined for each sequence. If only71* a keysym is given, the keysym's string representation is used for the72* result string (using xkb_keysym_to_utf8()).73*74* This library provides low-level support for Compose file parsing and75* processing. Higher-level APIs (such as libX11's `Xutf8LookupString`(3))76* may be built upon it, or it can be used directly.77*78* @endparblock79*/8081/**82* @page compose-conflicting Conflicting Sequences83* @parblock84*85* To avoid ambiguity, a sequence is not allowed to be a prefix of another.86* In such a case, the conflict is resolved thus:87*88* 1. A longer sequence overrides a shorter one.89* 2. An equal sequence overrides an existing one.90* 3. A shorter sequence does not override a longer one.91*92* Sequences of length 1 are allowed.93*94* @endparblock95*/9697/**98* @page compose-cancellation Cancellation Behavior99* @parblock100*101* What should happen when a sequence is cancelled? For example, consider102* there are only the above sequences, and the input keysyms are103* `<dead_acute> <b>`. There are a few approaches:104*105* 1. Swallow the cancelling keysym; that is, no keysym is produced.106* This is the approach taken by libX11.107* 2. Let the cancelling keysym through; that is, `<b>` is produced.108* 3. Replay the entire sequence; that is, `<dead_acute> <b>` is produced.109* This is the approach taken by Microsoft Windows (approximately;110* instead of `<dead_acute>`, the underlying key is used. This is111* difficult to simulate with XKB keymaps).112*113* You can program whichever approach best fits users' expectations.114*115* @endparblock116*/117118/**119* @struct xkb_compose_table120* Opaque Compose table object.121*122* The compose table holds the definitions of the Compose sequences, as123* gathered from Compose files. It is immutable.124*/125struct xkb_compose_table;126127/**128* @struct xkb_compose_state129* Opaque Compose state object.130*131* The compose state maintains state for compose sequence matching, such132* as which possible sequences are being matched, and the position within133* these sequences. It acts as a simple state machine wherein keysyms are134* the input, and composed keysyms and strings are the output.135*136* The compose state is usually associated with a keyboard device.137*/138struct xkb_compose_state;139140/** Flags affecting Compose file compilation. */141enum xkb_compose_compile_flags {142/** Do not apply any flags. */143XKB_COMPOSE_COMPILE_NO_FLAGS = 0144};145146/** The recognized Compose file formats. */147enum xkb_compose_format {148/** The classic libX11 Compose text format, described in Compose(5). */149XKB_COMPOSE_FORMAT_TEXT_V1 = 1150};151152/**153* @page compose-locale Compose Locale154* @parblock155*156* Compose files are locale dependent:157* - Compose files are written for a locale, and the locale is used when158* searching for the appropriate file to use.159* - Compose files may reference the locale internally, with directives160* such as \%L.161*162* As such, functions like xkb_compose_table_new_from_locale() require163* a `locale` parameter. This will usually be the current locale (see164* locale(7) for more details). You may also want to allow the user to165* explicitly configure it, so he can use the Compose file of a given166* locale, but not use that locale for other things.167*168* You may query the current locale as follows:169* @code170* const char *locale;171* locale = setlocale(LC_CTYPE, NULL);172* @endcode173*174* This will only give useful results if the program had previously set175* the current locale using setlocale(3), with `LC_CTYPE` or `LC_ALL`176* and a non-NULL argument.177*178* If you prefer not to use the locale system of the C runtime library,179* you may nevertheless obtain the user's locale directly using180* environment variables, as described in locale(7). For example,181* @code182* const char *locale;183* locale = getenv("LC_ALL");184* if (!locale || !*locale)185* locale = getenv("LC_CTYPE");186* if (!locale || !*locale)187* locale = getenv("LANG");188* if (!locale || !*locale)189* locale = "C";190* @endcode191*192* Note that some locales supported by the C standard library may not193* have a Compose file assigned.194*195* @endparblock196*/197198/**199* Create a compose table for a given locale.200*201* The locale is used for searching the file-system for an appropriate202* Compose file. The search order is described in Compose(5). It is203* affected by the following environment variables:204*205* 1. `XCOMPOSEFILE` - see Compose(5).206* 2. `XDG_CONFIG_HOME` - before `$HOME/.XCompose` is checked,207* `$XDG_CONFIG_HOME/XCompose` is checked (with a fall back to208* `$HOME/.config/XCompose` if `XDG_CONFIG_HOME` is not defined).209* This is a libxkbcommon extension to the search procedure in210* Compose(5) (since libxkbcommon 1.0.0). Note that other211* implementations, such as libX11, might not find a Compose file in212* this path.213* 3. `HOME` - see Compose(5).214* 4. `XLOCALEDIR` - if set, used as the base directory for the system's215* X locale files, e.g. `/usr/share/X11/locale`, instead of the216* preconfigured directory.217*218* @param context219* The library context in which to create the compose table.220* @param locale221* The current locale. See @ref compose-locale.222* \n223* The value is copied, so it is safe to pass the result of getenv(3)224* (or similar) without fear of it being invalidated by a subsequent225* setenv(3) (or similar).226* @param flags227* Optional flags for the compose table, or 0.228*229* @returns A compose table for the given locale, or NULL if the230* compilation failed or a Compose file was not found.231*232* @memberof xkb_compose_table233*/234struct xkb_compose_table *235xkb_compose_table_new_from_locale(struct xkb_context *context,236const char *locale,237enum xkb_compose_compile_flags flags);238239/**240* Create a new compose table from a Compose file.241*242* @param context243* The library context in which to create the compose table.244* @param file245* The Compose file to compile.246* @param locale247* The current locale. See @ref compose-locale.248* @param format249* The text format of the Compose file to compile.250* @param flags251* Optional flags for the compose table, or 0.252*253* @returns A compose table compiled from the given file, or NULL if254* the compilation failed.255*256* @memberof xkb_compose_table257*/258struct xkb_compose_table *259xkb_compose_table_new_from_file(struct xkb_context *context,260FILE *file,261const char *locale,262enum xkb_compose_format format,263enum xkb_compose_compile_flags flags);264265/**266* Create a new compose table from a memory buffer.267*268* This is just like xkb_compose_table_new_from_file(), but instead of269* a file, gets the table as one enormous string.270*271* @see xkb_compose_table_new_from_file()272* @memberof xkb_compose_table273*/274struct xkb_compose_table *275xkb_compose_table_new_from_buffer(struct xkb_context *context,276const char *buffer, size_t length,277const char *locale,278enum xkb_compose_format format,279enum xkb_compose_compile_flags flags);280281/**282* Take a new reference on a compose table.283*284* @returns The passed in object.285*286* @memberof xkb_compose_table287*/288struct xkb_compose_table *289xkb_compose_table_ref(struct xkb_compose_table *table);290291/**292* Release a reference on a compose table, and possibly free it.293*294* @param table The object. If it is NULL, this function does nothing.295*296* @memberof xkb_compose_table297*/298void299xkb_compose_table_unref(struct xkb_compose_table *table);300301/** Flags for compose state creation. */302enum xkb_compose_state_flags {303/** Do not apply any flags. */304XKB_COMPOSE_STATE_NO_FLAGS = 0305};306307/**308* Create a new compose state object.309*310* @param table311* The compose table the state will use.312* @param flags313* Optional flags for the compose state, or 0.314*315* @returns A new compose state, or NULL on failure.316*317* @memberof xkb_compose_state318*/319struct xkb_compose_state *320xkb_compose_state_new(struct xkb_compose_table *table,321enum xkb_compose_state_flags flags);322323/**324* Take a new reference on a compose state object.325*326* @returns The passed in object.327*328* @memberof xkb_compose_state329*/330struct xkb_compose_state *331xkb_compose_state_ref(struct xkb_compose_state *state);332333/**334* Release a reference on a compose state object, and possibly free it.335*336* @param state The object. If NULL, do nothing.337*338* @memberof xkb_compose_state339*/340void341xkb_compose_state_unref(struct xkb_compose_state *state);342343/**344* Get the compose table which a compose state object is using.345*346* @returns The compose table which was passed to xkb_compose_state_new()347* when creating this state object.348*349* This function does not take a new reference on the compose table; you350* must explicitly reference it yourself if you plan to use it beyond the351* lifetime of the state.352*353* @memberof xkb_compose_state354*/355struct xkb_compose_table *356xkb_compose_state_get_compose_table(struct xkb_compose_state *state);357358/** Status of the Compose sequence state machine. */359enum xkb_compose_status {360/** The initial state; no sequence has started yet. */361XKB_COMPOSE_NOTHING,362/** In the middle of a sequence. */363XKB_COMPOSE_COMPOSING,364/** A complete sequence has been matched. */365XKB_COMPOSE_COMPOSED,366/** The last sequence was cancelled due to an unmatched keysym. */367XKB_COMPOSE_CANCELLED368};369370/** The effect of a keysym fed to xkb_compose_state_feed(). */371enum xkb_compose_feed_result {372/** The keysym had no effect - it did not affect the status. */373XKB_COMPOSE_FEED_IGNORED,374/** The keysym started, advanced or cancelled a sequence. */375XKB_COMPOSE_FEED_ACCEPTED376};377378/**379* Feed one keysym to the Compose sequence state machine.380*381* This function can advance into a compose sequence, cancel a sequence,382* start a new sequence, or do nothing in particular. The resulting383* status may be observed with xkb_compose_state_get_status().384*385* Some keysyms, such as keysyms for modifier keys, are ignored - they386* have no effect on the status or otherwise.387*388* The following is a description of the possible status transitions, in389* the format CURRENT STATUS => NEXT STATUS, given a non-ignored input390* keysym `keysym`:391*392@verbatim393NOTHING or CANCELLED or COMPOSED =>394NOTHING if keysym does not start a sequence.395COMPOSING if keysym starts a sequence.396COMPOSED if keysym starts and terminates a single-keysym sequence.397398COMPOSING =>399COMPOSING if keysym advances any of the currently possible400sequences but does not terminate any of them.401COMPOSED if keysym terminates one of the currently possible402sequences.403CANCELLED if keysym does not advance any of the currently404possible sequences.405@endverbatim406*407* The current Compose formats do not support multiple-keysyms.408* Therefore, if you are using a function such as xkb_state_key_get_syms()409* and it returns more than one keysym, consider feeding XKB_KEY_NoSymbol410* instead.411*412* @param state413* The compose state object.414* @param keysym415* A keysym, usually obtained after a key-press event, with a416* function such as xkb_state_key_get_one_sym().417*418* @returns Whether the keysym was ignored. This is useful, for example,419* if you want to keep a record of the sequence matched thus far.420*421* @memberof xkb_compose_state422*/423enum xkb_compose_feed_result424xkb_compose_state_feed(struct xkb_compose_state *state,425xkb_keysym_t keysym);426427/**428* Reset the Compose sequence state machine.429*430* The status is set to XKB_COMPOSE_NOTHING, and the current sequence431* is discarded.432*433* @memberof xkb_compose_state434*/435void436xkb_compose_state_reset(struct xkb_compose_state *state);437438/**439* Get the current status of the compose state machine.440*441* @see xkb_compose_status442* @memberof xkb_compose_state443**/444enum xkb_compose_status445xkb_compose_state_get_status(struct xkb_compose_state *state);446447/**448* Get the result Unicode/UTF-8 string for a composed sequence.449*450* See @ref compose-overview for more details. This function is only451* useful when the status is XKB_COMPOSE_COMPOSED.452*453* @param[in] state454* The compose state.455* @param[out] buffer456* A buffer to write the string into.457* @param[in] size458* Size of the buffer.459*460* @warning If the buffer passed is too small, the string is truncated461* (though still NUL-terminated).462*463* @returns464* The number of bytes required for the string, excluding the NUL byte.465* If the sequence is not complete, or does not have a viable result466* string, returns 0, and sets `buffer` to the empty string (if possible).467* @returns468* You may check if truncation has occurred by comparing the return value469* with the size of `buffer`, similarly to the `snprintf`(3) function.470* You may safely pass NULL and 0 to `buffer` and `size` to find the471* required size (without the NUL-byte).472*473* @memberof xkb_compose_state474**/475int476xkb_compose_state_get_utf8(struct xkb_compose_state *state,477char *buffer, size_t size);478479/**480* Get the result keysym for a composed sequence.481*482* See @ref compose-overview for more details. This function is only483* useful when the status is XKB_COMPOSE_COMPOSED.484*485* @returns The result keysym. If the sequence is not complete, or does486* not specify a result keysym, returns XKB_KEY_NoSymbol.487*488* @memberof xkb_compose_state489**/490xkb_keysym_t491xkb_compose_state_get_one_sym(struct xkb_compose_state *state);492493/** @} */494495#ifdef __cplusplus496} /* extern "C" */497#endif498499#endif /* _XKBCOMMON_COMPOSE_H */500501502