summaryrefslogtreecommitdiff
path: root/console-client/xkb/kstoucs.c
blob: fb62445e3f8086dd85741d5576622810f045eef3 (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
#include <assert.h>

struct ksmap {
  int keysym;
  unsigned int ucs;
};

#include "kstoucs_map.c"

/* Binary search through `kstoucs_map'.  */
static unsigned int
find_ucs (int keysym, struct ksmap *first, struct ksmap *last)
{
  struct ksmap *middle = first + (last - first) / 2;

  assert (first <= last);

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

unsigned int
KeySymToUcs4 (int keysym)
{
#ifdef XKB_DEBUG
  char *XKeysymToString(int keysym);
  printf ("KeySymToUcs4: %s (%d) -> ", XKeysymToString (keysym), keysym);
unsigned int doit (int keysym)
{
#endif

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

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

  #define NUM_KEYSYMS (sizeof kstoucs_map / sizeof(struct ksmap))
  return find_ucs(keysym, &kstoucs_map[0], &kstoucs_map[NUM_KEYSYMS - 1]);
#ifdef XKB_DEBUG
}
  unsigned int ret = doit (keysym);
  printf ("%d\n", ret);
  return ret;
#endif
}