diff options
author | Campbell Barton <campbell@blender.org> | 2022-07-14 06:54:26 +0300 |
---|---|---|
committer | Campbell Barton <campbell@blender.org> | 2022-07-14 08:59:19 +0300 |
commit | d6fef73ef110eb43756b7b87c2cba80abae3b39f (patch) | |
tree | 5fdc7097a5fabdf5d52027340894433ddadb4ddb /source/blender/windowmanager | |
parent | 816a73891b69e2060c5b99d599e2a99e273db124 (diff) |
WM: Remove ASCII members from wmEvent & GHOST_TEventKeyData
The `ascii` member was only kept for historic reason as some platforms
didn't support utf8 when it was first introduced.
Remove the `ascii` struct members since many checks used this as a
fall-back for utf8_buf not being set which isn't needed.
There are a few cases where it's convenient to access the ASCII value
of an event (or nil) so a function has been added to do that.
*Details*
- WM_event_utf8_to_ascii() has been added for the few cases an events
ASCII value needs to be accessed, this just avoids having to do
multi-byte character checks in-line.
- RNA Event.ascii remains, using utf8_buf[0] for single byte characters.
- GHOST_TEventKeyData.ascii has been removed.
- To avoid regressions non-ASCII Latin1 characters from GHOST are
converted into multi-byte UTF8, when building X11 without
XInput & X_HAVE_UTF8_STRING it seems like could still occur.
Diffstat (limited to 'source/blender/windowmanager')
-rw-r--r-- | source/blender/windowmanager/WM_api.h | 1 | ||||
-rw-r--r-- | source/blender/windowmanager/WM_types.h | 8 | ||||
-rw-r--r-- | source/blender/windowmanager/intern/wm_event_query.c | 17 | ||||
-rw-r--r-- | source/blender/windowmanager/intern/wm_event_system.cc | 25 | ||||
-rw-r--r-- | source/blender/windowmanager/intern/wm_window.c | 1 |
5 files changed, 36 insertions, 16 deletions
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index d852b85a3d0..f337dd9d89a 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -1530,6 +1530,7 @@ bool WM_event_is_modal_drag_exit(const struct wmEvent *event, bool WM_event_is_mouse_drag(const struct wmEvent *event); bool WM_event_is_mouse_drag_or_press(const wmEvent *event); int WM_event_drag_direction(const wmEvent *event); +char WM_event_utf8_to_ascii(const struct wmEvent *event) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; /** * Detect motion between selection (callers should only use this for selection picking), diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h index 32bc2f96365..e7cbe936607 100644 --- a/source/blender/windowmanager/WM_types.h +++ b/source/blender/windowmanager/WM_types.h @@ -683,13 +683,11 @@ typedef struct wmEvent { /** Region relative mouse position (name convention before Blender 2.5). */ int mval[2]; /** - * From, ghost if utf8 is enabled for the platform, - * #BLI_str_utf8_size() must _always_ be valid, check - * when assigning s we don't need to check on every access after. + * A single UTF8 encoded character. + * #BLI_str_utf8_size() must _always_ return a valid value, + * check when assigning so we don't need to check on every access after. */ char utf8_buf[6]; - /** From ghost, fallback if utf8 isn't set. */ - char ascii; /** Modifier states: #KM_SHIFT, #KM_CTRL, #KM_ALT & #KM_OSKEY. */ uint8_t modifier; diff --git a/source/blender/windowmanager/intern/wm_event_query.c b/source/blender/windowmanager/intern/wm_event_query.c index 221073d288a..81044197ae7 100644 --- a/source/blender/windowmanager/intern/wm_event_query.c +++ b/source/blender/windowmanager/intern/wm_event_query.c @@ -112,7 +112,7 @@ void WM_event_print(const wmEvent *event) "wmEvent type:%d/%s, val:%d/%s, " "prev_type:%d/%s, prev_val:%d/%s, " "modifier=%s, keymodifier:%d, flag:%s, " - "mouse:(%d,%d), ascii:'%c', utf8:'%.*s', pointer:%p", + "mouse:(%d,%d), utf8:'%.*s', pointer:%p", event->type, type_id, event->val, @@ -126,7 +126,6 @@ void WM_event_print(const wmEvent *event) flag_id, event->xy[0], event->xy[1], - event->ascii, BLI_str_utf8_size(event->utf8_buf), event->utf8_buf, (const void *)event); @@ -401,6 +400,20 @@ void WM_event_drag_start_xy(const wmEvent *event, int r_xy[2]) /** \} */ /* -------------------------------------------------------------------- */ +/** \name Event Text Queries + * \{ */ + +char WM_event_utf8_to_ascii(const struct wmEvent *event) +{ + if (BLI_str_utf8_size(event->utf8_buf) == 1) { + return event->utf8_buf[0]; + } + return '\0'; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ /** \name Event Preference Mapping * \{ */ diff --git a/source/blender/windowmanager/intern/wm_event_system.cc b/source/blender/windowmanager/intern/wm_event_system.cc index 0395e8bda7a..a371fa7e185 100644 --- a/source/blender/windowmanager/intern/wm_event_system.cc +++ b/source/blender/windowmanager/intern/wm_event_system.cc @@ -2088,7 +2088,7 @@ static bool wm_eventmatch(const wmEvent *winevent, const wmKeyMapItem *kmi) if (winevent->val == KM_PRESS) { /* Prevent double clicks. */ /* Not using #ISTEXTINPUT anymore because (at least on Windows) some key codes above 255 * could have printable ascii keys, See T30479. */ - if (ISKEYBOARD(winevent->type) && (winevent->ascii || winevent->utf8_buf[0])) { + if (ISKEYBOARD(winevent->type) && winevent->utf8_buf[0]) { return true; } } @@ -5042,7 +5042,6 @@ static wmEvent *wm_event_add_mousemove_to_head(wmWindow *win) tevent = *event_last; tevent.flag = (eWM_EventFlag)0; - tevent.ascii = '\0'; tevent.utf8_buf[0] = '\0'; wm_event_custom_clear(&tevent); @@ -5329,7 +5328,6 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, void break; } - event.ascii = kd->ascii; /* Might be not nullptr terminated. */ memcpy(event.utf8_buf, kd->utf8_buf, sizeof(event.utf8_buf)); if (kd->is_repeat) { @@ -5341,8 +5339,6 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, void /* Exclude arrow keys, escape, etc from text input. */ if (type == GHOST_kEventKeyUp) { - event.ascii = '\0'; - /* Ghost should do this already for key up. */ if (event.utf8_buf[0]) { CLOG_ERROR(WM_LOG_EVENTS, @@ -5351,15 +5347,28 @@ void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, void event.utf8_buf[0] = '\0'; } else { - if (event.ascii < 32 && event.ascii > 0) { - event.ascii = '\0'; - } if (event.utf8_buf[0] < 32 && event.utf8_buf[0] > 0) { event.utf8_buf[0] = '\0'; } } if (event.utf8_buf[0]) { + /* NOTE(@campbellbarton): Detect non-ASCII characters stored in `utf8_buf`, + * ideally this would never happen but it can't be ruled out for X11 which has + * special handling of Latin1 when building without UTF8 support. + * Avoid regressions by adding this conversions, it should eventually be removed. */ + if ((event.utf8_buf[0] >= 0x80) && (event.utf8_buf[1] == '\0')) { + const uint c = (uint)event.utf8_buf[0]; + int utf8_buf_len = BLI_str_utf8_from_unicode(c, event.utf8_buf, sizeof(event.utf8_buf)); + CLOG_ERROR(WM_LOG_EVENTS, + "ghost detected non-ASCII single byte character '%u', converting to utf8 " + "('%.*s', length=%d)", + c, + utf8_buf_len, + event.utf8_buf, + utf8_buf_len); + } + if (BLI_str_utf8_size(event.utf8_buf) == -1) { CLOG_ERROR(WM_LOG_EVENTS, "ghost detected an invalid unicode character '%d'", diff --git a/source/blender/windowmanager/intern/wm_window.c b/source/blender/windowmanager/intern/wm_window.c index 36f91f8414a..70640eda605 100644 --- a/source/blender/windowmanager/intern/wm_window.c +++ b/source/blender/windowmanager/intern/wm_window.c @@ -1148,7 +1148,6 @@ static bool ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_pt */ GHOST_TEventKeyData kdata = { .key = GHOST_kKeyUnknown, - .ascii = '\0', .utf8_buf = {'\0'}, .is_repeat = false, }; |