SDL  2.0
SDL_androidmouse.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #include "../../SDL_internal.h"
23 
24 #if SDL_VIDEO_DRIVER_ANDROID
25 
26 #include "SDL_androidmouse.h"
27 
28 #include "SDL_events.h"
29 #include "../../events/SDL_mouse_c.h"
30 
31 #include "../../core/android/SDL_android.h"
32 
33 /* See Android's MotionEvent class for constants */
34 #define ACTION_DOWN 0
35 #define ACTION_UP 1
36 #define ACTION_MOVE 2
37 #define ACTION_HOVER_MOVE 7
38 #define ACTION_SCROLL 8
39 #define BUTTON_PRIMARY 1
40 #define BUTTON_SECONDARY 2
41 #define BUTTON_TERTIARY 4
42 #define BUTTON_BACK 8
43 #define BUTTON_FORWARD 16
44 
45 /* Last known Android mouse button state (includes all buttons) */
46 static int last_state;
47 
48 void
50 {
51  last_state = 0;
52 }
53 
54 /* Translate Android mouse button state to SDL mouse button */
55 static Uint8
56 TranslateButton(int state)
57 {
58  if (state & BUTTON_PRIMARY) {
59  return SDL_BUTTON_LEFT;
60  } else if (state & BUTTON_SECONDARY) {
61  return SDL_BUTTON_RIGHT;
62  } else if (state & BUTTON_TERTIARY) {
63  return SDL_BUTTON_MIDDLE;
64  } else if (state & BUTTON_FORWARD) {
65  return SDL_BUTTON_X1;
66  } else if (state & BUTTON_BACK) {
67  return SDL_BUTTON_X2;
68  } else {
69  return 0;
70  }
71 }
72 
73 void
74 Android_OnMouse(int state, int action, float x, float y)
75 {
76  int changes;
77  Uint8 button;
78 
79  if (!Android_Window) {
80  return;
81  }
82 
83  switch(action) {
84  case ACTION_DOWN:
85  changes = state & ~last_state;
86  button = TranslateButton(changes);
87  last_state = state;
90  break;
91 
92  case ACTION_UP:
93  changes = last_state & ~state;
94  button = TranslateButton(changes);
95  last_state = state;
98  break;
99 
100  case ACTION_MOVE:
101  case ACTION_HOVER_MOVE:
102  SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
103  break;
104 
105  case ACTION_SCROLL:
107  break;
108 
109  default:
110  break;
111  }
112 }
113 
114 #endif /* SDL_VIDEO_DRIVER_ANDROID */
115 
116 /* vi: set ts=4 sw=4 expandtab: */
117 
SDL_Texture * button
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
#define SDL_BUTTON_RIGHT
Definition: SDL_mouse.h:284
struct xkb_state * state
void Android_InitMouse(void)
#define SDL_BUTTON_X1
Definition: SDL_mouse.h:285
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
Definition: SDL_mouse.c:237
#define SDL_BUTTON_LEFT
Definition: SDL_mouse.h:282
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_Window * Android_Window
#define SDL_BUTTON_MIDDLE
Definition: SDL_mouse.h:283
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
void Android_OnMouse(int button, int action, float x, float y)
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
Definition: SDL_mouse.c:512
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_BUTTON_X2
Definition: SDL_mouse.h:286
#define SDL_RELEASED
Definition: SDL_events.h:49
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
Definition: SDL_mouse.c:506