File: x_keyboard_synthesis.c

package info (click to toggle)
gxr 0.15.1-2~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 1,728 kB
  • sloc: ansic: 15,256; sh: 72; xml: 71; awk: 35; makefile: 12
file content (370 lines) | stat: -rw-r--r-- 11,292 bytes parent folder | download | duplicates (3)
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
/*
 * gxr
 * Copyright 2018 Collabora Ltd.
 * Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * Author: Christoph Haag <christoph.haag@collabora.com>
 * SPDX-License-Identifier: MIT
 *
 * This example shows the openvr keyboard and sends its input via XTest to the
 * currently focused window.
 */

#include <glib.h>
#include <glib/gprintf.h>
#include <gdk/gdk.h>

#include "gxr.h"

#include <graphene.h>

#define XK_XKB_KEYS
#define XK_MISCELLANY
#define XK_LATIN1
#include <X11/keysymdef.h>

#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

static Display *dpy;
static graphene_matrix_t keyboard_position;

typedef struct KeySymTable {
  KeySym *table;
  int min_keycode;
  //int max_keycode;
  int keysyms_per_keycode;
  int num_elements;

  XModifierKeymap *modmap;
} KeySymTable;

typedef struct ModifierKeyCodes {
  // there are only up to 8 modifier keys
  KeyCode key_codes[8];
  int length;
} ModifierKeyCodes;

static gboolean
timeout_callback (gpointer data)
{
  gxr_context_poll_event (GXR_CONTEXT (data));
  return TRUE;
}

static void
_keyboard_closed (GxrContext  *context,
                  GdkEventKey *event,
                  gpointer     data)
{
  (void) event;
  (void) data;
  g_print ("Closed keyboard... Let's open it again!\n");
  gxr_context_show_keyboard (context);
  gxr_context_set_keyboard_transform (context, &keyboard_position);
}

ModifierKeyCodes
_modifier_keycode_for (KeySym sym, KeySymTable *keysym_table);

static void
_keyboard_input (GxrContext  *context,
                 GdkEventKey *event,
                 KeySymTable *keysym_table)
{
  (void) context;
  for (int char_index = 0; char_index < event->length; char_index++)
    {
      char c = event->string[char_index];
      //g_print ("Got char %d\n", c);

      // TODO: many openvr key codes match Latin 1 keysyms from X11/keysymdef.h
      // lucky coincidence or subject to change?
      KeySym key_sym;
      // OpenVR Backspace = 8 and XK_Backspace = 0xff08 etc.
      if (c >= 8 && c <= 17)
        key_sym = (KeySym) (0xff00 + c);
      else
        key_sym = (KeySym) c;

      // character 10 on the open vr keyboard (Line Feed)
      // should be return for us. There is no return on the openvr keyboard?!
      if (c == 10)
          key_sym = XK_Return;

      ModifierKeyCodes mod_key_codes =
        _modifier_keycode_for (key_sym, keysym_table);

      if (mod_key_codes.length == -1)
        {
          g_print ("There was an error, not synthing %c keysym %lu %s\n",
                   event->string[char_index], key_sym,
                   XKeysymToString (key_sym));
          return;
        }

      for (int i = 0; i < mod_key_codes.length; i++)
        {
          KeySym key_code = mod_key_codes.key_codes[i];
          //g_print ("%c: modkey %lu\n", c, key_code);
          XTestFakeKeyEvent (dpy, (guint) key_code, TRUE, 0);
        }

      unsigned int key_code = XKeysymToKeycode (dpy, key_sym);
      //g_print ("%c (%d): keycode %d (keysym %d)\n", c, c, key_code, key_sym);
      XTestFakeKeyEvent (dpy, key_code, TRUE, 0);
      XTestFakeKeyEvent (dpy, key_code, FALSE, 0);

      for (int i = 0; i < mod_key_codes.length; i++)
        {
          KeySym key_code_to_send = mod_key_codes.key_codes[i];
          //g_print ("%c: modkey %lu\n", c, key_code);
          XTestFakeKeyEvent (dpy, (guint)key_code_to_send, FALSE, 0);
        }

      XSync (dpy, FALSE);
    }
}

