Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/xkbcommon/xkbcommon-compose.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Copyright © 2013 Ran Benita
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the next
0012  * paragraph) shall be included in all copies or substantial portions of the
0013  * Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0021  * DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 #ifndef _XKBCOMMON_COMPOSE_H
0025 #define _XKBCOMMON_COMPOSE_H
0026 
0027 #include <xkbcommon/xkbcommon.h>
0028 
0029 #ifdef __cplusplus
0030 extern "C" {
0031 #endif
0032 
0033 /**
0034  * @file
0035  * libxkbcommon Compose API - support for Compose and dead-keys.
0036  */
0037 
0038 /**
0039  * @defgroup compose Compose and dead-keys support
0040  * Support for Compose and dead-keys.
0041  * @since 0.5.0
0042  *
0043  * @{
0044  */
0045 
0046 /**
0047  * @page compose-overview Overview
0048  * @parblock
0049  *
0050  * Compose and dead-keys are a common feature of many keyboard input
0051  * systems.  They extend the range of the keysysm that can be produced
0052  * directly from a keyboard by using a sequence of key strokes, instead
0053  * of just one.
0054  *
0055  * Here are some example sequences, in the libX11 Compose file format:
0056  *
0057  *     <dead_acute> <a>         : "á"   aacute # LATIN SMALL LETTER A WITH ACUTE
0058  *     <Multi_key> <A> <T>      : "@"   at     # COMMERCIAL AT
0059  *
0060  * When the user presses a key which produces the `<dead_acute>` keysym,
0061  * nothing initially happens (thus the key is dubbed a "dead-key").  But
0062  * when the user enters `<a>`, "á" is "composed", in place of "a".  If
0063  * instead the user had entered a keysym which does not follow
0064  * `<dead_acute>` in any compose sequence, the sequence is said to be
0065  * "cancelled".
0066  *
0067  * Compose files define many such sequences.  For a description of the
0068  * common file format for Compose files, see the Compose(5) man page.
0069  *
0070  * A successfuly-composed sequence has two results: a keysym and a UTF-8
0071  * string.  At least one of the two is defined for each sequence.  If only
0072  * a keysym is given, the keysym's string representation is used for the
0073  * result string (using xkb_keysym_to_utf8()).
0074  *
0075  * This library provides low-level support for Compose file parsing and
0076  * processing.  Higher-level APIs (such as libX11's `Xutf8LookupString`(3))
0077  * may be built upon it, or it can be used directly.
0078  *
0079  * @endparblock
0080  */
0081 
0082 /**
0083  * @page compose-conflicting Conflicting Sequences
0084  * @parblock
0085  *
0086  * To avoid ambiguity, a sequence is not allowed to be a prefix of another.
0087  * In such a case, the conflict is resolved thus:
0088  *
0089  * 1. A longer sequence overrides a shorter one.
0090  * 2. An equal sequence overrides an existing one.
0091  * 3. A shorter sequence does not override a longer one.
0092  *
0093  * Sequences of length 1 are allowed.
0094  *
0095  * @endparblock
0096  */
0097 
0098 /**
0099  * @page compose-cancellation Cancellation Behavior
0100  * @parblock
0101  *
0102  * What should happen when a sequence is cancelled?  For example, consider
0103  * there are only the above sequences, and the input keysyms are
0104  * `<dead_acute> <b>`.  There are a few approaches:
0105  *
0106  * 1. Swallow the cancelling keysym; that is, no keysym is produced.
0107  *    This is the approach taken by libX11.
0108  * 2. Let the cancelling keysym through; that is, `<b>` is produced.
0109  * 3. Replay the entire sequence; that is, `<dead_acute> <b>` is produced.
0110  *    This is the approach taken by Microsoft Windows (approximately;
0111  *    instead of `<dead_acute>`, the underlying key is used.  This is
0112  *    difficult to simulate with XKB keymaps).
0113  *
0114  * You can program whichever approach best fits users' expectations.
0115  *
0116  * @endparblock
0117  */
0118 
0119 /**
0120  * @struct xkb_compose_table
0121  * Opaque Compose table object.
0122  *
0123  * The compose table holds the definitions of the Compose sequences, as
0124  * gathered from Compose files.  It is immutable.
0125  */
0126 struct xkb_compose_table;
0127 
0128 /**
0129  * @struct xkb_compose_state
0130  * Opaque Compose state object.
0131  *
0132  * The compose state maintains state for compose sequence matching, such
0133  * as which possible sequences are being matched, and the position within
0134  * these sequences.  It acts as a simple state machine wherein keysyms are
0135  * the input, and composed keysyms and strings are the output.
0136  *
0137  * The compose state is usually associated with a keyboard device.
0138  */
0139 struct xkb_compose_state;
0140 
0141 /** Flags affecting Compose file compilation. */
0142 enum xkb_compose_compile_flags {
0143     /** Do not apply any flags. */
0144     XKB_COMPOSE_COMPILE_NO_FLAGS = 0
0145 };
0146 
0147 /** The recognized Compose file formats. */
0148 enum xkb_compose_format {
0149     /** The classic libX11 Compose text format, described in Compose(5). */
0150     XKB_COMPOSE_FORMAT_TEXT_V1 = 1
0151 };
0152 
0153 /**
0154  * @page compose-locale Compose Locale
0155  * @parblock
0156  *
0157  * Compose files are locale dependent:
0158  * - Compose files are written for a locale, and the locale is used when
0159  *   searching for the appropriate file to use.
0160  * - Compose files may reference the locale internally, with directives
0161  *   such as \%L.
0162  *
0163  * As such, functions like xkb_compose_table_new_from_locale() require
0164  * a `locale` parameter.  This will usually be the current locale (see
0165  * locale(7) for more details).  You may also want to allow the user to
0166  * explicitly configure it, so he can use the Compose file of a given
0167  * locale, but not use that locale for other things.
0168  *
0169  * You may query the current locale as follows:
0170  * @code
0171  *     const char *locale;
0172  *     locale = setlocale(LC_CTYPE, NULL);
0173  * @endcode
0174  *
0175  * This will only give useful results if the program had previously set
0176  * the current locale using setlocale(3), with `LC_CTYPE` or `LC_ALL`
0177  * and a non-NULL argument.
0178  *
0179  * If you prefer not to use the locale system of the C runtime library,
0180  * you may nevertheless obtain the user's locale directly using
0181  * environment variables, as described in locale(7).  For example,
0182  * @code
0183  *     const char *locale;
0184  *     locale = getenv("LC_ALL");
0185  *     if (!locale || !*locale)
0186  *         locale = getenv("LC_CTYPE");
0187  *     if (!locale || !*locale)
0188  *         locale = getenv("LANG");
0189  *     if (!locale || !*locale)
0190  *         locale = "C";
0191  * @endcode
0192  *
0193  * Note that some locales supported by the C standard library may not
0194  * have a Compose file assigned.
0195  *
0196  * @endparblock
0197  */
0198 
0199 /**
0200  * Create a compose table for a given locale.
0201  *
0202  * The locale is used for searching the file-system for an appropriate
0203  * Compose file.  The search order is described in Compose(5).  It is
0204  * affected by the following environment variables:
0205  *
0206  * 1. `XCOMPOSEFILE` - see Compose(5).
0207  * 2. `XDG_CONFIG_HOME` - before `$HOME/.XCompose` is checked,
0208  *    `$XDG_CONFIG_HOME/XCompose` is checked (with a fall back to
0209  *    `$HOME/.config/XCompose` if `XDG_CONFIG_HOME` is not defined).
0210  *    This is a libxkbcommon extension to the search procedure in
0211  *    Compose(5) (since libxkbcommon 1.0.0). Note that other
0212  *    implementations, such as libX11, might not find a Compose file in
0213  *    this path.
0214  * 3. `HOME` - see Compose(5).
0215  * 4. `XLOCALEDIR` - if set, used as the base directory for the system's
0216  *    X locale files, e.g. `/usr/share/X11/locale`, instead of the
0217  *    preconfigured directory.
0218  *
0219  * @param context
0220  *     The library context in which to create the compose table.
0221  * @param locale
0222  *     The current locale.  See @ref compose-locale.
0223  *     \n
0224  *     The value is copied, so it is safe to pass the result of getenv(3)
0225  *     (or similar) without fear of it being invalidated by a subsequent
0226  *     setenv(3) (or similar).
0227  * @param flags
0228  *     Optional flags for the compose table, or 0.
0229  *
0230  * @returns A compose table for the given locale, or NULL if the
0231  * compilation failed or a Compose file was not found.
0232  *
0233  * @memberof xkb_compose_table
0234  */
0235 struct xkb_compose_table *
0236 xkb_compose_table_new_from_locale(struct xkb_context *context,
0237                                   const char *locale,
0238                                   enum xkb_compose_compile_flags flags);
0239 
0240 /**
0241  * Create a new compose table from a Compose file.
0242  *
0243  * @param context
0244  *     The library context in which to create the compose table.
0245  * @param file
0246  *     The Compose file to compile.
0247  * @param locale
0248  *     The current locale.  See @ref compose-locale.
0249  * @param format
0250  *     The text format of the Compose file to compile.
0251  * @param flags
0252  *     Optional flags for the compose table, or 0.
0253  *
0254  * @returns A compose table compiled from the given file, or NULL if
0255  * the compilation failed.
0256  *
0257  * @memberof xkb_compose_table
0258  */
0259 struct xkb_compose_table *
0260 xkb_compose_table_new_from_file(struct xkb_context *context,
0261                                 FILE *file,
0262                                 const char *locale,
0263                                 enum xkb_compose_format format,
0264                                 enum xkb_compose_compile_flags flags);
0265 
0266 /**
0267  * Create a new compose table from a memory buffer.
0268  *
0269  * This is just like xkb_compose_table_new_from_file(), but instead of
0270  * a file, gets the table as one enormous string.
0271  *
0272  * @see xkb_compose_table_new_from_file()
0273  * @memberof xkb_compose_table
0274  */
0275 struct xkb_compose_table *
0276 xkb_compose_table_new_from_buffer(struct xkb_context *context,
0277                                   const char *buffer, size_t length,
0278                                   const char *locale,
0279                                   enum xkb_compose_format format,
0280                                   enum xkb_compose_compile_flags flags);
0281 
0282 /**
0283  * Take a new reference on a compose table.
0284  *
0285  * @returns The passed in object.
0286  *
0287  * @memberof xkb_compose_table
0288  */
0289 struct xkb_compose_table *
0290 xkb_compose_table_ref(struct xkb_compose_table *table);
0291 
0292 /**
0293  * Release a reference on a compose table, and possibly free it.
0294  *
0295  * @param table The object.  If it is NULL, this function does nothing.
0296  *
0297  * @memberof xkb_compose_table
0298  */
0299 void
0300 xkb_compose_table_unref(struct xkb_compose_table *table);
0301 
0302 /**
0303  * @struct xkb_compose_table_entry
0304  * Opaque Compose table entry object.
0305  *
0306  * Represents a single entry in a Compose file in the iteration API.
0307  * It is immutable.
0308  *
0309  * @sa xkb_compose_table_iterator_new
0310  * @since 1.6.0
0311  */
0312 struct xkb_compose_table_entry;
0313 
0314 /**
0315  * Get the left-hand keysym sequence of a Compose table entry.
0316  *
0317  * For example, given the following entry:
0318  *
0319  * ```
0320  * <dead_tilde> <space> : "~" asciitilde # TILDE
0321  * ```
0322  *
0323  * it will return `{XKB_KEY_dead_tilde, XKB_KEY_space}`.
0324  *
0325  * @param[in]  entry The compose table entry object to process.
0326  *
0327  * @param[out] sequence_length Number of keysyms in the sequence.
0328  *
0329  * @returns The array of left-hand side keysyms.  The number of keysyms
0330  * is returned in the @p sequence_length out-parameter.
0331  *
0332  * @memberof xkb_compose_table_entry
0333  * @since 1.6.0
0334  */
0335 const xkb_keysym_t *
0336 xkb_compose_table_entry_sequence(struct xkb_compose_table_entry *entry,
0337                                  size_t *sequence_length);
0338 
0339 /**
0340  * Get the right-hand result keysym of a Compose table entry.
0341  *
0342  * For example, given the following entry:
0343  *
0344  * ```
0345  * <dead_tilde> <space> : "~" asciitilde # TILDE
0346  * ```
0347  *
0348  * it will return `XKB_KEY_asciitilde`.
0349  *
0350  * The keysym is optional; if the entry does not specify a keysym,
0351  * returns `XKB_KEY_NoSymbol`.
0352  *
0353  * @memberof xkb_compose_table_entry
0354  * @since 1.6.0
0355  */
0356 xkb_keysym_t
0357 xkb_compose_table_entry_keysym(struct xkb_compose_table_entry *entry);
0358 
0359 /**
0360  * Get the right-hand result string of a Compose table entry.
0361  *
0362  * The string is UTF-8 encoded and NULL-terminated.
0363  *
0364  * For example, given the following entry:
0365  *
0366  * ```
0367  * <dead_tilde> <space> : "~" asciitilde # TILDE
0368  * ```
0369  *
0370  * it will return `"~"`.
0371  *
0372  * The string is optional; if the entry does not specify a string,
0373  * returns the empty string.
0374  *
0375  * @memberof xkb_compose_table_entry
0376  * @since 1.6.0
0377  */
0378 const char *
0379 xkb_compose_table_entry_utf8(struct xkb_compose_table_entry *entry);
0380 
0381 /**
0382  * @struct xkb_compose_table_iterator
0383  * Iterator over a compose table’s entries.
0384  *
0385  * @sa xkb_compose_table_iterator_new()
0386  * @since 1.6.0
0387  */
0388 struct xkb_compose_table_iterator;
0389 
0390 /**
0391  * Create a new iterator for a compose table.
0392  *
0393  * Intended use:
0394  *
0395  * ```c
0396  * struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(compose_table);
0397  * struct xkb_compose_table_entry *entry;
0398  * while ((entry = xkb_compose_table_iterator_next(iter))) {
0399  *     // ...
0400  * }
0401  * xkb_compose_table_iterator_free(iter);
0402  * ```
0403  *
0404  * @returns A new compose table iterator, or `NULL` on failure.
0405  *
0406  * @memberof xkb_compose_table_iterator
0407  * @sa xkb_compose_table_iterator_free()
0408  * @since 1.6.0
0409  */
0410 struct xkb_compose_table_iterator *
0411 xkb_compose_table_iterator_new(struct xkb_compose_table *table);
0412 
0413 /**
0414  * Free a compose iterator.
0415  *
0416  * @memberof xkb_compose_table_iterator
0417  * @since 1.6.0
0418  */
0419 void
0420 xkb_compose_table_iterator_free(struct xkb_compose_table_iterator *iter);
0421 
0422 /**
0423  * Get the next compose entry from a compose table iterator.
0424  *
0425  * The entries are returned in lexicographic order of the left-hand
0426  * side of entries. This does not correspond to the order in which
0427  * the entries appear in the Compose file.
0428  *
0429  * @attention The return value is valid until the next call to this function.
0430  *
0431  * Returns `NULL` in case there is no more entries.
0432  *
0433  * @memberof xkb_compose_table_iterator
0434  * @since 1.6.0
0435  */
0436 struct xkb_compose_table_entry *
0437 xkb_compose_table_iterator_next(struct xkb_compose_table_iterator *iter);
0438 
0439 /** Flags for compose state creation. */
0440 enum xkb_compose_state_flags {
0441     /** Do not apply any flags. */
0442     XKB_COMPOSE_STATE_NO_FLAGS = 0
0443 };
0444 
0445 /**
0446  * Create a new compose state object.
0447  *
0448  * @param table
0449  *     The compose table the state will use.
0450  * @param flags
0451  *     Optional flags for the compose state, or 0.
0452  *
0453  * @returns A new compose state, or NULL on failure.
0454  *
0455  * @memberof xkb_compose_state
0456  */
0457 struct xkb_compose_state *
0458 xkb_compose_state_new(struct xkb_compose_table *table,
0459                       enum xkb_compose_state_flags flags);
0460 
0461 /**
0462  * Take a new reference on a compose state object.
0463  *
0464  * @returns The passed in object.
0465  *
0466  * @memberof xkb_compose_state
0467  */
0468 struct xkb_compose_state *
0469 xkb_compose_state_ref(struct xkb_compose_state *state);
0470 
0471 /**
0472  * Release a reference on a compose state object, and possibly free it.
0473  *
0474  * @param state The object.  If NULL, do nothing.
0475  *
0476  * @memberof xkb_compose_state
0477  */
0478 void
0479 xkb_compose_state_unref(struct xkb_compose_state *state);
0480 
0481 /**
0482  * Get the compose table which a compose state object is using.
0483  *
0484  * @returns The compose table which was passed to xkb_compose_state_new()
0485  * when creating this state object.
0486  *
0487  * This function does not take a new reference on the compose table; you
0488  * must explicitly reference it yourself if you plan to use it beyond the
0489  * lifetime of the state.
0490  *
0491  * @memberof xkb_compose_state
0492  */
0493 struct xkb_compose_table *
0494 xkb_compose_state_get_compose_table(struct xkb_compose_state *state);
0495 
0496 /** Status of the Compose sequence state machine. */
0497 enum xkb_compose_status {
0498     /** The initial state; no sequence has started yet. */
0499     XKB_COMPOSE_NOTHING,
0500     /** In the middle of a sequence. */
0501     XKB_COMPOSE_COMPOSING,
0502     /** A complete sequence has been matched. */
0503     XKB_COMPOSE_COMPOSED,
0504     /** The last sequence was cancelled due to an unmatched keysym. */
0505     XKB_COMPOSE_CANCELLED
0506 };
0507 
0508 /** The effect of a keysym fed to xkb_compose_state_feed(). */
0509 enum xkb_compose_feed_result {
0510     /** The keysym had no effect - it did not affect the status. */
0511     XKB_COMPOSE_FEED_IGNORED,
0512     /** The keysym started, advanced or cancelled a sequence. */
0513     XKB_COMPOSE_FEED_ACCEPTED
0514 };
0515 
0516 /**
0517  * Feed one keysym to the Compose sequence state machine.
0518  *
0519  * This function can advance into a compose sequence, cancel a sequence,
0520  * start a new sequence, or do nothing in particular.  The resulting
0521  * status may be observed with xkb_compose_state_get_status().
0522  *
0523  * Some keysyms, such as keysyms for modifier keys, are ignored - they
0524  * have no effect on the status or otherwise.
0525  *
0526  * The following is a description of the possible status transitions, in
0527  * the format CURRENT STATUS => NEXT STATUS, given a non-ignored input
0528  * keysym `keysym`:
0529  *
0530    @verbatim
0531    NOTHING or CANCELLED or COMPOSED =>
0532       NOTHING   if keysym does not start a sequence.
0533       COMPOSING if keysym starts a sequence.
0534       COMPOSED  if keysym starts and terminates a single-keysym sequence.
0535 
0536    COMPOSING =>
0537       COMPOSING if keysym advances any of the currently possible
0538                 sequences but does not terminate any of them.
0539       COMPOSED  if keysym terminates one of the currently possible
0540                 sequences.
0541       CANCELLED if keysym does not advance any of the currently
0542                 possible sequences.
0543    @endverbatim
0544  *
0545  * The current Compose formats do not support multiple-keysyms.
0546  * Therefore, if you are using a function such as xkb_state_key_get_syms()
0547  * and it returns more than one keysym, consider feeding XKB_KEY_NoSymbol
0548  * instead.
0549  *
0550  * @param state
0551  *     The compose state object.
0552  * @param keysym
0553  *     A keysym, usually obtained after a key-press event, with a
0554  *     function such as xkb_state_key_get_one_sym().
0555  *
0556  * @returns Whether the keysym was ignored.  This is useful, for example,
0557  * if you want to keep a record of the sequence matched thus far.
0558  *
0559  * @memberof xkb_compose_state
0560  */
0561 enum xkb_compose_feed_result
0562 xkb_compose_state_feed(struct xkb_compose_state *state,
0563                        xkb_keysym_t keysym);
0564 
0565 /**
0566  * Reset the Compose sequence state machine.
0567  *
0568  * The status is set to XKB_COMPOSE_NOTHING, and the current sequence
0569  * is discarded.
0570  *
0571  * @memberof xkb_compose_state
0572  */
0573 void
0574 xkb_compose_state_reset(struct xkb_compose_state *state);
0575 
0576 /**
0577  * Get the current status of the compose state machine.
0578  *
0579  * @see xkb_compose_status
0580  * @memberof xkb_compose_state
0581  **/
0582 enum xkb_compose_status
0583 xkb_compose_state_get_status(struct xkb_compose_state *state);
0584 
0585 /**
0586  * Get the result Unicode/UTF-8 string for a composed sequence.
0587  *
0588  * See @ref compose-overview for more details.  This function is only
0589  * useful when the status is XKB_COMPOSE_COMPOSED.
0590  *
0591  * @param[in] state
0592  *     The compose state.
0593  * @param[out] buffer
0594  *     A buffer to write the string into.
0595  * @param[in] size
0596  *     Size of the buffer.
0597  *
0598  * @warning If the buffer passed is too small, the string is truncated
0599  * (though still NUL-terminated).
0600  *
0601  * @returns
0602  *   The number of bytes required for the string, excluding the NUL byte.
0603  *   If the sequence is not complete, or does not have a viable result
0604  *   string, returns 0, and sets `buffer` to the empty string (if possible).
0605  * @returns
0606  *   You may check if truncation has occurred by comparing the return value
0607  *   with the size of `buffer`, similarly to the `snprintf`(3) function.
0608  *   You may safely pass NULL and 0 to `buffer` and `size` to find the
0609  *   required size (without the NUL-byte).
0610  *
0611  * @memberof xkb_compose_state
0612  **/
0613 int
0614 xkb_compose_state_get_utf8(struct xkb_compose_state *state,
0615                            char *buffer, size_t size);
0616 
0617 /**
0618  * Get the result keysym for a composed sequence.
0619  *
0620  * See @ref compose-overview for more details.  This function is only
0621  * useful when the status is XKB_COMPOSE_COMPOSED.
0622  *
0623  * @returns The result keysym.  If the sequence is not complete, or does
0624  * not specify a result keysym, returns XKB_KEY_NoSymbol.
0625  *
0626  * @memberof xkb_compose_state
0627  **/
0628 xkb_keysym_t
0629 xkb_compose_state_get_one_sym(struct xkb_compose_state *state);
0630 
0631 /** @} */
0632 
0633 #ifdef __cplusplus
0634 } /* extern "C" */
0635 #endif
0636 
0637 #endif /* _XKBCOMMON_COMPOSE_H */