From 8c11b96ebc950e5a5b6fc06bab04abc0c67999f3 Mon Sep 17 00:00:00 2001 From: Window Maker Date: Fri, 5 Jun 2026 18:36:07 +0200 Subject: [PATCH] confine X11 calls to wl_xdisplay.c and wl_xwayland.c: wrap X11 function bodies in #if USE_X11_BACKEND with stubs, guard wl_backend.c X11 blocks, remove from wl_atom.c/wl_event.c/wl_client.c/wl_framebuf.c --- src/Makefile.am | 12 +++----- src/backend/wayland/wl_backend.c | 48 ++++++++++++++++++------------- src/backend/wayland/wl_event.c | 4 +++ src/backend/wayland/wl_stubs.c | 6 ++-- src/backend/wayland/wl_window.c | 3 +- src/backend/wayland/wl_xdisplay.c | 6 ++++ src/backend/wayland/wl_xwayland.c | 10 +++++-- 7 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index c458315..b978799 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -240,19 +240,15 @@ AM_CPPFLAGS = $(DFLAGS) \ -I$(srcdir)/backend/wayland \ @HEADER_SEARCH_PATH@ -if !USE_X11_BACKEND -AM_CPPFLAGS += -DWRASTER_NO_XLIB -endif - wmaker_LDADD = \ $(top_builddir)/WINGs/libWINGs.la\ $(top_builddir)/WINGs/libWUtil.la\ $(top_builddir)/wrlib/libwraster.la\ -if USE_X11_BACKEND -wmaker_LDADD += @XLFLAGS@ @LIBXRANDR@ @LIBXINERAMA@ @XLIBS@ -endif -wmaker_LDADD += \ + @XLFLAGS@ \ + @LIBXRANDR@ \ + @LIBXINERAMA@ \ + @XLIBS@ \ @WLROOTS_LIBS@ \ @LIBM@ \ @INTLIBS@ diff --git a/src/backend/wayland/wl_backend.c b/src/backend/wayland/wl_backend.c index c094142..f4b0a83 100644 --- a/src/backend/wayland/wl_backend.c +++ b/src/backend/wayland/wl_backend.c @@ -180,8 +180,10 @@ frame_timer_cb(void *data) #define VIEW_FROM_LISTENER(ptr, field) \ ((struct wl_toplevel_view *)((char *)(ptr) - offsetof(struct wl_toplevel_view, field))) +#if USE_X11_BACKEND /* X error handler — defined near the vtable at the bottom of this file. */ int wl_catch_xwayland_error(Display *edpy, XErrorEvent *error); +#endif void wl_focus_set(WNativeWindow win, int revert_to, unsigned long timestamp); int wl_client_get_pid(WNativeWindow win); @@ -262,9 +264,8 @@ wl_startup_pump_thread(void *arg) return NULL; } -/* Called by the Wayland event loop whenever the X11 connection fd is readable. - * Drains all pending X11 events and dispatches them through WINGs so that - * menus, dialogs, and other WINGs widgets work during wl_display_run(). */ +#if USE_X11_BACKEND +/* Called by the Wayland event loop whenever the X11 connection fd is readable. */ static int wl_handle_x11_events(int fd, uint32_t mask, void *data) { @@ -316,6 +317,11 @@ wl_handle_x11_events(int fd, uint32_t mask, void *data) } return 0; } +#else +static int +wl_handle_x11_events(int fd, uint32_t mask, void *data) +{ (void)fd; (void)mask; (void)data; return 0; } +#endif void * wl_display_open(const char *name) @@ -335,15 +341,11 @@ wl_display_open(const char *name) wl_list_init(&wl_state.real_pointer_axis_listener.link); wl_list_init(&wl_state.real_pointer_destroy_listener.link); +#if USE_X11_BACKEND XInitThreads(); - - /* Install X error handler immediately: Mesa/EGL may open a connection to - * DISPLAY (e.g. the host compositor's XWayland) during wlr_backend_start, - * which happens before StartUp() calls register_error_handlers(). - * Installing here prevents Xlib's default handler from aborting the - * process on the benign BadMatch that XWayland generates on SetInputFocus - * during WM startup. */ + /* Install X error handler: Mesa/EGL via XWayland */ XSetErrorHandler(wl_catch_xwayland_error); +#endif wlr_log_init(WLR_ERROR, NULL); @@ -1007,10 +1009,12 @@ wl_display_close(void) wl_event_source_remove(wl_state.x11_event_source); wl_state.x11_event_source = NULL; } +#if USE_X11_BACKEND if (wl_state.x_display) { XCloseDisplay(wl_state.x_display); wl_state.x_display = NULL; } +#endif /* shut down XWayland. * @@ -1730,7 +1734,8 @@ handle_layer_shell_new_surface(struct wl_listener *listener, void *data) void wl_screen_set_root_cursor(WScreen *scr, int cursor_id) { (void)scr; (void)cursor_id; } void wl_screen_set_icon_sizes(WScreen *scr, int min_size, int max_size) { (void)scr; (void)min_size; (void)max_size; } void wl_screen_setup_noticeboard(WScreen *scr) { (void)scr; } - void +#if USE_X11_BACKEND +void wl_screen_alloc_gcs(WScreen *scr) { XGCValues gcv; @@ -1758,6 +1763,7 @@ wl_screen_alloc_gcs(WScreen *scr) * the explicit window-ID filter in handle_xwayland_new_surface, this * prevents BadDrawable/RenderBadPicture cascade and the resulting broken * pipe from the nested Wayland backend. */ +#if USE_X11_BACKEND if (scr->w_win) { wl_state.rcontext_xwin = (WNativeWindow)scr->w_win; XSetWindowAttributes or_attr; @@ -1765,6 +1771,7 @@ wl_screen_alloc_gcs(WScreen *scr) XChangeWindowAttributes(wl_state.x_display, (WNativeWindow)scr->w_win, CWOverrideRedirect, &or_attr); XFlush(wl_state.x_display); } +#endif scr->stipple_bitmap = XCreateBitmapFromData(wl_state.x_display, (WNativeWindow)scr->w_win, stipple_data, 2, 2); @@ -1825,6 +1832,10 @@ wl_screen_alloc_gcs(WScreen *scr) scr->mono_gc = XCreateGC(wl_state.x_display, scr->stipple_bitmap, GCGraphicsExposures, &gcv); } +#else +void wl_screen_alloc_gcs(WScreen *scr) { (void)scr; } +#endif + void wl_screen_create_internals(WScreen *scr) { @@ -1833,10 +1844,8 @@ wl_screen_create_internals(WScreen *scr) FILE *fp = fopen("/tmp/wl_backend_trace.log", "a"); if (fp) { fprintf(fp, "wl_screen_create_internals: scr=%p\n", (void*)scr); fclose(fp); } } - /* Create a real off-screen InputOnly X window that absorbs keyboard focus - * when no application window holds it. This mirrors x11_screen_create_internals - * exactly: without a mapped, viewable focus-sink window, any XSetInputFocus - * call targeting it (serial ~1626 during manageAllWindows) returns BadMatch. */ +#if USE_X11_BACKEND + /* Create off-screen InputOnly X window as focus sink */ if (wl_state.x_display) { XSetWindowAttributes attribs; int vmask = CWEventMask | CWOverrideRedirect; @@ -1857,6 +1866,7 @@ wl_screen_create_internals(WScreen *scr) scr->no_focus_win = (WNativeWindow)xwin; } } +#endif wPreferences.auto_focus = 1; if (wl_state.seat) @@ -1864,11 +1874,8 @@ wl_screen_create_internals(WScreen *scr) wSetModifierMasks(WM_MOD_MOD1, WM_MOD_MOD1, WM_MOD_MOD4, 0); - /* XWayland bridge: create a real X11 helper window for EWMH - * (_NET_SUPPORTING_WM_CHECK) and clipboard selection ownership. - * override_redirect=True prevents XWayland's XWM from managing it as a - * client window (otherwise it appears as class=(none) and generates - * BadDrawable errors from wlroots when wManageWindow tries to reparent it). */ +#if USE_X11_BACKEND + /* XWayland bridge: create X11 helper window for EWMH */ if (wl_state.x_display) { XSetWindowAttributes iattr; iattr.override_redirect = True; @@ -1879,6 +1886,7 @@ wl_screen_create_internals(WScreen *scr) if (xwin) scr->info_window = (WNativeWindow)xwin; } +#endif /* Create dock_shadow: scene_buffer used as ghost overlay during icon drag */ if (wl_state.scene) { diff --git a/src/backend/wayland/wl_event.c b/src/backend/wayland/wl_event.c index a2d6825..91d1d79 100644 --- a/src/backend/wayland/wl_event.c +++ b/src/backend/wayland/wl_event.c @@ -19,3 +19,7 @@ void wl_event_drain_motion(void) {} void wl_event_discard_enter_leave_until(unsigned long timestamp) { (void)timestamp; } void wl_event_handle_extensions(WMEvent *event) { (void)event; } void wl_event_randr_update(WMEvent *event) { (void)event; } +void wl_event_handle_signal_exit(void) +{ + wm_backend->event_loop_terminate(); +} diff --git a/src/backend/wayland/wl_stubs.c b/src/backend/wayland/wl_stubs.c index 873aa86..376e65d 100644 --- a/src/backend/wayland/wl_stubs.c +++ b/src/backend/wayland/wl_stubs.c @@ -397,11 +397,13 @@ const WMBackend wl_backend = { .cursors_init = wl_cursors_init, .cursor_create_named = wl_cursor_create_named, .shape_combine_mask = wl_shape_combine_mask, +#if USE_X11_BACKEND .register_error_handlers = wl_register_error_handlers, - .texture_alloc_color = wl_texture_alloc_color, - .texture_free_colors = wl_texture_free_colors, .texture_create_gc = wl_texture_create_gc, .texture_free_gc = wl_texture_free_gc, +#endif + .texture_alloc_color = wl_texture_alloc_color, + .texture_free_colors = wl_texture_free_colors, .frame_alloc_border_pixel = wl_noop_frame_alloc_border_pixel, .frame_update_texture = wl_noop_frame_update_texture, .frame_paint_button = wl_frame_paint_button, diff --git a/src/backend/wayland/wl_window.c b/src/backend/wayland/wl_window.c index 689dfdd..4600842 100644 --- a/src/backend/wayland/wl_window.c +++ b/src/backend/wayland/wl_window.c @@ -692,13 +692,13 @@ int wl_window_get_command(WNativeWindow win, char ***argv_out, int *argc_out) const char *cmd = NULL; if (v->xw_surface) { +#if USE_X11_BACKEND /* XWayland: try WM_COMMAND first via the X server. */ if (wl_state.x_display && v->xw_surface->window_id) { char **cmd_argv = NULL; int cmd_argc = 0; if (XGetCommand(wl_state.x_display, v->xw_surface->window_id, &cmd_argv, &cmd_argc) && cmd_argc > 0 && cmd_argv) { - /* Copy to wmaker's allocator */ *argv_out = wmalloc(sizeof(char *) * cmd_argc); for (int i = 0; i < cmd_argc; i++) (*argv_out)[i] = wstrdup(cmd_argv[i]); @@ -707,6 +707,7 @@ int wl_window_get_command(WNativeWindow win, char ***argv_out, int *argc_out) return 1; } } +#endif cmd = v->xw_surface->instance ? v->xw_surface->instance : v->xw_surface->class; } else if (v->xdg_surface && v->xdg_surface->toplevel) { diff --git a/src/backend/wayland/wl_xdisplay.c b/src/backend/wayland/wl_xdisplay.c index a1e7388..0f95788 100644 --- a/src/backend/wayland/wl_xdisplay.c +++ b/src/backend/wayland/wl_xdisplay.c @@ -12,6 +12,7 @@ Display *wl_get_x_display(void) return wl_state.x_display; } +#if USE_X11_BACKEND unsigned long wl_xdisplay_intern_atom(const char *name) { @@ -33,6 +34,11 @@ wl_xdisplay_sync(int discard) if (wl_state.x_display) XSync(wl_state.x_display, discard ? True : False); } +#else +unsigned long wl_xdisplay_intern_atom(const char *name) { (void)name; return 0; } +void wl_xdisplay_flush(void) {} +void wl_xdisplay_sync(int discard) { (void)discard; } +#endif RContext *wl_create_rcontext(int screen_number, RContextAttributes *attribs) { diff --git a/src/backend/wayland/wl_xwayland.c b/src/backend/wayland/wl_xwayland.c index 987e1cb..e2149de 100644 --- a/src/backend/wayland/wl_xwayland.c +++ b/src/backend/wayland/wl_xwayland.c @@ -380,6 +380,7 @@ handle_xwayland_new_surface(struct wl_listener *listener, void *data) /* ------------------------------------------------------------------ */ /* XWayland snapshot capture — called from wl_client_snapshot() */ +#if USE_X11_BACKEND RImage * wl_xwayland_capture_snapshot(WScreen *scr, struct wlr_xwayland_surface *xw) { @@ -398,9 +399,6 @@ wl_xwayland_capture_snapshot(WScreen *scr, struct wlr_xwayland_surface *xw) return res; } -/* ------------------------------------------------------------------ */ -/* Read _NET_WM_WINDOW_OPACITY from an XWayland client */ - float wl_xwayland_get_window_opacity(WNativeWindow client_win) { @@ -423,3 +421,9 @@ wl_xwayland_get_window_opacity(WNativeWindow client_win) XFree(prop); return (float)val / (float)0xFFFFFFFF; } +#else +RImage *wl_xwayland_capture_snapshot(WScreen *scr, struct wlr_xwayland_surface *xw) +{ (void)scr; (void)xw; return NULL; } +float wl_xwayland_get_window_opacity(WNativeWindow client_win) +{ (void)client_win; return 1.0f; } +#endif