SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
sbgl_platform.h File Reference

Internal Platform Abstraction Layer (HAL). More...

#include "sbgl_types.h"
#include "sbgl_input.h"
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for sbgl_platform.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

sbgl_platform_Result sbgl_os_CreateWindow (struct SblArena *arena, sbgl_InputState *input, int width, int height, const char *title, sbgl_Window **outWindow)
 Creates a native OS window.
 
void sbgl_os_DestroyWindow (sbgl_Window *window)
 Destroys a native window.
 
bool sbgl_os_WindowShouldClose (sbgl_Window *window)
 Checks the window's close flag.
 
void sbgl_os_GetWindowSize (sbgl_Window *window, int *w, int *h)
 Retrieves the current client area size.
 
bool sbgl_os_WasWindowResized (sbgl_Window *window)
 Checks if the window has been resized since the last check.
 
void sbgl_os_SetCursorVisible (sbgl_Window *window, bool visible)
 Sets the visibility of the OS cursor for the given window.
 
void sbgl_os_SetCursorLocked (sbgl_Window *window, bool locked)
 Locks or unlocks the cursor within the window bounds.
 
bool sbgl_os_IsWindowFocused (sbgl_Window *window)
 Checks if the window currently has input focus.
 
void sbgl_os_PollEvents (sbgl_Window *window)
 Dispatches OS events (messages/protocol requests).
 
uint64_t sbgl_os_GetPerfCount (sbgl_Window *window)
 Gets the high-resolution performance counter.
 
uint64_t sbgl_os_GetPerfFreq (sbgl_Window *window)
 Gets the performance counter frequency.
 
void * sbgl_os_GetNativeWindowHandle (sbgl_Window *window)
 Retrieves the raw window handle for Vulkan surface creation.
 
void * sbgl_os_GetNativeInstanceHandle (sbgl_Window *window)
 Retrieves the native instance handle (Win32 specific).
 
void * sbgl_os_GetNativeDisplayHandle (sbgl_Window *window)
 Retrieves the native display handle (Linux specific).
 

Detailed Description

Internal Platform Abstraction Layer (HAL).

Defines the strict interface that OS-specific implementations must follow. Prevents OS headers (windows.h, wayland-client.h) from leaking into the core.

Definition in file sbgl_platform.h.

Function Documentation

◆ sbgl_os_CreateWindow()

sbgl_platform_Result sbgl_os_CreateWindow ( struct SblArena * arena,
sbgl_InputState * input,
int width,
int height,
const char * title,
sbgl_Window ** outWindow )

Creates a native OS window.

Parameters
arenaArena to allocate the window state from.
inputPointer to input state.
widthInitial width.
heightInitial height.
titleWindow title.
outWindowPointer to store the created window.
Returns
Platform result code.

Definition at line 68 of file window_wayland.c.

68 {
69 if (!outWindow) {
71 }
72 *outWindow = NULL;
73
74 if (!arena) {
76 }
77
79 if (!window) return SBGL_PLATFORM_ERROR_INIT_FAILED;
80
81 window->width = width; window->height = height;
82 window->shouldClose = false;
83 window->resized = false;
84 window->focused = false;
85 window->cursor_visible = true;
86 window->input = input;
87
88 window->display = wl_display_connect(NULL);
89 if (!window->display) return SBGL_PLATFORM_ERROR_NO_DISPLAY;
90
91 struct wl_registry* reg = wl_display_get_registry(window->display);
92 wl_registry_add_listener(reg, &registry_listener, window);
93 wl_display_roundtrip(window->display);
94
95 window->surface = wl_compositor_create_surface(window->compositor);
96 if (!window->surface) return SBGL_PLATFORM_ERROR_WINDOW_FAILED;
97
98 window->xdg_surface = xdg_wm_base_get_xdg_surface(window->wm_base, window->surface);
99 if (!window->xdg_surface) return SBGL_PLATFORM_ERROR_WINDOW_FAILED;
100
101 xdg_surface_add_listener(window->xdg_surface, &xdg_surface_listener, window);
102 window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
103 if (!window->xdg_toplevel) return SBGL_PLATFORM_ERROR_WINDOW_FAILED;
104
105 xdg_toplevel_add_listener(window->xdg_toplevel, &toplevel_listener, window);
106 xdg_toplevel_set_title(window->xdg_toplevel, title);
107
108 if (window->seat) {
109 struct wl_keyboard* k = wl_seat_get_keyboard(window->seat);
110 if (k) wl_keyboard_add_listener(k, &keyboard_listener, window);
111 struct wl_pointer* p = wl_seat_get_pointer(window->seat);
112 if (p) wl_pointer_add_listener(p, &pointer_listener, window);
113 }
114
115 wl_surface_commit(window->surface);
116 wl_display_roundtrip(window->display);
117
118 *outWindow = window;
120}
const struct wl_pointer_listener pointer_listener
const struct wl_keyboard_listener keyboard_listener
@ SBGL_PLATFORM_SUCCESS
Definition sbgl_types.h:234
@ SBGL_PLATFORM_ERROR_INIT_FAILED
Definition sbgl_types.h:237
@ SBGL_PLATFORM_ERROR_NO_DISPLAY
Definition sbgl_types.h:236
@ SBGL_PLATFORM_ERROR_WINDOW_FAILED
Definition sbgl_types.h:235
#define SBL_ARENA_PUSH_STRUCT_ZERO(arena, type)
Definition sbl_arena.h:20
Native X11 window state.
sbgl_InputState * input
Display * display
static const struct xdg_surface_listener xdg_surface_listener
static const struct wl_registry_listener registry_listener
static const struct xdg_toplevel_listener toplevel_listener

