receive bar state

This commit is contained in:
Raphael Robatsch 2021-10-25 20:52:21 +02:00
parent 7298730969
commit 629e8f6e1c
5 changed files with 63 additions and 27 deletions

View file

@ -55,6 +55,12 @@
<description summary="control one monitor">
</description>
<enum name="tag_state">
<entry name="none" value="0" summary="no state"/>
<entry name="active" value="1" summary="tag is active"/>
<entry name="urgent" value="2" summary="tag has at least one urgent client"/>
</enum>
<request name="release" type="destructor">
<description summary="release dwl_monitor">
</description>
@ -64,9 +70,9 @@
<description summary="sent every time a tag state gets updated">
</description>
<arg name="tag" type="int"/>
<arg name="active" type="int"/>
<arg name="state" type="uint" enum="tag_state"/>
<arg name="num_clients" type="int"/>
<arg name="urgent" type="int"/>
<arg name="focused_client" type="int" summary="-1 if there is no focused client"/>
</event>
<event name="frame">

View file

@ -30,7 +30,16 @@ static QFont getFont()
static QFont font = getFont();
static QFontMetrics fontMetrics = QFontMetrics {font};
Bar::Bar(const wl_output *output)
const wl_surface* Bar::surface() const { return _surface.get(); }
Bar::Bar()
{
for (auto tag : tagNames) {
_tags.push_back({ tag, ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_NONE, 0, 0, 0 });
}
}
void Bar::create(wl_output *output)
{
_surface.reset(wl_compositor_create_surface(compositor));
_layerSurface.reset(zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell,
@ -47,21 +56,15 @@ Bar::Bar(const wl_output *output)
zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface.get(), barSize);
wl_surface_commit(_surface.get());
for (auto tag : tagNames) {
_tags.push_back({ tag, false });
}
_windowTitle = "Window title";
_status = "Status";
}
const wl_surface* Bar::surface() const { return _surface.get(); }
void Bar::click(int x, int)
{
for (auto tag=_tags.rbegin(); tag != _tags.rend(); tag++) {
if (x > tag->x) {
tag->active = !tag->active;
invalidate();
// todo toggle
return;
}
}
@ -76,10 +79,17 @@ void Bar::invalidate()
wl_surface_commit(_surface.get());
}
void Bar::setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient)
{
auto& t = _tags[tag];
t.state = state;
t.numClients = numClients;
t.focusedClient = focusedClient;
}
void Bar::setStatus(const QString &status)
{
_status = status;
invalidate();
}
void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height)
@ -103,8 +113,6 @@ void Bar::render()
_x = 0;
painter.setFont(font);
setColorScheme(colorActive);
painter.fillRect(0, 0, img.width(), img.height(), painter.brush());
renderTags();
setColorScheme(colorActive);
renderText(_windowTitle);
@ -128,8 +136,17 @@ void Bar::renderTags()
{
for (auto &tag : _tags) {
tag.x = _x;
setColorScheme(tag.active ? colorActive : colorInactive);
setColorScheme(tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_URGENT ? colorUrgent
: tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE ? colorActive : colorInactive);
renderText(tag.name);
auto indicators = qMin(tag.numClients, _bufs->height/2);
for (auto ind = 0; ind < indicators; ind++) {
if (ind == tag.focusedClient) {
_painter->drawLine(tag.x, ind*2, tag.x+5, ind*2);
} else {
_painter->drawPoint(tag.x, ind*2);
}
}
}
}
@ -143,9 +160,9 @@ void Bar::renderText(const QString &text)
void Bar::renderStatus()
{
_painter->fillRect(_x, 0, _bufs->width-_x, _bufs->height, _painter->brush());
auto size = textWidth(_status) + paddingX*2;
_x = _bufs->width - size;
_painter->fillRect(_x, 0, size, _bufs->height, _painter->brush());
_painter->drawText(paddingX+_x, _textY, _status);
_x = _bufs->width;
}

View file

