Skip to content

Commit

Permalink
Merge pull request #6087 from Susko3/better-input-handler-statistics
Browse files Browse the repository at this point in the history
Add basic global statistics for input handler events
  • Loading branch information
smoogipoo authored Dec 26, 2023
2 parents ba2f83f + 0c0b3d1 commit a86dfaa
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 3 deletions.
3 changes: 3 additions & 0 deletions osu.Framework.Android/Input/AndroidJoystickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace osu.Framework.Android.Input
{
public class AndroidJoystickHandler : AndroidInputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<AndroidJoystickHandler>(), "Total events");

public BindableFloat DeadzoneThreshold { get; } = new BindableFloat(0.1f)
{
MinValue = 0,
Expand Down Expand Up @@ -223,6 +225,7 @@ private void enqueueInput(IInput input)
{
PendingInputs.Enqueue(input);
FrameStatistics.Increment(StatisticsCounterType.JoystickEvents);
statistic_total_events.Value++;
}
}
}
3 changes: 3 additions & 0 deletions osu.Framework.Android/Input/AndroidKeyboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace osu.Framework.Android.Input
{
public class AndroidKeyboardHandler : AndroidInputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<AndroidKeyboardHandler>(), "Total events");

protected override IEnumerable<InputSourceType> HandledEventSources => new[]
{
InputSourceType.Keyboard,
Expand Down Expand Up @@ -209,6 +211,7 @@ private void enqueueInput(IInput input)
{
PendingInputs.Enqueue(input);
FrameStatistics.Increment(StatisticsCounterType.KeyEvents);
statistic_total_events.Value++;
}
}
}
3 changes: 3 additions & 0 deletions osu.Framework.Android/Input/AndroidMouseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace osu.Framework.Android.Input
/// </summary>
public class AndroidMouseHandler : AndroidInputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<AndroidMouseHandler>(), "Total events");

/// <summary>
/// Whether relative mode should be preferred when the window has focus, the cursor is contained and the OS cursor is not visible.
/// </summary>
Expand Down Expand Up @@ -308,6 +310,7 @@ private void enqueueInput(IInput input)
{
PendingInputs.Enqueue(input);
FrameStatistics.Increment(StatisticsCounterType.MouseEvents);
statistic_total_events.Value++;
}
}
}
9 changes: 9 additions & 0 deletions osu.Framework.Android/Input/AndroidTouchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace osu.Framework.Android.Input
{
public class AndroidTouchHandler : AndroidInputHandler
{
private static readonly GlobalStatistic<ulong> statistic_touch_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<AndroidTouchHandler>(), "Touch events");
private static readonly GlobalStatistic<ulong> statistic_hover_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<AndroidTouchHandler>(), "Hover events");

public override bool IsActive => true;

protected override IEnumerable<InputSourceType> HandledEventSources => new[] { InputSourceType.BluetoothStylus, InputSourceType.Stylus, InputSourceType.Touchscreen };
Expand Down Expand Up @@ -87,14 +90,20 @@ protected override bool OnHover(MotionEvent hoverEvent)
void apply(MotionEvent e, int historyPosition)
{
if (tryGetEventPosition(e, historyPosition, 0, out var position))
{
enqueueInput(new MousePositionAbsoluteInput { Position = position });
statistic_hover_events.Value++;
}
}
}

private void applyTouchInput(MotionEvent touchEvent, int historyPosition, int pointerIndex)
{
if (tryGetEventTouch(touchEvent, historyPosition, pointerIndex, out var touch))
{
enqueueInput(new TouchInput(touch, touchEvent.ActionMasked.IsTouchDownAction()));
statistic_touch_events.Value++;
}
}

private bool tryGetEventTouch(MotionEvent motionEvent, int historyPosition, int pointerIndex, out Touch touch)
Expand Down
13 changes: 13 additions & 0 deletions osu.Framework/Input/Handlers/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Input.StateChanges;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Statistics;