static KeySymTable *
_init_keysym_table ()
{
  int min_keycode;
  int max_keycode;
  int keysyms_per_keycode;
  XDisplayKeycodes (dpy, &min_keycode, &max_keycode);


  int keycode_count = max_keycode - min_keycode + 1;
  KeySym *keysyms = XGetKeyboardMapping (dpy,
                                         (KeyCode) min_keycode,
                                         (KeyCode) keycode_count,
                                         &keysyms_per_keycode);

  int num_elements = keycode_count * keysyms_per_keycode;
/*
  g_print ("Keycodes: min %d, max %d\n", min_keycode, max_keycode);
  g_print ("Key Syms per key code: %d\n", keysyms_per_keycode);
  g_print ("Key code: syms\n");
  for (int keycode = min_keycode; keycode < num_elements / keysyms_per_keycode;
       keycode++)
    {
      g_print ("%3d ", keycode);
      for (int keysym_num = 0; keysym_num < keysyms_per_keycode; keysym_num++)
        {
          int index = (keycode - min_keycode) * keysyms_per_keycode +
            keysym_num;

          KeySym sym = keysyms[index];
          g_print ("| %-22s", sym == NoSymbol ? "" : XKeysymToString(sym));
        }
      g_print ("\n");
    }
*/

  XModifierKeymap *modmap = XGetModifierMapping(dpy);
/*
  g_print ("Modifiers (max %d keys per mod):\n", modmap->max_keypermod);
  for (int modifier = 0; modifier < 8; modifier++)
    {
      for (int mod_key = 0; mod_key < modmap->max_keypermod; mod_key++)
        {
          KeyCode keycode = modmap->modifiermap[modifier * modmap->max_keypermod + mod_key];
          int syms;
          if (keycode == 0) continue;
          KeySym *sym = XGetKeyboardMapping (dpy, keycode, 1, &syms);
          for (int i = 0; i < syms; i++)
            {
              if (sym[i] == NoSymbol) continue;
              g_print ("| %3d %-22s", keycode, XKeysymToString(sym[i]));
            }
        }
      g_print ("\n");
    }
*/
  KeySymTable *keysym_table = malloc (sizeof (KeySymTable));
  keysym_table->table = keysyms;
  keysym_table->min_keycode = min_keycode;
  //keysym_table->max_keycode = max_keycode;
  keysym_table->keysyms_per_keycode = keysyms_per_keycode;
  keysym_table->num_elements = num_elements;
  keysym_table->modmap = modmap;

  return keysym_table;
}

static int
_find_modifier_num (KeySymTable *keysym_table, KeySym sym)
{
  int min_keycode = keysym_table->min_keycode;
  int num_elements = keysym_table->num_elements;
  int keysyms_per_keycode = keysym_table->keysyms_per_keycode;
  KeySym *keysyms = keysym_table->table;
  for (int keycode = min_keycode; keycode < num_elements / keysyms_per_keycode;
     keycode++)
    {
      for (int keysym_num = 0; keysym_num < keysyms_per_keycode; keysym_num++)
        {

          // TODO: 2 (ctr+alt) or 3 (ctrl+alt+shift) are weird
          // XK_apostrophe (') on the german keyboard is Shift+#, but also the
          // weird Ctrl+Alt+รค combinatoin, which comes first, but only works in
          // some (?) apps, so skip the weird ones.
          if (keysym_num == 2 || keysym_num == 3) continue;

          int index = (keycode - min_keycode) * keysyms_per_keycode +
            keysym_num;
          if (keysyms[index] == sym)
            {
              int modifier_num = keysym_num;
              return modifier_num;
            }
        }
    }
  return -1;
}

