Compare commits

..

7 Commits

Author SHA1 Message Date
12dac2fc04 feat: event system 2026-02-25 12:19:59 +01:00
0dd2404881 feat: event sketch 2026-02-25 10:31:43 +01:00
73fda759c5 feat: input handler work 2026-02-25 10:08:29 +01:00
dde47e7fac feat: build input_wayland.o 2026-02-15 12:50:32 +01:00
f4dd85ddc2 test: check if WaylandInputHandler works 2026-02-15 12:50:16 +01:00
f4c646a7e0 feat: handle wl_seat global 2026-02-15 12:49:46 +01:00
4ddd2423b9 feat: input handler class init 2026-02-15 12:49:29 +01:00
16 changed files with 531 additions and 6 deletions

View File

@@ -1,8 +1,8 @@
CFLAGS = -Wall -Wextra -I./include/ -g
LIBS = $(shell pkg-config --libs wayland-client wayland-egl egl glesv2)
LIBS = $(shell pkg-config --libs wayland-client wayland-egl egl glesv2 wayland-cursor)
build/main: src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/xdg-shell-protocol.o
g++ -o build/main $(CFLAGS) src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/xdg-shell-protocol.o $(LIBS)
build/main: src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/input.o build/input_wayland.o build/event.o build/event_input.o build/xdg-shell-protocol.o
g++ -o build/main $(CFLAGS) src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/input.o build/input_wayland.o build/event.o build/event_input.o build/xdg-shell-protocol.o $(LIBS)
build/shader.o: src/shader.cpp include/shader.h
g++ -o build/shader.o $(CFLAGS) -c src/shader.cpp
@@ -21,6 +21,19 @@ build/window.o: src/window.cpp include/window.h
build/window_wayland.o: include/window/wayland.h src/window/wayland.cpp
g++ -o build/window_wayland.o $(CFLAGS) -c src/window/wayland.cpp
build/input.o: src/input.cpp include/input.h
g++ -o build/input.o $(CFLAGS) -c src/input.cpp
# TODO: dynamic input impl selection depending on platform (wayland/x11)
build/input_wayland.o: include/input/wayland.h src/input/wayland.cpp
g++ -o build/input_wayland.o $(CFLAGS) -c src/input/wayland.cpp
build/event.o: src/event.cpp include/event.h
g++ -o build/event.o $(CFLAGS) -c src/event.cpp
build/event_input.o: src/event/input.cpp include/event/input.h
g++ -o build/event_input.o $(CFLAGS) -c src/event/input.cpp
build/xdg-shell-protocol.o: src/xdg-shell-protocol.c
gcc -o build/xdg-shell-protocol.o -c $(CFLAGS) $(LIBS) src/xdg-shell-protocol.c

59
include/event.h Normal file
View File

@@ -0,0 +1,59 @@
#pragma once
#ifndef EVENT_H_
#define EVENT_H_
#include <stdbool.h>
#include <stddef.h>
enum class EventType : unsigned int {
// ----- INPUT -----
INPUT = 1 << 0,
// Mouse
MOUSE = 1 << 1,
MOUSE_MOVED = 1 << 2,
};
constexpr EventType operator|(EventType a, EventType b) {
return static_cast<EventType>(
static_cast<unsigned int>(a) |
static_cast<unsigned int>(b)
);
}
constexpr EventType operator&(EventType a, EventType b) {
return static_cast<EventType>(
static_cast<unsigned int>(a) &
static_cast<unsigned int>(b)
);
}
constexpr EventType operator~(EventType a) {
return static_cast<EventType>(
~static_cast<unsigned int>(a)
);
}
struct Event {
public:
Event();
virtual ~Event() = default;
public:
virtual EventType GetType() const = 0;
public:
void Handle();
size_t Id() const;
public:
bool IsHandled() const;
private:
bool m_handled;
size_t m_id;
private:
static size_t s_id_counter;
};
#endif // EVENT_H_

