diff options
author | dec05eba <dec05eba@protonmail.com> | 2025-04-17 16:36:06 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2025-04-17 16:36:06 +0200 |
commit | 506c271eafec23bf469caf6c29431191fa885e58 (patch) | |
tree | 4608b4f49d5b4bd8e48702ec758535bb580f4b5d /include | |
parent | 0243be4eebe488b449742474d4301a39f00ac67f (diff) |
Move x11 code to separate place for main window code to prepare for wayland support
Diffstat (limited to 'include')
-rw-r--r-- | include/mgl/window/window.h | 123 | ||||
-rw-r--r-- | include/mgl/window/x11/window.h | 9 |
2 files changed, 88 insertions, 44 deletions
diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h index 748e728..07c19ca 100644 --- a/include/mgl/window/window.h +++ b/include/mgl/window/window.h @@ -9,6 +9,8 @@ #include <stdbool.h> #include <stddef.h> +#define MGL_MAX_MONITORS 12 + /* Vsync is automatically set for created windows, if supported by the system */ typedef union _XEvent XEvent; @@ -16,6 +18,7 @@ typedef struct mgl_event mgl_event; /* x11 window handle. TODO: Add others when wayland, etc is added */ typedef unsigned long mgl_window_handle; +typedef void* mgl_connection; typedef struct mgl_window mgl_window; typedef struct { @@ -37,29 +40,15 @@ typedef struct { int refresh_rate; } mgl_monitor; -struct mgl_window { - mgl_window_handle window; - void *context; - mgl_vec2i pos; - mgl_vec2i size; - /* relative to the top left of the window. only updates when the cursor is inside the window */ - mgl_vec2i cursor_position; - mgl_view view; - mgl_scissor scissor; - bool open; - bool focused; - bool key_repeat_enabled; - bool vsync_enabled; /* true by default */ - double frame_time_limit; - double frame_time_limit_monitor; - mgl_clock frame_timer; - char *clipboard_string; - size_t clipboard_size; - bool low_latency; /* false by default */ +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; - mgl_monitor *monitors; /* TODO: Move these to mgl file */ - int num_monitors; -}; +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, @@ -68,9 +57,69 @@ typedef enum { } mgl_window_type; typedef enum { - MGL_RENDER_API_GLX, - MGL_RENDER_API_EGL -} mgl_render_api; + MGL_CLIPBOARD_TYPE_STRING = 1 << 0, + MGL_CLIPBOARD_TYPE_IMAGE_PNG = 1 << 1, + MGL_CLIPBOARD_TYPE_IMAGE_JPG = 1 << 2, + MGL_CLIPBOARD_TYPE_IMAGE_GIF = 1 << 3, +} mgl_clipboard_type; + +#define MGL_CLIPBOARD_TYPE_ALL 0xFFFFFFFF +#define MGL_CLIPBOARD_TYPE_IMAGE (MGL_CLIPBOARD_TYPE_IMAGE_PNG | MGL_CLIPBOARD_TYPE_IMAGE_JPG | MGL_CLIPBOARD_TYPE_IMAGE_GIF) + +/* + Return true to continue. |mgl_window_get_clipboard| returns false if this returns false. + Note: |size| is the size of the current data, not the total data (if the callback only contains a part of the data). +*/ +typedef bool (*mgl_clipboard_callback)(const unsigned char *data, size_t size, mgl_clipboard_type clipboard_type, void *userdata); +typedef void (*mgl_active_monitor_callback)(const mgl_monitor *monitor, void *userdata); + +struct mgl_window { + mgl_window_handle (*get_system_handle)(const 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 */ + void (*swap_buffers)(mgl_window *self); + void (*set_visible)(mgl_window *self, bool visible); + bool (*is_key_pressed)(const mgl_window *self, mgl_key key); + bool (*is_mouse_button_pressed)(const mgl_window *self, mgl_mouse_button button); + void (*set_title)(mgl_window *self, const char *title); + void (*set_cursor_visible)(mgl_window *self, bool visible); + void (*set_vsync_enabled)(mgl_window *self, bool enabled); + bool (*is_vsync_enabled)(const mgl_window *self); + void (*set_fullscreen)(mgl_window *self, bool fullscreen); + bool (*is_fullscreen)(const mgl_window *self); + void (*set_position)(mgl_window *self, mgl_vec2i position); + void (*set_size)(mgl_window *self, mgl_vec2i size); + void (*set_size_limits)(mgl_window *self, mgl_vec2i minimum, mgl_vec2i maximum); + void (*set_clipboard)(mgl_window *self, const char *str, size_t size); + bool (*get_clipboard)(mgl_window *self, mgl_clipboard_callback callback, void *userdata, uint32_t clipboard_types); + bool (*get_clipboard_string)(mgl_window *self, char **str, size_t *size); + void (*set_key_repeat_enabled)(mgl_window *self, bool enabled); + void (*flush)(mgl_window *self); + void* (*get_egl_display)(mgl_window *self); + void* (*get_egl_context)(mgl_window *self); + void (*for_each_active_monitor_output)(mgl_window *self, mgl_active_monitor_callback callback, void *userdata); + + void *impl; + + bool vsync_enabled; /* true by default */ + bool low_latency; /* false by default */ + bool open; + bool focused; + bool key_repeat_enabled; /* true by default */ + double frame_time_limit; + double frame_time_limit_monitor; + mgl_clock frame_timer; + mgl_vec2i pos; + mgl_vec2i size; + /* relative to the top left of the window. only updates when the cursor is inside the window */ + mgl_vec2i cursor_position; + mgl_view view; + mgl_scissor scissor; + /* This only contains connected and active monitors */ + mgl_monitor monitors[MGL_MAX_MONITORS]; + int num_monitors; +}; /* TODO: Some of these parameters only apply to new window */ typedef struct { @@ -87,25 +136,10 @@ 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 */ + 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_window_create_params; -typedef enum { - MGL_CLIPBOARD_TYPE_STRING = 1 << 0, - MGL_CLIPBOARD_TYPE_IMAGE_PNG = 1 << 1, - MGL_CLIPBOARD_TYPE_IMAGE_JPG = 1 << 2, - MGL_CLIPBOARD_TYPE_IMAGE_GIF = 1 << 3, -} mgl_clipboard_type; - -#define MGL_CLIPBOARD_TYPE_ALL 0xFFFFFFFF -#define MGL_CLIPBOARD_TYPE_IMAGE (MGL_CLIPBOARD_TYPE_IMAGE_PNG | MGL_CLIPBOARD_TYPE_IMAGE_JPG | MGL_CLIPBOARD_TYPE_IMAGE_GIF) - -/* - Return true to continue. |mgl_window_get_clipboard| returns false if this returns false. - Note: |size| is the size of the current data, not the total data (if the callback only contains a part of the data). -*/ -typedef bool (*mgl_clipboard_callback)(const unsigned char *data, size_t size, mgl_clipboard_type clipboard_type, void *userdata); - /* |params| can be NULL. Note: vsync is enabled by default */ int mgl_window_create(mgl_window *self, const char *title, const mgl_window_create_params *params); int mgl_window_init_from_existing_window(mgl_window *self, mgl_window_handle existing_window); @@ -142,6 +176,8 @@ bool mgl_window_has_focus(const mgl_window *self); bool mgl_window_is_key_pressed(const mgl_window *self, mgl_key key); bool mgl_window_is_mouse_button_pressed(const mgl_window *self, mgl_mouse_button button); +/* Returns 0 if none is available */ +mgl_window_handle mgl_window_get_system_handle(const mgl_window *self); void mgl_window_close(mgl_window *self); void mgl_window_set_title(mgl_window *self, const char *title); void mgl_window_set_cursor_visible(mgl_window *self, bool visible); @@ -187,7 +223,6 @@ void mgl_window_flush(mgl_window *self); void* mgl_window_get_egl_display(mgl_window *self); void* mgl_window_get_egl_context(mgl_window *self); -typedef void (*mgl_active_monitor_callback)(const mgl_monitor *monitor, void *userdata); -void mgl_for_each_active_monitor_output(void *display, mgl_active_monitor_callback callback, void *userdata); +void mgl_window_for_each_active_monitor_output(mgl_window *self, mgl_active_monitor_callback callback, void *userdata); #endif /* MGL_WINDOW_H */ diff --git a/include/mgl/window/x11/window.h b/include/mgl/window/x11/window.h new file mode 100644 index 0000000..9ffc968 --- /dev/null +++ b/include/mgl/window/x11/window.h @@ -0,0 +1,9 @@ +#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 */ |