xkbcommon-compose.h 16 KB

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