37
include/event/input.h Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#ifndef H_INPUT_EVENT_
#define H_INPUT_EVENT_
#include "event.h"
struct InputEvent : public Event {
public:
InputEvent() = default;
~InputEvent() = default;
public:
EventType GetType() const override;
};
struct MouseEvent : public InputEvent {
public:
MouseEvent(EventType type);
~MouseEvent() = default;
public:
EventType GetType() const override;
private:
EventType m_mouse_type;
};
struct MouseMoveEvent : public MouseEvent {
public:
MouseMoveEvent(size_t x, size_t y);
~MouseMoveEvent() = default;
public:
size_t x;
size_t y;
};
#endif // H_INPUT_EVENT_

33
include/input.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#ifndef H_INPUT_
#define H_INPUT_
#include <cstddef>
class InputHandlerImpl {
public:
InputHandlerImpl() = default;
virtual ~InputHandlerImpl() = default;
public:
virtual size_t GetMouseX() = 0;
virtual size_t GetMouseY() = 0;
};
class InputHandler {
public:
static InputHandler* GetInstance() {
if (!s_instance) s_instance = new InputHandler;
return s_instance;
}
private:
InputHandler();
public:
size_t GetMouseX();
size_t GetMouseY();
private:
static InputHandler* s_instance;
InputHandlerImpl* m_impl;
};
#endif // H_INPUT_

78
include/input/wayland.h Normal file
View File

@@ -0,0 +1,78 @@
#pragma once
#ifndef H_WAYLAND_INPUT_
#define H_WAYLAND_INPUT_
#include "input.h"
#include <vector>
#include <wayland-client-protocol.h>
#include "event.h"
class WaylandInputHandler : public InputHandlerImpl {
private:
static void handle_pointer_enter(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface,
wl_fixed_t surface_x,
wl_fixed_t surface_y);
static void handle_pointer_leave(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface);
static void handle_pointer_motion(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
wl_fixed_t surface_x,
wl_fixed_t surface_y);
static void handle_pointer_button(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
uint32_t state);
static void handle_pointer_axis(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis,
wl_fixed_t value);
static void handle_pointer_frame(void *data,
struct wl_pointer *wl_pointer);
static void handle_pointer_axis_source(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis_source);
static void handle_pointer_axis_stop(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis);
static void handle_pointer_axis_discrete(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
int32_t discrete);
static void handle_pointer_axis_value120(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
int32_t value120);
static void handle_pointer_axis_relative_direction(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
uint32_t direction);
private:
WaylandInputHandler();
~WaylandInputHandler() = default;
friend class InputHandler;
public:
size_t GetMouseX() override;
size_t GetMouseY() override;
private:
struct wl_pointer_listener m_pointer_listener;
wl_fixed_t m_surface_x;
wl_fixed_t m_surface_y;
private:
std::vector<Event*> m_event_queue;
};
#endif // H_WAYLAND_INPUT_

View File

@@ -19,6 +19,12 @@ private:
static void registry_handle_global_remove(void *data,
struct wl_registry *registry,
uint32_t name);
static void seat_handle_capabilities(void *data,
struct wl_seat *seat,
uint32_t capabilities);
static void seat_handle_name(void *data,
struct wl_seat *seat,
const char *name);
private:
WaylandState();
~WaylandState();
@@ -33,8 +39,15 @@ private:
struct xdg_wm_base_listener m_wm_base_listener;
struct wl_registry_listener m_reg_listener;
struct wl_shm *m_shm;
struct wl_seat *m_seat;
struct wl_seat_listener m_seat_listener;
struct wl_pointer* m_pointer;
friend class WaylandWindowImpl;
friend class Renderer;
friend class WaylandInputHandler;
};
#endif // H_WAYLAND_STATE_

View File

@@ -35,6 +35,8 @@ public:
Window(size_t width, size_t height);
virtual ~Window();
static void Init();
Window(const Window &) = delete;
Window(Window &&) = delete;

