blob: 8b5418894e0dc69fc4dedf2e90eef3216d075ed3 (
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
|
#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;
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);
fcntl(kbfd,F_SETFL,O_NONBLOCK);
}
if(kbfd == -1) {
fprintf(stderr,"# keyboard shit fucked up.\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;
}
|