Skip to content

Commit

Permalink
Add centralised logging of unhandled events
Browse files Browse the repository at this point in the history
  • Loading branch information
Susko3 committed Jul 2, 2022
1 parent f8e6e06 commit 7a6cc93
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 45 deletions.
56 changes: 40 additions & 16 deletions osu.Framework.Android/Input/AndroidInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Android.Views;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Input.Handlers;
using osu.Framework.Logging;
using osu.Framework.Platform;

namespace osu.Framework.Android.Input
Expand All @@ -30,6 +31,8 @@ public abstract class AndroidInputHandler : InputHandler
/// </summary>
protected readonly AndroidGameView View;

public override string Description => base.Description.Replace("Android", null);

/// <summary>
/// Bitmask of all <see cref="HandledEventSources"/>.
/// </summary>
Expand Down Expand Up @@ -61,8 +64,9 @@ public override bool Initialize(GameHost host)
/// </summary>
/// <remarks>
/// Subscribe <see cref="HandleCapturedPointer"/> to <see cref="View"/>.<see cref="AndroidGameView.CapturedPointer"/> to receive events here.
/// <returns>Whether the event was handled. Unhandled events are logged.</returns>
/// </remarks>
protected virtual void OnCapturedPointer(MotionEvent capturedPointerEvent)
protected virtual bool OnCapturedPointer(MotionEvent capturedPointerEvent)
{
throw new NotSupportedException($"{nameof(HandleCapturedPointer)} subscribed to {nameof(View.CapturedPointer)} but the relevant method was not overriden.");
}
Expand All @@ -72,8 +76,9 @@ protected virtual void OnCapturedPointer(MotionEvent capturedPointerEvent)
/// </summary>
/// <remarks>
/// Subscribe <see cref="HandleGenericMotion"/> to <see cref="View"/>.<see cref="AndroidGameView.GenericMotion"/> to receive events here.
/// <returns>Whether the event was handled. Unhandled events are logged.</returns>
/// </remarks>
protected virtual void OnGenericMotion(MotionEvent genericMotionEvent)
protected virtual bool OnGenericMotion(MotionEvent genericMotionEvent)
{
throw new NotSupportedException($"{nameof(HandleGenericMotion)} subscribed to {nameof(View.GenericMotion)} but the relevant method was not overriden.");
}
Expand All @@ -83,8 +88,9 @@ protected virtual void OnGenericMotion(MotionEvent genericMotionEvent)
/// </summary>
/// <remarks>
/// Subscribe <see cref="HandleHover"/> to <see cref="View"/>.<see cref="AndroidGameView.Hover"/> to receive events here.
/// <returns>Whether the event was handled. Unhandled events are logged.</returns>
/// </remarks>
protected virtual void OnHover(MotionEvent hoverEvent)
protected virtual bool OnHover(MotionEvent hoverEvent)
{
throw new NotSupportedException($"{nameof(HandleHover)} subscribed to {nameof(View.Hover)} but the relevant method was not overriden.");
}
Expand All @@ -94,8 +100,9 @@ protected virtual void OnHover(MotionEvent hoverEvent)
/// </summary>
/// <remarks>
/// Subscribe <see cref="HandleKeyDown"/> to <see cref="View"/>.<see cref="AndroidGameView.KeyDown"/> to receive events here.
/// <returns>Whether the event was handled. Unhandled events are logged.</returns>
/// </remarks>
protected virtual void OnKeyDown(Keycode keycode, KeyEvent e)
protected virtual bool OnKeyDown(Keycode keycode, KeyEvent e)
{
throw new NotSupportedException($"{nameof(HandleKeyDown)} subscribed to {nameof(View.KeyDown)} but the relevant method was not overriden.");
}
Expand All @@ -105,8 +112,9 @@ protected virtual void OnKeyDown(Keycode keycode, KeyEvent e)
/// </summary>
/// <remarks>
/// Subscribe <see cref="HandleKeyUp"/> to <see cref="View"/>.<see cref="AndroidGameView.KeyUp"/> to receive events here.
/// <returns>Whether the event was handled. Unhandled events are logged.</returns>
/// </remarks>
protected virtual void OnKeyUp(Keycode keycode, KeyEvent e)
protected virtual bool OnKeyUp(Keycode keycode, KeyEvent e)
{
throw new NotSupportedException($"{nameof(HandleKeyUp)} subscribed to {nameof(View.KeyUp)} but the relevant method was not overriden.");
}
Expand All @@ -116,8 +124,9 @@ protected virtual void OnKeyUp(Keycode keycode, KeyEvent e)
/// </summary>
/// <remarks>
/// Subscribe <see cref="HandleTouch"/> to <see cref="View"/>.<see cref="AndroidGameView.Touch"/> to receive events here.
/// <returns>Whether the event was handled. Unhandled events are logged.</returns>
/// </remarks>
protected virtual void OnTouch(MotionEvent touchEvent)
protected virtual bool OnTouch(MotionEvent touchEvent)
{
throw new NotSupportedException($"{nameof(HandleTouch)} subscribed to {nameof(View.Touch)} but the relevant method was not overriden.");
}
Expand All @@ -142,8 +151,10 @@ protected void HandleCapturedPointer(object sender, View.CapturedPointerEventArg
{
if (ShouldHandleEvent(e.Event))
{
OnCapturedPointer(e.Event);
e.Handled = true;
if (OnCapturedPointer(e.Event))
e.Handled = true;
else
logUnhandledEvent(nameof(OnCapturedPointer), e.Event);
}
}

Expand All @@ -154,8 +165,10 @@ protected void HandleGenericMotion(object sender, View.GenericMotionEventArgs e)
{
if (ShouldHandleEvent(e.Event))
{
OnGenericMotion(e.Event);
e.Handled = true;
if (OnGenericMotion(e.Event))
e.Handled = true;
else
logUnhandledEvent(nameof(OnGenericMotion), e.Event);
}
}

Expand All @@ -166,8 +179,10 @@ protected void HandleHover(object sender, View.HoverEventArgs e)
{
if (ShouldHandleEvent(e.Event))
{
OnHover(e.Event);
e.Handled = true;
if (OnHover(e.Event))
e.Handled = true;
else
logUnhandledEvent(nameof(OnHover), e.Event);
}
}

Expand All @@ -178,7 +193,8 @@ protected void HandleKeyDown(Keycode keycode, KeyEvent e)
{
if (ShouldHandleEvent(e))
{
OnKeyDown(keycode, e);
if (!OnKeyDown(keycode, e))
logUnhandledEvent(nameof(OnKeyDown), e);
}
}

Expand All @@ -189,7 +205,8 @@ protected void HandleKeyUp(Keycode keycode, KeyEvent e)
{
if (ShouldHandleEvent(e))
{
OnKeyUp(keycode, e);
if (!OnKeyUp(keycode, e))
logUnhandledEvent(nameof(OnKeyUp), e);
}
}

Expand All @@ -200,11 +217,18 @@ protected void HandleTouch(object sender, View.TouchEventArgs e)
{
if (ShouldHandleEvent(e.Event))
{
OnTouch(e.Event);
e.Handled = true;
if (OnTouch(e.Event))
e.Handled = true;
else
logUnhandledEvent(nameof(OnTouch), e.Event);
}
}

#endregion

private void logUnhandledEvent(string methodName, InputEvent inputEvent)
{
Logger.Log($"Unknown {Description} {methodName} event: {inputEvent}");
}
}
}
23 changes: 17 additions & 6 deletions osu.Framework.Android/Input/AndroidJoystickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,26 @@ public override bool Initialize(GameHost host)
/// <remarks>See xmldoc <see cref="AndroidKeyboardHandler.ShouldHandleEvent"/></remarks>
protected override bool ShouldHandleEvent(InputEvent? inputEvent) => base.ShouldHandleEvent(inputEvent) && inputEvent.Source != InputSourceType.Keyboard;

