Added "mouse" support for the Android touch screen
authorSam Lantinga <slouken@libsdl.org>
Thu, 13 Jan 2011 18:31:15 -0800
changeset 5001 77df56570442
parent 5000 6a10693e66c3
child 5002 c5b9486688ce
Added "mouse" support for the Android touch screen
src/SDL_android.cpp
src/events/SDL_mouse_c.h
src/video/android/SDL_androidtouch.c
src/video/android/SDL_androidtouch.h
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidvideo.h
src/video/android/SDL_androidwindow.c
--- a/src/SDL_android.cpp	Thu Jan 13 18:03:56 2011 -0800
+++ b/src/SDL_android.cpp	Thu Jan 13 18:31:15 2011 -0800
@@ -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 @@
                                     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
--- a/src/events/SDL_mouse_c.h	Thu Jan 13 18:03:56 2011 -0800
+++ b/src/events/SDL_mouse_c.h	Thu Jan 13 18:31:15 2011 -0800
@@ -26,7 +26,7 @@
 
 struct SDL_Cursor
 {
-    SDL_Cursor *next;
+    struct SDL_Cursor *next;
     void *driverdata;
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/video/android/SDL_androidtouch.c	Thu Jan 13 18:31:15 2011 -0800
@@ -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 <android/log.h>
+
+#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: */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/video/android/SDL_androidtouch.h	Thu Jan 13 18:31:15 2011 -0800
@@ -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: */
--- a/src/video/android/SDL_androidvideo.c	Thu Jan 13 18:03:56 2011 -0800
+++ b/src/video/android/SDL_androidvideo.c	Thu Jan 13 18:31:15 2011 -0800
@@ -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_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_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: */
--- a/src/video/android/SDL_androidvideo.h	Thu Jan 13 18:03:56 2011 -0800
+++ b/src/video/android/SDL_androidvideo.h	Thu Jan 13 18:31:15 2011 -0800
@@ -34,6 +34,7 @@
 extern int Android_ScreenWidth;
 extern int Android_ScreenHeight;
 extern Uint32 Android_ScreenFormat;
+extern SDL_Window *Android_Window;
 
 #endif /* _SDL_androidvideo_h */
 
--- a/src/video/android/SDL_androidwindow.c	Thu Jan 13 18:03:56 2011 -0800
+++ b/src/video/android/SDL_androidwindow.c	Thu Jan 13 18:31:15 2011 -0800
@@ -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 @@
 void
 Android_DestroyWindow(_THIS, SDL_Window * window)
 {
+    if (window == Android_Window) {
+        Android_Window = NULL;
+    }
 }
 
 /* vi: set ts=4 sw=4 expandtab: */