add bar class

This commit is contained in:
Raphael Robatsch 2021-10-20 20:45:23 +02:00
parent d8098fccc0
commit be9f2f9903
5 changed files with 59 additions and 44 deletions

View file

@ -11,6 +11,7 @@ subdir('protocols')
executable('somebar', executable('somebar',
'src/main.cpp', 'src/main.cpp',
'src/shm_buffer.cpp', 'src/shm_buffer.cpp',
'src/bar.cpp',
wayland_sources, wayland_sources,
#moc, #moc,
dependencies: [qt5_dep, wayland_dep]) dependencies: [qt5_dep, wayland_dep])

44
src/bar.cpp Normal file
View file

@ -0,0 +1,44 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#include <cstdio>
#include <cstring>
#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<Bar*>(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();
}

View file

@ -2,15 +2,21 @@
// See LICENSE file for copyright and license details. // See LICENSE file for copyright and license details.
#pragma once #pragma once
#include <optional>
#include <wayland-client.h> #include <wayland-client.h>
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "common.hpp" #include "common.hpp"
#include "shm_buffer.hpp" #include "shm_buffer.hpp"
class Bar { class Bar {
static const zwlr_layer_surface_v1_listener _layerSurfaceListener;
wl_surface *_surface {nullptr}; wl_surface *_surface {nullptr};
zwlr_layer_surface_v1 *_layerSurface {nullptr}; zwlr_layer_surface_v1 *_layerSurface {nullptr};
ShmBuffer _bufs; std::optional<ShmBuffer> _bufs;
void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height);
public: public:
explicit Bar(wl_output *output); explicit Bar(const wl_output *output);
~Bar();
}; };

4
src/config.hpp Normal file
View file

@ -0,0 +1,4 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
constexpr int barSize = 20;

View file

@ -12,9 +12,7 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h" #include "xdg-shell-client-protocol.h"
#include "common.hpp" #include "common.hpp"
#include "shm_buffer.hpp" #include "bar.hpp"
constexpr uint32_t barSize = 20;
static void waylandWriteReady(); static void waylandWriteReady();
static void requireGlobal(const void *p, const char *name); 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 // called after we have received the initial batch of globals
static void onReady() static void onReady()
{ {
requireGlobal(compositor, "wl_compositor"); requireGlobal(compositor, "wl_compositor");
requireGlobal(shm, "wl_shm"); requireGlobal(shm, "wl_shm");
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1"); requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
surface = wl_compositor_create_surface(compositor); std::ignore = new Bar(nullptr);
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);
waylandFlush(); waylandFlush();
} }