Be explicit about the event mouse and keyboard ID

This commit is contained in:
Sam Lantinga
2024-03-21 09:27:12 -07:00
parent 1c54455353
commit d1eb4adb16
53 changed files with 383 additions and 703 deletions

View File

@@ -46,6 +46,9 @@ extern "C" {
#include <vector>
#define BAPP_KEYBOARD_ID 1
#define BAPP_MOUSE_ID 1
/* Forward declarations */
class SDL_BLooper;
class SDL_BWin;
@@ -248,12 +251,12 @@ class SDL_BLooper : public BLooper
SDL_GetWindowPosition(win, &winPosX, &winPosY);
int dx = x - (winWidth / 2);
int dy = y - (winHeight / 2);
SDL_SendMouseMotion(0, win, 0, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
SDL_SendMouseMotion(0, win, BAPP_MOUSE_ID, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
if (!be_app->IsCursorHidden())
be_app->HideCursor();
} else {
SDL_SendMouseMotion(0, win, 0, 0, (float)x, (float)y);
SDL_SendMouseMotion(0, win, BAPP_MOUSE_ID, SDL_FALSE, (float)x, (float)y);
if (SDL_CursorVisible() && be_app->IsCursorHidden())
be_app->ShowCursor();
}
@@ -271,7 +274,7 @@ class SDL_BLooper : public BLooper
return;
}
win = GetSDLWindow(winID);
SDL_SendMouseButton(0, win, 0, state, button);
SDL_SendMouseButton(0, win, BAPP_MOUSE_ID, state, button);
}
void _HandleMouseWheel(BMessage *msg)
@@ -286,7 +289,7 @@ class SDL_BLooper : public BLooper
return;
}
win = GetSDLWindow(winID);
SDL_SendMouseWheel(0, win, 0, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(0, win, BAPP_MOUSE_ID, xTicks, -yTicks, SDL_MOUSEWHEEL_NORMAL);
}
void _HandleKey(BMessage *msg)
@@ -303,7 +306,7 @@ class SDL_BLooper : public BLooper
return;
}
HAIKU_SetKeyState(scancode, state);
SDL_SendKeyboardKey(0, 0, state, HAIKU_GetScancodeFromBeKey(scancode));
SDL_SendKeyboardKey(0, BAPP_KEYBOARD_ID, state, HAIKU_GetScancodeFromBeKey(scancode));
if (state == SDL_PRESSED && SDL_EventEnabled(SDL_EVENT_TEXT_INPUT)) {
const int8 *keyUtf8;

View File

@@ -605,11 +605,20 @@ static SDL_Scancode SDL_EVDEV_translate_keycode(int keycode)
return scancode;
}
static int SDL_EVDEV_init_keyboard(SDL_evdevlist_item *item, int udev_class)
{
SDL_AddKeyboard((SDL_KeyboardID)item->fd, SDL_TRUE);
return 0;
}
static int SDL_EVDEV_init_mouse(SDL_evdevlist_item *item, int udev_class)
{
int ret;
struct input_absinfo abs_info;
SDL_AddMouse((SDL_MouseID)item->fd, SDL_TRUE);
ret = ioctl(item->fd, EVIOCGABS(ABS_X), &abs_info);
if (ret < 0) {
// no absolute mode info, continue
@@ -922,6 +931,14 @@ static int SDL_EVDEV_device_added(const char *dev_path, int udev_class)
SDL_free(item);
return ret;
}
} else if (udev_class & SDL_UDEV_DEVICE_KEYBOARD) {
int ret = SDL_EVDEV_init_keyboard(item, udev_class);
if (ret < 0) {
close(item->fd);
SDL_free(item->path);
SDL_free(item);
return ret;
}
}
if (!_this->last) {

View File

@@ -33,6 +33,7 @@
#include <unistd.h>
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_keyboard_c.h"
#ifdef SDL_PLATFORM_NETBSD
#define KS_GROUP_Ascii KS_GROUP_Plain
@@ -388,6 +389,7 @@ static struct wscons_keycode_to_SDL
typedef struct
{
int fd;
SDL_KeyboardID keyboardID;
struct wskbd_map_data keymap;
int ledstate;
int origledstate;
@@ -420,14 +422,19 @@ static SDL_WSCONS_input_data *SDL_WSCONS_Init_Keyboard(const char *dev)
SDL_WSCONS_input_data *input = (SDL_WSCONS_input_data *)SDL_calloc(1, sizeof(SDL_WSCONS_input_data));
if (!input) {
return input;
return NULL;
}
input->fd = open(dev, O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (input->fd == -1) {
SDL_free(input);
input = NULL;
return NULL;
}
input->keyboardID = SDL_GetNextObjectID();
SDL_AddKeyboard(input->keyboardID, SDL_FALSE);
input->keymap.map = SDL_calloc(sizeof(struct wscons_keymap), KS_NUMKEYCODES);
if (!input->keymap.map) {
SDL_free(input);
@@ -553,22 +560,22 @@ static void Translate_to_keycode(SDL_WSCONS_input_data *input, int type, keysym_
switch (keyDesc.command) {
case KS_Cmd_ScrollBack:
{
SDL_SendKeyboardKey(0, 0, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_PAGEUP);
SDL_SendKeyboardKey(0, input->keyboardID, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_PAGEUP);
return;
}
case KS_Cmd_ScrollFwd:
{
SDL_SendKeyboardKey(0, 0, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_PAGEDOWN);
SDL_SendKeyboardKey(0, input->keyboardID, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_PAGEDOWN);
return;
}
}
for (i = 0; i < sizeof(conversion_table) / sizeof(struct wscons_keycode_to_SDL); i++) {
if (conversion_table[i].sourcekey == group[0]) {
SDL_SendKeyboardKey(0, 0, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, conversion_table[i].targetKey);
SDL_SendKeyboardKey(0, input->keyboardID, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, conversion_table[i].targetKey);
return;
}
}
SDL_SendKeyboardKey(0, 0, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_UNKNOWN);
SDL_SendKeyboardKey(0, input->keyboardID, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_UNKNOWN);
}
static void updateKeyboard(SDL_WSCONS_input_data *input)
@@ -802,13 +809,13 @@ static void updateKeyboard(SDL_WSCONS_input_data *input)
} break;
case WSCONS_EVENT_ALL_KEYS_UP:
for (i = 0; i < SDL_NUM_SCANCODES; i++) {
SDL_SendKeyboardKey(0, 0, SDL_RELEASED, i);
SDL_SendKeyboardKey(0, input->keyboardID, SDL_RELEASED, i);
}
break;
}
if (input->type == WSKBD_TYPE_USB && events[i].value <= 0xE7)
SDL_SendKeyboardKey(0, 0, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, (SDL_Scancode)events[i].value);
SDL_SendKeyboardKey(0, input->keyboardID, type == WSCONS_EVENT_KEY_DOWN ? SDL_PRESSED : SDL_RELEASED, (SDL_Scancode)events[i].value);
else
Translate_to_keycode(input, type, events[i].value);

View File

@@ -32,6 +32,7 @@
typedef struct SDL_WSCONS_mouse_input_data
{
int fd;
SDL_MouseID mouseID;
} SDL_WSCONS_mouse_input_data;
SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse()
@@ -39,32 +40,36 @@ SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse()
#ifdef WSMOUSEIO_SETVERSION
int version = WSMOUSE_EVENT_VERSION;
#endif
SDL_WSCONS_mouse_input_data *mouseInputData = SDL_calloc(1, sizeof(SDL_WSCONS_mouse_input_data));
SDL_WSCONS_mouse_input_data *input = SDL_calloc(1, sizeof(SDL_WSCONS_mouse_input_data));
if (!mouseInputData) {
if (!input) {
return NULL;
}
mouseInputData->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (mouseInputData->fd == -1) {
SDL_free(mouseInputData);
input->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (input->fd == -1) {
SDL_free(input);
return NULL;
}
input->mouseID = SDL_GetNextObjectID();
SDL_AddMouse(input->mouseID, SDL_FALSE);
#ifdef WSMOUSEIO_SETMODE
ioctl(mouseInputData->fd, WSMOUSEIO_SETMODE, WSMOUSE_COMPAT);
ioctl(input->fd, WSMOUSEIO_SETMODE, WSMOUSE_COMPAT);
#endif
#ifdef WSMOUSEIO_SETVERSION
ioctl(mouseInputData->fd, WSMOUSEIO_SETVERSION, &version);
ioctl(input->fd, WSMOUSEIO_SETVERSION, &version);
#endif
return mouseInputData;
return input;
}
void updateMouse(SDL_WSCONS_mouse_input_data *inputData)
void updateMouse(SDL_WSCONS_mouse_input_data *input)
{
struct wscons_event events[64];
int n;
SDL_Mouse *mouse = SDL_GetMouse();
if ((n = read(inputData->fd, events, sizeof(events))) > 0) {
if ((n = read(input->fd, events, sizeof(events))) > 0) {
int i;
n /= sizeof(struct wscons_event);
for (i = 0; i < n; i++) {
@@ -74,13 +79,13 @@ void updateMouse(SDL_WSCONS_mouse_input_data *inputData)
{
switch (events[i].value) {
case 0: /* Left Mouse Button. */
SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, SDL_PRESSED, SDL_BUTTON_LEFT);
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_PRESSED, SDL_BUTTON_LEFT);
break;
case 1: /* Middle Mouse Button. */
SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, SDL_PRESSED, SDL_BUTTON_MIDDLE);
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_PRESSED, SDL_BUTTON_MIDDLE);
break;
case 2: /* Right Mouse Button. */
SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, SDL_PRESSED, SDL_BUTTON_RIGHT);
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_PRESSED, SDL_BUTTON_RIGHT);
break;
}
} break;
@@ -88,34 +93,34 @@ void updateMouse(SDL_WSCONS_mouse_input_data *inputData)
{
switch (events[i].value) {
case 0: /* Left Mouse Button. */
SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, SDL_RELEASED, SDL_BUTTON_LEFT);
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_RELEASED, SDL_BUTTON_LEFT);
break;
case 1: /* Middle Mouse Button. */
SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, SDL_RELEASED, SDL_BUTTON_MIDDLE);
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_RELEASED, SDL_BUTTON_MIDDLE);
break;
case 2: /* Right Mouse Button. */
SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, SDL_RELEASED, SDL_BUTTON_RIGHT);
SDL_SendMouseButton(0, mouse->focus, input->mouseID, SDL_RELEASED, SDL_BUTTON_RIGHT);
break;
}
} break;
case WSCONS_EVENT_MOUSE_DELTA_X:
{
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, (float)events[i].value, 0.0f);
SDL_SendMouseMotion(0, mouse->focus, input->mouseID, 1, (float)events[i].value, 0.0f);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_Y:
{
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, 0.0f, -(float)events[i].value);
SDL_SendMouseMotion(0, mouse->focus, input->mouseID, 1, 0.0f, -(float)events[i].value);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_W:
{
SDL_SendMouseWheel(0, mouse->focus, mouse->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(0, mouse->focus, input->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_Z:
{
SDL_SendMouseWheel(0, mouse->focus, mouse->mouseID, 0, -events[i].value, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(0, mouse->focus, input->mouseID, 0, -events[i].value, SDL_MOUSEWHEEL_NORMAL);
break;
}
}
@@ -123,11 +128,11 @@ void updateMouse(SDL_WSCONS_mouse_input_data *inputData)
}
}
void SDL_WSCONS_Quit_Mouse(SDL_WSCONS_mouse_input_data *inputData)
void SDL_WSCONS_Quit_Mouse(SDL_WSCONS_mouse_input_data *input)
{
if (!inputData) {
if (!input) {
return;
}
close(inputData->fd);
SDL_free(inputData);
close(input->fd);
SDL_free(input);
}

View File

@@ -538,7 +538,7 @@ void SDL_WinRTApp::OnWindowActivated(CoreWindow ^ sender, WindowActivatedEventAr
*/
#if !SDL_WINAPI_FAMILY_PHONE || NTDDI_VERSION >= NTDDI_WINBLUE
Point cursorPos = WINRT_TransformCursorPosition(window, sender->PointerPosition, TransformToSDLWindowSize);
SDL_SendMouseMotion(0, window, 0, 0, cursorPos.X, cursorPos.Y);
SDL_SendMouseMotion(0, window, 0, SDL_FALSE, cursorPos.X, cursorPos.Y);
#endif
/* TODO, WinRT: see if the Win32 bugfix from https://hg.libsdl.org/SDL/rev/d278747da408 needs to be applied (on window activation) */