Skip to content

Commit e2bbbdc

Browse files
Susko3icculus
authored andcommitted
Implement SDL_GetPenDeviceType() for Android
1 parent 23b487c commit e2bbbdc

File tree

12 files changed

+75
-12
lines changed

12 files changed

+75
-12
lines changed

android-project/app/src/main/java/org/libsdl/app/SDLActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,11 @@ public enum NativeState {
229229
private static SDLFileDialogState mFileDialogState = null;
230230
protected static boolean mDispatchingKeyEvent = false;
231231

232-
protected static SDLGenericMotionListener_API14 getMotionListener() {
232+
public static SDLGenericMotionListener_API14 getMotionListener() {
233233
if (mMotionListener == null) {
234-
if (Build.VERSION.SDK_INT >= 26 /* Android 8.0 (O) */) {
234+
if (Build.VERSION.SDK_INT >= 29 /* Android 10 (Q) */) {
235+
mMotionListener = new SDLGenericMotionListener_API29();
236+
} else if (Build.VERSION.SDK_INT >= 26 /* Android 8.0 (O) */) {
235237
mMotionListener = new SDLGenericMotionListener_API26();
236238
} else if (Build.VERSION.SDK_INT >= 24 /* Android 7.0 (N) */) {
237239
mMotionListener = new SDLGenericMotionListener_API24();
@@ -1063,7 +1065,7 @@ protected boolean sendCommand(int command, Object data) {
10631065
public static native void onNativeTouch(int touchDevId, int pointerFingerId,
10641066
int action, float x,
10651067
float y, float p);
1066-
public static native void onNativePen(int penId, int button, int action, float x, float y, float p);
1068+
public static native void onNativePen(int penId, int device_type, int button, int action, float x, float y, float p);
10671069
public static native void onNativeAccel(float x, float y, float z);
10681070
public static native void onNativeClipboardChanged();
10691071
public static native void onNativeSurfaceCreated();

android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,10 @@ protected SDLHaptic getHaptic(int device_id) {
646646
}
647647

648648
class SDLGenericMotionListener_API14 implements View.OnGenericMotionListener {
649+
protected static final int SDL_PEN_DEVICE_TYPE_UNKNOWN = 0;
650+
protected static final int SDL_PEN_DEVICE_TYPE_DIRECT = 1;
651+
protected static final int SDL_PEN_DEVICE_TYPE_INDIRECT = 2;
652+
649653
// Generic Motion (mouse hover, joystick...) events go here
650654
@Override
651655
public boolean onGenericMotion(View v, MotionEvent event) {
@@ -697,7 +701,7 @@ public boolean onGenericMotion(View v, MotionEvent event) {
697701
// BUTTON_STYLUS_PRIMARY is 2^5, so shift by 4, and apply SDL_PEN_INPUT_DOWN/SDL_PEN_INPUT_ERASER_TIP
698702
int buttons = (event.getButtonState() >> 4) | (1 << (toolType == MotionEvent.TOOL_TYPE_STYLUS ? 0 : 30));
699703

700-
SDLActivity.onNativePen(event.getPointerId(i), buttons, action, x, y, p);
704+
SDLActivity.onNativePen(event.getPointerId(i), getPenDeviceType(event.getDevice()), buttons, action, x, y, p);
701705
consumed = true;
702706
break;
703707
}
@@ -735,6 +739,9 @@ float getEventY(MotionEvent event, int pointerIndex) {
735739
return event.getY(pointerIndex);
736740
}
737741

742+
int getPenDeviceType(InputDevice penDevice) {
743+
return SDL_PEN_DEVICE_TYPE_UNKNOWN;
744+
}
738745
}
739746

740747
class SDLGenericMotionListener_API24 extends SDLGenericMotionListener_API14 {
@@ -856,3 +863,15 @@ float getEventY(MotionEvent event, int pointerIndex) {
856863
return event.getY(pointerIndex);
857864
}
858865
}
866+
867+
class SDLGenericMotionListener_API29 extends SDLGenericMotionListener_API26 {
868+
@Override
869+
int getPenDeviceType(InputDevice penDevice)
870+
{
871+
if (penDevice == null) {
872+
return SDL_PEN_DEVICE_TYPE_UNKNOWN;
873+
}
874+
875+
return penDevice.isExternal() ? SDL_PEN_DEVICE_TYPE_INDIRECT : SDL_PEN_DEVICE_TYPE_DIRECT;
876+
}
877+
}

android-project/app/src/main/java/org/libsdl/app/SDLSurface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public boolean onTouch(View v, MotionEvent event) {
281281
// BUTTON_STYLUS_PRIMARY is 2^5, so shift by 4, and apply SDL_PEN_INPUT_DOWN/SDL_PEN_INPUT_ERASER_TIP
282282
int buttonState = (event.getButtonState() >> 4) | (1 << (toolType == MotionEvent.TOOL_TYPE_STYLUS ? 0 : 30));
283283

284-
SDLActivity.onNativePen(pointerId, buttonState, action, x, y, p);
284+
SDLActivity.onNativePen(pointerId, SDLActivity.getMotionListener().getPenDeviceType(event.getDevice()), buttonState, action, x, y, p);
285285
} else { // MotionEvent.TOOL_TYPE_FINGER or MotionEvent.TOOL_TYPE_UNKNOWN
286286
pointerId = event.getPointerId(i);
287287
x = getNormalizedX(event.getX(i));

include/SDL3/SDL_pen.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <SDL3/SDL_mouse.h>
5353
#include <SDL3/SDL_touch.h>
5454

55+
#include <SDL3/SDL_begin_code.h>
5556
/* Set up for C function definitions, even when using C++ */
5657
#ifdef __cplusplus
5758
extern "C" {
@@ -84,7 +85,6 @@ typedef Uint32 SDL_PenID;
8485
*/
8586
#define SDL_PEN_TOUCHID ((SDL_TouchID)-2)
8687

87-
8888
/**
8989
* Pen input flags, as reported by various pen events' `pen_state` field.
9090
*
@@ -127,10 +127,36 @@ typedef enum SDL_PenAxis
127127
SDL_PEN_AXIS_COUNT /**< Total known pen axis types in this version of SDL. This number may grow in future releases! */
128128
} SDL_PenAxis;
129129

130+
/**
131+
* An enum that describes the type of a pen device.
132+
*
133+
* \since This enum is available since SDL 3.4.0.
134+
*/
135+
typedef enum SDL_PenDeviceType
136+
{
137+
SDL_PEN_DEVICE_TYPE_INVALID = -1,
138+
SDL_PEN_DEVICE_TYPE_UNKNOWN,
139+
SDL_PEN_DEVICE_TYPE_DIRECT,
140+
SDL_PEN_DEVICE_TYPE_INDIRECT
141+
} SDL_PenDeviceType;
142+
143+
/**
144+
* Get the device type of the given pen.
145+
*
146+
* \param instance_id the pen instance ID.
147+
* \returns the device type of the given pen, or SDL_PEN_DEVICE_TYPE_INVALID on failure; call SDL_GetError() for more information.
148+
*
149+
* \threadsafety It is safe to call this function from any thread.
150+
*
151+
* \since This function is available since SDL 3.4.0.
152+
*/
153+
extern SDL_DECLSPEC SDL_PenDeviceType SDLCALL SDL_GetPenDeviceType(SDL_PenID instance_id);
154+
130155
/* Ends C function definitions when using C++ */
131156
#ifdef __cplusplus
132157
}
133158
#endif
159+
#include <SDL3/SDL_close_code.h>
134160

135161
#endif /* SDL_pen_h_ */
136162

src/core/android/SDL_android.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
137137

138138
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativePen)(
139139
JNIEnv *env, jclass jcls,
140-
jint pen_id_in, jint button, jint action, jfloat x, jfloat y, jfloat p);
140+
jint pen_id_in, jint device_type, jint button, jint action, jfloat x, jfloat y, jfloat p);
141141

142142
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeAccel)(
143143
JNIEnv *env, jclass jcls,
@@ -235,7 +235,7 @@ static JNINativeMethod SDLActivity_tab[] = {
235235
{ "onNativePinchUpdate", "(F)V", SDL_JAVA_INTERFACE(onNativePinchUpdate) },
236236
{ "onNativePinchEnd", "()V", SDL_JAVA_INTERFACE(onNativePinchEnd) },
237237
{ "onNativeMouse", "(IIFFZ)V", SDL_JAVA_INTERFACE(onNativeMouse) },
238-
{ "onNativePen", "(IIIFFF)V", SDL_JAVA_INTERFACE(onNativePen) },
238+
{ "onNativePen", "(IIIIFFF)V", SDL_JAVA_INTERFACE(onNativePen) },
239239
{ "onNativeAccel", "(FFF)V", SDL_JAVA_INTERFACE(onNativeAccel) },
240240
{ "onNativeClipboardChanged", "()V", SDL_JAVA_INTERFACE(onNativeClipboardChanged) },
241241
{ "nativeLowMemory", "()V", SDL_JAVA_INTERFACE(nativeLowMemory) },
@@ -1431,11 +1431,11 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
14311431
// Pen
14321432
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativePen)(
14331433
JNIEnv *env, jclass jcls,
1434-
jint pen_id_in, jint button, jint action, jfloat x, jfloat y, jfloat p)
1434+
jint pen_id_in, jint device_type, jint button, jint action, jfloat x, jfloat y, jfloat p)
14351435
{
14361436
SDL_LockMutex(Android_ActivityMutex);
14371437

1438-
Android_OnPen(Android_Window, pen_id_in, button, action, x, y, p);
1438+
Android_OnPen(Android_Window, pen_id_in, device_type, button, action, x, y, p);
14391439

14401440
SDL_UnlockMutex(Android_ActivityMutex);
14411441
}

src/dynapi/SDL_dynapi.sym

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,7 @@ SDL3_0.0.0 {
12651265
SDL_SavePNG_IO;
12661266
SDL_SavePNG;
12671267
SDL_GetSystemPageSize;
1268+
SDL_GetPenDeviceType;
12681269
# extra symbols go here (don't modify this line)
12691270
local: *;
12701271
};

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,3 +1291,4 @@
12911291
#define SDL_SavePNG_IO SDL_SavePNG_IO_REAL
12921292
#define SDL_SavePNG SDL_SavePNG_REAL
12931293
#define SDL_GetSystemPageSize SDL_GetSystemPageSize_REAL
1294+
#define SDL_GetPenDeviceType SDL_GetPenDeviceType_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,3 +1299,4 @@ SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadPNG,(const char *a),(a),return)
12991299
SDL_DYNAPI_PROC(bool,SDL_SavePNG_IO,(SDL_Surface *a,SDL_IOStream *b,bool c),(a,b,c),return)
13001300
SDL_DYNAPI_PROC(bool,SDL_SavePNG,(SDL_Surface *a,const char *b),(a,b),return)
13011301
SDL_DYNAPI_PROC(int,SDL_GetSystemPageSize,(void),(),return)
1302+
SDL_DYNAPI_PROC(SDL_PenDeviceType,SDL_GetPenDeviceType,(SDL_PenID a),(a),return)

src/events/SDL_pen.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ SDL_PenInputFlags SDL_GetPenStatus(SDL_PenID instance_id, float *axes, int num_a
199199
return result;
200200
}
201201

202+
SDL_PenDeviceType SDL_GetPenDeviceType(SDL_PenID instance_id)
203+
{
204+
SDL_LockRWLockForReading(pen_device_rwlock);
205+
const SDL_Pen *pen = FindPenByInstanceId(instance_id);
206+
const SDL_PenDeviceType result = pen ? pen->info.device_type : SDL_PEN_DEVICE_TYPE_INVALID;
207+
SDL_UnlockRWLock(pen_device_rwlock);
208+
return result;
209+
}
210+
202211
SDL_PenCapabilityFlags SDL_GetPenCapabilityFromAxis(SDL_PenAxis axis)
203212
{
204213
// the initial capability bits happen to match up, but as

src/events/SDL_pen_c.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef Uint32 SDL_PenCapabilityFlags;
3636
#define SDL_PEN_CAPABILITY_TANGENTIAL_PRESSURE (1u << 6) /**< Provides barrel pressure on SDL_PEN_AXIS_TANGENTIAL_PRESSURE. */
3737
#define SDL_PEN_CAPABILITY_ERASER (1u << 7) /**< Pen also has an eraser tip. */
3838

39+
// Rename before making this public as it clashes with SDL_PenDeviceType.
40+
// Prior art in Android calls this "tool type".
3941
typedef enum SDL_PenSubtype
4042
{
4143
SDL_PEN_TYPE_UNKNOWN, /**< Unknown pen device */
@@ -53,6 +55,7 @@ typedef struct SDL_PenInfo
5355
Uint32 wacom_id; /**< For Wacom devices: wacom tool type ID, otherwise 0 (useful e.g. with libwacom) */
5456
int num_buttons; /**< Number of pen buttons (not counting the pen tip), or -1 if unknown. */
5557
SDL_PenSubtype subtype; /**< type of pen device */
58+
SDL_PenDeviceType device_type;
5659
} SDL_PenInfo;
5760

5861
// Backend calls this when a new pen device is hotplugged, plus once for each pen already connected at startup.

0 commit comments

Comments
 (0)