aboutsummaryrefslogtreecommitdiff
path: root/console-client/xkb/xkb.c
blob: 8759c8bf110a75fe851f03ff33e077a143c68abb (plain)
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
/*  xkb.c -- Main XKB routines.

    Copyright (C) 2002, 2003, 2004  Marco Gerards
   
    Written by Marco Gerards <metgerards@student.han.nl>
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.  

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
  
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <locale.h>
#include <error.h>
#include <device/device.h>
#include <mach/mach_port.h>

#include "xkb.h"
#include <hurd/console.h>
#include <driver.h>
#include <mach-inputdev.h>
#include <wctype.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <assert.h>


/* The xkbcommon kdb context */
struct xkb_context *ctx;

/* The loaded keymap (immutable) */
struct xkb_keymap *keymap;

/* The keyboard state */
struct xkb_state *state;

/* The compose context and table */
struct xkb_compose_table *compose_table;
struct xkb_compose_state *compose_state;


int
debug_printf (const char *f, ...)
{
  va_list ap;
  int ret = 0;

  va_start (ap, f);
#ifdef XKB_DEBUG
  ret = vfprintf (stderr, f, ap);
#endif
  va_end (ap);

  return ret;  
}

static int
symtoctrlsym (xkb_keysym_t c)
{
  c = xkb_keysym_to_upper(c);

  switch (c)
    {
    case 'A' ... 'Z':
      c = c - 'A' + 1;
      break;
    case '[': case '3':
      c = '\e';
      break;
    case '\\': case '4':
      c = '';
      break;
    case ']': case '5':
      c = '';
      break;
    case '^': case '6':
      c = '';
      break;
    case '/':
      c = '/';
      break;
    case ' ':
      c = '\0';
      break;
    case '_': case '7':
      c= '';
      break;
    case '8':
      c = '\x7f';
      break;
    }
  
  return (int) c;
}

int
execute_action(keycode_t keycode)
{

  xkb_keysym_t keysym = xkb_state_key_get_one_sym (state, keycode);
  /* if CTRL+ALT+Delete is pressed notify the caller */
  if (keysym == XKB_KEY_Delete &&
      xkb_state_mod_names_are_active (state, XKB_STATE_MODS_EFFECTIVE, XKB_STATE_MATCH_ALL, XKB_MOD_NAME_CTRL,
                                      XKB_MOD_NAME_ALT, NULL) > 0)
    {
      console_exit ();
      return 1;
    }

  if (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE) > 0)
    {
      if (keysym >= XKB_KEY_F1 && keysym <= XKB_KEY_F35)
        {
          console_switch (keysym - XKB_KEY_F1 + 1, 0);
          return 1;
        }
      if (keysym == XKB_KEY_Right)
        {
          console_switch (0, 1);
          return 1;
        }
      if (keysym == XKB_KEY_Left)
        {
          console_switch (0, -1);
          return 1;
        }
      if (keysym == XKB_KEY_Up)
        {
          console_scrollback (CONS_SCROLL_DELTA_LINES, 1);
          return 1;
        }
      if (keysym == XKB_KEY_Down)
        {
          console_scrollback (CONS_SCROLL_DELTA_LINES, -1);
          return 1;
        }
      if (keysym == XKB_KEY_Page_Up)
        {
          console_scrollback (CONS_SCROLL_DELTA_SCREENS, -0.5);
          return 1;
        }
      if (keysym == XKB_KEY_Page_Up)
        {
          console_scrollback (CONS_SCROLL_DELTA_SCREENS, 0.5);
          return 1;

        }
      if (keysym == XKB_KEY_Home)
        {
          console_scrollback (CONS_SCROLL_ABSOLUTE_PERCENTAGE, 100 - 0);
          return 1;
        }
      if (keysym == XKB_KEY_End)
        {
          console_scrollback (CONS_SCROLL_ABSOLUTE_PERCENTAGE, 100 - 100);
          return 1;
        }
    }

    if (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_EFFECTIVE) > 0)
    {
      if (keysym == XKB_KEY_Home)
        {
          console_scrollback (CONS_SCROLL_ABSOLUTE_PERCENTAGE, 100 - 25);
          return 1;
        }
      if (keysym == XKB_KEY_End)
        {
          console_scrollback (CONS_SCROLL_ABSOLUTE_PERCENTAGE, 100 - 75);
          return 1;
        }
    }
  return 0;
}

