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