From e5ce6c1be068cfc013bab6b386e56c61f3297ea5 Mon Sep 17 00:00:00 2001 From: anon3989 Date: Sun, 7 Sep 2025 18:34:59 +0000 Subject: [PATCH] ZWLR example refactor --- .gitignore | 2 + examples/wayland-widget.c | 220 +++++++++++++++++++++++++------------- include/GLFW/glfw3.h | 10 +- src/wl_window.c | 15 ++- 4 files changed, 158 insertions(+), 89 deletions(-) diff --git a/.gitignore b/.gitignore index 8ed24d98..c0aa0aa1 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ tests/triangle-vulkan tests/window tests/windows +#local test file +run diff --git a/examples/wayland-widget.c b/examples/wayland-widget.c index 00a5659f..957f38ec 100644 --- a/examples/wayland-widget.c +++ b/examples/wayland-widget.c @@ -7,78 +7,39 @@ #include #include - #include #include #include +#define NK_IMPLEMENTATION +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_FONT_BAKING +#define NK_INCLUDE_DEFAULT_FONT +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT +#define NK_INCLUDE_STANDARD_VARARGS +#define NK_BUTTON_TRIGGER_ON_RELEASE +#include + +#define NK_GLFW_GL2_IMPLEMENTATION +#include + + #define WIN_HEIGHT 150 +void nk_process(GLFWwindow*, struct nk_context*, float, float); + + static void GLFW_DebugCallback(int err_code, const char* description) { printf("GLFW error: %s\n", description); } - static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) -{ - static bool onTopLayer = true; - static bool onTopAnchor = true; + { printf("Captured %i\n", key); } - if (action == GLFW_PRESS) - { - printf("Grabbing...\n"); - switch (key) - { - case GLFW_KEY_RIGHT: - case GLFW_KEY_LEFT : - { - onTopLayer = !onTopLayer; - - if (onTopLayer) - { - printf("Send to top\n"); - glfwWaylandZwlrSetLayer(window, GLFW_WAYLAND_ZWLR_LAYER_TOP); - } - else - { - printf("Send to bottom\n"); - glfwWaylandZwlrSetLayer(window, GLFW_WAYLAND_ZWLR_LAYER_BOTTOM); - } - - break; - } - - case GLFW_KEY_UP: - case GLFW_KEY_DOWN: - { - onTopAnchor = !onTopAnchor; - - if (onTopAnchor) - { - printf("Stick to top\n"); - glfwWaylandZwlrSetAnchor(window, GLFW_WAYLAND_ZWLR_ANCHOR_TOP); - - } - else - { - printf("Stick to bottom\n"); - glfwWaylandZwlrSetAnchor(window, GLFW_WAYLAND_ZWLR_ANCHOR_BOTTOM); - } - break; - } - } - } -} - - -void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) -{ - if (action == GLFW_RELEASE) - printf("click\n"); -} int main(int argc, char** argv) @@ -89,40 +50,149 @@ int main(int argc, char** argv) glfwWindowHint(GLFW_WAYLAND_USE_ZWLR, GLFW_WAYLAND_ZWLR_LAYER_TOP); - int monitorX_pos, monitorY_pos; - int monitorWidth, monitorHeight; - GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor(); - glfwGetMonitorWorkarea(primary_monitor, &monitorX_pos, &monitorY_pos, &monitorWidth, &monitorHeight); - - // v-- 0 not allowed by glfw api, for avoiding mess with internal api, you are forced.. - // v ..to request monitor size and manually handle window size - GLFWwindow* window = glfwCreateWindow(300, WIN_HEIGHT, "Don't Care", NULL, NULL); + GLFWwindow* window = glfwCreateWindow(600, 400, "Don't Care", NULL, NULL); if (!window) return 1; - glfwSetWindowSize(window, monitorWidth, WIN_HEIGHT); // just testing //glfwWaylandZwlrSetExclusiveZone(window, WIN_HEIGHT + 50); // try to play with //glfwWaylandZwlrSetMargin(window, 10, 10, 10, 10); - glfwMakeContextCurrent (window); - gladLoadGL(glfwGetProcAddress); - glfwSetMouseButtonCallback(window, mouse_button_callback ); - //glfwSetCursorPosCallback (window, cursor_position_callback); - glfwSetKeyCallback (window, key_callback ); - //glfwSetWindowFocusCallback(window, window_focus_callback) ; + glfwMakeContextCurrent(window); + glfwSetKeyCallback (window, key_callback); + gladLoadGL (glfwGetProcAddress); - glClearColor(0.3f, 0.2f, 0.3f, 1.0f); + struct nk_context* nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS); + struct nk_font_atlas* atlas; + nk_glfw3_font_stash_begin(&atlas); + nk_glfw3_font_stash_end(); + + + int width, height; while (!glfwWindowShouldClose(window)) { - glfwSwapBuffers(window); - glfwPollEvents(); + glfwGetWindowSize(window, &width, &height); + + struct nk_rect area = nk_rect(0.f, 0.f, (float) width, (float) height); + nk_window_set_bounds(nk, "main", area); + + nk_glfw3_new_frame(); + if (nk_begin(nk, "main", area, 0)) + nk_process(window, nk, width, height); + + nk_end(nk); glClear(GL_COLOR_BUFFER_BIT); + nk_glfw3_render(NK_ANTI_ALIASING_ON); + + glfwSwapBuffers(window); + glfwWaitEvents(); } + nk_glfw3_shutdown(); + glfwTerminate(); + return 0; } +void nk_process(GLFWwindow* window, struct nk_context* ctx, float width, float height) +{ + nk_flags events; + const nk_flags flags = NK_EDIT_FIELD | + NK_EDIT_SIG_ENTER | + NK_EDIT_GOTO_END_ON_ACTIVATE; + + nk_layout_row_dynamic(ctx, 30, 4); + nk_spacing(ctx, 3); + + if (nk_button_label(ctx, "Exit")) + { + glfwSetWindowShouldClose(window, GLFW_TRUE); + } + + nk_layout_row_dynamic(ctx, (height / 3), 1); + nk_layout_row_dynamic(ctx, 30, 2); + + { + static char exclusiveBuff[12]; + + nk_label(ctx, "Exclusive zone (anchor required):", NK_TEXT_LEFT); + events = nk_edit_string_zero_terminated(ctx, flags, exclusiveBuff, + sizeof(exclusiveBuff), + nk_filter_decimal); + if (events & NK_EDIT_COMMITED) + { + int zone = atoi(exclusiveBuff); + glfwWaylandZwlrSetExclusiveZone(window, zone); + } + } + { + static char exclusiveBuff[12]; + + nk_label(ctx, "Margin all edges (anchor required):", NK_TEXT_LEFT); + events = nk_edit_string_zero_terminated(ctx, flags, exclusiveBuff, + sizeof(exclusiveBuff), + nk_filter_decimal); + if (events & NK_EDIT_COMMITED) + { + int margin = atoi(exclusiveBuff); + glfwWaylandZwlrSetMargin(window, margin, margin, margin, margin); + } + } + + nk_layout_row_dynamic(ctx, 30, 4); + nk_spacing(ctx, 1); + + { + static int pickedLayer = 0; + static const char* layerItems[] = { + "Layer Top", "Layer Overlay", + "Layer Bottom", "Layer Background" + }; + static const int layerFlags[] = { + GLFW_WAYLAND_ZWLR_LAYER_TOP, GLFW_WAYLAND_ZWLR_LAYER_OVERLAY, + GLFW_WAYLAND_ZWLR_LAYER_BOTTOM, GLFW_WAYLAND_ZWLR_LAYER_BACKGROUND + }; + if (nk_combo_begin_label(ctx, layerItems[pickedLayer], nk_vec2(200, 200))) + { + nk_layout_row_dynamic(ctx, 30, 1); + for (int i = 0; i < 4; i++) + { + if (nk_combo_item_label(ctx, layerItems[i], NK_TEXT_LEFT)) + { + pickedLayer = i; + glfwWaylandZwlrSetLayer(window, layerFlags[i]); + } + } + nk_combo_end(ctx); + } + } + { + static int pickedAnchor = 0; + static const char* anchorItems[] = { + "Anchor Top" , "Anchor Bottom", + "Anchor Left", "Anchor Right" + }; + static const int anchorFlags[] = { + GLFW_WAYLAND_ZWLR_ANCHOR_TOP, GLFW_WAYLAND_ZWLR_ANCHOR_BOTTOM, + GLFW_WAYLAND_ZWLR_ANCHOR_LEFT, GLFW_WAYLAND_ZWLR_ANCHOR_RIGHT + }; + if (nk_combo_begin_label(ctx, anchorItems[pickedAnchor], nk_vec2(200, 200))) + { + nk_layout_row_dynamic(ctx, 30, 1); + for (int i = 0; i < 4; i++) + { + if (nk_combo_item_label(ctx, anchorItems[i], NK_TEXT_LEFT)) + { + pickedAnchor = i; + glfwWaylandZwlrSetAnchor(window, anchorFlags[i]); // Flags can be combined, but.. + } // ..it's pain in ass to implement here + } + nk_combo_end(ctx); + } + } +} + + diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index fd7e7215..9181882f 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1136,10 +1136,10 @@ extern "C" { */ #define GLFW_WAYLAND_APP_ID 0x00026001 -#define GLFW_WAYLAND_ZWLR_LAYER_BACKGROUD 0x00026002 -#define GLFW_WAYLAND_ZWLR_LAYER_BOTTOM 0x00026003 -#define GLFW_WAYLAND_ZWLR_LAYER_TOP 0x00026004 -#define GLFW_WAYLAND_ZWLR_LAYER_OVERLAY 0x00026005 +#define GLFW_WAYLAND_ZWLR_LAYER_BACKGROUND 0x00026002 +#define GLFW_WAYLAND_ZWLR_LAYER_BOTTOM 0x00026003 +#define GLFW_WAYLAND_ZWLR_LAYER_TOP 0x00026004 +#define GLFW_WAYLAND_ZWLR_LAYER_OVERLAY 0x00026005 #define GLFW_WAYLAND_ZWLR_ANCHOR_TOP 0x1 #define GLFW_WAYLAND_ZWLR_ANCHOR_BOTTOM 0x2 @@ -1149,7 +1149,7 @@ extern "C" { * [window hint](@ref GLFW_WAYLAND_USE_ZWLR_hint). * * Available options are: - * - GLFW_WAYLAND_ZWLR_BACKGROUD + * - GLFW_WAYLAND_ZWLR_BACKGROUND * - GLFW_WAYLAND_ZWLR_BOTTOM * - GLFW_WAYLAND_ZWLR_TOP * - GLFW_WAYLAND_ZWLR_OVERLAY diff --git a/src/wl_window.c b/src/wl_window.c index 2aa2d6c4..9924ee73 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -1198,11 +1198,8 @@ static bool createZwlrShellObjects(_GLFWwindow* window) static GLFWbool createShellObjects(_GLFWwindow* window) { - if (_glfw.hints.window.wl.useZWLR) - { - if (createZwlrShellObjects(window)) - return GLFW_TRUE; - } + if (_glfw.hints.window.wl.useZWLR) // I can't imagine why fallback to xdg needed.. + return createZwlrShellObjects(window); // ..if user explicitly request zwlr. if (_glfw.wl.libdecor.context) { @@ -3470,10 +3467,10 @@ GLFWAPI void glfwWaylandZwlrSetLayer(GLFWwindow* handle, int layer) switch (layer) { - case GLFW_WAYLAND_ZWLR_LAYER_BACKGROUD: layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND; break; - case GLFW_WAYLAND_ZWLR_LAYER_BOTTOM: layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; break; - case GLFW_WAYLAND_ZWLR_LAYER_TOP: layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; break; - case GLFW_WAYLAND_ZWLR_LAYER_OVERLAY: layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; break; + case GLFW_WAYLAND_ZWLR_LAYER_BACKGROUND: layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND; break; + case GLFW_WAYLAND_ZWLR_LAYER_BOTTOM: layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; break; + case GLFW_WAYLAND_ZWLR_LAYER_TOP: layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; break; + case GLFW_WAYLAND_ZWLR_LAYER_OVERLAY: layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; break; default: { _glfwInputError(GLFW_INVALID_ENUM,