summaryrefslogtreecommitdiff
path: root/src/keyboard_x11.c
blob: 67def885e08b8b5a4eb56beac1661dc4b9ba355c (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
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
//so... this gets called... where? input.c????
#include <stdio.h>
#include "common.h"
#include <X11/keysym.h>
#include <X11/Xutil.h>//XLookupString()
#include "input.h" //we need to call the function that does stuff with the keys?
#include "keyboard.h"
#include "keyboard_x11.h" //for event mask and key definitions

extern struct hvr_global global;

#include "graphics_c3.h"
extern struct gra_global gra_global;

#include "graphics_x11.h"
extern struct x11_global x11_global;

int keyboard_init() {//return the fd that events should be read from.
  return x11_global.fd;//good for now?
}

//I need some xkey -> HVK conversion
hvk_t x11_keypress_handler(XKeyEvent *);
hvk_t x11_passthru(XKeyEvent *);

hvk_t get_keyboard_event() {//this returns a HVK_ key + for keydown and - for keyup?
  XEvent e;
  if(XCheckMaskEvent(x11_global.dpy,HV_X11_KB_EVENT_MASK,&e)) {
    switch(e.type) {
      case KeyPress:
        if(gra_global.input_mode == 0) return x11_keypress_handler(&e.xkey);
        else return x11_passthru(&e.xkey);
      case KeyRelease:
        if(gra_global.input_mode == 0) return -x11_keypress_handler(&e.xkey);
      default:
        return 0;//I have not idea how this happened.
    }
  }
  return 0;
}

hvk_t x11_passthru(XKeyEvent *xkey) {
  int i,len,sym=XLookupKeysym(xkey,0);
  char line[8];
  char line2[16];
  switch(sym) {
    case XK_Return:
      strcpy(line,"\n");
      len=1;
      break;
    case XK_Left://hack. probably just replace this with printf()s
      strcpy(line,"\x1b[D");
      len=3;
      break;
    case XK_Right:
      strcpy(line,"\x1b[C");
      len=3;
      break;
    case XK_Down:
      strcpy(line,"\x1b[B");
      len=3;
      break;
    case XK_Up:
      strcpy(line,"\x1b[A");
      len=3;
      break;
    default:
      len=XLookupString(xkey,line,1023,NULL,NULL);
      break;
  }
  for(i=0;i/2 < len;i++) {
    line2[i]="0123456789abcdef"[(line[i/2]>>(4*(1-(i%2)))) % 16];
  }
  line2[i]=0;
  printf("%s data %s\n",global.user,line2);
  return 0;
}

hvk_t x11_keypress_handler(XKeyEvent *xkey) {//this only needs to return HVK_ keys based on the XKeyEvent's value... this could be a sparse array.
  int sym=XLookupKeysym(xkey,0);
  switch(sym) {
    case XK_r: return HVK_UP;
    case XK_w: return HVK_FORWARD;
    case XK_space: return HVK_JUMP;
    case XK_s: return HVK_BACKWARD;
    case XK_a: return HVK_LEFT;
    case XK_d: return HVK_RIGHT;
    case XK_Escape: return HVK_ESCAPE;
    case XK_f: return HVK_MAGIC;
    default: return 0;//HVK_NONE;//0
  }
  return 0;
}