1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/*
* Copyright © 2012 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The copyright holders make
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/input.h>
#include "window.h"
#include "input-method-client-protocol.h"
enum compose_state {
state_normal,
state_compose
};
struct compose_seq {
uint32_t keys[4];
const char *text;
};
struct simple_im;
typedef void (*keyboard_input_key_handler_t)(struct simple_im *keyboard,
uint32_t serial,
uint32_t time, uint32_t key, uint32_t unicode,
enum wl_keyboard_key_state state);
struct simple_im {
struct input_method *input_method;
struct input_method_context *context;
struct wl_display *display;
struct wl_registry *registry;
struct wl_keyboard *keyboard;
enum compose_state compose_state;
struct compose_seq compose_seq;
struct xkb_context *xkb_context;
uint32_t modifiers;
struct xkb_keymap *keymap;
struct xkb_state *state;
xkb_mod_mask_t control_mask;
xkb_mod_mask_t alt_mask;
xkb_mod_mask_t shift_mask;
keyboard_input_key_handler_t key_handler;
};
static const struct compose_seq compose_seqs[] = {
{ { XKB_KEY_quotedbl, XKB_KEY_A, 0 }, "Ä" },
{ { XKB_KEY_quotedbl, XKB_KEY_O, 0 }, "Ö" },
{ { XKB_KEY_quotedbl, XKB_KEY_U, 0 }, "Ü" },
{ { XKB_KEY_quotedbl, XKB_KEY_a, 0 }, "ä" },
{ { XKB_KEY_quotedbl, XKB_KEY_o, 0 }, "ö" },
{ { XKB_KEY_quotedbl, XKB_KEY_u, 0 }, "ü" },
{ { XKB_KEY_apostrophe, XKB_KEY_A, 0 }, "Á" },
{ { XKB_KEY_apostrophe, XKB_KEY_a, 0 }, "á" },
{ { XKB_KEY_slash, XKB_KEY_O, 0 }, "Ø" },
{ { XKB_KEY_slash, XKB_KEY_o, 0 }, "ø" },
{ { XKB_KEY_less, XKB_KEY_3, 0 }, "♥" },
{ { XKB_KEY_A, XKB_KEY_A, 0 }, "Å" },
{ { XKB_KEY_A, XKB_KEY_E, 0 }, "Æ" },
{ { XKB_KEY_O, XKB_KEY_C, 0 }, "©" },
{ { XKB_KEY_O, XKB_KEY_R, 0 }, "®" },
{ { XKB_KEY_s, XKB_KEY_s, 0 }, "ß" },
{ { XKB_KEY_a, XKB_KEY_e, 0 }, "æ" },
{ { XKB_KEY_a, XKB_KEY_a, 0 }, "å" },
};
static const uint32_t ignore_keys_on_compose[] = {
XKB_KEY_Shift_L,
XKB_KEY_Shift_R
};
static void
input_method_context_surrounding_text(void *data,
struct input_method_context *context,
const char *text,
uint32_t cursor,
uint32_t anchor)
{
fprintf(stderr, "Surrounding text updated: %s\n", text);
}
static void
input_method_context_reset(void *data,
struct input_method_context *context)
{
struct simple_im *keyboard = data;
fprintf(stderr, "Reset pre-edit buffer\n");
keyboard->compose_state = state_normal;
}
static const struct input_method_context_listener input_method_context_listener = {
input_method_context_surrounding_text,
input_method_context_reset
};
static void
input_method_keyboard_keymap(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t format,
int32_t fd,
uint32_t size)
{
struct simple_im *keyboard = data;
char *map_str;
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map_str == MAP_FAILED) {
close(fd);
return;
}
keyboard->keymap =
xkb_map_new_from_string(keyboard->xkb_context,
map_str,
XKB_KEYMAP_FORMAT_TEXT_V1,
0);
munmap(map_str, size);
close(fd);
if (!keyboard->keymap) {
fprintf(stderr, "failed to compile keymap\n");
return;
}
keyboard->state = xkb_state_new(keyboard->keymap);
if (!keyboard->state) {
fprintf(stderr, "failed to create XKB state\n");
xkb_map_unref(keyboard->keymap);
return;
}
keyboard->control_mask =
1 << xkb_map_mod_get_index(keyboard->keymap, "Control");
keyboard->alt_mask =
1 << xkb_map_mod_get_index(keyboard->keymap, "Mod1");
keyboard->shift_mask =
1 << xkb_map_mod_get_index(keyboard->keymap, "Shift");
}
static void
input_method_keyboard_key(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state_w)
{
struct simple_im *keyboard = data;
uint32_t code;
uint32_t num_syms;
const xkb_keysym_t *syms;
xkb_keysym_t sym;
enum wl_keyboard_key_state state = state_w;
if (!keyboard->state)
return;
code = key + 8;
num_syms = xkb_key_get_syms(keyboard->state, code, &syms);
sym = XKB_KEY_NoSymbol;
if (num_syms == 1)
sym = syms[0];
if (keyboard->key_handler)
(*keyboard->key_handler)(keyboard, serial, time, key, sym,
state);
}
static void
input_method_keyboard_modifiers(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group)
{
struct simple_im *keyboard = data;
struct input_method_context *context = keyboard->context;
xkb_mod_mask_t mask;
xkb_state_update_mask(keyboard->state, mods_depressed,
mods_latched, mods_locked, 0, 0, group);
mask = xkb_state_serialize_mods(keyboard->state,
XKB_STATE_DEPRESSED |
XKB_STATE_LATCHED);
keyboard->modifiers = 0;
if (mask & keyboard->control_mask)
keyboard->modifiers |= MOD_CONTROL_MASK;
if (mask & keyboard->alt_mask)
keyboard->modifiers |= MOD_ALT_MASK;
if (mask & keyboard->shift_mask)
keyboard->modifiers |= MOD_SHIFT_MASK;
input_method_context_modifiers(context, serial,
mods_depressed, mods_depressed,
mods_latched, group);
}
static const struct wl_keyboard_listener input_method_keyboard_listener = {
input_method_keyboard_keymap,
NULL, /* enter */
NULL, /* leave */
input_method_keyboard_key,
input_method_keyboard_modifiers
};
static void
input_method_activate(void *data,
struct input_method *input_method,
struct input_method_context *context)
{
struct simple_im *keyboard = data;
if (keyboard->context)
input_method_context_destroy(keyboard->context);
keyboard->compose_state = state_normal;
keyboard->context = context;
input_method_context_add_listener(context,
&input_method_context_listener,
keyboard);
keyboard->keyboard = input_method_context_grab_keyboard(context);
wl_keyboard_add_listener(keyboard->keyboard,
&input_method_keyboard_listener,
keyboard);
}
static void
input_method_deactivate(void *data,
struct input_method *input_method,
struct input_method_context *context)
{
struct simple_im *keyboard = data;
if (!keyboard->context)
return;
input_method_context_destroy(keyboard->context);
keyboard->context = NULL;
}
static const struct input_method_listener input_method_listener = {
input_method_activate,
input_method_deactivate
};
static void
registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version)
{
struct simple_im *keyboard = data;
if (!strcmp(interface, "input_method")) {
keyboard->input_method =
wl_registry_bind(registry, name,
&input_method_interface, 1);
input_method_add_listener(keyboard->input_method,
&input_method_listener, keyboard);
}
}
static const struct wl_registry_listener registry_listener = {
registry_handle_global
};
static int
compare_compose_keys(const void *c1, const void *c2)
{
const struct compose_seq *cs1 = c1;
const struct compose_seq *cs2 = c2;
int i;
for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) {
if (cs1->keys[i] != cs2->keys[i])
return cs1->keys[i] - cs2->keys[i];
}
if (cs1->keys[i] == cs2->keys[i]
|| cs1->keys[i] == 0)
return 0;
return cs1->keys[i] - cs2->keys[i];
}
static void
simple_im_key_handler(struct simple_im *keyboard,
uint32_t serial, uint32_t time, uint32_t key, uint32_t sym,
enum wl_keyboard_key_state state)
{
struct input_method_context *context = keyboard->context;
char text[64];
if (sym == XKB_KEY_Multi_key &&
state == WL_KEYBOARD_KEY_STATE_RELEASED &&
keyboard->compose_state == state_normal) {
keyboard->compose_state = state_compose;
memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq));
return;
}
if (keyboard->compose_state == state_compose) {
uint32_t i = 0;
struct compose_seq *cs;
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
return;
for (i = 0; i < sizeof(ignore_keys_on_compose) / sizeof(ignore_keys_on_compose[0]); i++) {
if (sym == ignore_keys_on_compose[i]) {
input_method_context_key(context, serial, time, key, state);
return;
}
}
for (i = 0; keyboard->compose_seq.keys[i] != 0; i++);
keyboard->compose_seq.keys[i] = sym;
cs = bsearch (&keyboard->compose_seq, compose_seqs,
sizeof(compose_seqs) / sizeof(compose_seqs[0]),
sizeof(compose_seqs[0]), compare_compose_keys);
if (cs) {
if (cs->keys[i + 1] == 0) {
input_method_context_preedit_string(keyboard->context,
"", 0);
input_method_context_commit_string(keyboard->context,
cs->text,
strlen(cs->text));
keyboard->compose_state = state_normal;
} else {
uint32_t j = 0, idx = 0;
for (; j <= i; j++) {
idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx);
}
input_method_context_preedit_string(keyboard->context,
text,
strlen(text));
}
} else {
uint32_t j = 0, idx = 0;
for (; j <= i; j++) {
idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx);
}
input_method_context_preedit_string(keyboard->context,
"", 0);
input_method_context_commit_string(keyboard->context,
text,
strlen(text));
keyboard->compose_state = state_normal;
}
return;
}
if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) {
input_method_context_key(context, serial, time, key, state);
return;
}
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
return;
input_method_context_commit_string(keyboard->context,
text,
strlen(text));
}
int
main(int argc, char *argv[])
{
struct simple_im simple_im;
int ret = 0;
memset(&simple_im, 0, sizeof(simple_im));
simple_im.display = wl_display_connect(NULL);
if (simple_im.display == NULL) {
fprintf(stderr, "failed to connect to server: %m\n");
return -1;
}
simple_im.registry = wl_display_get_registry(simple_im.display);
wl_registry_add_listener(simple_im.registry,
®istry_listener, &simple_im);
wl_display_roundtrip(simple_im.display);
if (simple_im.input_method == NULL) {
fprintf(stderr, "No input_method global\n");
exit(1);
}
simple_im.xkb_context = xkb_context_new(0);
if (simple_im.xkb_context == NULL) {
fprintf(stderr, "Failed to create XKB context\n");
return -1;
}
simple_im.context = NULL;
simple_im.key_handler = simple_im_key_handler;
while (ret != -1)
ret = wl_display_dispatch(simple_im.display);
if (ret == -1) {
fprintf(stderr, "Dispatch error: %m\n");
exit(1);
}
return 0;
}
|