ModifierKeyCodes
_modifier_keycode_for (KeySym sym, KeySymTable *keysym_table)
{
  /* each row of the key sym table is one key code
   * in each row there are up to "keysyms_per_keycode" key syms that are located
   * on the same physical key in the current layout
   *
   * for example in the german layout the row for the q key looks like this
   * key code 24; key syms: q, Q, q, Q, at, Greek_OMEGA, at
   * (note that XKeysymToString() omits the XK_ prefix that can be found in the
   * #define in keysymdef.h, e.g. the "at" key sym is defined as XK_at)
   *
   * _find_modifier_num() finds the index of the first applicable sym:
   * XK_q => 0
   * XK_Q => 1
   * XK_at => 4
   */
  int modifier_num = _find_modifier_num (keysym_table, sym);

  if (modifier_num == -1) {
    g_print ("ERROR: Did not find key sym!\n");
    return (ModifierKeyCodes) { .length = -1 };
  } else {
    //g_print ("Found key sym with mod number %d!\n", modifier_num);
  }

  /* There are up to 8 modifiers that can be enumerated (shift, ctrl, etc.)
   *
   * each of these 8 keys has up to "max_keypermod" "equivalent" modifiers
   * (e.g. left shift, right shift, etc.)
   *
   * TODO:
   * from XK_at appearing in column 4 of the table above we know that XK_at
   * can be achieved by q + modifier "4" and
   * I know from my keyboard that modifier 4 is the alt gr key (which has keysym
   * XK_ISO_Level3_Shift) but I do not know how to programatically find wich
   * entry of the XModifierKeymap entries below corresponds to e.g. column 4
   *
   * If modmap->modifiermap[modmap_index(4)] was XK_ISO_Level3_Shift we would
   * have solved the problem

  XModifierKeymap *modmap = keysym_table->modmap;
  // + 0: There are modmap->max_keypermod "equivalent" modifier keys.
  int modmap_index = modifier_num * modmap->max_keypermod + 0;
  KeyCode key_code = modmap->modifiermap[modmap_index];
  return key_code;
  */

  // for now hardcode the modifier key
  switch (modifier_num) {
  case 0:
    return (ModifierKeyCodes) {
      .key_codes[0] = XKeysymToKeycode (dpy, NoSymbol),
      .length = 1
    };
  case 1:
    return (ModifierKeyCodes) {
      .key_codes[0] = XKeysymToKeycode (dpy, XK_Shift_L),
      .length = 1
    };
  // TODO: are 2 and 3 correct? They are super weird!
  case 2:
    return (ModifierKeyCodes) {
      .key_codes[0] = XKeysymToKeycode (dpy, XK_Control_L),
      .key_codes[1] = XKeysymToKeycode (dpy, XK_Alt_L),
      .length = 2
    };
  case 3:
    return (ModifierKeyCodes) {
      .key_codes[0] = XKeysymToKeycode (dpy, XK_Shift_L),
      .key_codes[1] = XKeysymToKeycode (dpy, XK_Control_L),
      .key_codes[2] = XKeysymToKeycode (dpy, XK_Alt_L),
      .length = 3
    };
  case 4:
    return (ModifierKeyCodes) {
      .key_codes[0] = XKeysymToKeycode (dpy, XK_ISO_Level3_Shift),
      .length = 1
    };
  case 5:
    return (ModifierKeyCodes) {
      .key_codes[0] = XKeysymToKeycode (dpy, XK_Shift_L),
      .key_codes[1] = XKeysymToKeycode (dpy, XK_ISO_Level3_Shift),
      .length = 2
    };
  default:
    return (ModifierKeyCodes) { .length = 0 };
  }
}

int
main (int argc, char *argv[])
{
  (void) argc;
  (void) argv;
  GMainLoop *loop = g_main_loop_new (NULL, FALSE);

  dpy = XOpenDisplay (NULL);

  KeySymTable *keysym_table = _init_keysym_table ();
/*
  KeySym test_key = XK_at;
  ModifierKeyCodes modifier_key_code =
    _modifier_keycode_for (test_key, keysym_table);

  g_print ("Got %d modifier key codes: ", modifier_key_code.length);
  for (int i = 0; i < modifier_key_code.length; i++)
    {
      int syms;
      KeySym *sym =
        XGetKeyboardMapping (dpy, modifier_key_code.key_codes[i], 1, &syms);
      for (int i = 0; i < syms; i++)
        {
          if (sym[i] == NoSymbol) continue;
            g_print ("| %-22s", XKeysymToString(sym[i]));
        }
      g_print ("\n");
    }
*/

  GxrContext *context =
    gxr_context_new (GXR_APP_OVERLAY, "X11 Keyboard Synth", 1);

  gxr_context_show_keyboard (GXR_CONTEXT (context));

  graphene_point3d_t position = {
    .x = 0,
    .y = 1,
    .z = -1.5
  };
  graphene_matrix_init_translate (&keyboard_position, &position);
  gxr_context_set_keyboard_transform (context, &keyboard_position);

  g_signal_connect (context, "keyboard-press-event",
                    (GCallback) _keyboard_input, keysym_table);
  g_signal_connect (context, "keyboard-close-event",
                    (GCallback) _keyboard_closed, context);
  g_timeout_add (20, timeout_callback, context);

  g_main_loop_run (loop);
  g_main_loop_unref (loop);
  g_object_unref (context);

  return 0;
}