summaryrefslogtreecommitdiff
path: root/src/keyboard_die.c
blob: e043db4f2d9c315bf62b759231de95e36894ce5a (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
#include <stdio.h>
#include <string.h>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>

#include "keyboard.h"

#define KBDEV "/dev/input/event0"

int kbfd = -1;

int keyboard_init() {
  if((kbfd=open(KBDEV,O_RDWR)) == -1) {
    fprintf(stderr,"# failed to open keyboard: %s\n",KBDEV);
  }
  return kbfd;
}

/*void keyboard_event_handler(struct *me,char *junk) {
  //wtf goes here?
}*/

hvk_t die_keypress_handler(unsigned short code) {
  switch(code) {
    case KEY_W: return HVK_FORWARD;
    case KEY_S: return HVK_BACKWARD;
    case KEY_A: return HVK_LEFT;
    case KEY_D: return HVK_RIGHT;
    case KEY_SPACE: return HVK_JUMP;
    case KEY_ESC: return HVK_ESCAPE;
  }
  return 0;//I dunno.
}

hvk_t get_keyboard_event() {
  struct input_event ie;
  int l;
  memset(&ie,0,sizeof(ie));
  if(kbfd == -1) {
    kbfd=open(KBDEV,O_RDWR);
    //probably not needed anymore
    //fcntl(kbfd,F_SETFL,O_NONBLOCK);
  }
  if(kbfd == -1) {
    fprintf(stderr,"# keyboard shit fucked up. probably permissions error.\n");
    return 1;
  }
  if((l=read(kbfd,&ie,sizeof(ie))) > 0) {
    if(ie.type == 1) {
     fprintf(stderr,"# value: %d code: %d type: %d\n",ie.value,ie.code,ie.type);
     if(ie.value == 0) { //release
      return -die_keypress_handler(ie.code);
     }
     if(ie.value == 1) { //press
      return die_keypress_handler(ie.code);
     }
     if(ie.value == 2) { //repeated from being held down
      //we should return die_keypress_handler(ie.code) if we want to allow outside repeats to be treated like a rapid-fire button.
     }
    }
  }
  return 0;
}