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