summaryrefslogtreecommitdiff
path: root/src/user/knob/key.c
blob: 1506f3b3b5927df7874f932e9fea64b2345b451c (plain) (blame)
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
#include <keypack.h>

static const char no_mod[] = {
   0,   0,   0,   0,   0,   0,   0,   0,   0, '\t','\n',  0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  ' ',  0,   0,   0,   0,   0,   0, '\'',  0,   0,   0,   0,  ',', '-', '.', '/',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  0,  ';',  0,  '=',  0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  '[','\\', ']',  0,   0,
  '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',  0,   0,   0,   0,   0
};

static const char shifted[] = {
   0,   0,   0,   0,   0,   0,   0,   0,   0, '\t','\n',  0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  ' ',  0,   0,   0,   0,   0,   0,  '"',  0,   0,   0,   0,  '<', '_', '>', '?',
  ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',  0,  ':',  0,  '+',  0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  '{', '|', '}',  0,   0,
  '~', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',  0,   0,   0,   0,   0
};

static const char caps[] = {
   0,   0,   0,   0,   0,   0,   0,   0,   0, '\t','\n',  0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  ' ',  0,   0,   0,   0,   0,   0, '\'',  0,   0,   0,   0,  ',', '-', '.', '/',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  0,  ';',  0,  '=',  0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  '[','\\', ']',  0,   0,
  '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',  0,   0,   0,   0,   0
};

static const char sh_caps[] = {
   0,   0,   0,   0,   0,   0,   0,   0,   0, '\t','\n',  0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  ' ',  0,   0,   0,   0,   0,   0,  '"',  0,   0,   0,   0,  '<', '_', '>', '?',
  ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',  0,  ':',  0,  '+',  0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  '{', '|', '}',  0,   0,
  '~', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',  0,   0,   0,   0,   0
};

static const char num[] = {
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '+', '\n', '-', '.', '/'
};

__attribute__ ((pure))
char key_to_char(struct key_packet kp) {
  if (kp.key_id < 0x80) {
    const char ch = (kp.modifiers & SHIFTS
               ? kp.modifiers & CAPS
                 ? sh_caps : shifted
               : kp.modifiers & CAPS
                 ? caps : no_mod)[kp.key_id];
    return ch ? kp.modifiers & ALTS ? 0x80 & ch : ch : 0;
  }

  else if ((kp.modifiers & NUM) && ((kp.key_id & 0xf0) == 0xb0))
    return num[kp.key_id & 0x0f];

  else
    return 0;
}