Updated raw input events to match SDL style

Also added raw keyboard events, and implemented raw input events on iOS, OpenBSD console, Linux console, and X11
This commit is contained in:
Sam Lantinga
2024-12-19 16:18:05 -08:00
parent 1025087c2e
commit c44fa5bb07
25 changed files with 515 additions and 312 deletions

View File

@@ -346,8 +346,10 @@ void SDL_EVDEV_Poll(void)
switch (event->type) {
case EV_KEY:
if (event->code >= BTN_MOUSE && event->code < BTN_MOUSE + SDL_arraysize(EVDEV_MouseButtons)) {
Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
mouse_button = event->code - BTN_MOUSE;
SDL_SendMouseButton(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, EVDEV_MouseButtons[mouse_button], (event->value != 0));
SDL_SendRawMouseButton(timestamp, (SDL_MouseID)item->fd, EVDEV_MouseButtons[mouse_button], (event->value != 0));
SDL_SendMouseButton(timestamp, mouse->focus, (SDL_MouseID)item->fd, EVDEV_MouseButtons[mouse_button], (event->value != 0));
break;
}
@@ -367,13 +369,18 @@ void SDL_EVDEV_Poll(void)
}
// Probably keyboard
scancode = SDL_EVDEV_translate_keycode(event->code);
if (event->value == 0) {
SDL_SendKeyboardKey(SDL_EVDEV_GetEventTimestamp(event), (SDL_KeyboardID)item->fd, event->code, scancode, false);
} else if (event->value == 1 || event->value == 2 /* key repeated */) {
SDL_SendKeyboardKey(SDL_EVDEV_GetEventTimestamp(event), (SDL_KeyboardID)item->fd, event->code, scancode, true);
{
Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
scancode = SDL_EVDEV_translate_keycode(event->code);
if (event->value == 0) {
SDL_SendRawKeyboardKey(timestamp, (SDL_KeyboardID)item->fd, event->code, scancode, false);
SDL_SendKeyboardKey(timestamp, (SDL_KeyboardID)item->fd, event->code, scancode, false);
} else if (event->value == 1 || event->value == 2 /* key repeated */) {
SDL_SendRawKeyboardKey(timestamp, (SDL_KeyboardID)item->fd, event->code, scancode, true);
SDL_SendKeyboardKey(timestamp, (SDL_KeyboardID)item->fd, event->code, scancode, true);
}
SDL_EVDEV_kbd_keycode(_this->kbd, event->code, event->value);
}
SDL_EVDEV_kbd_keycode(_this->kbd, event->code, event->value);
break;
case EV_ABS:
switch (event->code) {
@@ -485,7 +492,9 @@ void SDL_EVDEV_Poll(void)
// Send mouse axis changes together to ensure consistency and reduce event processing overhead
if (item->relative_mouse) {
if (item->mouse_x != 0 || item->mouse_y != 0) {
SDL_SendMouseMotion(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse, (float)item->mouse_x, (float)item->mouse_y);
Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
SDL_SendRawMouseMotion(timestamp, (SDL_MouseID)item->fd, item->mouse_x, item->mouse_y, 1.0f, 1.0f);
SDL_SendMouseMotion(timestamp, mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse, (float)item->mouse_x, (float)item->mouse_y);
item->mouse_x = item->mouse_y = 0;
}
} else if (item->range_x > 0 && item->range_y > 0) {
@@ -508,10 +517,15 @@ void SDL_EVDEV_Poll(void)
}
if (item->mouse_wheel != 0 || item->mouse_hwheel != 0) {
SDL_SendMouseWheel(SDL_EVDEV_GetEventTimestamp(event),
Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
const float scale = (item->high_res_hwheel ? 1.0f / 120.0f : 1.0f);
SDL_SendRawMouseWheel(timestamp,
(SDL_MouseID)item->fd,
item->mouse_hwheel, item->mouse_wheel, scale, scale);
SDL_SendMouseWheel(timestamp,
mouse->focus, (SDL_MouseID)item->fd,
item->mouse_hwheel / (item->high_res_hwheel ? 120.0f : 1.0f),
item->mouse_wheel / (item->high_res_wheel ? 120.0f : 1.0f),
item->mouse_hwheel * scale,
item->mouse_wheel * scale,
SDL_MOUSEWHEEL_NORMAL);
item->mouse_wheel = item->mouse_hwheel = 0;
}

View File

@@ -550,7 +550,7 @@ static void Translate_to_text(SDL_WSCONS_input_data *input, keysym_t ksym)
}
}
static void Translate_to_keycode(SDL_WSCONS_input_data *input, int type, keysym_t ksym)
static void Translate_to_keycode(SDL_WSCONS_input_data *input, int type, keysym_t ksym, Uint64 timestamp)
{
struct wscons_keymap keyDesc = input->keymap.map[ksym];
keysym_t *group = &keyDesc.group1[KS_GROUP(keyDesc.group1[0]) == KS_GROUP_Keypad && IS_NUMLOCK_ON ? !IS_SHIFT_HELD : 0];
@@ -560,22 +560,32 @@ static void Translate_to_keycode(SDL_WSCONS_input_data *input, int type, keysym_
switch (keyDesc.command) {
case KS_Cmd_ScrollBack:
{
SDL_SendKeyboardKey(0, input->keyboardID, 0, SDL_SCANCODE_PAGEUP, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendKeyboardKey(timestamp, input->keyboardID, 0, SDL_SCANCODE_PAGEUP, (type == WSCONS_EVENT_KEY_DOWN));
return;
}
case KS_Cmd_ScrollFwd:
{
SDL_SendKeyboardKey(0, input->keyboardID, 0, SDL_SCANCODE_PAGEDOWN, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendKeyboardKey(timestamp, input->keyboardID, 0, SDL_SCANCODE_PAGEDOWN, (type == WSCONS_EVENT_KEY_DOWN));
return;
}
default:
break;
}
for (i = 0; i < sizeof(conversion_table) / sizeof(struct wscons_keycode_to_SDL); i++) {
for (i = 0; i < SDL_arraysize(conversion_table); i++) {
if (conversion_table[i].sourcekey == group[0]) {
SDL_SendKeyboardKey(0, input->keyboardID, group[0], conversion_table[i].targetKey, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendRawKeyboardKey(timestamp, input->keyboardID, group[0], conversion_table[i].targetKey, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendKeyboardKey(timestamp, input->keyboardID, group[0], conversion_table[i].targetKey, (type == WSCONS_EVENT_KEY_DOWN));
return;
}
}
SDL_SendKeyboardKey(0, input->keyboardID, group[0], SDL_SCANCODE_UNKNOWN, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendRawKeyboardKey(timestamp, input->keyboardID, group[0], SDL_SCANCODE_UNKNOWN, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendKeyboardKey(timestamp, input->keyboardID, group[0], SDL_SCANCODE_UNKNOWN, (type == WSCONS_EVENT_KEY_DOWN));
}
static Uint64 GetEventTimestamp(struct timespec *time)
{
// FIXME: Get the event time in the SDL tick time base
return SDL_GetTicksNS();
}
static void updateKeyboard(SDL_WSCONS_input_data *input)
@@ -592,6 +602,7 @@ static void updateKeyboard(SDL_WSCONS_input_data *input)
if ((n = read(input->fd, events, sizeof(events))) > 0) {
n /= sizeof(struct wscons_event);
for (i = 0; i < n; i++) {
Uint64 timestamp = GetEventTimestamp(&events[i].time);
type = events[i].type;
switch (type) {
case WSCONS_EVENT_KEY_DOWN:
@@ -809,15 +820,20 @@ static void updateKeyboard(SDL_WSCONS_input_data *input)
} break;
case WSCONS_EVENT_ALL_KEYS_UP:
for (i = 0; i < SDL_SCANCODE_COUNT; i++) {
SDL_SendKeyboardKey(0, input->keyboardID, 0, (SDL_Scancode)i, false);
SDL_SendRawKeyboardKey(timestamp, input->keyboardID, 0, (SDL_Scancode)i, false);
SDL_SendKeyboardKey(timestamp, input->keyboardID, 0, (SDL_Scancode)i, false);
}
break;
default:
break;
}
if (input->type == WSKBD_TYPE_USB && events[i].value <= 0xE7)
SDL_SendKeyboardKey(0, input->keyboardID, 0, (SDL_Scancode)events[i].value, (type == WSCONS_EVENT_KEY_DOWN));
else
Translate_to_keycode(input, type, events[i].value);
if (input->type == WSKBD_TYPE_USB && events[i].value <= 0xE7) {
SDL_SendRawKeyboardKey(timestamp, input->keyboardID, 0, (SDL_Scancode)events[i].value, (type == WSCONS_EVENT_KEY_DOWN));
SDL_SendKeyboardKey(timestamp, input->keyboardID, 0, (SDL_Scancode)events[i].value, (type == WSCONS_EVENT_KEY_DOWN));
} else {
Translate_to_keycode(input, type, events[i].value, timestamp);
}
if (type == WSCONS_EVENT_KEY_UP) {
continue;
@@ -910,7 +926,7 @@ static void updateKeyboard(SDL_WSCONS_input_data *input)
}
if (IS_ALT_HELD) {
if (input->encoding & KB_METAESC) {
Translate_to_keycode(input, WSCONS_EVENT_KEY_DOWN, KS_Escape);
Translate_to_keycode(input, WSCONS_EVENT_KEY_DOWN, KS_Escape, 0);
Translate_to_text(input, result);
continue;
} else {

View File

@@ -63,6 +63,12 @@ SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse(void)
return input;
}
static Uint64 GetEventTimestamp(struct timespec *time)
{
// FIXME: Get the event time in the SDL tick time base
return SDL_GetTicksNS();
}
void updateMouse(SDL_WSCONS_mouse_input_data *input)
{
struct wscons_event events[64];
@@ -73,56 +79,48 @@ void updateMouse(SDL_WSCONS_mouse_input_data *input)
int i;
n /= sizeof(struct wscons_event);
for (i = 0; i < n; i++) {
Uint64 timestamp = GetEventTimestamp(&events[i].time);
int type = events[i].type;
switch (type) {
case WSCONS_EVENT_MOUSE_DOWN:
{
switch (events[i].value) {
case 0: // Left Mouse Button.
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_BUTTON_LEFT, true);
break;
case 1: // Middle Mouse Button.
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_BUTTON_MIDDLE, true);
break;
case 2: // Right Mouse Button.
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_BUTTON_RIGHT, true);
break;
}
} break;
case WSCONS_EVENT_MOUSE_UP:
{
switch (events[i].value) {
case 0: // Left Mouse Button.
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_BUTTON_LEFT, false);
break;
case 1: // Middle Mouse Button.
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_BUTTON_MIDDLE, false);
break;
case 2: // Right Mouse Button.
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_BUTTON_RIGHT, false);
break;
}
} break;
Uint8 button = SDL_BUTTON_LEFT + events[i].value;
bool down = (type == WSCONS_EVENT_MOUSE_DOWN);
SDL_SendRawMouseButton(timestamp, input->mouseID, button, down);
SDL_SendMouseButton(timestamp, mouse->focus, input->mouseID, button, down);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_X:
{
SDL_SendMouseMotion(0, mouse->focus, input->mouseID, 1, (float)events[i].value, 0.0f);
const float scale = 1.0f;
SDL_SendRawMouseMotion(timestamp, input->mouseID, events[i].value, 0, scale, scale);
SDL_SendMouseMotion(timestamp, mouse->focus, input->mouseID, true, (float)events[i].value, 0.0f);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_Y:
{
SDL_SendMouseMotion(0, mouse->focus, input->mouseID, 1, 0.0f, -(float)events[i].value);
const float scale = 1.0f;
SDL_SendRawMouseMotion(timestamp, input->mouseID, 0, -events[i].value, scale, scale);
SDL_SendMouseMotion(timestamp, mouse->focus, input->mouseID, true, 0.0f, -(float)events[i].value);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_W:
{
SDL_SendMouseWheel(0, mouse->focus, input->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL);
const float scale = 1.0f;
SDL_SendRawMouseWheel(timestamp, input->mouseID, events[i].value, 0, scale, scale);
SDL_SendMouseWheel(timestamp, mouse->focus, input->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_Z:
{
SDL_SendMouseWheel(0, mouse->focus, input->mouseID, 0, -events[i].value, SDL_MOUSEWHEEL_NORMAL);
const float scale = 1.0f;
SDL_SendRawMouseWheel(timestamp, input->mouseID, 0, -events[i].value, scale, scale);
SDL_SendMouseWheel(timestamp, mouse->focus, input->mouseID, 0, -events[i].value, SDL_MOUSEWHEEL_NORMAL);
break;
}
default:
break;
}
}
}