From 6dc0cc9e71712e6f84bf17bd5e642a474488c4d3 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 13 Jan 2011 18:31:15 -0800 Subject: [PATCH] Added "mouse" support for the Android touch screen --- src/SDL_android.cpp | 9 +--- src/events/SDL_mouse_c.h | 2 +- src/video/android/SDL_androidtouch.c | 60 +++++++++++++++++++++++++++ src/video/android/SDL_androidtouch.h | 28 +++++++++++++ src/video/android/SDL_androidvideo.c | 7 ++++ src/video/android/SDL_androidvideo.h | 1 + src/video/android/SDL_androidwindow.c | 9 ++++ 7 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/video/android/SDL_androidtouch.c create mode 100644 src/video/android/SDL_androidtouch.h diff --git a/src/SDL_android.cpp b/src/SDL_android.cpp index 87d3ea451..4f5f8f5ae 100644 --- a/src/SDL_android.cpp +++ b/src/SDL_android.cpp @@ -26,6 +26,7 @@ extern "C" { #include "events/SDL_events_c.h" #include "video/android/SDL_androidkeyboard.h" +#include "video/android/SDL_androidtouch.h" #include "video/android/SDL_androidvideo.h" /* Impelemented in audio/android/SDL_androidaudio.c */ @@ -124,13 +125,7 @@ extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch( JNIEnv* env, jclass jcls, jint action, jfloat x, jfloat y, jfloat p) { -#ifdef DEBUG - __android_log_print(ANDROID_LOG_INFO, "SDL", - "SDL: native touch event %d @ %f/%f, pressure %f\n", - action, x, y, p); -#endif - - //TODO: Pass this off to the SDL multitouch stuff + Android_OnTouch(action, x, y, p); } // Accelerometer diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h index 0f0f6de54..4983b1a42 100644 --- a/src/events/SDL_mouse_c.h +++ b/src/events/SDL_mouse_c.h @@ -26,7 +26,7 @@ struct SDL_Cursor { - SDL_Cursor *next; + struct SDL_Cursor *next; void *driverdata; }; diff --git a/src/video/android/SDL_androidtouch.c b/src/video/android/SDL_androidtouch.c new file mode 100644 index 000000000..7b3fc2fab --- /dev/null +++ b/src/video/android/SDL_androidtouch.c @@ -0,0 +1,60 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +#include + +#include "SDL_events.h" +#include "../../events/SDL_mouse_c.h" + +#include "SDL_androidtouch.h" + + +#define ACTION_DOWN 0 +#define ACTION_UP 1 +#define ACTION_MOVE 2 +#define ACTION_CANCEL 3 +#define ACTION_OUTSIDE 4 + +void Android_OnTouch(int action, float x, float y, float p) +{ + if (!Android_Window) { + return; + } + + if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE)) { + SDL_SetMouseFocus(Android_Window); + SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y); + switch(action) { + case ACTION_DOWN: + SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT); + break; + case ACTION_UP: + SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT); + break; + } + } else { + SDL_SetMouseFocus(NULL); + } +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/android/SDL_androidtouch.h b/src/video/android/SDL_androidtouch.h new file mode 100644 index 000000000..13a0ebe07 --- /dev/null +++ b/src/video/android/SDL_androidtouch.h @@ -0,0 +1,28 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +#include "SDL_androidvideo.h" + +extern void Android_OnTouch(int action, float x, float y, float p); + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c index a744e70e9..bc36d3240 100644 --- a/src/video/android/SDL_androidvideo.c +++ b/src/video/android/SDL_androidvideo.c @@ -29,6 +29,7 @@ #include "../SDL_sysvideo.h" #include "../SDL_pixels_c.h" #include "../../events/SDL_events_c.h" +#include "../../events/SDL_windowevents_c.h" #include "SDL_androidvideo.h" #include "SDL_androidevents.h" @@ -63,6 +64,8 @@ int Android_ScreenWidth = 0; int Android_ScreenHeight = 0; Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN; +/* Currently only one window */ +SDL_Window *Android_Window = NULL; static int Android_Available(void) @@ -158,6 +161,10 @@ Android_SetScreenResolution(int width, int height, Uint32 format) Android_ScreenWidth = width; Android_ScreenHeight = height; Android_ScreenFormat = format; + + if (Android_Window) { + SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height); + } } /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/android/SDL_androidvideo.h b/src/video/android/SDL_androidvideo.h index 3fc7d2a6b..f909169fc 100644 --- a/src/video/android/SDL_androidvideo.h +++ b/src/video/android/SDL_androidvideo.h @@ -34,6 +34,7 @@ extern void Android_SetScreenResolution(int width, int height, Uint32 format); extern int Android_ScreenWidth; extern int Android_ScreenHeight; extern Uint32 Android_ScreenFormat; +extern SDL_Window *Android_Window; #endif /* _SDL_androidvideo_h */ diff --git a/src/video/android/SDL_androidwindow.c b/src/video/android/SDL_androidwindow.c index 1cd929b2a..397de1c0b 100644 --- a/src/video/android/SDL_androidwindow.c +++ b/src/video/android/SDL_androidwindow.c @@ -29,6 +29,12 @@ int Android_CreateWindow(_THIS, SDL_Window * window) { + if (Android_Window) { + SDL_SetError("Android only supports one window"); + return -1; + } + Android_Window = window; + /* Adjust the window data to match the screen */ window->x = 0; window->y = 0; @@ -47,6 +53,9 @@ Android_SetWindowTitle(_THIS, SDL_Window * window) void Android_DestroyWindow(_THIS, SDL_Window * window) { + if (window == Android_Window) { + Android_Window = NULL; + } } /* vi: set ts=4 sw=4 expandtab: */