Add xkb_key_get_syms 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 <X11/X.h>
59
60 /**
61  * Returns the level to use for the given key and state, or -1 if invalid.
62  */
63 static int
64 xkb_key_get_level(struct xkb_desc *xkb, struct xkb_state *state,
65                   xkb_keycode_t key, unsigned int group)
66 {
67     struct xkb_key_type *type = XkbKeyType(xkb, key, group);
68     unsigned int active_mods = state->mods & type->mods.mask;
69     int i;
70
71     fprintf(stderr, "get_level: %s, %d entries, mods %x, mask %x (v%x r%x)\n",
72             type->name, type->map_count, state->mods, type->mods.mask,
73             type->mods.vmods, type->mods.real_mods);
74
75     for (i = 0; i < type->map_count; i++) {
76         if (!type->map[i].active)
77             continue;
78         fprintf(stderr, "    type active, level %d (%s), mask %x (v%x r%x), active %x\n",
79                 type->map[i].level, type->level_names[i], type->map[i].mods.mask,
80                 type->map[i].mods.real_mods, type->map[i].mods.vmods, active_mods);
81         if (type->map[i].mods.mask == active_mods)
82             return type->map[i].level;
83     }
84
85     return 0;
86 }
87
88 /**
89  * Returns the group to use for the given key and state, or -1 if invalid,
90  * taking wrapping/clamping/etc into account.
91  */
92 static int
93 xkb_key_get_group(struct xkb_desc *xkb, struct xkb_state *state,
94                   xkb_keycode_t key)
95 {
96     unsigned int info = XkbKeyGroupInfo(xkb, key);
97     unsigned int num_groups = XkbKeyNumGroups(xkb, key);
98     int ret = state->group;
99
100     if (ret < XkbKeyNumGroups(xkb, key))
101         return ret;
102
103     switch (XkbOutOfRangeGroupAction(info)) {
104     case XkbRedirectIntoRange:
105         ret = XkbOutOfRangeGroupInfo(info);
106         if (ret >= num_groups)
107             ret = 0;
108         break;
109     case XkbClampIntoRange:
110         ret = num_groups - 1;
111         break;
112     case XkbWrapIntoRange:
113     default:
114         ret %= num_groups;
115         break;
116     }
117
118     return ret;
119 }
120
121 /**
122  * Provides the symbols to use for the given key and state.  Returns the
123  * number of symbols pointed to in syms_out.
124  */
125 unsigned int
126 xkb_key_get_syms(struct xkb_desc *xkb, struct xkb_state *state,
127                  xkb_keycode_t key, xkb_keysym_t **syms_out)
128 {
129     int group;
130     int level;
131
132     if (!xkb || !state || key < xkb->min_key_code || key > xkb->max_key_code)
133         return -1;
134
135     group = xkb_key_get_group(xkb, state, key);
136     if (group == -1)
137         goto err;
138     level = xkb_key_get_level(xkb, state, key, group);
139     if (level == -1)
140         goto err;
141
142     *syms_out = &(XkbKeySymEntry(xkb, key, level, group));
143     if (**syms_out == NoSymbol)
144         goto err;
145
146     return 1;
147
148 err:
149     *syms_out = NULL;
150     return 0;
151 }