From 59dd9296a70771ac141571516938bd9b3303d179 Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sun, 4 Jun 2023 04:19:00 +0200 Subject: [PATCH] Add GetErrorText and reply codes --- SPB/Platform/X11/X11.cs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/SPB/Platform/X11/X11.cs b/SPB/Platform/X11/X11.cs index 7f712ab..a965616 100644 --- a/SPB/Platform/X11/X11.cs +++ b/SPB/Platform/X11/X11.cs @@ -120,7 +120,7 @@ public enum XEventName [StructLayout(LayoutKind.Sequential)] public struct XErrorEvent { - public XEventName type; + public XReplyCode type; public Display display; public IntPtr resourceid; public IntPtr serial; @@ -131,6 +131,14 @@ public struct XErrorEvent public delegate int XErrorHandler(Display displayHandle, ref XErrorEvent errorEvent); + public enum XReplyCode : byte + { + // Normal reply + X_Reply = 1, + // Error + X_Error = 0 + } + public enum XRequest : byte { X_CreateWindow = 1, @@ -258,6 +266,9 @@ public enum XRequest : byte [DllImport(LibraryName, EntryPoint = "XSetErrorHandler")] public extern static IntPtr SetErrorHandler(XErrorHandler error_handler); + [DllImport(LibraryName, EntryPoint = "XGetErrorText", CharSet = CharSet.Unicode)] + public extern static int GetErrorText(Display display, int code, IntPtr buffer, int length); + public enum Gravity { ForgetGravity = 0, @@ -337,7 +348,22 @@ public struct XSetWindowAttributes static int ErrorHandler(Display displayHandle, ref XErrorEvent errorEvent) { - Console.WriteLine($"XError: {errorEvent.type} result is {errorEvent.error_code}"); + IntPtr buffer = Marshal.AllocHGlobal(256); + string error; + int result = GetErrorText(displayHandle, errorEvent.error_code, buffer, 256); + + if (result == 0) + { + error = Marshal.PtrToStringAuto(buffer); + } + else + { + error = $"GetErrorText returned: {result}"; + } + + Marshal.FreeHGlobal(buffer); + + Console.WriteLine($"XError: {errorEvent.type} result is {error} (code: {errorEvent.error_code})"); Console.Out.Flush(); return 0; }