Skip to content

Commit f2f017c

Browse files
authored
Nullable annotations for X11 (#17814)
* Nullable annotations for X11 * Address review
1 parent e8d3544 commit f2f017c

39 files changed

+193
-202
lines changed

src/Avalonia.X11/Avalonia.X11.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</ItemGroup>
1414
<Import Project="..\..\build\SourceGenerators.props" />
1515
<Import Project="..\..\build\TrimmingEnable.props" />
16+
<Import Project="..\..\build\NullableEnable.props" />
1617

1718
<ItemGroup>
1819
<Compile Remove="..\Shared\SourceGeneratorAttributes.cs"/>

src/Avalonia.X11/Dispatching/GLibDispatcherImpl.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using System.Diagnostics;
@@ -312,4 +311,4 @@ public void Dispose()
312311
}
313312

314313
public X11EventDispatcher EventDispatcher => _x11Events;
315-
}
314+
}

src/Avalonia.X11/Dispatching/X11PlatformThreading.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public void Signal()
186186

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

189-
public event Action Signaled;
190-
public event Action Timer;
189+
public event Action? Signaled;
190+
public event Action? Timer;
191191

192192
public void UpdateTimer(long? dueTimeInMs)
193193
{

src/Avalonia.X11/Glx/Glx.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public static IntPtr SafeGetProcAddress(string proc)
115115
public string[] GetExtensions(IntPtr display)
116116
{
117117
var s = Marshal.PtrToStringAnsi(QueryExtensionsString(display, 0));
118+
if (string.IsNullOrEmpty(s))
119+
return [];
120+
118121
return s.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
119122
.Select(x => x.Trim()).ToArray();
120123

src/Avalonia.X11/Glx/GlxContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable enable
2-
31
using System;
42
using System.Collections.Generic;
53
using System.Threading;

src/Avalonia.X11/Glx/GlxDisplay.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ public GlxContext CreateContext() => CreateContext(CreatePBuffer(), null, Deferr
119119
public GlxContext CreateContext(IGlContext share) => CreateContext(CreatePBuffer(), share,
120120
share.SampleCount, share.StencilSize, true);
121121

122-
private GlxContext CreateContext(IntPtr defaultXid, IGlContext share,
122+
private GlxContext CreateContext(IntPtr defaultXid, IGlContext? share,
123123
int sampleCount, int stencilSize, bool ownsPBuffer)
124124
{
125-
var sharelist = ((GlxContext)share)?.Handle ?? IntPtr.Zero;
125+
var sharelist = ((GlxContext?)share)?.Handle ?? IntPtr.Zero;
126126
IntPtr handle = default;
127127

128-
GlxContext Create(GlVersion profile)
128+
GlxContext? Create(GlVersion profile)
129129
{
130130
var profileMask = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
131131
if (profile.Type == GlProfileType.OpenGL &&
@@ -149,7 +149,7 @@ GlxContext Create(GlVersion profile)
149149
if (handle != IntPtr.Zero)
150150
{
151151
_version = profile;
152-
return new GlxContext(new GlxInterface(), handle, this, (GlxContext)share, profile,
152+
return new GlxContext(new GlxInterface(), handle, this, (GlxContext?)share, profile,
153153
sampleCount, stencilSize, _x11, defaultXid, ownsPBuffer);
154154

155155
}
@@ -162,7 +162,7 @@ GlxContext Create(GlVersion profile)
162162
return null;
163163
}
164164

165-
GlxContext rv = null;
165+
GlxContext? rv = null;
166166
if (_version.HasValue)
167167
{
168168
rv = Create(_version.Value);

src/Avalonia.X11/Glx/GlxGlPlatformSurface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void Dispose()
9696

9797
public PixelSize Size => _size ?? _info.Size;
9898
public double Scaling => _info.Scaling;
99-
public bool IsYFlipped { get; }
99+
public bool IsYFlipped => false;
100100
}
101101
}
102102
}

src/Avalonia.X11/Glx/GlxPlatformFeature.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ namespace Avalonia.X11.Glx
88
{
99
internal class GlxPlatformGraphics : IPlatformGraphics
1010
{
11-
public GlxDisplay Display { get; private set; }
11+
public GlxDisplay Display { get; }
1212
public bool CanCreateContexts => true;
1313
public bool CanShareContexts => true;
1414
public bool UsesSharedContext => false;
1515
IPlatformGraphicsContext IPlatformGraphics.CreateContext() => Display.CreateContext();
1616

1717
public IPlatformGraphicsContext GetSharedContext() => throw new NotSupportedException();
18-
19-
public static GlxPlatformGraphics TryCreate(X11Info x11, IList<GlVersion> glProfiles)
18+
19+
public GlxPlatformGraphics(GlxDisplay display)
20+
{
21+
Display = display;
22+
}
23+
24+
public static GlxPlatformGraphics? TryCreate(X11Info x11, IList<GlVersion> glProfiles)
2025
{
2126
try
2227
{
2328
var disp = new GlxDisplay(x11, glProfiles);
24-
return new GlxPlatformGraphics
25-
{
26-
Display = disp
27-
};
29+
return new GlxPlatformGraphics(disp);
2830
}
2931
catch(Exception e)
3032
{

src/Avalonia.X11/Interop/Glib.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#nullable enable
2+
23
using System;
34
using System.Runtime.InteropServices;
45
using System.Threading.Tasks;
56
using Avalonia.Platform.Interop;
6-
using static Avalonia.X11.Interop.Glib;
7+
78
namespace Avalonia.X11.Interop;
89

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

7778
private static readonly GSourceFunc s_sourceFuncDispatchCallback =
78-
handle => ((Func<bool>)GCHandle.FromIntPtr(handle).Target)() ? 1 : 0;
79+
handle => ((Func<bool>)GCHandle.FromIntPtr(handle).Target!)() ? 1 : 0;
7980

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

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

113114
[DllImport(GlibName)]
114115
public static extern uint g_unix_fd_add_full (int priority,
@@ -152,6 +153,7 @@ public void Dispose()
152153
}
153154

154155
public static IDisposable ConnectSignal<T>(IntPtr obj, string name, T handler)
156+
where T : notnull
155157
{
156158
var handle = GCHandle.Alloc(handler);
157159
var ptr = Marshal.GetFunctionPointerForDelegate(handler);
@@ -187,4 +189,4 @@ internal unsafe struct GSList
187189
{
188190
public readonly IntPtr Data;
189191
public readonly GSList* Next;
190-
}
192+
}

src/Avalonia.X11/NativeDialogs/Gtk.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
#nullable enable
2+
13
using System;
24
using System.Runtime.InteropServices;
35
using System.Threading;
46
using System.Threading.Tasks;
57
using Avalonia.Platform.Interop;
6-
using Avalonia.Threading;
7-
using Avalonia.X11.Dispatching;
88
using Avalonia.X11.Interop;
99

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

144144
static object s_startGtkLock = new();
145-
static Task<bool> s_startGtkTask;
145+
static Task<bool>? s_startGtkTask;
146146

147147
public static Task<bool> StartGtk()
148148
{

src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
#nullable enable
2-
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.IO;
64
using System.Linq;
75
using System.Threading.Tasks;
8-
using Avalonia.Controls.Platform;
96
using Avalonia.Platform;
107
using Avalonia.Platform.Interop;
118
using Avalonia.Platform.Storage;

src/Avalonia.X11/SMLib.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Runtime.InteropServices;
43

src/Avalonia.X11/Screens/X11Screen.Providers.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
2-
#nullable enable
31
using System;
2+
using System.Diagnostics;
43
using System.Linq;
54
using System.Runtime.InteropServices;
65
using Avalonia.Platform;
76
using static Avalonia.X11.XLib;
7+
88
namespace Avalonia.X11.Screens;
99

1010
internal partial class X11Screens
@@ -44,7 +44,7 @@ public virtual void Refresh()
4444

4545
private unsafe Size? GetPhysicalMonitorSizeFromEDID(IntPtr rrOutput)
4646
{
47-
if (rrOutput == IntPtr.Zero || x11 == null)
47+
if (rrOutput == IntPtr.Zero)
4848
return null;
4949
var properties = XRRListOutputProperties(x11.Display, rrOutput, out int propertyCount);
5050
var hasEDID = false;
@@ -130,7 +130,7 @@ internal interface IX11RawScreenInfoProvider
130130
{
131131
nint[] ScreenKeys { get; }
132132
event Action? Changed;
133-
X11Screen? CreateScreenFromKey(nint key);
133+
X11Screen CreateScreenFromKey(nint key);
134134
}
135135

136136
internal unsafe struct MonitorInfo
@@ -211,20 +211,18 @@ private unsafe MonitorInfo[] MonitorInfos
211211
}
212212
}
213213

214-
public X11Screen? CreateScreenFromKey(nint key)
214+
public X11Screen CreateScreenFromKey(nint key)
215215
{
216-
var info = MonitorInfos.Where(x => x.Name == key).FirstOrDefault();
217-
218216
var infos = MonitorInfos;
219217
for (var i = 0; i < infos.Length; i++)
220218
{
221219
if (infos[i].Name == key)
222220
{
223-
return new X11Screen(info, _x11, _scalingProvider, i);
221+
return new X11Screen(infos[i], _x11, _scalingProvider, i);
224222
}
225223
}
226224

227-
return null;
225+
throw new ArgumentOutOfRangeException(nameof(key));
228226
}
229227
}
230228

@@ -248,7 +246,7 @@ public FallbackScreensImpl(AvaloniaX11Platform platform)
248246

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

251-
public X11Screen? CreateScreenFromKey(nint key)
249+
public X11Screen CreateScreenFromKey(nint key)
252250
{
253251
return new FallBackScreen(new PixelRect(0, 0, _geo.width, _geo.height), _info);
254252
}

src/Avalonia.X11/Screens/X11Screens.Scaling.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using System.Globalization;

src/Avalonia.X11/Screens/X11Screens.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Globalization;
4-
using System.Linq;
5-
using System.Runtime.InteropServices;
6-
using System.Threading.Tasks;
73
using Avalonia.Platform;
84
using static Avalonia.X11.Screens.X11Screens;
9-
using static Avalonia.X11.XLib;
105

116
namespace Avalonia.X11.Screens
127
{

src/Avalonia.X11/TransparencyHelper.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using Avalonia.Controls;
44

5-
#nullable enable
6-
75
namespace Avalonia.X11
86
{
97
internal class TransparencyHelper : IDisposable

src/Avalonia.X11/Vulkan/VulkanSupport.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable enable
2-
31
using System;
42
using System.Collections.Generic;
53
using System.Runtime.InteropServices;

src/Avalonia.X11/X11Atoms.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public IntPtr GetAtom(string name)
222222
return atom;
223223
}
224224

225-
public string GetAtomName(IntPtr atom)
225+
public string? GetAtomName(IntPtr atom)
226226
{
227227
if (_atomsToNames.TryGetValue(atom, out var rv))
228228
return rv;

0 commit comments

Comments
 (0)