e15dbac00b91901046ea41576cba3cee10f9b2d5
[platform/upstream/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 <stdint.h>
84 #include <stdio.h>
85 #include <X11/Xfuncproto.h>
86 #include <X11/extensions/XKB.h>
87
88 typedef uint32_t xkb_keycode_t;
89 typedef uint32_t xkb_keysym_t;
90 typedef uint32_t xkb_mod_index_t;
91 typedef uint32_t xkb_mod_mask_t;
92 typedef uint32_t xkb_group_index_t;
93 typedef uint32_t xkb_led_index_t;
94
95 #define XKB_MOD_INVALID                 (0xffffffff)
96 #define XKB_GROUP_INVALID               (0xffffffff)
97 #define XKB_KEYCODE_INVALID             (0xffffffff)
98 #define XKB_LED_INVALID                 (0xffffffff)
99
100 #define XKB_KEYSYM_NO_SYMBOL            0
101
102 #define XKB_KEYCODE_MAX                 (0xffffffff - 1)
103 #define xkb_keycode_is_legal_ext(kc)    (kc <= XKB_KEYCODE_MAX)
104 #define xkb_keycode_is_legal_x11(kc)    (kc <= XKB_KEYCODE_MAX)
105 #define xkb_keymap_keycode_range_is_legal(xkb) \
106     (xkb->max_key_code > 0 && \
107      xkb->max_key_code > xkb->min_key_code && \
108      xkb_keycode_is_legal_ext(xkb->min_key_code) && \
109      xkb_keycode_is_legal_ext(xkb->max_key_code))
110
111
112 /**
113  * Names to compile a keymap with, also known as RMLVO.  These names together
114  * should be the primary identifier for a keymap.
115  */
116 struct xkb_rule_names {
117     const char *rules;
118     const char *model;
119     const char *layout;
120     const char *variant;
121     const char *options;
122 };
123
124 /**
125  * Legacy names for the components of an XKB keymap, also known as KcCGST.
126  * This is only used in deprecated entrypoints which might be removed or
127  * shuffled off to a support library.
128  */
129 struct xkb_component_names {
130     char *keymap;
131     char *keycodes;
132     char *types;
133     char *compat;
134     char *symbols;
135 };
136
137 /**
138  * Opaque context object; may only be created, accessed, manipulated and
139  * destroyed through the xkb_context_*() API.
140  */
141 struct xkb_context;
142
143 /**
144  * Opaque keymap object; may only be created, accessed, manipulated and
145  * destroyed through the xkb_state_*() API.
146  */
147 struct xkb_desc;
148
149 /**
150  * Opaque state object; may only be created, accessed, manipulated and
151  * destroyed through the xkb_state_*() API.
152  */
153 struct xkb_state;
154
155 _XFUNCPROTOBEGIN
156
157 /*
158  * Canonicalises component names by prepending the relevant component from
159  * 'old' to the one in 'names' when the latter has a leading '+' or '|', and
160  * by replacing a '%' with the relevant component, e.g.:
161  *
162  * names        old           output
163  * ------------------------------------------
164  * +bar         foo           foo+bar
165  * |quux        baz           baz|quux
166  * foo+%|baz    bar           foo+bar|baz
167  *
168  * If a component in names needs to be modified, the existing value will be
169  * free()d, and a new one allocated with malloc().
170  */
171 _X_EXPORT extern void
172 xkb_canonicalise_components(struct xkb_component_names *names,
173                             const struct xkb_component_names *old);
174
175 /*
176  * Converts a keysym to a string; will return unknown Unicode codepoints
177  * as "Ua1b2", and other unknown keysyms as "0xabcd1234".
178  */
179 _X_EXPORT extern void
180 xkb_keysym_to_string(xkb_keysym_t ks, char *buffer, size_t size);
181
182 /*
183  * See xkb_keysym_to_string comments: this function will accept any string
184  * from that function.
185  */
186 _X_EXPORT extern xkb_keysym_t
187 xkb_string_to_keysym(const char *s);
188
189 _X_EXPORT unsigned int
190 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
191                  xkb_keysym_t **syms_out);
192
193 /**
194  * @defgroup ctx XKB contexts
195  * Every keymap compilation request must have an XKB context associated with
196  * it.  The context keeps around state such as the include path.
197  *
198  * @{
199  */
200
201 /**
202  * Returns a new XKB context, or NULL on failure.  If successful, the caller
203  * holds a reference on the context, and must free it when finished with
204  * xkb_context_unref().
205  */
206 _X_EXPORT struct xkb_context *
207 xkb_context_new(void);
208
209 /**
210  * Appends a new entry to the include path used for keymap compilation.
211  * Returns 1 on success, or 0 if the include path could not be added or is
212  * inaccessible.
213  */
214 _X_EXPORT int
215 xkb_context_include_path_append(struct xkb_context *context, const char *path);
216
217 /**
218  * Appends the default include paths to the context's current include path.
219  * Returns 1 on success, or 0 if the primary include path could not be
220  * added.
221  */
222 _X_EXPORT int
223 xkb_context_include_path_append_default(struct xkb_context *context);
224
225 /**
226  * Removes all entries from the context's include path, and inserts the
227  * default paths.  Returns 1 on success, or 0 if the primary include path
228  * could not be added.
229  */
230 _X_EXPORT int
231 xkb_context_include_path_reset(struct xkb_context *context);
232
233 /**
234  * Removes all entries from the context's include path.
235  */
236 _X_EXPORT void
237 xkb_context_include_path_clear(struct xkb_context *context);
238
239 /**
240  * Takes a new reference on an XKB context.
241  */
242 _X_EXPORT void
243 xkb_context_ref(struct xkb_context *context);
244
245 /**
246  * Releases a reference on an XKB context, and possibly frees it.
247  */
248 _X_EXPORT void
249 xkb_context_unref(struct xkb_context *context);
250
251 /** @} */
252
253 /**
254  * @defgroup map Keymap management
255  * These utility functions allow you to create and deallocate XKB keymaps.
256  *
257  * @{
258  */
259
260 /**
261  * The primary keymap entry point: creates a new XKB keymap from a set of
262  * RMLVO (Rules + Model + Layout + Variant + Option) names.
263  *
264  * You should almost certainly be using this and nothing else to create
265  * keymaps.
266  */
267 _X_EXPORT extern struct xkb_desc *
268 xkb_map_new_from_names(const struct xkb_rule_names *names);
269
270 /**
271  * Deprecated entrypoint for legacy users who need to be able to compile
272  * XKB keymaps by KcCGST (Keycodes + Compat + Geometry + Symbols + Types)
273  * names.
274  *
275  * You should not use this unless you are the X server.  This entrypoint
276  * may well disappear in future releases.  Please, please, don't use it.
277  *
278  * Geometry will be ignored since xkbcommon does not support it in any way.
279  */
280 _X_EXPORT extern struct xkb_desc *
281 xkb_map_new_from_kccgst(const struct xkb_component_names *kccgst);
282
283 enum xkb_keymap_format {
284     /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
285     XKB_KEYMAP_FORMAT_TEXT_V1 = 1,
286 };
287
288 /**
289  * Creates an XKB keymap from a full text XKB keymap passed into the
290  * file descriptor.
291  */
292 _X_EXPORT extern struct xkb_desc *
293 xkb_map_new_from_fd(int fd, enum xkb_keymap_format format);
294
295 /**
296  * Creates an XKB keymap from a full text XKB keymap serialised into one
297  * enormous string.
298  */
299 _X_EXPORT extern struct xkb_desc *
300 xkb_map_new_from_string(const char *string, enum xkb_keymap_format format);
301
302 /**
303  * Takes a new reference on a keymap.
304  */
305 _X_EXPORT extern void
306 xkb_map_ref(struct xkb_desc *xkb);
307
308 /**
309  * Releases a reference on a keymap.
310  */
311 _X_EXPORT extern void
312 xkb_map_unref(struct xkb_desc *xkb);
313
314 /** @} */
315
316 /**
317  * @defgroup components XKB state components
318  * Allows enumeration of state components such as modifiers and groups within
319  * the current keymap.
320  *
321  * @{
322  */
323
324 /**
325  * Returns the number of modifiers active in the keymap.
326  */
327 _X_EXPORT xkb_mod_index_t
328 xkb_map_num_mods(struct xkb_desc *xkb);
329
330 /**
331  * Returns the name of the modifier specified by 'idx', or NULL if invalid.
332  */
333 _X_EXPORT const char *
334 xkb_map_mod_get_name(struct xkb_desc *xkb, xkb_mod_index_t idx);
335
336 /**
337  * Returns the index of the modifier specified by 'name', or XKB_MOD_INVALID.
338  */
339 _X_EXPORT xkb_mod_index_t
340 xkb_map_mod_get_index(struct xkb_desc *xkb, const char *name);
341
342 /**
343  * Returns the number of groups active in the keymap.
344  */
345 _X_EXPORT xkb_group_index_t
346 xkb_map_num_groups(struct xkb_desc *xkb);
347
348 /**
349  * Returns the name of the group specified by 'idx', or NULL if invalid.
350  */
351 _X_EXPORT const char *
352 xkb_map_group_get_name(struct xkb_desc *xkb, xkb_group_index_t idx);
353
354 /**
355  * Returns the index of the group specified by 'name', or XKB_GROUP_INVALID.
356  */
357 _X_EXPORT xkb_group_index_t
358 xkb_map_group_get_index(struct xkb_desc *xkb, const char *name);
359
360 /**
361  * Returns the number of groups active for the specified key.
362  */
363 _X_EXPORT xkb_group_index_t
364 xkb_key_num_groups(struct xkb_desc *xkb, xkb_keycode_t key);
365
366 /**
367  * Returns the number of LEDs in the given map.
368  */
369 _X_EXPORT xkb_led_index_t
370 xkb_map_num_leds(struct xkb_desc *xkb);
371
372 /**
373  * Returns the name of the LED specified by 'idx', or NULL if invalid.
374  */
375 _X_EXPORT const char *
376 xkb_map_led_get_name(struct xkb_desc *xkb, xkb_led_index_t idx);
377
378 /**
379  * Returns the index of the LED specified by 'name', or XKB_LED_INVALID.
380  */
381 _X_EXPORT xkb_led_index_t
382 xkb_map_led_get_index(struct xkb_desc *xkb, const char *name);
383
384 /** @} */
385
386 /**
387  * @defgroup state XKB state objects
388  * Creation, destruction and manipulation of keyboard state objects,
389  * representing modifier and group state.
390  *
391  * @{
392  */
393
394 /**
395  * Returns a new XKB state object for use with the given keymap, or NULL on
396  * failure.
397  */
398 _X_EXPORT struct xkb_state *
399 xkb_state_new(struct xkb_desc *xkb);
400
401 /**
402  * Adds a reference to a state object, so it will not be freed until unref.
403  */
404 _X_EXPORT void
405 xkb_state_ref(struct xkb_state *state);
406
407 /**
408  * Unrefs and potentially deallocates a state object; the caller must not
409  * use the state object after calling this.
410  */
411 _X_EXPORT void
412 xkb_state_unref(struct xkb_state *state);
413
414 enum xkb_key_direction {
415     XKB_KEY_UP,
416     XKB_KEY_DOWN,
417 };
418
419 /**
420  * Updates a state object to reflect the given key being pressed or released.
421  */
422 _X_EXPORT void
423 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
424                      enum xkb_key_direction direction);
425
426 /**
427  * Modifier and group types for state objects.  This enum is bitmaskable,
428  * e.g. (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED) is valid to exclude
429  * locked modifiers.
430  */
431 enum xkb_state_component {
432     /** A key holding this modifier or group is currently physically
433      *  depressed; also known as 'base'. */
434     XKB_STATE_DEPRESSED = (1 << 0),
435     /** Modifier or group is latched, i.e. will be unset after the next
436      *  non-modifier key press. */
437     XKB_STATE_LATCHED = (1 << 1),
438     /** Modifier or group is locked, i.e. will be unset after the key
439      *  provoking the lock has been pressed again. */
440     XKB_STATE_LOCKED = (1 << 2),
441     /** Combinatination of depressed, latched, and locked. */
442     XKB_STATE_EFFECTIVE =
443         (XKB_STATE_DEPRESSED | XKB_STATE_LATCHED | XKB_STATE_LOCKED),
444 };
445
446 /**
447  * Updates a state object from a set of explicit masks.  This entrypoint is
448  * really only for window systems and the like, where a master process
449  * holds an xkb_state, then serialises it over a wire protocol, and clients
450  * then use the serialisation to feed in to their own xkb_state.
451  *
452  * All parameters must always be passed, or the resulting state may be
453  * incoherent.
454  *
455  * The serialisation is lossy and will not survive round trips; it must only
456  * be used to feed slave state objects, and must not be used to update the
457  * master state.
458  *
459  * Please do not use this unless you fit the description above.
460  */
461 _X_EXPORT void
462 xkb_state_update_mask(struct xkb_state *state,
463                       xkb_mod_mask_t base_mods,
464                       xkb_mod_mask_t latched_mods,
465                       xkb_mod_mask_t locked_mods,
466                       xkb_group_index_t base_group,
467                       xkb_group_index_t latched_group,
468                       xkb_group_index_t locked_group);
469
470 /**
471  * The counterpart to xkb_state_update_mask, to be used on the server side
472  * of serialisation.  Returns a xkb_mod_mask_t representing the given
473  * component(s) of the state.
474  *
475  * This function should not be used in regular clients; please use the
476  * xkb_state_mod_*_is_active or xkb_state_foreach_active_mod API instead.
477  *
478  * Can return NULL on failure.
479  */
480 _X_EXPORT xkb_mod_mask_t
481 xkb_state_serialise_mods(struct xkb_state *state,
482                          enum xkb_state_component component);
483
484 /**
485  * The group equivalent of xkb_state_serialise_mods: please see its
486  * documentation.
487  */
488 _X_EXPORT xkb_group_index_t
489 xkb_state_serialise_group(struct xkb_state *state,
490                           enum xkb_state_component component);
491
492 /**
493  * Returns 1 if the modifier specified by 'name' is active in the manner
494  * specified by 'type', 0 if it is unset, or -1 if the modifier does not
495  * exist in the current map.
496  */
497 _X_EXPORT int
498 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
499                              enum xkb_state_component type);
500
501 /**
502  * Returns 1 if the modifier specified by 'idx' is active in the manner
503  * specified by 'type', 0 if it is unset, or -1 if the modifier does not
504  * exist in the current map.
505  */
506 _X_EXPORT int
507 xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
508                               enum xkb_state_component type);
509
510 /**
511  * Returns 1 if the group specified by 'name' is active in the manner
512  * specified by 'type', 0 if it is unset, or -1 if the group does not
513  * exist in the current map.
514  */
515 _X_EXPORT int
516 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
517                                enum xkb_state_component type);
518
519 /**
520  * Returns 1 if the group specified by 'idx' is active in the manner
521  * specified by 'type', 0 if it is unset, or -1 if the group does not
522  * exist in the current map.
523  */
524 _X_EXPORT int
525 xkb_state_group_index_is_active(struct xkb_state *state, xkb_group_index_t idx,
526                                 enum xkb_state_component type);
527
528 /**
529  * Returns 1 if the LED specified by 'name' is active, 0 if it is unset, or
530  * -1 if the LED does not exist in the current map.
531  */
532 _X_EXPORT int
533 xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
534
535 /**
536  * Returns 1 if the LED specified by 'idx' is active, 0 if it is unset, or
537  * -1 if the LED does not exist in the current map.
538  */
539 _X_EXPORT int
540 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
541
542 /** @} */
543
544 _XFUNCPROTOEND
545
546 #endif /* _XKBCOMMON_H_ */