Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added preliminary keyboard event support
Browse files Browse the repository at this point in the history
  • Loading branch information
bieh committed Jul 6, 2010
1 parent 6f2641d commit b839e97
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 19 deletions.
16 changes: 16 additions & 0 deletions android/testproject/jni/app-android.cpp
Expand Up @@ -36,6 +36,8 @@ jmethodID midCreateGLContext;
jmethodID midFlipBuffers;

extern "C" int SDL_main();
extern "C" int Android_OnKeyDown(int keycode);
extern "C" int Android_OnKeyUp(int keycode);

/*******************************************************************************
Functions called by JNI
Expand Down Expand Up @@ -77,6 +79,20 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
return JNI_VERSION_1_4;
}

extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyDown(JNIEnv* env,
jobject obj, jint keycode){

int r = Android_OnKeyDown(keycode);
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: native key down %d, %d\n", keycode, r);
}

extern "C" void Java_org_libsdl_android_SDLActivity_onNativeKeyUp(JNIEnv* env,
jobject obj, jint keycode){

int r = Android_OnKeyUp(keycode);
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: native key up %d, %d\n", keycode, r);
}



/*******************************************************************************
Expand Down
17 changes: 15 additions & 2 deletions android/testproject/jni/lesson05.c
Expand Up @@ -37,6 +37,8 @@
/* This is our SDL surface */
SDL_Surface *surface;

int rotation = 0;


/**************************************
gluperspective implementation
Expand Down Expand Up @@ -196,10 +198,20 @@ void handleKeyPress( SDL_keysym *keysym )
*/
SDL_WM_ToggleFullScreen( surface );
break;
case SDLK_LEFT:
rotation -= 30;
break;

case SDLK_RIGHT:
rotation += 30;
break;

default:
break;
}

__android_log_print(ANDROID_LOG_INFO, "SDL","Keycode: %d, %d, %d\n", keysym->sym, SDLK_LEFT, SDLK_RIGHT);

return;
}

Expand Down Expand Up @@ -231,6 +243,7 @@ int initGL( GLvoid )
/* Here goes our drawing code */
int drawGLScene( GLvoid )
{

static int Frames = 0;
static int T0 = 0;

Expand All @@ -253,14 +266,14 @@ int drawGLScene( GLvoid )
//Draw a triangle
//glRotatef(iRot, 0, 1, 0);

glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
glRotatef( rotation, 0.0f, 1.0f, 0.0f );


glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);

/* Rotate The Triangle On The Y axis ( NEW ) */
glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
//glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );

/* GLES variant of drawing a triangle */
const GLfloat triVertices[][9] = {
Expand Down
51 changes: 38 additions & 13 deletions android/testproject/src/org/libsdl/android/SDLActivity.java
Expand Up @@ -4,14 +4,14 @@
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.*;

import android.app.Activity;
import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.app.*;
import android.content.*;
import android.view.*;
import android.os.*;
import android.util.Log;
import android.graphics.*;
import android.text.method.*;
import android.text.*;

import java.lang.*;

Expand Down Expand Up @@ -55,13 +55,14 @@ protected void onResume() {
super.onResume();
}







//C functions we call
public static native void nativeInit();
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);



Expand All @@ -82,8 +83,7 @@ public static void flipBuffers(){




//EGL context creation


}

Expand All @@ -104,7 +104,7 @@ public void run(){
Because of this, that's where we set up the SDL thread
*/
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback{
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, View.OnKeyListener {

//This is what SDL runs in. It invokes SDL_main(), eventually
private Thread mSDLThread;
Expand All @@ -117,7 +117,12 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback{
//Startup
public SDLSurface(Context context) {
super(context);
getHolder().addCallback(this);
getHolder().addCallback(this);

setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
setOnKeyListener(this);
}

//Called when we have a valid drawing surface
Expand Down Expand Up @@ -175,13 +180,13 @@ public boolean initEGL(){
mEGLDisplay = dpy;
mEGLSurface = surface;


}catch(Exception e){
Log.v("SDL", e + "");
for(StackTraceElement s : e.getStackTrace()){
Log.v("SDL", s.toString());
}
}

Log.v("SDL","Done making!");

return true;
Expand Down Expand Up @@ -211,6 +216,26 @@ public void flipEGL(){
}
}
}




public boolean onKey(View v, int keyCode, KeyEvent event){

if(event.getAction() == KeyEvent.ACTION_DOWN){
SDLActivity.onNativeKeyDown(keyCode);
return true;
}

else if(event.getAction() == KeyEvent.ACTION_UP){
SDLActivity.onNativeKeyUp(keyCode);
return true;
}

return false;
}


}


16 changes: 12 additions & 4 deletions src/events/SDL_keyboard.c
Expand Up @@ -694,8 +694,16 @@ SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode)
Uint16 modstate;
Uint32 type;

if(!keyboard){
return 7;
}

if(!scancode){
return 8;
}

if (!keyboard || !scancode) {
return 0;
return 1;
}
#if 0
printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
Expand Down Expand Up @@ -788,22 +796,22 @@ SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode)
break;
default:
/* Invalid state -- bail */
return 0;
return 2;
}

/* Drop events that don't change state */
if (keyboard->keystate[scancode] == state) {
#if 0
printf("Keyboard event didn't change state - dropped!\n");
#endif
return 0;
return 3;
}

/* Update internal keyboard state */
keyboard->keystate[scancode] = state;

/* Post the event, if desired */
posted = 0;
posted = 4;
if (SDL_GetEventState(type) == SDL_ENABLE) {
SDL_Event event;
event.key.type = type;
Expand Down
28 changes: 28 additions & 0 deletions src/video/android/SDL_androidevents.c
Expand Up @@ -30,6 +30,24 @@
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"

#include "SDL_androidevents.h"

void Android_InitEvents(){

SDL_Keyboard keyboard;

SDL_zero(keyboard);
SDL_AddKeyboard(&keyboard, -1);

SDLKey keymap[SDL_NUM_SCANCODES];

/* Add default scancode to key mapping */
SDL_GetDefaultKeymap(keymap);
SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);


}

void
Android_PumpEvents(_THIS)
{
Expand All @@ -49,4 +67,14 @@ Android_PumpEvents(_THIS)
*/
}

int
Android_OnKeyDown(int keycode){
return SDL_SendKeyboardKey(0, SDL_PRESSED, (SDL_scancode)keycode);
}

int
Android_OnKeyUp(int keycode){
return SDL_SendKeyboardKey(0, SDL_RELEASED, (SDL_scancode)keycode);
}

/* vi: set ts=4 sw=4 expandtab: */
1 change: 1 addition & 0 deletions src/video/android/SDL_androidevents.h
Expand Up @@ -24,5 +24,6 @@
#include "SDL_androidvideo.h"

extern void Android_PumpEvents(_THIS);
extern void Android_InitEvents();

/* vi: set ts=4 sw=4 expandtab: */
2 changes: 2 additions & 0 deletions src/video/android/SDL_androidvideo.c
Expand Up @@ -132,6 +132,8 @@ Android_VideoInit(_THIS)
SDL_zero(mode);
SDL_AddDisplayMode(&_this->displays[0], &mode);

Android_InitEvents();

/* We're done! */
return 0;
}
Expand Down

0 comments on commit b839e97

Please sign in to comment.