diff --git a/android/app-android.cpp b/android/app-android.cpp index 4e45c72d5f..dc487e003b 100644 --- a/android/app-android.cpp +++ b/android/app-android.cpp @@ -384,24 +384,21 @@ extern "C" jint Java_com_henrikrydgard_libnative_NativeApp_audioRender(JNIEnv* e extern "C" void JNICALL Java_com_henrikrydgard_libnative_NativeApp_touch (JNIEnv *, jclass, float x, float y, int code, int pointerId) { - float scaledX = (int)(x * dp_xscale); // why the (int) cast? - float scaledY = (int)(y * dp_yscale); + float scaledX = x * dp_xscale; + float scaledY = y * dp_yscale; TouchInput touch; touch.id = pointerId; touch.x = scaledX; touch.y = scaledY; - if (code == 1) { + touch.flags = code; + if (code & 2) { input_state.pointer_down[pointerId] = true; - touch.flags = TOUCH_DOWN; - } else if (code == 2) { + } else if (code & 4) { input_state.pointer_down[pointerId] = false; - touch.flags = TOUCH_UP; - } else { - touch.flags = TOUCH_MOVE; } - NativeTouch(touch); + NativeTouch(touch); { lock_guard guard(input_state.lock); if (pointerId >= MAX_POINTERS) { diff --git a/android/src/com/henrikrydgard/libnative/NativeGLView.java b/android/src/com/henrikrydgard/libnative/NativeGLView.java index b9455fb6fa..6fd0fc9391 100644 --- a/android/src/com/henrikrydgard/libnative/NativeGLView.java +++ b/android/src/com/henrikrydgard/libnative/NativeGLView.java @@ -3,12 +3,14 @@ package com.henrikrydgard.libnative; // Touch- and sensor-enabled GLSurfaceView. // Supports simple multitouch and pressure. +import android.annotation.TargetApi; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.opengl.GLSurfaceView; +import android.os.Build; import android.os.Handler; // import android.os.Build; // import android.util.Log; @@ -62,34 +64,44 @@ public class NativeGLView extends GLSurfaceView implements SensorEventListener, } } + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) + private int getToolType(final MotionEvent ev, int pointer) { + return ev.getToolType(pointer); + } + public boolean onTouchEvent(final MotionEvent ev) { + boolean canReadToolType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; for (int i = 0; i < ev.getPointerCount(); i++) { int pid = ev.getPointerId(i); int code = 0; final int action = ev.getActionMasked(); + // These code bits are now the same as the constants in input_state.h. switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: if (ev.getActionIndex() == i) - code = 1; + code = 2; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: if (ev.getActionIndex() == i) - code = 2; + code = 4; break; case MotionEvent.ACTION_MOVE: - code = 3; + code = 1; break; default: break; } + if (code != 0) { - float x = ev.getX(i); - float y = ev.getY(i); - NativeApp.touch(x, y, code, pid); + if (canReadToolType) { + int tool = getToolType(ev, i); + code |= tool << 10; // We use the Android tool type codes + } + NativeApp.touch(ev.getX(i), ev.getY(i), code, pid); } } return true; diff --git a/input/input_state.h b/input/input_state.h index 378a4ba98b..eb16a81bfa 100644 --- a/input/input_state.h +++ b/input/input_state.h @@ -8,12 +8,13 @@ // For more detailed and configurable input, implement NativeTouch, NativeKey and NativeAxis and do your // own mapping. Might later move the mapping system from PPSSPP to native. +#include +#include + #include "math/lin/vec3.h" #include "base/mutex.h" #include "base/basictypes.h" #include "input/keycodes.h" -#include -#include // Default device IDs @@ -113,11 +114,19 @@ void UpdateInputState(InputState *input, bool merge = false); void EndInputState(InputState *input); enum { - TOUCH_MOVE = 1, - TOUCH_DOWN = 2, - TOUCH_UP = 4, - TOUCH_CANCEL = 8, // Sent by scrollviews to their children when they detect a scroll - TOUCH_WHEEL = 16, // Scrollwheel event. Usually only affects Y. + TOUCH_MOVE = 1 << 0, + TOUCH_DOWN = 1 << 1, + TOUCH_UP = 1 << 2, + TOUCH_CANCEL = 1 << 3, // Sent by scrollviews to their children when they detect a scroll + TOUCH_WHEEL = 1 << 4, // Scrollwheel event. Usually only affects Y. + + // These are the Android getToolType() codes, shifted by 10. + TOUCH_TOOL_MASK = 7 << 10, + TOUCH_TOOL_UNKNOWN = 0 << 10, + TOUCH_TOOL_FINGER = 1 << 10, + TOUCH_TOOL_STYLUS = 2 << 10, + TOUCH_TOOL_MOUSE = 3 << 10, + TOUCH_TOOL_ERASER = 4 << 10, }; // Used for asynchronous touch input. @@ -135,9 +144,9 @@ struct TouchInput { #undef KEY_UP enum { - KEY_DOWN = 1, - KEY_UP = 2, - KEY_HASWHEELDELTA = 4, + KEY_DOWN = 1 << 0, + KEY_UP = 1 << 1, + KEY_HASWHEELDELTA = 1 << 2, }; struct KeyInput {