@ -14,7 +14,9 @@
struct Tag {
QString name;
bool active;
znet_tapesoftware_dwl_wm_monitor_v1_tag_state state;
int numClients;
int focusedClient;
int x;
};
@ -40,10 +42,12 @@ class Bar {
void renderText(const QString &text);
int textWidth(const QString &text);
void setColorScheme(const ColorScheme &scheme);
void invalidate();
public:
explicit Bar(const wl_output *output);
Bar();
const wl_surface* surface() const;
void create(wl_output *output);
void setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient);
void setStatus(const QString &status);
void invalidate();
void click(int x, int y);
};

View file

@ -4,7 +4,7 @@
#pragma once
#include "common.hpp"
constexpr bool topbar = 1;
constexpr bool topbar = true;
constexpr int paddingX = 10;
constexpr int paddingY = 3;
@ -15,3 +15,4 @@ constexpr bool fontBold = true;
constexpr ColorScheme colorInactive = {QColor(255, 255, 255), QColor(0, 0, 0)};
constexpr ColorScheme colorActive = {QColor(255, 255, 255), QColor(0, 0, 255)};
constexpr ColorScheme colorUrgent = {colorActive.bg, colorActive.fg};

View file

@ -27,6 +27,7 @@ struct Monitor {
wl_unique_ptr<wl_output> wlOutput;
wl_unique_ptr<znet_tapesoftware_dwl_wm_monitor_v1> dwlMonitor;
std::optional<Bar> bar;
bool created;
};
static void waylandFlush();
@ -44,6 +45,7 @@ znet_tapesoftware_dwl_wm_v1 *dwlWm;
std::vector<QString> tagNames;
static bool ready;
static std::vector<Monitor> monitors;
static QString lastStatus;
static std::string statusFifoName;
static int statusFifoFd {-1};
static int statusFifoWriter {-1};
@ -68,10 +70,10 @@ struct SeatState {
static SeatState seatState;
static Bar* barFromSurface(const wl_surface *surface)
{
auto fbar = std::find_if(begin(monitors), end(monitors), [surface](const Monitor &mon) {
auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor &mon) {
return mon.bar && mon.bar->surface() == surface;
});
return fbar != end(monitors) && fbar->bar ? &*fbar->bar : nullptr;
return mon != end(monitors) && mon->bar ? &*mon->bar : nullptr;
}
static const struct wl_pointer_listener pointerListener = {
.enter = [](void*, wl_pointer *pointer, uint32_t serial,
@ -132,19 +134,24 @@ static const struct znet_tapesoftware_dwl_wm_v1_listener dwlWmListener = {
};
static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwlWmMonitorListener {
.tag = [](void*, znet_tapesoftware_dwl_wm_monitor_v1*, int tag, int active, int numClients, int urgent) {
printf("tag %s: active=%d, num_clients=%d, urgent=%d\n", qPrintable(tagNames[tag]), active, numClients, urgent);
.tag = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, int32_t tag, uint32_t state, int32_t numClients, int32_t focusedClient) {
auto mon = static_cast<Monitor*>(mv);
mon->bar->setTag(tag, static_cast<znet_tapesoftware_dwl_wm_monitor_v1_tag_state>(state), numClients, focusedClient);
},
.frame = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*) {
auto mon = static_cast<Monitor*>(mv);
if (!mon->bar) {
mon->bar.emplace(mon->wlOutput.get());
if (mon->created) {
mon->bar->invalidate();
} else {
mon->bar->create(mon->wlOutput.get());
}
}
};
static void setupMonitor(Monitor &monitor) {
monitor.dwlMonitor.reset(znet_tapesoftware_dwl_wm_v1_get_monitor(dwlWm, monitor.wlOutput.get()));
monitor.bar.emplace();
monitor.bar->setStatus(lastStatus);
znet_tapesoftware_dwl_wm_monitor_v1_add_listener(monitor.dwlMonitor.get(), &dwlWmMonitorListener, &monitor);
}
@ -208,10 +215,11 @@ static void onStatus()
{
char buffer[512];
auto n = read(statusFifoFd, buffer, sizeof(buffer));
auto str = QString::fromUtf8(buffer, n);
lastStatus = QString::fromUtf8(buffer, n);
for (auto &monitor : monitors) {
if (monitor.bar) {
monitor.bar->setStatus(str);
monitor.bar->setStatus(lastStatus);
monitor.bar->invalidate();
}
}
}