Added "mouse" support for the Android touch screen
authorSam Lantinga <slouken@libsdl.org>
Thu, 13 Jan 2011 18:31:15 -0800
changeset 500177df56570442
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
     1.1 --- a/src/SDL_android.cpp	Thu Jan 13 18:03:56 2011 -0800
     1.2 +++ b/src/SDL_android.cpp	Thu Jan 13 18:31:15 2011 -0800
     1.3 @@ -26,6 +26,7 @@
     1.4  extern "C" {
     1.5  #include "events/SDL_events_c.h"
     1.6  #include "video/android/SDL_androidkeyboard.h"
     1.7 +#include "video/android/SDL_androidtouch.h"
     1.8  #include "video/android/SDL_androidvideo.h"
     1.9  
    1.10  /* Impelemented in audio/android/SDL_androidaudio.c */
    1.11 @@ -124,13 +125,7 @@
    1.12                                      JNIEnv* env, jclass jcls,
    1.13                                      jint action, jfloat x, jfloat y, jfloat p)
    1.14  {
    1.15 -#ifdef DEBUG
    1.16 -    __android_log_print(ANDROID_LOG_INFO, "SDL", 
    1.17 -                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
    1.18 -                        action, x, y, p);
    1.19 -#endif
    1.20 -
    1.21 -    //TODO: Pass this off to the SDL multitouch stuff
    1.22 +    Android_OnTouch(action, x, y, p);
    1.23  }
    1.24  
    1.25  // Accelerometer
     2.1 --- a/src/events/SDL_mouse_c.h	Thu Jan 13 18:03:56 2011 -0800
     2.2 +++ b/src/events/SDL_mouse_c.h	Thu Jan 13 18:31:15 2011 -0800
     2.3 @@ -26,7 +26,7 @@
     2.4  
     2.5  struct SDL_Cursor
     2.6  {
     2.7 -    SDL_Cursor *next;
     2.8 +    struct SDL_Cursor *next;
     2.9      void *driverdata;
    2.10  };
    2.11  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/android/SDL_androidtouch.c	Thu Jan 13 18:31:15 2011 -0800
     3.3 @@ -0,0 +1,60 @@
     3.4 +/*
     3.5 +    SDL - Simple DirectMedia Layer
     3.6 +    Copyright (C) 1997-2010 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Lesser General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2.1 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Lesser General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Lesser General Public
    3.19 +    License along with this library; if not, write to the Free Software
    3.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +#include "SDL_config.h"
    3.26 +
    3.27 +#include <android/log.h>
    3.28 +
    3.29 +#include "SDL_events.h"
    3.30 +#include "../../events/SDL_mouse_c.h"
    3.31 +
    3.32 +#include "SDL_androidtouch.h"
    3.33 +
    3.34 +
    3.35 +#define ACTION_DOWN 0
    3.36 +#define ACTION_UP 1
    3.37 +#define ACTION_MOVE 2
    3.38 +#define ACTION_CANCEL 3
    3.39 +#define ACTION_OUTSIDE 4
    3.40 +
    3.41 +void Android_OnTouch(int action, float x, float y, float p)
    3.42 +{
    3.43 +    if (!Android_Window) {
    3.44 +        return;
    3.45 +    }
    3.46 +
    3.47 +    if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE)) {
    3.48 +        SDL_SetMouseFocus(Android_Window);
    3.49 +        SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y);
    3.50 +        switch(action) {
    3.51 +        case ACTION_DOWN:
    3.52 +            SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT);
    3.53 +            break;
    3.54 +        case ACTION_UP:
    3.55 +            SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT);
    3.56 +            break;
    3.57 +        }
    3.58 +    } else {
    3.59 +        SDL_SetMouseFocus(NULL);
    3.60 +    }
    3.61 +}
    3.62 +
    3.63 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/android/SDL_androidtouch.h	Thu Jan 13 18:31:15 2011 -0800
     4.3 @@ -0,0 +1,28 @@
     4.4 +/*
     4.5 +    SDL - Simple DirectMedia Layer
     4.6 +    Copyright (C) 1997-2010 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Lesser General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2.1 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Lesser General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Lesser General Public
    4.19 +    License along with this library; if not, write to the Free Software
    4.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +#include "SDL_config.h"
    4.26 +
    4.27 +#include "SDL_androidvideo.h"
    4.28 +
    4.29 +extern void Android_OnTouch(int action, float x, float y, float p);
    4.30 +
    4.31 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/android/SDL_androidvideo.c	Thu Jan 13 18:03:56 2011 -0800
     5.2 +++ b/src/video/android/SDL_androidvideo.c	Thu Jan 13 18:31:15 2011 -0800
     5.3 @@ -29,6 +29,7 @@
     5.4  #include "../SDL_sysvideo.h"
     5.5  #include "../SDL_pixels_c.h"
     5.6  #include "../../events/SDL_events_c.h"
     5.7 +#include "../../events/SDL_windowevents_c.h"
     5.8  
     5.9  #include "SDL_androidvideo.h"
    5.10  #include "SDL_androidevents.h"
    5.11 @@ -63,6 +64,8 @@
    5.12  int Android_ScreenHeight = 0;
    5.13  Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    5.14  
    5.15 +/* Currently only one window */
    5.16 +SDL_Window *Android_Window = NULL;
    5.17  
    5.18  static int
    5.19  Android_Available(void)
    5.20 @@ -158,6 +161,10 @@
    5.21      Android_ScreenWidth = width;
    5.22      Android_ScreenHeight = height;   
    5.23      Android_ScreenFormat = format;
    5.24 +
    5.25 +    if (Android_Window) {
    5.26 +        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
    5.27 +    }
    5.28  }
    5.29  
    5.30  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/android/SDL_androidvideo.h	Thu Jan 13 18:03:56 2011 -0800
     6.2 +++ b/src/video/android/SDL_androidvideo.h	Thu Jan 13 18:31:15 2011 -0800
     6.3 @@ -34,6 +34,7 @@
     6.4  extern int Android_ScreenWidth;
     6.5  extern int Android_ScreenHeight;
     6.6  extern Uint32 Android_ScreenFormat;
     6.7 +extern SDL_Window *Android_Window;
     6.8  
     6.9  #endif /* _SDL_androidvideo_h */
    6.10  
     7.1 --- a/src/video/android/SDL_androidwindow.c	Thu Jan 13 18:03:56 2011 -0800
     7.2 +++ b/src/video/android/SDL_androidwindow.c	Thu Jan 13 18:31:15 2011 -0800
     7.3 @@ -29,6 +29,12 @@
     7.4  int
     7.5  Android_CreateWindow(_THIS, SDL_Window * window)
     7.6  {
     7.7 +    if (Android_Window) {
     7.8 +        SDL_SetError("Android only supports one window");
     7.9 +        return -1;
    7.10 +    }
    7.11 +    Android_Window = window;
    7.12 +
    7.13      /* Adjust the window data to match the screen */
    7.14      window->x = 0;
    7.15      window->y = 0;
    7.16 @@ -47,6 +53,9 @@
    7.17  void
    7.18  Android_DestroyWindow(_THIS, SDL_Window * window)
    7.19  {
    7.20 +    if (window == Android_Window) {
    7.21 +        Android_Window = NULL;
    7.22 +    }
    7.23  }
    7.24  
    7.25  /* vi: set ts=4 sw=4 expandtab: */