Proper mouse/stylus support in Gingerbread and ICS

This commit is contained in:
pelya
2012-06-13 19:35:29 +03:00
parent 77a16225aa
commit dd67bba742
5 changed files with 172 additions and 72 deletions

View File

@@ -89,7 +89,10 @@ class Mouse
abstract class DifferentTouchInput
{
public static boolean ExternalMouseDetected = true;
public abstract void process(final MotionEvent event);
public abstract void processGenericEvent(final MotionEvent event);
public static boolean ExternalMouseDetected = false;
public static DifferentTouchInput getInstance()
{
@@ -105,7 +108,9 @@ abstract class DifferentTouchInput
multiTouchAvailable2 = true;
}
try {
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD )
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH && IcsTouchInput.Holder.sInstance != null )
return IcsTouchInput.Holder.sInstance;
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD && XperiaPlayTouchpadTouchInput.Holder.sInstance != null )
return XperiaPlayTouchpadTouchInput.Holder.sInstance;
if (multiTouchAvailable1 && multiTouchAvailable2)
return MultiTouchInput.Holder.sInstance;
@@ -122,8 +127,6 @@ abstract class DifferentTouchInput
}
}
}
public abstract void process(final MotionEvent event);
public abstract void processGenericEvent(final MotionEvent event);
private static class SingleTouchInput extends DifferentTouchInput
{
private static class Holder
@@ -145,14 +148,13 @@ abstract class DifferentTouchInput
if( event.getAction() == MotionEvent.ACTION_MOVE )
action = Mouse.SDL_FINGER_MOVE;
if ( action >= 0 )
DemoGLSurfaceView.nativeMouse( (int)event.getX(), (int)event.getY(), action, 0,
DemoGLSurfaceView.nativeMotionEvent( (int)event.getX(), (int)event.getY(), action, 0,
(int)(event.getPressure() * 1000.0),
(int)(event.getSize() * 1000.0) );
}
}
private static class MultiTouchInput extends DifferentTouchInput
{
public static final int TOUCH_EVENTS_MAX = 16; // Max multitouch pointers
private class touchEvent
@@ -196,7 +198,7 @@ abstract class DifferentTouchInput
if( touchEvents[i].down )
{
touchEvents[i].down = false;
DemoGLSurfaceView.nativeMouse( touchEvents[i].x, touchEvents[i].y, action, i, touchEvents[i].pressure, touchEvents[i].size );
DemoGLSurfaceView.nativeMotionEvent( touchEvents[i].x, touchEvents[i].y, action, i, touchEvents[i].pressure, touchEvents[i].size );
}
}
}
@@ -213,7 +215,7 @@ abstract class DifferentTouchInput
touchEvents[id].y = (int)event.getY(i);
touchEvents[id].pressure = (int)(event.getPressure(i) * 1000.0);
touchEvents[id].size = (int)(event.getSize(i) * 1000.0);
DemoGLSurfaceView.nativeMouse( touchEvents[id].x, touchEvents[id].y, action, id, touchEvents[id].pressure, touchEvents[id].size );
DemoGLSurfaceView.nativeMotionEvent( touchEvents[id].x, touchEvents[id].y, action, id, touchEvents[id].pressure, touchEvents[id].size );
}
}
if( (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_MOVE ||
@@ -247,7 +249,7 @@ abstract class DifferentTouchInput
{
action = Mouse.SDL_FINGER_UP;
touchEvents[id].down = false;
DemoGLSurfaceView.nativeMouse( touchEvents[id].x, touchEvents[id].y, action, id, touchEvents[id].pressure, touchEvents[id].size );
DemoGLSurfaceView.nativeMotionEvent( touchEvents[id].x, touchEvents[id].y, action, id, touchEvents[id].pressure, touchEvents[id].size );
}
}
else
@@ -270,20 +272,12 @@ abstract class DifferentTouchInput
touchEvents[id].y = (int)event.getY(ii);
touchEvents[id].pressure = (int)(event.getPressure(ii) * 1000.0);
touchEvents[id].size = (int)(event.getSize(ii) * 1000.0);
DemoGLSurfaceView.nativeMouse( touchEvents[id].x, touchEvents[id].y, action, id, touchEvents[id].pressure, touchEvents[id].size );
DemoGLSurfaceView.nativeMotionEvent( touchEvents[id].x, touchEvents[id].y, action, id, touchEvents[id].pressure, touchEvents[id].size );
}
}
}
if( (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_HOVER_MOVE ) // Support bluetooth/USB mouse - available since Android 3.1
{
/*
if( !ExternalMouseDetected )
{
ExternalMouseDetected = true;
Settings.nativeSetExternalMouseDetected();
Toast.makeText(MainActivity.instance, R.string.hardware_mouse_detected, Toast.LENGTH_SHORT).show();
}
*/
// TODO: it is possible that multiple pointers return that event, but we're handling only pointer #0
if( touchEvents[0].down )
action = Mouse.SDL_FINGER_UP;
@@ -294,7 +288,7 @@ abstract class DifferentTouchInput
touchEvents[0].y = (int)event.getY();
touchEvents[0].pressure = 0;
touchEvents[0].size = 0;
DemoGLSurfaceView.nativeMouse( touchEvents[0].x, touchEvents[0].y, action, 0, touchEvents[0].pressure, touchEvents[0].size );
DemoGLSurfaceView.nativeMotionEvent( touchEvents[0].x, touchEvents[0].y, action, 0, touchEvents[0].pressure, touchEvents[0].size );
}
}
}
@@ -343,19 +337,20 @@ abstract class DifferentTouchInput
minRange = Math.min( Math.abs(ymax - ymin), Math.abs(xmax - xmin) );
}
}
public void process(final MotionEvent event)
{
boolean hwMouseEvent = ( event.getSource() == InputDevice.SOURCE_MOUSE || event.getSource() == InputDevice.SOURCE_STYLUS );
if( ExternalMouseDetected != hwMouseEvent )
{
ExternalMouseDetected = hwMouseEvent;
DemoGLSurfaceView.nativeHardwareMouseDetected(hwMouseEvent ? 1 : 0);
}
super.process(event);
}
public void processGenericEvent(final MotionEvent event)
{
if( event.getSource() != InputDevice.SOURCE_TOUCHPAD )
{
/*
if( !ExternalMouseDetected &&
( event.getSource() == InputDevice.SOURCE_MOUSE || event.getSource() == InputDevice.SOURCE_STYLUS ) )
{
ExternalMouseDetected = true;
Settings.nativeSetExternalMouseDetected();
Toast.makeText(MainActivity.instance, R.string.hardware_mouse_detected, Toast.LENGTH_SHORT).show();
}
*/
process(event);
return;
}
@@ -385,6 +380,24 @@ abstract class DifferentTouchInput
DemoGLSurfaceView.nativeTouchpad( x, 65535-y, down, multitouch ); // Y axis is inverted, as you may have guessed
}
}
private static class IcsTouchInput extends XperiaPlayTouchpadTouchInput
{
private static class Holder
{
private static final IcsTouchInput sInstance = new IcsTouchInput();
}
private int buttonState = 0;
public void process(final MotionEvent event)
{
if( event.getButtonState() != buttonState )
{
buttonState = event.getButtonState();
//System.out.println("IcsTouchInput: button state " + buttonState);
DemoGLSurfaceView.nativeMouseButtonsPressed(buttonState);
}
super.process(event);
}
}
}
@@ -436,7 +449,6 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
MainActivity.LoadApplicationLibrary(context);
Settings.Apply(context);
DifferentTouchInput.ExternalMouseDetected = false;
accelerometer = new AccelerometerReader(context);
// Tweak video thread priority, if user selected big audio buffer
if(Globals.AudioBufferConfig >= 2)
@@ -676,10 +688,12 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
MainActivity mParent;
DifferentTouchInput touchInput = null;
public static native void nativeMouse( int x, int y, int action, int pointerId, int pressure, int radius );
public static native void nativeMotionEvent( int x, int y, int action, int pointerId, int pressure, int radius );
public static native int nativeKey( int keyCode, int down );
public static native void nativeTouchpad( int x, int y, int down, int multitouch );
public static native void initJavaCallbacks();
public static native void nativeHardwareMouseDetected( int detected );
public static native void nativeMouseButtonsPressed( int buttons );
}