char*
get_special_char_interpretation(xkb_keysym_t input)
{
  /* Special key, generate escape sequence.  */
  char *escseq = NULL;

  switch (input)
    {
    case XKB_KEY_Up: case XKB_KEY_KP_Up:
      escseq = CONS_KEY_UP;
      break;
    case XKB_KEY_Down: case XKB_KEY_KP_Down:
      escseq = CONS_KEY_DOWN;
      break;
    case XKB_KEY_Left: case XKB_KEY_KP_Left:
      escseq = CONS_KEY_LEFT;
      break;
    case XKB_KEY_Right: case XKB_KEY_KP_Right:
      escseq = CONS_KEY_RIGHT;
      break;
    case XKB_KEY_BackSpace:
      escseq = CONS_KEY_BACKSPACE;
      break;
    case XKB_KEY_F1: case XKB_KEY_KP_F1:
      escseq = CONS_KEY_F1;
      break;
    case XKB_KEY_F2: case XKB_KEY_KP_F2:
      escseq = CONS_KEY_F2;
      break;
    case XKB_KEY_F3: case XKB_KEY_KP_F3:
      escseq = CONS_KEY_F3;
      break;
    case XKB_KEY_F4: case XKB_KEY_KP_F4:
      escseq = CONS_KEY_F4;
      break;
    case XKB_KEY_F5:
      escseq = CONS_KEY_F5;
      break;
    case XKB_KEY_F6:
      escseq = CONS_KEY_F6;
      break;
    case XKB_KEY_F7:
      escseq = CONS_KEY_F7;
      break;
    case XKB_KEY_F8:
      escseq = CONS_KEY_F8;
      break;
    case XKB_KEY_F9:
      escseq = CONS_KEY_F9;
      break;
    case XKB_KEY_F10:
      escseq = CONS_KEY_F10;
      break;
    case XKB_KEY_F11:
      escseq = CONS_KEY_F11;
      break;
    case XKB_KEY_F12:
      escseq = CONS_KEY_F12;
      break;
    case XKB_KEY_F13:
      escseq = CONS_KEY_F13;
      break;
    case XKB_KEY_F14:
      escseq = CONS_KEY_F14;
      break;
    case XKB_KEY_F15:
      escseq = CONS_KEY_F15;
      break;
    case XKB_KEY_F16:
      escseq = CONS_KEY_F16;
      break;
    case XKB_KEY_F17:
      escseq = CONS_KEY_F17;
      break;
    case XKB_KEY_F18:
      escseq = CONS_KEY_F18;
      break;
    case XKB_KEY_F19:
      escseq = CONS_KEY_F19;
      break;
    case XKB_KEY_F20:
      escseq = CONS_KEY_F20;
      break;
    case XKB_KEY_Home: case XKB_KEY_KP_Home:
      escseq = CONS_KEY_HOME;
      break;
    case XKB_KEY_Insert: case XKB_KEY_KP_Insert:
      escseq = CONS_KEY_IC;
      break;
    case XKB_KEY_Delete: case XKB_KEY_KP_Delete:
      escseq = CONS_KEY_DC;
      break;
    case XKB_KEY_End: case XKB_KEY_KP_End:
      escseq = CONS_KEY_END;
      break;
    case XKB_KEY_Prior: case XKB_KEY_KP_Prior:
      escseq = CONS_KEY_PPAGE;
      break;
    case XKB_KEY_Next: case XKB_KEY_KP_Next:
      escseq = CONS_KEY_NPAGE;
      break;
    case XKB_KEY_KP_Begin:
      escseq = CONS_KEY_B2;
      break;
    case XKB_KEY_ISO_Left_Tab:
      escseq = CONS_KEY_BTAB;
      break;
    case XKB_KEY_Return: case XKB_KEY_KP_Enter:
      escseq = "\x0d";
      break;
    case XKB_KEY_Tab: case XKB_KEY_KP_Tab:
      escseq = "\t";
      break;
    case XKB_KEY_Escape:
      escseq = "\e";
      break;
    }
  return escseq;
}

void
xkb_compose_update (keypress_t key)
{
  if (!key.rel)
    xkb_compose_state_feed(compose_state, key.keycode);
}

void
xkb_compose_update_fini (void)
{
  enum xkb_compose_status status = xkb_compose_state_get_status(compose_state);
  if (status == XKB_COMPOSE_CANCELLED || status == XKB_COMPOSE_COMPOSED)
    xkb_compose_state_reset(compose_state);
}

/* update the xkb state with the key event*/
void
xkb_state_update_input_key (keypress_t key)
{
  enum xkb_key_direction direction = key.rel ? XKB_KEY_UP : XKB_KEY_DOWN;
  xkb_state_update_key (state, key.keycode, direction);
}

