79 lines
2.0 KiB
C
79 lines
2.0 KiB
C
#include "../include/ci2_window.h"
|
|
#include <X11/Xlib.h>
|
|
#include <X11/Xutil.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
struct CI2_Window {
|
|
Display* display;
|
|
Window window;
|
|
Atom wm_delete_window;
|
|
bool should_close;
|
|
};
|
|
|
|
bool platform_init(void) {
|
|
return true; // Nothing global needed for X11
|
|
}
|
|
|
|
CI2_Window* platform_create_window(const char* title, int width, int height) {
|
|
Display* display = XOpenDisplay(NULL);
|
|
if (!display) return NULL;
|
|
|
|
int screen = DefaultScreen(display);
|
|
Window win = XCreateSimpleWindow(display, RootWindow(display, screen),
|
|
0, 0, width, height, 1,
|
|
BlackPixel(display, screen),
|
|
WhitePixel(display, screen));
|
|
|
|
XStoreName(display, win, title);
|
|
|
|
Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
|
XSetWMProtocols(display, win, &wm_delete_window, 1);
|
|
|
|
XMapWindow(display, win);
|
|
XFlush(display);
|
|
|
|
CI2_Window* window = malloc(sizeof(CI2_Window));
|
|
if (!window) {
|
|
XDestroyWindow(display, win);
|
|
XCloseDisplay(display);
|
|
return NULL;
|
|
}
|
|
|
|
window->display = display;
|
|
window->window = win;
|
|
window->wm_delete_window = wm_delete_window;
|
|
window->should_close = false;
|
|
|
|
return window;
|
|
}
|
|
|
|
bool platform_poll_events(CI2_Window* window) {
|
|
if (!window) return false;
|
|
|
|
while (XPending(window->display)) {
|
|
XEvent event;
|
|
XNextEvent(window->display, &event);
|
|
|
|
if (event.type == ClientMessage &&
|
|
(Atom)event.xclient.data.l[0] == window->wm_delete_window) {
|
|
window->should_close = true;
|
|
}
|
|
}
|
|
|
|
return !window->should_close;
|
|
}
|
|
|
|
void platform_destroy_window(CI2_Window* window) {
|
|
if (!window) return;
|
|
|
|
XDestroyWindow(window->display, window->window);
|
|
XCloseDisplay(window->display);
|
|
free(window);
|
|
}
|
|
|
|
void platform_shutdown(void) {
|
|
// Nothing global needed
|
|
}
|