diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/android/SDL_androidmouse.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/android/SDL_androidmouse.c | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/android/SDL_androidmouse.c b/contrib/SDL-3.2.8/src/video/android/SDL_androidmouse.c new file mode 100644 index 0000000..2facf0d --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/android/SDL_androidmouse.c | |||
| @@ -0,0 +1,250 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 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 | #ifdef SDL_VIDEO_DRIVER_ANDROID | ||
| 25 | |||
| 26 | #include "SDL_androidmouse.h" | ||
| 27 | |||
| 28 | #include "../../events/SDL_mouse_c.h" | ||
| 29 | |||
| 30 | #include "../../core/android/SDL_android.h" | ||
| 31 | |||
| 32 | // See Android's MotionEvent class for constants | ||
| 33 | #define ACTION_DOWN 0 | ||
| 34 | #define ACTION_UP 1 | ||
| 35 | #define ACTION_MOVE 2 | ||
| 36 | #define ACTION_HOVER_MOVE 7 | ||
| 37 | #define ACTION_SCROLL 8 | ||
| 38 | #define BUTTON_PRIMARY 1 | ||
| 39 | #define BUTTON_SECONDARY 2 | ||
| 40 | #define BUTTON_TERTIARY 4 | ||
| 41 | #define BUTTON_BACK 8 | ||
| 42 | #define BUTTON_FORWARD 16 | ||
| 43 | |||
| 44 | struct SDL_CursorData | ||
| 45 | { | ||
| 46 | int custom_cursor; | ||
| 47 | int system_cursor; | ||
| 48 | }; | ||
| 49 | |||
| 50 | // Last known Android mouse button state (includes all buttons) | ||
| 51 | static int last_state; | ||
| 52 | |||
| 53 | // Blank cursor | ||
| 54 | static SDL_Cursor *empty_cursor; | ||
| 55 | |||
| 56 | static SDL_Cursor *Android_WrapCursor(int custom_cursor, int system_cursor) | ||
| 57 | { | ||
| 58 | SDL_Cursor *cursor; | ||
| 59 | |||
| 60 | cursor = SDL_calloc(1, sizeof(*cursor)); | ||
| 61 | if (cursor) { | ||
| 62 | SDL_CursorData *data = (SDL_CursorData *)SDL_calloc(1, sizeof(*data)); | ||
| 63 | if (data) { | ||
| 64 | data->custom_cursor = custom_cursor; | ||
| 65 | data->system_cursor = system_cursor; | ||
| 66 | cursor->internal = data; | ||
| 67 | } else { | ||
| 68 | SDL_free(cursor); | ||
| 69 | cursor = NULL; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | return cursor; | ||
| 74 | } | ||
| 75 | |||
| 76 | static SDL_Cursor *Android_CreateDefaultCursor(void) | ||
| 77 | { | ||
| 78 | SDL_SystemCursor id = SDL_GetDefaultSystemCursor(); | ||
| 79 | return Android_WrapCursor(0, id); | ||
| 80 | } | ||
| 81 | |||
| 82 | static SDL_Cursor *Android_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y) | ||
| 83 | { | ||
| 84 | int custom_cursor; | ||
| 85 | SDL_Surface *converted; | ||
| 86 | |||
| 87 | converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888); | ||
| 88 | if (!converted) { | ||
| 89 | return NULL; | ||
| 90 | } | ||
| 91 | custom_cursor = Android_JNI_CreateCustomCursor(converted, hot_x, hot_y); | ||
| 92 | SDL_DestroySurface(converted); | ||
| 93 | if (!custom_cursor) { | ||
| 94 | SDL_Unsupported(); | ||
| 95 | return NULL; | ||
| 96 | } | ||
| 97 | return Android_WrapCursor(custom_cursor, 0); | ||
| 98 | } | ||
| 99 | |||
| 100 | static SDL_Cursor *Android_CreateSystemCursor(SDL_SystemCursor id) | ||
| 101 | { | ||
| 102 | return Android_WrapCursor(0, id); | ||
| 103 | } | ||
| 104 | |||
| 105 | static void Android_FreeCursor(SDL_Cursor *cursor) | ||
| 106 | { | ||
| 107 | SDL_CursorData *data = cursor->internal; | ||
| 108 | if (data->custom_cursor != 0) { | ||
| 109 | Android_JNI_DestroyCustomCursor(data->custom_cursor); | ||
| 110 | } | ||
| 111 | SDL_free(cursor->internal); | ||
| 112 | SDL_free(cursor); | ||
| 113 | } | ||
| 114 | |||
| 115 | static SDL_Cursor *Android_CreateEmptyCursor(void) | ||
| 116 | { | ||
| 117 | if (!empty_cursor) { | ||
| 118 | SDL_Surface *empty_surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888); | ||
| 119 | if (empty_surface) { | ||
| 120 | SDL_memset(empty_surface->pixels, 0, (size_t)empty_surface->h * empty_surface->pitch); | ||
| 121 | empty_cursor = Android_CreateCursor(empty_surface, 0, 0); | ||
| 122 | SDL_DestroySurface(empty_surface); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | return empty_cursor; | ||
| 126 | } | ||
| 127 | |||
| 128 | static void Android_DestroyEmptyCursor(void) | ||
| 129 | { | ||
| 130 | if (empty_cursor) { | ||
| 131 | Android_FreeCursor(empty_cursor); | ||
| 132 | empty_cursor = NULL; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | static bool Android_ShowCursor(SDL_Cursor *cursor) | ||
| 137 | { | ||
| 138 | if (!cursor) { | ||
| 139 | cursor = Android_CreateEmptyCursor(); | ||
| 140 | } | ||
| 141 | if (cursor) { | ||
| 142 | SDL_CursorData *data = cursor->internal; | ||
| 143 | if (data->custom_cursor) { | ||
| 144 | if (!Android_JNI_SetCustomCursor(data->custom_cursor)) { | ||
| 145 | return SDL_Unsupported(); | ||
| 146 | } | ||
| 147 | } else { | ||
| 148 | if (!Android_JNI_SetSystemCursor(data->system_cursor)) { | ||
| 149 | return SDL_Unsupported(); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | return true; | ||
| 153 | } else { | ||
| 154 | // SDL error set inside Android_CreateEmptyCursor() | ||
| 155 | return false; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | static bool Android_SetRelativeMouseMode(bool enabled) | ||
| 160 | { | ||
| 161 | if (!Android_JNI_SupportsRelativeMouse()) { | ||
| 162 | return SDL_Unsupported(); | ||
| 163 | } | ||
| 164 | |||
| 165 | if (!Android_JNI_SetRelativeMouseEnabled(enabled)) { | ||
| 166 | return SDL_Unsupported(); | ||
| 167 | } | ||
| 168 | |||
| 169 | return true; | ||
| 170 | } | ||
| 171 | |||
| 172 | void Android_InitMouse(void) | ||
| 173 | { | ||
| 174 | SDL_Mouse *mouse = SDL_GetMouse(); | ||
| 175 | |||
| 176 | mouse->CreateCursor = Android_CreateCursor; | ||
| 177 | mouse->CreateSystemCursor = Android_CreateSystemCursor; | ||
| 178 | mouse->ShowCursor = Android_ShowCursor; | ||
| 179 | mouse->FreeCursor = Android_FreeCursor; | ||
| 180 | mouse->SetRelativeMouseMode = Android_SetRelativeMouseMode; | ||
| 181 | |||
| 182 | SDL_SetDefaultCursor(Android_CreateDefaultCursor()); | ||
| 183 | |||
| 184 | last_state = 0; | ||
| 185 | } | ||
| 186 | |||
| 187 | void Android_QuitMouse(void) | ||
| 188 | { | ||
| 189 | Android_DestroyEmptyCursor(); | ||
| 190 | } | ||
| 191 | |||
| 192 | // Translate Android mouse button state to SDL mouse button | ||
| 193 | static Uint8 TranslateButton(int state) | ||
| 194 | { | ||
| 195 | if (state & BUTTON_PRIMARY) { | ||
| 196 | return SDL_BUTTON_LEFT; | ||
| 197 | } else if (state & BUTTON_SECONDARY) { | ||
| 198 | return SDL_BUTTON_RIGHT; | ||
| 199 | } else if (state & BUTTON_TERTIARY) { | ||
| 200 | return SDL_BUTTON_MIDDLE; | ||
| 201 | } else if (state & BUTTON_FORWARD) { | ||
| 202 | return SDL_BUTTON_X1; | ||
| 203 | } else if (state & BUTTON_BACK) { | ||
| 204 | return SDL_BUTTON_X2; | ||
| 205 | } else { | ||
| 206 | return 0; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y, bool relative) | ||
| 211 | { | ||
| 212 | int changes; | ||
| 213 | Uint8 button; | ||
| 214 | |||
| 215 | if (!window) { | ||
| 216 | return; | ||
| 217 | } | ||
| 218 | |||
| 219 | switch (action) { | ||
| 220 | case ACTION_DOWN: | ||
| 221 | changes = state & ~last_state; | ||
| 222 | button = TranslateButton(changes); | ||
| 223 | last_state = state; | ||
| 224 | SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y); | ||
| 225 | SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, true); | ||
| 226 | break; | ||
| 227 | |||
| 228 | case ACTION_UP: | ||
| 229 | changes = last_state & ~state; | ||
| 230 | button = TranslateButton(changes); | ||
| 231 | last_state = state; | ||
| 232 | SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y); | ||
| 233 | SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, false); | ||
| 234 | break; | ||
| 235 | |||
| 236 | case ACTION_MOVE: | ||
| 237 | case ACTION_HOVER_MOVE: | ||
| 238 | SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y); | ||
| 239 | break; | ||
| 240 | |||
| 241 | case ACTION_SCROLL: | ||
| 242 | SDL_SendMouseWheel(0, window, SDL_DEFAULT_MOUSE_ID, x, y, SDL_MOUSEWHEEL_NORMAL); | ||
| 243 | break; | ||
| 244 | |||
| 245 | default: | ||
| 246 | break; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | #endif // SDL_VIDEO_DRIVER_ANDROID | ||
