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