Make xkbcommon mandatory
[platform/upstream/kmscon.git] / src / uterm_input.h
1 /*
2  * uterm - Linux User-Space Terminal
3  *
4  * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /* Internal definitions */
27
28 #ifndef UTERM_INPUT_H
29 #define UTERM_INPUT_H
30
31 #include <inttypes.h>
32 #include <limits.h>
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <xkbcommon/xkbcommon-keysyms.h>
36 #include "eloop.h"
37 #include "uterm.h"
38
39 struct kbd_desc;
40 struct kbd_dev;
41
42 struct kbd_desc_ops {
43         int (*init) (struct kbd_desc **out, const char *layout,
44                      const char *variant, const char *options);
45         void (*ref) (struct kbd_desc *desc);
46         void (*unref) (struct kbd_desc *desc);
47         int (*alloc) (struct kbd_desc *desc, struct kbd_dev **out);
48         void (*keysym_to_string) (uint32_t keysym, char *str, size_t size);
49         int (*string_to_keysym) (const char *n, uint32_t *out);
50 };
51
52 struct kbd_dev_ops {
53         void (*ref) (struct kbd_dev *dev);
54         void (*unref) (struct kbd_dev *dev);
55         void (*reset) (struct kbd_dev *dev, const unsigned long *ledbits);
56         int (*process) (struct kbd_dev *dev, uint16_t state, uint16_t code,
57                         struct uterm_input_event *out);
58 };
59
60 struct plain_desc {
61         int unused;
62 };
63
64 struct plain_dev {
65         unsigned int mods;
66 };
67
68 static const bool plain_available = true;
69 extern const struct kbd_desc_ops plain_desc_ops;
70 extern const struct kbd_dev_ops plain_dev_ops;
71
72 extern int plain_string_to_keysym(const char *n, uint32_t *out);
73
74 struct uxkb_desc {
75         struct xkb_context *ctx;
76         struct xkb_keymap *keymap;
77 };
78
79 struct uxkb_dev {
80         struct xkb_state *state;
81 };
82
83 static const bool uxkb_available = true;
84 extern const struct kbd_desc_ops uxkb_desc_ops;
85 extern const struct kbd_dev_ops uxkb_dev_ops;
86
87 extern int uxkb_string_to_keysym(const char *n, uint32_t *out);
88
89 struct kbd_desc {
90         unsigned long ref;
91         const struct kbd_desc_ops *ops;
92
93         union {
94                 struct plain_desc plain;
95                 struct uxkb_desc uxkb;
96         };
97 };
98
99 struct kbd_dev {
100         unsigned long ref;
101         struct kbd_desc *desc;
102         const struct kbd_dev_ops *ops;
103
104         union {
105                 struct plain_dev plain;
106                 struct uxkb_dev uxkb;
107         };
108 };
109
110 enum kbd_mode {
111         KBD_PLAIN,
112         KBD_UXKB,
113 };
114
115 static inline int kbd_desc_new(struct kbd_desc **out, const char *layout,
116                                const char *variant, const char *options,
117                                unsigned int mode)
118 {
119         const struct kbd_desc_ops *ops;
120
121         switch (mode) {
122         case KBD_UXKB:
123                 if (!uxkb_available) {
124                         log_error("XKB KBD backend not available");
125                         return -EOPNOTSUPP;
126                 }
127                 ops = &uxkb_desc_ops;
128                 break;
129         case KBD_PLAIN:
130                 if (!plain_available) {
131                         log_error("plain KBD backend not available");
132                         return -EOPNOTSUPP;
133                 }
134                 ops = &plain_desc_ops;
135                 break;
136         default:
137                 log_error("unknown KBD backend %u", mode);
138                 return -EINVAL;
139         }
140
141         return ops->init(out, layout, variant, options);
142 }
143
144 static inline void kbd_desc_ref(struct kbd_desc *desc)
145 {
146         if (!desc)
147                 return;
148
149         return desc->ops->ref(desc);
150 }
151
152 static inline void kbd_desc_unref(struct kbd_desc *desc)
153 {
154         if (!desc)
155                 return;
156
157         return desc->ops->unref(desc);
158 }
159
160 static inline int kbd_desc_alloc(struct kbd_desc *desc, struct kbd_dev **out)
161 {
162         if (!desc)
163                 return -EINVAL;
164
165         return desc->ops->alloc(desc, out);
166 }
167
168 static inline void kbd_desc_keysym_to_string(struct kbd_desc *desc,
169                                              uint32_t keysym,
170                                              char *str, size_t size)
171 {
172         if (!desc)
173                 return;
174
175         return desc->ops->keysym_to_string(keysym, str, size);
176 }
177
178 static inline int kbd_desc_string_to_keysym(struct kbd_desc *desc,
179                                             const char *n,
180                                             uint32_t *out)
181 {
182         if (!desc)
183                 return -EINVAL;
184
185         return desc->ops->string_to_keysym(n, out);
186 }
187
188 static inline void kbd_dev_ref(struct kbd_dev *dev)
189 {
190         if (!dev)
191                 return;
192
193         return dev->ops->ref(dev);
194 }
195
196 static inline void kbd_dev_unref(struct kbd_dev *dev)
197 {
198         if (!dev)
199                 return;
200
201         return dev->ops->unref(dev);
202 }
203
204 static inline void kbd_dev_reset(struct kbd_dev *dev,
205                                  const unsigned long *ledbits)
206 {
207         if (!dev)
208                 return;
209
210         return dev->ops->reset(dev, ledbits);
211 }
212
213 static inline int kbd_dev_process(struct kbd_dev *dev,
214                                   uint16_t key_state,
215                                   uint16_t code,
216                                   struct uterm_input_event *out)
217 {
218         if (!dev)
219                 return -EINVAL;
220
221         return dev->ops->process(dev, key_state, code, out);
222 }
223
224 static inline bool input_bit_is_set(const unsigned long *array, int bit)
225 {
226         return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
227 }
228
229 #endif /* UTERM_INPUT_H */