Skip to content

Implement IPlatformLifetimeEvents for X11/Linux #6883

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

Merged
merged 13 commits into from
Nov 23, 2021
Merged
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
60 changes: 60 additions & 0 deletions src/Avalonia.X11/ICELib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Runtime.InteropServices;

namespace Avalonia.X11
{
internal static class ICELib
{
private const string LibIce = "libICE.so.6";

[DllImport(LibIce, CallingConvention = CallingConvention.StdCall)]
public static extern int IceAddConnectionWatch(
IntPtr watchProc,
IntPtr clientData
);

[DllImport(LibIce, CallingConvention = CallingConvention.StdCall)]
public static extern void IceRemoveConnectionWatch(
IntPtr watchProc,
IntPtr clientData
);

[DllImport(LibIce, CallingConvention = CallingConvention.StdCall)]
public static extern IceProcessMessagesStatus IceProcessMessages(
IntPtr iceConn,
out IntPtr replyWait,
out bool replyReadyRet
);

[DllImport(LibIce, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr IceSetErrorHandler(
IntPtr handler
);

[DllImport(LibIce, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr IceSetIOErrorHandler(
IntPtr handler
);

public enum IceProcessMessagesStatus
{
IceProcessMessagesIoError = 1
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void IceErrorHandler(
IntPtr iceConn,
bool swap,
int offendingMinorOpcode,
ulong offendingSequence,
int errorClass,
int severity,
IntPtr values
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void IceIOErrorHandler(
IntPtr iceConn
);
}
}
133 changes: 133 additions & 0 deletions src/Avalonia.X11/SMLib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Runtime.InteropServices;

namespace Avalonia.X11
{
internal static unsafe class SMLib
{
private const string LibSm = "libSM.so.6";

[DllImport(LibSm, CharSet = CharSet.Ansi)]
public static extern IntPtr SmcOpenConnection(
[MarshalAs(UnmanagedType.LPWStr)] string networkId,
IntPtr content,
int xsmpMajorRev,
int xsmpMinorRev,
ulong mask,
ref SmcCallbacks callbacks,
[MarshalAs(UnmanagedType.LPWStr)] [Out]
out string previousId,
[MarshalAs(UnmanagedType.LPWStr)] [Out]
out string clientIdRet,
int errorLength,
[Out] char[] errorStringRet);

[DllImport(LibSm, CallingConvention = CallingConvention.StdCall)]
public static extern int SmcCloseConnection(
IntPtr smcConn,
int count,
string[] reasonMsgs
);

[DllImport(LibSm, CallingConvention = CallingConvention.StdCall)]
public static extern void SmcSaveYourselfDone(
IntPtr smcConn,
bool success
);

[DllImport(LibSm, CallingConvention = CallingConvention.StdCall)]
public static extern int SmcInteractRequest(
IntPtr smcConn,
SmDialogValue dialogType,
IntPtr interactProc,
IntPtr clientData
);

[DllImport(LibSm, CallingConvention = CallingConvention.StdCall)]
public static extern void SmcInteractDone(
IntPtr smcConn,
bool success
);

[DllImport(LibSm, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SmcGetIceConnection(
IntPtr smcConn
);

[DllImport(LibSm, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SmcSetErrorHandler(
IntPtr handler
);

public enum SmDialogValue
{
SmDialogError = 0
}

[StructLayout(LayoutKind.Sequential)]
public struct SmcCallbacks
{
public IntPtr SaveYourself;
private readonly IntPtr Unused0;
public IntPtr Die;
private readonly IntPtr Unused1;
public IntPtr SaveComplete;
private readonly IntPtr Unused2;
public IntPtr ShutdownCancelled;
private readonly IntPtr Unused3;
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void IceWatchProc(
IntPtr iceConn,
IntPtr clientData,
bool opening,
IntPtr* watchData
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SmcDieProc(
IntPtr smcConn,
IntPtr clientData
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SmcInteractProc(
IntPtr smcConn,
IntPtr clientData
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SmcSaveCompleteProc(
IntPtr smcConn,
IntPtr clientData
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SmcSaveYourselfProc(
IntPtr smcConn,
IntPtr clientData,
int saveType,
bool shutdown,
int interactStyle,
bool fast
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SmcShutdownCancelledProc(
IntPtr smcConn,
IntPtr clientData
);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SmcErrorHandler(
IntPtr smcConn,
bool swap,
int offendingMinorOpcode,
ulong offendingSequence,
int errorClass,
int severity,
IntPtr values
);
}
}
17 changes: 15 additions & 2 deletions src/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public void Initialize(X11PlatformOptions options)
.Bind<IPlatformSettings>().ToConstant(new PlatformSettingsStub())
.Bind<IPlatformIconLoader>().ToConstant(new X11IconLoader(Info))
.Bind<ISystemDialogImpl>().ToConstant(new GtkSystemDialog())
.Bind<IMountedVolumeInfoProvider>().ToConstant(new LinuxMountedVolumeInfoProvider());
.Bind<IMountedVolumeInfoProvider>().ToConstant(new LinuxMountedVolumeInfoProvider())
.Bind<IPlatformLifetimeEventsImpl>().ToConstant(new X11PlatformLifetimeEvents(this));

X11Screens = Avalonia.X11.X11Screens.Init(this);
Screens = new X11Screens(X11Screens);
Expand Down Expand Up @@ -230,7 +231,19 @@ public class X11PlatformOptions
/// on their input devices by using sequences of characters or mouse operations that are natively available on their input devices.
/// </remarks>
public bool? EnableIme { get; set; }


/// <summary>
/// Determines whether to enable support for the
/// X Session Management Protocol.
/// </summary>
/// <remarks>
/// X Session Management Protocol is a standard implemented on most
/// Linux systems that uses Xorg. This enables apps to control how they
/// can control and/or cancel the pending shutdown requested by the user.
/// </remarks>
public bool EnableSessionManagement { get; set; } =
Environment.GetEnvironmentVariable("AVALONIA_X11_USE_SESSION_MANAGEMENT") != "0";

public IList<GlVersion> GlProfiles { get; set; } = new List<GlVersion>
{
new GlVersion(GlProfileType.OpenGL, 4, 0),
Expand Down
Loading