Reimplemented Android cursor API support using reflection so it builds with older SDKs
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
22 #include "../../SDL_internal.h"
24 #if SDL_VIDEO_DRIVER_ANDROID
26 #include "SDL_androidmouse.h"
28 #include "SDL_events.h"
29 #include "../../events/SDL_mouse_c.h"
31 #include "../../core/android/SDL_android.h"
33 /* See Android's MotionEvent class for constants */
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
43 #define BUTTON_FORWARD 16
50 } SDL_AndroidCursorData;
52 /* Last known Android mouse button state (includes all buttons) */
53 static int last_state;
57 Android_WrapCursor(int custom_cursor, int system_cursor)
61 cursor = SDL_calloc(1, sizeof(*cursor));
63 SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)SDL_calloc(1, sizeof(*data));
65 data->custom_cursor = custom_cursor;
66 data->system_cursor = system_cursor;
67 cursor->driverdata = data;
81 Android_CreateDefaultCursor()
83 return Android_WrapCursor(0, SDL_SYSTEM_CURSOR_ARROW);
87 Android_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
90 SDL_Surface *converted;
92 converted = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
96 custom_cursor = Android_JNI_CreateCustomCursor(converted, hot_x, hot_y);
97 SDL_FreeSurface(converted);
102 return Android_WrapCursor(custom_cursor, 0);
106 Android_CreateSystemCursor(SDL_SystemCursor id)
108 return Android_WrapCursor(0, id);
112 Android_FreeCursor(SDL_Cursor * cursor)
114 SDL_free(cursor->driverdata);
119 Android_ShowCursor(SDL_Cursor * cursor)
122 SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)cursor->driverdata;
123 if (data->custom_cursor) {
124 if (!Android_JNI_SetCustomCursor(data->custom_cursor)) {
125 return SDL_Unsupported();
128 if (!Android_JNI_SetSystemCursor(data->system_cursor)) {
129 return SDL_Unsupported();
133 if (!Android_JNI_SetSystemCursor(-1)) {
134 return SDL_Unsupported();
141 Android_InitMouse(void)
143 SDL_Mouse *mouse = SDL_GetMouse();
145 mouse->CreateCursor = Android_CreateCursor;
146 mouse->CreateSystemCursor = Android_CreateSystemCursor;
147 mouse->ShowCursor = Android_ShowCursor;
148 mouse->FreeCursor = Android_FreeCursor;
150 SDL_SetDefaultCursor(Android_CreateDefaultCursor());
155 /* Translate Android mouse button state to SDL mouse button */
157 TranslateButton(int state)
159 if (state & BUTTON_PRIMARY) {
160 return SDL_BUTTON_LEFT;
161 } else if (state & BUTTON_SECONDARY) {
162 return SDL_BUTTON_RIGHT;
163 } else if (state & BUTTON_TERTIARY) {
164 return SDL_BUTTON_MIDDLE;
165 } else if (state & BUTTON_FORWARD) {
166 return SDL_BUTTON_X1;
167 } else if (state & BUTTON_BACK) {
168 return SDL_BUTTON_X2;
175 Android_OnMouse(int state, int action, float x, float y)
180 if (!Android_Window) {
186 changes = state & ~last_state;
187 button = TranslateButton(changes);
189 SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
190 SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button);
194 changes = last_state & ~state;
195 button = TranslateButton(changes);
197 SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
198 SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button);
202 case ACTION_HOVER_MOVE:
203 SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
207 SDL_SendMouseWheel(Android_Window, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
215 #endif /* SDL_VIDEO_DRIVER_ANDROID */
217 /* vi: set ts=4 sw=4 expandtab: */