From 00c7e6dfb2a0faae0ad076e801f09eec5379309e Mon Sep 17 00:00:00 2001 From: tt2468 Date: Wed, 27 Mar 2024 13:38:33 -0700 Subject: [PATCH] Move X11 error handler registration to before screen sets (#62) If X11 throws an error during screen config get/set, the default error handler at the time will be one which causes a program exit. This splits the error handler registration in xevent into its own call, allowing us to register our error handler earlier on. This results in the safe error handling logic actually being called instead of a full program exit. --- internal/desktop/manager.go | 3 +++ pkg/xevent/xevent.c | 5 ++++- pkg/xevent/xevent.go | 4 ++++ pkg/xevent/xevent.h | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/desktop/manager.go b/internal/desktop/manager.go index dad4dd2d..da998df2 100644 --- a/internal/desktop/manager.go +++ b/internal/desktop/manager.go @@ -50,6 +50,9 @@ func (manager *DesktopManagerCtx) Start() { manager.logger.Panic().Str("display", manager.config.Display).Msg("unable to open display") } + // X11 can throw errors below, and the default error handler exits + xevent.SetupErrorHandler() + xorg.GetScreenConfigurations() screenSize, err := xorg.ChangeScreenSize(manager.config.ScreenSize) diff --git a/pkg/xevent/xevent.c b/pkg/xevent/xevent.c index 991f6987..1d75422c 100644 --- a/pkg/xevent/xevent.c +++ b/pkg/xevent/xevent.c @@ -14,6 +14,10 @@ static int XEventError(Display *display, XErrorEvent *event) { return 1; } +void XSetupErrorHandler() { + XSetErrorHandler(XEventError); +} + void XEventLoop(char *name) { Display *display = XOpenDisplay(name); Window root = DefaultRootWindow(display); @@ -33,7 +37,6 @@ void XEventLoop(char *name) { XSelectInput(display, root, SubstructureNotifyMask); XSync(display, 0); - XSetErrorHandler(XEventError); while (goXEventActive()) { XEvent event; diff --git a/pkg/xevent/xevent.go b/pkg/xevent/xevent.go index 53b820e9..b2fd9d4f 100644 --- a/pkg/xevent/xevent.go +++ b/pkg/xevent/xevent.go @@ -23,6 +23,10 @@ func init() { Emmiter = events.New() } +func SetupErrorHandler() { + C.XSetupErrorHandler() +} + func EventLoop(display string) { displayUnsafe := C.CString(display) defer C.free(unsafe.Pointer(displayUnsafe)) diff --git a/pkg/xevent/xevent.h b/pkg/xevent/xevent.h index 801e0e6e..c3924dae 100644 --- a/pkg/xevent/xevent.h +++ b/pkg/xevent/xevent.h @@ -17,6 +17,7 @@ extern void goXEventError(XErrorEvent *event, char *message); extern int goXEventActive(); static int XEventError(Display *display, XErrorEvent *event); +void XSetupErrorHandler(); void XEventLoop(char *display); static void XWindowManagerStateEvent(Display *display, Window window, ulong action, ulong first, ulong second);