/* Crafter®.Graphics Copyright (C) 2025 Catcrafts® catcrafts.net This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3.0 as published by the Free Software Foundation; 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 */ module; #ifdef CRAFTER_GRAPHICS_WAYLAND #include #include #include #include #include #include "../lib/xdg-shell-client-protocol.h" #include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h" #include #include #include #include #include #include #include #include #include #include #endif export module Crafter.Graphics:Window; import std; import :Types; import Crafter.Event; export namespace Crafter { class Transform; class RenderingElement; class Window { public: std::int_fast32_t width; std::int_fast32_t height; std::chrono::time_point lastFrameBegin; std::vector elements; Event onClose; Event onUpdate; bool open = true; bool updating = false; std::vector dirtyRects; Window() = default; Window(std::int_fast32_t width, std::int_fast32_t height); Window(Window&) = delete; Window(Window&&) = delete; virtual ~Window() = default; Window& operator=(const Window&) = delete; void AddDirtyRect(ScaleData rect); virtual void StartSync() = 0; virtual void StartUpdate() = 0; virtual void StopUpdate() = 0; void ScaleElement(Transform& element, Transform& parent); void ScaleElement(Transform& element); void ScaleMouse(Transform& element, Transform& parent); void ScaleMouse(Transform& element); #ifdef CRAFTER_TIMING std::chrono::nanoseconds totalUpdate; std::vector*, std::chrono::nanoseconds>> updateTimings; std::chrono::nanoseconds totalRender; std::vector> renderTimings; std::chrono::nanoseconds vblank; std::chrono::nanoseconds totalFrame; std::chrono::time_point frameEnd; std::vector frameTimes; void LogTiming(); #endif }; class WindowKeyboard { public: bool heldkeys[255] = {}; Event onKeyDown[255]; Event onKeyHold[255]; Event onKeyUp[255]; Event onAnyKeyDown; Event onAnyKeyHold; Event onAnyKeyUp; }; class MouseElement; class WindowMouse { public: Event onMouseRightClick; Event onMouseLeftClick; Event onMouseRightHold; Event onMouseLeftHold; Event onMouseRightRelease; Event onMouseLeftRelease; Event onMouseMove; Event onMouseEnter; Event onMouseLeave; Event onMouseScroll; MousePoint currentMousePos; MousePoint lastMousePos; MousePoint mouseDelta; bool mouseLeftHeld = false; bool mouseRightHeld = false; std::vector mouseElements; }; class WindowTitle { public: virtual void SetTitle(const std::string_view title) = 0; }; class Transform; class WindowFramebuffer : public Window { public: WindowFramebuffer() = default; WindowFramebuffer(std::uint_fast32_t width, std::uint_fast32_t height); 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; }; #ifdef CRAFTER_GRAPHICS_WAYLAND class RenderingElement; class WindowWayland final : public WindowKeyboard, public WindowMouse, public WindowFramebuffer, public WindowTitle { 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 RenderElement(Transform* transform); 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*); 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, }; constexpr static wl_registry_listener registry_listener = { .global = handle_global, .global_remove = handle_global_remove, }; constexpr static xdg_toplevel_listener xdg_toplevel_listener = { .configure = xdg_toplevel_configure, .close = xdg_toplevel_handle_close, }; 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, }; }; #endif }