Skip to content

Commit

Permalink
Merge pull request #4 from TSRBerry/master
Browse files Browse the repository at this point in the history
Add GetErrorText and reply codes
  • Loading branch information
marysaka authored Jul 4, 2023
2 parents 56354d4 + 59dd929 commit e541cc3
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions SPB/Platform/X11/X11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e541cc3

Please sign in to comment.