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

Make Display nullable in IWindow #6501

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Make Display nullable in IWindow #6501

wants to merge 3 commits into from

Conversation

hwsmm
Copy link
Contributor

@hwsmm hwsmm commented Jan 21, 2025

As explained in the discussion, it may be the case that all monitors are turned off and the window is left alone without any displays. Current osu!framework implementation expects there to be at least one display, and such expectation makes the game crash when there is no display available.

To fix the issue, this PR makes Display nullable in IWindow, and adds some checks to make it less unsafe. However, there still needs to be one display on launch. This PR only lets Display nullable later on.

I understand that making Display nullable sounds not so good, but I think it's less surprising than letting the game crash on a sudden monitor disconnection. I made a contact to the issue reporter, and confirmed that this PR fixes the issue.

Feel free to reject this PR if it doesn't feel right for you.

It's hard to add the test case since it doesn't seem to happen on Windows, and it would require manual intervention (turning off the monitor)... I'd like to hear if there is any idea for this.

hwsmm added 2 commits January 20, 2025 15:20
SDL2 doesn't return bool for most of these functions, so it stays the same there.
@hwsmm
Copy link
Contributor Author

hwsmm commented Jan 21, 2025

Will fix tests soon...

They are all visual tests, so assume that Display is not null.
@Susko3
Copy link
Member

Susko3 commented Jan 21, 2025

If you want to make it not crash, just ensure that the display is never set to null – in case there is no display, just lie to everyone that the last display is still current. Nullable display will complicated game code for no reason.

@hwsmm
Copy link
Contributor Author

hwsmm commented Jan 21, 2025

I did what you suggested before trying this option, but it ended up with bunch of invalid display errors, so I decided to stop all operations regarding to Display until it becomes available again.

Copy link
Member

@Susko3 Susko3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't hate this diff, it does make sense to allow null display.

@@ -85,13 +85,13 @@ private void load(FrameworkConfigManager config, GameHost host)
AddStep("switch to borderless", () => windowMode.Value = WindowMode.Borderless);
AddAssert("check window position", () => new Point(window.Position.X, window.Position.Y) == display.Bounds.Location);
AddAssert("check window size", () => new Size(window.Size.Width, window.Size.Height) == display.Bounds.Size, desc2);
AddAssert("check current screen", () => window.CurrentDisplayBindable.Value.Index == display.Index);
AddAssert("check current screen", () => window.CurrentDisplayBindable.Value!.Index == display.Index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let this fail gracefully, use null forgiving operator. Ditto for all ! in tests.

@@ -388,9 +391,9 @@ private static bool tryGetDisplayFromSDL(int displayIndex, [NotNullWhen(true)] o
/// <summary>
/// Gets the <see cref="Display"/> that has been set as "primary" or "default" in the operating system.
/// </summary>
public virtual Display PrimaryDisplay => Displays.First();
public virtual Display? PrimaryDisplay => Displays.IsEmpty ? null : Displays.First();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public virtual Display? PrimaryDisplay => Displays.IsEmpty ? null : Displays.First();
public virtual Display? PrimaryDisplay => Displays.FirstOrDefault();

Ditto for SDL3.

else
{
Logger.Log($"Failed to get window display bounds. SDL Error: {SDL_GetError()}");
return Rectangle.Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this returns a non-empty rectangle. Maybe a 1x1 at (0, 0)?
SDL2 also needs this error handling.

@peppy peppy self-requested a review January 21, 2025 05:28
Copy link
Member

@peppy peppy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno

@@ -94,7 +94,7 @@ private bool mouseOutsideAllDisplays(Vector2 mousePosition)
break;

default:
windowLocation = Host.Window.CurrentDisplayBindable.Value.Bounds.Location;
windowLocation = Host.Window.CurrentDisplayBindable.Value?.Bounds.Location ?? Host.Window.Position;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks my mind. Does this fallback really work?

@@ -170,12 +170,12 @@ public interface IWindow : IDisposable
/// <summary>
/// Gets the <see cref="Display"/> that has been set as "primary" or "default" in the operating system.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to explain how/when it can be null.

@hwsmm
Copy link
Contributor Author

hwsmm commented Jan 21, 2025

Along with peppy's comment, I'll revisit after I understand a bit more how SDL or X11/Wayland is dealing with display disconnection. Considering that this only happens on Linux (so far), this needs more consideration.

@hwsmm hwsmm marked this pull request as draft January 21, 2025 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants