anarch/smallinput.h

405 lines
10 KiB
C
Raw Normal View History

2020-10-17 14:59:07 -04:00
/**
@file smallinput.h
Small API for getting keyboard/mouse input, with possiblity to record it and
play back.
The Linux Input API requires root pirivileges (sudo).
by Milsolav "drummyfish" Ciz, released under CC0 1.0 (public domain)
*/
#ifndef _SMALLINPUT_H
#define _SMALLINPUT_H
#include <stdint.h>
2020-10-18 05:36:38 -04:00
#include <stdio.h>
#define SMALLINPUT_MODE_NORMAL 0 ///< Only handle input.
#define SMALLINPUT_MODE_RECORD 1 ///< Handle input and record it.
#define SMALLINPUT_MODE_PLAY 2 ///< Play back recorded input.
2020-10-17 14:59:07 -04:00
#define SMALLINPUT_KEY_NONE 0
#define SMALLINPUT_SPACE ' '
#define SMALLINPUT_BACKSPACE 8
#define SMALLINPUT_TAB 9
#define SMALLINPUT_RETURN 13
#define SMALLINPUT_SHIFT 14
#define SMALLINPUT_ESCAPE 27
#define SMALLINPUT_DELETE 127
2020-10-18 05:36:38 -04:00
#define SMALLINPUT_ARROW_UP 128
#define SMALLINPUT_ARROW_RIGHT 129
#define SMALLINPUT_ARROW_DOWN 130
#define SMALLINPUT_ARROW_LEFT 131
#define SMALLINPUT_F1 132
#define SMALLINPUT_F2 133
#define SMALLINPUT_F3 134
#define SMALLINPUT_F4 135
#define SMALLINPUT_F5 136
#define SMALLINPUT_F6 137
#define SMALLINPUT_F7 138
#define SMALLINPUT_F8 139
#define SMALLINPUT_F9 140
#define SMALLINPUT_F10 141
#define SMALLINPUT_F11 142
#define SMALLINPUT_F12 143
#define SMALLINPUT_CTRL 144
2020-10-17 14:59:07 -04:00
#define SMALLINPUT_MOUSE_L 253
#define SMALLINPUT_MOUSE_M 254
#define SMALLINPUT_MOUSE_R 255
2020-10-18 05:36:38 -04:00
#define SMALLINPUT_RECORD_KEY_DOWN 1 ///< Mouse down event, followed by code.
#define SMALLINPUT_RECORD_KEY_UP 2 ///< Moue up event, followed by code.
#define SMALLINPUT_RECORD_MOUSE_X 3 ///< Mouse x move, followed by s32 value.
#define SMALLINPUT_RECORD_MOUSE_Y 4 ///< Mouse y move, followed by s32 value.
#define SMALLINPUT_RECORD_END 255 ///< Record end, followed by 4 more same values.
2020-10-17 14:59:07 -04:00
uint8_t input_keyStates[256];
int32_t input_mousePosition[2];
2020-10-18 05:36:38 -04:00
uint8_t input_keyStatesPrevious[256];
int32_t input_mousePositionPrevious[2];
uint8_t input_mode;
2020-10-17 14:59:07 -04:00
uint32_t input_frame = 0;
2020-10-18 05:36:38 -04:00
uint8_t *input_recordData;
uint32_t input_recordPosition;
uint32_t input_recordSize;
2020-10-17 14:59:07 -04:00
#if 1 // TODO: add other options for input handling (SDL, xinput, ...)
/*
This is using Linux Input Subsystem API. Defines can be found in
include/uapi/linux/input-event-codes.h.
*/
#include <fcntl.h>
#include <linux/input.h>
#include <sys/time.h>
#include <unistd.h>
typedef struct
{
struct timeval time;
uint16_t type;
uint16_t code;
int32_t value;
} LinuxInputEvent;
#define INPUT_KEYBOARD_FILE "/dev/input/event0"
#define INPUT_MOUSE_FILE "/dev/input/event1"
int input_keyboardFile = 0;
int input_mouseFile = 0;
/**
Maps this library's key codes to linux input key codes.
*/
static const int input_linuxCodes[256] =
{
#define no KEY_RESERVED
no,no,no,no,no,no,no,no,KEY_BACKSPACE,KEY_TAB,no,no,no,KEY_ENTER,KEY_LEFTSHIFT,no,
no,no,no,no,no,no,no,no,no,no,no,KEY_ESC,no,no,no,no,
2020-10-18 05:36:38 -04:00
KEY_SPACE,no,no,no,no,no,no,no,no,no,no,no,KEY_COMMA,no,KEY_DOT,no,
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,
2020-10-17 14:59:07 -04:00
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,
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,
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,
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,
2020-10-18 05:36:38 -04:00
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,
KEY_LEFTCTRL,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
2020-10-17 14:59:07 -04:00
no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no
#undef no
};
#endif
2020-10-18 05:36:38 -04:00
void input_recordU8(uint8_t value)
{
if (input_recordPosition < (input_recordSize - 1))
{
input_recordData[input_recordPosition] = value;
input_recordPosition++;
}
}
void input_recordU32(uint32_t value)
{
for (uint8_t i = 0; i < 4; ++i)
{
input_recordU8(value % 256);
value /= 256;
}
}
uint32_t input_readU32(uint8_t *data)
{
uint32_t result = 0;
for (uint8_t i = 0; i < 4; ++i)
result = result * 256 + data[3 - i];
return result;
}
2020-10-17 14:59:07 -04:00
/**
2020-10-18 05:36:38 -04:00
Initializes the library with given mode (SMALLINPUT_MODE_*).
2020-10-17 14:59:07 -04:00
*/
2020-10-18 05:36:38 -04:00
uint8_t input_init(uint8_t mode, uint8_t *recordData, uint32_t recordDataLength)
2020-10-17 14:59:07 -04:00
{
2020-10-18 05:36:38 -04:00
input_mode = mode;
2020-10-17 14:59:07 -04:00
input_mousePosition[0] = 0;
input_mousePosition[1] = 0;
2020-10-18 05:36:38 -04:00
input_mousePositionPrevious[0] = 0;
input_mousePositionPrevious[1] = 0;
2020-10-17 14:59:07 -04:00
input_frame = 0;
2020-10-18 05:36:38 -04:00
input_recordData = recordData;
input_recordPosition = 0;
input_recordSize = recordDataLength;
2020-10-17 14:59:07 -04:00
for (int16_t i = 0; i < 256; ++i)
2020-10-18 05:36:38 -04:00
{
2020-10-17 14:59:07 -04:00
input_keyStates[i] = 0;
2020-10-18 05:36:38 -04:00
input_keyStatesPrevious[i] = 0;
}
uint8_t result = 1;
if (input_mode != SMALLINPUT_MODE_PLAY)
{
input_keyboardFile = open(INPUT_KEYBOARD_FILE, O_RDONLY);
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
result = input_keyboardFile >= 0;
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
if (result)
{
fcntl(input_keyboardFile, F_SETFL, O_NONBLOCK);
input_mouseFile = open(INPUT_MOUSE_FILE, O_RDONLY);
result = input_mouseFile >= 0;
if (result)
fcntl(input_mouseFile, F_SETFL, O_NONBLOCK);
}
if (!result)
puts("could not open device file (are you root?)");
}
return result;
2020-10-17 14:59:07 -04:00
}
void input_end(void)
{
2020-10-18 05:36:38 -04:00
if (input_mode == SMALLINPUT_MODE_RECORD)
for (uint8_t i = 0; i < 5; ++i)
input_recordU8(SMALLINPUT_RECORD_END);
2020-10-17 14:59:07 -04:00
close(input_keyboardFile);
close(input_mouseFile);
}
/**
Should be called once every main loop iteration to retrieve current input
state.
*/
void input_update(void)
{
LinuxInputEvent event;
2020-10-18 05:36:38 -04:00
if (input_mode == SMALLINPUT_MODE_PLAY)
2020-10-17 14:59:07 -04:00
{
2020-10-18 05:36:38 -04:00
while (input_recordPosition < input_recordSize)
{
uint32_t nextFrame = input_readU32(input_recordData + input_recordPosition);
if (input_frame >= nextFrame)
{
input_recordPosition += 4;
uint8_t rec = input_recordData[input_recordPosition];
switch (rec)
{
case SMALLINPUT_RECORD_KEY_DOWN:
case SMALLINPUT_RECORD_KEY_UP:
input_recordPosition++;
input_keyStates[input_recordData[input_recordPosition]] = rec == SMALLINPUT_RECORD_KEY_DOWN;
input_recordPosition++;
break;
case SMALLINPUT_RECORD_MOUSE_X:
case SMALLINPUT_RECORD_MOUSE_Y:
input_recordPosition++;
input_mousePosition[rec == SMALLINPUT_RECORD_MOUSE_Y] =
input_readU32(input_recordData + input_recordPosition);
input_recordPosition += 4;
break;
case SMALLINPUT_RECORD_END:
input_recordPosition = input_recordSize;
break;
default: /*printf("corrupt record\n");*/ break;
2020-10-17 14:59:07 -04:00
}
2020-10-18 05:36:38 -04:00
}
else
break;
}
2020-10-17 14:59:07 -04:00
}
2020-10-18 05:36:38 -04:00
else
2020-10-17 14:59:07 -04:00
{
2020-10-18 05:36:38 -04:00
while (1) // keyboard
{
if (read(input_keyboardFile, &event, sizeof(event)) <= 0)
break;
if (event.type == EV_KEY && (event.value == 1 || event.value == 0))
for (uint16_t i = 0; i < 256; ++i)
if (event.code == input_linuxCodes[i])
{
input_keyStates[i] = event.value;
break;
}
}
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
while (1) // mouse
2020-10-17 14:59:07 -04:00
{
2020-10-18 05:36:38 -04:00
if (read(input_mouseFile, &event, sizeof(event)) <= 0)
break;
if (event.type == EV_REL)
input_mousePosition[event.code % 2] += event.value;
else if (event.type == EV_KEY)
{
input_keyStates[
event.code == BTN_LEFT ? SMALLINPUT_MOUSE_L :
(event.code == BTN_RIGHT ? SMALLINPUT_MOUSE_R : SMALLINPUT_MOUSE_M)]
= event.value;
}
2020-10-17 14:59:07 -04:00
}
}
for (uint16_t i = 0; i < 256; ++i)
if (input_keyStates[i] && input_keyStates[i] < 255)
input_keyStates[i]++;
2020-10-18 05:36:38 -04:00
if (input_mode == SMALLINPUT_MODE_RECORD)
{
for (uint8_t i = 0; i < 2; ++i) // record mouse events
if (input_mousePositionPrevious[i] != input_mousePosition[i])
{
input_recordU32(input_frame + 1);
input_recordU8((i == 0) ? SMALLINPUT_RECORD_MOUSE_X : SMALLINPUT_RECORD_MOUSE_Y);
input_recordU32(input_mousePosition[i]);
input_mousePositionPrevious[i] = input_mousePosition[i];
}
for (uint16_t i = 0; i < 256; ++i) // record key events
{
uint8_t a = input_keyStates[i] > 0;
uint8_t b = input_keyStatesPrevious[i] > 0;
if (a != b)
{
input_recordU32(input_frame + 1);
input_recordU8(a ? SMALLINPUT_RECORD_KEY_DOWN : SMALLINPUT_RECORD_KEY_UP);
input_recordU8(i);
input_keyStatesPrevious[i] = input_keyStates[i];
}
}
}
2020-10-17 14:59:07 -04:00
input_frame++;
}
/**
Returns the number of input frames for which given key has been pressed (> 1:
key is pressed, == 1: key was just pressed, == 0: key is not pressed).
*/
static inline uint8_t input_getKey(uint8_t key)
{
if (key >= 'a' && key <= 'z')
key = 'A' + (key - 'a');
return input_keyStates[key];
}
/**
Gets the mouse position.
*/
2020-10-18 05:36:38 -04:00
static inline void input_getMousePos(int32_t *x, int32_t *y)
2020-10-17 14:59:07 -04:00
{
*x = input_mousePosition[0];
*y = input_mousePosition[1];
}
static inline void input_setMousePos(int32_t x, int32_t y)
{
input_mousePosition[0] = x;
input_mousePosition[1] = y;
}
2020-10-18 05:36:38 -04:00
/**
Prints the current input state.
*/
void input_print()
2020-10-17 14:59:07 -04:00
{
2020-10-18 05:36:38 -04:00
printf("frame: %d\nmouse pos: %d %d",input_frame,input_mousePosition[0],input_mousePosition[1]);
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
for (uint16_t i = 0; i < 256; ++i)
2020-10-17 14:59:07 -04:00
{
2020-10-18 05:36:38 -04:00
if (i % 8 == 0)
putchar('\n');
char c = (i > ' ' && i <= 126) ? i : '?';
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
uint8_t n = input_getKey(i);
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
printf("%s",n ? " [" : " ");
printf("%03d (\'%c\'): %03d",i,c,input_getKey(i));
printf("%s",n ? "] " : " ");
}
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
putchar('\n');
}
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
void input_printRecord()
{
for (uint32_t i = 0; i < input_recordPosition; ++i)
{
if (i % 32 == 0)
putchar('\n');
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
printf("%d,",input_recordData[i]);
2020-10-17 14:59:07 -04:00
}
}
2020-10-18 05:36:38 -04:00
uint32_t input_hash()
{
uint32_t result = 0;
2020-10-17 14:59:07 -04:00
2020-10-18 05:36:38 -04:00
for (uint16_t i = 0; i < 256; ++i)
result += input_getKey(i) * (i + 1);
int32_t x, y;
input_getMousePos(&x,&y);
result += x + y << 16;
return result;
}
#endif // guard