◆ sbgl_os_DestroyWindow()

void sbgl_os_DestroyWindow ( sbgl_Window * window)

Destroys a native window.

Parameters
windowThe window to destroy.

Definition at line 205 of file window_wayland.c.

205 {
206 if (!window) return;
207 if (window->relative_pointer) zwp_relative_pointer_v1_destroy(window->relative_pointer);
208 if (window->locked_pointer) zwp_locked_pointer_v1_destroy(window->locked_pointer);
209 if (window->relative_pointer_manager) zwp_relative_pointer_manager_v1_destroy(window->relative_pointer_manager);
210 if (window->pointer_constraints) zwp_pointer_constraints_v1_destroy(window->pointer_constraints);
211 xdg_toplevel_destroy(window->xdg_toplevel);
212 xdg_surface_destroy(window->xdg_surface);
213 wl_surface_destroy(window->surface);
214 wl_display_disconnect(window->display);
215}

◆ sbgl_os_GetNativeDisplayHandle()

void * sbgl_os_GetNativeDisplayHandle ( sbgl_Window * window)

Retrieves the native display handle (Linux specific).

Parameters
windowThe window handle.
Returns
Display* or wl_display*.

Definition at line 204 of file window_wayland.c.

204{ return (void*)window->display; }

◆ sbgl_os_GetNativeInstanceHandle()

void * sbgl_os_GetNativeInstanceHandle ( sbgl_Window * window)

Retrieves the native instance handle (Win32 specific).

Parameters
windowThe window handle.
Returns
HINSTANCE on Windows, NULL otherwise.

Definition at line 152 of file window_x11.c.

152 {
153 (void)window;
154 return NULL;
155}

◆ sbgl_os_GetNativeWindowHandle()

void * sbgl_os_GetNativeWindowHandle ( sbgl_Window * window)

Retrieves the raw window handle for Vulkan surface creation.

Parameters
windowThe window handle.
Returns
Native handle (e.g., HWND or wl_surface*).

Definition at line 203 of file window_wayland.c.

203{ return (void*)window->surface; }

◆ sbgl_os_GetPerfCount()

uint64_t sbgl_os_GetPerfCount ( sbgl_Window * window)

Gets the high-resolution performance counter.

Parameters
windowThe window handle.
Returns
Absolute ticks.

Definition at line 201 of file window_wayland.c.

201{ (void)window; struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ULL + ts.tv_nsec; }

◆ sbgl_os_GetPerfFreq()

uint64_t sbgl_os_GetPerfFreq ( sbgl_Window * window)

Gets the performance counter frequency.

Parameters
windowThe window handle.
Returns
Ticks per second.

Definition at line 202 of file window_wayland.c.

202{ (void)window; return 1000000000ULL; }

◆ sbgl_os_GetWindowSize()

void sbgl_os_GetWindowSize ( sbgl_Window * window,
int * w,
int * h )

Retrieves the current client area size.

Parameters
windowThe window handle.
wPointer to store width.
hPointer to store height.

Definition at line 150 of file window_wayland.c.

150{ *w = window->width; *h = window->height; }

◆ sbgl_os_IsWindowFocused()

bool sbgl_os_IsWindowFocused ( sbgl_Window * window)

Checks if the window currently has input focus.

Parameters
windowThe native window handle.
Returns
True if focused.