namespace osu.Framework.Input.Handlers
{
public abstract class InputHandler : IDisposable, IHasDescription
{
/// <summary>
/// Base category to use for input-related <see cref="GlobalStatistics"/>.
/// </summary>
public const string STATISTIC_GROUP = "Input";

/// <summary>
/// Gets the appropriate statistic group for use in <see cref="GlobalStatistics.Get{T}"/>.
/// </summary>
/// <typeparam name="T">Calling class</typeparam>
protected static string StatisticGroupFor<T>() where T : InputHandler => $"{STATISTIC_GROUP} - {typeof(T).ReadableName()}";

private static readonly Logger logger = Logger.GetLogger(LoggingTarget.Input);

private bool isInitialized;
Expand Down
3 changes: 3 additions & 0 deletions osu.Framework/Input/Handlers/Joystick/JoystickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace osu.Framework.Input.Handlers.Joystick
{
public class JoystickHandler : InputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<JoystickHandler>(), "Total events");

public BindableFloat DeadzoneThreshold { get; } = new BindableFloat(0.1f)
{
MinValue = 0,
Expand Down Expand Up @@ -53,6 +55,7 @@ private void enqueueJoystickEvent(IInput evt)
{
PendingInputs.Enqueue(evt);
FrameStatistics.Increment(StatisticsCounterType.JoystickEvents);
statistic_total_events.Value++;
}

private void enqueueJoystickButtonDown(JoystickButton button) => enqueueJoystickEvent(new JoystickButtonInput(button, true));
Expand Down
3 changes: 3 additions & 0 deletions osu.Framework/Input/Handlers/Keyboard/KeyboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace osu.Framework.Input.Handlers.Keyboard
{
public class KeyboardHandler : InputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<KeyboardHandler>(), "Total events");

public override string Description => "Keyboard";

public override bool IsActive => true;
Expand Down Expand Up @@ -43,6 +45,7 @@ private void enqueueInput(IInput input)
{
PendingInputs.Enqueue(input);
FrameStatistics.Increment(StatisticsCounterType.KeyEvents);
statistic_total_events.Value++;
}

private void handleKeyDown(TKKey key) => enqueueInput(new KeyboardKeyInput(key, true));
Expand Down
4 changes: 4 additions & 0 deletions osu.Framework/Input/Handlers/Midi/MidiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace osu.Framework.Input.Handlers.Midi
{
public class MidiHandler : InputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<MidiHandler>(), "Total events");

public override string Description => "MIDI";
public override bool IsActive => inGoodState;

Expand Down Expand Up @@ -251,12 +253,14 @@ void noteOn()
{
PendingInputs.Enqueue(new MidiKeyInput((MidiKey)key, velocity, true));
FrameStatistics.Increment(StatisticsCounterType.MidiEvents);
statistic_total_events.Value++;
}

void noteOff()
{
PendingInputs.Enqueue(new MidiKeyInput((MidiKey)key, 0, false));
FrameStatistics.Increment(StatisticsCounterType.MidiEvents);
statistic_total_events.Value++;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions osu.Framework/Input/Handlers/Mouse/MouseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace osu.Framework.Input.Handlers.Mouse
/// </summary>
public class MouseHandler : InputHandler, IHasCursorSensitivity, INeedsMousePositionFeedback
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<MouseHandler>(), "Total events");

/// <summary>
/// Whether relative mode should be preferred when the window has focus, the cursor is contained and the OS cursor is not visible.
/// </summary>
Expand Down Expand Up @@ -219,6 +221,7 @@ private void enqueueInput(IInput input)
{
PendingInputs.Enqueue(input);
FrameStatistics.Increment(StatisticsCounterType.MouseEvents);
statistic_total_events.Value++;
}

private void transferLastPositionToHostCursor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace osu.Framework.Input.Handlers.Tablet
{
public class OpenTabletDriverHandler : InputHandler, IAbsolutePointer, IRelativePointer, IPressureHandler, ITabletHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<OpenTabletDriverHandler>(), "Total events");

private TabletDriver? tabletDriver;

private InputDeviceTree? device;
Expand Down Expand Up @@ -203,6 +205,7 @@ private void enqueueInput(IInput input)
{
PendingInputs.Enqueue(input);
FrameStatistics.Increment(StatisticsCounterType.TabletEvents);
statistic_total_events.Value++;
}
}
}
3 changes: 3 additions & 0 deletions osu.Framework/Input/Handlers/Touch/TouchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace osu.Framework.Input.Handlers.Touch
{
public class TouchHandler : InputHandler
{
private static readonly GlobalStatistic<ulong> statistic_total_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<TouchHandler>(), "Total events");

public override bool IsActive => true;

public override bool Initialize(GameHost host)
Expand Down Expand Up @@ -50,6 +52,7 @@ private void enqueueTouch(Input.Touch touch, bool activate)
{
PendingInputs.Enqueue(new TouchInput(touch, activate));
FrameStatistics.Increment(StatisticsCounterType.TouchEvents);
statistic_total_events.Value++;
}
}
}
21 changes: 18 additions & 3 deletions osu.Framework/Platform/Windows/WindowsMouseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
using osu.Framework.Input.Handlers.Mouse;
using osu.Framework.Input.StateChanges;
using osu.Framework.Platform.Windows.Native;
using osu.Framework.Statistics;
using osuTK;
using SDL2;

