#ifndef PLATFORM_LINUX_WAYLAND_H #define PLATFORM_LINUX_WAYLAND_H // NOTE: no shm buffers here (unlike loo). vulkan owns presenting: vkCreateWaylandSurfaceKHR // only needs the wl_display and wl_surface, the driver attaches and commits buffers itself. typedef struct WaylandState WaylandState; struct WaylandState { //- globals struct wl_display *display; struct wl_registry *registry; struct wl_compositor *compositor; struct xdg_wm_base *xdg_wm_base; struct zwlr_layer_shell_v1 *layer_shell; struct wl_seat *seat; struct wp_cursor_shape_manager_v1 *cursor_shape_manager; struct zxdg_decoration_manager_v1 *decoration_manager; u32 compositor_version; u32 layer_shell_version; //- surface struct wl_surface *surface; struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; struct zwlr_layer_surface_v1 *layer_surface; struct zxdg_toplevel_decoration_v1 *decoration; b32 is_layer; b32 maximized; b32 suspended; // xdg-shell v6: compositor says we're not visible (niri: other workspace) //- frame pacing // NOTE: we only draw when the compositor asked for a frame (wl_surface.frame). a window that // is scrolled away or on another workspace gets no callbacks, so we simply stop drawing // instead of blocking inside vkQueuePresentKHR. struct wl_callback *frame_callback; u64 last_frame_done_ns; // when the compositor last consumed a frame u64 frame_requested_ns; // when we asked for the pending callback. unanswered for long = hidden //- input struct wl_keyboard *keyboard; struct wl_pointer *pointer; struct wp_cursor_shape_device_v1 *cursor_shape_device; struct xkb_context *xkb_context; struct xkb_keymap *xkb_keymap; struct xkb_state *xkb_state; PlatformInput input; u32 last_button_serial; // needed for xdg_toplevel.move u32 pointer_serial; // last enter, needed to change the cursor shape u32 cursor; // PlatformCursor currently shown // key repeat, wayland leaves it to the client s32 repeat_rate; // per second, 0 = off s32 repeat_delay_ms; u32 repeat_key; // evdev keycode, 0 = nothing held u64 repeat_next_ns; //- size u32 width; // logical u32 height; u32 scale; // integer buffer scale b32 resized; b32 configured; b32 should_close; }; struct PlatformWindow { WaylandState wayland; }; internal PlatformWindow *wayland_window_open(Arena *arena, WindowParams params); // NOTE: blocks up to timeout_ms (-1 forever) for wayland events or extra_fd becoming readable. // fills input. false once the window should close. internal b32 wayland_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask); internal void wayland_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale); internal b32 wayland_window_take_resized(PlatformWindow *window); internal void wayland_window_close(PlatformWindow *window); internal void wayland_window_begin_move(PlatformWindow *window); internal void wayland_window_toggle_maximized(PlatformWindow *window); internal void wayland_window_request_frame(PlatformWindow *window); // call right before presenting internal void wayland_window_cancel_frame(PlatformWindow *window); // the present didn't happen internal b32 wayland_window_frame_ready(PlatformWindow *window); internal void wayland_window_set_cursor(PlatformWindow *window, PlatformCursor cursor); #define PlatformWindowOpen(arena, params) wayland_window_open(arena, params) #define PlatformWindowPump(window, input, timeout_ns, fds, count, ready) wayland_window_pump(window, input, timeout_ns, fds, count, ready) #define PlatformWindowLastFrameDone(window) ((window)->wayland.last_frame_done_ns) #define PlatformWindowFrameRequested(window) ((window)->wayland.frame_requested_ns) #define PlatformWindowSize(window, w, h, s) wayland_window_size(window, w, h, s) #define PlatformWindowTakeResized(window) wayland_window_take_resized(window) #define PlatformWindowClose(window) wayland_window_close(window) #define PlatformWindowBeginMove(window) wayland_window_begin_move(window) #define PlatformWindowToggleMaximized(window) wayland_window_toggle_maximized(window) #define PlatformWindowRequestFrame(window) wayland_window_request_frame(window) #define PlatformWindowCancelFrame(window) wayland_window_cancel_frame(window) #define PlatformWindowFrameReady(window) wayland_window_frame_ready(window) #define PlatformWindowIsLayer(window) ((window)->wayland.is_layer) #define PlatformWindowSetCursor(window, cursor) wayland_window_set_cursor(window, cursor) #define PlatformWindowSuspended(window) ((window)->wayland.suspended) #endif // PLATFORM_LINUX_WAYLAND_H