SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
window_x11.c File Reference
#include "linux_internal.h"
#include "core/sbgl_platform.h"
#include "core/sbl_arena.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/extensions/Xfixes.h>
#include <time.h>
#include <string.h>
Include dependency graph for window_x11.c:

Go to the source code of this file.

Functions

void x11_internal_process_event (XEvent *event, sbgl_Window *window)
 
sbgl_Windowsbgl_os_CreateWindow (struct SblArena *arena, sbgl_InputState *input, int width, int height, const char *title)
 
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.
 
bool sbgl_os_IsWindowFocused (sbgl_Window *window)
 Checks if the window currently has input focus.
 
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.
 
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).
 

Function Documentation

◆ sbgl_os_CreateWindow()

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

Definition at line 14 of file window_x11.c.

14 {
15 Display* display = XOpenDisplay(NULL);
16 if (!display) return NULL;
17
18 int screen = DefaultScreen(display);
19
20 Window win = XCreateSimpleWindow(
21 display, RootWindow(display, screen),
22 0, 0, (unsigned int)width, (unsigned int)height, 0,
23 BlackPixel(display, screen),
24 WhitePixel(display, screen)
25 );
26
27 XStoreName(display, win, title);
28 XSelectInput(display, win, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | StructureNotifyMask | PointerMotionMask | FocusChangeMask);
29
30 Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
31 XSetWMProtocols(display, win, &wmDeleteMessage, 1);
32
33 XMapWindow(display, win);
34 XFlush(display);
35
37 if (!window) {
38 XCloseDisplay(display);
39 return NULL;
40 }
41 window->display = display;
42 window->window = win;
43 window->wmDeleteMessage = wmDeleteMessage;
44 window->input = input;
45 window->shouldClose = false;
46 window->resized = false;
47 window->width = width;
48 window->height = height;
49
50 return window;
51}
#define SBL_ARENA_PUSH_STRUCT_ZERO(arena, type)
Definition sbl_arena.h:20
Native X11 window state.
sbgl_InputState * input
Display * display

◆ sbgl_os_DestroyWindow()

void sbgl_os_DestroyWindow ( sbgl_Window * window)

Destroys a native window.

Parameters
windowThe window to destroy.

Definition at line 53 of file window_x11.c.

53 {
54 if (!window) return;
55 XDestroyWindow(window->display, window->window);
56 XCloseDisplay(window->display);
57}

◆ 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 157 of file window_x11.c.

157 {
158 return window ? (void*)window->display : NULL;
159}

◆ 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 148 of file window_x11.c.

148 {
149 return window ? (void*)(uintptr_t)window->window : NULL;
150}

◆ 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 136 of file window_x11.c.

136 {
137 (void)window;
138 struct timespec ts;
139 clock_gettime(CLOCK_MONOTONIC, &ts);
140 return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
141}

◆ 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 143 of file window_x11.c.

143 {
144 (void)window;
145 return 1000000000ULL;
146}

◆ 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 63 of file window_x11.c.

63 {
64 if (window) {
65 *w = window->width;
66 *h = window->height;
67 }
68}

◆ 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 76 of file window_x11.c.

76 {
77 return window ? window->focused : false;
78}

◆ 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 107 of file window_x11.c.

107 {
108 if (!window || !window->display) return;
109
111
112 while (XPending(window->display)) {
113 XEvent event;
114 XNextEvent(window->display, &event);
115
116 if (event.type == ClientMessage) {
117 if ((Atom)event.xclient.data.l[0] == window->wmDeleteMessage) {
118 window->shouldClose = true;
119 }
120 } else if (event.type == ConfigureNotify) {
121 int w = event.xconfigure.width;
122 int h = event.xconfigure.height;
123 if (w > 0 && h > 0 && (w != window->width || h != window->height)) {
124 window->width = w;
125 window->height = h;
126 window->resized = true;
127 }
128 }
129
130 // Pass event to input system
131 x11_internal_process_event(&event, window);
132 }
133}
void linux_internal_update_input_states(sbgl_Window *window)
void x11_internal_process_event(XEvent *event, sbgl_Window *window)
Definition input_x11.c:32

