vulkan not working
This commit is contained in:
parent
a1be917178
commit
c45afab0dd
21 changed files with 1319 additions and 438 deletions
|
|
@ -1,360 +1,12 @@
|
|||
module;
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <wayland-cursor.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
#include <iostream>
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <vulkan/vulkan_wayland.h>
|
||||
#include <wayland-client.h>
|
||||
#include <cstring>
|
||||
#include "xdg-shell-client-protocol.h"
|
||||
#include "wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <print>
|
||||
#include "cat.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <wayland-client.h>
|
||||
#include <wayland-client-protocol.h>
|
||||
#include <linux/input-event-codes.h>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
module Crafter.Graphics;
|
||||
import Crafter.Event;
|
||||
using namespace Crafter;
|
||||
|
||||
static void 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);
|
||||
}
|
||||
|
||||
xdg_wm_base_listener xdgWmBaseListener = {
|
||||
.ping = xdg_wm_base_handle_ping,
|
||||
};
|
||||
|
||||
|
||||
void Window::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
|
||||
Window* window = reinterpret_cast<Window*>(data);
|
||||
if (button == BTN_LEFT) {
|
||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||
window->mouseLeftHeld = true;
|
||||
window->onMouseLeftClick.Invoke(window->currentMousePos);
|
||||
} else {
|
||||
window->mouseLeftHeld = false;
|
||||
window->onMouseLeftRelease.Invoke(window->currentMousePos);
|
||||
}
|
||||
} else if(button == BTN_RIGHT){
|
||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||
window->mouseRightHeld = true;
|
||||
window->onMouseRightClick.Invoke(window->currentMousePos);
|
||||
} else {
|
||||
window->mouseRightHeld = true;
|
||||
window->onMouseRightRelease.Invoke(window->currentMousePos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Window::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||
Window* window = reinterpret_cast<Window*>(data);
|
||||
MousePoint pos = {wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y)};
|
||||
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});
|
||||
}
|
||||
|
||||
void Window::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||
Window* window = reinterpret_cast<Window*>(data);
|
||||
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
||||
}
|
||||
|
||||
void Window::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
|
||||
Window* window = reinterpret_cast<Window*>(data);
|
||||
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
||||
}
|
||||
|
||||
void Window::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
|
||||
std::cout << wl_fixed_to_double(value) << std::endl;
|
||||
}
|
||||
|
||||
wl_pointer_listener Window::pointer_listener = {
|
||||
.enter = Window::PointerListenerHandleEnter,
|
||||
.leave = Window::PointerListenerHandleLeave,
|
||||
.motion = Window::PointerListenerHandleMotion,
|
||||
.button = Window::pointer_handle_button,
|
||||
.axis = Window::PointerListenerHandleAxis,
|
||||
};
|
||||
|
||||
void Window::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
|
||||
Window* window = reinterpret_cast<Window*>(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);
|
||||
}
|
||||
}
|
||||
|
||||
wl_seat_listener Window::seat_listener = {
|
||||
.capabilities = seat_handle_capabilities,
|
||||
};
|
||||
|
||||
void Window::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
|
||||
Window* window = reinterpret_cast<Window*>(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) {
|
||||
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 1));
|
||||
} 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));
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name) {
|
||||
|
||||
Window::Window(std::string name, std::uint32_t width, std::uint32_t height) : name(name), width(width), height(height) {
|
||||
|
||||
}
|
||||
|
||||
wl_registry_listener Window::registry_listener = {
|
||||
.global = Window::handle_global,
|
||||
.global_remove = handle_global_remove,
|
||||
};
|
||||
|
||||
static void noop5(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){
|
||||
|
||||
}
|
||||
|
||||
void Window::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
|
||||
Window* window = reinterpret_cast<Window*>(data);
|
||||
window->onClose.Invoke();
|
||||
}
|
||||
|
||||
xdg_toplevel_listener Window::xdg_toplevel_listener = {
|
||||
.configure = noop5,
|
||||
.close = Window::xdg_toplevel_handle_close,
|
||||
};
|
||||
|
||||
void Window::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
|
||||
Window* window = reinterpret_cast<Window*>(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;
|
||||
}
|
||||
|
||||
xdg_surface_listener Window::xdg_surface_listener = {
|
||||
.configure = xdg_surface_handle_configure,
|
||||
};
|
||||
|
||||
static void randname(char *buf) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
long r = ts.tv_nsec;
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
buf[i] = 'A'+(r&15)+(r&16)*2;
|
||||
r >>= 5;
|
||||
}
|
||||
}
|
||||
|
||||
static int anonymous_shm_open(void) {
|
||||
char name[] = "/hello-wayland-XXXXXX";
|
||||
int retries = 100;
|
||||
|
||||
do {
|
||||
randname(name + strlen(name) - 6);
|
||||
|
||||
--retries;
|
||||
// shm_open guarantees that O_CLOEXEC is set
|
||||
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
|
||||
if (fd >= 0) {
|
||||
shm_unlink(name);
|
||||
return fd;
|
||||
}
|
||||
} while (retries > 0 && errno == EEXIST);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int create_shm_file(off_t size) {
|
||||
int fd = anonymous_shm_open();
|
||||
if (fd < 0) {
|
||||
return fd;
|
||||
}
|
||||
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
Window::Window(std::string name, std::uint32_t width, std::uint32_t height) : width(width), height(height) {
|
||||
// Connect to the Wayland compositor
|
||||
display = wl_display_connect(NULL);
|
||||
if (display == NULL) {
|
||||
std::cerr << "failed to create display" << std::endl;
|
||||
}
|
||||
// Obtain the wl_registry and fetch the list of globals
|
||||
wl_registry* registry = wl_display_get_registry(display);
|
||||
wl_registry_add_listener(registry, ®istry_listener, this);
|
||||
if (wl_display_roundtrip(display) == -1) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Check that all globals we require are available
|
||||
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
|
||||
std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Create a wl_surface, a xdg_surface and a xdg_toplevel
|
||||
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);
|
||||
|
||||
while (wl_display_dispatch(display) != -1 && !configured) {
|
||||
// This space intentionally left blank
|
||||
}
|
||||
|
||||
// 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
|
||||
shm_data = reinterpret_cast<Pixel*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
|
||||
if (shm_data == 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);
|
||||
|
||||
for(std::uint32_t x = 0; x < 128; x++) {
|
||||
for(std::uint32_t y = 0; y < 128; y++) {
|
||||
shm_data[x*128+y].r = 0;
|
||||
shm_data[x*128+y].g = 0;
|
||||
shm_data[x*128+y].b = 0;
|
||||
shm_data[x*128+y].a = 255;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel); // toplevel is from xdg_surface_get_toplevel
|
||||
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
|
||||
}
|
||||
|
||||
|
||||
void ScaleBitmapR8G8B8(Pixel* dst, const Pixel* src, std::uint32_t srcWidth, std::uint32_t srcHeight, std::uint32_t dstWidth, std::uint32_t dstHeight) {
|
||||
for (std::uint32_t y = 0; y < dstHeight; y++) {
|
||||
std::uint32_t srcY = y * srcHeight / dstHeight;
|
||||
for (std::uint32_t x = 0; x < dstWidth; x++) {
|
||||
std::uint32_t srcX = x * srcWidth / dstWidth;
|
||||
const Pixel* srcPixel = src + (srcY * srcWidth + srcX);
|
||||
Pixel* dstPixel = dst + (y * dstWidth + x);
|
||||
dstPixel[0] = srcPixel[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Window::Start() {
|
||||
while (wl_display_dispatch(display) != -1) {
|
||||
wl_surface_attach(surface, buffer, 0, 0);
|
||||
for(UiElement* element : elements.components) {
|
||||
std::int32_t realX;
|
||||
std::int32_t realY;
|
||||
std::int32_t elementWidth;
|
||||
std::int32_t elementHeight;
|
||||
if(element->ignoreScaling) {
|
||||
if(element->useRelativeSize) {
|
||||
elementWidth = element->relativeWidth*width;
|
||||
elementHeight = element->relativeHeight*height;
|
||||
} else {
|
||||
elementWidth = element->absoluteWidth;
|
||||
elementHeight = element->absoluteHeight;
|
||||
}
|
||||
} else {
|
||||
if(element->useRelativeSize) {
|
||||
elementWidth = element->relativeWidth*width*scale;
|
||||
elementHeight = element->relativeHeight*height*scale;
|
||||
} else {
|
||||
elementWidth = element->absoluteWidth*scale;
|
||||
elementHeight = element->absoluteHeight*scale;
|
||||
}
|
||||
}
|
||||
realX = (element->anchorX*width)-(element->anchorOffsetX*elementWidth);
|
||||
realY = (element->anchorY*height)-(element->anchorOffsetY*elementHeight);
|
||||
std::vector<Pixel> scaled(elementWidth*elementHeight);
|
||||
ScaleBitmapR8G8B8(scaled.data(), element->buffer.data(), element->bufferWidth, element->bufferHeight, elementWidth, elementHeight);
|
||||
for(std::int32_t x = realX; x-realX < elementWidth; x++) {
|
||||
for(std::int32_t y = realY; y-realY < elementHeight; y++) {
|
||||
if(x > 0 && x < width && y > 0 && y < height) {
|
||||
shm_data[x*width+y] = scaled[(x-realX)*elementWidth+(y-realY)];
|
||||
}
|
||||
}
|
||||
}
|
||||
wl_surface_damage(surface, realX, realY, elementWidth, elementHeight);
|
||||
}
|
||||
wl_surface_commit(surface);
|
||||
}
|
||||
}
|
||||
|
||||
Window::~Window() {
|
||||
xdg_toplevel_destroy(xdgToplevel);
|
||||
xdg_surface_destroy(xdgSurface);
|
||||
wl_surface_destroy(surface);
|
||||
wl_buffer_destroy(buffer);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue