Skip to content

Commit

Permalink
Enable SDL text events at window creation time. (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR authored Nov 4, 2024
1 parent 1fc116b commit f731701
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ DEVELOPERS
in places where "fix_viewport_ratio" previously was and also
to convert mouse coordinates. Most renderers should use one of
the two prefab implementations of this function.
+ SDL text event enablement now happens after the window is
created. This shouldn't really affect anything.
+ Added detection for LoongArch64. 64-bit architectures that
platform_endian.h fails to identify as such should no longer
abort in the software layer renderer.
Expand Down
1 change: 1 addition & 0 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void __wait_event(void);
void __warp_mouse(int x, int y);

// "Driver" functions currently only supported by SDL.
void sdl_init_window_text_events(unsigned sdl_window_id);
void gamepad_map_sym(const char *sym, const char *value);
void gamepad_add_mapping(const char *mapping);

Expand Down
62 changes: 46 additions & 16 deletions src/event_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@

extern struct input_status input;

/* Enable converting keycodes to fake unicode presses when text input isn't
* active. Enabling text input also enables an onscreen keyboard in some
* ports, so it isn't always desired.
*
* SDL 1.2 builds will disable this automatically if unicode is detected.
*/
static boolean unicode_fallback;

static boolean numlock_status_initialized;
static int joystick_instance_ids[MAX_JOYSTICKS];
static SDL_Joystick *joysticks[MAX_JOYSTICKS];
Expand Down Expand Up @@ -968,16 +976,6 @@ static boolean process_event(SDL_Event *event)
struct buffered_status *status = store_status();
enum keycode ckey;

#if SDL_VERSION_ATLEAST(2,0,0)
/* Enable converting keycodes to fake unicode presses when text input isn't
* active. Enabling text input also enables an onscreen keyboard in some
* ports, so it isn't always desired. */
boolean unicode_fallback = !SDL_IsTextInputActive();
#else
/* SDL 1.2 might also need this (Pandora? doesn't generate unicode presses). */
static boolean unicode_fallback = true;
#endif

/* SDL's numlock keyboard modifier handling seems to be broken on X11,
* and it will only get numlock's status right on application init. We
* can trust this value once, and then toggle based on user presses of
Expand Down Expand Up @@ -1366,13 +1364,9 @@ static boolean process_event(SDL_Event *event)

trace("--EVENT_SDL-- SDL_TEXTINPUT: %s\n", text);

// This should never happen; ignore.
if(unicode_fallback)
{
// Clear any unicode keys on the buffer generated from the fallback...
status->unicode_length = 0;
status->unicode_repeat = 0;
unicode_fallback = false;
}
break;

// Decode the input UTF-8 string into UTF-32 for the event buffer.
while(*text)
Expand Down Expand Up @@ -1664,6 +1658,42 @@ void __warp_mouse(int x, int y)
SDL_WarpMouseInWindow(window, x, y);
}

/**
* Enable text events for the provided window.
* Prior to SDL3, this was global, so the window ID isn't used.
*/
void sdl_init_window_text_events(unsigned sdl_window_id)
{
#if SDL_VERSION_ATLEAST(2,0,0)
/* Most platforms want text input events always on so they can generate
* convenient unicode text values, but in Android this causes some problems:
*
* - On older versions the navigation bar will ALWAYS display, regardless
* of whether or not there's an attached keyboard.
* - Holding the space key no longer works, breaking built-in shooting (as
* recent as Android 10).
* - The onscreen keyboard Android pops up can be moved but not collapsed.
*
* TODO: Instead, enable text input on demand at text prompts.
*/
if(!SDL_HasScreenKeyboardSupport())
{
SDL_StartTextInput();
unicode_fallback = false;
}
else
{
SDL_StopTextInput();
unicode_fallback = true;
}
#else
SDL_EnableUNICODE(1);
/* SDL 1.2 might also need this (Pandora? doesn't generate unicode presses).
* If it isn't required, real unicode events will turn this off. */
unicode_fallback = true;
#endif
}

void platform_init_event(void)
{
#if !SDL_VERSION_ATLEAST(2,0,0) || defined(CONFIG_SWITCH) || defined(CONFIG_PSVITA) \
Expand Down
19 changes: 0 additions & 19 deletions src/platform_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,6 @@ boolean platform_init(void)
return false;
}
}

#if SDL_VERSION_ATLEAST(2,0,0)
/* Most platforms want text input events always on so they can generate
* convenient unicode text values, but in Android this causes some problems:
*
* - On older versions the navigation bar will ALWAYS display, regardless
* of whether or not there's an attached keyboard.
* - Holding the space key no longer works, breaking built-in shooting (as
* recent as Android 10).
* - The onscreen keyboard Android pops up can be moved but not collapsed.
*
* TODO: Instead, enable text input on demand at text prompts.
* TODO: this probably redundant with behavior already in SDL.
*/
if(!SDL_HasScreenKeyboardSupport())
SDL_StartTextInput();
#else
SDL_EnableUNICODE(1);
#endif
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/render_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "SDLmzx.h"
#include "event.h"
#include "render.h"
#include "render_sdl.h"
#include "util.h"
Expand Down Expand Up @@ -679,6 +680,7 @@ boolean sdl_create_window_soft(struct graphics_data *graphics,

sdl_set_system_cursor(graphics);
sdl_set_window_grab(render_data, window->grab_mouse);
sdl_init_window_text_events(window->platform_id);
return true;

#if SDL_VERSION_ATLEAST(2,0,0)
Expand Down Expand Up @@ -991,6 +993,7 @@ boolean sdl_create_window_renderer(struct graphics_data *graphics,
sdl_set_screensaver_enabled(graphics->disable_screensaver == SCREENSAVER_ENABLE);
sdl_set_system_cursor(graphics);
sdl_set_window_grab(render_data, window->grab_mouse);
sdl_init_window_text_events(window->platform_id);
return true;

err_free:
Expand Down Expand Up @@ -1119,6 +1122,7 @@ boolean gl_create_window(struct graphics_data *graphics,

sdl_set_system_cursor(graphics);
sdl_set_window_grab(render_data, window->grab_mouse);
sdl_init_window_text_events(window->platform_id);
return true;

#if SDL_VERSION_ATLEAST(2,0,0)
Expand Down

0 comments on commit f731701

Please sign in to comment.