From fd501be2d6e2534b1d38b2c771caeb53cf2dd351 Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Fri, 29 Oct 2021 22:22:58 +0200 Subject: [PATCH] read dwl status from stdin --- protocols/meson.build | 1 - src/bar.cpp | 10 +-- src/bar.hpp | 6 +- src/common.hpp | 5 +- src/config.def.hpp | 14 ++-- src/main.cpp | 144 ++++++++++++++++++++---------------------- 6 files changed, 84 insertions(+), 96 deletions(-) diff --git a/protocols/meson.build b/protocols/meson.build index 5752608..7bd222b 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -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), diff --git a/src/bar.cpp b/src/bar.cpp index bcc4c05..6d1fdda 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -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; iheight/2); for (auto ind = 0; ind < indicators; ind++) { diff --git a/src/bar.hpp b/src/bar.hpp index 4fc72e9..0fd7710 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -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(); diff --git a/src/common.hpp b/src/common.hpp index 548f67e..2fba11b 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -10,7 +10,6 @@ #include #include #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 tagNames; -extern std::vector 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); diff --git a/src/config.def.hpp b/src/config.def.hpp index a9560cb..788cfa2 100644 --- a/src/config.def.hpp +++ b/src/config.def.hpp @@ -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 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} }, }; diff --git a/src/main.cpp b/src/main.cpp index 68be4b4..7e941d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -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 wlOutput; - wl_unique_ptr dwlMonitor; std::optional 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 tagNames; -std::vector 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(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(mv); - mon->bar->setTag(tag, static_cast(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(mv); - mon->bar->setLayout(layout); - }, - .title = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, const char* title) { - auto mon = static_cast(mv); - mon->bar->setTitle(title); - }, - .frame = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*) { - auto mon = static_cast(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; ibar->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 {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 {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) {