Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/core/linux/SDL_evdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,14 @@ static bool SDL_EVDEV_init_keyboard(SDL_evdevlist_item *item, int udev_class)
name[0] = '\0';
ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);

SDL_AddKeyboard((SDL_KeyboardID)item->fd, name, true);
SDL_AddKeyboard((SDL_KeyboardID)item->fd, name);

return true;
}

static void SDL_EVDEV_destroy_keyboard(SDL_evdevlist_item *item)
{
SDL_RemoveKeyboard((SDL_KeyboardID)item->fd, true);
SDL_RemoveKeyboard((SDL_KeyboardID)item->fd);
}

static bool SDL_EVDEV_init_mouse(SDL_evdevlist_item *item, int udev_class)
Expand All @@ -631,7 +631,7 @@ static bool SDL_EVDEV_init_mouse(SDL_evdevlist_item *item, int udev_class)
name[0] = '\0';
ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);

SDL_AddMouse((SDL_MouseID)item->fd, name, true);
SDL_AddMouse((SDL_MouseID)item->fd, name);

ret = ioctl(item->fd, EVIOCGABS(ABS_X), &abs_info);
if (ret < 0) {
Expand All @@ -656,7 +656,7 @@ static bool SDL_EVDEV_init_mouse(SDL_evdevlist_item *item, int udev_class)

static void SDL_EVDEV_destroy_mouse(SDL_evdevlist_item *item)
{
SDL_RemoveMouse((SDL_MouseID)item->fd, true);
SDL_RemoveMouse((SDL_MouseID)item->fd);
}

static bool SDL_EVDEV_init_touchscreen(SDL_evdevlist_item *item, int udev_class)
Expand Down
2 changes: 1 addition & 1 deletion src/core/openbsd/SDL_wscons_kbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ static SDL_WSCONS_input_data *SDL_WSCONS_Init_Keyboard(const char *dev)
}

input->keyboardID = SDL_GetNextObjectID();
SDL_AddKeyboard(input->keyboardID, NULL, false);
SDL_AddKeyboard(input->keyboardID, NULL);

input->keymap.map = SDL_calloc(KS_NUMKEYCODES, sizeof(struct wscons_keymap));
if (!input->keymap.map) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/openbsd/SDL_wscons_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse(void)
}

input->mouseID = SDL_GetNextObjectID();
SDL_AddMouse(input->mouseID, NULL, false);
SDL_AddMouse(input->mouseID, NULL);

#ifdef WSMOUSEIO_SETMODE
ioctl(input->fd, WSMOUSEIO_SETMODE, WSMOUSE_COMPAT);
Expand Down
66 changes: 38 additions & 28 deletions src/events/SDL_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@
#define KEYCODE_OPTION_LATIN_LETTERS 0x04
#define DEFAULT_KEYCODE_OPTIONS (KEYCODE_OPTION_FRENCH_NUMBERS | KEYCODE_OPTION_LATIN_LETTERS)

typedef struct SDL_KeyboardInstance
{
SDL_KeyboardID instance_id;
char *name;
} SDL_KeyboardInstance;

