aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-07-13 11:38:26 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-07-13 11:42:21 +0100
commit0687370b2e0d8caab3cc4f6ad18fda068bed5518 (patch)
treee65f3679bb35e31d982ff88f78d29b301bb9bc3a
parentf357024377435d2b2343f283b900979c307d6a36 (diff)
downloadimv-0687370b2e0d8caab3cc4f6ad18fda068bed5518.tar.gz
Create stub X11 window implementation
-rw-r--r--Makefile5
-rw-r--r--config.mk2
-rw-r--r--src/x11_window.c150
3 files changed, 156 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index e690e8b..a1e6289 100644
--- a/Makefile
+++ b/Makefile
@@ -41,6 +41,11 @@ ifeq ($(WINDOWS),wayland)
override LIBS += -lwayland-client -lwayland-egl -lEGL
endif
+ifeq ($(WINDOWS),x11)
+ SOURCES += src/x11_window.c
+ override LIBS += -lX11 -lGL -lGLU
+endif
+
# Add backends to build as configured
ifeq ($(BACKEND_FREEIMAGE),yes)
SOURCES += src/backend_freeimage.c
diff --git a/config.mk b/config.mk
index c5fe837..7b3e554 100644
--- a/config.mk
+++ b/config.mk
@@ -3,7 +3,7 @@
#Choices:
# wayland - Use wayland backend
# x11 - Use X11 backend
-WINDOWS=wayland
+WINDOWS=x11
# Configure available backends:
diff --git a/src/x11_window.c b/src/x11_window.c
new file mode 100644
index 0000000..79046ff
--- /dev/null
+++ b/src/x11_window.c
@@ -0,0 +1,150 @@
+#include "window.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include<GL/gl.h>
+#include<GL/glx.h>
+
+struct imv_window {
+ Display *x_display;
+ Window x_window;
+ GLXContext x_glc;
+};
+
+struct imv_window *imv_window_create(int w, int h, const char *title)
+{
+ struct imv_window *window = calloc(1, sizeof *window);
+ window->x_display = XOpenDisplay(NULL);
+ assert(window->x_display);
+ Window root = DefaultRootWindow(window->x_display);
+ assert(root);
+
+ GLint att[] = {
+ GLX_RGBA,
+ GLX_DEPTH_SIZE,
+ 24,
+ GLX_DOUBLEBUFFER,
+ None
+ };
+
+ XVisualInfo *vi = glXChooseVisual(window->x_display, 0, att);
+ assert(vi);
+
+ Colormap cmap = XCreateColormap(window->x_display, root,
+ vi->visual, AllocNone);
+
+ XSetWindowAttributes wa = {
+ .colormap = cmap,
+ .event_mask = ExposureMask | KeyPressMask
+ };
+
+ window->x_window = XCreateWindow(window->x_display, root, 0, 0, w, h,
+ 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &wa);
+
+ XMapWindow(window->x_display, window->x_window);
+ XStoreName(window->x_display, window->x_window, title);
+
+ window->x_glc = glXCreateContext(window->x_display, vi, NULL, GL_TRUE);
+ assert(window->x_glc);
+ glXMakeCurrent(window->x_display, window->x_window, window->x_glc);
+
+ return window;
+}
+
+void imv_window_free(struct imv_window *window)
+{
+ glXDestroyContext(window->x_display, window->x_glc);
+ XCloseDisplay(window->x_display);
+ free(window);
+}
+
+void imv_window_clear(struct imv_window *window, unsigned char r,
+ unsigned char g, unsigned char b)
+{
+ (void)window;
+ glClearColor(r / 255.0f, g / 255.0f, b / 255.0f, 1.0);
+ glClear(GL_COLOR_BUFFER_BIT);
+}
+
+void imv_window_get_size(struct imv_window *window, int *w, int *h)
+{
+ (void)window;
+ (void)w;
+ (void)h;
+}
+
+void imv_window_get_framebuffer_size(struct imv_window *window, int *w, int *h)
+{
+ (void)window;
+ (void)w;
+ (void)h;
+}
+
+void imv_window_set_title(struct imv_window *window, const char *title)
+{
+ XStoreName(window->x_display, window->x_window, title);
+}
+
+bool imv_window_is_fullscreen(struct imv_window *window)
+{
+ (void)window;
+ return false;
+}
+
+void imv_window_set_fullscreen(struct imv_window *window, bool fullscreen)
+{
+ (void)window;
+ (void)fullscreen;
+}
+
+bool imv_window_get_mouse_button(struct imv_window *window, int button)
+{
+ (void)window;
+ (void)button;
+ return false;
+}
+
+void imv_window_get_mouse_position(struct imv_window *window, double *x, double *y)
+{
+ (void)window;
+ (void)x;
+ (void)y;
+}
+
+void imv_window_present(struct imv_window *window)
+{
+ (void)window;
+}
+
+void imv_window_wait_for_event(struct imv_window *window, double timeout)
+{
+ (void)window;
+ (void)timeout;
+}
+
+void imv_window_push_event(struct imv_window *window, struct imv_event *e)
+{
+ (void)window;
+ (void)e;
+}
+
+void imv_window_pump_events(struct imv_window *window, imv_event_handler handler, void *data)
+{
+ (void)handler;
+ (void)data;
+ XEvent xev;
+
+ while (XPending(window->x_display)) {
+ XNextEvent(window->x_display, &xev);
+
+ if (xev.type == Expose) {
+ XWindowAttributes wa;
+ XGetWindowAttributes(window->x_display, window->x_window, &wa);
+ glViewport(0, 0, wa.width, wa.height);
+ glXSwapBuffers(window->x_display, window->x_window);
+ }
+ }
+
+}
+