protected override void OnKeyDown(Keycode keycode, KeyEvent e)
protected override bool OnKeyDown(Keycode keycode, KeyEvent e)
{
if (keycode.TryGetJoystickButton(out var button))
{
enqueueButtonDown(button);
else
Logger.Log($"Unknown joystick keycode: {keycode}");
return true;
}

return false;
}

protected override void OnKeyUp(Keycode keycode, KeyEvent e)
protected override bool OnKeyUp(Keycode keycode, KeyEvent e)
{
if (keycode.TryGetJoystickButton(out var button))
{
enqueueButtonUp(button);
return true;
}

return false;
}

/// <summary>
Expand Down Expand Up @@ -142,14 +150,17 @@ bool isValid(Axis axis)
}
}

protected override void OnGenericMotion(MotionEvent genericMotionEvent)
protected override bool OnGenericMotion(MotionEvent genericMotionEvent)
{
switch (genericMotionEvent.Action)
{
case MotionEventActions.Move:
updateAvailableAxesForDevice(genericMotionEvent.Device);
genericMotionEvent.HandleHistorically(apply);
break;
return true;

default:
return false;
}
}

Expand Down
8 changes: 6 additions & 2 deletions osu.Framework.Android/Input/AndroidKeyboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,24 @@ public override bool Initialize(GameHost host)
/// </remarks>
protected override bool ShouldHandleEvent(InputEvent? inputEvent) => base.ShouldHandleEvent(inputEvent) && !inputEvent.Source.HasFlagFast(InputSourceType.Gamepad);

