build: make xkbcommon built-time dependency
[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 #ifdef UTERM_HAVE_XKBCOMMON
75
76 struct uxkb_desc {
77         struct xkb_context *ctx;
78         struct xkb_keymap *keymap;
79 };
80
81 struct uxkb_dev {
82         struct xkb_state *state;
83 };
84
85 static const bool uxkb_available = true;
86 extern const struct kbd_desc_ops uxkb_desc_ops;
87 extern const struct kbd_dev_ops uxkb_dev_ops;
88
89 extern int uxkb_string_to_keysym(const char *n, uint32_t *out);
90
91 #else /* !UTERM_HAVE_XKBCOMMON */
92
93 struct uxkb_desc {
94         int unused;
95 };
96
97 struct uxkb_dev {
98         int unused;
99 };
100
101 static const bool uxkb_available = false;
102 static const struct kbd_desc_ops uxkb_desc_ops;
103 static const struct kbd_dev_ops uxkb_dev_ops;
104
105 #endif /* UTERM_HAVE_XKBCOMMON */
106
107 struct kbd_desc {
108         unsigned long ref;
109         const struct kbd_desc_ops *ops;
110
111         union {
112                 struct plain_desc plain;
113                 struct uxkb_desc uxkb;
114         };
115 };
116
117 struct kbd_dev {
118         unsigned long ref;
119         struct kbd_desc *desc;
120         const struct kbd_dev_ops *ops;
121
122         union {
123                 struct plain_dev plain;
124                 struct uxkb_dev uxkb;
125         };
126 };
127
128 enum kbd_mode {
129         KBD_PLAIN,
130         KBD_UXKB,
131 };
132
133 static inline int kbd_desc_new(struct kbd_desc **out, const char *layout,
134                                const char *variant, const char *options,
135                                unsigned int mode)
136 {
137         const struct kbd_desc_ops *ops;
138
139         switch (mode) {
140         case KBD_UXKB:
141                 if (!uxkb_available) {
142                         log_error("XKB KBD backend not available");
143                         return -EOPNOTSUPP;
144                 }
145                 ops = &uxkb_desc_ops;
146                 break;
147         case KBD_PLAIN:
148                 if (!plain_available) {
149                         log_error("plain KBD backend not available");
150                         return -EOPNOTSUPP;
151                 }
152                 ops = &plain_desc_ops;
153                 break;
154         default:
155                 log_error("unknown KBD backend %u", mode);
156                 return -EINVAL;
157         }
158
159         return ops->init(out, layout, variant, options);
160 }
161
162 static inline void kbd_desc_ref(struct kbd_desc *desc)
163 {
164         if (!desc)
165                 return;
166
167         return desc->ops->ref(desc);
168 }
169
170 static inline void kbd_desc_unref(struct kbd_desc *desc)
171 {
172         if (!desc)
173                 return;
174
175         return desc->ops->unref(desc);
176 }
177
178 static inline int kbd_desc_alloc(struct kbd_desc *desc, struct kbd_dev **out)
179 {
180         if (!desc)
181                 return -EINVAL;
182
183         return desc->ops->alloc(desc, out);
184 }
185
186 static inline void kbd_desc_keysym_to_string(struct kbd_desc *desc,
187                                              uint32_t keysym,
188                                              char *str, size_t size)
189 {
190         if (!desc)
191                 return;
192
193         return desc->ops->keysym_to_string(keysym, str, size);
194 }
195
196 static inline int kbd_desc_string_to_keysym(struct kbd_desc *desc,
197                                             const char *n,
198                                             uint32_t *out)
199 {
200         if (!desc)
201                 return -EINVAL;
202
203         return desc->ops->string_to_keysym(n, out);
204 }
205
206 static inline void kbd_dev_ref(struct kbd_dev *dev)
207 {
208         if (!dev)
209                 return;
210
211         return dev->ops->ref(dev);
212 }
213
214 static inline void kbd_dev_unref(struct kbd_dev *dev)
215 {
216         if (!dev)
217                 return;
218
219         return dev->ops->unref(dev);
220 }
221
222 static inline void kbd_dev_reset(struct kbd_dev *dev,
223                                  const unsigned long *ledbits)
224 {
225         if (!dev)
226                 return;
227
228         return dev->ops->reset(dev, ledbits);
229 }
230
231 static inline int kbd_dev_process(struct kbd_dev *dev,
232                                   uint16_t key_state,
233                                   uint16_t code,
234                                   struct uterm_input_event *out)
235 {
236         if (!dev)
237                 return -EINVAL;
238
239         return dev->ops->process(dev, key_state, code, out);
240 }
241
242 static inline bool input_bit_is_set(const unsigned long *array, int bit)
243 {
244         return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
245 }
246
247 #endif /* UTERM_INPUT_H */