From 6cbb72f0ec2784b29f2d776fc70803e79d7ea010 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 11 Feb 2026 22:13:02 +0100 Subject: [PATCH] feat: c++ classes --- .gitignore | 1 + Makefile | 2 +- compile_commands.json | 49 ++++++ include/state.h | 27 ---- include/window.hpp | 347 ++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 278 +-------------------------------- 6 files changed, 400 insertions(+), 304 deletions(-) create mode 100644 compile_commands.json delete mode 100644 include/state.h create mode 100644 include/window.hpp diff --git a/.gitignore b/.gitignore index 567609b..a4fb4fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +.cache/ diff --git a/Makefile b/Makefile index fdf0cf9..375a432 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -Wall -Wextra -I./include/ -g LIBS = $(shell pkg-config --libs wayland-client wayland-egl egl glesv2) -build/main: src/main.cpp build/shader.o include/state.h build/xdg-shell-protocol.o +build/main: src/main.cpp build/shader.o build/xdg-shell-protocol.o g++ -o build/main $(CFLAGS) src/main.cpp build/shader.o build/xdg-shell-protocol.o $(LIBS) build/shader.o: src/shader.cpp include/shader.h diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..9dd190d --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,49 @@ +[ + { + "file": "src/shader.cpp", + "arguments": [ + "g++", + "-o", + "build/shader.o", + "-Wall", + "-Wextra", + "-I./include/", + "-g", + "-c", + "src/shader.cpp" + ], + "directory": "/home/landam/dev/custom/hyprwindow", + "output": "build/shader.o" + }, + { + "file": "src/xdg-shell-protocol.c", + "arguments": [ + "gcc", + "-o", + "build/xdg-shell-protocol.o", + "-c", + "-Wall", + "-Wextra", + "-I./include/", + "-g", + "src/xdg-shell-protocol.c" + ], + "directory": "/home/landam/dev/custom/hyprwindow", + "output": "build/xdg-shell-protocol.o" + }, + { + "file": "src/main.cpp", + "arguments": [ + "g++", + "-o", + "build/main", + "-Wall", + "-Wextra", + "-I./include/", + "-g", + "src/main.cpp" + ], + "directory": "/home/landam/dev/custom/hyprwindow", + "output": "build/main" + } +] \ No newline at end of file diff --git a/include/state.h b/include/state.h deleted file mode 100644 index 0e5cdcf..0000000 --- a/include/state.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef H_STATE -#define H_STATE - -#include -#include -#include - -typedef struct { - struct wl_display *display; - EGLDisplay egl_display; - struct wl_egl_window *egl_window; - EGLSurface egl_surface; - bool ready; - - struct wl_compositor *compositor; - struct xdg_wm_base *wm_base; - struct wl_surface *w_surface; - struct xdg_surface *x_surface; - struct xdg_toplevel *toplevel; - - struct wl_shm *shm; - - struct wl_callback *frame_callback; - GLuint program; -} State; - -#endif // H_STATE diff --git a/include/window.hpp b/include/window.hpp new file mode 100644 index 0000000..8007807 --- /dev/null +++ b/include/window.hpp @@ -0,0 +1,347 @@ +#ifndef H_WINDOW_ +#define H_WINDOW_ + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "xdg-shell-client-protocol.h" +#include "shader.h" + +#define UNUSED(x) (void)(x) + +class Window; +class Renderer; + +class AppState { +public: + static AppState* GetInstance() { + if (!AppState::s_instance) AppState::s_instance = new AppState(); + return AppState::s_instance; + } +private: +static void wm_base_handle_ping(void *data, struct xdg_wm_base *wm_base, + uint32_t serial) { + UNUSED(data); + printf("INFO: ping method fired! sending pong...\n"); + xdg_wm_base_pong(wm_base, serial); +} + + static void registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, + uint32_t version) { + UNUSED(data); + auto state = reinterpret_cast(data); + + printf("HANDLE_GLOBAL: new interface '%s', name = %d, version = %d\n", + interface, name, version); + std::string interface_name(interface); + if (interface_name == "wl_compositor") { + state->m_compositor = reinterpret_cast( + wl_registry_bind(registry, name, &wl_compositor_interface, version)); + printf("%p compositor binding\n", state->m_compositor); + } + if (interface_name == "xdg_wm_base") { + state->m_wm_base = reinterpret_cast( + wl_registry_bind(registry, name, &xdg_wm_base_interface, version)); + if (state->m_wm_base == NULL) { + fprintf(stderr, "ERROR: could not bind to xdg_wm_base\n"); + } + state->m_wm_base_listener.ping = wm_base_handle_ping; + int res = xdg_wm_base_add_listener(state->m_wm_base, &state->m_wm_base_listener, + NULL); + printf("bound to wm_base: %d\n", res); + } + } + + static void registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + UNUSED(data); + UNUSED(registry); + UNUSED(name); + } +private: + AppState() { + m_display = wl_display_connect(NULL); + if (m_display == NULL) { + fprintf(stderr, "ERROR: could not connect to wl display\n"); + exit(1); + } + + struct wl_registry *registry = wl_display_get_registry(m_display); + if (registry == NULL) { + fprintf(stderr, "ERROR: could not connect to wl registry\n"); + exit(1); + } + + m_reg_listener.global = registry_handle_global; + m_reg_listener.global_remove = registry_handle_global_remove; + + auto handle = wl_registry_add_listener(registry, &m_reg_listener, this); + printf("registry listener bound: %d\n", handle); + + wl_display_roundtrip(m_display); + } +private: + static AppState* s_instance; + + struct wl_display *m_display = nullptr; + struct wl_compositor *m_compositor = nullptr; + struct xdg_wm_base *m_wm_base = nullptr; + + struct xdg_wm_base_listener m_wm_base_listener; + struct wl_registry_listener m_reg_listener; + + friend class Window; + friend class Renderer; +}; + +AppState* AppState::s_instance = nullptr; + +class Renderer { +public: + static Renderer* GetInstance() { + if (!s_instance) s_instance = new Renderer; + return s_instance; + } +public: + const EGLConfig& GetConfig() const { return m_config; } +public: + EGLContext CreateContext() { + EGLint ctx_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; + + return eglCreateContext(m_egl_display, m_config, + EGL_NO_CONTEXT, ctx_attribs); + } + + EGLSurface CreateSurface(struct wl_egl_window* window) { + PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createPlatformWindowSurface = + reinterpret_cast( + eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT")); + + return createPlatformWindowSurface( + m_egl_display, m_config, window, NULL); + } + + void SurfaceSetContext(const EGLSurface& surface, const EGLContext& context) { + if (!eglMakeCurrent(m_egl_display, surface, + surface, context)) { + fprintf(stderr, "ERROR: could not change current EGL context\n"); + fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); + exit(1); + } + } + + void SwapBuffers(const EGLSurface& surface) + { + eglSwapBuffers(m_egl_display, surface); + } +private: + Renderer() { + PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay = + reinterpret_cast( + eglGetProcAddress("eglGetPlatformDisplayEXT")); + + m_egl_display = getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, AppState::GetInstance()->m_display, NULL); + if (m_egl_display == NULL) { + fprintf(stderr, "ERROR: could not get platform display\n"); + fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); + exit(1); + } + + if (!eglInitialize(m_egl_display, NULL, NULL)) { + fprintf(stderr, "ERROR: could not initialize EGL\n"); + fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); + exit(1); + } + + EGLint config_attribs[] = {EGL_SURFACE_TYPE, + EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, + EGL_OPENGL_ES2_BIT, + EGL_RED_SIZE, + 8, + EGL_GREEN_SIZE, + 8, + EGL_BLUE_SIZE, + 8, + EGL_ALPHA_SIZE, + 8, + EGL_NONE}; + + EGLint num_configs; + if (!eglChooseConfig(m_egl_display, config_attribs, &m_config, 1, + &num_configs)) { + fprintf(stderr, "ERROR: could not choose EGL config\n"); + fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); + exit(1); + } + } +private: + static Renderer* s_instance; + + EGLDisplay m_egl_display; + EGLConfig m_config; +}; + +Renderer* Renderer::s_instance = nullptr; + +class Window { +private: + static void handle_frame_callback(void *data, + struct wl_callback *cb, + uint32_t time) { + auto window = reinterpret_cast(data); + + UNUSED(time); + printf("INFO: frame callback\n"); + + wl_callback_destroy(cb); + auto frame_callback = wl_surface_frame(window->m_wsurface); + + window->m_frame_listener.done = handle_frame_callback; + + wl_callback_add_listener(frame_callback, &window->m_frame_listener, window); + + GLint angle_loc = glGetUniformLocation(window->m_program, "angle"); + + static float angle = 0.0f; + angle += 0.02f; + + glClear(GL_COLOR_BUFFER_BIT); + + glUniform1f(angle_loc, angle); + glDrawArrays(GL_TRIANGLES, 0, 3); + + Renderer::GetInstance()->SwapBuffers(window->m_egl_surface); + } + + static void xsurface_handle_configure(void *data, struct xdg_surface *surface, + uint32_t serial) { + auto window = reinterpret_cast(data); + + xdg_surface_ack_configure(surface, serial); + + auto frame_callback = wl_surface_frame(window->m_wsurface); + printf("frame cb: %p\n", frame_callback); + + window->m_frame_listener.done = handle_frame_callback; + + int cb_res = wl_callback_add_listener(frame_callback, + &window->m_frame_listener, window); + + printf("mount callback listener: %d\n", cb_res); + + glClear(GL_COLOR_BUFFER_BIT); + Renderer::GetInstance()->SwapBuffers(window->m_egl_surface); + } + +public: + Window(size_t width, size_t height) { + m_wsurface = wl_compositor_create_surface(AppState::GetInstance()->m_compositor); + if (m_wsurface == NULL) { + fprintf(stderr, "ERROR: could not connect create a surface\n"); + exit(1); + } + + m_egl_window = + wl_egl_window_create(m_wsurface, width, height); + if (m_egl_window == NULL) { + fprintf(stderr, "ERROR: could not create wl_egl_window\n"); + exit(1); + } + + m_egl_surface = Renderer::GetInstance()->CreateSurface(m_egl_window); + if (m_egl_surface == NULL) { + fprintf(stderr, "ERROR: could not create EGL window surface\n"); + fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); + exit(1); + } + + m_egl_context = Renderer::GetInstance()->CreateContext(); + if (m_egl_context == NULL) { + fprintf(stderr, "ERROR: could not create EGL context\n"); + fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); + exit(1); + } + + Renderer::GetInstance()->SurfaceSetContext(m_egl_surface, m_egl_context); + + const char *vs_src = "attribute vec2 pos;\n" + "uniform float angle;\n" + "void main() {\n" + " float c = cos(angle);\n" + " float s = sin(angle);\n" + " mat2 rot = mat2(c, -s, s, c);\n" + " gl_Position = vec4(rot * pos, 0.0, 1.0);\n" + "}\n"; + + const char *fs_src = "precision mediump float;\n" + "void main() {\n" + " gl_FragColor = vec4(1.0, 0.3, 0.2, 1.0);\n" + "}\n"; + + m_program = create_program(vs_src, fs_src); + glUseProgram(m_program); + + GLint pos_loc = glGetAttribLocation(m_program, "pos"); + // GLint angle_loc = glGetUniformLocation(app_state->program, "angle"); + + float vertices[] = {0.0f, 0.6f, -0.6f, -0.6f, 0.6f, -0.6f}; + + glEnableVertexAttribArray(pos_loc); + glVertexAttribPointer(pos_loc, 2, GL_FLOAT, GL_FALSE, 0, vertices); + glViewport(0, 0, width, height); + glClearColor(0.2f, 0.4f, 0.8f, 1.0f); + + m_xsurface = + xdg_wm_base_get_xdg_surface(AppState::GetInstance()->m_wm_base, m_wsurface); + if (m_xsurface == NULL) { + fprintf(stderr, "ERROR: could not get an xdg surface\n"); + exit(1); + } + + m_toplevel = xdg_surface_get_toplevel(m_xsurface); + if (m_toplevel == NULL) { + fprintf(stderr, "ERROR: could not get an xdg toplevel\n"); + exit(1); + } + + m_xsurface_listener.configure = xsurface_handle_configure; + + xdg_surface_add_listener(m_xsurface, &m_xsurface_listener, this); + wl_surface_commit(m_wsurface); + + while (wl_display_dispatch(AppState::GetInstance()->m_display)) { + } + } + ~Window() {} + + Window(const Window&) = delete; + Window(Window&&) = delete; +private: + EGLSurface m_egl_surface; + EGLContext m_egl_context; + + GLuint m_program; + + struct wl_egl_window *m_egl_window; + struct wl_surface *m_wsurface; + struct xdg_surface *m_xsurface; + struct xdg_toplevel *m_toplevel; + + struct wl_callback_listener m_frame_listener; + struct xdg_surface_listener m_xsurface_listener; +}; + +#endif // H_WINDOW_ diff --git a/src/main.cpp b/src/main.cpp index c0c0e72..87eebb7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,286 +1,12 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include /* For O_* constants */ -#include -#include /* For mode constants */ -#include - -#include "shader.h" -#include "state.h" -#include "xdg-shell-client-protocol.h" +#include "window.hpp" #define UNUSED(x) (void)(x) -#define WIDTH 780 -#define HEIGHT 420 - -const char *vs_src = "attribute vec2 pos;\n" - "uniform float angle;\n" - "void main() {\n" - " float c = cos(angle);\n" - " float s = sin(angle);\n" - " mat2 rot = mat2(c, -s, s, c);\n" - " gl_Position = vec4(rot * pos, 0.0, 1.0);\n" - "}\n"; - -const char *fs_src = "precision mediump float;\n" - "void main() {\n" - " gl_FragColor = vec4(1.0, 0.3, 0.2, 1.0);\n" - "}\n"; - -void wm_base_handle_ping(void *data, struct xdg_wm_base *wm_base, - uint32_t serial) { - UNUSED(data); - printf("INFO: ping method fired! sending pong...\n"); - xdg_wm_base_pong(wm_base, serial); -} - -static struct xdg_wm_base_listener wm_base_listener = { - .ping = wm_base_handle_ping, -}; - -void registry_handle_global(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, - uint32_t version) { - State *app_state = reinterpret_cast(data); - printf("HANDLE_GLOBAL: new interface '%s', name = %d, version = %d\n", - interface, name, version); - if (strcmp(interface, "wl_compositor") == 0) { - app_state->compositor = reinterpret_cast( - wl_registry_bind(registry, name, &wl_compositor_interface, version)); - printf("%p compositor binding\n", app_state->compositor); - } - if (strcmp(interface, "xdg_wm_base") == 0) { - app_state->wm_base = reinterpret_cast( - wl_registry_bind(registry, name, &xdg_wm_base_interface, version)); - if (app_state->wm_base == NULL) { - fprintf(stderr, "ERROR: could not bind to xdg_wm_base\n"); - } - int res = xdg_wm_base_add_listener(app_state->wm_base, &wm_base_listener, - app_state); - printf("bound to wm_base: %d\n", res); - } - if (strcmp(interface, "wl_shm") == 0) { - app_state->shm = reinterpret_cast( - wl_registry_bind(registry, name, &wl_shm_interface, version)); - if (app_state->shm == NULL) { - fprintf(stderr, "ERROR: could not bind to wl_shm\n"); - } - } -} - -void registry_handle_global_remove(void *data, struct wl_registry *registry, - uint32_t name) { - UNUSED(data); - UNUSED(registry); - UNUSED(name); -} - -static struct wl_registry_listener listener = { - .global = registry_handle_global, - .global_remove = registry_handle_global_remove, -}; - -static struct wl_callback_listener frame_listener; - -static void frame_done(void *data, struct wl_callback *cb, uint32_t time) { - UNUSED(time); - printf("INFO: frame callback\n"); - - State *state = reinterpret_cast(data); - printf("state ptr = %p\n", state->w_surface); - - wl_callback_destroy(cb); - state->frame_callback = wl_surface_frame(state->w_surface); - wl_callback_add_listener(state->frame_callback, &frame_listener, state); - - GLint angle_loc = glGetUniformLocation(state->program, "angle"); - - static float angle = 0.0f; - angle += 0.02f; - - glClear(GL_COLOR_BUFFER_BIT); - - glUniform1f(angle_loc, angle); - glDrawArrays(GL_TRIANGLES, 0, 3); - - eglSwapBuffers(state->egl_display, state->egl_surface); -} - -static void xsurface_handle_configure(void *data, struct xdg_surface *surface, - uint32_t serial) { - State *app_state = reinterpret_cast(data); - - xdg_surface_ack_configure(surface, serial); - - app_state->frame_callback = wl_surface_frame(app_state->w_surface); - printf("frame cb: %p\n", app_state->frame_callback); - - int cb_res = wl_callback_add_listener(app_state->frame_callback, - &frame_listener, app_state); - - printf("mount callback listener: %d\n", cb_res); - - glClear(GL_COLOR_BUFFER_BIT); - eglSwapBuffers(app_state->egl_display, app_state->egl_surface); -} - -static const struct xdg_surface_listener xsurface_listener = { - .configure = xsurface_handle_configure, -}; - int main(int argc, char **argv) { UNUSED(argc); UNUSED(argv); - State *app_state = new State; - frame_listener.done = frame_done; - - app_state->display = wl_display_connect(NULL); - if (app_state->display == NULL) { - fprintf(stderr, "ERROR: could not connect to wl display\n"); - return 1; - } - - struct wl_registry *registry = wl_display_get_registry(app_state->display); - if (registry == NULL) { - fprintf(stderr, "ERROR: could not connect to wl registry\n"); - return 1; - } - - int handle = wl_registry_add_listener(registry, &listener, app_state); - printf("registry listener bound: %d\n", handle); - - wl_display_roundtrip(app_state->display); - - app_state->w_surface = wl_compositor_create_surface(app_state->compositor); - if (app_state->w_surface == NULL) { - fprintf(stderr, "ERROR: could not connect create a surface\n"); - return 1; - } - - PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay = - reinterpret_cast( - eglGetProcAddress("eglGetPlatformDisplayEXT")); - - PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createPlatformWindowSurface = - reinterpret_cast( - eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT")); - - app_state->egl_display = - getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, app_state->display, NULL); - if (app_state->egl_display == NULL) { - fprintf(stderr, "ERROR: could not get platform display\n"); - fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); - exit(1); - } - - if (!eglInitialize(app_state->egl_display, NULL, NULL)) { - fprintf(stderr, "ERROR: could not initialize EGL\n"); - fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); - exit(1); - } - - EGLint config_attribs[] = {EGL_SURFACE_TYPE, - EGL_WINDOW_BIT, - EGL_RENDERABLE_TYPE, - EGL_OPENGL_ES2_BIT, - EGL_RED_SIZE, - 8, - EGL_GREEN_SIZE, - 8, - EGL_BLUE_SIZE, - 8, - EGL_ALPHA_SIZE, - 8, - EGL_NONE}; - - EGLConfig config; - EGLint num_configs; - if (!eglChooseConfig(app_state->egl_display, config_attribs, &config, 1, - &num_configs)) { - fprintf(stderr, "ERROR: could not choose EGL config\n"); - fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); - exit(1); - } - - EGLint ctx_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; - - EGLContext context = eglCreateContext(app_state->egl_display, config, - EGL_NO_CONTEXT, ctx_attribs); - if (context == NULL) { - fprintf(stderr, "ERROR: could not create EGL context\n"); - fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); - exit(1); - } - - app_state->egl_window = - wl_egl_window_create(app_state->w_surface, WIDTH, HEIGHT); - if (app_state->egl_window == NULL) { - fprintf(stderr, "ERROR: could not create wl_egl_window\n"); - exit(1); - } - - app_state->egl_surface = createPlatformWindowSurface( - app_state->egl_display, config, app_state->egl_window, NULL); - if (app_state->egl_surface == NULL) { - fprintf(stderr, "ERROR: could not create EGL window surface\n"); - fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); - exit(1); - } - - if (!eglMakeCurrent(app_state->egl_display, app_state->egl_surface, - app_state->egl_surface, context)) { - fprintf(stderr, "ERROR: could not change current EGL context\n"); - fprintf(stderr, "EGL error: 0x%x\n", eglGetError()); - exit(1); - } - - app_state->program = create_program(vs_src, fs_src); - glUseProgram(app_state->program); - - GLint pos_loc = glGetAttribLocation(app_state->program, "pos"); - // GLint angle_loc = glGetUniformLocation(app_state->program, "angle"); - - float vertices[] = {0.0f, 0.6f, -0.6f, -0.6f, 0.6f, -0.6f}; - - glEnableVertexAttribArray(pos_loc); - glVertexAttribPointer(pos_loc, 2, GL_FLOAT, GL_FALSE, 0, vertices); - glViewport(0, 0, WIDTH, HEIGHT); - glClearColor(0.2f, 0.4f, 0.8f, 1.0f); - - app_state->x_surface = - xdg_wm_base_get_xdg_surface(app_state->wm_base, app_state->w_surface); - if (app_state->x_surface == NULL) { - fprintf(stderr, "ERROR: could not get an xdg surface\n"); - return 1; - } - - app_state->toplevel = xdg_surface_get_toplevel(app_state->x_surface); - if (app_state->toplevel == NULL) { - fprintf(stderr, "ERROR: could not get an xdg toplevel\n"); - return 1; - } - - xdg_surface_add_listener(app_state->x_surface, &xsurface_listener, app_state); - app_state->ready = true; - wl_surface_commit(app_state->w_surface); - - while (wl_display_dispatch(app_state->display)) { - } + Window window(720, 480); return 0; }