Add new state API
[platform/upstream/libxkbcommon.git] / src / map.c
1 /**
2  * Copyright © 2012 Intel Corporation
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  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 /************************************************************
27  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
28  *
29  * Permission to use, copy, modify, and distribute this
30  * software and its documentation for any purpose and without
31  * fee is hereby granted, provided that the above copyright
32  * notice appear in all copies and that both that copyright
33  * notice and this permission notice appear in supporting
34  * documentation, and that the name of Silicon Graphics not be
35  * used in advertising or publicity pertaining to distribution
36  * of the software without specific prior written permission.
37  * Silicon Graphics makes no representation about the suitability
38  * of this software for any purpose. It is provided "as is"
39  * without any express or implied warranty.
40  *
41  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
42  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
43  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
44  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
45  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
46  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
47  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
48  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
49  *
50  * ********************************************************/
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include "xkbcommon/xkbcommon.h"
57 #include "XKBcommonint.h"
58 #include "xkballoc.h"
59 #include <X11/X.h>
60
61 /**
62  * Returns the level to use for the given key and state, or -1 if invalid.
63  */
64 unsigned int
65 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
66                   unsigned int group)
67 {
68     struct xkb_key_type *type = XkbKeyType(state->xkb, key, group);
69     unsigned int active_mods = state->mods & type->mods.mask;
70     int i;
71
72     for (i = 0; i < type->map_count; i++) {
73         if (!type->map[i].active)
74             continue;
75         if (type->map[i].mods.mask == active_mods)
76             return type->map[i].level;
77     }
78
79     return 0;
80 }
81
82 /**
83  * Returns the group to use for the given key and state, taking
84  * wrapping/clamping/etc into account.
85  */
86 unsigned int
87 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
88 {
89     unsigned int info = XkbKeyGroupInfo(state->xkb, key);
90     unsigned int num_groups = XkbKeyNumGroups(state->xkb, key);
91     int ret = state->group;
92
93     if (ret < XkbKeyNumGroups(state->xkb, key))
94         return ret;
95
96     switch (XkbOutOfRangeGroupAction(info)) {
97     case XkbRedirectIntoRange:
98         ret = XkbOutOfRangeGroupInfo(info);
99         if (ret >= num_groups)
100             ret = 0;
101         break;
102     case XkbClampIntoRange:
103         ret = num_groups - 1;
104         break;
105     case XkbWrapIntoRange:
106     default:
107         ret %= num_groups;
108         break;
109     }
110
111     return ret;
112 }
113
114 /**
115  * As below, but takes an explicit group/level rather than state.
116  */
117 unsigned int
118 xkb_key_get_syms_by_level(struct xkb_desc *xkb, xkb_keycode_t key, unsigned int group,
119                           unsigned int level, xkb_keysym_t **syms_out)
120 {
121     *syms_out = &(XkbKeySymEntry(xkb, key, level, group));
122     if (**syms_out == NoSymbol)
123         goto err;
124
125     return 1;
126
127 err:
128     *syms_out = NULL;
129     return 0;
130 }
131
132 /**
133  * Provides the symbols to use for the given key and state.  Returns the
134  * number of symbols pointed to in syms_out.
135  */
136 unsigned int
137 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
138                  xkb_keysym_t **syms_out)
139 {
140     struct xkb_desc *xkb = state->xkb;
141     int group;
142     int level;
143
144     if (!state || key < xkb->min_key_code || key > xkb->max_key_code)
145         return -1;
146
147     group = xkb_key_get_group(state, key);
148     if (group == -1)
149         goto err;
150     level = xkb_key_get_level(state, key, group);
151     if (level == -1)
152         goto err;
153
154     return xkb_key_get_syms_by_level(xkb, key, group, level, syms_out);
155
156 err:
157     *syms_out = NULL;
158     return 0;
159 }