View File

@@ -8,6 +8,8 @@ public:
WaylandWindowImpl(size_t width, size_t height);
~WaylandWindowImpl() override;
static void Init();
public:
bool Dispatch() override;
void OnFrame(IFrameListener fn) override;

18
src/event.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "event.h"
size_t Event::s_id_counter = 0;
Event::Event() : m_id(s_id_counter++) {}
void Event::Handle() {
m_handled = true;
}
bool Event::IsHandled() const {
return m_handled;
}
size_t Event::Id() const {
return m_id;
}

15
src/event/input.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "event/input.h"
#include "event.h"
EventType InputEvent::GetType() const {
return EventType::INPUT;
}
MouseEvent::MouseEvent(EventType type) : InputEvent(), m_mouse_type(type) {}
EventType MouseEvent::GetType() const {
return InputEvent::GetType() | EventType::MOUSE | m_mouse_type;
}
MouseMoveEvent::MouseMoveEvent() : MouseEvent(EventType::MOUSE_MOVED) {}

25
src/input.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "input.h"
#include <assert.h>
#include "input/wayland.h"
InputHandler* InputHandler::s_instance = nullptr;
// TODO: dynamically load input handling implementation depending on platform
#define WAYLAND 1
InputHandler::InputHandler() {
#ifdef WAYLAND
m_impl = new WaylandInputHandler();
#endif
assert(m_impl && "no implementation found for this platform");
}
size_t InputHandler::GetMouseX() {
return m_impl->GetMouseX();
}
size_t InputHandler::GetMouseY() {
return m_impl->GetMouseY();
}

157
src/input/wayland.cpp Normal file
View File

