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

Nullable annotations for X11 #17814

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/Avalonia.X11/Avalonia.X11.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>
<Import Project="..\..\build\SourceGenerators.props" />
<Import Project="..\..\build\TrimmingEnable.props" />
<Import Project="..\..\build\NullableEnable.props" />

<ItemGroup>
<Compile Remove="..\Shared\SourceGeneratorAttributes.cs"/>
Expand Down
3 changes: 1 addition & 2 deletions src/Avalonia.X11/Dispatching/GLibDispatcherImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -312,4 +311,4 @@ public void Dispose()
}

public X11EventDispatcher EventDispatcher => _x11Events;
}
}
4 changes: 2 additions & 2 deletions src/Avalonia.X11/Dispatching/X11PlatformThreading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public void Signal()

public bool CurrentThreadIsLoopThread => Thread.CurrentThread == _mainThread;

public event Action Signaled;
public event Action Timer;
public event Action? Signaled;
public event Action? Timer;

public void UpdateTimer(long? dueTimeInMs)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.X11/Glx/Glx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public static IntPtr SafeGetProcAddress(string proc)
public string[] GetExtensions(IntPtr display)
{
var s = Marshal.PtrToStringAnsi(QueryExtensionsString(display, 0));
if (string.IsNullOrEmpty(s))
return [];

return s.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim()).ToArray();

Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.X11/Glx/GlxContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Threading;
Expand Down
10 changes: 5 additions & 5 deletions src/Avalonia.X11/Glx/GlxDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ public GlxContext CreateContext() => CreateContext(CreatePBuffer(), null, Deferr
public GlxContext CreateContext(IGlContext share) => CreateContext(CreatePBuffer(), share,
share.SampleCount, share.StencilSize, true);

private GlxContext CreateContext(IntPtr defaultXid, IGlContext share,
private GlxContext CreateContext(IntPtr defaultXid, IGlContext? share,
int sampleCount, int stencilSize, bool ownsPBuffer)
{
var sharelist = ((GlxContext)share)?.Handle ?? IntPtr.Zero;
var sharelist = ((GlxContext?)share)?.Handle ?? IntPtr.Zero;
IntPtr handle = default;

GlxContext Create(GlVersion profile)
GlxContext? Create(GlVersion profile)
{
var profileMask = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
if (profile.Type == GlProfileType.OpenGL &&
Expand All @@ -149,7 +149,7 @@ GlxContext Create(GlVersion profile)
if (handle != IntPtr.Zero)
{
_version = profile;
return new GlxContext(new GlxInterface(), handle, this, (GlxContext)share, profile,
return new GlxContext(new GlxInterface(), handle, this, (GlxContext?)share, profile,
sampleCount, stencilSize, _x11, defaultXid, ownsPBuffer);

}
Expand All @@ -162,7 +162,7 @@ GlxContext Create(GlVersion profile)
return null;
}

GlxContext rv = null;
GlxContext? rv = null;
if (_version.HasValue)
{
rv = Create(_version.Value);
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.X11/Glx/GlxGlPlatformSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Dispose()

public PixelSize Size => _size ?? _info.Size;
public double Scaling => _info.Scaling;
public bool IsYFlipped { get; }
public bool IsYFlipped => false;
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Avalonia.X11/Glx/GlxPlatformFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ namespace Avalonia.X11.Glx
{
internal class GlxPlatformGraphics : IPlatformGraphics
{
public GlxDisplay Display { get; private set; }
public GlxDisplay Display { get; }
public bool CanCreateContexts => true;
public bool CanShareContexts => true;
public bool UsesSharedContext => false;
IPlatformGraphicsContext IPlatformGraphics.CreateContext() => Display.CreateContext();

public IPlatformGraphicsContext GetSharedContext() => throw new NotSupportedException();

public static GlxPlatformGraphics TryCreate(X11Info x11, IList<GlVersion> glProfiles)

public GlxPlatformGraphics(GlxDisplay display)
{
Display = display;
}

public static GlxPlatformGraphics? TryCreate(X11Info x11, IList<GlVersion> glProfiles)
{
try
{
var disp = new GlxDisplay(x11, glProfiles);
return new GlxPlatformGraphics
{
Display = disp
};
return new GlxPlatformGraphics(disp);
}
catch(Exception e)
{
Expand Down
10 changes: 6 additions & 4 deletions src/Avalonia.X11/Interop/Glib.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#nullable enable

using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Avalonia.Platform.Interop;
using static Avalonia.X11.Interop.Glib;

namespace Avalonia.X11.Interop;

internal static unsafe class Glib
Expand Down Expand Up @@ -75,7 +76,7 @@ public static uint g_timeout_add_once(uint interval, Action cb) =>
private static readonly GDestroyNotify s_gcHandleDestroyNotify = handle => GCHandle.FromIntPtr(handle).Free();

private static readonly GSourceFunc s_sourceFuncDispatchCallback =
handle => ((Func<bool>)GCHandle.FromIntPtr(handle).Target)() ? 1 : 0;
handle => ((Func<bool>)GCHandle.FromIntPtr(handle).Target!)() ? 1 : 0;

[DllImport(GlibName)]
private static extern uint g_idle_add_full (int priority, GSourceFunc function, IntPtr data, GDestroyNotify notify);
Expand Down Expand Up @@ -108,7 +109,7 @@ public enum GIOCondition
public delegate int GUnixFDSourceFunc(int fd, GIOCondition condition, IntPtr user_data);

private static readonly GUnixFDSourceFunc s_unixFdSourceCallback = (fd, cond, handle) =>
((Func<int, GIOCondition, bool>)GCHandle.FromIntPtr(handle).Target)(fd, cond) ? 1 : 0;
((Func<int, GIOCondition, bool>)GCHandle.FromIntPtr(handle).Target!)(fd, cond) ? 1 : 0;

[DllImport(GlibName)]
public static extern uint g_unix_fd_add_full (int priority,
Expand Down Expand Up @@ -152,6 +153,7 @@ public void Dispose()
}

public static IDisposable ConnectSignal<T>(IntPtr obj, string name, T handler)
where T : notnull
{
var handle = GCHandle.Alloc(handler);
var ptr = Marshal.GetFunctionPointerForDelegate(handler);
Expand Down Expand Up @@ -187,4 +189,4 @@ internal unsafe struct GSList
{
public readonly IntPtr Data;
public readonly GSList* Next;
}
}
6 changes: 3 additions & 3 deletions src/Avalonia.X11/NativeDialogs/Gtk.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#nullable enable

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Platform.Interop;
using Avalonia.Threading;
using Avalonia.X11.Dispatching;
using Avalonia.X11.Interop;

// ReSharper disable IdentifierTypo
Expand Down Expand Up @@ -142,7 +142,7 @@ public static extern void
public static IntPtr GetForeignWindow(IntPtr xid) => gdk_x11_window_foreign_new_for_display(s_display, xid);

static object s_startGtkLock = new();
static Task<bool> s_startGtkTask;
static Task<bool>? s_startGtkTask;

public static Task<bool> StartGtk()
{
Expand Down
5 changes: 1 addition & 4 deletions src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#nullable enable

using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls.Platform;
using Avalonia.Platform;
using Avalonia.Platform.Interop;
using Avalonia.Platform.Storage;
Expand Down
1 change: 0 additions & 1 deletion src/Avalonia.X11/SMLib.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Runtime.InteropServices;

Expand Down
17 changes: 7 additions & 10 deletions src/Avalonia.X11/Screens/X11Screen.Providers.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

#nullable enable
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Avalonia.Platform;
using static Avalonia.X11.XLib;

namespace Avalonia.X11.Screens;

internal partial class X11Screens
Expand Down Expand Up @@ -44,7 +43,7 @@ public virtual void Refresh()

private unsafe Size? GetPhysicalMonitorSizeFromEDID(IntPtr rrOutput)
{
if (rrOutput == IntPtr.Zero || x11 == null)
if (rrOutput == IntPtr.Zero)
return null;
var properties = XRRListOutputProperties(x11.Display, rrOutput, out int propertyCount);
var hasEDID = false;
Expand Down Expand Up @@ -130,7 +129,7 @@ internal interface IX11RawScreenInfoProvider
{
nint[] ScreenKeys { get; }
event Action? Changed;
X11Screen? CreateScreenFromKey(nint key);
X11Screen CreateScreenFromKey(nint key);
}

internal unsafe struct MonitorInfo
Expand Down Expand Up @@ -211,20 +210,18 @@ private unsafe MonitorInfo[] MonitorInfos
}
}

public X11Screen? CreateScreenFromKey(nint key)
public X11Screen CreateScreenFromKey(nint key)
{
var info = MonitorInfos.Where(x => x.Name == key).FirstOrDefault();

var infos = MonitorInfos;
for (var i = 0; i < infos.Length; i++)
{
if (infos[i].Name == key)
{
return new X11Screen(info, _x11, _scalingProvider, i);
return new X11Screen(infos[i], _x11, _scalingProvider, i);
}
}

return null;
return new FallBackScreen(default, _x11);
}
Copy link
Member

Choose a reason for hiding this comment

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

I think that's not a valid behavior to return a fake screen for invalid parameter here. It's a problem of the calling code to handle a missing screen properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Out of 10 Screen implementations, this one was the only one returning null - it's fairly recent, and should have respected the non-null contract. I guess it was missed due to the nullability mismatch here.

I've changed it to throw instead. We should never get here anyway: the key comes from the ScreenKeys property, and should always be found in the for loop above.

}

Expand All @@ -248,7 +245,7 @@ public FallbackScreensImpl(AvaloniaX11Platform platform)

private bool UpdateRootWindowGeometry() => XGetGeometry(_info.Display, _info.RootWindow, out _geo);

public X11Screen? CreateScreenFromKey(nint key)
public X11Screen CreateScreenFromKey(nint key)
{
return new FallBackScreen(new PixelRect(0, 0, _geo.width, _geo.height), _info);
}
Expand Down
1 change: 0 additions & 1 deletion src/Avalonia.X11/Screens/X11Screens.Scaling.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down
5 changes: 0 additions & 5 deletions src/Avalonia.X11/Screens/X11Screens.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Avalonia.Platform;
using static Avalonia.X11.Screens.X11Screens;
using static Avalonia.X11.XLib;

namespace Avalonia.X11.Screens
{
Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.X11/TransparencyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using Avalonia.Controls;

#nullable enable

namespace Avalonia.X11
{
internal class TransparencyHelper : IDisposable
Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.X11/Vulkan/VulkanSupport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.X11/X11Atoms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public IntPtr GetAtom(string name)
return atom;
}

public string GetAtomName(IntPtr atom)
public string? GetAtomName(IntPtr atom)
{
if (_atomsToNames.TryGetValue(atom, out var rv))
return rv;
Expand Down
Loading
Loading