xkbcommon-compose.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright © 2013 Ran Benita
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #ifndef _XKBCOMMON_COMPOSE_H
  6. #define _XKBCOMMON_COMPOSE_H
  7. #include <xkbcommon/xkbcommon.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * @file
  13. * libxkbcommon Compose API - support for Compose and dead-keys.
  14. */
  15. /**
  16. * @defgroup compose Compose and dead-keys support
  17. * Support for Compose and dead-keys.
  18. * @since 0.5.0
  19. *
  20. * @{
  21. */
  22. /**
  23. * @page compose-overview Overview
  24. * @parblock
  25. *
  26. * Compose and dead-keys are a common feature of many keyboard input
  27. * systems. They extend the range of the keysyms that can be produced
  28. * directly from a keyboard by using a sequence of key strokes, instead
  29. * of just one.
  30. *
  31. * Here are some example sequences, in the libX11 Compose file format:
  32. *
  33. * <dead_acute> <a> : "á" aacute # LATIN SMALL LETTER A WITH ACUTE
  34. * <Multi_key> <A> <T> : "@" at # COMMERCIAL AT
  35. *
  36. * When the user presses a key which produces the `<dead_acute>` keysym,
  37. * nothing initially happens (thus the key is dubbed a *dead-key*). But
  38. * when the user enters `<a>`, “á” is *composed*, in place of “a”. If
  39. * instead the user had entered a keysym which does not follow
  40. * `<dead_acute>` in any compose sequence, the sequence is said to be
  41. * *cancelled*.
  42. *
  43. * Compose files define many such sequences. For a description of the
  44. * common file format for Compose files, see the `Compose(5)` man page.
  45. *
  46. * A successfully-composed sequence has two results: a keysym and a UTF-8
  47. * string. At least one of the two is defined for each sequence. If only
  48. * a keysym is given, the keysym’s string representation is used for the
  49. * result string (using xkb_keysym_to_utf8()).
  50. *
  51. * This library provides low-level support for Compose file parsing and
  52. * processing. Higher-level APIs (such as libX11’s `Xutf8LookupString(3)`)
  53. * may be built upon it, or it can be used directly.
  54. *
  55. * @endparblock
  56. */
  57. /**
  58. * @page compose-conflicting Conflicting Sequences
  59. * @parblock
  60. *
  61. * To avoid ambiguity, a sequence is not allowed to be a prefix of another.
  62. * When such a conflict occurs, the sequence defined last is the one retained.
  63. *
  64. * Sequences of length 1 are allowed.
  65. *
  66. * @endparblock
  67. */
  68. /**
  69. * @page compose-cancellation Cancellation Behavior
  70. * @parblock
  71. *
  72. * What should happen when a sequence is cancelled? For example, consider
  73. * there are only the above sequences, and the input keysyms are
  74. * `<dead_acute> <b>`. There are a few approaches:
  75. *
  76. * 1. Swallow the cancelling keysym; that is, no keysym is produced.
  77. * This is the approach taken by libX11.
  78. * 2. Let the cancelling keysym through; that is, `<b>` is produced.
  79. * 3. Replay the entire sequence; that is, `<dead_acute> <b>` is produced.
  80. * This is the approach taken by Microsoft Windows (approximately;
  81. * instead of `<dead_acute>`, the underlying key is used. This is
  82. * difficult to simulate with XKB keymaps).
  83. *
  84. * You can program whichever approach best fits users' expectations.
  85. *
  86. * @endparblock
  87. */
  88. /**
  89. * @struct xkb_compose_table
  90. * Opaque Compose table object.
  91. *
  92. * The compose table holds the definitions of the Compose sequences, as
  93. * gathered from Compose files. It is immutable.
  94. */
  95. struct xkb_compose_table;
  96. /**
  97. * @struct xkb_compose_state
  98. * Opaque Compose state object.
  99. *
  100. * The compose state maintains state for compose sequence matching, such
  101. * as which possible sequences are being matched, and the position within
  102. * these sequences. It acts as a simple state machine wherein keysyms are
  103. * the input, and composed keysyms and strings are the output.
  104. *
  105. * The compose state is usually associated with a keyboard device.
  106. */
  107. struct xkb_compose_state;
  108. /** Flags affecting Compose file compilation. */
  109. enum xkb_compose_compile_flags {
  110. /** Do not apply any flags. */
  111. XKB_COMPOSE_COMPILE_NO_FLAGS = 0
  112. };
  113. /** The recognized Compose file formats. */
  114. enum xkb_compose_format {
  115. /** The classic libX11 Compose text format, described in Compose(5). */
  116. XKB_COMPOSE_FORMAT_TEXT_V1 = 1
  117. };
  118. /**
  119. * @page compose-locale Compose Locale
  120. * @parblock
  121. *
  122. * Compose files are locale dependent:
  123. * - Compose files are written for a locale, and the locale is used when
  124. * searching for the appropriate file to use.
  125. * - Compose files may reference the locale internally, with directives
  126. * such as `%%L`.
  127. *
  128. * As such, functions like `xkb_compose_table::xkb_compose_table_new_from_locale()`
  129. * require a `locale` parameter. This will usually be the current locale (see
  130. * `locale(7)` for more details). You may also want to allow the user to
  131. * explicitly configure it, so he can use the Compose file of a given
  132. * locale, but not use that locale for other things.
  133. *
  134. * You may query the current locale as follows:
  135. *
  136. * ```c
  137. * const char *locale;
  138. * locale = setlocale(LC_CTYPE, NULL);
  139. * ```
  140. *
  141. * This will only give useful results if the program had previously set
  142. * the current locale using setlocale(3), with `LC_CTYPE` or `LC_ALL`
  143. * and a non-`NULL` argument.
  144. *
  145. * If you prefer not to use the locale system of the C runtime library,
  146. * you may nevertheless obtain the user’s locale directly using
  147. * environment variables, as described in `locale(7)`. For example,
  148. *
  149. * ```c
  150. * const char *locale;
  151. * locale = getenv("LC_ALL");
  152. * if (!locale || !*locale)
  153. * locale = getenv("LC_CTYPE");
  154. * if (!locale || !*locale)
  155. * locale = getenv("LANG");
  156. * if (!locale || !*locale)
  157. * locale = "C";
  158. * ```
  159. *
  160. * Note that some locales supported by the C standard library may not
  161. * have a Compose file assigned.
  162. *
  163. * @endparblock
  164. */
  165. /**
  166. * Create a compose table for a given locale.
  167. *
  168. * The locale is used for searching the file-system for an appropriate
  169. * Compose file. The search order is described in Compose(5). It is
  170. * affected by the following environment variables:
  171. *
  172. * 1. `XCOMPOSEFILE` - see Compose(5).
  173. * 2. `XDG_CONFIG_HOME` - before `$HOME/.XCompose` is checked,
  174. * `$XDG_CONFIG_HOME/XCompose` is checked (with a fall back to
  175. * `$HOME/.config/XCompose` if `XDG_CONFIG_HOME` is not defined).
  176. * This is a libxkbcommon extension to the search procedure in
  177. * Compose(5) (since libxkbcommon 1.0.0). Note that other
  178. * implementations, such as libX11, might not find a Compose file in
  179. * this path.
  180. * 3. `HOME` - see Compose(5).
  181. * 4. `XLOCALEDIR` - if set, used as the base directory for the system’s
  182. * X locale files, e.g. `/usr/share/X11/locale`, instead of the
  183. * preconfigured directory.
  184. *
  185. * Since 1.12, system locales not registered in `$XLOCALEDIR` will fallback
  186. * to `en_US.UTF-8`, if missing locale detection is supported by the C standard
  187. * library in use.
  188. *
  189. * @param context
  190. * The library context in which to create the compose table.
  191. * @param locale
  192. * The current locale. See @ref compose-locale.
  193. * \n
  194. * The value is copied, so it is safe to pass the result of `getenv(3)`
  195. * (or similar) without fear of it being invalidated by a subsequent
  196. * `setenv(3)` (or similar).
  197. * @param flags
  198. * Optional flags for the compose table, or 0.
  199. *
  200. * @returns A compose table for the given locale, or `NULL` if the
  201. * compilation failed or a Compose file was not found.
  202. *
  203. * @memberof xkb_compose_table
  204. */
  205. XKB_EXPORT struct xkb_compose_table *
  206. xkb_compose_table_new_from_locale(struct xkb_context *context,
  207. const char *locale,
  208. enum xkb_compose_compile_flags flags);
  209. /**
  210. * Create a new compose table from a Compose file.
  211. *
  212. * @param context
  213. * The library context in which to create the compose table.
  214. * @param file
  215. * The Compose file to compile.
  216. * @param locale
  217. * The current locale. See @ref compose-locale.
  218. * @param format
  219. * The text format of the Compose file to compile.
  220. * @param flags
  221. * Optional flags for the compose table, or 0.
  222. *
  223. * @returns A compose table compiled from the given file, or `NULL` if
  224. * the compilation failed.
  225. *
  226. * @memberof xkb_compose_table
  227. */
  228. XKB_EXPORT struct xkb_compose_table *
  229. xkb_compose_table_new_from_file(struct xkb_context *context,
  230. FILE *file,
  231. const char *locale,
  232. enum xkb_compose_format format,
  233. enum xkb_compose_compile_flags flags);
  234. /**
  235. * Create a new compose table from a memory buffer.
  236. *
  237. * This is just like xkb_compose_table_new_from_file(), but instead of
  238. * a file, gets the table as one enormous string.
  239. *
  240. * @see xkb_compose_table_new_from_file()
  241. * @memberof xkb_compose_table
  242. */
  243. XKB_EXPORT struct xkb_compose_table *
  244. xkb_compose_table_new_from_buffer(struct xkb_context *context,
  245. const char *buffer, size_t length,
  246. const char *locale,
  247. enum xkb_compose_format format,
  248. enum xkb_compose_compile_flags flags);
  249. /**
  250. * Take a new reference on a compose table.
  251. *
  252. * @returns The passed in object.
  253. *
  254. * @memberof xkb_compose_table
  255. */
  256. XKB_EXPORT struct xkb_compose_table *
  257. xkb_compose_table_ref(struct xkb_compose_table *table);
  258. /**
  259. * Release a reference on a compose table, and possibly free it.
  260. *
  261. * @param table The object. If it is `NULL`, this function does nothing.
  262. *
  263. * @memberof xkb_compose_table
  264. */
  265. XKB_EXPORT void
  266. xkb_compose_table_unref(struct xkb_compose_table *table);
  267. /**
  268. * @struct xkb_compose_table_entry
  269. * Opaque Compose table entry object.
  270. *
  271. * Represents a single entry in a Compose file in the iteration API.
  272. * It is immutable.
  273. *
  274. * @sa xkb_compose_table_iterator_new
  275. * @since 1.6.0
  276. */
  277. struct xkb_compose_table_entry;
  278. /**
  279. * Get the left-hand keysym sequence of a Compose table entry.
  280. *
  281. * For example, given the following entry:
  282. *
  283. * ```
  284. * <dead_tilde> <space> : "~" asciitilde # TILDE
  285. * ```
  286. *
  287. * it will return `{XKB_KEY_dead_tilde, XKB_KEY_space}`.
  288. *
  289. * @param[in] entry The compose table entry object to process.
  290. *
  291. * @param[out] sequence_length Number of keysyms in the sequence.
  292. *
  293. * @returns The array of left-hand side keysyms. The number of keysyms
  294. * is returned in the @p sequence_length out-parameter.
  295. *
  296. * @memberof xkb_compose_table_entry
  297. * @since 1.6.0
  298. */
  299. XKB_EXPORT const xkb_keysym_t *
  300. xkb_compose_table_entry_sequence(struct xkb_compose_table_entry *entry,
  301. size_t *sequence_length);
  302. /**
  303. * Get the right-hand result keysym of a Compose table entry.
  304. *
  305. * For example, given the following entry:
  306. *
  307. * ```
  308. * <dead_tilde> <space> : "~" asciitilde # TILDE
  309. * ```
  310. *
  311. * it will return `XKB_KEY_asciitilde`.
  312. *
  313. * The keysym is optional; if the entry does not specify a keysym,
  314. * returns `XKB_KEY_NoSymbol`.
  315. *
  316. * @memberof xkb_compose_table_entry
  317. * @since 1.6.0
  318. */
  319. XKB_EXPORT xkb_keysym_t
  320. xkb_compose_table_entry_keysym(struct xkb_compose_table_entry *entry);
  321. /**
  322. * Get the right-hand result string of a Compose table entry.
  323. *
  324. * The string is UTF-8 encoded and `NULL`-terminated.
  325. *
  326. * For example, given the following entry:
  327. *
  328. * ```
  329. * <dead_tilde> <space> : "~" asciitilde # TILDE
  330. * ```
  331. *
  332. * it will return `"~"`.
  333. *
  334. * The string is optional; if the entry does not specify a string,
  335. * returns the empty string.
  336. *
  337. * @memberof xkb_compose_table_entry
  338. * @since 1.6.0
  339. */
  340. XKB_EXPORT const char *
  341. xkb_compose_table_entry_utf8(struct xkb_compose_table_entry *entry);
  342. /**
  343. * @struct xkb_compose_table_iterator
  344. * Iterator over a compose table’s entries.
  345. *
  346. * @sa xkb_compose_table_iterator_new()
  347. * @since 1.6.0
  348. */
  349. struct xkb_compose_table_iterator;
  350. /**
  351. * Create a new iterator for a compose table.
  352. *
  353. * Intended use:
  354. *
  355. * ```c
  356. * struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(compose_table);
  357. * struct xkb_compose_table_entry *entry;
  358. * while ((entry = xkb_compose_table_iterator_next(iter))) {
  359. * // ...
  360. * }
  361. * xkb_compose_table_iterator_free(iter);
  362. * ```
  363. *
  364. * @returns A new compose table iterator, or `NULL` on failure.
  365. *
  366. * @memberof xkb_compose_table_iterator
  367. * @sa xkb_compose_table_iterator_free()
  368. * @since 1.6.0
  369. */
  370. XKB_EXPORT struct xkb_compose_table_iterator *
  371. xkb_compose_table_iterator_new(struct xkb_compose_table *table);
  372. /**
  373. * Free a compose iterator.
  374. *
  375. * @memberof xkb_compose_table_iterator
  376. * @since 1.6.0
  377. */
  378. XKB_EXPORT void
  379. xkb_compose_table_iterator_free(struct xkb_compose_table_iterator *iter);
  380. /**
  381. * Get the next compose entry from a compose table iterator.
  382. *
  383. * The entries are returned in lexicographic order of the left-hand
  384. * side of entries. This does not correspond to the order in which
  385. * the entries appear in the Compose file.
  386. *
  387. * @attention The return value is valid until the next call to this function.
  388. *
  389. * Returns `NULL` in case there is no more entries.
  390. *
  391. * @memberof xkb_compose_table_iterator
  392. * @since 1.6.0
  393. */
  394. XKB_EXPORT struct xkb_compose_table_entry *
  395. xkb_compose_table_iterator_next(struct xkb_compose_table_iterator *iter);
  396. /** Flags for compose state creation. */
  397. enum xkb_compose_state_flags {
  398. /** Do not apply any flags. */
  399. XKB_COMPOSE_STATE_NO_FLAGS = 0
  400. };
  401. /**
  402. * Create a new compose state object.
  403. *
  404. * @param table
  405. * The compose table the state will use.
  406. * @param flags
  407. * Optional flags for the compose state, or 0.
  408. *
  409. * @returns A new compose state, or `NULL` on failure.
  410. *
  411. * @memberof xkb_compose_state
  412. */
  413. XKB_EXPORT struct xkb_compose_state *
  414. xkb_compose_state_new(struct xkb_compose_table *table,
  415. enum xkb_compose_state_flags flags);
  416. /**
  417. * Take a new reference on a compose state object.
  418. *
  419. * @returns The passed in object.
  420. *
  421. * @memberof xkb_compose_state
  422. */
  423. XKB_EXPORT struct xkb_compose_state *
  424. xkb_compose_state_ref(struct xkb_compose_state *state);
  425. /**
  426. * Release a reference on a compose state object, and possibly free it.
  427. *
  428. * @param state The object. If `NULL`, do nothing.
  429. *
  430. * @memberof xkb_compose_state
  431. */
  432. XKB_EXPORT void
  433. xkb_compose_state_unref(struct xkb_compose_state *state);
  434. /**
  435. * Get the compose table which a compose state object is using.
  436. *
  437. * @returns The compose table which was passed to xkb_compose_state_new()
  438. * when creating this state object.
  439. *
  440. * This function does not take a new reference on the compose table; you
  441. * must explicitly reference it yourself if you plan to use it beyond the
  442. * lifetime of the state.
  443. *
  444. * @memberof xkb_compose_state
  445. */
  446. XKB_EXPORT struct xkb_compose_table *
  447. xkb_compose_state_get_compose_table(struct xkb_compose_state *state);
  448. /** Status of the Compose sequence state machine. */
  449. enum xkb_compose_status {
  450. /** The initial state; no sequence has started yet. */
  451. XKB_COMPOSE_NOTHING,
  452. /** In the middle of a sequence. */
  453. XKB_COMPOSE_COMPOSING,
  454. /** A complete sequence has been matched. */
  455. XKB_COMPOSE_COMPOSED,
  456. /** The last sequence was cancelled due to an unmatched keysym. */
  457. XKB_COMPOSE_CANCELLED
  458. };
  459. /** The effect of a keysym fed to xkb_compose_state_feed(). */
  460. enum xkb_compose_feed_result {
  461. /** The keysym had no effect - it did not affect the status. */
  462. XKB_COMPOSE_FEED_IGNORED,
  463. /** The keysym started, advanced or cancelled a sequence. */
  464. XKB_COMPOSE_FEED_ACCEPTED
  465. };
  466. /**
  467. * Feed one keysym to the Compose sequence state machine.
  468. *
  469. * This function can advance into a compose sequence, cancel a sequence,
  470. * start a new sequence, or do nothing in particular. The resulting
  471. * status may be observed with `xkb_compose_state_get_status()`.
  472. *
  473. * Some keysyms, such as keysyms for modifier keys, are ignored - they
  474. * have no effect on the status or otherwise.
  475. *
  476. * The following is a description of the possible status transitions, in
  477. * the format CURRENT STATUS => NEXT STATUS, given a non-ignored input
  478. * keysym `keysym`:
  479. *
  480. @verbatim
  481. NOTHING or CANCELLED or COMPOSED =>
  482. NOTHING if keysym does not start a sequence.
  483. COMPOSING if keysym starts a sequence.
  484. COMPOSED if keysym starts and terminates a single-keysym sequence.
  485. COMPOSING =>
  486. COMPOSING if keysym advances any of the currently possible
  487. sequences but does not terminate any of them.
  488. COMPOSED if keysym terminates one of the currently possible
  489. sequences.
  490. CANCELLED if keysym does not advance any of the currently
  491. possible sequences.
  492. @endverbatim
  493. *
  494. * The current Compose formats do not support multiple-keysyms. Therefore, if
  495. * you are using a function such as `xkb_state::xkb_state_key_get_syms()`
  496. * and it returns more than one keysym, consider feeding `XKB_KEY_NoSymbol`
  497. * instead.
  498. *
  499. * @param state
  500. * The compose state object.
  501. * @param keysym
  502. * A keysym, usually obtained after a key-press event, with a
  503. * function such as `xkb_state::xkb_state_key_get_one_sym()`.
  504. *
  505. * @returns Whether the keysym was ignored. This is useful, for example,
  506. * if you want to keep a record of the sequence matched thus far.
  507. *
  508. * @memberof xkb_compose_state
  509. */
  510. XKB_EXPORT enum xkb_compose_feed_result
  511. xkb_compose_state_feed(struct xkb_compose_state *state,
  512. xkb_keysym_t keysym);
  513. /**
  514. * Reset the Compose sequence state machine.
  515. *
  516. * The status is set to `::XKB_COMPOSE_NOTHING`, and the current sequence
  517. * is discarded.
  518. *
  519. * @memberof xkb_compose_state
  520. */
  521. XKB_EXPORT void
  522. xkb_compose_state_reset(struct xkb_compose_state *state);
  523. /**
  524. * Get the current status of the compose state machine.
  525. *
  526. * @see xkb_compose_status
  527. * @memberof xkb_compose_state
  528. **/
  529. XKB_EXPORT enum xkb_compose_status
  530. xkb_compose_state_get_status(struct xkb_compose_state *state);
  531. /**
  532. * Get the result Unicode/UTF-8 string for a composed sequence.
  533. *
  534. * See @ref compose-overview for more details. This function is only
  535. * useful when the status is `::XKB_COMPOSE_COMPOSED`.
  536. *
  537. * @param[in] state
  538. * The compose state.
  539. * @param[out] buffer
  540. * A buffer to write the string into.
  541. * @param[in] size
  542. * Size of the buffer.
  543. *
  544. * @warning If the buffer passed is too small, the string is truncated
  545. * (though still `NULL`-terminated).
  546. *
  547. * @returns
  548. * The number of bytes required for the string, excluding the `NULL` byte.
  549. * If the sequence is not complete, or does not have a viable result
  550. * string, returns 0, and sets `buffer` to the empty string (if possible).
  551. * @returns
  552. * You may check if truncation has occurred by comparing the return value
  553. * with the size of `buffer`, similarly to the `snprintf(3)` function.
  554. * You may safely pass `NULL` and 0 to `buffer` and `size` to find the
  555. * required size (without the `NULL`-byte).
  556. *
  557. * @memberof xkb_compose_state
  558. **/
  559. XKB_EXPORT int
  560. xkb_compose_state_get_utf8(struct xkb_compose_state *state,
  561. char *buffer, size_t size);
  562. /**
  563. * Get the result keysym for a composed sequence.
  564. *
  565. * See @ref compose-overview for more details. This function is only
  566. * useful when the status is `::XKB_COMPOSE_COMPOSED`.
  567. *
  568. * @returns The result keysym. If the sequence is not complete, or does
  569. * not specify a result keysym, returns `XKB_KEY_NoSymbol`.
  570. *
  571. * @memberof xkb_compose_state
  572. **/
  573. XKB_EXPORT xkb_keysym_t
  574. xkb_compose_state_get_one_sym(struct xkb_compose_state *state);
  575. /** @} */
  576. #ifdef __cplusplus
  577. } /* extern "C" */
  578. #endif
  579. #endif /* _XKBCOMMON_COMPOSE_H */