Linux window handle

This commit is contained in:
2026-05-11 18:43:11 -05:00
parent 71a5964041
commit a952b94a15
4 changed files with 25 additions and 17 deletions

View File

@@ -25,6 +25,12 @@ SOFTWARE.
#include "./base/ci2_base.h" #include "./base/ci2_base.h"
struct CI2_Window_Handle {
ci2_fd fd;
ci2_bool should_close;
};
typedef struct CI2_Window_Handle CI2_Window_Handle;
// Opaque type for a window // Opaque type for a window
typedef struct CI2_Window CI2_Window; typedef struct CI2_Window CI2_Window;

View File

@@ -30,10 +30,9 @@ SOFTWARE.
const ci2_sys_char *ci2_window_class = "Linux CI2 Window"; const ci2_sys_char *ci2_window_class = "Linux CI2 Window";
struct CI2_Window { struct CI2_Window {
struct CI2_Window_Handle handle;
Display* display; Display* display;
Window window;
Atom wm_delete_window; Atom wm_delete_window;
ci2_bool should_close;
}; };
ci2_bool ci2_init(void) { ci2_bool ci2_init(void) {
@@ -58,18 +57,19 @@ CI2_Window* ci2_create_window(const ci2_sys_char* title, int width, int height)
XMapWindow(display, win); XMapWindow(display, win);
XFlush(display); XFlush(display);
CI2_Window* window = malloc(sizeof(CI2_Window)); CI2_Window* window = malloc(sizeof(CI2_Window));
// CI2_Window_Handle* h = malloc(sizeof(CI2_Window_Handle));
if (!window) { if (!window) {
XDestroyWindow(display, win); XDestroyWindow(display, win);
XCloseDisplay(display); XCloseDisplay(display);
return NULL; return NULL;
} }
// window->handle = h;
window->display = display; window->display = display;
window->window = win;
window->wm_delete_window = wm_delete_window; window->wm_delete_window = wm_delete_window;
window->should_close = ci2_false; window->handle.should_close = ci2_false;
window->handle.fd = win;
return window; return window;
} }
@@ -82,17 +82,17 @@ ci2_bool ci2_poll_events(CI2_Window* window) {
if (event.type == ClientMessage && if (event.type == ClientMessage &&
(Atom)event.xclient.data.l[0] == window->wm_delete_window) { (Atom)event.xclient.data.l[0] == window->wm_delete_window) {
window->should_close = ci2_true; window->handle.should_close = ci2_true;
} }
} }
return !window->should_close; return !window->handle.should_close;
} }
void ci2_destroy_window(CI2_Window* window) { void ci2_destroy_window(CI2_Window* window) {
if (!window) return; if (!window) return;
XDestroyWindow(window->display, window->window); XDestroyWindow(window->display, window->handle.fd);
XCloseDisplay(window->display); XCloseDisplay(window->display);
free(window); free(window);
} }

View File

@@ -24,6 +24,7 @@ SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
struct CI2_Window { struct CI2_Window {
CI2_Window_Handle handle;
HWND hwnd; HWND hwnd;
ci2_bool should_close; ci2_bool should_close;
}; };
@@ -69,12 +70,13 @@ CI2_Window* ci2_create_window(const ci2_sys_char* title, int width, int height)
return NULL; return NULL;
} }
window->hwnd = hwnd; window->handle.fd = hwnd;
window->should_close = false; window->handle.should_close = ci2_false;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window); SetWindowLongPtr(window->handle.fd, GWLP_USERDATA, (LONG_PTR)window);
ShowWindow(window->hwnd, SW_SHOWNORMAL); // Maybe?
UpdateWindow(window->hwnd); ShowWindow(window->handle.fd, SW_SHOWNORMAL);
UpdateWindow(window->handle.fd);
return window; return window;
} }
@@ -85,12 +87,12 @@ ci2_bool ci2_poll_events(CI2_Window* window) {
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
} }
return !window->should_close; return !window->handle.should_close;
} }
void ci2_destroy_window(CI2_Window* window) { void ci2_destroy_window(CI2_Window* window) {
if (!window) return; if (!window) return;
DestroyWindow(window->hwnd); DestroyWindow(window->handle.fd);
free(window); free(window);
} }
@@ -109,7 +111,7 @@ static LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
switch (uMsg) { switch (uMsg) {
case WM_CLOSE: case WM_CLOSE:
if (window) window->should_close = true; if (window) window->handle.should_close = ci2_true;
return 0; return 0;
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage(0); PostQuitMessage(0);

BIN
tests/bin/01_ci2 Executable file

Binary file not shown.