From 26c56565cc0573ce23eb8d172a6765bce1f657ce Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 18 Apr 2025 12:55:00 +0200 Subject: Separate glx and egl from window system to prepare for wayland --- include/mgl/graphics/backend/egl.h | 8 ++++++ include/mgl/graphics/backend/glx.h | 8 ++++++ include/mgl/graphics/backend/graphics.h | 48 +++++++++++++++++++++++++++++++++ include/mgl/mgl.h | 10 ++++++- include/mgl/window/wayland.h | 8 ++++++ include/mgl/window/window.h | 19 ++++--------- include/mgl/window/x11.h | 8 ++++++ include/mgl/window/x11/window.h | 9 ------- 8 files changed, 94 insertions(+), 24 deletions(-) create mode 100644 include/mgl/graphics/backend/egl.h create mode 100644 include/mgl/graphics/backend/glx.h create mode 100644 include/mgl/graphics/backend/graphics.h create mode 100644 include/mgl/window/wayland.h create mode 100644 include/mgl/window/x11.h delete mode 100644 include/mgl/window/x11/window.h (limited to 'include') diff --git a/include/mgl/graphics/backend/egl.h b/include/mgl/graphics/backend/egl.h new file mode 100644 index 0000000..5dede61 --- /dev/null +++ b/include/mgl/graphics/backend/egl.h @@ -0,0 +1,8 @@ +#ifndef MGL_GRAPHICS_EGL +#define MGL_GRAPHICS_EGL + +#include "graphics.h" + +bool mgl_graphics_egl_init(mgl_graphics *self); + +#endif /* MGL_GRAPHICS_EGL */ diff --git a/include/mgl/graphics/backend/glx.h b/include/mgl/graphics/backend/glx.h new file mode 100644 index 0000000..e2a5758 --- /dev/null +++ b/include/mgl/graphics/backend/glx.h @@ -0,0 +1,8 @@ +#ifndef MGL_GRAPHICS_GLX +#define MGL_GRAPHICS_GLX + +#include "graphics.h" + +bool mgl_graphics_glx_init(mgl_graphics *self); + +#endif /* MGL_GRAPHICS_GLX */ diff --git a/include/mgl/graphics/backend/graphics.h b/include/mgl/graphics/backend/graphics.h new file mode 100644 index 0000000..59f4a0d --- /dev/null +++ b/include/mgl/graphics/backend/graphics.h @@ -0,0 +1,48 @@ +#ifndef MGL_GRAPHICS_H +#define MGL_GRAPHICS_H + +#include + +/* Each window should have its own mgl_graphics */ + +typedef struct mgl_graphics mgl_graphics; +typedef void* mgl_window_handle; + +typedef enum { + MGL_GRAPHICS_API_GLX, /* Only available when using X11 (or XWayland) */ + MGL_GRAPHICS_API_EGL +} mgl_graphics_api; + +struct mgl_graphics { + void (*deinit)(mgl_graphics *self); + bool (*make_context_current)(mgl_graphics *self, mgl_window_handle window); + void (*swap_buffers)(mgl_graphics *self, mgl_window_handle window); + bool (*set_swap_interval)(mgl_graphics *self, mgl_window_handle window, bool enabled); + void* (*get_xvisual_info)(mgl_graphics *self); + + /* Optional */ + void* (*get_display)(mgl_graphics *self); + void* (*get_context)(mgl_graphics *self); + + void *impl; + + mgl_graphics_api graphics_api; + bool alpha; +}; + +typedef struct { + mgl_graphics_api graphics_api; /* The graphics api to use. MGL_GRAPHICS_API_EGL by default */ + bool alpha; /* Support window alpha transparency. False by default */ +} mgl_graphics_create_params; + +bool mgl_graphics_init(mgl_graphics *self, const mgl_graphics_create_params *params); +void mgl_graphics_deinit(mgl_graphics *self); + +bool mgl_graphics_make_context_current(mgl_graphics *self, mgl_window_handle window); +void mgl_graphics_swap_buffers(mgl_graphics *self, mgl_window_handle window); +bool mgl_graphics_set_swap_interval(mgl_graphics *self, mgl_window_handle window, bool enabled); +void* mgl_graphics_get_xvisual_info(mgl_graphics *self); +void* mgl_graphics_get_display(mgl_graphics *self); +void* mgl_graphics_get_context(mgl_graphics *self); + +#endif /* MGL_GRAPHICS_H */ diff --git a/include/mgl/mgl.h b/include/mgl/mgl.h index c5d1f2d..fd3367e 100644 --- a/include/mgl/mgl.h +++ b/include/mgl/mgl.h @@ -9,7 +9,15 @@ typedef void* mgl_connection; typedef struct mgl_context mgl_context; typedef struct mgl_window mgl_window; +typedef enum { + MGL_WINDOW_SYSTEM_NATIVE, /* Use X11 on X11 and Wayland on Wayland */ + MGL_WINDOW_SYSTEM_X11, /* Use X11 on X11 and XWayland on Wayland */ + MGL_WINDOW_SYSTEM_WAYLAND, /* Use Wayland. If user runs on X11 then it fails to connect */ +} mgl_window_system; + struct mgl_context { + bool display_server_is_wayland; + mgl_window_system window_system; /* Window system requested with mgl_init */ mgl_connection connection; mgl_gl gl; mgl_window *current_window; @@ -30,7 +38,7 @@ struct mgl_context { Returns non-0 value on failure. Note: not thread safe. */ -int mgl_init(void); +int mgl_init(mgl_window_system window_system); /* Safe to call multiple times, but will only be deinitialized the last time called. diff --git a/include/mgl/window/wayland.h b/include/mgl/window/wayland.h new file mode 100644 index 0000000..081f87c --- /dev/null +++ b/include/mgl/window/wayland.h @@ -0,0 +1,8 @@ +#ifndef MGL_WINDOW_WAYLAND_H +#define MGL_WINDOW_WAYLAND_H + +#include "window.h" + +bool mgl_window_wayland_init(mgl_window *self, const char *title, const mgl_window_create_params *params, mgl_window_handle existing_window); + +#endif /* MGL_WINDOW_WAYLAND_H */ diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h index 07c19ca..c788e9f 100644 --- a/include/mgl/window/window.h +++ b/include/mgl/window/window.h @@ -2,6 +2,7 @@ #define MGL_WINDOW_H #include "../graphics/color.h" +#include "../graphics/backend/graphics.h" #include "../system/vec.h" #include "../system/clock.h" #include "key.h" @@ -15,8 +16,8 @@ typedef union _XEvent XEvent; typedef struct mgl_event mgl_event; -/* x11 window handle. TODO: Add others when wayland, etc is added */ -typedef unsigned long mgl_window_handle; +/* x11/wayland window handle */ +typedef void* mgl_window_handle; typedef void* mgl_connection; typedef struct mgl_window mgl_window; @@ -40,16 +41,6 @@ typedef struct { int refresh_rate; } mgl_monitor; -typedef enum { - MGL_WINDOW_SYSTEM_NATIVE, /* Use X11 on X11 and Wayland on Wayland */ - MGL_WINDOW_SYSTEM_X11 /* Use X11 on X11 and XWayland on Wayland */ -} mgl_window_system; - -typedef enum { - MGL_RENDER_API_GLX, /* Only available when using X11 (or XWayland) */ - MGL_RENDER_API_EGL -} mgl_render_api; - typedef enum { MGL_WINDOW_TYPE_NORMAL, MGL_WINDOW_TYPE_DIALOG, /* Also sets the window as always on top */ @@ -75,6 +66,7 @@ typedef void (*mgl_active_monitor_callback)(const mgl_monitor *monitor, void *us struct mgl_window { mgl_window_handle (*get_system_handle)(const mgl_window *self); + void (*deinit)(mgl_window *self); void (*close)(mgl_window *self); bool (*poll_event)(mgl_window *self, mgl_event *event); bool (*inject_x11_event)(mgl_window *self, XEvent *xev, mgl_event *event); /* Optional */ @@ -136,8 +128,7 @@ typedef struct { const char *class_name; mgl_window_type window_type; /* default: normal */ mgl_window_handle transient_for_window; /* 0 = none */ - mgl_render_api render_api; /* default: MGL_RENDER_API_GLX on X11 and MGL_RENDER_API_EGL on Wayland */ - mgl_window_system window_system; /* default: MGL_WINDOW_SYSTEM_NATIVE */ + mgl_graphics_api graphics_api; /* Can only be MGL_GRAPHICS_API_GLX in an X11 window. default: MGL_GRAPHICS_API_EGL */ } mgl_window_create_params; /* |params| can be NULL. Note: vsync is enabled by default */ diff --git a/include/mgl/window/x11.h b/include/mgl/window/x11.h new file mode 100644 index 0000000..6c4aa93 --- /dev/null +++ b/include/mgl/window/x11.h @@ -0,0 +1,8 @@ +#ifndef MGL_WINDOW_X11_H +#define MGL_WINDOW_X11_H + +#include "window.h" + +bool mgl_window_x11_init(mgl_window *self, const char *title, const mgl_window_create_params *params, mgl_window_handle existing_window); + +#endif /* MGL_WINDOW_X11_H */ diff --git a/include/mgl/window/x11/window.h b/include/mgl/window/x11/window.h deleted file mode 100644 index 9ffc968..0000000 --- a/include/mgl/window/x11/window.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef MGL_X11_WINDOW_H -#define MGL_X11_WINDOW_H - -#include "../window.h" - -bool mgl_x11_window_init(mgl_window *self, const char *title, const mgl_window_create_params *params, mgl_window_handle existing_window); -void mgl_x11_window_deinit(mgl_window *self); - -#endif /* MGL_X11_WINDOW_H */ -- cgit v1.2.3-70-g09d2