Update
[anarch.git] / smallinput.h
blobcee85079c329bdada54f48688df47bb1242b4776
1 /**
2 @file smallinput.h
4 Small API for getting keyboard/mouse input, with possiblity to record it and
5 play back.
7 The Linux Input API requires root pirivileges (sudo).
9 by Milsolav "drummyfish" Ciz, released under CC0 1.0 (public domain)
12 #ifndef _SMALLINPUT_H
13 #define _SMALLINPUT_H
15 #include <stdint.h>
16 #include <stdio.h>
18 #define SMALLINPUT_MODE_NORMAL 0 ///< Only handle input.
19 #define SMALLINPUT_MODE_RECORD 1 ///< Handle input and record it.
20 #define SMALLINPUT_MODE_PLAY 2 ///< Play back recorded input.
22 #define SMALLINPUT_KEY_NONE 0
23 #define SMALLINPUT_SPACE ' '
24 #define SMALLINPUT_BACKSPACE 8
25 #define SMALLINPUT_TAB 9
26 #define SMALLINPUT_RETURN 13
27 #define SMALLINPUT_SHIFT 14
28 #define SMALLINPUT_ESCAPE 27
29 #define SMALLINPUT_DELETE 127
30 #define SMALLINPUT_ARROW_UP 128
31 #define SMALLINPUT_ARROW_RIGHT 129
32 #define SMALLINPUT_ARROW_DOWN 130
33 #define SMALLINPUT_ARROW_LEFT 131
34 #define SMALLINPUT_F1 132
35 #define SMALLINPUT_F2 133
36 #define SMALLINPUT_F3 134
37 #define SMALLINPUT_F4 135
38 #define SMALLINPUT_F5 136
39 #define SMALLINPUT_F6 137
40 #define SMALLINPUT_F7 138
41 #define SMALLINPUT_F8 139
42 #define SMALLINPUT_F9 140
43 #define SMALLINPUT_F10 141
44 #define SMALLINPUT_F11 142
45 #define SMALLINPUT_F12 143
46 #define SMALLINPUT_CTRL 144
47 #define SMALLINPUT_MOUSE_L 253
48 #define SMALLINPUT_MOUSE_M 254
49 #define SMALLINPUT_MOUSE_R 255
51 #define SMALLINPUT_RECORD_KEY_DOWN 1 ///< Mouse down event, followed by code.
52 #define SMALLINPUT_RECORD_KEY_UP 2 ///< Moue up event, followed by code.
53 #define SMALLINPUT_RECORD_MOUSE_X 3 ///< Mouse x move, followed by s32 value.
54 #define SMALLINPUT_RECORD_MOUSE_Y 4 ///< Mouse y move, followed by s32 value.
55 #define SMALLINPUT_RECORD_END 255 ///< Record end, followed by 4 more same values.
57 uint8_t input_keyStates[256];
58 int32_t input_mousePosition[2];
60 uint8_t input_keyStatesPrevious[256];
61 int32_t input_mousePositionPrevious[2];
63 uint8_t input_mode;
65 uint32_t input_frame = 0;
66 uint8_t *input_recordData;
67 uint32_t input_recordPosition;
68 uint32_t input_recordSize;
70 #if 1 // TODO: add other options for input handling (SDL, xinput, ...)
72 This is using Linux Input Subsystem API. Defines can be found in
73 include/uapi/linux/input-event-codes.h.
76 #include <fcntl.h>
77 #include <linux/input.h>
78 #include <sys/time.h>
79 #include <unistd.h>
81 typedef struct
83 struct timeval time;
84 uint16_t type;
85 uint16_t code;
86 int32_t value;
87 } LinuxInputEvent;
89 #define INPUT_KEYBOARD_FILE "/dev/input/event0"
90 #define INPUT_MOUSE_FILE "/dev/input/event1"
92 int input_keyboardFile = 0;
93 int input_mouseFile = 0;
95 /**
96 Maps this library's key codes to linux input key codes.
98 static const int input_linuxCodes[256] =
100 #define no KEY_RESERVED
101 no,no,no,no,no,no,no,no,KEY_BACKSPACE,KEY_TAB,no,no,no,KEY_ENTER,KEY_LEFTSHIFT,no,
102 no,no,no,no,no,no,no,no,no,no,no,KEY_ESC,no,no,no,no,
103 KEY_SPACE,no,no,no,no,no,no,no,no,no,no,no,KEY_COMMA,no,KEY_DOT,no,
104 KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6,KEY_7,KEY_8,KEY_9,no,KEY_SEMICOLON,no,KEY_EQUAL,no,KEY_QUESTION,
105 no,KEY_A,KEY_B,KEY_C,KEY_D,KEY_E,KEY_F,KEY_G,KEY_H,KEY_I,KEY_J,KEY_K,KEY_L,KEY_M,KEY_N,KEY_O,
106 KEY_P,KEY_Q,KEY_R,KEY_S,KEY_T,KEY_U,KEY_V,KEY_W,KEY_X,KEY_Y,KEY_Z,no,no,no,no,no,
107 no,KEY_A,KEY_B,KEY_C,KEY_D,KEY_E,KEY_F,KEY_G,KEY_H,KEY_I,KEY_J,KEY_K,KEY_L,KEY_M,KEY_N,KEY_O,
108 KEY_P,KEY_Q,KEY_R,KEY_S,KEY_T,KEY_U,KEY_V,KEY_W,KEY_X,KEY_Y,KEY_Z,no,no,no,no,KEY_DELETE,
109 KEY_UP,KEY_RIGHT,KEY_DOWN,KEY_LEFT,KEY_F1,KEY_F2,KEY_F3,KEY_F4,KEY_F5,KEY_F6,KEY_F7,KEY_F8,KEY_F9,KEY_F10,KEY_F11,KEY_F12,
110 KEY_LEFTCTRL,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
111 no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
112 no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
113 no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
114 no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
115 no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
116 no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no
117 #undef no
119 #endif
121 void input_recordU8(uint8_t value)
123 if (input_recordPosition < (input_recordSize - 1))
125 input_recordData[input_recordPosition] = value;
126 input_recordPosition++;
130 void input_recordU32(uint32_t value)
132 for (uint8_t i = 0; i < 4; ++i)
134 input_recordU8(value % 256);
135 value /= 256;
139 uint32_t input_readU32(uint8_t *data)
141 uint32_t result = 0;
143 for (uint8_t i = 0; i < 4; ++i)
144 result = result * 256 + data[3 - i];
146 return result;
150 Initializes the library with given mode (SMALLINPUT_MODE_*).
152 uint8_t input_init(uint8_t mode, uint8_t *recordData, uint32_t recordDataLength)
154 input_mode = mode;
156 input_mousePosition[0] = 0;
157 input_mousePosition[1] = 0;
159 input_mousePositionPrevious[0] = 0;
160 input_mousePositionPrevious[1] = 0;
162 input_frame = 0;
163 input_recordData = recordData;
164 input_recordPosition = 0;
165 input_recordSize = recordDataLength;
167 for (int16_t i = 0; i < 256; ++i)
169 input_keyStates[i] = 0;
170 input_keyStatesPrevious[i] = 0;
173 uint8_t result = 1;
175 if (input_mode != SMALLINPUT_MODE_PLAY)
177 input_keyboardFile = open(INPUT_KEYBOARD_FILE, O_RDONLY);
179 result = input_keyboardFile >= 0;
181 if (result)
183 fcntl(input_keyboardFile, F_SETFL, O_NONBLOCK);
185 input_mouseFile = open(INPUT_MOUSE_FILE, O_RDONLY);
187 result = input_mouseFile >= 0;
189 if (result)
190 fcntl(input_mouseFile, F_SETFL, O_NONBLOCK);
193 if (!result)
194 puts("could not open device file (are you root?)");
197 return result;
200 void input_end(void)
202 if (input_mode == SMALLINPUT_MODE_RECORD)
203 for (uint8_t i = 0; i < 5; ++i)
204 input_recordU8(SMALLINPUT_RECORD_END);
206 close(input_keyboardFile);
207 close(input_mouseFile);
211 Should be called once every main loop iteration to retrieve current input
212 state.
214 void input_update(void)
216 LinuxInputEvent event;
218 if (input_mode == SMALLINPUT_MODE_PLAY)
220 while (input_recordPosition < input_recordSize)
222 uint32_t nextFrame = input_readU32(input_recordData + input_recordPosition);
224 if (input_frame >= nextFrame)
226 input_recordPosition += 4;
228 uint8_t rec = input_recordData[input_recordPosition];
230 switch (rec)
232 case SMALLINPUT_RECORD_KEY_DOWN:
233 case SMALLINPUT_RECORD_KEY_UP:
234 input_recordPosition++;
235 input_keyStates[input_recordData[input_recordPosition]] = rec == SMALLINPUT_RECORD_KEY_DOWN;
236 input_recordPosition++;
237 break;
239 case SMALLINPUT_RECORD_MOUSE_X:
240 case SMALLINPUT_RECORD_MOUSE_Y:
241 input_recordPosition++;
242 input_mousePosition[rec == SMALLINPUT_RECORD_MOUSE_Y] =
243 input_readU32(input_recordData + input_recordPosition);
244 input_recordPosition += 4;
245 break;
247 case SMALLINPUT_RECORD_END:
248 input_recordPosition = input_recordSize;
249 break;
251 default: /*printf("corrupt record\n");*/ break;
254 else
255 break;
258 else
260 while (1) // keyboard
262 if (read(input_keyboardFile, &event, sizeof(event)) <= 0)
263 break;
265 if (event.type == EV_KEY && (event.value == 1 || event.value == 0))
266 for (uint16_t i = 0; i < 256; ++i)
267 if (event.code == input_linuxCodes[i])
269 input_keyStates[i] = event.value;
270 break;
274 while (1) // mouse
276 if (read(input_mouseFile, &event, sizeof(event)) <= 0)
277 break;
279 if (event.type == EV_REL)
280 input_mousePosition[event.code % 2] += event.value;
281 else if (event.type == EV_KEY)
283 input_keyStates[
284 event.code == BTN_LEFT ? SMALLINPUT_MOUSE_L :
285 (event.code == BTN_RIGHT ? SMALLINPUT_MOUSE_R : SMALLINPUT_MOUSE_M)]
286 = event.value;
291 for (uint16_t i = 0; i < 256; ++i)
292 if (input_keyStates[i] && input_keyStates[i] < 255)
293 input_keyStates[i]++;
295 if (input_mode == SMALLINPUT_MODE_RECORD)
297 for (uint8_t i = 0; i < 2; ++i) // record mouse events
298 if (input_mousePositionPrevious[i] != input_mousePosition[i])
300 input_recordU32(input_frame + 1);
301 input_recordU8((i == 0) ? SMALLINPUT_RECORD_MOUSE_X : SMALLINPUT_RECORD_MOUSE_Y);
302 input_recordU32(input_mousePosition[i]);
304 input_mousePositionPrevious[i] = input_mousePosition[i];
307 for (uint16_t i = 0; i < 256; ++i) // record key events
309 uint8_t a = input_keyStates[i] > 0;
310 uint8_t b = input_keyStatesPrevious[i] > 0;
312 if (a != b)
314 input_recordU32(input_frame + 1);
315 input_recordU8(a ? SMALLINPUT_RECORD_KEY_DOWN : SMALLINPUT_RECORD_KEY_UP);
316 input_recordU8(i);
318 input_keyStatesPrevious[i] = input_keyStates[i];
323 input_frame++;
327 Returns the number of input frames for which given key has been pressed (> 1:
328 key is pressed, == 1: key was just pressed, == 0: key is not pressed).
330 static inline uint8_t input_getKey(uint8_t key)
332 if (key >= 'a' && key <= 'z')
333 key = 'A' + (key - 'a');
335 return input_keyStates[key];
339 Gets the mouse position.
341 static inline void input_getMousePos(int32_t *x, int32_t *y)
343 *x = input_mousePosition[0];
344 *y = input_mousePosition[1];
347 static inline void input_setMousePos(int32_t x, int32_t y)
349 input_mousePosition[0] = x;
350 input_mousePosition[1] = y;
354 Prints the current input state.
356 void input_print()
358 printf("frame: %d\nmouse pos: %d %d",input_frame,input_mousePosition[0],input_mousePosition[1]);
360 for (uint16_t i = 0; i < 256; ++i)
362 if (i % 8 == 0)
363 putchar('\n');
365 char c = (i > ' ' && i <= 126) ? i : '?';
367 uint8_t n = input_getKey(i);
369 printf("%s",n ? " [" : " ");
370 printf("%03d (\'%c\'): %03d",i,c,input_getKey(i));
371 printf("%s",n ? "] " : " ");
374 putchar('\n');
377 void input_printRecord()
379 for (uint32_t i = 0; i < input_recordPosition; ++i)
381 if (i % 32 == 0)
382 putchar('\n');
384 printf("%d,",input_recordData[i]);
388 uint32_t input_hash()
390 uint32_t result = 0;
392 for (uint16_t i = 0; i < 256; ++i)
393 result += input_getKey(i) * (i + 1);
395 int32_t x, y;
397 input_getMousePos(&x,&y);
399 result += x + y << 16;
401 return result;
404 #endif // guard