@@ -0,0 +1,157 @@
#include <cassert>
#include <stdio.h>
#include "input/wayland.h"
#include "event/input.h"
#include "state/wayland.h"
#include <wayland-client-core.h>
#include <wayland-client-protocol.h>
#include <wayland-util.h>
#include <wayland-cursor.h>
#define UNUSED(x) (void)(x)
WaylandInputHandler::WaylandInputHandler() {
auto pointer = WaylandState::GetInstance()->m_pointer;
assert(pointer && "wayland seat_pointer not initialized");
m_pointer_listener.enter = handle_pointer_enter;
m_pointer_listener.leave = handle_pointer_leave;
m_pointer_listener.motion = handle_pointer_motion;
m_pointer_listener.button = handle_pointer_button;
m_pointer_listener.axis = handle_pointer_axis;
m_pointer_listener.frame = handle_pointer_frame;
m_pointer_listener.axis_source = handle_pointer_axis_source;
m_pointer_listener.axis_stop = handle_pointer_axis_stop;
m_pointer_listener.axis_discrete = handle_pointer_axis_discrete;
m_pointer_listener.axis_value120 = handle_pointer_axis_value120;
m_pointer_listener.axis_relative_direction = handle_pointer_axis_relative_direction;
wl_pointer_add_listener(pointer, &m_pointer_listener, this);
}
size_t WaylandInputHandler::GetMouseX() {
return wl_fixed_to_double(m_surface_x);
}
size_t WaylandInputHandler::GetMouseY() {
return wl_fixed_to_double(m_surface_y);
}
void WaylandInputHandler::handle_pointer_enter(
void *data, struct wl_pointer *wl_pointer, uint32_t serial,
struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
UNUSED(wl_pointer);
UNUSED(surface);
auto handler = reinterpret_cast<WaylandInputHandler*>(data);
// auto state = WaylandState::GetInstance();
printf("[DEBUG] INPUT.POINTER: pointer enter event:\n\tserial = %d, surface_x = %f, surface_y = %f\n", serial, wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y));
handler->m_surface_x = surface_x;
handler->m_surface_y = surface_y;
// const char* cursor_theme = "XCursor-Pro-Dark";
// auto theme = wl_cursor_theme_load(cursor_theme, 22, state->m_shm);
// printf("[DEBUG] CURSOR: theme '%s' loaded: %p\n", cursor_theme, theme);
// auto cursor = wl_cursor_theme_get_cursor(theme, "hand2");
// printf("[DEBUG] CURSOR: cursor found (%p) with %u images\n", cursor, cursor->image_count);
// auto buffer = wl_cursor_image_get_buffer(*cursor->images);
// printf("[DEBUG] CURSOR: buffer for cursor '%s' loaded (%p)\n", cursor->name, buffer);
// auto pointer_surface = wl_compositor_create_surface(state->m_compositor);
// wl_pointer_set_cursor(state->m_pointer, serial, pointer_surface, cursor->images[0]->hotspot_x, cursor->images[0]->hotspot_y);
// wl_surface_attach(pointer_surface, buffer, 0, 0);
// wl_surface_commit(pointer_surface);
}
void WaylandInputHandler::handle_pointer_leave(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface) {
UNUSED(data);
UNUSED(wl_pointer);
UNUSED(serial);
UNUSED(surface);
}
void WaylandInputHandler::handle_pointer_motion(void *data,
struct wl_pointer *pointer,
uint32_t time,
wl_fixed_t surface_x,
wl_fixed_t surface_y) {
UNUSED(pointer);
UNUSED(time);
auto handler = reinterpret_cast<WaylandInputHandler*>(data);
handler->m_surface_x = surface_x;
handler->m_surface_y = surface_y;
handler->m_event_queue.push_back(new MouseMoveEvent(surface_x, surface_y));
}
void WaylandInputHandler::handle_pointer_button(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time,
uint32_t button,
uint32_t state) {
UNUSED(data);
UNUSED(wl_pointer);
UNUSED(serial);
UNUSED(button);
UNUSED(state);
UNUSED(time);
}
void WaylandInputHandler::handle_pointer_axis(void *data,
struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis,
wl_fixed_t value) {
UNUSED(data);
UNUSED(wl_pointer);
UNUSED(axis);
UNUSED(value);
UNUSED(time);
}
void WaylandInputHandler::handle_pointer_frame(void *data,
struct wl_pointer *wl_pointer) {
printf("[DEBUG] INPUT.POINTER: received pointer frame event\n");
UNUSED(data);
UNUSED(wl_pointer);
}
void WaylandInputHandler::handle_pointer_axis_source(
void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) {
UNUSED(data);
UNUSED(wl_pointer);
UNUSED(axis_source);
}
void WaylandInputHandler::handle_pointer_axis_stop(
void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
UNUSED(data);
UNUSED(wl_pointer);
UNUSED(time);
UNUSED(axis);
}
void WaylandInputHandler::handle_pointer_axis_discrete(
void *data, struct wl_pointer *wl_pointer, uint32_t axis,
int32_t discrete) {
UNUSED(data);
UNUSED(wl_pointer);
UNUSED(axis);
UNUSED(discrete);
}
void WaylandInputHandler::handle_pointer_axis_value120(
void *data, struct wl_pointer *wl_pointer, uint32_t axis,
int32_t value120) {
UNUSED(data);
UNUSED(value120);
UNUSED(axis);
UNUSED(wl_pointer);
}
void WaylandInputHandler::handle_pointer_axis_relative_direction(
void *data, struct wl_pointer *wl_pointer, uint32_t axis,
uint32_t direction) {
UNUSED(data);
UNUSED(direction);
UNUSED(axis);
UNUSED(wl_pointer);
}

View File

