Added preliminary keyboard event support
authorPaul Hunkin
Wed, 07 Jul 2010 00:43:23 +1200
changeset 4713ba38983b10c2
parent 4712 8319aa8fa4dc
child 4714 0f05f2f16fad
Added preliminary keyboard event support
android/testproject/jni/app-android.cpp
android/testproject/jni/lesson05.c
android/testproject/src/org/libsdl/android/SDLActivity.java
src/events/SDL_keyboard.c
src/video/android/SDL_androidevents.c
src/video/android/SDL_androidevents.h
src/video/android/SDL_androidvideo.c
     1.1 --- a/android/testproject/jni/app-android.cpp	Tue Jun 29 01:30:11 2010 +1200
     1.2 +++ b/android/testproject/jni/app-android.cpp	Wed Jul 07 00:43:23 2010 +1200
     1.3 @@ -36,6 +36,8 @@
     1.4  jmethodID midFlipBuffers;
     1.5  
     1.6  extern "C" int SDL_main();
     1.7 +extern "C" int Android_OnKeyDown(int keycode);
     1.8 +extern "C" int Android_OnKeyUp(int keycode);
     1.9  
    1.10  /*******************************************************************************
    1.11                   Functions called by JNI
    1.12 @@ -77,6 +79,20 @@
    1.13      return JNI_VERSION_1_4;
    1.14  }
    1.15  
    1.16 +extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv*  env, 
    1.17 +               jobject obj, jint keycode){
    1.18 +    
    1.19 +    int r = Android_OnKeyDown(keycode);
    1.20 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: native key down %d, %d\n", keycode, r);
    1.21 +}
    1.22 +
    1.23 +extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv*  env, 
    1.24 +               jobject obj, jint keycode){
    1.25 +    
    1.26 +    int r = Android_OnKeyUp(keycode);
    1.27 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: native key up %d, %d\n", keycode, r);
    1.28 +}
    1.29 +
    1.30  
    1.31  
    1.32  /*******************************************************************************
     2.1 --- a/android/testproject/jni/lesson05.c	Tue Jun 29 01:30:11 2010 +1200
     2.2 +++ b/android/testproject/jni/lesson05.c	Wed Jul 07 00:43:23 2010 +1200
     2.3 @@ -37,6 +37,8 @@
     2.4  /* This is our SDL surface */
     2.5  SDL_Surface *surface;
     2.6  
     2.7 +int rotation = 0;
     2.8 +
     2.9  
    2.10  /**************************************
    2.11  	gluperspective implementation
    2.12 @@ -196,10 +198,20 @@
    2.13  	     */
    2.14  	    SDL_WM_ToggleFullScreen( surface );
    2.15  	    break;
    2.16 +    case SDLK_LEFT:
    2.17 +        rotation -= 30;
    2.18 +        break;
    2.19 +
    2.20 +    case SDLK_RIGHT:
    2.21 +        rotation += 30;
    2.22 +        break;
    2.23 +        
    2.24  	default:
    2.25  	    break;
    2.26  	}
    2.27  
    2.28 +    __android_log_print(ANDROID_LOG_INFO, "SDL","Keycode: %d, %d, %d\n", keysym->sym, SDLK_LEFT, SDLK_RIGHT);
    2.29 +
    2.30      return;
    2.31  }
    2.32  
    2.33 @@ -231,6 +243,7 @@
    2.34  /* Here goes our drawing code */
    2.35  int drawGLScene( GLvoid )
    2.36  {
    2.37 +      
    2.38  	static int Frames = 0;
    2.39  	static int T0 = 0;
    2.40  	
    2.41 @@ -253,14 +266,14 @@
    2.42  	//Draw a triangle
    2.43  	//glRotatef(iRot, 0, 1, 0);
    2.44  
    2.45 -	glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
    2.46 +	glRotatef( rotation, 0.0f, 1.0f, 0.0f );
    2.47  
    2.48  
    2.49  	glEnableClientState (GL_VERTEX_ARRAY);
    2.50  	glEnableClientState (GL_COLOR_ARRAY);
    2.51  	
    2.52  	/* Rotate The Triangle On The Y axis ( NEW ) */
    2.53 -    glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
    2.54 +    //glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
    2.55  
    2.56      /* GLES variant of drawing a triangle */
    2.57      const GLfloat triVertices[][9] = {
     3.1 --- a/android/testproject/src/org/libsdl/android/SDLActivity.java	Tue Jun 29 01:30:11 2010 +1200
     3.2 +++ b/android/testproject/src/org/libsdl/android/SDLActivity.java	Wed Jul 07 00:43:23 2010 +1200
     3.3 @@ -4,14 +4,14 @@
     3.4  import javax.microedition.khronos.opengles.GL10;
     3.5  import javax.microedition.khronos.egl.*;
     3.6  
     3.7 -import android.app.Activity;
     3.8 -import android.content.Context;
     3.9 -import android.view.SurfaceHolder;
    3.10 -import android.view.SurfaceView;
    3.11 -import android.os.Bundle;
    3.12 -import android.view.MotionEvent;
    3.13 +import android.app.*;
    3.14 +import android.content.*;
    3.15 +import android.view.*;
    3.16 +import android.os.*;
    3.17  import android.util.Log;
    3.18  import android.graphics.*;
    3.19 +import android.text.method.*;
    3.20 +import android.text.*;
    3.21  
    3.22  import java.lang.*;
    3.23  
    3.24 @@ -55,13 +55,14 @@
    3.25          super.onResume();
    3.26      }
    3.27  
    3.28 -
    3.29 -
    3.30 +    
    3.31  
    3.32  
    3.33  
    3.34      //C functions we call
    3.35      public static native void nativeInit();
    3.36 +    public static native void onNativeKeyDown(int keycode);
    3.37 +    public static native void onNativeKeyUp(int keycode);
    3.38  
    3.39  
    3.40  
    3.41 @@ -82,8 +83,7 @@
    3.42  
    3.43  
    3.44  
    3.45 -
    3.46 -    //EGL context creation
    3.47 +    
    3.48      
    3.49  }
    3.50  
    3.51 @@ -104,7 +104,7 @@
    3.52  
    3.53      Because of this, that's where we set up the SDL thread
    3.54  */
    3.55 -class SDLSurface extends SurfaceView implements SurfaceHolder.Callback{
    3.56 +class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, View.OnKeyListener  {
    3.57  
    3.58      //This is what SDL runs in. It invokes SDL_main(), eventually
    3.59      private Thread mSDLThread;    
    3.60 @@ -117,7 +117,12 @@
    3.61      //Startup    
    3.62      public SDLSurface(Context context) {
    3.63          super(context);
    3.64 -        getHolder().addCallback(this);      
    3.65 +        getHolder().addCallback(this); 
    3.66 +    
    3.67 +        setFocusable(true);
    3.68 +        setFocusableInTouchMode(true);
    3.69 +        requestFocus();
    3.70 +        setOnKeyListener(this);      
    3.71      }
    3.72  
    3.73      //Called when we have a valid drawing surface
    3.74 @@ -175,13 +180,13 @@
    3.75              mEGLDisplay = dpy;
    3.76              mEGLSurface = surface;
    3.77              
    3.78 +            
    3.79          }catch(Exception e){
    3.80              Log.v("SDL", e + "");
    3.81              for(StackTraceElement s : e.getStackTrace()){
    3.82                  Log.v("SDL", s.toString());
    3.83              }
    3.84          }
    3.85 -
    3.86          Log.v("SDL","Done making!");
    3.87  
    3.88          return true;
    3.89 @@ -211,6 +216,26 @@
    3.90              }
    3.91          }
    3.92      }
    3.93 +
    3.94 +
    3.95 +  
    3.96 +
    3.97 +    public boolean onKey(View  v, int keyCode, KeyEvent event){
    3.98 +
    3.99 +        if(event.getAction() == KeyEvent.ACTION_DOWN){
   3.100 +            SDLActivity.onNativeKeyDown(keyCode);
   3.101 +            return true;
   3.102 +        }
   3.103 +        
   3.104 +        else if(event.getAction() == KeyEvent.ACTION_UP){
   3.105 +            SDLActivity.onNativeKeyUp(keyCode);
   3.106 +            return true;
   3.107 +        }
   3.108 +        
   3.109 +        return false;
   3.110 +    }
   3.111 +
   3.112 +
   3.113  }
   3.114  
   3.115  
     4.1 --- a/src/events/SDL_keyboard.c	Tue Jun 29 01:30:11 2010 +1200
     4.2 +++ b/src/events/SDL_keyboard.c	Wed Jul 07 00:43:23 2010 +1200
     4.3 @@ -694,8 +694,16 @@
     4.4      Uint16 modstate;
     4.5      Uint32 type;
     4.6  
     4.7 +    if(!keyboard){
     4.8 +        return 7;
     4.9 +    }
    4.10 +
    4.11 +    if(!scancode){
    4.12 +        return 8;
    4.13 +    }
    4.14 +
    4.15      if (!keyboard || !scancode) {
    4.16 -        return 0;
    4.17 +        return 1;
    4.18      }
    4.19  #if 0
    4.20      printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
    4.21 @@ -788,7 +796,7 @@
    4.22          break;
    4.23      default:
    4.24          /* Invalid state -- bail */
    4.25 -        return 0;
    4.26 +        return 2;
    4.27      }
    4.28  
    4.29      /* Drop events that don't change state */
    4.30 @@ -796,14 +804,14 @@
    4.31  #if 0
    4.32          printf("Keyboard event didn't change state - dropped!\n");
    4.33  #endif
    4.34 -        return 0;
    4.35 +        return 3;
    4.36      }
    4.37  
    4.38      /* Update internal keyboard state */
    4.39      keyboard->keystate[scancode] = state;
    4.40  
    4.41      /* Post the event, if desired */
    4.42 -    posted = 0;
    4.43 +    posted = 4;
    4.44      if (SDL_GetEventState(type) == SDL_ENABLE) {
    4.45          SDL_Event event;
    4.46          event.key.type = type;
     5.1 --- a/src/video/android/SDL_androidevents.c	Tue Jun 29 01:30:11 2010 +1200
     5.2 +++ b/src/video/android/SDL_androidevents.c	Wed Jul 07 00:43:23 2010 +1200
     5.3 @@ -30,6 +30,24 @@
     5.4  #include "../../events/SDL_sysevents.h"
     5.5  #include "../../events/SDL_events_c.h"
     5.6  
     5.7 +#include "SDL_androidevents.h"
     5.8 +
     5.9 +void Android_InitEvents(){
    5.10 +
    5.11 +    SDL_Keyboard keyboard;
    5.12 +
    5.13 +    SDL_zero(keyboard);
    5.14 +    SDL_AddKeyboard(&keyboard, -1);
    5.15 +
    5.16 +    SDLKey keymap[SDL_NUM_SCANCODES];
    5.17 +
    5.18 +    /* Add default scancode to key mapping */
    5.19 +    SDL_GetDefaultKeymap(keymap);
    5.20 +    SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
    5.21 +
    5.22 +
    5.23 +}
    5.24 +
    5.25  void
    5.26  Android_PumpEvents(_THIS)
    5.27  {
    5.28 @@ -49,4 +67,14 @@
    5.29      */
    5.30  }
    5.31  
    5.32 +int
    5.33 +Android_OnKeyDown(int keycode){
    5.34 +    return SDL_SendKeyboardKey(0, SDL_PRESSED, (SDL_scancode)keycode);
    5.35 +}
    5.36 +
    5.37 +int
    5.38 +Android_OnKeyUp(int keycode){
    5.39 +    return SDL_SendKeyboardKey(0, SDL_RELEASED, (SDL_scancode)keycode);
    5.40 +}
    5.41 +
    5.42  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/android/SDL_androidevents.h	Tue Jun 29 01:30:11 2010 +1200
     6.2 +++ b/src/video/android/SDL_androidevents.h	Wed Jul 07 00:43:23 2010 +1200
     6.3 @@ -24,5 +24,6 @@
     6.4  #include "SDL_androidvideo.h"
     6.5  
     6.6  extern void Android_PumpEvents(_THIS);
     6.7 +extern void Android_InitEvents();
     6.8  
     6.9  /* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/video/android/SDL_androidvideo.c	Tue Jun 29 01:30:11 2010 +1200
     7.2 +++ b/src/video/android/SDL_androidvideo.c	Wed Jul 07 00:43:23 2010 +1200
     7.3 @@ -132,6 +132,8 @@
     7.4      SDL_zero(mode);
     7.5      SDL_AddDisplayMode(&_this->displays[0], &mode);
     7.6  
     7.7 +    Android_InitEvents();
     7.8 +
     7.9      /* We're done! */
    7.10      return 0;
    7.11  }