Fixed bug 3461 - Implement TEXTINPUT events for Haiku
authorSam Lantinga <slouken@libsdl.org>
Wed, 19 Oct 2016 20:42:22 -0700
changeset 10555476a717c10cf
parent 10554 6c07c69fb842
child 10556 007dfe83abf8
Fixed bug 3461 - Implement TEXTINPUT events for Haiku

Kai Sterker

Apparently, SDL2 on Haiku does not generate SDL_TEXTINPUT events.
Attached is a patch that adds this functionality.

Tested with SDLs own checkkeys program and different keymaps as well as my own SDL application and German keyboard layout to verify it generates the expected input.
src/main/haiku/SDL_BApp.h
src/video/haiku/SDL_BWin.h
     1.1 --- a/src/main/haiku/SDL_BApp.h	Wed Oct 19 20:39:12 2016 -0700
     1.2 +++ b/src/main/haiku/SDL_BApp.h	Wed Oct 19 20:42:22 2016 -0700
     1.3 @@ -193,7 +193,8 @@
     1.4          if(_current_context)
     1.5              _current_context->UnlockGL();
     1.6          _current_context = newContext;
     1.7 -        _current_context->LockGL();
     1.8 +        if (_current_context)
     1.9 +	        _current_context->LockGL();
    1.10      }
    1.11  private:
    1.12      /* Event management */
    1.13 @@ -272,6 +273,17 @@
    1.14          }
    1.15          BE_SetKeyState(scancode, state);
    1.16          SDL_SendKeyboardKey(state, BE_GetScancodeFromBeKey(scancode));
    1.17 +        
    1.18 +        if (state == SDL_PRESSED && SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
    1.19 +            const int8 *keyUtf8;
    1.20 +            ssize_t count;
    1.21 +            if (msg->FindData("key-utf8", B_INT8_TYPE, (const void**)&keyUtf8, &count) == B_OK) {
    1.22 +                char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
    1.23 +                SDL_zero(text);
    1.24 +                SDL_memcpy(text, keyUtf8, count);
    1.25 +                SDL_SendKeyboardText(text);
    1.26 +            }
    1.27 +        }
    1.28      }
    1.29  
    1.30      void _HandleMouseFocus(BMessage *msg) {
     2.1 --- a/src/video/haiku/SDL_BWin.h	Wed Oct 19 20:39:12 2016 -0700
     2.2 +++ b/src/video/haiku/SDL_BWin.h	Wed Oct 19 20:42:22 2016 -0700
     2.3 @@ -337,16 +337,30 @@
     2.4              break;
     2.5  
     2.6          case B_KEY_DOWN:
     2.7 +            {
     2.8 +                int32 i = 0;
     2.9 +                int8 byte;
    2.10 +                int8 bytes[4] = { 0, 0, 0, 0 };
    2.11 +                while (i < 4 && msg->FindInt8("byte", i, &byte) == B_OK) {
    2.12 +                    bytes[i] = byte;
    2.13 +                    i++;
    2.14 +                }
    2.15 +                if (msg->FindInt32("key", &key) == B_OK) {
    2.16 +                    _KeyEvent((SDL_Scancode)key, &bytes[0], i, SDL_PRESSED);
    2.17 +                }
    2.18 +            }
    2.19 +            break;
    2.20 +            
    2.21          case B_UNMAPPED_KEY_DOWN:      /* modifier keys are unmapped */
    2.22              if (msg->FindInt32("key", &key) == B_OK) {
    2.23 -                _KeyEvent((SDL_Scancode)key, SDL_PRESSED);
    2.24 +                _KeyEvent((SDL_Scancode)key, NULL, 0, SDL_PRESSED);
    2.25              }
    2.26              break;
    2.27  
    2.28          case B_KEY_UP:
    2.29          case B_UNMAPPED_KEY_UP:        /* modifier keys are unmapped */
    2.30              if (msg->FindInt32("key", &key) == B_OK) {
    2.31 -                _KeyEvent(key, SDL_RELEASED);
    2.32 +                _KeyEvent(key, NULL, 0, SDL_RELEASED);
    2.33              }
    2.34              break;
    2.35  
    2.36 @@ -512,13 +526,15 @@
    2.37          _PostWindowEvent(msg);
    2.38      }
    2.39  
    2.40 -    void _KeyEvent(int32 keyCode, int32 keyState) {
    2.41 +    void _KeyEvent(int32 keyCode, const int8 *keyUtf8, const ssize_t & len, int32 keyState) {
    2.42          /* Create a message to pass along to the BeApp thread */
    2.43          BMessage msg(BAPP_KEY);
    2.44          msg.AddInt32("key-state", keyState);
    2.45          msg.AddInt32("key-scancode", keyCode);
    2.46 +        if (keyUtf8 != NULL) {
    2.47 +        	msg.AddData("key-utf8", B_INT8_TYPE, (const void*)keyUtf8, len);
    2.48 +        }
    2.49          be_app->PostMessage(&msg);
    2.50 -        /* Apparently SDL only uses the scancode */
    2.51      }
    2.52  
    2.53      void _RepaintEvent() {