SDL: capture mouse input on Android O, so notification bar won't pop up every time mouse cursor hits top of screen

This commit is contained in:
Sergii Pylypenko
2020-02-26 21:28:38 +02:00
parent 2133b4bdbf
commit ddd9b70af3
4 changed files with 125 additions and 92 deletions

View File

@@ -453,14 +453,13 @@ public class MainActivity extends Activity
{
_videoLayout.addView(mGLView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
}
mGLView.setFocusableInTouchMode(true);
mGLView.setFocusable(true);
mGLView.requestFocus();
if (Globals.HideSystemMousePointer && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
mGLView.captureMouse(true);
if( Globals.HideSystemMousePointer && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N )
{
mGLView.setPointerIcon(android.view.PointerIcon.getSystemIcon(this, android.view.PointerIcon.TYPE_NULL));
}
if( _ad.getView() != null )
{
_videoLayout.addView(_ad.getView());
@@ -553,9 +552,13 @@ public class MainActivity extends Activity
super.onWindowFocusChanged(hasFocus);
Log.i("SDL", "libSDL: onWindowFocusChanged: " + hasFocus + " - sending onPause/onResume");
if (hasFocus == false)
{
onPause();
}
else
{
onResume();
}
}
public boolean isPaused()
@@ -618,6 +621,7 @@ public class MainActivity extends Activity
{
public void run()
{
mGLView.captureMouse(false);
if (keyboard == 0)
{
_inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
@@ -798,6 +802,7 @@ public class MainActivity extends Activity
_inputManager.hideSoftInputFromWindow(mGLView.getWindowToken(), 0);
DimSystemStatusBar.get().dim(_videoLayout);
//DimSystemStatusBar.get().dim(mGLView);
mGLView.captureMouse(true);
}
});
}
@@ -809,6 +814,7 @@ public class MainActivity extends Activity
if(Globals.CompatibilityHacksTextInputEmulatesHwKeyboard)
{
showScreenKeyboardWithoutTextInputField(Globals.TextInputKeyboard);
mGLView.captureMouse(false);
return;
}
if(_screenKeyboard != null)
@@ -882,6 +888,7 @@ public class MainActivity extends Activity
screenKeyboard.setInputType(InputType.TYPE_CLASS_TEXT);
screenKeyboard.setFocusableInTouchMode(true);
screenKeyboard.setFocusable(true);
mGLView.captureMouse(false);
//_inputManager.showSoftInput(screenKeyboard, InputMethodManager.SHOW_IMPLICIT);
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// Hack to try to force on-screen keyboard
@@ -910,7 +917,10 @@ public class MainActivity extends Activity
public void hideScreenKeyboard()
{
if( keyboardWithoutTextInputShown )
{
showScreenKeyboardWithoutTextInputField(Globals.TextInputKeyboard);
mGLView.captureMouse(true);
}
if(_screenKeyboard == null || ! (_screenKeyboard instanceof EditText))
return;
@@ -927,9 +937,7 @@ public class MainActivity extends Activity
_inputManager.hideSoftInputFromWindow(_screenKeyboard.getWindowToken(), 0);
_videoLayout.removeView(_screenKeyboard);
_screenKeyboard = null;
mGLView.setFocusableInTouchMode(true);
mGLView.setFocusable(true);
mGLView.requestFocus();
mGLView.captureMouse(true);
DimSystemStatusBar.get().dim(_videoLayout);
_videoLayout.postDelayed( new Runnable()
@@ -945,7 +953,7 @@ public class MainActivity extends Activity
{
return _screenKeyboard != null;
};
public void setScreenKeyboardHintMessage(String s)
{
_screenKeyboardHintMessage = s;
@@ -1051,79 +1059,6 @@ public class MainActivity extends Activity
}
}
/*
@Override
public boolean onKeyDown(int keyCode, final KeyEvent event)
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE )
{
// Stupid Samsung and stupid Acer remaps right mouse button to BACK key
DemoGLSurfaceView.nativeMouseButtonsPressed(2, 1);
return true;
}
else if( keyboardWithoutTextInputShown )
{
return true;
}
}
if( _screenKeyboard != null && _screenKeyboard.onKeyDown(keyCode, event) )
return true;
if( mGLView != null )
{
if( mGLView.nativeKey( keyCode, 1, event.getUnicodeChar() ) == 0 )
return super.onKeyDown(keyCode, event);
}
else
if( keyListener != null )
{
keyListener.onKeyEvent(keyCode);
}
else
if( _btn != null )
return _btn.onKeyDown(keyCode, event);
return true;
}
@Override
public boolean onKeyUp(int keyCode, final KeyEvent event)
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE )
{
// Stupid Samsung and stupid Acer remaps right mouse button to BACK key
DemoGLSurfaceView.nativeMouseButtonsPressed(2, 0);
return true;
}
else if( keyboardWithoutTextInputShown )
{
showScreenKeyboardWithoutTextInputField(0); // Hide keyboard
return true;
}
}
if( _screenKeyboard != null && _screenKeyboard.onKeyUp(keyCode, event) )
return true;
if( mGLView != null )
{
if( mGLView.nativeKey( keyCode, 0, event.getUnicodeChar() ) == 0 )
return super.onKeyUp(keyCode, event);
if( keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU )
{
DimSystemStatusBar.get().dim(_videoLayout);
//DimSystemStatusBar.get().dim(mGLView);
}
}
else
if( _btn != null )
return _btn.onKeyUp(keyCode, event);
return true;
}
*/
//private Configuration oldConfig = null;
@Override
public void onConfigurationChanged(Configuration newConfig)

View File

@@ -108,6 +108,10 @@ abstract class DifferentTouchInput
public abstract void processGenericEvent(final MotionEvent event);
public static int ExternalMouseDetected = Mouse.MOUSE_HW_INPUT_FINGER;
public static int buttonState = 0;
public static float capturedMouseX = 0;
public static float capturedMouseY = 0;
public static DifferentTouchInput touchInput = getInstance();
@@ -319,6 +323,12 @@ abstract class DifferentTouchInput
int hwMouseEvent = ((event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE || Globals.ForceHardwareMouse) ? Mouse.MOUSE_HW_INPUT_MOUSE :
((event.getSource() & InputDevice.SOURCE_STYLUS) == InputDevice.SOURCE_STYLUS) ? Mouse.MOUSE_HW_INPUT_STYLUS :
Mouse.MOUSE_HW_INPUT_FINGER;
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE_RELATIVE) == InputDevice.SOURCE_MOUSE_RELATIVE )
hwMouseEvent = Mouse.MOUSE_HW_INPUT_MOUSE;
}
if( ExternalMouseDetected != hwMouseEvent )
{
ExternalMouseDetected = hwMouseEvent;
@@ -366,7 +376,6 @@ abstract class DifferentTouchInput
{
private static final IcsTouchInput sInstance = new IcsTouchInput();
}
private int buttonState = 0;
public void process(final MotionEvent event)
{
//Log.i("SDL", "Got motion event, type " + (int)(event.getAction()) + " X " + (int)event.getX() + " Y " + (int)event.getY() + " buttons " + buttonState + " source " + event.getSource());
@@ -1045,9 +1054,18 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
@Override
public boolean onKeyDown(int keyCode, final KeyEvent event)
{
//Log.v("SDL", "DemoGLSurfaceView::onKeyDown(): keyCode " + keyCode + " event.getSource() " + event.getSource());
if( keyCode == KeyEvent.KEYCODE_BACK )
{
boolean mouseInput = false;
if( (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE )
mouseInput = true;
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE_RELATIVE) == InputDevice.SOURCE_MOUSE_RELATIVE )
mouseInput = true;
}
if( mouseInput )
{
// Stupid Samsung and stupid Acer remaps right mouse button to BACK key
nativeMouseButtonsPressed(2, 1);
@@ -1070,7 +1088,15 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
boolean mouseInput = false;
if( (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE )
mouseInput = true;
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE_RELATIVE) == InputDevice.SOURCE_MOUSE_RELATIVE )
mouseInput = true;
}
if( mouseInput )
{
// Stupid Samsung and stupid Acer remaps right mouse button to BACK key
nativeMouseButtonsPressed(2, 0);
@@ -1097,8 +1123,8 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
{
if( event.getCharacters() != null )
{
// International text input
for(int i = 0; i < event.getCharacters().length(); i++ )
// Non-English text input
for( int i = 0; i < event.getCharacters().length(); i++ )
{
nativeKey( event.getKeyCode(), 1, event.getCharacters().codePointAt(i), 0 );
nativeKey( event.getKeyCode(), 0, event.getCharacters().codePointAt(i), 0 );
@@ -1110,13 +1136,14 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
@Override
public boolean onTouchEvent(final MotionEvent event)
{
if (mParent.keyboardWithoutTextInputShown && mParent._screenKeyboard != null &&
mParent._screenKeyboard.getY() <= event.getY()) {
if( mParent.keyboardWithoutTextInputShown && mParent._screenKeyboard != null &&
mParent._screenKeyboard.getY() <= event.getY() )
{
event.offsetLocation(-mParent._screenKeyboard.getX(), -mParent._screenKeyboard.getY());
mParent._screenKeyboard.onTouchEvent(event);
return true;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH )
{
if (getX() != 0)
event.offsetLocation(-getX(), -getY());
@@ -1139,7 +1166,42 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
}
return true;
}
@Override
public boolean onCapturedPointerEvent (final MotionEvent event)
{
DifferentTouchInput.capturedMouseX += event.getX();
DifferentTouchInput.capturedMouseY += event.getY();
if (DifferentTouchInput.capturedMouseX < 0)
DifferentTouchInput.capturedMouseX = 0;
if (DifferentTouchInput.capturedMouseY < 0)
DifferentTouchInput.capturedMouseY = 0;
if (DifferentTouchInput.capturedMouseX >= this.getWidth())
DifferentTouchInput.capturedMouseX = this.getWidth() - 1;
if (DifferentTouchInput.capturedMouseY >= this.getHeight())
DifferentTouchInput.capturedMouseY = this.getHeight() - 1;
//Log.v("SDL", "DemoGLSurfaceView::onCapturedPointerEvent(): X " + DifferentTouchInput.capturedMouseX + " Y " + DifferentTouchInput.capturedMouseY +
// " W " + this.getWidth() + " H " + this.getHeight() + " getX " + event.getX() + " getY " + event.getY() +
// " RelX " + event.getAxisValue(MotionEvent.AXIS_RELATIVE_X) + " RelY " + event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y) );
event.setLocation(DifferentTouchInput.capturedMouseX, DifferentTouchInput.capturedMouseY);
event.setAction(MotionEvent.ACTION_HOVER_MOVE);
//Log.v("SDL", "DemoGLSurfaceView::onCapturedPointerEvent(): XY " + event.getX() + " " + event.getY() + " action " + event.getAction());
return this.onTouchEvent(event);
}
@Override
public void onPointerCaptureChange (boolean hasCapture)
{
Log.v("SDL", "DemoGLSurfaceView::onPointerCaptureChange(): " + hasCapture);
super.onPointerCaptureChange(hasCapture);
DifferentTouchInput.capturedMouseX = this.getWidth() / 2;
DifferentTouchInput.capturedMouseY = this.getHeight() / 2;
}
public void limitEventRate(final MotionEvent event)
{
// Wait a bit, and try to synchronize to app framerate, or event thread will eat all CPU and we'll lose FPS
@@ -1193,8 +1255,44 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
mRenderer.nativeGlContextRecreated();
if( mRenderer.accelerometer != null && mRenderer.accelerometer.openedBySDL ) // For some reason it crashes here often - are we getting this event before initialization?
mRenderer.accelerometer.start();
captureMouse(true);
};
public void captureMouse(boolean capture)
{
if( capture )
{
setFocusableInTouchMode(true);
setFocusable(true);
requestFocus();
if( Globals.HideSystemMousePointer && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O )
{
postDelayed( new Runnable()
{
public void run()
{
Log.v("SDL", "captureMouse::requestPointerCapture() delayed");
requestPointerCapture();
}
}, 50 );
}
}
else
{
if( Globals.HideSystemMousePointer && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O )
{
postDelayed( new Runnable()
{
public void run()
{
Log.v("SDL", "captureMouse::releasePointerCapture()");
releasePointerCapture();
}
}, 50 );
}
}
}
DemoRenderer mRenderer;
MainActivity mParent;