Definition at line 158 of file window_wayland.c.

158{ return window->focused; }

◆ sbgl_os_PollEvents()

void sbgl_os_PollEvents ( sbgl_Window * window)

Dispatches OS events (messages/protocol requests).

Parameters
windowThe window to process events for.

Definition at line 122 of file window_wayland.c.

122 {
123 if (!window) return;
124
125 // Clear deltas if relative pointer is active to allow accumulation in listeners
126 if (window->relative_pointer && window->input) {
127 window->input->mouseDeltaX = 0;
128 window->input->mouseDeltaY = 0;
129 }
130
131 /* The system ensures that pending events are dispatched and new events are read
132 from the display in a non-blocking manner to prevent main loop stalls. */
133 while (wl_display_prepare_read(window->display) != 0) {
134 wl_display_dispatch_pending(window->display);
135 }
136 wl_display_flush(window->display);
137
138 struct pollfd pfd = { .fd = wl_display_get_fd(window->display), .events = POLLIN };
139 if (poll(&pfd, 1, 0) > 0) {
140 wl_display_read_events(window->display);
141 } else {
142 wl_display_cancel_read(window->display);
143 }
144
145 wl_display_dispatch_pending(window->display);
147}
void linux_internal_update_input_states(sbgl_Window *window)

◆ sbgl_os_SetCursorLocked()

void sbgl_os_SetCursorLocked ( sbgl_Window * window,
bool locked )

Locks or unlocks the cursor within the window bounds.

When locked, the cursor is typically constrained to the window center to support relative motion for 3D navigation.

Parameters
windowThe native window handle.
lockedTrue to capture the cursor, false to release it.

Definition at line 175 of file window_wayland.c.

175 {
176 if (!window || !window->seat || !window->pointer_constraints) return;
177 struct wl_pointer* pointer = wl_seat_get_pointer(window->seat);
178 if (!pointer) return;
179
180 if (locked && !window->locked_pointer) {
181 window->locked_pointer = zwp_pointer_constraints_v1_lock_pointer(
182 window->pointer_constraints, window->surface, pointer, NULL,
183 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
184 zwp_locked_pointer_v1_add_listener(window->locked_pointer, &locked_pointer_listener, window);
185
186 if (window->relative_pointer_manager) {
187 window->relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
188 window->relative_pointer_manager, pointer);
189 zwp_relative_pointer_v1_add_listener(window->relative_pointer, &relative_pointer_listener, window);
190 }
191 } else if (!locked && window->locked_pointer) {
192 zwp_locked_pointer_v1_destroy(window->locked_pointer);
193 window->locked_pointer = NULL;
194 if (window->relative_pointer) {
195 zwp_relative_pointer_v1_destroy(window->relative_pointer);
196 window->relative_pointer = NULL;
197 }
198 }
199}
const struct zwp_relative_pointer_v1_listener relative_pointer_listener
static const struct zwp_locked_pointer_v1_listener locked_pointer_listener

◆ sbgl_os_SetCursorVisible()

void sbgl_os_SetCursorVisible ( sbgl_Window * window,
bool visible )

Sets the visibility of the OS cursor for the given window.

Provides a platform-agnostic way to show or hide the mouse pointer.

Parameters
windowThe native window handle.
visibleTrue to show the cursor, false to hide it.

Definition at line 160 of file window_wayland.c.

160 {
161 if (!window) return;
162 window->cursor_visible = visible;
163
164 if (!visible && window->seat) {
165 struct wl_pointer* pointer = wl_seat_get_pointer(window->seat);
166 if (pointer) {
167 // Setting the cursor surface to NULL hides the cursor for this surface.
168 wl_pointer_set_cursor(pointer, window->pointer_serial, NULL, 0, 0);
169 }
170 }
171}

◆ sbgl_os_WasWindowResized()

bool sbgl_os_WasWindowResized ( sbgl_Window * window)

Checks if the window has been resized since the last check.

Resets the internal resize flag to false upon returning.

Parameters
windowThe window handle.
Returns
True if a resize event occurred.

Definition at line 152 of file window_wayland.c.

152 {
153 bool r = window->resized;
154 window->resized = false;
155 return r;
156}

◆ sbgl_os_WindowShouldClose()

bool sbgl_os_WindowShouldClose ( sbgl_Window * window)

Checks the window's close flag.

Parameters
windowThe window to check.
Returns
True if closing.

Definition at line 149 of file window_wayland.c.

149{ return window->shouldClose; }