Crafter.Graphics/interfaces/Crafter.Graphics-Window.cppm

218 lines
9.6 KiB
Text
Raw Normal View History

2025-05-07 19:21:51 +02:00
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
2025-11-22 20:58:42 +01:00
catcrafts.net
2025-05-07 19:21:51 +02:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
2025-11-22 20:58:42 +01:00
License version 3.0 as published by the Free Software Foundation;
2025-05-07 19:21:51 +02:00
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2025-11-17 00:44:45 +01:00
module;
2025-11-22 20:58:42 +01:00
#ifdef CRAFTER_GRAPHICS_WAYLAND
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input-event-codes.h>
2025-11-17 00:44:45 +01:00
#include <xkbcommon/xkbcommon.h>
#include "../lib/xdg-shell-client-protocol.h"
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
2025-11-22 20:58:42 +01:00
#include <string.h>
#include <linux/input.h>
#include <sys/mman.h>
#include <wayland-cursor.h>
#include <xkbcommon/xkbcommon.h>
#include <errno.h>
#include <fcntl.h>
#include <print>
#include <wayland-client.h>
#include <wayland-client-protocol.h>
#endif
2025-11-17 00:44:45 +01:00
2025-04-16 00:43:33 +02:00
export module Crafter.Graphics:Window;
2025-11-16 15:32:11 +01:00
import std;
2025-11-23 04:04:53 +01:00
import :Types;
import Crafter.Event;
2025-04-16 00:43:33 +02:00
export namespace Crafter {
2025-11-25 18:52:32 +01:00
class Transform;
2025-11-23 04:04:53 +01:00
class Window {
2025-04-16 00:43:33 +02:00
public:
2025-11-25 02:21:06 +01:00
std::uint_fast32_t width;
std::uint_fast32_t height;
2025-11-25 18:52:32 +01:00
std::chrono::time_point<std::chrono::high_resolution_clock> lastFrameBegin;
2025-11-25 20:30:54 +01:00
std::vector<Transform*> elements;
2025-11-23 04:04:53 +01:00
Event<void> onClose;
2025-11-24 03:38:20 +01:00
Event<FrameTime> onUpdate;
2025-11-23 04:04:53 +01:00
bool open = true;
bool updating = false;
Window() = default;
2025-11-25 02:21:06 +01:00
Window(std::uint_fast32_t width, std::uint_fast32_t height);
2025-11-23 04:04:53 +01:00
Window(Window&) = delete;
Window(Window&&) = delete;
virtual ~Window() = default;
Window& operator=(const Window&) = delete;
virtual void StartSync() = 0;
virtual void StartUpdate() = 0;
virtual void StopUpdate() = 0;
2025-11-25 18:52:32 +01:00
void ScaleElement(Transform& element, Transform& parent);
void ScaleElement(Transform& element);
void ScaleMouse(Transform& element, Transform& parent);
2025-11-25 19:43:40 +01:00
void ScaleMouse(Transform& element);
2025-11-23 04:04:53 +01:00
};
2025-11-22 20:58:42 +01:00
2025-11-23 04:04:53 +01:00
class WindowKeyboard {
public:
bool heldkeys[255] = {};
Event<void> onKeyDown[255];
Event<void> onKeyHold[255];
Event<void> onKeyUp[255];
Event<char> onAnyKeyDown;
Event<char> onAnyKeyHold;
Event<char> onAnyKeyUp;
};
2025-11-22 20:58:42 +01:00
2025-11-25 19:43:40 +01:00
class MouseElement;
2025-11-23 04:04:53 +01:00
class WindowMouse {
public:
Event<MousePoint> onMouseRightClick;
Event<MousePoint> onMouseLeftClick;
Event<MousePoint> onMouseRightHold;
Event<MousePoint> onMouseLeftHold;
Event<MousePoint> onMouseRightRelease;
Event<MousePoint> onMouseLeftRelease;
Event<MouseMoveEvent> onMouseMove;
Event<MouseMoveEvent> onMouseEnter;
Event<MouseMoveEvent> onMouseLeave;
Event<std::uint_fast32_t> onMouseScroll;
MousePoint currentMousePos;
MousePoint lastMousePos;
MousePoint mouseDelta;
bool mouseLeftHeld = false;
bool mouseRightHeld = false;
2025-11-25 19:43:40 +01:00
std::vector<MouseElement*> mouseElements;
2025-11-23 04:04:53 +01:00
};
2025-11-22 20:58:42 +01:00
2025-11-23 04:04:53 +01:00
class WindowTitle {
public:
virtual void SetTitle(const std::string_view title) = 0;
};
2025-11-22 20:58:42 +01:00
2025-11-24 01:47:49 +01:00
class Transform;
2025-11-25 02:21:06 +01:00
class WindowFramebuffer : public Window {
2025-11-23 04:04:53 +01:00
public:
WindowFramebuffer() = default;
2025-11-25 02:21:06 +01:00
WindowFramebuffer(std::uint_fast32_t width, std::uint_fast32_t height);
2025-11-23 04:04:53 +01:00
virtual void Resize(std::uint_fast32_t width, std::uint_fast32_t height) = 0;
virtual void Write(Pixel_BU8_GU8_RU8_AU8* pixels) = 0;
virtual void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) = 0;
virtual Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const = 0;
virtual const Pixel_BU8_GU8_RU8_AU8* Read() const = 0;
virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0;
virtual void Store() = 0;
virtual void Render() = 0;
};
2025-11-22 20:58:42 +01:00
2025-11-23 04:04:53 +01:00
#ifdef CRAFTER_GRAPHICS_WAYLAND
2025-11-25 19:43:40 +01:00
class RenderingElement;
2025-11-25 02:21:06 +01:00
class WindowWayland final : public WindowKeyboard, public WindowMouse, public WindowFramebuffer, public WindowTitle {
2025-11-23 04:04:53 +01:00
public:
Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr;
WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height);
WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height, const std::string_view title);
~WindowWayland();
bool configured = false;
wl_shm* shm = nullptr;
wl_seat* seat = nullptr;
xdg_toplevel* xdgToplevel = nullptr;
xdg_wm_base* xdgWmBase = nullptr;
zxdg_decoration_manager_v1* manager = nullptr;
wl_surface* surface = nullptr;
wl_buffer* buffer = nullptr;
wl_buffer* backBuffer = nullptr;
xdg_surface* xdgSurface = nullptr;
wl_display* display = nullptr;
wl_callback* cb = nullptr;
xkb_keymap* xkb_keymap;
xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
xkb_state* xkb_state;
void Render() override;
void StartSync() override;
void StartUpdate() override;
void StopUpdate() override;
void SetTitle(const std::string_view title) override;
void Resize(std::uint_fast32_t width, std::uint_fast32_t height) override;
void Write(Pixel_BU8_GU8_RU8_AU8* pixels) override;
void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) override;
Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const override;
const Pixel_BU8_GU8_RU8_AU8* Read() const override;
Pixel_BU8_GU8_RU8_AU8* Get() override;
void Store() override;
inline static wl_compositor* compositor = nullptr;
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*);
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial);
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial);
static void keyboard_keymap(void* data, wl_keyboard* keyboard, uint32_t format, int fd, uint32_t size);
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay);
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name);
static void xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*);
2025-11-22 20:58:42 +01:00
constexpr static wl_pointer_listener pointer_listener = {
.enter = PointerListenerHandleEnter,
.leave = PointerListenerHandleLeave,
.motion = PointerListenerHandleMotion,
.button = pointer_handle_button,
.axis = PointerListenerHandleAxis,
};
constexpr static wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_keymap,
.enter = keyboard_enter,
.leave = keyboard_leave,
.key = keyboard_key,
.modifiers = keyboard_modifiers,
.repeat_info = keyboard_repeat_info,
};
constexpr static wl_seat_listener seat_listener = {
.capabilities = seat_handle_capabilities,
};
2025-11-23 04:04:53 +01:00
constexpr static wl_registry_listener registry_listener = {
2025-11-22 20:58:42 +01:00
.global = handle_global,
.global_remove = handle_global_remove,
};
2025-11-23 04:04:53 +01:00
constexpr static xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_configure,
.close = xdg_toplevel_handle_close,
2025-11-22 20:58:42 +01:00
};
2025-11-23 04:04:53 +01:00
constexpr static wl_callback_listener wl_callback_listener = {
.done = wl_surface_frame_done,
};
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
.ping = xdg_wm_base_handle_ping,
};
constexpr static xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_handle_configure,
2025-11-22 20:58:42 +01:00
};
2025-04-16 00:43:33 +02:00
};
2025-11-23 04:04:53 +01:00
#endif
2025-04-16 00:43:33 +02:00
}