src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Oct 2012 20:49:16 -0700
changeset 6555 f2c03c06d987
parent 6464 ab55284b389f
child 6654 2ecfb25be1e2
permissions -rw-r--r--
Fixed bug 1614 - SDL for Android does not implement TextInput API

Andrey Isakov 2012-10-03 08:30:25 PDT

I've found out in the process of porting one OS project to Android/SDL2 that
there is no support for TextInput events/APIs on Android.
So I implemented some kind of initial support of that feature, and at the very
least it seems to work fine with latin chars input with soft and hardware
keyboards on my Moto Milestone2. I've also tried playing around with more
complex IMEs, like japanese, logging the process and it seemed to work too. I'm
not sure since the app itself I am working on does not have support for
non-latin input.

The main point of the patch is to place a fake input view in the region
specified by SDL_SetTextInputRect and create a custom InputConnection for it.
The reason to make it a separate view is to support Android's pan&scan on input
feature properly. For details please refer to
http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html
Even though the manual states that SetTextInputRect is used to determine the
IME variants position, I thought this would be a proper use for this too.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 /* Android SDL video driver implementation
    26 */
    27 
    28 #include "SDL_video.h"
    29 #include "SDL_mouse.h"
    30 #include "../SDL_sysvideo.h"
    31 #include "../SDL_pixels_c.h"
    32 #include "../../events/SDL_events_c.h"
    33 #include "../../events/SDL_windowevents_c.h"
    34 
    35 #include "SDL_androidvideo.h"
    36 #include "SDL_androidclipboard.h"
    37 #include "SDL_androidevents.h"
    38 #include "SDL_androidkeyboard.h"
    39 #include "SDL_androidwindow.h"
    40 
    41 #define ANDROID_VID_DRIVER_NAME "Android"
    42 
    43 /* Initialization/Query functions */
    44 static int Android_VideoInit(_THIS);
    45 static void Android_VideoQuit(_THIS);
    46 
    47 /* GL functions (SDL_androidgl.c) */
    48 extern int Android_GL_LoadLibrary(_THIS, const char *path);
    49 extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
    50 extern void Android_GL_UnloadLibrary(_THIS);
    51 //extern int *Android_GL_GetVisual(_THIS, Display * display, int screen);
    52 extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
    53 extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
    54                               SDL_GLContext context);
    55 extern int Android_GL_SetSwapInterval(_THIS, int interval);
    56 extern int Android_GL_GetSwapInterval(_THIS);
    57 extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
    58 extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
    59 
    60 /* Android driver bootstrap functions */
    61 
    62 
    63 // These are filled in with real values in Android_SetScreenResolution on 
    64 // init (before SDL_main())
    65 int Android_ScreenWidth = 0;
    66 int Android_ScreenHeight = 0;
    67 Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    68 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    69 
    70 /* Currently only one window */
    71 SDL_Window *Android_Window = NULL;
    72 
    73 static int
    74 Android_Available(void)
    75 {
    76     return 1;
    77 }
    78 
    79 static void
    80 Android_DeleteDevice(SDL_VideoDevice * device)
    81 {
    82     SDL_free(device);
    83 }
    84 
    85 static SDL_VideoDevice *
    86 Android_CreateDevice(int devindex)
    87 {
    88     printf("Creating video device\n");
    89     SDL_VideoDevice *device;
    90     SDL_VideoData *data;
    91 
    92     /* Initialize all variables that we clean on shutdown */
    93     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    94     if (!device) {
    95         SDL_OutOfMemory();
    96         return NULL;
    97     }
    98 
    99     data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
   100     if (!data) {
   101         SDL_OutOfMemory();
   102         SDL_free(device);
   103         return NULL;
   104     }
   105 
   106     device->driverdata = data;
   107 
   108     /* Set the function pointers */
   109     device->VideoInit = Android_VideoInit;
   110     device->VideoQuit = Android_VideoQuit;
   111     device->PumpEvents = Android_PumpEvents;
   112 
   113     device->CreateWindow = Android_CreateWindow;
   114     device->SetWindowTitle = Android_SetWindowTitle;
   115     device->DestroyWindow = Android_DestroyWindow;
   116 
   117     device->free = Android_DeleteDevice;
   118 
   119     /* GL pointers */
   120     device->GL_LoadLibrary = Android_GL_LoadLibrary;
   121     device->GL_GetProcAddress = Android_GL_GetProcAddress;
   122     device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
   123     device->GL_CreateContext = Android_GL_CreateContext;
   124     device->GL_MakeCurrent = Android_GL_MakeCurrent;
   125     device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
   126     device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
   127     device->GL_SwapWindow = Android_GL_SwapWindow;
   128     device->GL_DeleteContext = Android_GL_DeleteContext;
   129 
   130     /* Screen keyboard */
   131     device->SDL_HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
   132     device->SDL_ShowScreenKeyboard = Android_ShowScreenKeyboard;
   133     device->SDL_HideScreenKeyboard = Android_HideScreenKeyboard;
   134     device->SDL_ToggleScreenKeyboard = Android_ToggleScreenKeyboard;
   135     device->SDL_IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
   136 
   137     /* Clipboard */
   138     device->SetClipboardText = Android_SetClipboardText;
   139     device->GetClipboardText = Android_GetClipboardText;
   140     device->HasClipboardText = Android_HasClipboardText;
   141 
   142     /* Text input */
   143     device->StartTextInput = Android_StartTextInput;
   144     device->StopTextInput = Android_StopTextInput;
   145     device->SetTextInputRect = Android_SetTextInputRect;
   146 
   147     return device;
   148 }
   149 
   150 VideoBootStrap Android_bootstrap = {
   151     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   152     Android_Available, Android_CreateDevice
   153 };
   154 
   155 
   156 int
   157 Android_VideoInit(_THIS)
   158 {
   159     SDL_DisplayMode mode;
   160 
   161     mode.format = Android_ScreenFormat;
   162     mode.w = Android_ScreenWidth;
   163     mode.h = Android_ScreenHeight;
   164     mode.refresh_rate = 0;
   165     mode.driverdata = NULL;
   166     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   167         return -1;
   168     }
   169 
   170     SDL_zero(mode);
   171     SDL_AddDisplayMode(&_this->displays[0], &mode);
   172 
   173     Android_InitKeyboard();
   174 
   175     /* We're done! */
   176     return 0;
   177 }
   178 
   179 void
   180 Android_VideoQuit(_THIS)
   181 {
   182 }
   183 
   184 /* This function gets called before VideoInit() */
   185 void
   186 Android_SetScreenResolution(int width, int height, Uint32 format)
   187 {
   188     Android_ScreenWidth = width;
   189     Android_ScreenHeight = height;   
   190     Android_ScreenFormat = format;
   191 
   192     if (Android_Window) {
   193         SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
   194     }
   195 }
   196 
   197 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   198 
   199 /* vi: set ts=4 sw=4 expandtab: */