// ReSharper disable UnusedParameter.Local (Class regularly handles native events where we don't consume all parameters)

namespace osu.Framework.Platform.Windows
{
/// <summary>
Expand All @@ -22,6 +21,11 @@ namespace osu.Framework.Platform.Windows
[SupportedOSPlatform("windows")]
internal unsafe class WindowsMouseHandler : MouseHandler
{
private static readonly GlobalStatistic<ulong> statistic_relative_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<WindowsMouseHandler>(), "Relative events");
private static readonly GlobalStatistic<ulong> statistic_absolute_events = GlobalStatistics.Get<ulong>(StatisticGroupFor<WindowsMouseHandler>(), "Absolute events");
private static readonly GlobalStatistic<ulong> statistic_dropped_touch_inputs = GlobalStatistics.Get<ulong>(StatisticGroupFor<WindowsMouseHandler>(), "Dropped native touch inputs");
private static readonly GlobalStatistic<ulong> statistic_inputs_with_extra_information = GlobalStatistics.Get<ulong>(StatisticGroupFor<WindowsMouseHandler>(), "Native inputs with ExtraInformation");

private const int raw_input_coordinate_space = 65535;

private SDL.SDL_WindowsMessageHook callback = null!;
Expand All @@ -35,6 +39,7 @@ public override bool Initialize(GameHost host)
return false;

window = desktopWindow;
// ReSharper disable once ConvertClosureToMethodGroup
callback = (ptr, wnd, u, param, l) => onWndProc(ptr, wnd, u, param, l);

Enabled.BindValueChanged(enabled =>
Expand Down Expand Up @@ -65,9 +70,12 @@ private IntPtr onWndProc(IntPtr userData, IntPtr hWnd, uint message, ulong wPara
return IntPtr.Zero;

if (Native.Input.IsTouchEvent(Native.Input.GetMessageExtraInfo()))
{
// sometimes GetMessageExtraInfo returns 0, so additionally, mouse.ExtraInformation is checked below.
// touch events are handled by TouchHandler
statistic_dropped_touch_inputs.Value++;
return IntPtr.Zero;
}

int payloadSize = sizeof(RawInputData);

Expand All @@ -78,13 +86,18 @@ private IntPtr onWndProc(IntPtr userData, IntPtr hWnd, uint message, ulong wPara

var mouse = data.Mouse;

// `ExtraInformation` doens't have the MI_WP_SIGNATURE set, so we have to rely solely on the touch flag.
// `ExtraInformation` doesn't have the MI_WP_SIGNATURE set, so we have to rely solely on the touch flag.
if (Native.Input.HasTouchFlag(mouse.ExtraInformation))
{
statistic_dropped_touch_inputs.Value++;
return IntPtr.Zero;
}

//TODO: this isn't correct.
if (mouse.ExtraInformation > 0)
{
statistic_inputs_with_extra_information.Value++;

// i'm not sure if there is a valid case where we need to handle packets with this present
// but the osu!tablet fires noise events with non-zero values, which we want to ignore.
// return IntPtr.Zero;
Expand Down Expand Up @@ -129,10 +142,12 @@ private IntPtr onWndProc(IntPtr userData, IntPtr hWnd, uint message, ulong wPara
position *= window.Scale;

PendingInputs.Enqueue(new MousePositionAbsoluteInput { Position = position });
statistic_absolute_events.Value++;
}
else
{
PendingInputs.Enqueue(new MousePositionRelativeInput { Delta = new Vector2(mouse.LastX, mouse.LastY) * sensitivity });
statistic_relative_events.Value++;
}

return IntPtr.Zero;
Expand Down

0 comments on commit a86dfaa

Please sign in to comment.