typedef struct SDL_Keyboard
{
// Data common to all keyboards
Expand All @@ -65,7 +59,9 @@ typedef struct SDL_Keyboard

static SDL_Keyboard SDL_keyboard;
static int SDL_keyboard_count;
static SDL_KeyboardInstance *SDL_keyboards;
static SDL_KeyboardID *SDL_keyboards;
static SDL_HashTable *SDL_keyboard_names;
static bool SDL_keyboard_quitting;

static void SDLCALL SDL_KeycodeOptionsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
Expand Down Expand Up @@ -94,6 +90,9 @@ bool SDL_InitKeyboard(void)
{
SDL_AddHintCallback(SDL_HINT_KEYCODE_OPTIONS,
SDL_KeycodeOptionsChanged, &SDL_keyboard);

SDL_keyboard_names = SDL_CreateHashTable(0, true, SDL_HashID, SDL_KeyMatchID, SDL_DestroyHashValue, NULL);

return true;
}

Expand All @@ -111,14 +110,14 @@ bool SDL_IsKeyboard(Uint16 vendor, Uint16 product, int num_keys)
static int SDL_GetKeyboardIndex(SDL_KeyboardID keyboardID)
{
for (int i = 0; i < SDL_keyboard_count; ++i) {
if (keyboardID == SDL_keyboards[i].instance_id) {
if (keyboardID == SDL_keyboards[i]) {
return i;
}
}
return -1;
}

void SDL_AddKeyboard(SDL_KeyboardID keyboardID, const char *name, bool send_event)
void SDL_AddKeyboard(SDL_KeyboardID keyboardID, const char *name)
{
int keyboard_index = SDL_GetKeyboardIndex(keyboardID);
if (keyboard_index >= 0) {
Expand All @@ -128,41 +127,40 @@ void SDL_AddKeyboard(SDL_KeyboardID keyboardID, const char *name, bool send_even

SDL_assert(keyboardID != 0);

SDL_KeyboardInstance *keyboards = (SDL_KeyboardInstance *)SDL_realloc(SDL_keyboards, (SDL_keyboard_count + 1) * sizeof(*keyboards));
SDL_KeyboardID *keyboards = (SDL_KeyboardID *)SDL_realloc(SDL_keyboards, (SDL_keyboard_count + 1) * sizeof(*keyboards));
if (!keyboards) {
return;
}
SDL_KeyboardInstance *instance = &keyboards[SDL_keyboard_count];
instance->instance_id = keyboardID;
instance->name = SDL_strdup(name ? name : "");
keyboards[SDL_keyboard_count] = keyboardID;
SDL_keyboards = keyboards;
++SDL_keyboard_count;

if (send_event) {
SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_KEYBOARD_ADDED;
event.kdevice.which = keyboardID;
SDL_PushEvent(&event);
if (!name) {
name = "Keyboard";
}
SDL_InsertIntoHashTable(SDL_keyboard_names, (const void *)(uintptr_t)keyboardID, SDL_strdup(name), true);

SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_KEYBOARD_ADDED;
event.kdevice.which = keyboardID;
SDL_PushEvent(&event);
}

void SDL_RemoveKeyboard(SDL_KeyboardID keyboardID, bool send_event)
void SDL_RemoveKeyboard(SDL_KeyboardID keyboardID)
{
int keyboard_index = SDL_GetKeyboardIndex(keyboardID);
if (keyboard_index < 0) {
// We don't know about this keyboard
return;
}

SDL_free(SDL_keyboards[keyboard_index].name);

if (keyboard_index != SDL_keyboard_count - 1) {
SDL_memmove(&SDL_keyboards[keyboard_index], &SDL_keyboards[keyboard_index + 1], (SDL_keyboard_count - keyboard_index - 1) * sizeof(SDL_keyboards[keyboard_index]));
}
--SDL_keyboard_count;

if (send_event) {
if (!SDL_keyboard_quitting) {
SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_KEYBOARD_REMOVED;
Expand All @@ -188,7 +186,7 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count)
}

for (i = 0; i < SDL_keyboard_count; ++i) {
keyboards[i] = SDL_keyboards[i].instance_id;
keyboards[i] = SDL_keyboards[i];
}
keyboards[i] = 0;
} else {
Expand All @@ -202,12 +200,17 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count)

const char *SDL_GetKeyboardNameForID(SDL_KeyboardID instance_id)
{
int keyboard_index = SDL_GetKeyboardIndex(instance_id);
if (keyboard_index < 0) {
const char *name = NULL;
if (!SDL_FindInHashTable(SDL_keyboard_names, (const void *)(uintptr_t)instance_id, (const void **)&name)) {
SDL_SetError("Keyboard %" SDL_PRIu32 " not found", instance_id);
return NULL;
}
return SDL_GetPersistentString(SDL_keyboards[keyboard_index].name);
if (!name) {
// SDL_strdup() failed during insert
SDL_OutOfMemory();
return NULL;
}
return name;
}

void SDL_ResetKeyboard(void)
Expand Down Expand Up @@ -869,19 +872,26 @@ void SDL_SendEditingTextCandidates(char **candidates, int num_candidates, int se

void SDL_QuitKeyboard(void)
{
SDL_keyboard_quitting = true;

for (int i = SDL_keyboard_count; i--;) {
SDL_RemoveKeyboard(SDL_keyboards[i].instance_id, false);
SDL_RemoveKeyboard(SDL_keyboards[i]);
}
SDL_free(SDL_keyboards);
SDL_keyboards = NULL;

SDL_DestroyHashTable(SDL_keyboard_names);
SDL_keyboard_names = NULL;

if (SDL_keyboard.keymap && SDL_keyboard.keymap->auto_release) {
SDL_DestroyKeymap(SDL_keyboard.keymap);
SDL_keyboard.keymap = NULL;
}

SDL_RemoveHintCallback(SDL_HINT_KEYCODE_OPTIONS,
SDL_KeycodeOptionsChanged, &SDL_keyboard);

SDL_keyboard_quitting = false;
}

const bool *SDL_GetKeyboardState(int *numkeys)
Expand Down
4 changes: 2 additions & 2 deletions src/events/SDL_keyboard_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ extern bool SDL_InitKeyboard(void);
extern bool SDL_IsKeyboard(Uint16 vendor, Uint16 product, int num_keys);

// A keyboard has been added to the system
extern void SDL_AddKeyboard(SDL_KeyboardID keyboardID, const char *name, bool send_event);
extern void SDL_AddKeyboard(SDL_KeyboardID keyboardID, const char *name);

// A keyboard has been removed from the system
extern void SDL_RemoveKeyboard(SDL_KeyboardID keyboardID, bool send_event);
extern void SDL_RemoveKeyboard(SDL_KeyboardID keyboardID);

// Set the mapping of scancode to key codes
extern void SDL_SetKeymap(SDL_Keymap *keymap, bool send_event);
Expand Down
65 changes: 37 additions & 28 deletions src/events/SDL_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@

#define WARP_EMULATION_THRESHOLD_NS SDL_MS_TO_NS(30)

typedef struct SDL_MouseInstance
{
SDL_MouseID instance_id;
char *name;
} SDL_MouseInstance;

// The mouse state
static SDL_Mouse SDL_mouse;
static int SDL_mouse_count;
static SDL_MouseInstance *SDL_mice;
static SDL_MouseID *SDL_mice;
static SDL_HashTable *SDL_mouse_names;
static bool SDL_mouse_quitting;

// for mapping mouse events to touch
static bool track_mouse_down = false;
Expand Down Expand Up @@ -310,6 +306,8 @@ bool SDL_PreInitMouse(void)

mouse->cursor_visible = true;

SDL_mouse_names = SDL_CreateHashTable(0, true, SDL_HashID, SDL_KeyMatchID, SDL_DestroyHashValue, NULL);

return true;
}

Expand Down Expand Up @@ -339,14 +337,14 @@ bool SDL_IsMouse(Uint16 vendor, Uint16 product)
static int SDL_GetMouseIndex(SDL_MouseID mouseID)
{
for (int i = 0; i < SDL_mouse_count; ++i) {
if (mouseID == SDL_mice[i].instance_id) {
if (mouseID == SDL_mice[i]) {
return i;
}
}
return -1;
}

void SDL_AddMouse(SDL_MouseID mouseID, const char *name, bool send_event)
void SDL_AddMouse(SDL_MouseID mouseID, const char *name)
{
int mouse_index = SDL_GetMouseIndex(mouseID);
if (mouse_index >= 0) {
Expand All @@ -356,35 +354,34 @@ void SDL_AddMouse(SDL_MouseID mouseID, const char *name, bool send_event)

SDL_assert(mouseID != 0);

SDL_MouseInstance *mice = (SDL_MouseInstance *)SDL_realloc(SDL_mice, (SDL_mouse_count + 1) * sizeof(*mice));
SDL_MouseID *mice = (SDL_MouseID *)SDL_realloc(SDL_mice, (SDL_mouse_count + 1) * sizeof(*mice));
if (!mice) {
return;
}
SDL_MouseInstance *instance = &mice[SDL_mouse_count];
instance->instance_id = mouseID;
instance->name = SDL_strdup(name ? name : "");
mice[SDL_mouse_count] = mouseID;
SDL_mice = mice;
++SDL_mouse_count;

if (send_event) {
SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_MOUSE_ADDED;
event.mdevice.which = mouseID;
SDL_PushEvent(&event);
if (!name) {
name = "Mouse";
}
SDL_InsertIntoHashTable(SDL_mouse_names, (const void *)(uintptr_t)mouseID, SDL_strdup(name), true);

SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_MOUSE_ADDED;
event.mdevice.which = mouseID;
SDL_PushEvent(&event);
}

void SDL_RemoveMouse(SDL_MouseID mouseID, bool send_event)
void SDL_RemoveMouse(SDL_MouseID mouseID)
{
int mouse_index = SDL_GetMouseIndex(mouseID);
if (mouse_index < 0) {
// We don't know about this mouse
return;
}

SDL_free(SDL_mice[mouse_index].name);

if (mouse_index != SDL_mouse_count - 1) {
SDL_memmove(&SDL_mice[mouse_index], &SDL_mice[mouse_index + 1], (SDL_mouse_count - mouse_index - 1) * sizeof(SDL_mice[mouse_index]));
}
Expand All @@ -404,7 +401,7 @@ void SDL_RemoveMouse(SDL_MouseID mouseID, bool send_event)
}
}

if (send_event) {
if (!SDL_mouse_quitting) {
SDL_Event event;
SDL_zero(event);
event.type = SDL_EVENT_MOUSE_REMOVED;
Expand All @@ -430,7 +427,7 @@ SDL_MouseID *SDL_GetMice(int *count)
}

for (i = 0; i < SDL_mouse_count; ++i) {
mice[i] = SDL_mice[i].instance_id;
mice[i] = SDL_mice[i];
}
mice[i] = 0;
} else {
Expand All @@ -444,12 +441,17 @@ SDL_MouseID *SDL_GetMice(int *count)

const char *SDL_GetMouseNameForID(SDL_MouseID instance_id)
{
int mouse_index = SDL_GetMouseIndex(instance_id);
if (mouse_index < 0) {
const char *name = NULL;
if (!SDL_FindInHashTable(SDL_mouse_names, (const void *)(uintptr_t)instance_id, (const void **)&name)) {
SDL_SetError("Mouse %" SDL_PRIu32 " not found", instance_id);
return NULL;
}
return SDL_GetPersistentString(SDL_mice[mouse_index].name);
if (!name) {
// SDL_strdup() failed during insert
SDL_OutOfMemory();
return NULL;
}
return name;
}

void SDL_SetDefaultCursor(SDL_Cursor *cursor)
Expand Down Expand Up @@ -1078,6 +1080,8 @@ void SDL_QuitMouse(void)
SDL_Cursor *cursor, *next;
SDL_Mouse *mouse = SDL_GetMouse();

SDL_mouse_quitting = true;

if (mouse->added_mouse_touch_device) {
SDL_DelTouch(SDL_MOUSE_TOUCHID);
mouse->added_mouse_touch_device = false;
Expand Down Expand Up @@ -1164,16 +1168,21 @@ void SDL_QuitMouse(void)
SDL_MouseIntegerModeChanged, mouse);

for (int i = SDL_mouse_count; i--; ) {
SDL_RemoveMouse(SDL_mice[i].instance_id, false);
SDL_RemoveMouse(SDL_mice[i]);
}
SDL_free(SDL_mice);
SDL_mice = NULL;

SDL_DestroyHashTable(SDL_mouse_names);
SDL_mouse_names = NULL;

if (mouse->internal) {
SDL_free(mouse->internal);
mouse->internal = NULL;
}
SDL_zerop(mouse);

SDL_mouse_quitting = false;
}

bool SDL_SetRelativeMouseTransform(SDL_MouseMotionTransformCallback transform, void *userdata)
Expand Down
Loading
Loading