Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take focus from game during calls on iOS #6462

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion osu.Framework.Tests/Visual/Platform/TestSceneActiveState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osuTK.Graphics;

Expand Down Expand Up @@ -49,7 +50,12 @@ protected override void LoadComplete()
},
};

isActive.BindValueChanged(active => isActiveBox.Colour = active.NewValue ? Color4.Green : Color4.Red, true);
isActive.BindValueChanged(active =>
{
Logger.Log($"Game activity changed from {active.OldValue} to {active.NewValue}");
isActiveBox.Colour = active.NewValue ? Color4.Green : Color4.Red;
}, true);

cursorInWindow?.BindValueChanged(active => cursorInWindowBox.Colour = active.NewValue ? Color4.Green : Color4.Red, true);
}

Expand Down
41 changes: 41 additions & 0 deletions osu.Framework.iOS/IOSCallObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using CallKit;
using Foundation;

namespace osu.Framework.iOS
{
internal class IOSCallObserver : NSObject, ICXCallObserverDelegate
{
public event Action? OnCall;
public event Action? OnCallEnded;

private readonly CXCallController callController;

public IOSCallObserver()
{
callController = new CXCallController();
callController.CallObserver.SetDelegate(this, null);

if (callController.CallObserver.Calls.Any(c => !c.HasEnded))
OnCall?.Invoke();
}

public void CallChanged(CXCallObserver callObserver, CXCall call)
{
if (!call.HasEnded)
OnCall?.Invoke();
else
OnCallEnded?.Invoke();
}

protected override void Dispose(bool disposing)
{
callController.Dispose();
base.Dispose(disposing);
}
}
}
51 changes: 51 additions & 0 deletions osu.Framework.iOS/IOSWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Framework.Platform.SDL3;
using SDL;
using static SDL.SDL3;
using UIKit;

Expand All @@ -22,6 +23,8 @@ internal class IOSWindow : SDL3MobileWindow

public UIWindow UIWindow => uiWindow!;

private IOSCallObserver callObserver = null!;

public override Size Size
{
get => base.Size;
Expand Down Expand Up @@ -50,8 +53,56 @@ public override void Create()

var appDelegate = (GameApplicationDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.DragDrop += TriggerDragDrop;

// osu! cannot operate when a call takes place, as the audio is completely cut from the game, making it behave in unexpected manner.
// while this is o!f code, it's simpler to do this here rather than in osu!.
// we can reconsider this if there are framework consumers which find this behaviour undesirable.
callObserver = new IOSCallObserver();
callObserver.OnCall += onCall;
callObserver.OnCallEnded += onCallEnded;

updateFocused();
}

private bool appInForeground;
private bool inCall;

protected override void HandleEvent(SDL_Event e)
{
switch (e.Type)
{
case SDL_EventType.SDL_EVENT_WINDOW_MINIMIZED:
case SDL_EventType.SDL_EVENT_WINDOW_FOCUS_LOST:
appInForeground = false;
updateFocused();
break;

case SDL_EventType.SDL_EVENT_WINDOW_RESTORED:
case SDL_EventType.SDL_EVENT_WINDOW_FOCUS_GAINED:
appInForeground = true;
updateFocused();
break;

default:
base.HandleEvent(e);
break;
}
}

private void onCall()
{
inCall = true;
updateFocused();
}

private void onCallEnded()
{
inCall = false;
updateFocused();
}

private void updateFocused() => Focused = appInForeground && !inCall;

protected override unsafe void RunMainLoop()
{
// Delegate running the main loop to CADisplayLink.
Expand Down
Loading