/* convert the keycode to a xkb keysym */
static xkb_keysym_t
get_keysym_from_keycode (keycode_t keycode)
{
  xkb_keysym_t sym;

  sym = xkb_state_key_get_one_sym (state, keycode);
  enum xkb_compose_status status = xkb_compose_state_get_status (compose_state);
  if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
    return XKB_KEYSYM_MAX + 1;

  if (status == XKB_COMPOSE_COMPOSED) {
      sym = xkb_compose_state_get_one_sym(compose_state);
    }

  return sym;
}

/* Take an input event and apply his effect, sending the result to the console */
void
process_keypress_event (keycode_t keycode)
{
  char buf[100];
  size_t size = 0;
  xkb_keysym_t input;

  debug_printf ("input: %d\n", keycode);

  if (execute_action (keycode)) // execute action if any
    return;

  input = get_keysym_from_keycode (keycode); // convert key to input

  debug_printf ("handle: %d\n", input);
  if (input > XKB_KEYSYM_MAX)
    return;

  /* If the real modifier MOD1 (AKA Alt) is set generate an ESC symbol.  */
  if(xkb_state_mod_name_is_active (state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE) > 0)
    buf[size++] = '\e';

  buf[size] = '\0';

  char *escseq = get_special_char_interpretation (input);

  if (escseq != NULL)
    {
      strcat (buf + size, escseq);
      size += strlen (escseq);
    }
  else
    {
      char *buffer = &buf[size];
      size_t left = sizeof (buf) - size;
      int nr;

      /* Control key behaviour.  */
      if (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0)
        {
          input = symtoctrlsym (input);
          buffer[0] = input;
          buffer[1] = '\0';
          nr = 2;
          size++;
        }
      else
        {
          nr = xkb_keysym_to_utf8 (input, buffer, left);
        }

      if (nr == -1)
        {
          console_error (L"Input buffer overflow");
          size = 0;
        }
      else if (nr == 0)
        size = 0;
      else
        size = nr - 1; // don’t include terminating byte
    }

  if (size > 0 && size <= sizeof(buf))
    console_input (buf, size);
}

void
process_input(keypress_t key)
{
  debug_printf ("keyboard event, keycode: %i, rel %i\n", key.keycode, key.rel);

  /* update the compose state to be able to know the modifiers when retrieving the key */
  xkb_compose_update (key);

  /* activate timers */
  if (key.rel || xkb_keymap_key_repeats (keymap, key.keycode))
    xkb_timer_notify_input (key);
  /* send input for key pressed */
  if (!key.rel)
    process_keypress_event (key.keycode);
  /* clear compose if finished or canceled */
  xkb_compose_update_fini ();
  /* update the keyboard state for the next input */
  xkb_state_update_input_key (key);
}


error_t
xkb_load_keymap (const char *rules, const char *model, const char *layout, const char *variant, const char* options)
{
  struct xkb_rule_names names = {
    .rules = rules,
    .model = model,
    .layout = layout,
    .variant = variant,
    .options = options
  };
  keymap = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);

  if (!keymap)
    return ENOMEM;

  return 0;
}

error_t
xkb_compose_init (const char *composefile) {
  const char *locale;
  FILE *cf;
  locale = setlocale (LC_CTYPE, NULL);

  if (composefile != NULL)
    {
      cf = fopen (composefile, "r");
      if (cf == NULL)
        return errno;
      compose_table = xkb_compose_table_new_from_file (ctx, cf, locale, XKB_COMPOSE_FORMAT_TEXT_V1, XKB_COMPOSE_COMPILE_NO_FLAGS);
    }
  else
    compose_table = xkb_compose_table_new_from_locale (ctx, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);

  if (!compose_table)
    return ENOMEM;

  compose_state = xkb_compose_state_new (compose_table, XKB_COMPOSE_STATE_NO_FLAGS);
  if (!compose_state)
    return ENOMEM;
  return 0;
}

error_t
xkb_context_init (const char *rules, const char *model, const char *layout, const char *variant, const char* options, const char *composefile)
{
  /* initialize a xka context */
  ctx = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
  if (!ctx)
    return ENOMEM;

  xkb_load_keymap (rules, model, layout, variant, options);

  /* load the state */
  state = xkb_state_new (keymap);
  if (!state)
    return ENOMEM;

  return xkb_compose_init (composefile);
}

void
xkb_context_cleanup (void)
{
  /* compose cleanup */
  xkb_compose_state_unref (compose_state);
  xkb_compose_table_unref (compose_table);

  /* state cleanup */
  xkb_state_unref (state);

  /* keymap cleanup */
  xkb_keymap_unref (keymap);

  /* context cleanup */
  xkb_context_unref (ctx);
}

int
get_min_keycode (void)
{
  return xkb_keymap_min_keycode (keymap);
}