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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*
* Copyright © 2012 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#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-unstable-v1-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 zwp_input_method_v1 *input_method;
struct zwp_input_method_context_v1 *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;
uint32_t serial;
};
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
handle_surrounding_text(void *data,
struct zwp_input_method_context_v1 *context,
const char *text,
uint32_t cursor,
uint32_t anchor)
{
fprintf(stderr, "Surrounding text updated: %s\n", text);
}
static void
handle_reset(void *data,
struct zwp_input_method_context_v1 *context)
{
struct simple_im *keyboard = data;
fprintf(stderr, "Reset pre-edit buffer\n");
keyboard->compose_state = state_normal;
}
static void
handle_content_type(void *data,
struct zwp_input_method_context_v1 *context,
uint32_t hint,
uint32_t purpose)
{
}
static void
handle_invoke_action(void *data,
struct zwp_input_method_context_v1 *context,
uint32_t button,
uint32_t index)
{
}
static void
handle_commit_state(void *data,
struct zwp_input_method_context_v1 *context,
uint32_t serial)
{
struct simple_im *keyboard = data;
keyboard->serial = serial;
}
static void
handle_preferred_language(void *data,
struct zwp_input_method_context_v1 *context,
const char *language)
{
}
static const struct zwp_input_method_context_v1_listener input_method_context_listener = {
handle_surrounding_text,
handle_reset,
handle_content_type,
handle_invoke_action,
handle_commit_state,
handle_preferred_language
};
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_keymap_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_keymap_unref(keyboard->keymap);
return;
}
keyboard->control_mask =
1 << xkb_keymap_mod_get_index(keyboard->keymap, "Control");
keyboard->alt_mask =
1 << xkb_keymap_mod_get_index(keyboard->keymap, "Mod1");
keyboard->shift_mask =
1 << xkb_keymap_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_state_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 zwp_input_method_context_v1 *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_MODS_DEPRESSED |
XKB_STATE_MODS_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;
zwp_input_method_context_v1_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 zwp_input_method_v1 *input_method,
struct zwp_input_method_context_v1 *context)
{
struct simple_im *keyboard = data;
if (keyboard->context)
zwp_input_method_context_v1_destroy(keyboard->context);
keyboard->compose_state = state_normal;
keyboard->serial = 0;
keyboard->context = context;
zwp_input_method_context_v1_add_listener(context,
&input_method_context_listener,
keyboard);
keyboard->keyboard = zwp_input_method_context_v1_grab_keyboard(context);
wl_keyboard_add_listener(keyboard->keyboard,
&input_method_keyboard_listener,
keyboard);
}
static void
input_method_deactivate(void *data,
struct zwp_input_method_v1 *input_method,
struct zwp_input_method_context_v1 *context)
{
struct simple_im *keyboard = data;
if (!keyboard->context)
return;
zwp_input_method_context_v1_destroy(keyboard->context);
keyboard->context = NULL;
}
static const struct zwp_input_method_v1_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, "zwp_input_method_v1")) {
keyboard->input_method =
wl_registry_bind(registry, name,
&zwp_input_method_v1_interface, 1);
zwp_input_method_v1_add_listener(keyboard->input_method,
&input_method_listener, keyboard);
}
}
static void
registry_handle_global_remove(void *data, struct wl_registry *registry,
uint32_t name)
{
}
static const struct wl_registry_listener registry_listener = {
registry_handle_global,
registry_handle_global_remove
};
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 zwp_input_method_context_v1 *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]) {
zwp_input_method_context_v1_key(context,
keyboard->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) {
zwp_input_method_context_v1_preedit_cursor(keyboard->context,
0);
zwp_input_method_context_v1_preedit_string(keyboard->context,
keyboard->serial,
"", "");
zwp_input_method_context_v1_cursor_position(keyboard->context,
0, 0);
zwp_input_method_context_v1_commit_string(keyboard->context,
keyboard->serial,
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);
}
zwp_input_method_context_v1_preedit_cursor(keyboard->context,
strlen(text));
zwp_input_method_context_v1_preedit_string(keyboard->context,
keyboard->serial,
text,
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);
}
zwp_input_method_context_v1_preedit_cursor(keyboard->context,
0);
zwp_input_method_context_v1_preedit_string(keyboard->context,
keyboard->serial,
"", "");
zwp_input_method_context_v1_cursor_position(keyboard->context,
0, 0);
zwp_input_method_context_v1_commit_string(keyboard->context,
keyboard->serial,
text);
keyboard->compose_state = state_normal;
}
return;
}
if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) {
zwp_input_method_context_v1_key(context, serial, time, key, state);
return;
}
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
return;
zwp_input_method_context_v1_cursor_position(keyboard->context,
0, 0);
zwp_input_method_context_v1_commit_string(keyboard->context,
keyboard->serial,
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;
}
|