◆ 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 92 of file window_x11.c.

92 {
93 if (!window || !window->display) return;
94
95 // Constrains or releases the pointer relative to the window.
96 if (locked) {
97 XGrabPointer(window->display, window->window, False,
98 ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
99 GrabModeAsync, GrabModeAsync,
100 window->window, None, CurrentTime);
101 } else {
102 XUngrabPointer(window->display, CurrentTime);
103 }
104 XFlush(window->display);
105}

◆ 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 80 of file window_x11.c.

80 {
81 if (!window || !window->display) return;
82
83 // Adjusts cursor visibility using XFixes extension.
84 if (visible) {
85 XFixesShowCursor(window->display, window->window);
86 } else {
87 XFixesHideCursor(window->display, window->window);
88 }
89 XFlush(window->display);
90}

◆ 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 70 of file window_x11.c.

70 {
71 bool r = window->resized;
72 window->resized = false;
73 return r;
74}

◆ 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 59 of file window_x11.c.

59 {
60 return window ? window->shouldClose : true;
61}

◆ x11_internal_process_event()

void x11_internal_process_event ( XEvent * event,
sbgl_Window * window )

Definition at line 32 of file input_x11.c.

32 {
33 sbgl_InputState* input = window->input;
34 switch (event->type) {
35 case KeyPress: {
36 KeySym keysym = XLookupKeysym(&event->xkey, 0);
38 if (code < SBGL_SCANCODE_MAX) {
39 if (!input->keysDown[code]) input->keysPressed[code] = true;
40 input->keysDown[code] = true;
41 }
42 break;
43 }
44 case KeyRelease: {
45 // X11 Auto-repeat handling
46 if (XEventsQueued(window->display, QueuedAfterReading)) {
47 XEvent next;
48 XPeekEvent(window->display, &next);
49 if (next.type == KeyPress && next.xkey.time == event->xkey.time && next.xkey.keycode == event->xkey.keycode) {
50 XNextEvent(window->display, event); // Consume repeat
51 break;
52 }
53 }
54 KeySym keysym = XLookupKeysym(&event->xkey, 0);
56 if (code < SBGL_SCANCODE_MAX) input->keysDown[code] = false;
57 break;
58 }
59 case MotionNotify: {
60 input->mouseX = event->xmotion.x;
61 input->mouseY = event->xmotion.y;
62 break;
63 }
64 case ButtonPress:
65 case ButtonRelease: {
66 bool down = (event->type == ButtonPress);
67 int btn = -1;
68 if (event->xbutton.button == Button1) btn = SBGL_MOUSE_BUTTON_LEFT;
69 else if (event->xbutton.button == Button3) btn = SBGL_MOUSE_BUTTON_RIGHT;
70 else if (event->xbutton.button == Button2) btn = SBGL_MOUSE_BUTTON_MIDDLE;
71 if (btn != -1 && btn < SBGL_MOUSE_BUTTON_MAX) input->mouseDown[btn] = down;
72 break;
73 }
74 case FocusIn: {
75 window->focused = true;
76 break;
77 }
78 case FocusOut: {
79 window->focused = false;
80 break;
81 }
82 }
83}
SBGL_Scancode
OS-independent physical scancodes.
Definition sbgl_input.h:21
@ SBGL_SCANCODE_MAX
Definition sbgl_input.h:137
@ SBGL_MOUSE_BUTTON_MIDDLE
Definition sbgl_input.h:146
@ SBGL_MOUSE_BUTTON_MAX
Definition sbgl_input.h:147
@ SBGL_MOUSE_BUTTON_LEFT
Definition sbgl_input.h:144
@ SBGL_MOUSE_BUTTON_RIGHT
Definition sbgl_input.h:145
static SBGL_Scancode x11_keysym_to_scancode(KeySym keysym)
Definition input_x11.c:7
Represents the real-time state of physical inputs.
Definition sbgl_input.h:165
bool mouseDown[SBGL_MOUSE_BUTTON_MAX]
Definition sbgl_input.h:168
bool keysPressed[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:167
bool keysDown[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:166