summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/android/SDL_androidpen.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/android/SDL_androidpen.c')
-rw-r--r--contrib/SDL-3.2.8/src/video/android/SDL_androidpen.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/android/SDL_androidpen.c b/contrib/SDL-3.2.8/src/video/android/SDL_androidpen.c
new file mode 100644
index 0000000..e287cab
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/android/SDL_androidpen.c
@@ -0,0 +1,98 @@
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#include "SDL_internal.h"
22
23#ifdef SDL_VIDEO_DRIVER_ANDROID
24
25#include "SDL_androidpen.h"
26#include "../../events/SDL_pen_c.h"
27#include "../../core/android/SDL_android.h"
28
29#define ACTION_DOWN 0
30#define ACTION_UP 1
31#define ACTION_CANCEL 3
32#define ACTION_POINTER_DOWN 5
33#define ACTION_POINTER_UP 6
34#define ACTION_HOVER_EXIT 10
35
36void Android_OnPen(SDL_Window *window, int pen_id_in, int button, int action, float x, float y, float p)
37{
38 if (!window) {
39 return;
40 }
41
42 // pointer index starts from zero.
43 pen_id_in++;
44
45 SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) pen_id_in);
46 if (!pen) {
47 // TODO: Query JNI for pen device info
48 SDL_PenInfo peninfo;
49 SDL_zero(peninfo);
50 peninfo.capabilities = SDL_PEN_CAPABILITY_PRESSURE | SDL_PEN_CAPABILITY_ERASER;
51 peninfo.num_buttons = 2;
52 peninfo.subtype = SDL_PEN_TYPE_PEN;
53 pen = SDL_AddPenDevice(0, NULL, &peninfo, (void *) (size_t) pen_id_in);
54 if (!pen) {
55 SDL_Log("error: can't add a pen device %d", pen_id_in);
56 return;
57 }
58 }
59
60 SDL_SendPenMotion(0, pen, window, x, y);
61 SDL_SendPenAxis(0, pen, window, SDL_PEN_AXIS_PRESSURE, p);
62 // TODO: add more axis
63
64 SDL_PenInputFlags current = SDL_GetPenStatus(pen, NULL, 0);
65 int diff = current ^ button;
66 if (diff != 0) {
67 // Android only exposes BUTTON_STYLUS_PRIMARY and BUTTON_STYLUS_SECONDARY
68 if (diff & SDL_PEN_INPUT_BUTTON_1)
69 SDL_SendPenButton(0, pen, window, 1, (button & SDL_PEN_INPUT_BUTTON_1) != 0);
70
71 if (diff & SDL_PEN_INPUT_BUTTON_2)
72 SDL_SendPenButton(0, pen, window, 2, (button & SDL_PEN_INPUT_BUTTON_2) != 0);
73 }
74
75 // button contains DOWN/ERASER_TIP on DOWN/UP regardless of pressed state, use action to distinguish
76 // we don't compare tip flags above because MotionEvent.getButtonState doesn't return stylus tip/eraser state.
77 switch (action) {
78 case ACTION_CANCEL:
79 case ACTION_HOVER_EXIT:
80 SDL_RemovePenDevice(0, pen);
81 break;
82
83 case ACTION_DOWN:
84 case ACTION_POINTER_DOWN:
85 SDL_SendPenTouch(0, pen, window, (button & SDL_PEN_INPUT_ERASER_TIP) != 0, true);
86 break;
87
88 case ACTION_UP:
89 case ACTION_POINTER_UP:
90 SDL_SendPenTouch(0, pen, window, (button & SDL_PEN_INPUT_ERASER_TIP) != 0, false);
91 break;
92
93 default:
94 break;
95 }
96}
97
98#endif // SDL_VIDEO_DRIVER_ANDROID