2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2025-01-01 07:45:41 -08:00
|
|
|
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
appreciated but is not required.
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-11-29 18:34:15 -08:00
|
|
|
#include "SDL_internal.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-03-29 21:49:01 +00:00
|
|
|
#ifdef SDL_VIDEO_DRIVER_ANDROID
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
#include "SDL_androidmouse.h"
|
|
|
|
|
|
|
|
|
|
#include "../../events/SDL_mouse_c.h"
|
|
|
|
|
|
|
|
|
|
#include "../../core/android/SDL_android.h"
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// See Android's MotionEvent class for constants
|
2022-11-30 12:51:59 -08:00
|
|
|
#define ACTION_DOWN 0
|
|
|
|
|
#define ACTION_UP 1
|
|
|
|
|
#define ACTION_MOVE 2
|
2015-06-21 17:33:46 +02:00
|
|
|
#define ACTION_HOVER_MOVE 7
|
2022-11-30 12:51:59 -08:00
|
|
|
#define ACTION_SCROLL 8
|
|
|
|
|
#define BUTTON_PRIMARY 1
|
|
|
|
|
#define BUTTON_SECONDARY 2
|
|
|
|
|
#define BUTTON_TERTIARY 4
|
|
|
|
|
#define BUTTON_BACK 8
|
|
|
|
|
#define BUTTON_FORWARD 16
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2024-08-01 08:17:44 -07:00
|
|
|
struct SDL_CursorData
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
|
|
|
|
int custom_cursor;
|
|
|
|
|
int system_cursor;
|
2024-08-01 08:17:44 -07:00
|
|
|
};
|
2018-03-15 18:22:48 -07:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Last known Android mouse button state (includes all buttons)
|
2017-08-12 08:06:16 -07:00
|
|
|
static int last_state;
|
2016-01-12 22:23:00 +01:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Blank cursor
|
2018-06-18 13:14:04 -07:00
|
|
|
static SDL_Cursor *empty_cursor;
|
2018-03-15 18:22:48 -07:00
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static SDL_Cursor *Android_WrapCursor(int custom_cursor, int system_cursor)
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
|
|
|
|
SDL_Cursor *cursor;
|
|
|
|
|
|
|
|
|
|
cursor = SDL_calloc(1, sizeof(*cursor));
|
|
|
|
|
if (cursor) {
|
2024-08-01 08:17:44 -07:00
|
|
|
SDL_CursorData *data = (SDL_CursorData *)SDL_calloc(1, sizeof(*data));
|
2018-03-15 18:22:48 -07:00
|
|
|
if (data) {
|
|
|
|
|
data->custom_cursor = custom_cursor;
|
|
|
|
|
data->system_cursor = system_cursor;
|
2024-07-16 11:09:18 -07:00
|
|
|
cursor->internal = data;
|
2018-03-15 18:22:48 -07:00
|
|
|
} else {
|
|
|
|
|
SDL_free(cursor);
|
|
|
|
|
cursor = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 22:09:32 +04:00
|
|
|
static SDL_Cursor *Android_CreateDefaultCursor(void)
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
2024-11-25 10:54:27 +08:00
|
|
|
SDL_SystemCursor id = SDL_GetDefaultSystemCursor();
|
|
|
|
|
return Android_WrapCursor(0, id);
|
2018-03-15 18:22:48 -07:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static SDL_Cursor *Android_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
|
|
|
|
int custom_cursor;
|
|
|
|
|
SDL_Surface *converted;
|
|
|
|
|
|
2024-07-08 14:59:18 -07:00
|
|
|
converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888);
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!converted) {
|
2018-03-15 18:22:48 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
custom_cursor = Android_JNI_CreateCustomCursor(converted, hot_x, hot_y);
|
2022-12-27 06:36:39 -08:00
|
|
|
SDL_DestroySurface(converted);
|
2018-03-15 18:22:48 -07:00
|
|
|
if (!custom_cursor) {
|
|
|
|
|
SDL_Unsupported();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return Android_WrapCursor(custom_cursor, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static SDL_Cursor *Android_CreateSystemCursor(SDL_SystemCursor id)
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
|
|
|
|
return Android_WrapCursor(0, id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void Android_FreeCursor(SDL_Cursor *cursor)
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
2024-08-01 08:17:44 -07:00
|
|
|
SDL_CursorData *data = cursor->internal;
|
2021-12-21 22:07:17 +01:00
|
|
|
if (data->custom_cursor != 0) {
|
|
|
|
|
Android_JNI_DestroyCustomCursor(data->custom_cursor);
|
|
|
|
|
}
|
2024-07-16 11:09:18 -07:00
|
|
|
SDL_free(cursor->internal);
|
2018-03-15 18:22:48 -07:00
|
|
|
SDL_free(cursor);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 22:09:32 +04:00
|
|
|
static SDL_Cursor *Android_CreateEmptyCursor(void)
|
2018-06-18 13:14:04 -07:00
|
|
|
{
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!empty_cursor) {
|
2022-12-01 17:04:02 +01:00
|
|
|
SDL_Surface *empty_surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
|
2018-06-18 13:14:04 -07:00
|
|
|
if (empty_surface) {
|
2022-12-01 16:07:03 -05:00
|
|
|
SDL_memset(empty_surface->pixels, 0, (size_t)empty_surface->h * empty_surface->pitch);
|
2018-06-18 13:14:04 -07:00
|
|
|
empty_cursor = Android_CreateCursor(empty_surface, 0, 0);
|
2022-12-27 06:36:39 -08:00
|
|
|
SDL_DestroySurface(empty_surface);
|
2018-06-18 13:14:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return empty_cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 22:09:32 +04:00
|
|
|
static void Android_DestroyEmptyCursor(void)
|
2018-06-18 13:14:04 -07:00
|
|
|
{
|
|
|
|
|
if (empty_cursor) {
|
|
|
|
|
Android_FreeCursor(empty_cursor);
|
|
|
|
|
empty_cursor = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
static bool Android_ShowCursor(SDL_Cursor *cursor)
|
2018-03-15 18:22:48 -07:00
|
|
|
{
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!cursor) {
|
2018-06-18 13:14:04 -07:00
|
|
|
cursor = Android_CreateEmptyCursor();
|
|
|
|
|
}
|
2018-03-15 18:22:48 -07:00
|
|
|
if (cursor) {
|
2024-08-01 08:17:44 -07:00
|
|
|
SDL_CursorData *data = cursor->internal;
|
2018-03-15 18:22:48 -07:00
|
|
|
if (data->custom_cursor) {
|
2018-03-16 11:08:53 -07:00
|
|
|
if (!Android_JNI_SetCustomCursor(data->custom_cursor)) {
|
|
|
|
|
return SDL_Unsupported();
|
|
|
|
|
}
|
2018-03-15 18:22:48 -07:00
|
|
|
} else {
|
2018-03-16 11:08:53 -07:00
|
|
|
if (!Android_JNI_SetSystemCursor(data->system_cursor)) {
|
|
|
|
|
return SDL_Unsupported();
|
|
|
|
|
}
|
2018-03-15 18:22:48 -07:00
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2018-03-15 18:22:48 -07:00
|
|
|
} else {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// SDL error set inside Android_CreateEmptyCursor()
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2018-03-15 18:22:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
static bool Android_SetRelativeMouseMode(bool enabled)
|
2018-06-05 12:46:11 -07:00
|
|
|
{
|
|
|
|
|
if (!Android_JNI_SupportsRelativeMouse()) {
|
|
|
|
|
return SDL_Unsupported();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Android_JNI_SetRelativeMouseEnabled(enabled)) {
|
|
|
|
|
return SDL_Unsupported();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2018-06-05 12:46:11 -07:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
void Android_InitMouse(void)
|
2016-01-12 22:23:00 +01:00
|
|
|
{
|
2018-03-15 18:22:48 -07:00
|
|
|
SDL_Mouse *mouse = SDL_GetMouse();
|
|
|
|
|
|
|
|
|
|
mouse->CreateCursor = Android_CreateCursor;
|
|
|
|
|
mouse->CreateSystemCursor = Android_CreateSystemCursor;
|
|
|
|
|
mouse->ShowCursor = Android_ShowCursor;
|
|
|
|
|
mouse->FreeCursor = Android_FreeCursor;
|
2018-06-05 12:46:11 -07:00
|
|
|
mouse->SetRelativeMouseMode = Android_SetRelativeMouseMode;
|
2018-03-15 18:22:48 -07:00
|
|
|
|
|
|
|
|
SDL_SetDefaultCursor(Android_CreateDefaultCursor());
|
|
|
|
|
|
2017-08-12 08:06:16 -07:00
|
|
|
last_state = 0;
|
2016-01-12 22:23:00 +01:00
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
void Android_QuitMouse(void)
|
2018-06-18 13:14:04 -07:00
|
|
|
{
|
|
|
|
|
Android_DestroyEmptyCursor();
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Translate Android mouse button state to SDL mouse button
|
2022-11-30 12:51:59 -08:00
|
|
|
static Uint8 TranslateButton(int state)
|
2017-08-12 08:06:16 -07:00
|
|
|
{
|
|
|
|
|
if (state & BUTTON_PRIMARY) {
|
|
|
|
|
return SDL_BUTTON_LEFT;
|
|
|
|
|
} else if (state & BUTTON_SECONDARY) {
|
|
|
|
|
return SDL_BUTTON_RIGHT;
|
|
|
|
|
} else if (state & BUTTON_TERTIARY) {
|
|
|
|
|
return SDL_BUTTON_MIDDLE;
|
|
|
|
|
} else if (state & BUTTON_FORWARD) {
|
|
|
|
|
return SDL_BUTTON_X1;
|
|
|
|
|
} else if (state & BUTTON_BACK) {
|
|
|
|
|
return SDL_BUTTON_X2;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 09:21:26 -07:00
|
|
|
void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y, bool relative)
|
2017-08-12 08:06:16 -07:00
|
|
|
{
|
|
|
|
|
int changes;
|
|
|
|
|
Uint8 button;
|
|
|
|
|
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!window) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
switch (action) {
|
|
|
|
|
case ACTION_DOWN:
|
|
|
|
|
changes = state & ~last_state;
|
|
|
|
|
button = TranslateButton(changes);
|
|
|
|
|
last_state = state;
|
2024-03-22 10:49:07 -07:00
|
|
|
SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
|
2024-09-09 09:18:02 -07:00
|
|
|
SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, true);
|
2022-11-30 12:51:59 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ACTION_UP:
|
|
|
|
|
changes = last_state & ~state;
|
|
|
|
|
button = TranslateButton(changes);
|
|
|
|
|
last_state = state;
|
2024-03-22 10:49:07 -07:00
|
|
|
SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
|
2024-09-09 09:18:02 -07:00
|
|
|
SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, false);
|
2022-11-30 12:51:59 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ACTION_MOVE:
|
|
|
|
|
case ACTION_HOVER_MOVE:
|
2024-03-22 10:49:07 -07:00
|
|
|
SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
|
2022-11-30 12:51:59 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ACTION_SCROLL:
|
2024-03-22 10:49:07 -07:00
|
|
|
SDL_SendMouseWheel(0, window, SDL_DEFAULT_MOUSE_ID, x, y, SDL_MOUSEWHEEL_NORMAL);
|
2022-11-30 12:51:59 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // SDL_VIDEO_DRIVER_ANDROID
|