read dwl status from stdin

This commit is contained in:
Raphael Robatsch 2021-10-29 22:22:58 +02:00
parent a600256e24
commit fd501be2d6
6 changed files with 84 additions and 96 deletions

View File

@ -15,7 +15,6 @@ wayland_xmls = [
wl_protocol_dir + '/stable/xdg-shell/xdg-shell.xml',
wl_protocol_dir + '/unstable/xdg-output/xdg-output-unstable-v1.xml',
'wlr-layer-shell-unstable-v1.xml',
'net-tapesoftware-dwl-wm-unstable-v1.xml',
]
wayland_sources = [
wayland_scanner_code.process(wayland_xmls),

View File

@ -72,7 +72,7 @@ Bar::Bar(Monitor* mon)
_pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default()));
if (!_pangoContext) die("pango_font_map_create_context");
for (auto i=0u; i<tagNames.size(); i++) {
_tags.push_back({ ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_NONE, 0, 0, createComponent(tagNames[i]) });
_tags.push_back({ TagState::None, 0, 0, createComponent(tagNames[i]) });
}
_layoutCmp = createComponent();
_titleCmp = createComponent();
@ -107,7 +107,7 @@ void Bar::hide()
_bufs.reset();
}
void Bar::setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient)
void Bar::setTag(int tag, int state, int numClients, int focusedClient)
{
auto& t = _tags[tag];
t.state = state;
@ -115,7 +115,7 @@ void Bar::setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, i
t.focusedClient = focusedClient;
}
void Bar::setSelected(bool selected) { _selected = selected; }
void Bar::setLayout(int layout) { _layoutCmp.setText(layoutNames[layout]); }
void Bar::setLayout(const std::string &layout) { _layoutCmp.setText(layout); }
void Bar::setTitle(const std::string& title) { _titleCmp.setText(title); }
void Bar::setStatus(const std::string& status) { _statusCmp.setText(status); }
@ -196,8 +196,8 @@ void Bar::renderTags()
{
for (auto &tag : _tags) {
setColorScheme(
tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE ? colorActive : colorInactive,
tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_URGENT);
tag.state & TagState::Active ? colorActive : colorInactive,
tag.state & TagState::Urgent);
renderComponent(tag.component);
auto indicators = std::min(tag.numClients, _bufs->height/2);
for (auto ind = 0; ind < indicators; ind++) {

View File

@ -22,7 +22,7 @@ public:
};
struct Tag {
znet_tapesoftware_dwl_wm_monitor_v1_tag_state state;
int state;
int numClients;
int focusedClient;
BarComponent component;
@ -65,9 +65,9 @@ public:
bool visible() const;
void show(wl_output* output);
void hide();
void setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient);
void setTag(int tag, int state, int numClients, int focusedClient);
void setSelected(bool selected);
void setLayout(int layout);
void setLayout(const std::string& layout);
void setTitle(const std::string& title);
void setStatus(const std::string& status);
void invalidate();

View File

@ -10,7 +10,6 @@
#include <cairo/cairo.h>
#include <pango/pango.h>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "net-tapesoftware-dwl-wm-unstable-v1-client-protocol.h"
struct Color {
Color() {}
@ -26,6 +25,7 @@ union Arg {
};
struct Monitor;
enum TagState { None, Active = 0x01, Urgent = 0x02 };
enum { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText };
struct Button {
int control;
@ -38,8 +38,6 @@ extern wl_display* display;
extern wl_compositor* compositor;
extern wl_shm* shm;
extern zwlr_layer_shell_v1* wlrLayerShell;
extern std::vector<std::string> tagNames;
extern std::vector<std::string> layoutNames;
void view(Monitor& m, const Arg& arg);
void toggleview(Monitor& m, const Arg& arg);
@ -64,7 +62,6 @@ WL_DELETER(wl_output, wl_output_release);
WL_DELETER(wl_pointer, wl_pointer_release);
WL_DELETER(wl_seat, wl_seat_release);
WL_DELETER(wl_surface, wl_surface_destroy);
WL_DELETER(znet_tapesoftware_dwl_wm_monitor_v1, znet_tapesoftware_dwl_wm_monitor_v1_release);
WL_DELETER(zwlr_layer_surface_v1, zwlr_layer_surface_v1_destroy);
WL_DELETER(cairo_t, cairo_destroy);

View File

@ -10,17 +10,17 @@ constexpr int paddingX = 10;
constexpr int paddingY = 3;
// See https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html
constexpr const char* font = "Sans 12";
constexpr const char *font = "Sans 12";
constexpr ColorScheme colorInactive = {Color(0xbb, 0xbb, 0xbb), Color(0x22, 0x22, 0x22)};
constexpr ColorScheme colorActive = {Color(0xee, 0xee, 0xee), Color(0x00, 0x55, 0x77)};
constexpr const char* termcmd[] = {"foot", nullptr};
constexpr const char *termcmd[] = {"foot", nullptr};
static std::vector<std::string> tagNames = {
"1", "2", "3",
"4", "5", "6",
"7", "8", "9",
};
constexpr Button buttons[] = {
{ ClkTagBar, BTN_LEFT, view, {0} },
{ ClkTagBar, BTN_RIGHT, tag, {0} },
{ ClkTagBar, BTN_MIDDLE, toggletag, {0} },
{ ClkLayoutSymbol, BTN_LEFT, setlayout, {.ui = 0} },
{ ClkLayoutSymbol, BTN_RIGHT, setlayout, {.ui = 2} },
{ ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} },
};

View File

@ -3,6 +3,7 @@
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <list>
#include <optional>
#include <vector>
@ -20,8 +21,8 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "net-tapesoftware-dwl-wm-unstable-v1-client-protocol.h"
#include "common.hpp"
#include "config.hpp"
#include "bar.hpp"
#include "line_buffer.hpp"
@ -29,7 +30,6 @@ struct Monitor {
uint32_t registryName;
std::string xdgName;
wl_unique_ptr<wl_output> wlOutput;
wl_unique_ptr<znet_tapesoftware_dwl_wm_monitor_v1> dwlMonitor;
std::optional<Bar> bar;
bool desiredVisibility {true};
bool hasData;
@ -60,9 +60,6 @@ wl_display* display;
wl_compositor* compositor;
wl_shm* shm;
zwlr_layer_shell_v1* wlrLayerShell;
znet_tapesoftware_dwl_wm_v1* dwlWm;
std::vector<std::string> tagNames;
std::vector<std::string> layoutNames;
static xdg_wm_base* xdgWmBase;
static zxdg_output_manager_v1* xdgOutputManager;
static wl_surface* cursorSurface;
@ -79,26 +76,6 @@ static int statusFifoFd {-1};
static int statusFifoWriter {-1};
static bool quitting {false};
void view(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(m.dwlMonitor.get(), arg.ui, 1);
}
void toggleview(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(m.dwlMonitor.get(), m.tags ^ arg.ui, 0);
}
void setlayout(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_layout(m.dwlMonitor.get(), arg.ui);
}
void tag(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_client_tags(m.dwlMonitor.get(), 0, arg.ui);
}
void toggletag(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_client_tags(m.dwlMonitor.get(), 0xffffff, arg.ui);
}
void spawn(Monitor&, const Arg& arg)
{
if (fork() == 0) {
@ -200,57 +177,64 @@ static const struct wl_seat_listener seatListener = {
.name = [](void*, wl_seat*, const char *name) { }
};
static const struct znet_tapesoftware_dwl_wm_v1_listener dwlWmListener = {
.tag = [](void*, znet_tapesoftware_dwl_wm_v1*, const char* name) {
tagNames.push_back(name);
},
.layout = [](void*, znet_tapesoftware_dwl_wm_v1*, const char* name) {
layoutNames.push_back(name);
},
};
static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwlWmMonitorListener {
.selected = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t selected) {
auto mon = static_cast<Monitor*>(mv);
if (selected) {
selmon = mon;
} else if (selmon == mon) {
selmon = nullptr;
}
mon->bar->setSelected(selected);
},
.tag = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t tag, uint32_t state, uint32_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);
uint32_t mask = 1 << tag;
if (state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE) {
mon->tags |= mask;
} else {
mon->tags &= ~mask;
}
},
.layout = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t layout) {
auto mon = static_cast<Monitor*>(mv);
mon->bar->setLayout(layout);
},
.title = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, const char* title) {
auto mon = static_cast<Monitor*>(mv);
mon->bar->setTitle(title);
},
.frame = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*) {
auto mon = static_cast<Monitor*>(mv);
mon->hasData = true;
updatemon(*mon);
static void handleStdin(const std::string& line)
{
// this parses the lines that dwl sends in printstatus()
std::string monName, command;
auto stream = std::istringstream {line};
stream >> monName >> command;
if (!stream.good()) {
return;
}
};
auto mon = std::find_if(begin(monitors), end(monitors), [&](const Monitor& mon) {
return mon.xdgName == monName;
});
if (mon == end(monitors))
return;
if (command == "title") {
auto title = std::string {};
std::getline(stream, title);
mon->bar->setTitle(title);
} else if (command == "selmon") {
uint32_t selected;
stream >> selected;
mon->bar->setSelected(selected);
} else if (command == "tags") {
uint32_t occupied, tags, clientTags, urgent;
stream >> occupied >> tags >> clientTags >> urgent;
for (auto i=0u; i<tagNames.size(); i++) {
auto tagMask = 1 << i;
int state = TagState::None;
if (tags & tagMask)
state |= TagState::Active;
if (urgent & tagMask)
state |= TagState::Urgent;
mon->bar->setTag(i, state, occupied & tagMask ? 1 : 0, clientTags ? 1 : 0);
}
mon->tags = tags;
} else if (command == "layout") {
auto layout = std::string {};
std::getline(stream, layout);
mon->bar->setLayout(layout);
}
mon->hasData = true;
updatemon(*mon);
}
static LineBuffer<512> _stdinBuffer;
static void onStdin()
{
auto res = _stdinBuffer.readLines(
[](void* p, size_t size) { return read(0, p, size); },
[](char* p, size_t size) { handleStdin({p, size}); });
if (res == 0) {
quitting = true;
}
}
static void setupMonitor(Monitor& monitor) {
monitor.dwlMonitor.reset(znet_tapesoftware_dwl_wm_v1_get_monitor(dwlWm, monitor.wlOutput.get()));
monitor.bar.emplace(&monitor);
monitor.bar->setStatus(lastStatus);
auto xdgOutput = zxdg_output_manager_v1_get_xdg_output(xdgOutputManager, monitor.wlOutput.get());
zxdg_output_v1_add_listener(xdgOutput, &xdgOutputListener, &monitor);
znet_tapesoftware_dwl_wm_monitor_v1_add_listener(monitor.dwlMonitor.get(), &dwlWmMonitorListener, &monitor);
}
static void updatemon(Monitor& mon)
@ -274,9 +258,17 @@ static void onReady()
requireGlobal(shm, "wl_shm");
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
requireGlobal(xdgOutputManager, "zxdg_output_manager_v1");
requireGlobal(dwlWm, "znet_tapesoftware_dwl_wm_v1");
setupStatusFifo();
wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc.
epoll_event epollEv = {0};
epollEv.events = EPOLLIN;
epollEv.data.fd = 0;
fcntl(0, F_SETFL, O_NONBLOCK);
if (epoll_ctl(epoll, EPOLL_CTL_ADD, 0, &epollEv) < 0) {
diesys("epoll_ctl add stdin");
}
ready = true;
for (auto& monitor : monitors) {
setupMonitor(monitor);
@ -390,10 +382,6 @@ static void registryHandleGlobal(void*, wl_registry* registry, uint32_t name, co
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
return;
}
if (reg.handle(dwlWm, znet_tapesoftware_dwl_wm_v1_interface, 1)) {
znet_tapesoftware_dwl_wm_v1_add_listener(dwlWm, &dwlWmListener, nullptr);
return;
}
if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) {
auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}});
wl_seat_add_listener(wlSeat, &seatListener, &seat);
@ -401,6 +389,8 @@ static void registryHandleGlobal(void*, wl_registry* registry, uint32_t name, co
}
if (wl_output *output; reg.handle(output, wl_output_interface, 1)) {
auto& m = monitors.emplace_back(Monitor {name, {}, wl_unique_ptr<wl_output> {output}});
auto xdgOutput = zxdg_output_manager_v1_get_xdg_output(xdgOutputManager, m.wlOutput.get());
zxdg_output_v1_add_listener(xdgOutput, &xdgOutputListener, &m);
if (ready) {
setupMonitor(m);
}
@ -516,6 +506,8 @@ int main(int argc, char* argv[])
}
waylandFlush();
}
} else if (ev.data.fd == 0) {
onStdin();
} else if (ev.data.fd == statusFifoFd) {
onStatus();
} else if (ev.data.fd == sfd) {