Crafter.Graphics/implementations/Crafter.Graphics-Window_wayland.cpp

829 lines
33 KiB
C++
Raw Normal View History

2025-11-22 20:58:42 +01:00
/*
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;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input-event-codes.h>
#include "../lib/xdg-shell-client-protocol.h"
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
2025-12-29 18:56:06 +01:00
#include "../lib/fractional-scale-v1.h"
#include "../lib/viewporter.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>
2025-11-23 04:04:53 +01:00
module Crafter.Graphics:Window_wayland_impl;
2025-11-22 20:58:42 +01:00
import :Window;
2025-11-25 20:30:54 +01:00
import :RenderingElement;
import :MouseElement;
2025-11-22 20:58:42 +01:00
import std;
import :Types;
import :Shm;
import Crafter.Event;
using namespace Crafter;
2025-12-30 23:28:38 +01:00
WindowFramebuffer::WindowFramebuffer(std::uint32_t width, std::uint32_t height) : Window(width, height) {
2025-11-23 04:04:53 +01:00
}
2025-12-30 23:28:38 +01:00
WindowWayland::WindowWayland(std::uint32_t width, std::uint32_t height) : WindowFramebuffer(width, height) {
2025-11-23 04:04:53 +01:00
display = wl_display_connect(NULL);
if (display == NULL) {
std::cerr << "failed to create display" << std::endl;
}
wl_registry* registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, this);
if (wl_display_roundtrip(display) == -1) {
exit(EXIT_FAILURE);
}
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl;
exit(EXIT_FAILURE);
}
surface = wl_compositor_create_surface(compositor);
xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface);
xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
wl_surface_commit(surface);
2025-12-29 18:56:06 +01:00
wp_scale = wp_fractional_scale_manager_v1_get_fractional_scale(fractionalScaleManager, surface);
wp_fractional_scale_v1_add_listener(wp_scale, &wp_fractional_scale_v1_listener, this);
2025-11-23 04:04:53 +01:00
while (wl_display_dispatch(display) != -1 && !configured) {}
wl_surface_commit(surface);
zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel);
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
2025-12-29 18:56:06 +01:00
wpViewport = wp_viewporter_get_viewport(wpViewporter, surface);
wp_viewport_set_destination(wpViewport, std::ceil(width/scale), std::ceil(height/scale));
wl_surface_commit(surface);
2025-11-23 04:04:53 +01:00
// Create a wl_buffer, attach it to the surface and commit the surface
int stride = width * 4;
int size = stride * height;
// Allocate a shared memory file with the right size
int fd = create_shm_file(size);
if (fd < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
}
// Map the shared memory file
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (framebuffer == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
}
// Create a wl_buffer from our shared memory file descriptor
wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
// Now that we've mapped the file and created the wl_buffer, we no longer
// need to keep file descriptor opened
close(fd);
if (buffer == NULL) {
exit(EXIT_FAILURE);
}
wl_surface_attach(surface, buffer, 0, 0);
wl_surface_commit(surface);
}
2025-12-30 23:28:38 +01:00
WindowWayland::WindowWayland(std::uint32_t width, std::uint32_t height, const std::string_view title) : WindowWayland(width, height) {
2025-11-23 04:04:53 +01:00
xdg_toplevel_set_title(xdgToplevel, title.data());
}
WindowWayland::~WindowWayland() {
xdg_toplevel_destroy(xdgToplevel);
xdg_surface_destroy(xdgSurface);
wl_surface_destroy(surface);
wl_buffer_destroy(buffer);
}
void WindowWayland::StartSync() {
while (open && wl_display_dispatch(display) != -1) {
}
}
2025-11-25 23:47:00 +01:00
// Optimized pixel blending function using SIMD-like operations
inline void blend_pixel_optimized(Pixel_BU8_GU8_RU8_AU8& dst, const Pixel_BU8_GU8_RU8_AU8& src) {
2025-11-26 04:26:59 +01:00
if(src.a == 0) {
return;
}
2025-11-27 00:08:10 +01:00
2025-11-25 23:47:00 +01:00
float srcA = src.a / 255.0f;
float dstA = dst.a / 255.0f;
float outA = srcA + dstA * (1.0f - srcA);
if (outA > 0.0f) {
dst = {
static_cast<uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA),
static_cast<uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA),
static_cast<uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA),
static_cast<uint8_t>(outA * 255)
};
}
}
2025-11-23 04:04:53 +01:00
2025-11-25 23:29:48 +01:00
void WindowWayland::RenderElement(Transform* transform) {
2025-12-28 00:05:03 +01:00
RenderingElementBase* element = dynamic_cast<RenderingElementBase*>(transform);
2025-11-25 20:30:54 +01:00
if(element) {
2025-11-25 23:29:48 +01:00
#ifdef CRAFTER_TIMING
auto start = std::chrono::high_resolution_clock::now();
#endif
2025-11-26 20:15:25 +01:00
if(element->scaled.width < 1 || element->scaled.height < 1) {
2025-11-25 23:47:00 +01:00
return;
}
2025-11-26 20:15:25 +01:00
for(ClipRect dirty : dirtyRects) {
dirty.left = std::max(element->scaled.x, dirty.left);
dirty.top = std::max(element->scaled.y, dirty.top);
dirty.right = std::min(element->scaled.x+element->scaled.width, dirty.right);
dirty.bottom = std::min(element->scaled.y+element->scaled.height, dirty.bottom);
2025-12-28 00:05:03 +01:00
const Pixel_BU8_GU8_RU8_AU8* src_buffer = element->buffer.data();
2025-12-30 23:28:38 +01:00
std::int32_t src_width = element->scaled.width;
std::int32_t src_height = element->scaled.height;
2025-11-27 00:08:10 +01:00
switch (element->opaque) {
case OpaqueType::FullyOpaque:
// For fully opaque, just copy pixels directly
2025-12-30 23:28:38 +01:00
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
std::int32_t src_y = y - element->scaled.y;
2025-11-27 00:08:10 +01:00
2025-12-30 23:28:38 +01:00
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
std::int32_t src_x = x - element->scaled.x;
2025-11-27 00:08:10 +01:00
framebuffer[y * width + x] = src_buffer[src_y * src_width + src_x];
}
}
break;
2025-11-25 23:47:00 +01:00
2025-11-27 00:08:10 +01:00
case OpaqueType::SemiOpaque:
// For semi-opaque, we can avoid blending when alpha is 0 or 255
2025-12-30 23:28:38 +01:00
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
std::int32_t src_y = y - element->scaled.y;
2025-11-26 20:15:25 +01:00
2025-12-30 23:28:38 +01:00
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
std::int32_t src_x = x - element->scaled.x;
2025-11-27 00:08:10 +01:00
Pixel_BU8_GU8_RU8_AU8 src_pixel = src_buffer[src_y * src_width + src_x];
if (src_pixel.a == 0) {
continue;
}
framebuffer[y * width + x] = src_pixel;
}
2025-11-26 00:00:50 +01:00
}
2025-11-27 00:08:10 +01:00
break;
2025-11-25 23:47:00 +01:00
2025-11-27 00:08:10 +01:00
case OpaqueType::Transparent:
// For transparent, always perform blending
2025-12-30 23:28:38 +01:00
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
std::int32_t src_y = y - element->scaled.y;
2025-11-27 00:08:10 +01:00
2025-12-30 23:28:38 +01:00
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
std::int32_t src_x = x - element->scaled.x;
2025-11-27 00:08:10 +01:00
blend_pixel_optimized(framebuffer[y * width + x], src_buffer[src_y * src_width + src_x]);
}
2025-11-26 00:00:50 +01:00
}
2025-11-27 00:08:10 +01:00
break;
2025-11-25 20:30:54 +01:00
}
}
2025-11-25 23:47:00 +01:00
2025-11-25 23:29:48 +01:00
#ifdef CRAFTER_TIMING
auto end = std::chrono::high_resolution_clock::now();
renderTimings.push_back({element, element->scaled.width, element->scaled.height, end-start});
#endif
2025-11-25 20:30:54 +01:00
}
2025-12-28 00:05:03 +01:00
std::sort(transform->children.begin(), transform->children.end(), [](Transform* a, Transform* b){ return a->anchor.z < b->anchor.z; });
2025-11-25 20:30:54 +01:00
for(Transform* child : transform->children) {
2025-11-25 23:29:48 +01:00
this->RenderElement(child);
2025-11-23 04:04:53 +01:00
}
2025-11-22 20:58:42 +01:00
}
2025-11-23 04:04:53 +01:00
void WindowWayland::Render() {
2025-11-26 02:56:38 +01:00
elements.erase(std::remove(elements.begin(), elements.end(), static_cast<Transform*>(nullptr)), elements.end());
2025-12-28 00:05:03 +01:00
std::sort(elements.begin(), elements.end(), [](Transform* a, Transform* b){ return a->anchor.z < b->anchor.z; });
//std::vector<ClipRect> newClip;
2025-12-30 23:28:38 +01:00
// for (std::uint32_t i = 0; i < dirtyRects.size(); i++) {
2025-12-28 00:05:03 +01:00
// ClipRect rect = dirtyRects[i];
2025-12-30 23:28:38 +01:00
// for (std::uint32_t i2 = i + 1; i2 < dirtyRects.size(); i2++) {
2025-12-28 00:05:03 +01:00
// ClipRect existing = dirtyRects[i2];
// if(rect.bottom >= existing.top && rect.top <= existing.top) {
// newClip.push_back({
// .left = rect.left,
// .right = rect.right,
// .top = rect.top,
// .bottom = existing.top,
// });
// //-| shape
// if(rect.right > existing.right) {
// newClip.push_back({
// .left = existing.right,
// .right = rect.right,
// .top = existing.top,
// .bottom = existing.bottom,
// });
// }
// //|- shape
// if(rect.left < existing.left) {
// newClip.push_back({
// .left = rect.left,
// .right = existing.left,
// .top = existing.top,
// .bottom = existing.bottom,
// });
// }
// //-| or |- shape where rect extends further down
// if(rect.bottom > existing.bottom) {
// newClip.push_back({
// .left = rect.left,
// .right = rect.right,
// .top = existing.bottom,
// .bottom = rect.bottom,
// });
// }
// goto inner;
// }
// if (rect.left <= existing.right && rect.right >= existing.left) {
// newClip.push_back({
// .left = rect.left,
// .right = existing.left,
// .top = rect.top,
// .bottom = rect.bottom,
// });
// if (rect.right > existing.right) {
// newClip.push_back({
// .left = existing.right,
// .right = rect.right,
// .top = rect.top,
// .bottom = rect.bottom,
// });
// }
// goto inner;
// }
// }
// newClip.push_back(rect);
// inner:;
// }
2025-11-26 20:41:54 +01:00
2025-12-28 00:05:03 +01:00
//dirtyRects = std::move(newClip);
2025-11-26 23:36:08 +01:00
2025-12-28 00:05:03 +01:00
// std::memset(framebuffer, 0, width*height*4);
2025-11-26 23:36:08 +01:00
// std::cout << dirtyRects.size() << std::endl;
2025-12-28 00:05:03 +01:00
// // Color palette
// static const std::vector<Pixel_BU8_GU8_RU8_AU8> colors = {
// {255, 0, 0, 255}, // red
// { 0, 255, 0, 255}, // green
// { 0, 0, 255, 255}, // blue
// {255, 255, 0, 255}, // yellow
// {255, 0, 255, 255}, // magenta
// { 0, 255, 255, 255}, // cyan
// };
// std::size_t rectIndex = 0;
// for (const ClipRect& rect : dirtyRects) {
// const Pixel_BU8_GU8_RU8_AU8& color = colors[rectIndex % colors.size()];
// std::cout << std::format(
// "ClipRect {}: [{}, {}, {}, {}] Color = RGBA({}, {}, {}, {})",
// rectIndex,
// rect.left, rect.top, rect.right, rect.bottom,
// color.r, color.g, color.b, color.a
// ) << std::endl;
2025-12-30 23:28:38 +01:00
// for (std::int32_t y = rect.top; y < rect.bottom; ++y) {
// for (std::int32_t x = rect.left; x < rect.right; ++x) {
2025-12-28 00:05:03 +01:00
// framebuffer[y * width + x] = color;
// }
// }
// ++rectIndex;
// }
2025-11-26 23:36:08 +01:00
if (!dirtyRects.empty()) {
for (ClipRect rect : dirtyRects) {
2025-12-30 23:28:38 +01:00
for (std::int32_t y = rect.top; y < rect.bottom; y++) {
for (std::int32_t x = rect.left; x < rect.right; x++) {
2025-11-26 23:36:08 +01:00
framebuffer[y * width + x] = {0, 0, 0, 0};
}
}
}
2025-11-26 18:48:58 +01:00
2025-11-26 23:36:08 +01:00
for(Transform* child : elements) {
RenderElement(child);
}
2025-11-26 18:48:58 +01:00
2025-11-26 23:36:08 +01:00
for (ClipRect rect : dirtyRects) {
wl_surface_damage(surface, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
}
dirtyRects.clear();
}
2025-11-23 04:04:53 +01:00
wl_surface_attach(surface, buffer, 0, 0);
2025-12-28 00:05:03 +01:00
wl_surface_commit(surface);
wl_surface_damage(surface, 0, 0, 10000, 100000);
}
void WindowWayland::QueueRender() {
if(cb == nullptr) {
cb = wl_surface_frame(surface);
wl_callback_add_listener(cb, &wl_callback_listener, this);
}
2025-11-23 04:04:53 +01:00
}
void WindowWayland::StartUpdate() {
2025-12-30 00:44:45 +01:00
lastFrameBegin = std::chrono::high_resolution_clock::now();
2025-11-22 20:58:42 +01:00
cb = wl_surface_frame(surface);
2025-11-23 04:04:53 +01:00
wl_callback_add_listener(cb, &wl_callback_listener, this);
updating = true;
}
void WindowWayland::StopUpdate() {
updating = false;
}
void WindowWayland::SetTitle(const std::string_view title) {
xdg_toplevel_set_title(xdgToplevel, title.data());
}
2025-12-30 23:28:38 +01:00
void WindowWayland::Resize(std::uint32_t width, std::uint32_t height) {
2025-11-23 04:04:53 +01:00
}
void WindowWayland::Write(Pixel_BU8_GU8_RU8_AU8* pixels) {
std::memcpy(framebuffer, pixels, width*height*sizeof(Pixel_BU8_GU8_RU8_AU8));
2025-11-22 20:58:42 +01:00
}
2025-12-30 23:28:38 +01:00
void WindowWayland::Write(std::uint32_t x, std::uint32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) {
2025-11-26 18:48:58 +01:00
framebuffer[y * width + x] = pixel;
2025-11-23 04:04:53 +01:00
}
2025-12-30 23:28:38 +01:00
Pixel_BU8_GU8_RU8_AU8 WindowWayland::Read(std::uint32_t x, std::uint32_t y) const{
2025-11-23 04:04:53 +01:00
return framebuffer[y * width + x];
}
const Pixel_BU8_GU8_RU8_AU8* WindowWayland::Read() const{
return framebuffer;
}
Pixel_BU8_GU8_RU8_AU8* WindowWayland::Get() {
return framebuffer;
}
void WindowWayland::Store() {
}
void WindowWayland::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) {
xdg_wm_base_pong(xdg_wm_base, serial);
}
2025-11-25 21:25:04 +01:00
#ifdef CRAFTER_TIMING
2025-11-25 02:10:18 +01:00
std::chrono::time_point<std::chrono::high_resolution_clock> framEnd;
2025-11-25 21:25:04 +01:00
#endif
2025-11-25 02:10:18 +01:00
2025-11-23 04:04:53 +01:00
void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time)
{
2025-11-25 02:10:18 +01:00
auto start = std::chrono::high_resolution_clock::now();
2025-11-23 04:04:53 +01:00
wl_callback_destroy(cb);
2025-12-28 00:05:03 +01:00
cb = nullptr;
2025-11-23 04:04:53 +01:00
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
2025-11-25 23:29:48 +01:00
#ifdef CRAFTER_TIMING
window->vblank = duration_cast<std::chrono::milliseconds>(start - window->frameEnd);
#endif
2025-11-23 04:04:53 +01:00
if(window->updating) {
cb = wl_surface_frame(window->surface);
wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window);
2025-11-25 18:52:32 +01:00
window->onUpdate.Invoke({start, start-window->lastFrameBegin});
2025-11-25 21:25:04 +01:00
#ifdef CRAFTER_TIMING
2025-11-25 23:29:48 +01:00
window->totalUpdate = std::chrono::nanoseconds(0);
window->updateTimings.clear();
2025-11-25 21:25:04 +01:00
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) {
2025-11-25 23:29:48 +01:00
window->updateTimings.push_back(entry);
window->totalUpdate += entry.second;
2025-11-25 21:25:04 +01:00
}
2025-11-25 23:29:48 +01:00
#endif
2025-11-23 04:04:53 +01:00
}
2025-12-28 00:05:03 +01:00
#ifdef CRAFTER_TIMING
auto renderStart = std::chrono::high_resolution_clock::now();
window->renderTimings.clear();
#endif
window->Render();
#ifdef CRAFTER_TIMING
auto renderEnd = std::chrono::high_resolution_clock::now();
window->totalRender = renderEnd - renderStart;
#endif
2025-11-25 21:25:04 +01:00
#ifdef CRAFTER_TIMING
2025-11-25 23:29:48 +01:00
window->frameEnd = std::chrono::high_resolution_clock::now();
2025-11-25 23:36:43 +01:00
window->frameTimes.push_back(window->totalUpdate+window->totalRender);
// Keep only the last 100 frame times
if (window->frameTimes.size() > 100) {
window->frameTimes.erase(window->frameTimes.begin());
}
2025-11-25 21:25:04 +01:00
#endif
2025-11-25 18:52:32 +01:00
window->lastFrameBegin = start;
2025-11-23 04:04:53 +01:00
}
void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
2025-12-30 23:28:38 +01:00
2025-11-23 04:04:53 +01:00
if (button == BTN_LEFT) {
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
window->mouseLeftHeld = true;
2025-11-26 02:56:38 +01:00
window->onMouseLeftClick.Invoke(window->currentMousePos);
2025-11-25 19:43:40 +01:00
for(MouseElement* element : window->mouseElements) {
2025-11-26 02:56:38 +01:00
if(element) {
2025-12-30 23:28:38 +01:00
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
element->onMouseLeftClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
2025-11-26 02:56:38 +01:00
}
2025-11-23 04:04:53 +01:00
}
}
} else {
window->mouseLeftHeld = false;
window->onMouseLeftRelease.Invoke(window->currentMousePos);
2025-11-25 19:43:40 +01:00
for(MouseElement* element : window->mouseElements) {
2025-11-26 02:56:38 +01:00
if(element) {
2025-12-30 23:28:38 +01:00
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
element->onMouseLeftRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
2025-11-26 02:56:38 +01:00
}
2025-11-23 04:04:53 +01:00
}
}
}
} else if(button == BTN_RIGHT){
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
window->mouseRightHeld = true;
window->onMouseRightClick.Invoke(window->currentMousePos);
2025-11-25 19:43:40 +01:00
for(MouseElement* element : window->mouseElements) {
2025-11-26 02:56:38 +01:00
if(element) {
2025-12-30 23:28:38 +01:00
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
element->onMouseRightClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
2025-11-26 02:56:38 +01:00
}
2025-11-23 04:04:53 +01:00
}
}
} else {
window->mouseRightHeld = true;
window->onMouseRightRelease.Invoke(window->currentMousePos);
2025-11-25 19:43:40 +01:00
for(MouseElement* element : window->mouseElements) {
2025-11-26 02:56:38 +01:00
if(element) {
2025-12-30 23:28:38 +01:00
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
element->onMouseRightRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
2025-11-26 02:56:38 +01:00
}
2025-11-23 04:04:53 +01:00
}
}
}
}
2025-11-26 02:56:38 +01:00
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
2025-12-29 22:21:22 +01:00
window->mouseElements.insert(window->mouseElements.end(), window->pendingMouseElements.begin(), window->pendingMouseElements.end());
window->pendingMouseElements.clear();
2025-11-23 04:04:53 +01:00
}
void WindowWayland::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
2025-12-30 23:28:38 +01:00
MousePoint pos = {FractionalToMappedBoundless<std::uint32_t>((wl_fixed_to_double(surface_x) * window->scale) / window->width), FractionalToMappedBoundless<std::uint32_t>((wl_fixed_to_double(surface_y) * window->scale) / window->height)};
2025-11-23 04:04:53 +01:00
window->lastMousePos = window->currentMousePos;
window->currentMousePos = pos;
window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y};
window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
2025-11-25 19:43:40 +01:00
for(MouseElement* element : window->mouseElements) {
2025-11-26 02:56:38 +01:00
if(element) {
2025-12-30 23:28:38 +01:00
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
element->onMouseMove.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
if(!(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height)) {
element->onMouseEnter.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
2025-11-26 02:56:38 +01:00
}
2025-12-30 23:28:38 +01:00
} else if(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
element->onMouseLeave.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
2025-11-23 04:04:53 +01:00
}
}
}
2025-11-26 02:56:38 +01:00
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
2025-11-23 04:04:53 +01:00
}
void WindowWayland::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
}
void WindowWayland::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
2025-12-30 23:28:38 +01:00
window->onMouseLeave.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
2025-11-23 04:04:53 +01:00
}
void WindowWayland::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
}
void WindowWayland::keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
fprintf(stderr, "Unsupported keymap format\n");
return;
}
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("mmap");
return;
}
window->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
window->xkb_keymap = xkb_keymap_new_from_string(window->xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(map, size);
close(fd);
window->xkb_state = xkb_state_new(window->xkb_keymap);
}
void WindowWayland::keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
}
void WindowWayland::keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) {
}
2025-12-28 00:05:03 +01:00
CrafterKeys keysym_to_crafter_key(xkb_keysym_t sym)
{
switch (sym)
{
// Alphabet
case XKB_KEY_a: return CrafterKeys::A;
case XKB_KEY_b: return CrafterKeys::B;
case XKB_KEY_c: return CrafterKeys::C;
case XKB_KEY_d: return CrafterKeys::D;
case XKB_KEY_e: return CrafterKeys::E;
case XKB_KEY_f: return CrafterKeys::F;
case XKB_KEY_g: return CrafterKeys::G;
case XKB_KEY_h: return CrafterKeys::H;
case XKB_KEY_i: return CrafterKeys::I;
case XKB_KEY_j: return CrafterKeys::J;
case XKB_KEY_k: return CrafterKeys::K;
case XKB_KEY_l: return CrafterKeys::L;
case XKB_KEY_m: return CrafterKeys::M;
case XKB_KEY_n: return CrafterKeys::N;
case XKB_KEY_o: return CrafterKeys::O;
case XKB_KEY_p: return CrafterKeys::P;
case XKB_KEY_q: return CrafterKeys::Q;
case XKB_KEY_r: return CrafterKeys::R;
case XKB_KEY_s: return CrafterKeys::S;
case XKB_KEY_t: return CrafterKeys::T;
case XKB_KEY_u: return CrafterKeys::U;
case XKB_KEY_v: return CrafterKeys::V;
case XKB_KEY_w: return CrafterKeys::W;
case XKB_KEY_x: return CrafterKeys::X;
case XKB_KEY_y: return CrafterKeys::Y;
case XKB_KEY_z: return CrafterKeys::Z;
// Numbers
case XKB_KEY_0: return CrafterKeys::_0;
case XKB_KEY_1: return CrafterKeys::_1;
case XKB_KEY_2: return CrafterKeys::_2;
case XKB_KEY_3: return CrafterKeys::_3;
case XKB_KEY_4: return CrafterKeys::_4;
case XKB_KEY_5: return CrafterKeys::_5;
case XKB_KEY_6: return CrafterKeys::_6;
case XKB_KEY_7: return CrafterKeys::_7;
case XKB_KEY_8: return CrafterKeys::_8;
case XKB_KEY_9: return CrafterKeys::_9;
// Function keys
case XKB_KEY_F1: return CrafterKeys::F1;
case XKB_KEY_F2: return CrafterKeys::F2;
case XKB_KEY_F3: return CrafterKeys::F3;
case XKB_KEY_F4: return CrafterKeys::F4;
case XKB_KEY_F5: return CrafterKeys::F5;
case XKB_KEY_F6: return CrafterKeys::F6;
case XKB_KEY_F7: return CrafterKeys::F7;
case XKB_KEY_F8: return CrafterKeys::F8;
case XKB_KEY_F9: return CrafterKeys::F9;
case XKB_KEY_F10: return CrafterKeys::F10;
case XKB_KEY_F11: return CrafterKeys::F11;
case XKB_KEY_F12: return CrafterKeys::F12;
// Control keys
case XKB_KEY_Escape: return CrafterKeys::Escape;
case XKB_KEY_Tab: return CrafterKeys::Tab;
case XKB_KEY_Return: return CrafterKeys::Enter;
case XKB_KEY_space: return CrafterKeys::Space;
case XKB_KEY_BackSpace: return CrafterKeys::Backspace;
case XKB_KEY_Delete: return CrafterKeys::Delete;
case XKB_KEY_Insert: return CrafterKeys::Insert;
case XKB_KEY_Home: return CrafterKeys::Home;
case XKB_KEY_End: return CrafterKeys::End;
case XKB_KEY_Page_Up: return CrafterKeys::PageUp;
case XKB_KEY_Page_Down: return CrafterKeys::PageDown;
case XKB_KEY_Caps_Lock: return CrafterKeys::CapsLock;
case XKB_KEY_Num_Lock: return CrafterKeys::NumLock;
case XKB_KEY_Scroll_Lock:return CrafterKeys::ScrollLock;
// Modifiers
case XKB_KEY_Shift_L: return CrafterKeys::LeftShift;
case XKB_KEY_Shift_R: return CrafterKeys::RightShift;
case XKB_KEY_Control_L: return CrafterKeys::LeftCtrl;
case XKB_KEY_Control_R: return CrafterKeys::RightCtrl;
case XKB_KEY_Alt_L: return CrafterKeys::LeftAlt;
case XKB_KEY_Alt_R: return CrafterKeys::RightAlt;
case XKB_KEY_Super_L: return CrafterKeys::LeftSuper;
case XKB_KEY_Super_R: return CrafterKeys::RightSuper;
// Arrows
case XKB_KEY_Up: return CrafterKeys::Up;
case XKB_KEY_Down: return CrafterKeys::Down;
case XKB_KEY_Left: return CrafterKeys::Left;
case XKB_KEY_Right: return CrafterKeys::Right;
// Keypad
case XKB_KEY_KP_0: return CrafterKeys::keypad_0;
case XKB_KEY_KP_1: return CrafterKeys::keypad_1;
case XKB_KEY_KP_2: return CrafterKeys::keypad_2;
case XKB_KEY_KP_3: return CrafterKeys::keypad_3;
case XKB_KEY_KP_4: return CrafterKeys::keypad_4;
case XKB_KEY_KP_5: return CrafterKeys::keypad_5;
case XKB_KEY_KP_6: return CrafterKeys::keypad_6;
case XKB_KEY_KP_7: return CrafterKeys::keypad_7;
case XKB_KEY_KP_8: return CrafterKeys::keypad_8;
case XKB_KEY_KP_9: return CrafterKeys::keypad_9;
case XKB_KEY_KP_Enter: return CrafterKeys::keypad_enter;
case XKB_KEY_KP_Add: return CrafterKeys::keypad_plus;
case XKB_KEY_KP_Subtract: return CrafterKeys::keypad_minus;
case XKB_KEY_KP_Multiply: return CrafterKeys::keypad_multiply;
case XKB_KEY_KP_Divide: return CrafterKeys::keypad_divide;
case XKB_KEY_KP_Decimal: return CrafterKeys::keypad_decimal;
// Punctuation
case XKB_KEY_grave: return CrafterKeys::grave;
case XKB_KEY_minus: return CrafterKeys::minus;
case XKB_KEY_equal: return CrafterKeys::equal;
case XKB_KEY_bracketleft: return CrafterKeys::bracket_left;
case XKB_KEY_bracketright:return CrafterKeys::bracket_right;
case XKB_KEY_backslash: return CrafterKeys::backslash;
case XKB_KEY_semicolon: return CrafterKeys::semicolon;
case XKB_KEY_apostrophe: return CrafterKeys::quote;
case XKB_KEY_comma: return CrafterKeys::comma;
case XKB_KEY_period: return CrafterKeys::period;
case XKB_KEY_slash: return CrafterKeys::slash;
default:
return CrafterKeys::CrafterKeysMax;
}
}
2025-11-23 04:04:53 +01:00
void WindowWayland::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
if (!window->xkb_state) {
return;
}
xkb_keycode_t keycode = key + 8;
xkb_keysym_t keysym = xkb_state_key_get_one_sym(window->xkb_state, keycode);
2025-12-28 00:05:03 +01:00
CrafterKeys crafterKey = keysym_to_crafter_key(keysym);
if(state == WL_KEYBOARD_KEY_STATE_PRESSED) {
if(window->heldkeys[static_cast<std::uint8_t>(crafterKey)]) {
window->onKeyHold[static_cast<std::uint8_t>(crafterKey)].Invoke();
window->onAnyKeyHold.Invoke(crafterKey);
} else{
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = true;
window->onKeyDown[static_cast<std::uint8_t>(crafterKey)].Invoke();
window->onAnyKeyDown.Invoke(crafterKey);
}
} else{
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = false;
window->onKeyUp[static_cast<std::uint8_t>(crafterKey)].Invoke();
window->onAnyKeyUp.Invoke(crafterKey);
}
2025-11-23 04:04:53 +01:00
}
void WindowWayland::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) {
}
void WindowWayland::keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {
}
void WindowWayland::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
window->seat = seat;
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
wl_pointer* pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(pointer, &pointer_listener, window);
}
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(keyboard, &keyboard_listener, window);
}
}
void WindowWayland::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
if (strcmp(interface, wl_shm_interface.name) == 0) {
window->shm = reinterpret_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1));
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
wl_seat* seat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 1));
wl_seat_add_listener(seat, &seat_listener, window);
} else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) {
2025-12-29 18:56:06 +01:00
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 3));
2025-11-23 04:04:53 +01:00
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
window->xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL);
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
window->manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));
2025-12-29 18:56:06 +01:00
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
window->wpViewporter = reinterpret_cast<wp_viewporter*>(wl_registry_bind(registry, name, &wp_viewporter_interface, 1));
} else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
window->fractionalScaleManager = reinterpret_cast<wp_fractional_scale_manager_v1*>(wl_registry_bind(registry, name, &wp_fractional_scale_manager_v1_interface, 1));
2025-11-23 04:04:53 +01:00
}
}
void WindowWayland::handle_global_remove(void* data, wl_registry* registry, uint32_t name) {
}
void WindowWayland::xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){
}
void WindowWayland::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
window->onClose.Invoke();
window->open = false;
}
void WindowWayland::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
// The compositor configures our surface, acknowledge the configure event
xdg_surface_ack_configure(xdg_surface, serial);
if (window->configured) {
// If this isn't the first configure event we've received, we already
// have a buffer attached, so no need to do anything. Commit the
// surface to apply the configure acknowledgement.
wl_surface_commit(window->surface);
}
window->configured = true;
2025-12-29 18:56:06 +01:00
}
void WindowWayland::xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
window->scale = scale / 120.0f;
2025-11-22 20:58:42 +01:00
}