From be9f2f9903ce0e1f96971524321e24e178dcd8e3 Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Wed, 20 Oct 2021 20:45:23 +0200 Subject: [PATCH] add bar class --- meson.build | 1 + src/bar.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/bar.hpp | 10 ++++++++-- src/config.hpp | 4 ++++ src/main.cpp | 44 ++------------------------------------------ 5 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 src/bar.cpp create mode 100644 src/config.hpp diff --git a/meson.build b/meson.build index 0d04bea..5a7892d 100644 --- a/meson.build +++ b/meson.build @@ -11,6 +11,7 @@ subdir('protocols') executable('somebar', 'src/main.cpp', 'src/shm_buffer.cpp', + 'src/bar.cpp', wayland_sources, #moc, dependencies: [qt5_dep, wayland_dep]) diff --git a/src/bar.cpp b/src/bar.cpp new file mode 100644 index 0000000..3b86d7a --- /dev/null +++ b/src/bar.cpp @@ -0,0 +1,44 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#include +#include +#include "bar.hpp" +#include "config.hpp" + +const zwlr_layer_surface_v1_listener Bar::_layerSurfaceListener = { + [](void *owner, zwlr_layer_surface_v1*, uint32_t serial, uint32_t width, uint32_t height) + { + static_cast(owner)->layerSurfaceConfigure(serial, width, height); + } +}; + +Bar::Bar(const wl_output *output) +{ + _surface = wl_compositor_create_surface(compositor); + _layerSurface = zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell, _surface, nullptr, ZWLR_LAYER_SHELL_V1_LAYER_TOP, "net.tapesoftware.Somebar"); + zwlr_layer_surface_v1_add_listener(_layerSurface, &_layerSurfaceListener, this); + zwlr_layer_surface_v1_set_anchor(_layerSurface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); + zwlr_layer_surface_v1_set_size(_layerSurface, 0, barSize); + zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface, barSize); + wl_surface_commit(_surface); +} + +Bar::~Bar() +{ + wl_surface_destroy(_surface); + zwlr_layer_surface_v1_destroy(_layerSurface); + waylandFlush(); +} + +void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height) +{ + zwlr_layer_surface_v1_ack_configure(_layerSurface, serial); + printf("configure: %d x %d\n", width, height); + + _bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888); + memset(_bufs->data(), 0xff, _bufs->stride*_bufs->height); + wl_surface_attach(_surface, _bufs->buffer(), 0, 0); + wl_surface_commit(_surface); + waylandFlush(); +} diff --git a/src/bar.hpp b/src/bar.hpp index 00d1cb1..86ea7b9 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -2,15 +2,21 @@ // See LICENSE file for copyright and license details. #pragma once +#include #include #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "common.hpp" #include "shm_buffer.hpp" class Bar { + static const zwlr_layer_surface_v1_listener _layerSurfaceListener; + wl_surface *_surface {nullptr}; zwlr_layer_surface_v1 *_layerSurface {nullptr}; - ShmBuffer _bufs; + std::optional _bufs; + + void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); public: - explicit Bar(wl_output *output); + explicit Bar(const wl_output *output); + ~Bar(); }; diff --git a/src/config.hpp b/src/config.hpp new file mode 100644 index 0000000..648a08a --- /dev/null +++ b/src/config.hpp @@ -0,0 +1,4 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +constexpr int barSize = 20; diff --git a/src/main.cpp b/src/main.cpp index 3698a04..46b832c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,9 +12,7 @@ #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "xdg-shell-client-protocol.h" #include "common.hpp" -#include "shm_buffer.hpp" - -constexpr uint32_t barSize = 20; +#include "bar.hpp" static void waylandWriteReady(); static void requireGlobal(const void *p, const char *name); @@ -33,51 +31,13 @@ static const struct xdg_wm_base_listener xdgWmBaseListener = { } }; -// app globals -static wl_surface *surface; -static zwlr_layer_surface_v1 *layerSurface; -static const struct wl_surface_listener surfaceListener = { - // todo -}; -static ShmBuffer *xbuf; -static const struct zwlr_layer_surface_v1_listener layerSurfaceListener = { - [](void*, zwlr_layer_surface_v1 *layerSurface, uint32_t serial, uint32_t width, uint32_t height) { - zwlr_layer_surface_v1_ack_configure(layerSurface, serial); - printf("configured to %d x %d\n", width, height); - xbuf = new ShmBuffer(width, height, WL_SHM_FORMAT_XRGB8888); - auto buffer = xbuf->data(); - - auto w = 2*M_PI/(width / 10); - for (auto x = 0; x < width; x++) { - auto val = 255*(sin(x*w)/2+0.5); - for (auto y = 0; y < height; y++) { - auto p = &buffer[y*xbuf->stride+x*4]; - *p++ = 0; - *p++ = 0; - *p++ = val; - *p++ = val; - } - } - - wl_surface_attach(surface, xbuf->buffer(), 0, 0); - wl_surface_commit(surface); - waylandFlush(); - } -}; - // called after we have received the initial batch of globals static void onReady() { requireGlobal(compositor, "wl_compositor"); requireGlobal(shm, "wl_shm"); requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1"); - surface = wl_compositor_create_surface(compositor); - layerSurface = zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell, surface, nullptr, ZWLR_LAYER_SHELL_V1_LAYER_TOP, "net.tapesoftware.Somebar"); - zwlr_layer_surface_v1_add_listener(layerSurface, &layerSurfaceListener, nullptr); - zwlr_layer_surface_v1_set_anchor(layerSurface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); - zwlr_layer_surface_v1_set_size(layerSurface, 0, barSize); - zwlr_layer_surface_v1_set_exclusive_zone(layerSurface, barSize); - wl_surface_commit(surface); + std::ignore = new Bar(nullptr); waylandFlush(); }