SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
window_x11.c
Go to the documentation of this file.
1#include "linux_internal.h"
3#include "core/sbl_arena.h"
4#include <X11/Xlib.h>
5#include <X11/Xutil.h>
6#include <X11/keysym.h>
7#include <X11/extensions/Xfixes.h>
8#include <time.h>
9#include <string.h>
10
11// Defined in input_x11.c
12void x11_internal_process_event(XEvent* event, sbgl_Window* window);
13
14sbgl_Window* sbgl_os_CreateWindow(struct SblArena* arena, sbgl_InputState* input, int width, int height, const char* title) {
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}
52
54 if (!window) return;
55 XDestroyWindow(window->display, window->window);
56 XCloseDisplay(window->display);
57}
58
60 return window ? window->shouldClose : true;
61}
62
63void sbgl_os_GetWindowSize(sbgl_Window* window, int* w, int* h) {
64 if (window) {
65 *w = window->width;
66 *h = window->height;
67 }
68}
69
71 bool r = window->resized;
72 window->resized = false;
73 return r;
74}
75
77 return window ? window->focused : false;
78}
79
80void sbgl_os_SetCursorVisible(sbgl_Window* window, bool visible) {
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}
91
92void sbgl_os_SetCursorLocked(sbgl_Window* window, bool locked) {
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}
106
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}
134
135
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}
142
144 (void)window;
145 return 1000000000ULL;
146}
147
149 return window ? (void*)(uintptr_t)window->window : NULL;
150}
151
153 (void)window;
154 return NULL;
155}
156
158 return window ? (void*)window->display : NULL;
159}
void linux_internal_update_input_states(sbgl_Window *window)
Internal Platform Abstraction Layer (HAL).
Arena allocator implementation.
#define SBL_ARENA_PUSH_STRUCT_ZERO(arena, type)
Definition sbl_arena.h:20
Arena allocator.
Definition sbl_arena.h:47
Represents the real-time state of physical inputs.
Definition sbgl_input.h:165
Native X11 window state.
sbgl_InputState * input
Display * display
bool sbgl_os_WasWindowResized(sbgl_Window *window)
Checks if the window has been resized since the last check.
Definition window_x11.c:70
uint64_t sbgl_os_GetPerfCount(sbgl_Window *window)
Gets the high-resolution performance counter.
Definition window_x11.c:136
void x11_internal_process_event(XEvent *event, sbgl_Window *window)
Definition input_x11.c:32
void sbgl_os_PollEvents(sbgl_Window *window)
Dispatches OS events (messages/protocol requests).
Definition window_x11.c:107
void sbgl_os_SetCursorLocked(sbgl_Window *window, bool locked)
Locks or unlocks the cursor within the window bounds.
Definition window_x11.c:92
uint64_t sbgl_os_GetPerfFreq(sbgl_Window *window)
Gets the performance counter frequency.
Definition window_x11.c:143
void * sbgl_os_GetNativeWindowHandle(sbgl_Window *window)
Retrieves the raw window handle for Vulkan surface creation.
Definition window_x11.c:148
void sbgl_os_SetCursorVisible(sbgl_Window *window, bool visible)
Sets the visibility of the OS cursor for the given window.
Definition window_x11.c:80
sbgl_Window * sbgl_os_CreateWindow(struct SblArena *arena, sbgl_InputState *input, int width, int height, const char *title)
Definition window_x11.c:14
bool sbgl_os_WindowShouldClose(sbgl_Window *window)
Checks the window's close flag.
Definition window_x11.c:59
void sbgl_os_GetWindowSize(sbgl_Window *window, int *w, int *h)
Retrieves the current client area size.
Definition window_x11.c:63
bool sbgl_os_IsWindowFocused(sbgl_Window *window)
Checks if the window currently has input focus.
Definition window_x11.c:76
void * sbgl_os_GetNativeDisplayHandle(sbgl_Window *window)
Retrieves the native display handle (Linux specific).
Definition window_x11.c:157
void * sbgl_os_GetNativeInstanceHandle(sbgl_Window *window)
Retrieves the native instance handle (Win32 specific).
Definition window_x11.c:152
void sbgl_os_DestroyWindow(sbgl_Window *window)
Destroys a native window.
Definition window_x11.c:53