From 91fe87974e8c0d02b230fb1218905920154f5af5 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Wed, 15 Apr 2026 16:12:52 +0200 Subject: Fix XError handling and Display Lock on Event handling --- main.c | 2 ++ manager.c | 16 +++++++++++++--- switcher.c | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index c2585d9..9bc3a4b 100644 --- a/main.c +++ b/main.c @@ -67,6 +67,7 @@ int main(int argc, char *argv[]) { while(wm.running) { XNextEvent(wm.dpy, &wm.ev); + XLockDisplay(wm.dpy); switch (wm.ev.type) { case MapRequest: handle_map_request(); @@ -116,6 +117,7 @@ int main(int argc, char *argv[]) { handle_configure_request(); break; } + XUnlockDisplay(wm.dpy); } deinit_window_manager(); diff --git a/manager.c b/manager.c index 58ee90a..f6b46f1 100644 --- a/manager.c +++ b/manager.c @@ -586,7 +586,11 @@ void handle_configure_request(void) { changes.sibling = ev->above; changes.stack_mode = ev->detail; + XErrorHandler old = XSetErrorHandler(ignore_x_error); XConfigureWindow(wm.dpy, ev->window, ev->value_mask, &changes); + XSync(wm.dpy, False); + XSetErrorHandler(old); + log_message(stdout, LOG_DEBUG, "ConfigureRequest for 0x%lx (x=%d, y=%d, w=%d, h=%d)", ev->window, ev->x, ev->y, ev->width, ev->height); } @@ -595,6 +599,8 @@ void handle_map_request(void) { Window window = wm.ev.xmaprequest.window; if (window == wm.root) return; + XErrorHandler old = XSetErrorHandler(ignore_x_error); + // Move window under cursor position and clamps inside the screen bounds. XWindowAttributes check_attr; if (XGetWindowAttributes(wm.dpy, window, &check_attr)) { @@ -635,6 +641,10 @@ void handle_map_request(void) { grab_buttons(window); add_client(window); + + XSync(wm.dpy, False); + XSetErrorHandler(old); + log_message(stdout, LOG_DEBUG, "Window 0x%lx mapped and grabbed on desktop %d", window, wm.current_desktop); redraw_widgets(); update_client_list(); @@ -972,10 +982,10 @@ void handle_button_release(void) { if (wm.start.subwindow != None && (wm.start.state & MODKEY)) { XDefineCursor(wm.dpy, wm.start.subwindow, None); - // Restore default error handler - XSetErrorHandler(NULL); + // Restore window manager error handler + XSetErrorHandler(x_error_handler); - log_message(stdout, LOG_DEBUG, "Restored default cursor on window 0x%lx", wm.start.subwindow); + log_message(stdout, LOG_DEBUG, "Restored window manager cursor on window 0x%lx", wm.start.subwindow); wm.start.subwindow = None; } diff --git a/switcher.c b/switcher.c index 949bcef..cde8c90 100644 --- a/switcher.c +++ b/switcher.c @@ -109,7 +109,7 @@ void cycle_active_window(const Arg *arg) { // We grab it on the root window. XGrabKeyboard(wm.dpy, wm.root, True, GrabModeAsync, GrabModeAsync, CurrentTime); - // Count clients + // Count clients - allocate extra to handle new windows mapping during this process int count = 0; Client *c = wm.clients; while (c) { @@ -122,7 +122,7 @@ void cycle_active_window(const Arg *arg) { return; } - wm.cycle_clients = malloc(sizeof(Window) * count); + wm.cycle_clients = malloc(sizeof(Window) * (count + 10)); wm.cycle_count = 0; wm.active_cycle_index = 0; -- cgit v1.2.3