1 /*
2 * Copyright © 2013 Ran Benita
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
24 #include "config.h"
25
26 #include "x11-priv.h"
27
28 static bool
update_initial_state(struct xkb_state * state,xcb_connection_t * conn,uint16_t device_id)29 update_initial_state(struct xkb_state *state, xcb_connection_t *conn,
30 uint16_t device_id)
31 {
32 xcb_xkb_get_state_cookie_t cookie =
33 xcb_xkb_get_state(conn, device_id);
34 xcb_xkb_get_state_reply_t *reply =
35 xcb_xkb_get_state_reply(conn, cookie, NULL);
36
37 if (!reply)
38 return false;
39
40 xkb_state_update_mask(state,
41 reply->baseMods,
42 reply->latchedMods,
43 reply->lockedMods,
44 reply->baseGroup,
45 reply->latchedGroup,
46 reply->lockedGroup);
47
48 free(reply);
49 return true;
50 }
51
52 XKB_EXPORT struct xkb_state *
xkb_x11_state_new_from_device(struct xkb_keymap * keymap,xcb_connection_t * conn,int32_t device_id)53 xkb_x11_state_new_from_device(struct xkb_keymap *keymap,
54 xcb_connection_t *conn, int32_t device_id)
55 {
56 struct xkb_state *state;
57
58 if (device_id < 0 || device_id > 255) {
59 log_err_func(keymap->ctx, "illegal device ID: %d", device_id);
60 return NULL;
61 }
62
63 state = xkb_state_new(keymap);
64 if (!state)
65 return NULL;
66
67 if (!update_initial_state(state, conn, device_id)) {
68 xkb_state_unref(state);
69 return NULL;
70 }
71
72 return state;
73 }
74