@@ -1,5 +1,6 @@
#include <stdio.h>
#include "input.h"
#include "window.h"
#include "renderer.h"
@@ -9,6 +10,13 @@ static Window window(720, 480);
void HandleFrame() {
printf("[APP]: width = %zu, height = %zu\n", window.GetWidth(), window.GetHeight());
auto x = InputHandler::GetInstance()->GetMouseX();
auto y = InputHandler::GetInstance()->GetMouseY();
printf("[APP]: mouse position: x = %zu, y = %zu\n", x, y);
}
void HandleMouseMove() {
printf("[APP]: mouse moved");
}
int main(int argc, char **argv) {
@@ -16,6 +24,7 @@ int main(int argc, char **argv) {
UNUSED(argv);
Renderer::GetInstance();
Window::Init();
window.OnFrame(HandleFrame);

View File

@@ -48,6 +48,54 @@ void WaylandState::registry_handle_global(void *data, struct wl_registry *regist
xdg_wm_base_add_listener(state->m_wm_base, &state->m_wm_base_listener,
NULL);
}
if (interface_name == "wl_seat") {
state->m_seat = reinterpret_cast<struct wl_seat *>(
wl_registry_bind(registry, name, &wl_seat_interface, version));
printf("[DEBUG] global_binding(%s): wl_seat %p\n", interface,
state->m_seat);
if (state->m_seat == NULL) {
fprintf(stderr, "ERROR: could not bind to wl_seat\n");
}
state->m_seat_listener.capabilities = seat_handle_capabilities;
state->m_seat_listener.name = seat_handle_name;
wl_seat_add_listener(state->m_seat, &state->m_seat_listener, state);
}
if (interface_name == "wl_shm") {
state->m_shm = reinterpret_cast<struct wl_shm *>(
wl_registry_bind(registry, name, &wl_shm_interface, version));
printf("[DEBUG] global_binding(%s): wl_shm %p\n", interface,
state->m_shm);
if (state->m_shm == NULL) {
fprintf(stderr, "ERROR: could not bind to wl_shm\n");
}
}
}
void WaylandState::seat_handle_name(void *data,
struct wl_seat *seat,
const char *name) {
UNUSED(seat);
UNUSED(data);
printf("[DEBUG] SEAT: name of the seat = %s\n", name);
}
void WaylandState::seat_handle_capabilities(void *data,
struct wl_seat *seat,
uint32_t capabilities) {
auto state = reinterpret_cast<WaylandState *>(data);
UNUSED(state);
UNUSED(seat);
if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) > 0) {
printf("[DEBUG] SEAT: keyboard is available\n");
}
if ((capabilities & WL_SEAT_CAPABILITY_POINTER) > 0) {
printf("[DEBUG] SEAT: pointer is available\n");
state->m_pointer = wl_seat_get_pointer(seat);
printf("[DEBUG] SEAT: pointer is bound under %p address\n", state->m_pointer);
}
if ((capabilities & WL_SEAT_CAPABILITY_TOUCH) > 0) {
printf("[DEBUG] SEAT: touch is available\n");
}
}
void WaylandState::registry_handle_global_remove(void *data,

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include "window.h"
#include "state/wayland.h"
#define WINDOW_WAYLAND 1
@@ -15,6 +16,14 @@ Window::Window(size_t width, size_t height)
#endif
}
void Window::Init() {
#ifdef WINDOW_WAYLAND
printf("[DEBUG] Window::Init() called\n");
WaylandState::GetInstance();
WaylandWindowImpl::Init();
#endif
}
Window::~Window() {}
size_t Window::GetWidth() const { return m_impl->GetWidth(); }

View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <assert.h>
#include <wayland-client-core.h>
#include <wayland-client-protocol.h>
@@ -100,6 +101,12 @@ WaylandWindowImpl::~WaylandWindowImpl() { m_running = false; }
void WaylandWindowImpl::OnFrame(IFrameListener fn) { m_on_frame = fn; }
void WaylandWindowImpl::Init() {
auto display = WaylandState::GetInstance()->m_display;
assert(display && "wayland display is not initialized");
wl_display_dispatch_pending(display);
}
bool WaylandWindowImpl::Dispatch() {
if (!m_running) return false;