Add xkb_map_get_as_string
[profile/ivi/libxkbcommon.git] / include / xkbcommon / xkbcommon.h
1 /*
2 Copyright 1985, 1987, 1990, 1998  The Open Group
3 Copyright 2008  Dan Nicholson
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 shall be included in
13 all copies or substantial portions of the 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 THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
26 */
27
28 /************************************************************
29 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
30
31 Permission to use, copy, modify, and distribute this
32 software and its documentation for any purpose and without
33 fee is hereby granted, provided that the above copyright
34 notice appear in all copies and that both that copyright
35 notice and this permission notice appear in supporting
36 documentation, and that the name of Silicon Graphics not be
37 used in advertising or publicity pertaining to distribution
38 of the software without specific prior written permission.
39 Silicon Graphics makes no representation about the suitability
40 of this software for any purpose. It is provided "as is"
41 without any express or implied warranty.
42
43 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
44 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
45 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
46 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
47 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
48 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
49 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
50 THE USE OR PERFORMANCE OF THIS SOFTWARE.
51
52 ********************************************************/
53
54 /*
55  * Copyright © 2009 Daniel Stone
56  *
57  * Permission is hereby granted, free of charge, to any person obtaining a
58  * copy of this software and associated documentation files (the "Software"),
59  * to deal in the Software without restriction, including without limitation
60  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
61  * and/or sell copies of the Software, and to permit persons to whom the
62  * Software is furnished to do so, subject to the following conditions:
63  *
64  * The above copyright notice and this permission notice (including the next
65  * paragraph) shall be included in all copies or substantial portions of the
66  * Software.
67  *
68  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
69  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
70  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
71  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
72  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
73  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
74  * DEALINGS IN THE SOFTWARE.
75  *
76  * Author: Daniel Stone <daniel@fooishbar.org>
77  */
78
79
80 #ifndef _XKBCOMMON_H_
81 #define _XKBCOMMON_H_
82
83 #include <stddef.h>
84 #include <stdint.h>
85 #include <stdio.h>
86
87 #include <xkbcommon/xkbcommon-names.h>
88 #include <xkbcommon/xkbcommon-keysyms.h>
89
90 #define XKB_KEY_NoSymbol             0L /* special KeySym */
91
92 typedef uint32_t xkb_keycode_t;
93 typedef uint32_t xkb_keysym_t;
94 typedef uint32_t xkb_mod_index_t;
95 typedef uint32_t xkb_mod_mask_t;
96 typedef uint32_t xkb_group_index_t;
97 typedef uint32_t xkb_led_index_t;
98
99 #define XKB_MOD_INVALID                 (0xffffffff)
100 #define XKB_GROUP_INVALID               (0xffffffff)
101 #define XKB_KEYCODE_INVALID             (0xffffffff)
102 #define XKB_LED_INVALID                 (0xffffffff)
103
104 #define XKB_KEYCODE_MAX                 (0xffffffff - 1)
105 #define xkb_keycode_is_legal_ext(kc)    (kc <= XKB_KEYCODE_MAX)
106 #define xkb_keycode_is_legal_x11(kc)    (kc >= 8 && kc <= 255)
107
108 /**
109  * Names to compile a keymap with, also known as RMLVO.  These names together
110  * should be the primary identifier for a keymap.
111  */
112 struct xkb_rule_names {
113     const char *rules;
114     const char *model;
115     const char *layout;
116     const char *variant;
117     const char *options;
118 };
119
120 /**
121  * Opaque context object; may only be created, accessed, manipulated and
122  * destroyed through the xkb_context_*() API.
123  */
124 struct xkb_context;
125
126 /**
127  * Opaque keymap object; may only be created, accessed, manipulated and
128  * destroyed through the xkb_state_*() API.
129  */
130 struct xkb_keymap;
131
132 /**
133  * Opaque state object; may only be created, accessed, manipulated and
134  * destroyed through the xkb_state_*() API.
135  */
136 struct xkb_state;
137
138 #ifdef __cplusplus
139 extern "C" {
140 #endif
141
142 /*
143  * Returns the name for a keysym as a string; will return unknown Unicode
144  * codepoints as "Ua1b2", and other unknown keysyms as "0xabcd1234".
145  */
146 void
147 xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size);
148
149 /*
150  * See xkb_keysym_to_string comments: this function will accept any string
151  * from that function.
152  */
153 xkb_keysym_t
154 xkb_keysym_from_name(const char *s);
155
156 /**
157  * @defgroup context XKB contexts
158  * Every keymap compilation request must have an XKB context associated with
159  * it.  The context keeps around state such as the include path.
160  *
161  * @{
162  */
163
164 enum xkb_context_flags {
165     /** Create this context with an empty include path. */
166     XKB_CONTEXT_NO_DEFAULT_INCLUDES = 1,
167 };
168
169 /**
170  * Returns a new XKB context, or NULL on failure.  If successful, the caller
171  * holds a reference on the context, and must free it when finished with
172  * xkb_context_unref().
173  */
174 struct xkb_context *
175 xkb_context_new(enum xkb_context_flags flags);
176
177 /**
178  * Appends a new entry to the include path used for keymap compilation.
179  * Returns 1 on success, or 0 if the include path could not be added or is
180  * inaccessible.
181  */
182 int
183 xkb_context_include_path_append(struct xkb_context *context, const char *path);
184
185 /**
186  * Appends the default include paths to the context's current include path.
187  * Returns 1 on success, or 0 if the primary include path could not be
188  * added.
189  */
190 int
191 xkb_context_include_path_append_default(struct xkb_context *context);
192
193 /**
194  * Removes all entries from the context's include path, and inserts the
195  * default paths.  Returns 1 on success, or 0 if the primary include path
196  * could not be added.
197  */
198 int
199 xkb_context_include_path_reset_defaults(struct xkb_context *context);
200
201 /**
202  * Removes all entries from the context's include path.
203  */
204 void
205 xkb_context_include_path_clear(struct xkb_context *context);
206
207 /**
208  * Returns the number of include paths currently active in the context.
209  */
210 unsigned int
211 xkb_context_num_include_paths(struct xkb_context *context);
212
213 /**
214  * Returns the include path at the specified index within the context.
215  */
216 const char *
217 xkb_context_include_path_get(struct xkb_context *context, unsigned int index);
218
219 /**
220  * Takes a new reference on an XKB context.
221  */
222 struct xkb_context *
223 xkb_context_ref(struct xkb_context *context);
224
225 /**
226  * Releases a reference on an XKB context, and possibly frees it.
227  */
228 void
229 xkb_context_unref(struct xkb_context *context);
230
231 /** @} */
232
233 /**
234  * @defgroup map Keymap management
235  * These utility functions allow you to create and deallocate XKB keymaps.
236  *
237  * @{
238  */
239
240 enum xkb_map_compile_flags {
241     /** Apparently you can't have empty enums.  What a drag. */
242     XKB_MAP_COMPILE_PLACEHOLDER = 0,
243 };
244
245 /**
246  * The primary keymap entry point: creates a new XKB keymap from a set of
247  * RMLVO (Rules + Model + Layout + Variant + Option) names.
248  *
249  * You should almost certainly be using this and nothing else to create
250  * keymaps.
251  */
252 struct xkb_keymap *
253 xkb_map_new_from_names(struct xkb_context *context,
254                        const struct xkb_rule_names *names,
255                        enum xkb_map_compile_flags flags);
256
257 enum xkb_keymap_format {
258     /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
259     XKB_KEYMAP_FORMAT_TEXT_V1 = 1,
260 };
261
262 /**
263  * Creates an XKB keymap from a full text XKB keymap passed into the
264  * file.
265  */
266 struct xkb_keymap *
267 xkb_map_new_from_file(struct xkb_context *context,
268                     FILE *file, enum xkb_keymap_format format,
269                     enum xkb_map_compile_flags flags);
270
271 /**
272  * Creates an XKB keymap from a full text XKB keymap serialized into one
273  * enormous string.
274  */
275 struct xkb_keymap *
276 xkb_map_new_from_string(struct xkb_context *context,
277                         const char *string,
278                         enum xkb_keymap_format format,
279                         enum xkb_map_compile_flags flags);
280
281 /**
282  * Returns the compiled XKB map as a string which can later be fed back into
283  * xkb_map_new_from_string to return the exact same keymap.
284  */
285 char *
286 xkb_map_get_as_string(struct xkb_keymap *keymap);
287
288 /**
289  * Takes a new reference on a keymap.
290  */
291 struct xkb_keymap *
292 xkb_map_ref(struct xkb_keymap *keymap);
293
294 /**
295  * Releases a reference on a keymap.
296  */
297 void
298 xkb_map_unref(struct xkb_keymap *keymap);
299
300 /** @} */
301
302 /**
303  * @defgroup components XKB state components
304  * Allows enumeration of state components such as modifiers and groups within
305  * the current keymap.
306  *
307  * @{
308  */
309
310 /**
311  * Returns the number of modifiers active in the keymap.
312  */
313 xkb_mod_index_t
314 xkb_map_num_mods(struct xkb_keymap *keymap);
315
316 /**
317  * Returns the name of the modifier specified by 'idx', or NULL if invalid.
318  */
319 const char *
320 xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx);
321
322 /**
323  * Returns the index of the modifier specified by 'name', or XKB_MOD_INVALID.
324  */
325 xkb_mod_index_t
326 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name);
327
328 /**
329  * Returns the number of groups active in the keymap.
330  */
331 xkb_group_index_t
332 xkb_map_num_groups(struct xkb_keymap *keymap);
333
334 /**
335  * Returns the name of the group specified by 'idx', or NULL if invalid.
336  */
337 const char *
338 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx);
339
340 /**
341  * Returns the index of the group specified by 'name', or XKB_GROUP_INVALID.
342  */
343 xkb_group_index_t
344 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name);
345
346 /**
347  * Returns the number of groups active for the specified key.
348  */
349 xkb_group_index_t
350 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t key);
351
352 /**
353  * Returns the number of LEDs in the given map.
354  */
355 xkb_led_index_t
356 xkb_map_num_leds(struct xkb_keymap *keymap);
357
358 /**
359  * Returns the name of the LED specified by 'idx', or NULL if invalid.
360  */
361 const char *
362 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx);
363
364 /**
365  * Returns the index of the LED specified by 'name', or XKB_LED_INVALID.
366  */
367 xkb_led_index_t
368 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name);
369
370 /** @} */
371
372 /**
373  * @defgroup state XKB state objects
374  * Creation, destruction and manipulation of keyboard state objects,
375  * representing modifier and group state.
376  *
377  * @{
378  */
379
380 /**
381  * Returns a new XKB state object for use with the given keymap, or NULL on
382  * failure.
383  */
384 struct xkb_state *
385 xkb_state_new(struct xkb_keymap *keymap);
386
387 /**
388  * Takes a new reference on a state object.
389  */
390 struct xkb_state *
391 xkb_state_ref(struct xkb_state *state);
392
393 /**
394  * Unrefs and potentially deallocates a state object; the caller must not
395  * use the state object after calling this.
396  */
397 void
398 xkb_state_unref(struct xkb_state *state);
399
400 /**
401  * Get the keymap from which the state object was created.  Does not take
402  * a new reference on the map; you must explicitly reference it yourself
403  * if you plan to use it beyond the lifetime of the state.
404  */
405 struct xkb_keymap *
406 xkb_state_get_map(struct xkb_state *state);
407
408 enum xkb_key_direction {
409     XKB_KEY_UP,
410     XKB_KEY_DOWN,
411 };
412
413 /**
414  * Updates a state object to reflect the given key being pressed or released.
415  */
416 void
417 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
418                      enum xkb_key_direction direction);
419
420 /**
421  * Gives the symbols obtained from pressing a particular key with the given
422  * state.  *syms_out will be set to point to an array of keysyms, with the
423  * return value being the number of symbols in *syms_out.  If the return
424  * value is 0, *syms_out will be set to NULL, as there are no symbols produced
425  * by this event.
426  *
427  * This should be called before xkb_state_update_key.
428  */
429 int
430 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
431                  const xkb_keysym_t **syms_out);
432
433 /**
434  * Modifier and group types for state objects.  This enum is bitmaskable,
435  * e.g. (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED) is valid to exclude
436  * locked modifiers.
437  */
438 enum xkb_state_component {
439     /** A key holding this modifier or group is currently physically
440      *  depressed; also known as 'base'. */
441     XKB_STATE_DEPRESSED = (1 << 0),
442     /** Modifier or group is latched, i.e. will be unset after the next
443      *  non-modifier key press. */
444     XKB_STATE_LATCHED = (1 << 1),
445     /** Modifier or group is locked, i.e. will be unset after the key
446      *  provoking the lock has been pressed again. */
447     XKB_STATE_LOCKED = (1 << 2),
448     /** Combinatination of depressed, latched, and locked. */
449     XKB_STATE_EFFECTIVE =
450         (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED | XKB_STATE_LOCKED),
451 };
452
453 /**
454  * Match flags for xkb_state_mod_indices_are_active and
455  * xkb_state_mod_names_are_active, specifying how the conditions for a
456  * successful match.  XKB_STATE_MATCH_NON_EXCLUSIVE is bitmaskable with
457  * the other modes.
458  */
459 enum xkb_state_match {
460     /** Returns true if any of the modifiers are active. */
461     XKB_STATE_MATCH_ANY = (1 << 0),
462     /** Returns true if all of the modifiers are active. */
463     XKB_STATE_MATCH_ALL = (1 << 1),
464     /** Makes matching non-exclusive, i.e. will not return false if a
465      *  modifier not specified in the arguments is active. */
466     XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16),
467 };
468
469 /**
470  * Updates a state object from a set of explicit masks.  This entrypoint is
471  * really only for window systems and the like, where a master process
472  * holds an xkb_state, then serializes it over a wire protocol, and clients
473  * then use the serialization to feed in to their own xkb_state.
474  *
475  * All parameters must always be passed, or the resulting state may be
476  * incoherent.
477  *
478  * The serialization is lossy and will not survive round trips; it must only
479  * be used to feed slave state objects, and must not be used to update the
480  * master state.
481  *
482  * Please do not use this unless you fit the description above.
483  */
484 void
485 xkb_state_update_mask(struct xkb_state *state,
486                       xkb_mod_mask_t base_mods,
487                       xkb_mod_mask_t latched_mods,
488                       xkb_mod_mask_t locked_mods,
489                       xkb_group_index_t base_group,
490                       xkb_group_index_t latched_group,
491                       xkb_group_index_t locked_group);
492
493 /**
494  * The counterpart to xkb_state_update_mask, to be used on the server side
495  * of serialization.  Returns a xkb_mod_mask_t representing the given
496  * component(s) of the state.
497  *
498  * This function should not be used in regular clients; please use the
499  * xkb_state_mod_*_is_active or xkb_state_foreach_active_mod API instead.
500  *
501  * Can return NULL on failure.
502  */
503 xkb_mod_mask_t
504 xkb_state_serialize_mods(struct xkb_state *state,
505                          enum xkb_state_component component);
506
507 /**
508  * The group equivalent of xkb_state_serialize_mods: please see its
509  * documentation.
510  */
511 xkb_group_index_t
512 xkb_state_serialize_group(struct xkb_state *state,
513                           enum xkb_state_component component);
514
515 /**
516  * Returns 1 if the modifier specified by 'name' is active in the manner
517  * specified by 'type', 0 if it is unset, or -1 if the modifier does not
518  * exist in the map.
519  */
520 int
521 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
522                              enum xkb_state_component type);
523
524 /**
525  * Returns 1 if the modifiers specified by the varargs (treated as
526  * NULL-terminated pointers to strings) are active in the manner
527  * specified by 'match', 0 otherwise, or -1 if any of the modifiers
528  * do not exist in the map.
529  */
530 int
531 xkb_state_mod_names_are_active(struct xkb_state *state,
532                                enum xkb_state_component type,
533                                enum xkb_state_match match,
534                                ...);
535
536 /**
537  * Returns 1 if the modifier specified by 'idx' is active in the manner
538  * specified by 'type', 0 if it is unset, or -1 if the modifier does not
539  * exist in the current map.
540  */
541 int
542 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
543                               enum xkb_state_component type);
544
545 /**
546  * Returns 1 if the modifiers specified by the varargs (treated as
547  * xkb_mod_index_t, terminated with XKB_MOD_INVALID) are active in the manner
548  * specified by 'match' and 'type', 0 otherwise, or -1 if the modifier does not
549  * exist in the current map.
550  */
551 int
552 xkb_state_mod_indices_are_active(struct xkb_state *state,
553                                  enum xkb_state_component type,
554                                  enum xkb_state_match match,
555                                  ...);
556
557 /**
558  * Returns 1 if the group specified by 'name' is active in the manner
559  * specified by 'type', 0 if it is unset, or -1 if the group does not
560  * exist in the current map.
561  */
562 int
563 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
564                                enum xkb_state_component type);
565
566 /**
567  * Returns 1 if the group specified by 'idx' is active in the manner
568  * specified by 'type', 0 if it is unset, or -1 if the group does not
569  * exist in the current map.
570  */
571 int
572 xkb_state_group_index_is_active(struct xkb_state *state, xkb_group_index_t idx,
573                                 enum xkb_state_component type);
574
575 /**
576  * Returns 1 if the LED specified by 'name' is active, 0 if it is unset, or
577  * -1 if the LED does not exist in the current map.
578  */
579 int
580 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
581
582 /**
583  * Returns 1 if the LED specified by 'idx' is active, 0 if it is unset, or
584  * -1 if the LED does not exist in the current map.
585  */
586 int
587 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
588
589 /** @} */
590
591 #ifdef __cplusplus
592 } /* extern "C" */
593 #endif
594
595 #endif /* _XKBCOMMON_H_ */