protected override void OnKeyDown(Keycode keycode, KeyEvent e)
protected override bool OnKeyDown(Keycode keycode, KeyEvent e)
{
var key = GetKeyCodeAsKey(keycode);

if (key != Key.Unknown)
enqueueInput(new KeyboardKeyInput(key, true));

return key != Key.Unknown;
}

protected override void OnKeyUp(Keycode keycode, KeyEvent e)
protected override bool OnKeyUp(Keycode keycode, KeyEvent e)
{
var key = GetKeyCodeAsKey(keycode);

if (key != Key.Unknown)
enqueueInput(new KeyboardKeyInput(key, false));

return key != Key.Unknown;
}

/// <summary>
Expand Down
65 changes: 52 additions & 13 deletions osu.Framework.Android/Input/AndroidMouseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,70 +136,109 @@ private void updatePointerCapture()
View.PointerCapture = shouldCapture;
}

protected override void OnKeyDown(Keycode keycode, KeyEvent e)
protected override bool OnKeyDown(Keycode keycode, KeyEvent e)
{
// some implementations might send Mouse1 and Mouse2 as keyboard keycodes, so we handle those here.
if (keycode.TryGetMouseButton(out var button))
{
handleMouseButton(button, true);
return true;
}

return false;
}

protected override void OnKeyUp(Keycode keycode, KeyEvent e)
protected override bool OnKeyUp(Keycode keycode, KeyEvent e)
{
if (keycode.TryGetMouseButton(out var button))
{
handleMouseButton(button, false);
return true;
}

return false;
}

protected override void OnHover(MotionEvent hoverEvent)
protected override bool OnHover(MotionEvent hoverEvent)
{
switch (hoverEvent.Action)
{
case MotionEventActions.HoverMove:
handleMouseMoveEvent(hoverEvent);
break;
return true;

// related to the mouse entering/exiting the view,
// and the mouse "losing" hover state as the screen is touched (the mouse pointer disappears)
// no need to log, and no need to handle them in any way here.
case MotionEventActions.HoverEnter:
case MotionEventActions.HoverExit:
return true;

default:
return false;
}
}

protected override void OnTouch(MotionEvent touchEvent)
protected override bool OnTouch(MotionEvent touchEvent)
{
switch (touchEvent.Action)
{
case MotionEventActions.Move:
handleMouseMoveEvent(touchEvent);
break;
return true;

default:
return false;
}
}

protected override void OnGenericMotion(MotionEvent genericMotionEvent)
protected override bool OnGenericMotion(MotionEvent genericMotionEvent)
{
switch (genericMotionEvent.Action)
{
case MotionEventActions.ButtonPress:
case MotionEventActions.ButtonRelease:
handleButtonEvent(genericMotionEvent);
break;
return true;

case MotionEventActions.Scroll:
handleScrollEvent(genericMotionEvent);
break;
return true;

// fired when buttons are pressed, but these don't have reliable ActionButton information
case MotionEventActions.Up:
case MotionEventActions.Down:
return true;

default:
return false;
}
}

protected override void OnCapturedPointer(MotionEvent capturedPointerEvent)
protected override bool OnCapturedPointer(MotionEvent capturedPointerEvent)
{
switch (capturedPointerEvent.Action)
{
case MotionEventActions.Move:
handleMouseMoveRelativeEvent(capturedPointerEvent);
break;
return true;

case MotionEventActions.Scroll:
handleScrollEvent(capturedPointerEvent);
break;
return true;

case MotionEventActions.ButtonPress:
case MotionEventActions.ButtonRelease:
handleButtonEvent(capturedPointerEvent);
break;
return true;

// fired when buttons are pressed, but these don't have reliable ActionButton information
case MotionEventActions.Up:
case MotionEventActions.Down:
return true;

default:
return false;
}
}

Expand Down
Loading

0 comments on commit 7a6cc93

Please sign in to comment.