From bf121747de93f7a7d04f04ffde5e35c8725451e5 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Mon, 10 Jan 2022 02:32:01 +0000 Subject: [PATCH] Don't hard code key codes on Haiku * src/haiku_support.cc (keysym_from_raw_char): New function. (DispatchMessage): Map raw character to keysym if applicable. (be_map_key): Delete function. * src/haiku_support.h (struct haiku_key_event): Update fields to hold keysym and character instead. * src/haikuterm.c (haiku_read_socket): Don't call be_map_key, and use keysym field if provided instead. --- src/haiku_support.cc | 167 +++++++++++++++++++++---------------------- src/haiku_support.h | 8 +-- src/haikuterm.c | 7 +- 3 files changed, 84 insertions(+), 98 deletions(-) diff --git a/src/haiku_support.cc b/src/haiku_support.cc index c4ff52a5305..459f8c2be40 100644 --- a/src/haiku_support.cc +++ b/src/haiku_support.cc @@ -136,6 +136,65 @@ gui_abort (const char *msg) emacs_abort (); } +static int +keysym_from_raw_char (int32 raw, int32 key, unsigned *code) +{ + switch (raw) + { + case B_BACKSPACE: + *code = XK_BackSpace; + break; + case B_RETURN: + *code = XK_Return; + break; + case B_TAB: + *code = XK_Tab; + break; + case B_ESCAPE: + *code = XK_Escape; + break; + case B_LEFT_ARROW: + *code = XK_Left; + break; + case B_RIGHT_ARROW: + *code = XK_Right; + break; + case B_UP_ARROW: + *code = XK_Up; + break; + case B_DOWN_ARROW: + *code = XK_Down; + break; + case B_INSERT: + *code = XK_Insert; + break; + case B_DELETE: + *code = XK_Delete; + break; + case B_HOME: + *code = XK_Home; + break; + case B_END: + *code = XK_End; + break; + case B_PAGE_UP: + *code = XK_Page_Up; + break; + case B_PAGE_DOWN: + *code = XK_Page_Down; + break; + + case B_FUNCTION_KEY: + *code = XK_F1 + key - 2; + break; + + default: + return 0; + } + + return 1; +} + static void map_key (char *chars, int32 offset, uint32_t *c) { @@ -616,7 +675,9 @@ public: rq.window = this; - int32_t code = msg->GetInt32 ("raw_char", 0); + int32 raw, key; + msg->FindInt32 ("raw_char", &raw); + msg->FindInt32 ("key", &key); rq.modifiers = 0; uint32_t mods = modifiers (); @@ -633,24 +694,27 @@ public: if (mods & B_OPTION_KEY) rq.modifiers |= HAIKU_MODIFIER_SUPER; - rq.mb_char = code; - rq.kc = msg->GetInt32 ("key", -1); - rq.unraw_mb_char = - BUnicodeChar::FromUTF8 (msg->GetString ("bytes")); + if (!keysym_from_raw_char (raw, key, &rq.keysym)) + rq.keysym = 0; - if ((mods & B_SHIFT_KEY) && rq.kc >= 0) - { - if (mods & B_CAPS_LOCK) - map_caps_shift (rq.kc, &rq.unraw_mb_char); - else - map_shift (rq.kc, &rq.unraw_mb_char); - } - else if (rq.kc >= 0) + rq.multibyte_char = 0; + + if (!rq.keysym) { - if (mods & B_CAPS_LOCK) - map_caps (rq.kc, &rq.unraw_mb_char); + if (mods & B_SHIFT_KEY) + { + if (mods & B_CAPS_LOCK) + map_caps_shift (key, &rq.multibyte_char); + else + map_shift (key, &rq.multibyte_char); + } else - map_normal (rq.kc, &rq.unraw_mb_char); + { + if (mods & B_CAPS_LOCK) + map_caps (key, &rq.multibyte_char); + else + map_normal (key, &rq.multibyte_char); + } } haiku_write (msg->what == B_KEY_DOWN ? KEY_DOWN : KEY_UP, &rq); @@ -1846,77 +1910,6 @@ BWindow_Flush (void *window) ((BWindow *) window)->Flush (); } -/* Map the keycode KC, storing the result in CODE and 1 in - NON_ASCII_P if it should be used. */ -void -be_map_key (uint32_t kc, int *non_ascii_p, unsigned *code) -{ - if (*code == 10 && kc != 0x42) - { - *code = XK_Return; - *non_ascii_p = 1; - return; - } - - switch (kc) - { - default: - *non_ascii_p = 0; - if (kc < 0xe && kc > 0x1) - { - *code = XK_F1 + kc - 2; - *non_ascii_p = 1; - } - return; - case 0x1e: - *code = XK_BackSpace; - break; - case 0x61: - *code = XK_Left; - break; - case 0x63: - *code = XK_Right; - break; - case 0x57: - *code = XK_Up; - break; - case 0x62: - *code = XK_Down; - break; - case 0x64: - case 0x1f: - *code = XK_Insert; - break; - case 0x65: - case 0x34: - *code = XK_Delete; - break; - case 0x37: - case 0x20: - *code = XK_Home; - break; - case 0x58: - case 0x35: - *code = XK_End; - break; - case 0x39: - case 0x21: - *code = XK_Page_Up; - break; - case 0x5a: - case 0x36: - *code = XK_Page_Down; - break; - case 0x1: - *code = XK_Escape; - break; - case 0x68: - *code = XK_Menu; - break; - } - *non_ascii_p = 1; -} - /* Make a scrollbar, attach it to VIEW's window, and return it. */ void * BScrollBar_make_for_view (void *view, int horizontal_p, diff --git a/src/haiku_support.h b/src/haiku_support.h index 114e8429ab5..d39e30735d3 100644 --- a/src/haiku_support.h +++ b/src/haiku_support.h @@ -130,9 +130,8 @@ struct haiku_key_event { void *window; int modifiers; - uint32_t mb_char; - uint32_t unraw_mb_char; - short kc; + unsigned keysym; + uint32_t multibyte_char; }; struct haiku_activation_event @@ -560,9 +559,6 @@ extern "C" extern void BWindow_Flush (void *window); - extern void - be_map_key (uint32_t kc, int *non_ascii_p, unsigned *code); - extern void * BScrollBar_make_for_view (void *view, int horizontal_p, int x, int y, int x1, int y1, diff --git a/src/haikuterm.c b/src/haikuterm.c index e5bf917fb9a..b2337fe1047 100644 --- a/src/haikuterm.c +++ b/src/haikuterm.c @@ -2670,7 +2670,6 @@ haiku_read_socket (struct terminal *terminal, struct input_event *hold_quit) struct haiku_key_event *b = buf; Mouse_HLInfo *hlinfo = &x_display_list->mouse_highlight; struct frame *f = haiku_window_to_frame (b->window); - int non_ascii_p; if (!f) continue; @@ -2686,11 +2685,9 @@ haiku_read_socket (struct terminal *terminal, struct input_event *hold_quit) need_flush = 1; } - inev.code = b->unraw_mb_char; + inev.code = b->keysym ? b->keysym : b->multibyte_char; - be_map_key (b->kc, &non_ascii_p, &inev.code); - - if (non_ascii_p) + if (b->keysym) inev.kind = NON_ASCII_KEYSTROKE_EVENT; else inev.kind = inev.code > 127 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : -- 2.39.5