Skip to content

Commit

Permalink
Merge pull request #5309 from Susko3/fix-windows-ink
Browse files Browse the repository at this point in the history
Fix Windows Ink events being handled as touch events
  • Loading branch information
peppy committed Dec 27, 2023
2 parents deae25b + 23870c3 commit 71610ef
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
37 changes: 37 additions & 0 deletions osu.Framework/Platform/SDL2/SDL2Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,5 +1120,42 @@ public static string GetAndClearError()
SDL.SDL_ClearError();
return error;
}

private static bool tryGetTouchDeviceIndex(long touchId, out int index)
{
int n = SDL.SDL_GetNumTouchDevices();

for (int i = 0; i < n; i++)
{
long currentTouchId = SDL.SDL_GetTouchDevice(i);

if (touchId == currentTouchId)
{
index = i;
return true;
}
}

index = -1;
return false;
}

/// <summary>
/// Gets the <paramref name="name"/> of the touch device for this <see cref="SDL.SDL_TouchFingerEvent"/>.
/// </summary>
/// <remarks>
/// On Windows, this will return <c>"touch"</c> for touchscreen events or <c>"pen"</c> for pen/tablet events.
/// </remarks>
public static bool TryGetTouchName(this SDL.SDL_TouchFingerEvent e, out string name)
{
if (tryGetTouchDeviceIndex(e.touchId, out int index))
{
name = SDL.SDL_GetTouchName(index);
return name != null;
}

name = null;
return false;
}
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Platform/SDL2Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ protected virtual void HandleEvent(SDL.SDL_Event e)
case SDL.SDL_EventType.SDL_FINGERDOWN:
case SDL.SDL_EventType.SDL_FINGERUP:
case SDL.SDL_EventType.SDL_FINGERMOTION:
handleTouchFingerEvent(e.tfinger);
HandleTouchFingerEvent(e.tfinger);
break;

case SDL.SDL_EventType.SDL_DROPFILE:
Expand Down
14 changes: 9 additions & 5 deletions osu.Framework/Platform/SDL2Window_Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,11 @@ private void handleDropEvent(SDL.SDL_DropEvent evtDrop)
return null;
}

private void handleTouchFingerEvent(SDL.SDL_TouchFingerEvent evtTfinger)
protected virtual void HandleTouchFingerEvent(SDL.SDL_TouchFingerEvent evtTfinger)
{
var eventType = (SDL.SDL_EventType)evtTfinger.type;

var existingSource = getTouchSource(evtTfinger.fingerId);

if (eventType == SDL.SDL_EventType.SDL_FINGERDOWN)
if (evtTfinger.type == SDL.SDL_EventType.SDL_FINGERDOWN)
{
Debug.Assert(existingSource == null);
existingSource = assignNextAvailableTouchSource(evtTfinger.fingerId);
Expand All @@ -255,7 +253,7 @@ private void handleTouchFingerEvent(SDL.SDL_TouchFingerEvent evtTfinger)

var touch = new Touch(existingSource.Value, new Vector2(x, y));

switch (eventType)
switch (evtTfinger.type)
{
case SDL.SDL_EventType.SDL_FINGERDOWN:
case SDL.SDL_EventType.SDL_FINGERMOTION:
Expand Down Expand Up @@ -582,6 +580,8 @@ private void updateConfineMode()
/// </summary>
public event Action<Vector2>? MouseMove;

protected void TriggerMouseMove(float x, float y) => MouseMove?.Invoke(new Vector2(x, y));

/// <summary>
/// Invoked when the user moves the mouse cursor within the window (via relative / raw input).
/// </summary>
Expand All @@ -592,11 +592,15 @@ private void updateConfineMode()
/// </summary>
public event Action<MouseButton>? MouseDown;

protected void TriggerMouseDown(MouseButton button) => MouseDown?.Invoke(button);

/// <summary>
/// Invoked when the user releases a mouse button.
/// </summary>
public event Action<MouseButton>? MouseUp;

protected void TriggerMouseUp(MouseButton button) => MouseUp?.Invoke(button);

/// <summary>
/// Invoked when the user presses a key.
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions osu.Framework/Platform/Windows/WindowsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using osu.Framework.Platform.SDL2;
using osu.Framework.Platform.Windows.Native;
using osuTK;
using osuTK.Input;
using SDL2;
using Icon = osu.Framework.Platform.Windows.Native.Icon;

Expand Down Expand Up @@ -223,6 +224,33 @@ private void handleImeMessage(IntPtr hWnd, uint uMsg, long lParam)

#endregion

protected override void HandleTouchFingerEvent(SDL.SDL_TouchFingerEvent evtTfinger)
{
if (evtTfinger.TryGetTouchName(out string name) && name == "pen")
{
// Windows Ink tablet/pen handling
// InputManager expects to receive this as mouse events, to have proper `mouseSource` input priority (see InputManager.GetPendingInputs)
// osu! expects to get tablet events as mouse events, and touch events as touch events for touch device (TD mod) handling (see https://github.com/ppy/osu/issues/25590)

TriggerMouseMove(evtTfinger.x * ClientSize.Width, evtTfinger.y * ClientSize.Height);

switch (evtTfinger.type)
{
case SDL.SDL_EventType.SDL_FINGERDOWN:
TriggerMouseDown(MouseButton.Left);
break;

case SDL.SDL_EventType.SDL_FINGERUP:
TriggerMouseUp(MouseButton.Left);
break;
}

return;
}

base.HandleTouchFingerEvent(evtTfinger);
}

public override Size Size
{
protected set
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/osu.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="ppy.osuTK.NS20" Version="1.0.211" />
<PackageReference Include="StbiSharp" Version="1.1.0" />
<PackageReference Include="ppy.SDL2-CS" Version="1.0.671-alpha" />
<PackageReference Include="ppy.SDL2-CS" Version="1.0.693-alpha" />
<PackageReference Include="ppy.osu.Framework.SourceGeneration" Version="2023.720.0" />

<!-- DO NOT use ProjectReference for native packaging project.
Expand Down

0 comments on commit 71610ef

Please sign in to comment.