uterm: input: use uxkb directly
[platform/upstream/kmscon.git] / src / uterm_input_uxkb.c
1 /*
2  * uterm - Linux User-Space Terminal
3  *
4  * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
5  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <linux/input.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <xkbcommon/xkbcommon.h>
34 #include "log.h"
35 #include "shl_misc.h"
36 #include "uterm.h"
37 #include "uterm_input.h"
38
39 #define LOG_SUBSYSTEM "input_uxkb"
40
41 int uxkb_desc_init(struct uterm_input *input,
42                    const char *layout,
43                    const char *variant,
44                    const char *options)
45 {
46         int ret;
47         struct xkb_rule_names rmlvo = {
48                 .rules = "evdev",
49                 .model = "evdev",
50                 .layout = layout,
51                 .variant = variant,
52                 .options = options,
53         };
54
55         input->ctx = xkb_context_new(0);
56         if (!input->ctx) {
57                 log_error("cannot create XKB context");
58                 return -ENOMEM;
59         }
60
61         input->keymap = xkb_map_new_from_names(input->ctx, &rmlvo, 0);
62         if (!input->keymap) {
63                 log_warn("failed to create keymap (%s, %s, %s), "
64                          "reverting to default US keymap",
65                          layout, variant, options);
66
67                 rmlvo.layout = "us";
68                 rmlvo.variant = "";
69                 rmlvo.options = "";
70
71                 input->keymap = xkb_map_new_from_names(input->ctx, &rmlvo, 0);
72                 if (!input->keymap) {
73                         log_warn("failed to create XKB keymap");
74                         ret = -EFAULT;
75                         goto err_ctx;
76                 }
77         }
78
79         log_debug("new keyboard description (%s, %s, %s)",
80                   layout, variant, options);
81         return 0;
82
83 err_ctx:
84         xkb_context_unref(input->ctx);
85         return ret;
86 }
87
88 void uxkb_desc_destroy(struct uterm_input *input)
89 {
90         xkb_map_unref(input->keymap);
91         xkb_context_unref(input->ctx);
92 }
93
94 int uxkb_dev_init(struct uterm_input_dev *dev)
95 {
96         dev->state = xkb_state_new(dev->input->keymap);
97         if (!dev->state)
98                 return -ENOMEM;
99
100         return 0;
101 }
102
103 void uxkb_dev_destroy(struct uterm_input_dev *dev)
104 {
105         xkb_state_unref(dev->state);
106 }
107
108 #define EVDEV_KEYCODE_OFFSET 8
109 enum {
110         KEY_RELEASED = 0,
111         KEY_PRESSED = 1,
112         KEY_REPEATED = 2,
113 };
114
115 int uxkb_dev_process(struct uterm_input_dev *dev,
116                      uint16_t key_state,
117                      uint16_t code,
118                      struct uterm_input_event *out)
119 {
120         struct xkb_state *state;
121         struct xkb_keymap *keymap;
122         xkb_keycode_t keycode;
123         const xkb_keysym_t *keysyms;
124         int num_keysyms;
125
126         state = dev->state;
127         keymap = xkb_state_get_map(state);
128         keycode = code + EVDEV_KEYCODE_OFFSET;
129
130         num_keysyms = xkb_key_get_syms(state, keycode, &keysyms);
131
132         if (key_state == KEY_PRESSED)
133                 xkb_state_update_key(state, keycode, XKB_KEY_DOWN);
134         else if (key_state == KEY_RELEASED)
135                 xkb_state_update_key(state, keycode, XKB_KEY_UP);
136
137         if (key_state == KEY_RELEASED)
138                 return -ENOKEY;
139
140         if (key_state == KEY_REPEATED && !xkb_key_repeats(keymap, keycode))
141                 return -ENOKEY;
142
143         if (num_keysyms <= 0)
144                 return -ENOKEY;
145
146         /*
147          * TODO: xkbcommon actually supports multiple keysyms
148          * per key press. Here we're just using the first one,
149          * but we might want to support this feature.
150          */
151         out->keycode = code;
152         out->keysym = keysyms[0];
153         out->mods = shl_get_xkb_mods(state);
154         out->unicode = xkb_keysym_to_utf32(out->keysym) ? : UTERM_INPUT_INVALID;
155
156         return 0;
157 }
158
159 /*
160  * Call this when we regain control of the keyboard after losing it.
161  * We don't reset the locked group, this should survive a VT switch, etc. The
162  * locked modifiers are reset according to the keyboard LEDs.
163  */
164 void uxkb_dev_reset(struct uterm_input_dev *dev, const unsigned long *ledbits)
165 {
166         unsigned int i;
167         struct xkb_state *state;
168         static const struct {
169                 int led;
170                 const char *name;
171         } led_names[] = {
172                 { LED_NUML, XKB_LED_NAME_NUM },
173                 { LED_CAPSL, XKB_LED_NAME_CAPS },
174                 { LED_SCROLLL, XKB_LED_NAME_SCROLL },
175         };
176
177         /* TODO: Urghs, while the input device was closed we might have missed
178          * some events that affect internal state. As xkbcommon does not provide
179          * a way to reset the internal state, we simply recreate the state. This
180          * should have the same effect.
181          * It also has a bug that if the CTRL-Release event is skipped, then
182          * every further release will never perform a _real_ release. Kind of
183          * buggy so we should fix it upstream. */
184         state = xkb_state_new(dev->input->keymap);
185         if (!state) {
186                 log_warning("cannot recreate xkb-state");
187                 return;
188         }
189         xkb_state_unref(dev->state);
190         dev->state = state;
191
192         for (i = 0; i < sizeof(led_names) / sizeof(*led_names); i++) {
193                 if (!input_bit_is_set(ledbits, led_names[i].led))
194                         continue;
195
196                 /*
197                  * TODO: Add support in xkbcommon for setting the led state,
198                  * and updating the modifier state accordingly. E.g., something
199                  * like this:
200                  *      xkb_state_led_name_set_active(state, led_names[i].led);
201                  */
202         }
203 }