Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
GH Action committed Jan 27, 2024
1 parent e56442c commit 989b773
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 91 deletions.
5 changes: 5 additions & 0 deletions src/sokol/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ pub const Desc = extern struct {
html5_preserve_drawing_buffer: bool = false,
html5_premultiplied_alpha: bool = false,
html5_ask_leave_site: bool = false,
html5_bubble_mouse_events: bool = false,
html5_bubble_touch_events: bool = false,
html5_bubble_wheel_events: bool = false,
html5_bubble_key_events: bool = false,
html5_bubble_char_events: bool = false,
ios_keyboard_resizes_canvas: bool = false,
};
pub const Html5FetchError = enum(i32) {
Expand Down
157 changes: 66 additions & 91 deletions src/sokol/c/sokol_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -941,13 +941,46 @@
sapp_show_keyboard(false);
Note that on the web platform, the keyboard can only be shown from
inside an input handler. On such platforms, sapp_show_keyboard()
will only work as expected when it is called from inside the
sokol-app event callback function. When called from other places,
an internal flag will be set, and the onscreen keyboard will be
called at the next 'legal' opportunity (when the next input event
is handled).
Note that onscreen keyboard functionality is no longer supported
on the browser platform (the previous hacks and workarounds to make browser
keyboards work for on web applications that don't use HTML UIs
never really worked across browsers).
INPUT EVENT BUBBLING ON THE WEB PLATFORM
========================================
By default, input event bubbling on the web platform is configured in
a way that makes the most sense for 'full-canvas' apps that cover the
entire browser client window area:
- mouse, touch and wheel events do not bubble up, this prevents various
ugly side events, like:
- HTML text overlays being selected on double- or triple-click into
the canvas
- 'scroll bumping' even when the canvas covers the entire client area
- key_up/down events for 'character keys' *do* bubble up (otherwise
the browser will not generate UNICODE character events)
- all other key events *do not* bubble up by default (this prevents side effects
like F1 opening help, or F7 starting 'caret browsing')
- character events do no bubble up (although I haven't noticed any side effects
otherwise)
Event bubbling can be enabled for input event categories during initialization
in the sapp_desc struct:
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc){
//...
.html5_bubble_mouse_events = true,
.html5_bubble_touch_events = true,
.html5_bubble_wheel_events = true,
.html5_bubble_key_events = true,
.html5_bubble_char_events = true,
};
}
This basically opens the floodgates lets *all* input events bubble up to the browser.
To prevent individual events from bubbling, call sapp_consume_event() from within
the sokol_app.h event callback.
OPTIONAL: DON'T HIJACK main() (#define SOKOL_NO_ENTRY)
======================================================
Expand Down Expand Up @@ -1101,10 +1134,7 @@
TEMP NOTE DUMP
==============
- onscreen keyboard support on Android requires Java :(, should we even bother?
- sapp_desc needs a bool whether to initialize depth-stencil surface
- GL context initialization needs more control (at least what GL version to initialize)
- application icon
- the Android implementation calls cleanup_cb() and destroys the egl context in onDestroy
at the latest but should do it earlier, in onStop, as an app is "killable" after onStop
on Android Honeycomb and later (it can't be done at the moment as the app may be started
Expand Down Expand Up @@ -1676,6 +1706,11 @@ typedef struct sapp_desc {
bool html5_preserve_drawing_buffer; // HTML5 only: whether to preserve default framebuffer content between frames
bool html5_premultiplied_alpha; // HTML5 only: whether the rendered pixels use premultiplied alpha convention
bool html5_ask_leave_site; // initial state of the internal html5_ask_leave_site flag (see sapp_html5_ask_leave_site())
bool html5_bubble_mouse_events; // if true, mouse events will bubble up to the web page
bool html5_bubble_touch_events; // same for touch events
bool html5_bubble_wheel_events; // same for wheel events
bool html5_bubble_key_events; // if true, bubble up *all* key events to browser, not just key events that represent characters
bool html5_bubble_char_events; // if true, bubble up character events to browser
bool ios_keyboard_resizes_canvas; // if true, showing the iOS keyboard shrinks the canvas
} sapp_desc;

Expand Down Expand Up @@ -5115,7 +5150,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_size_changed(int event_type, const EmscriptenU

_SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(user_data);
bool consume_event = false;
bool consume_event = !_sapp.desc.html5_bubble_mouse_events;
_sapp.emsc.mouse_buttons = emsc_event->buttons;
if (_sapp.mouse.locked) {
_sapp.mouse.dx = (float) emsc_event->movementX;
Expand Down Expand Up @@ -5176,7 +5211,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseE
} else {
_sapp.event.mouse_button = SAPP_MOUSEBUTTON_INVALID;
}
consume_event = _sapp_call_event(&_sapp.event);
consume_event |= _sapp_call_event(&_sapp.event);
}
// mouse lock can only be activated in mouse button events (not in move, enter or leave)
if (is_button_event) {
Expand All @@ -5189,6 +5224,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseE
_SOKOL_PRIVATE EM_BOOL _sapp_emsc_wheel_cb(int emsc_type, const EmscriptenWheelEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(emsc_type);
_SOKOL_UNUSED(user_data);
bool consume_event = !_sapp.desc.html5_bubble_wheel_events;
_sapp.emsc.mouse_buttons = emsc_event->mouse.buttons;
if (_sapp_events_enabled()) {
_sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL);
Expand All @@ -5203,12 +5239,10 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_wheel_cb(int emsc_type, const EmscriptenWheelE
}
_sapp.event.scroll_x = scale * (float)emsc_event->deltaX;
_sapp.event.scroll_y = scale * (float)emsc_event->deltaY;
_sapp_call_event(&_sapp.event);
consume_event |= _sapp_call_event(&_sapp.event);
}
_sapp_emsc_update_mouse_lock_state();
// NOTE: wheel events are always consumed because they try to scroll the
// page which looks pretty bad
return true;
return consume_event;
}

static struct {
Expand Down Expand Up @@ -5332,6 +5366,12 @@ _SOKOL_PRIVATE sapp_keycode _sapp_emsc_translate_key(const char* str) {
return SAPP_KEYCODE_INVALID;
}

// returns true if the key code is a 'character key', this is used to decide
// if a key event needs to bubble up to create a char event
_SOKOL_PRIVATE bool _sapp_emsc_is_char_key(sapp_keycode key_code) {
return key_code < SAPP_KEYCODE_WORLD_1;
}

_SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboardEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(user_data);
bool consume_event = false;
Expand Down Expand Up @@ -5359,6 +5399,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard
if (type == SAPP_EVENTTYPE_CHAR) {
// NOTE: charCode doesn't appear to be supported on Android Chrome
_sapp.event.char_code = emsc_event->charCode;
consume_event |= !_sapp.desc.html5_bubble_char_events;
} else {
if (0 != emsc_event->code[0]) {
// This code path is for desktop browsers which send untranslated 'physical' key code strings
Expand All @@ -5383,83 +5424,17 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard
{
send_keyup_followup = true;
}
// Only forward alpha-numeric keys to the browser (can further be suppressed by sapp_consume_event())
// NOTE: it should be possible to disable this behaviour via sapp_desc to give apps more
// controls over input event bubbling.
switch (_sapp.event.key_code) {
case SAPP_KEYCODE_WORLD_1:
case SAPP_KEYCODE_WORLD_2:
case SAPP_KEYCODE_ESCAPE:
case SAPP_KEYCODE_ENTER:
case SAPP_KEYCODE_TAB:
case SAPP_KEYCODE_BACKSPACE:
case SAPP_KEYCODE_INSERT:
case SAPP_KEYCODE_DELETE:
case SAPP_KEYCODE_RIGHT:
case SAPP_KEYCODE_LEFT:
case SAPP_KEYCODE_DOWN:
case SAPP_KEYCODE_UP:
case SAPP_KEYCODE_PAGE_UP:
case SAPP_KEYCODE_PAGE_DOWN:
case SAPP_KEYCODE_HOME:
case SAPP_KEYCODE_END:
case SAPP_KEYCODE_CAPS_LOCK:
case SAPP_KEYCODE_SCROLL_LOCK:
case SAPP_KEYCODE_NUM_LOCK:
case SAPP_KEYCODE_PRINT_SCREEN:
case SAPP_KEYCODE_PAUSE:
case SAPP_KEYCODE_F1:
case SAPP_KEYCODE_F2:
case SAPP_KEYCODE_F3:
case SAPP_KEYCODE_F4:
case SAPP_KEYCODE_F5:
case SAPP_KEYCODE_F6:
case SAPP_KEYCODE_F7:
case SAPP_KEYCODE_F8:
case SAPP_KEYCODE_F9:
case SAPP_KEYCODE_F10:
case SAPP_KEYCODE_F11:
case SAPP_KEYCODE_F12:
case SAPP_KEYCODE_F13:
case SAPP_KEYCODE_F14:
case SAPP_KEYCODE_F15:
case SAPP_KEYCODE_F16:
case SAPP_KEYCODE_F17:
case SAPP_KEYCODE_F18:
case SAPP_KEYCODE_F19:
case SAPP_KEYCODE_F20:
case SAPP_KEYCODE_F21:
case SAPP_KEYCODE_F22:
case SAPP_KEYCODE_F23:
case SAPP_KEYCODE_F24:
case SAPP_KEYCODE_F25:
case SAPP_KEYCODE_LEFT_SHIFT:
case SAPP_KEYCODE_LEFT_CONTROL:
case SAPP_KEYCODE_LEFT_ALT:
case SAPP_KEYCODE_LEFT_SUPER:
case SAPP_KEYCODE_RIGHT_SHIFT:
case SAPP_KEYCODE_RIGHT_CONTROL:
case SAPP_KEYCODE_RIGHT_ALT:
case SAPP_KEYCODE_RIGHT_SUPER:
case SAPP_KEYCODE_MENU:
// consume the event
consume_event = true;
break;
default:
// forward key to browser
consume_event = false;
break;

// 'character key events' will always need to bubble up, otherwise the browser
// wouldn't be able to generate character events.
if (!_sapp_emsc_is_char_key(_sapp.event.key_code)) {
consume_event |= !_sapp.desc.html5_bubble_key_events;
}
}
if (_sapp_call_event(&_sapp.event)) {
// event was consumed via sapp_consume_event()
consume_event = true;
}
consume_event |= _sapp_call_event(&_sapp.event);
if (send_keyup_followup) {
_sapp.event.type = SAPP_EVENTTYPE_KEY_UP;
if (_sapp_call_event(&_sapp.event)) {
consume_event = true;
}
consume_event |= _sapp_call_event(&_sapp.event);
}
}
}
Expand All @@ -5469,7 +5444,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard

_SOKOL_PRIVATE EM_BOOL _sapp_emsc_touch_cb(int emsc_type, const EmscriptenTouchEvent* emsc_event, void* user_data) {
_SOKOL_UNUSED(user_data);
bool consume_event = false;
bool consume_event = !_sapp.desc.html5_bubble_touch_events;
if (_sapp_events_enabled()) {
sapp_event_type type;
switch (emsc_type) {
Expand Down Expand Up @@ -5504,7 +5479,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_touch_cb(int emsc_type, const EmscriptenTouchE
dst->pos_y = src->targetY * _sapp.dpi_scale;
dst->changed = src->isChanged;
}
consume_event = _sapp_call_event(&_sapp.event);
consume_event |= _sapp_call_event(&_sapp.event);
}
}
return consume_event;
Expand Down

0 comments on commit 989b773

Please sign in to comment.