summaryrefslogtreecommitdiff
path: root/console-client/xkb/kstoucs.c
blob: e1a553f91ecfa83539c9e9a9df4ae03554b94c0a (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
struct ksmap {
  int keysym;
  unsigned int ucs;
};

#include "kstoucs_map.c"

unsigned int
KeySymToUcs4 (int keysym)
{
  /* Control characters not covered by keysym map. */
  if (keysym > 0 && keysym < 32)
    return keysym;

  /* 'Unicode keysym' */
  if ((keysym & 0xff000000) == 0x01000000)
    return (keysym & 0x00ffffff);

  unsigned int
  find_ucs (int keysym, struct ksmap *first, struct ksmap *last)
  {
    struct ksmap *middle = first + (last - first) / 2;

    if (middle->keysym == keysym)
      return middle->ucs; /* base case: needle found. */
    else if (middle == first && middle == last)
      return 0; /* base case: empty search space. */
    /* recursive cases: halve search space. */
    else if (middle->keysym < keysym)
      return find_ucs (keysym, middle, last);
    else if (middle->keysym > keysym)
      return find_ucs (keysym, first, middle);
  }

  #define NUM_KEYSYMS (sizeof kstoucs_map / sizeof(struct ksmap))
  return find_ucs(keysym, &kstoucs_map[0], &kstoucs_map[NUM_KEYSYMS - 1]);
}