From 5b5086084b4f8451afb097a471f905c2cb2ad634 Mon Sep 17 00:00:00 2001 From: stkhan Date: Tue, 26 Dec 2023 13:42:41 -0600 Subject: Init commit --- somebar/.builds/archlinux.yml | 19 + somebar/.builds/freebsd.yml | 20 + somebar/.editorconfig | 5 + somebar/CHANGELOG.md | 19 + somebar/LICENSE | 20 + somebar/README.md | 113 ++++ somebar/contrib/clickable-tags-using-wtype.patch | 91 +++ somebar/contrib/colorless-status.patch | 15 + somebar/contrib/disable-window-title.patch | 15 + somebar/contrib/dwm-like-tag-indicator.patch | 34 ++ somebar/contrib/hide-vacant-tags.patch | 54 ++ somebar/contrib/indicator-size-props.patch | 54 ++ somebar/contrib/ipc.patch | 506 +++++++++++++++++ somebar/contrib/markup-in-status-messages.patch | 65 +++ .../contrib/show-status-on-selected-monitor.patch | 43 ++ somebar/meson.build | 31 ++ somebar/protocols/meson.build | 22 + somebar/protocols/wlr-layer-shell-unstable-v1.xml | 390 +++++++++++++ somebar/screenshot.png | Bin 0 -> 6715 bytes somebar/somebar.1 | 55 ++ somebar/src/bar.cpp | 315 +++++++++++ somebar/src/bar.hpp | 74 +++ somebar/src/common.hpp | 76 +++ somebar/src/config.def.hpp | 27 + somebar/src/config.hpp | 27 + somebar/src/line_buffer.hpp | 71 +++ somebar/src/main.cpp | 613 +++++++++++++++++++++ somebar/src/shm_buffer.cpp | 85 +++ somebar/src/shm_buffer.hpp | 45 ++ 29 files changed, 2904 insertions(+) create mode 100644 somebar/.builds/archlinux.yml create mode 100644 somebar/.builds/freebsd.yml create mode 100644 somebar/.editorconfig create mode 100644 somebar/CHANGELOG.md create mode 100644 somebar/LICENSE create mode 100644 somebar/README.md create mode 100644 somebar/contrib/clickable-tags-using-wtype.patch create mode 100644 somebar/contrib/colorless-status.patch create mode 100644 somebar/contrib/disable-window-title.patch create mode 100644 somebar/contrib/dwm-like-tag-indicator.patch create mode 100644 somebar/contrib/hide-vacant-tags.patch create mode 100644 somebar/contrib/indicator-size-props.patch create mode 100644 somebar/contrib/ipc.patch create mode 100644 somebar/contrib/markup-in-status-messages.patch create mode 100644 somebar/contrib/show-status-on-selected-monitor.patch create mode 100644 somebar/meson.build create mode 100644 somebar/protocols/meson.build create mode 100644 somebar/protocols/wlr-layer-shell-unstable-v1.xml create mode 100644 somebar/screenshot.png create mode 100644 somebar/somebar.1 create mode 100644 somebar/src/bar.cpp create mode 100644 somebar/src/bar.hpp create mode 100644 somebar/src/common.hpp create mode 100644 somebar/src/config.def.hpp create mode 100644 somebar/src/config.hpp create mode 100644 somebar/src/line_buffer.hpp create mode 100644 somebar/src/main.cpp create mode 100644 somebar/src/shm_buffer.cpp create mode 100644 somebar/src/shm_buffer.hpp (limited to 'somebar') diff --git a/somebar/.builds/archlinux.yml b/somebar/.builds/archlinux.yml new file mode 100644 index 0000000..7c4f209 --- /dev/null +++ b/somebar/.builds/archlinux.yml @@ -0,0 +1,19 @@ +image: archlinux +packages: + - base-devel + - meson + - wayland + - wayland-protocols + - cairo + - pango +sources: + - https://git.sr.ht/~raphi/somebar +tasks: + - setup: | + cd somebar + meson build --fatal-meson-warnings + cp src/config.def.hpp src/config.hpp + - build: | + cd somebar/build + ninja + sudo ninja install diff --git a/somebar/.builds/freebsd.yml b/somebar/.builds/freebsd.yml new file mode 100644 index 0000000..90e6f1a --- /dev/null +++ b/somebar/.builds/freebsd.yml @@ -0,0 +1,20 @@ +image: freebsd/latest +packages: + - devel/evdev-proto + - devel/meson + - devel/pkgconf + - graphics/cairo + - graphics/wayland + - graphics/wayland-protocols + - x11-toolkits/pango +sources: + - https://git.sr.ht/~raphi/somebar +tasks: + - setup: | + cd somebar + meson build --fatal-meson-warnings + cp src/config.def.hpp src/config.hpp + - build: | + cd somebar/build + ninja + sudo ninja install \ No newline at end of file diff --git a/somebar/.editorconfig b/somebar/.editorconfig new file mode 100644 index 0000000..5c4a037 --- /dev/null +++ b/somebar/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*] +indent_style = tab +indent_brace_style = K&R diff --git a/somebar/CHANGELOG.md b/somebar/CHANGELOG.md new file mode 100644 index 0000000..a24e7ab --- /dev/null +++ b/somebar/CHANGELOG.md @@ -0,0 +1,19 @@ +## [1.0.3] - 2022-12-11 +### Added +- New patches: markup-in-status-messages, show-status-on-selected-monitor (benc) +### Fixed +- Fixed crash when an output disappears + +## [1.0.2] - 2022-11-27 +### Fixed +- Fixed bug where hidden bar could not be shown anymore + +## [1.0.1] - 2022-11-25 +### Added +- Manpage +- New patches: indicator-size-props, hide-vacant-tags, colorless-status (medanisjbara) +### Fixed +- Remove spaces from title and layout symbol (benc) + +## [1.0.0] - 2022-04-23 +Initial release diff --git a/somebar/LICENSE b/somebar/LICENSE new file mode 100644 index 0000000..7bebc11 --- /dev/null +++ b/somebar/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2021 Raphael Robatsch + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/somebar/README.md b/somebar/README.md new file mode 100644 index 0000000..b61c935 --- /dev/null +++ b/somebar/README.md @@ -0,0 +1,113 @@ +# somebar - dwm-like bar for dwl + +![Screenshot](screenshot.png) + +The mailing list for this project is +[~raphi/public-inbox@lists.sr.ht](mailto:~raphi/public-inbox@lists.sr.ht). + +## Dependencies + +* c++ compiler, meson, and ninja +* wayland-scanner +* libwayland-client +* libwayland-cursor +* libcairo +* libpango +* libpangocairo + +``` +sudo apt install build-essential meson ninja-build \ + libwayland-bin libwayland-client0 libwayland-cursor0 libwayland-dev \ + libcairo2 libcairo2-dev \ + libpango-1.0-0 libpango1.0-dev libpangocairo-1.0-0 + +# or + +sudo pacman -S base-devel meson \ + wayland wayland-protocols cairo pango +``` + +## Configuration + +Copy `src/config.def.hpp` to `src/config.hpp`, and adjust if needed. + +## Building + +``` +cp src/config.def.hpp src/config.hpp +meson setup build +ninja -C build +sudo ninja -C build install +``` + +## Usage + +You must start somebar using dwl's `-s` flag, e.g. `dwl -s somebar`. + +Somebar can be controlled by writing to `$XDG_RUNTIME_DIR/somebar-0` +or the path defined by `-s` argument. +The following commands are supported: + +* `status TEXT`: Updates the status bar +* `hide MONITOR` Hides somebar on the specified monitor +* `show MONITOR` Shows somebar on the specified monitor +* `toggle MONITOR` Toggles somebar on the specified monitor + +MONITOR is an zxdg_output_v1 name, which can be determined e.g. using `weston-info`. +Additionally, MONITOR can be `all` (all monitors) or `selected` (the monitor with focus). + +Commands can be sent either by writing to the file name above, or equivalently by calling +somebar with the `-c` argument. For example: `somebar -c toggle all`. This is recommended +for shell scripts, as there is no race-free way to write to a file only if it exists. + +The maintainer of somebar also maintains +[someblocks](https://git.sr.ht/~raphi/someblocks/), +a fork of [dwmblocks](https://github.com/torrinfail/dwmblocks) that outputs +to somebar instead of dwm's bar. + +## IPC + +Out of the box, somebar cannot control dwl. Clicking on the tag bar has no +effect, because there is no communication channel from somebar back to dwl. + +If you apply the patch `contrib/ipc.patch`, then somebar will + +1. Not read stdin anymore, and instead use a wayland extension to read dwl's + state. This means you must close stdin yourself, if you choose to launch + somebar using dwl's -s flag. +2. somebar can use the same wayland extension to send commands back to dwl. + This means that clicking on tags will switch to that tag (this can of course + be customized in config.h). + +If you apply the IPC patch to somebar, then +**dwl must have the [wayland-ipc patch](https://git.sr.ht/~raphi/dwl/blob/master/patches/wayland-ipc.patch) applied too**, +since dwl must implement the wayland extension too. + +## Other patches + +Like all suckless software, somebar can be customized via patches. You can find some patches in the contrib folder with descriptions written in them. + +## License + +somebar - dwm-like bar for dwl + +Copyright (c) 2021 Raphael Robatsch + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/somebar/contrib/clickable-tags-using-wtype.patch b/somebar/contrib/clickable-tags-using-wtype.patch new file mode 100644 index 0000000..92ce15c --- /dev/null +++ b/somebar/contrib/clickable-tags-using-wtype.patch @@ -0,0 +1,91 @@ +From: Ben Collerson +Date: Sat, 1 Apr 2023 09:16:19 +1000 +Subject: [PATCH somebar] clickable tags using wtype + +--- + src/bar.cpp | 3 ++- + src/common.hpp | 2 ++ + src/config.def.hpp | 5 +++++ + src/main.cpp | 15 +++++++++++++++ + 4 files changed, 24 insertions(+), 1 deletion(-) + +diff --git a/src/bar.cpp b/src/bar.cpp +index af92f49..8b68759 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -182,9 +182,10 @@ void Bar::click(Monitor* mon, int x, int, int btn) + } else if (x > _layoutCmp.x) { + control = ClkLayoutSymbol; + } else for (int tag = _tags.size()-1; tag >= 0; tag--) { ++ // you may need logic to skip tags if they are hidden elsewhere. + if (x > _tags[tag].component.x) { + control = ClkTagBar; +- arg.ui = 1< tagNames = { + }; + + constexpr Button buttons[] = { ++ { ClkTagBar, BTN_LEFT, view, {0} }, ++ { ClkTagBar, BTN_RIGHT, tag, {0} }, + { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, + }; +diff --git a/src/main.cpp b/src/main.cpp +index 6274959..3c35b20 100644 +--- a/src/main.cpp ++++ b/src/main.cpp +@@ -85,6 +85,21 @@ static int statusFifoFd {-1}; + static int statusFifoWriter {-1}; + static bool quitting {false}; + ++// update keybindings to match your dwl ++void view(Monitor& m, const Arg& arg) ++{ ++ char tag[2]; ++ snprintf(tag, 2, "%i", arg.ui + 1); ++ if(fork() == 0) ++ execl(WTYPE, "wtype", "-M", MODKEY, tag, (char*)NULL); ++} ++void tag(Monitor& m, const Arg& arg) ++{ ++ char tag[2]; ++ snprintf(tag, 2, "%i", arg.ui + 1); ++ if(fork() == 0) ++ execl(WTYPE, "wtype", "-M", MODKEY, "-M", "shift", tag, (char*)NULL); ++} + void spawn(Monitor&, const Arg& arg) + { + if (fork() == 0) { +-- +2.40.1 + diff --git a/somebar/contrib/colorless-status.patch b/somebar/contrib/colorless-status.patch new file mode 100644 index 0000000..f280070 --- /dev/null +++ b/somebar/contrib/colorless-status.patch @@ -0,0 +1,15 @@ +From: medanisjbara anis2834133766619@gmail.com +Date: Mon, 14 Nov 2022 10:28:00 +Description: sets the color of status component to inactive +diff --git a/src/bar.cpp b/src/bar.cpp +index fab5a8f..aebe28b 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -266,6 +266,7 @@ void Bar::renderStatus() + cairo_fill(_painter); + + _x = start; ++ setColorScheme(colorInactive); + renderComponent(_statusCmp); + } + diff --git a/somebar/contrib/disable-window-title.patch b/somebar/contrib/disable-window-title.patch new file mode 100644 index 0000000..b1dc9cd --- /dev/null +++ b/somebar/contrib/disable-window-title.patch @@ -0,0 +1,15 @@ +From: Sam Nystrom +Date: Sat, 4 Mar 2023 17:38:12 -0500 +Description: disable window title +diff --git a/src/bar.cpp b/src/bar.cpp +index 507ce62..1b6f771 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -227,7 +227,6 @@ void Bar::render() + renderTags(); + setColorScheme(_selected ? colorActive : colorInactive); + renderComponent(_layoutCmp); +- renderComponent(_titleCmp); + renderStatus(); + + _painter = nullptr; diff --git a/somebar/contrib/dwm-like-tag-indicator.patch b/somebar/contrib/dwm-like-tag-indicator.patch new file mode 100644 index 0000000..c4458e9 --- /dev/null +++ b/somebar/contrib/dwm-like-tag-indicator.patch @@ -0,0 +1,34 @@ +From: Henrique Possatto +Date: Mon, 26 Dec 2022 18:01:35 -0300 +Subject: [PATCH somebar] patch to show square tag indicator like dwm +diff --git a/src/bar.cpp b/src/bar.cpp +index 507ce62..4fda8b0 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -245,12 +245,17 @@ void Bar::renderTags() + tag.state & TagState::Active ? colorActive : colorInactive, + tag.state & TagState::Urgent); + renderComponent(tag.component); +- auto indicators = std::min(tag.numClients, static_cast(_bufs->height/2)); +- for (auto ind = 0; ind < indicators; ind++) { +- auto w = ind == tag.focusedClient ? 7 : 1; +- cairo_move_to(_painter, tag.component.x, ind*2+0.5); +- cairo_rel_line_to(_painter, w, 0); +- cairo_close_path(_painter); ++ ++ if(!tag.numClients) ++ continue; ++ ++ int s = barfont.height / 9; ++ int w = barfont.height / 6 + 2; ++ if (tag.focusedClient != -1) { ++ cairo_rectangle(_painter, tag.component.x + s, s, w, w); ++ cairo_fill(_painter); ++ } else { ++ cairo_rectangle(_painter, (double)(tag.component.x + s) + 0.5, (double)(s) + 0.5, w, w); + cairo_set_line_width(_painter, 1); + cairo_stroke(_painter); + } +-- +2.39.0 + diff --git a/somebar/contrib/hide-vacant-tags.patch b/somebar/contrib/hide-vacant-tags.patch new file mode 100644 index 0000000..055dd51 --- /dev/null +++ b/somebar/contrib/hide-vacant-tags.patch @@ -0,0 +1,54 @@ +From: medanisjbara anis2834133766619@gmail.com +Date: Mon, 14 Nov 2022 22:52:00 +Description: somebar equivalent of https://dwm.suckless.org/patches/hide_vacant_tags +diff --git a/src/bar.cpp b/src/bar.cpp +index fab5a8f..38e7b5f 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -240,13 +240,36 @@ void Bar::render() + + void Bar::renderTags() + { ++ // Check if all tags are active (Mod+0) ++ bool allActive = true; + for (auto &tag : _tags) { ++ if (tag.state & TagState::Active){ ++ if (!allActive){ ++ allActive = true; ++ break; ++ } ++ allActive = false; ++ } ++ } ++ ++ bool renderThis; ++ for (auto &tag : _tags) { ++ renderThis = false; + setColorScheme( + tag.state & TagState::Active ? colorActive : colorInactive, + tag.state & TagState::Urgent); +- renderComponent(tag.component); ++ // Reder active tag if it's the only one active ++ if (!allActive && tag.state & TagState::Active) ++ renderThis = true; + auto indicators = std::min(tag.numClients, static_cast(_bufs->height/2)); + for (auto ind = 0; ind < indicators; ind++) { ++ // render tags having indicators ++ if (tag.focusedClient == -1) ++ renderThis = true; ++ // render tags having the focused client ++ if (tag.focusedClient == 0){ ++ renderThis = true; ++ } + auto w = ind == tag.focusedClient ? 7 : 1; + cairo_move_to(_painter, tag.component.x, ind*2+0.5); + cairo_rel_line_to(_painter, w, 0); +@@ -254,6 +277,8 @@ void Bar::renderTags() + cairo_set_line_width(_painter, 1); + cairo_stroke(_painter); + } ++ if (renderThis) ++ renderComponent(tag.component); + } + } + diff --git a/somebar/contrib/indicator-size-props.patch b/somebar/contrib/indicator-size-props.patch new file mode 100644 index 0000000..ac17520 --- /dev/null +++ b/somebar/contrib/indicator-size-props.patch @@ -0,0 +1,54 @@ +From: medanisjbara anis2834133766619@gmail.com +Date: Mon, 15 Nov 2022 08:16:00 +Description: lets config.h customize indicators sizes +diff --git a/src/bar.cpp b/src/bar.cpp +index fab5a8f..c0e070c 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -247,11 +247,11 @@ void Bar::renderTags() + renderComponent(tag.component); + auto indicators = std::min(tag.numClients, static_cast(_bufs->height/2)); + for (auto ind = 0; ind < indicators; ind++) { +- auto w = ind == tag.focusedClient ? 7 : 1; ++ auto w = ind == tag.focusedClient ? indprops.focused_width : indprops.width; + cairo_move_to(_painter, tag.component.x, ind*2+0.5); + cairo_rel_line_to(_painter, w, 0); + cairo_close_path(_painter); +- cairo_set_line_width(_painter, 1); ++ cairo_set_line_width(_painter, ind == tag.focusedClient ? indprops.focused_height : indprops.height); + cairo_stroke(_painter); + } + } +diff --git a/src/common.hpp b/src/common.hpp +index aed4480..acdca1b 100644 +--- a/src/common.hpp ++++ b/src/common.hpp +@@ -34,6 +34,13 @@ struct Button { + const Arg arg; + }; + ++struct IndicatorProps { ++ int width; ++ int height; ++ int focused_width; ++ int focused_height; ++}; ++ + extern wl_display* display; + extern wl_compositor* compositor; + extern wl_shm* shm; +diff --git a/src/config.def.hpp b/src/config.def.hpp +index 40a8c95..d51fee8 100644 +--- a/src/config.def.hpp ++++ b/src/config.def.hpp +@@ -25,3 +25,10 @@ static std::vector tagNames = { + constexpr Button buttons[] = { + { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, + }; ++ ++constexpr IndicatorProps indprops = { ++ 5, /* unfocused indicator width */ ++ 5, /* unfocused indicator height */ ++ 7, /* focused indicator width */ ++ 1 /* focused indicator height */ ++}; diff --git a/somebar/contrib/ipc.patch b/somebar/contrib/ipc.patch new file mode 100644 index 0000000..80c1a56 --- /dev/null +++ b/somebar/contrib/ipc.patch @@ -0,0 +1,506 @@ +Replaces somebar's channel to dwl from stdin to a wayland extension. + +Normally, somebar reads dwl's state by reading from stdin. This requires +that somebar is started from dwl, and does not allow sending commands back +to dwl. + +This patch replaces this channel with a wayland protocol extension. somebar +can use this protocol extension to observe and update the dwl window management +state. + +Important! dwl must have the wayland-ipc patch applied for this to work! +https://git.sr.ht/~raphi/dwl/blob/master/patches/wayland-ipc.patch +diff --git a/protocols/meson.build b/protocols/meson.build +index 7bd222b..5752608 100644 +--- a/protocols/meson.build ++++ b/protocols/meson.build +@@ -15,6 +15,7 @@ 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/protocols/net-tapesoftware-dwl-wm-unstable-v1.xml b/protocols/net-tapesoftware-dwl-wm-unstable-v1.xml +new file mode 100644 +index 0000000..390f5a1 +--- /dev/null ++++ b/protocols/net-tapesoftware-dwl-wm-unstable-v1.xml +@@ -0,0 +1,164 @@ ++ ++ ++ ++ Copyright (c) 2021 Raphael Robatsch ++ ++ Permission is hereby granted, free of charge, to any person obtaining a ++ copy of this software and associated documentation files (the ++ "Software"), to deal in the Software without restriction, including ++ without limitation the rights to use, copy, modify, merge, publish, ++ distribute, sublicense, and/or sell copies of the Software, and to ++ permit persons to whom the Software is furnished to do so, subject to ++ the following conditions: ++ ++ The above copyright notice and this permission notice (including the ++ next paragraph) shall be included in all copies or substantial portions ++ of the Software. ++ ++ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ++ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ++ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ++ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ++ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ++ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ++ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++ ++ ++ ++ ++ This interface is exposed as a global in the wl_registry. ++ ++ Clients can use this protocol to receive updates of the window manager ++ state (active tags, active layout, and focused window). ++ Clients can also control this state. ++ ++ After binding, the client will receive the available tags and layouts ++ with the 'tag' and 'layout' events. These can be used in subsequent ++ dwl_wm_monitor_v1.set_tags/set_layout requests, and to interpret the ++ dwl_wm_monitor_v1.layout/tag events. ++ ++ ++ ++ ++ This request indicates that the client will not use the dwl_wm ++ object any more. Objects that have been created through this instance ++ are not affected. ++ ++ ++ ++ ++ ++ Gets a dwl monitor for the specified output. The window manager ++ state on the output can be controlled using the monitor. ++ ++ ++ ++ ++ ++ ++ ++ This event is sent immediately after binding. ++ A roundtrip after binding guarantees that the client has received all tags. ++ ++ ++ ++ ++ ++ ++ This event is sent immediately after binding. ++ A roundtrip after binding guarantees that the client has received all layouts. ++ ++ ++ ++ ++ ++ ++ ++ Observes and controls one monitor. ++ ++ Events are double-buffered: Clients should cache all events and only ++ redraw themselves once the 'frame' event is sent. ++ ++ Requests are not double-buffered: The compositor will update itself ++ immediately. ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ This request indicates that the client is done with this dwl_monitor. ++ All further requests are ignored. ++ ++ ++ ++ ++ ++ If 'selected' is nonzero, this monitor is the currently selected one. ++ ++ ++ ++ ++ ++ ++ Announces the update of a tag. num_clients and focused_client can be ++ used to draw client indicators. ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ Announces the update of the selected layout. ++ ++ ++ ++ ++ ++ ++ Announces the update of the selected client. ++ ++ ++ ++ ++ ++ ++ Sent after all other events belonging to the status update has been sent. ++ Clients should redraw themselves now. ++ ++ ++ ++ ++ ++ Changes are applied immediately. ++ ++ ++ ++ ++ ++ ++ ++ tags are updated as follows: ++ new_tags = (current_tags AND and_tags) XOR xor_tags ++ ++ Changes are applied immediately. ++ ++ ++ ++ ++ ++ ++ ++ Changes are applied immediately. ++ ++ ++ ++ ++ +diff --git a/src/common.hpp b/src/common.hpp +index c905358..9b62a94 100644 +--- a/src/common.hpp ++++ b/src/common.hpp +@@ -10,6 +10,7 @@ + #include + #include + #include "wlr-layer-shell-unstable-v1-client-protocol.h" ++#include "net-tapesoftware-dwl-wm-unstable-v1-client-protocol.h" + + struct Color { + Color() {} +@@ -38,7 +39,14 @@ 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); ++void setlayout(Monitor& m, const Arg& arg); ++void tag(Monitor& m, const Arg& arg); ++void toggletag(Monitor& m, const Arg& arg); + void spawn(Monitor&, const Arg& arg); + void setCloexec(int fd); + [[noreturn]] void die(const char* why); +@@ -65,6 +73,7 @@ WL_DELETER(wl_output, wl_output_release_checked); + 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 40a8c95..a9560cb 100644 +--- a/src/config.def.hpp ++++ b/src/config.def.hpp +@@ -16,12 +16,11 @@ constexpr ColorScheme colorInactive = {Color(0xbb, 0xbb, 0xbb), Color(0x22, 0x22 + constexpr ColorScheme colorActive = {Color(0xee, 0xee, 0xee), Color(0x00, 0x55, 0x77)}; + 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 6274959..01be870 100644 +--- a/src/main.cpp ++++ b/src/main.cpp +@@ -3,7 +3,6 @@ + + #include + #include +-#include + #include + #include + #include +@@ -21,8 +20,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" + +@@ -34,6 +33,7 @@ struct Monitor { + bool desiredVisibility {true}; + bool hasData; + uint32_t tags; ++ wl_unique_ptr dwlMonitor; + }; + + struct SeatPointer { +@@ -54,8 +54,6 @@ static void updatemon(Monitor &mon); + static void onReady(); + static void setupStatusFifo(); + static void onStatus(); +-static void onStdin(); +-static void handleStdin(const std::string& line); + static void updateVisibility(const std::string& name, bool(*updater)(bool)); + static void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version); + static void onGlobalRemove(void*, wl_registry* registry, uint32_t name); +@@ -67,6 +65,9 @@ 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; +@@ -85,6 +86,26 @@ 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(), ~0, arg.ui); ++} + void spawn(Monitor&, const Arg& arg) + { + if (fork() == 0) { +@@ -189,11 +210,62 @@ 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); ++ int tagState = TagState::None; ++ if (state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE) ++ tagState |= TagState::Active; ++ if (state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_URGENT) ++ tagState |= TagState::Urgent; ++ mon->bar.setTag(tag, tagState, numClients, focusedClient); ++ uint32_t mask = 1 << tag; ++ if (tagState & TagState::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(layoutNames[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); ++ } ++}; ++ + void setupMonitor(uint32_t name, wl_output* output) { + auto& monitor = monitors.emplace_back(Monitor {name, {}, wl_unique_ptr {output}}); + 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); ++ monitor.dwlMonitor.reset(znet_tapesoftware_dwl_wm_v1_get_monitor(dwlWm, monitor.wlOutput.get())); ++ znet_tapesoftware_dwl_wm_monitor_v1_add_listener(monitor.dwlMonitor.get(), &dwlWmMonitorListener, &monitor); + } + + void updatemon(Monitor& mon) +@@ -219,6 +291,7 @@ 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. + +@@ -226,7 +299,6 @@ void onReady() + for (auto output : uninitializedOutputs) { + setupMonitor(output.first, output.second); + } +- wl_display_roundtrip(display); // wait for xdg_output names before we read stdin + } + + bool createFifo(std::string path) +@@ -273,68 +345,6 @@ void setupStatusFifo() + } + } + +-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 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 {}; +- stream >> std::ws; +- std::getline(stream, title); +- mon->bar.setTitle(title); +- } else if (command == "selmon") { +- uint32_t selected; +- stream >> selected; +- mon->bar.setSelected(selected); +- if (selected) { +- selmon = &*mon; +- } else if (selmon == &*mon) { +- selmon = nullptr; +- } +- } 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 & tagMask ? 0 : -1); +- } +- mon->tags = tags; +- } else if (command == "layout") { +- auto layout = std::string {}; +- stream >> std::ws; +- std::getline(stream, layout); +- mon->bar.setLayout(layout); +- } +- mon->hasData = true; +- updatemon(*mon); +-} +- + const std::string prefixStatus = "status "; + const std::string prefixShow = "show "; + const std::string prefixHide = "hide "; +@@ -409,6 +419,10 @@ void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interf + 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); +@@ -522,10 +536,6 @@ int main(int argc, char* argv[]) + .fd = displayFd, + .events = POLLIN, + }); +- pollfds.push_back({ +- .fd = STDIN_FILENO, +- .events = POLLIN, +- }); + if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) { + diesys("fcntl F_SETFL"); + } +@@ -550,8 +560,6 @@ int main(int argc, char* argv[]) + ev.events = POLLIN; + waylandFlush(); + } +- } else if (ev.fd == STDIN_FILENO && (ev.revents & POLLIN)) { +- onStdin(); + } else if (ev.fd == statusFifoFd && (ev.revents & POLLIN)) { + onStatus(); + } else if (ev.fd == signalSelfPipe[0] && (ev.revents & POLLIN)) { diff --git a/somebar/contrib/markup-in-status-messages.patch b/somebar/contrib/markup-in-status-messages.patch new file mode 100644 index 0000000..ab7e998 --- /dev/null +++ b/somebar/contrib/markup-in-status-messages.patch @@ -0,0 +1,65 @@ +From: Ben Collerson +Date: Tue, 29 Nov 2022 11:29:15 +1000 +Subject: [PATCH] markup in status messages +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Allows pango markup to be used in status messages which allow colours to +be used to highlight parts of status bar messages. eg: + +battery: full 🔇0 Tue 11-20 20:10 +--- + src/bar.cpp | 16 +++++++++++++++- + src/bar.hpp | 1 + + 2 files changed, 16 insertions(+), 1 deletion(-) + +diff --git a/src/bar.cpp b/src/bar.cpp +index 507ce62..a96c547 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -81,6 +81,11 @@ void BarComponent::setText(const std::string& text) + pango_layout_set_text(pangoLayout.get(), _text->c_str(), _text->size()); + } + ++void BarComponent::setAttributes(PangoAttrList *attrs) ++{ ++ pango_layout_set_attributes(pangoLayout.get(), attrs); ++} ++ + Bar::Bar() + { + _pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default())); +@@ -156,7 +161,16 @@ void Bar::setTitle(const std::string& title) + } + void Bar::setStatus(const std::string& status) + { +- _statusCmp.setText(status); ++ char *buf; ++ GError *error = NULL; ++ PangoAttrList *attrs; ++ if (pango_parse_markup(status.c_str(), -1, 0, &attrs, &buf, NULL, &error)) { ++ _statusCmp.setText(buf); ++ _statusCmp.setAttributes(attrs); ++ } ++ else { ++ _statusCmp.setText(error->message); ++ } + } + + void Bar::invalidate() +diff --git a/src/bar.hpp b/src/bar.hpp +index 176a1bc..dfc2043 100644 +--- a/src/bar.hpp ++++ b/src/bar.hpp +@@ -17,6 +17,7 @@ public: + explicit BarComponent(wl_unique_ptr layout); + int width() const; + void setText(const std::string& text); ++ void setAttributes(PangoAttrList *attrs); + wl_unique_ptr pangoLayout; + int x {0}; + }; +-- +2.38.1 + diff --git a/somebar/contrib/show-status-on-selected-monitor.patch b/somebar/contrib/show-status-on-selected-monitor.patch new file mode 100644 index 0000000..ab26835 --- /dev/null +++ b/somebar/contrib/show-status-on-selected-monitor.patch @@ -0,0 +1,43 @@ +From 1ca4603b79e888980fb46d5dc6aa0d6f8afa2b3c Mon Sep 17 00:00:00 2001 +From: Ben Collerson +Date: Mon, 28 Nov 2022 13:22:16 +1000 +Subject: [PATCH] show status on selected monitor + +--- + src/bar.cpp | 7 ++++++- + src/main.cpp | 1 + + 2 files changed, 7 insertions(+), 1 deletion(-) + +diff --git a/src/bar.cpp b/src/bar.cpp +index 507ce62..f962927 100644 +--- a/src/bar.cpp ++++ b/src/bar.cpp +@@ -156,7 +156,12 @@ void Bar::setTitle(const std::string& title) + } + void Bar::setStatus(const std::string& status) + { +- _statusCmp.setText(status); ++ if (_selected) { ++ _statusCmp.setText(status); ++ } ++ else { ++ _statusCmp.setText(""); ++ } + } + + void Bar::invalidate() +diff --git a/src/main.cpp b/src/main.cpp +index 6274959..c6fd401 100644 +--- a/src/main.cpp ++++ b/src/main.cpp +@@ -307,6 +307,7 @@ static void handleStdin(const std::string& line) + uint32_t selected; + stream >> selected; + mon->bar.setSelected(selected); ++ mon->bar.setStatus(lastStatus); + if (selected) { + selmon = &*mon; + } else if (selmon == &*mon) { +-- +2.38.1 + diff --git a/somebar/meson.build b/somebar/meson.build new file mode 100644 index 0000000..6ad5a0f --- /dev/null +++ b/somebar/meson.build @@ -0,0 +1,31 @@ +project('somebar', ['c', 'cpp'], + version: '0.1.0', + default_options: [ + 'cpp_std=c++17', + 'cpp_args=-Wno-parentheses', + ]) + +wayland_dep = dependency('wayland-client') +wayland_cursor_dep = dependency('wayland-cursor') +cairo_dep = dependency('cairo') +pango_dep = dependency('pango') +pangocairo_dep = dependency('pangocairo') + +subdir('protocols') + +executable('somebar', + 'src/main.cpp', + 'src/shm_buffer.cpp', + 'src/bar.cpp', + wayland_sources, + dependencies: [ + wayland_dep, + wayland_cursor_dep, + cairo_dep, + pango_dep, + pangocairo_dep, + ], + install: true, + cpp_args: '-DSOMEBAR_VERSION="@0@"'.format(meson.project_version())) + +install_man('somebar.1') diff --git a/somebar/protocols/meson.build b/somebar/protocols/meson.build new file mode 100644 index 0000000..7bd222b --- /dev/null +++ b/somebar/protocols/meson.build @@ -0,0 +1,22 @@ +# adapted from https://github.com/swaywm/swayidle/blob/0467c1e03a5780ed8e3ba611f099a838822ab550/meson.build +wayland_scanner = find_program('wayland-scanner') +wayland_protos_dep = dependency('wayland-protocols') +wl_protocol_dir = wayland_protos_dep.get_pkgconfig_variable('pkgdatadir') +wayland_scanner_code = generator( + wayland_scanner, + output: '@BASENAME@-protocol.c', + arguments: ['private-code', '@INPUT@', '@OUTPUT@']) +wayland_scanner_client = generator( + wayland_scanner, + output: '@BASENAME@-client-protocol.h', + arguments: ['client-header', '@INPUT@', '@OUTPUT@']) + +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', +] +wayland_sources = [ + wayland_scanner_code.process(wayland_xmls), + wayland_scanner_client.process(wayland_xmls), +] diff --git a/somebar/protocols/wlr-layer-shell-unstable-v1.xml b/somebar/protocols/wlr-layer-shell-unstable-v1.xml new file mode 100644 index 0000000..78ba050 --- /dev/null +++ b/somebar/protocols/wlr-layer-shell-unstable-v1.xml @@ -0,0 +1,390 @@ + + + + Copyright © 2017 Drew DeVault + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + + + + + Clients can use this interface to assign the surface_layer role to + wl_surfaces. Such surfaces are assigned to a "layer" of the output and + rendered with a defined z-depth respective to each other. They may also be + anchored to the edges and corners of a screen and specify input handling + semantics. This interface should be suitable for the implementation of + many desktop shell components, and a broad number of other applications + that interact with the desktop. + + + + + Create a layer surface for an existing surface. This assigns the role of + layer_surface, or raises a protocol error if another role is already + assigned. + + Creating a layer surface from a wl_surface which has a buffer attached + or committed is a client error, and any attempts by a client to attach + or manipulate a buffer prior to the first layer_surface.configure call + must also be treated as errors. + + After creating a layer_surface object and setting it up, the client + must perform an initial commit without any buffer attached. + The compositor will reply with a layer_surface.configure event. + The client must acknowledge it and is then allowed to attach a buffer + to map the surface. + + You may pass NULL for output to allow the compositor to decide which + output to use. Generally this will be the one that the user most + recently interacted with. + + Clients can specify a namespace that defines the purpose of the layer + surface. + + + + + + + + + + + + + + + + + These values indicate which layers a surface can be rendered in. They + are ordered by z depth, bottom-most first. Traditional shell surfaces + will typically be rendered between the bottom and top layers. + Fullscreen shell surfaces are typically rendered at the top layer. + Multiple surfaces can share a single layer, and ordering within a + single layer is undefined. + + + + + + + + + + + + + This request indicates that the client will not use the layer_shell + object any more. Objects that have been created through this instance + are not affected. + + + + + + + An interface that may be implemented by a wl_surface, for surfaces that + are designed to be rendered as a layer of a stacked desktop-like + environment. + + Layer surface state (layer, size, anchor, exclusive zone, + margin, interactivity) is double-buffered, and will be applied at the + time wl_surface.commit of the corresponding wl_surface is called. + + Attaching a null buffer to a layer surface unmaps it. + + Unmapping a layer_surface means that the surface cannot be shown by the + compositor until it is explicitly mapped again. The layer_surface + returns to the state it had right after layer_shell.get_layer_surface. + The client can re-map the surface by performing a commit without any + buffer attached, waiting for a configure event and handling it as usual. + + + + + Sets the size of the surface in surface-local coordinates. The + compositor will display the surface centered with respect to its + anchors. + + If you pass 0 for either value, the compositor will assign it and + inform you of the assignment in the configure event. You must set your + anchor to opposite edges in the dimensions you omit; not doing so is a + protocol error. Both values are 0 by default. + + Size is double-buffered, see wl_surface.commit. + + + + + + + + Requests that the compositor anchor the surface to the specified edges + and corners. If two orthogonal edges are specified (e.g. 'top' and + 'left'), then the anchor point will be the intersection of the edges + (e.g. the top left corner of the output); otherwise the anchor point + will be centered on that edge, or in the center if none is specified. + + Anchor is double-buffered, see wl_surface.commit. + + + + + + + Requests that the compositor avoids occluding an area with other + surfaces. The compositor's use of this information is + implementation-dependent - do not assume that this region will not + actually be occluded. + + A positive value is only meaningful if the surface is anchored to one + edge or an edge and both perpendicular edges. If the surface is not + anchored, anchored to only two perpendicular edges (a corner), anchored + to only two parallel edges or anchored to all edges, a positive value + will be treated the same as zero. + + A positive zone is the distance from the edge in surface-local + coordinates to consider exclusive. + + Surfaces that do not wish to have an exclusive zone may instead specify + how they should interact with surfaces that do. If set to zero, the + surface indicates that it would like to be moved to avoid occluding + surfaces with a positive exclusive zone. If set to -1, the surface + indicates that it would not like to be moved to accommodate for other + surfaces, and the compositor should extend it all the way to the edges + it is anchored to. + + For example, a panel might set its exclusive zone to 10, so that + maximized shell surfaces are not shown on top of it. A notification + might set its exclusive zone to 0, so that it is moved to avoid + occluding the panel, but shell surfaces are shown underneath it. A + wallpaper or lock screen might set their exclusive zone to -1, so that + they stretch below or over the panel. + + The default value is 0. + + Exclusive zone is double-buffered, see wl_surface.commit. + + + + + + + Requests that the surface be placed some distance away from the anchor + point on the output, in surface-local coordinates. Setting this value + for edges you are not anchored to has no effect. + + The exclusive zone includes the margin. + + Margin is double-buffered, see wl_surface.commit. + + + + + + + + + + Types of keyboard interaction possible for layer shell surfaces. The + rationale for this is twofold: (1) some applications are not interested + in keyboard events and not allowing them to be focused can improve the + desktop experience; (2) some applications will want to take exclusive + keyboard focus. + + + + + This value indicates that this surface is not interested in keyboard + events and the compositor should never assign it the keyboard focus. + + This is the default value, set for newly created layer shell surfaces. + + This is useful for e.g. desktop widgets that display information or + only have interaction with non-keyboard input devices. + + + + + Request exclusive keyboard focus if this surface is above the shell surface layer. + + For the top and overlay layers, the seat will always give + exclusive keyboard focus to the top-most layer which has keyboard + interactivity set to exclusive. If this layer contains multiple + surfaces with keyboard interactivity set to exclusive, the compositor + determines the one receiving keyboard events in an implementation- + defined manner. In this case, no guarantee is made when this surface + will receive keyboard focus (if ever). + + For the bottom and background layers, the compositor is allowed to use + normal focus semantics. + + This setting is mainly intended for applications that need to ensure + they receive all keyboard events, such as a lock screen or a password + prompt. + + + + + This requests the compositor to allow this surface to be focused and + unfocused by the user in an implementation-defined manner. The user + should be able to unfocus this surface even regardless of the layer + it is on. + + Typically, the compositor will want to use its normal mechanism to + manage keyboard focus between layer shell surfaces with this setting + and regular toplevels on the desktop layer (e.g. click to focus). + Nevertheless, it is possible for a compositor to require a special + interaction to focus or unfocus layer shell surfaces (e.g. requiring + a click even if focus follows the mouse normally, or providing a + keybinding to switch focus between layers). + + This setting is mainly intended for desktop shell components (e.g. + panels) that allow keyboard interaction. Using this option can allow + implementing a desktop shell that can be fully usable without the + mouse. + + + + + + + Set how keyboard events are delivered to this surface. By default, + layer shell surfaces do not receive keyboard events; this request can + be used to change this. + + This setting is inherited by child surfaces set by the get_popup + request. + + Layer surfaces receive pointer, touch, and tablet events normally. If + you do not want to receive them, set the input region on your surface + to an empty region. + + Keyboard interactivity is double-buffered, see wl_surface.commit. + + + + + + + This assigns an xdg_popup's parent to this layer_surface. This popup + should have been created via xdg_surface::get_popup with the parent set + to NULL, and this request must be invoked before committing the popup's + initial state. + + See the documentation of xdg_popup for more details about what an + xdg_popup is and how it is used. + + + + + + + When a configure event is received, if a client commits the + surface in response to the configure event, then the client + must make an ack_configure request sometime before the commit + request, passing along the serial of the configure event. + + If the client receives multiple configure events before it + can respond to one, it only has to ack the last configure event. + + A client is not required to commit immediately after sending + an ack_configure request - it may even ack_configure several times + before its next surface commit. + + A client may send multiple ack_configure requests before committing, but + only the last request sent before a commit indicates which configure + event the client really is responding to. + + + + + + + This request destroys the layer surface. + + + + + + The configure event asks the client to resize its surface. + + Clients should arrange their surface for the new states, and then send + an ack_configure request with the serial sent in this configure event at + some point before committing the new surface. + + The client is free to dismiss all but the last configure event it + received. + + The width and height arguments specify the size of the window in + surface-local coordinates. + + The size is a hint, in the sense that the client is free to ignore it if + it doesn't resize, pick a smaller size (to satisfy aspect ratio or + resize in steps of NxM pixels). If the client picks a smaller size and + is anchored to two opposite anchors (e.g. 'top' and 'bottom'), the + surface will be centered on this axis. + + If the width or height arguments are zero, it means the client should + decide its own window dimension. + + + + + + + + + The closed event is sent by the compositor when the surface will no + longer be shown. The output may have been destroyed or the user may + have asked for it to be removed. Further changes to the surface will be + ignored. The client should destroy the resource after receiving this + event, and create a new surface if they so choose. + + + + + + + + + + + + + + + + + + + + + + Change the layer that the surface is rendered on. + + Layer is double-buffered, see wl_surface.commit. + + + + + diff --git a/somebar/screenshot.png b/somebar/screenshot.png new file mode 100644 index 0000000..7a800fb Binary files /dev/null and b/somebar/screenshot.png differ diff --git a/somebar/somebar.1 b/somebar/somebar.1 new file mode 100644 index 0000000..d822770 --- /dev/null +++ b/somebar/somebar.1 @@ -0,0 +1,55 @@ +.TH somebar 1 somebar\-1.0 +.SH NAME +somebar \- dwm-like bar for dwl +.SH SYNOPSIS +.B somebar +.RB [ \-h ] +.RB [ \-v ] +.RB [ \-s +.IR path ] +.RB [ \-c +.IR command +arguments... ] +.SH DESCRIPTION +somebar is a status bar for dwl, visually and functionally resembling the +dwm bar. +.SH USAGE +You must start somebar using dwl's `-s` flag, e.g. `dwl -s somebar`. + +Somebar can be controlled by writing to $XDG_RUNTIME_DIR/somebar-0, or the path +defined by the `-s` argument. The following commands are supported: +.TP +.B status TEXT +Updates the status bar +.TP +.B hide MONITOR +Hides somebar on the specified monitor +.TP +.B show MONITOR +Shows somebar on the specified monitor +.TP +.B toggle MONITOR +Toggles somebar on the specified monitor +.P +MONITOR is an zxdg_output_v1 name, which can be determined e.g. using `weston-info`. +Additionally, MONITOR can be `all` (all monitors) or `selected` (the monitor with focus). + +Commands can be sent either by writing to the file name above, or equivalently by calling +somebar with the `-c` argument. For example: `somebar -c toggle all`. This is recommended +for shell scripts, as there is no race-free way to write to a file only if it exists. +.SH OPTIONS +.TP +.B \-h +Displays a short help text and exits +.TP +.B \-v +Displays version information and exits +.TP +.B \-s +Sets the path to the somebar control FIFO. The default value is +$XDG_RUNTIME_DIR/somebar-0 +.TP +.B \-c +Sends a command to the control FIFO. See the USAGE section. +.SH BUGS +Send bug reports to ~raphi/public-inbox@lists.sr.ht diff --git a/somebar/src/bar.cpp b/somebar/src/bar.cpp new file mode 100644 index 0000000..af92f49 --- /dev/null +++ b/somebar/src/bar.cpp @@ -0,0 +1,315 @@ +// somebar - dwl barbar +// See LICENSE file for copyright and license details. + +#include +#include +#include "bar.hpp" +#include "cairo.h" +#include "config.hpp" +#include "pango/pango-font.h" +#include "pango/pango-fontmap.h" +#include "pango/pango-layout.h" + +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); + } +}; +const wl_callback_listener Bar::_frameListener = { + [](void* owner, wl_callback* cb, uint32_t) + { + static_cast(owner)->render(); + wl_callback_destroy(cb); + } +}; + +struct Font { + PangoFontDescription* description; + int height {0}; +}; +static Font getFont() +{ + auto fontMap = pango_cairo_font_map_get_default(); + if (!fontMap) { + die("pango_cairo_font_map_get_default"); + } + auto fontDesc = pango_font_description_from_string(font); + if (!fontDesc) { + die("pango_font_description_from_string"); + } + auto tempContext = pango_font_map_create_context(fontMap); + if (!tempContext) { + die("pango_font_map_create_context"); + } + auto font = pango_font_map_load_font(fontMap, tempContext, fontDesc); + if (!font) { + die("pango_font_map_load_font"); + } + auto metrics = pango_font_get_metrics(font, pango_language_get_default()); + if (!metrics) { + die("pango_font_get_metrics"); + } + + auto res = Font {}; + res.description = fontDesc; + res.height = PANGO_PIXELS(pango_font_metrics_get_height(metrics)); + + pango_font_metrics_unref(metrics); + g_object_unref(font); + g_object_unref(tempContext); + return res; +} +static Font barfont = getFont(); + +BarComponent::BarComponent() { } +BarComponent::BarComponent(wl_unique_ptr layout) + : pangoLayout {std::move(layout)} +{ +} + +int BarComponent::width() const +{ + int w, h; + pango_layout_get_size(pangoLayout.get(), &w, &h); + return PANGO_PIXELS(w); +} + +void BarComponent::setText(const std::string& text) +{ + _text = std::make_unique(text); + pango_layout_set_text(pangoLayout.get(), _text->c_str(), _text->size()); +} + +Bar::Bar() +{ + _pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default())); + if (!_pangoContext) { + die("pango_font_map_create_context"); + } + for (const auto& tagName : tagNames) { + _tags.push_back({ TagState::None, 0, 0, createComponent(tagName) }); + } + _layoutCmp = createComponent(); + _titleCmp = createComponent(); + _statusCmp = createComponent(); +} + +const wl_surface* Bar::surface() const +{ + return _surface.get(); +} + +bool Bar::visible() const +{ + return _surface.get(); +} + +void Bar::show(wl_output* output) +{ + if (visible()) { + return; + } + _surface.reset(wl_compositor_create_surface(compositor)); + _layerSurface.reset(zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell, + _surface.get(), output, ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "net.tapesoftware.Somebar")); + zwlr_layer_surface_v1_add_listener(_layerSurface.get(), &_layerSurfaceListener, this); + auto anchor = topbar ? ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP : ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + zwlr_layer_surface_v1_set_anchor(_layerSurface.get(), + anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); + + auto barSize = barfont.height + paddingY * 2; + zwlr_layer_surface_v1_set_size(_layerSurface.get(), 0, barSize); + zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface.get(), barSize); + wl_surface_commit(_surface.get()); +} + +void Bar::hide() +{ + if (!visible()) { + return; + } + _layerSurface.reset(); + _surface.reset(); + _bufs.reset(); +} + +void Bar::setTag(int tag, int state, int numClients, int focusedClient) +{ + auto& t = _tags[tag]; + t.state = state; + t.numClients = numClients; + t.focusedClient = focusedClient; +} + +void Bar::setSelected(bool selected) +{ + _selected = selected; +} +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); +} + +void Bar::invalidate() +{ + if (_invalid || !visible()) { + return; + } + _invalid = true; + auto frame = wl_surface_frame(_surface.get()); + wl_callback_add_listener(frame, &_frameListener, this); + wl_surface_commit(_surface.get()); +} + +void Bar::click(Monitor* mon, int x, int, int btn) +{ + Arg arg = {0}; + Arg* argp = nullptr; + int control = ClkNone; + if (x > _statusCmp.x) { + control = ClkStatusText; + } else if (x > _titleCmp.x) { + control = ClkWinTitle; + } else if (x > _layoutCmp.x) { + control = ClkLayoutSymbol; + } else for (int tag = _tags.size()-1; tag >= 0; tag--) { + if (x > _tags[tag].component.x) { + control = ClkTagBar; + arg.ui = 1<width && height == _bufs->height) { + return; + } + _bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888); + render(); +} + +void Bar::render() +{ + if (!_bufs) { + return; + } + auto img = wl_unique_ptr {cairo_image_surface_create_for_data( + _bufs->data(), + CAIRO_FORMAT_ARGB32, + _bufs->width, + _bufs->height, + _bufs->stride + )}; + auto painter = wl_unique_ptr {cairo_create(img.get())}; + _painter = painter.get(); + pango_cairo_update_context(_painter, _pangoContext.get()); + _x = 0; + + renderTags(); + setColorScheme(_selected ? colorActive : colorInactive); + renderComponent(_layoutCmp); + renderComponent(_titleCmp); + renderStatus(); + + _painter = nullptr; + wl_surface_attach(_surface.get(), _bufs->buffer(), 0, 0); + wl_surface_damage(_surface.get(), 0, 0, _bufs->width, _bufs->height); + wl_surface_commit(_surface.get()); + _bufs->flip(); + _invalid = false; +} + +void Bar::renderTags() +{ + for (auto &tag : _tags) { + setColorScheme( + tag.state & TagState::Active ? colorActive : colorInactive, + tag.state & TagState::Urgent); + renderComponent(tag.component); + auto indicators = std::min(tag.numClients, static_cast(_bufs->height/2)); + for (auto ind = 0; ind < indicators; ind++) { + auto w = ind == tag.focusedClient ? 7 : 1; + cairo_move_to(_painter, tag.component.x, ind*2+0.5); + cairo_rel_line_to(_painter, w, 0); + cairo_close_path(_painter); + cairo_set_line_width(_painter, 1); + cairo_stroke(_painter); + } + } +} + +void Bar::renderStatus() +{ + pango_cairo_update_layout(_painter, _statusCmp.pangoLayout.get()); + beginBg(); + auto start = _bufs->width - _statusCmp.width() - paddingX*2; + cairo_rectangle(_painter, _x, 0, _bufs->width-_x+start, _bufs->height); + cairo_fill(_painter); + + _x = start; + renderComponent(_statusCmp); +} + +void Bar::setColorScheme(const ColorScheme& scheme, bool invert) +{ + _colorScheme = invert + ? ColorScheme {scheme.bg, scheme.fg} + : ColorScheme {scheme.fg, scheme.bg}; +} +static void setColor(cairo_t* painter, const Color& color) +{ + cairo_set_source_rgba(painter, + color.r/255.0, color.g/255.0, color.b/255.0, color.a/255.0); +} +void Bar::beginFg() +{ + setColor(_painter, _colorScheme.fg); +} +void Bar::beginBg() +{ + setColor(_painter, _colorScheme.bg); +} + +void Bar::renderComponent(BarComponent& component) +{ + pango_cairo_update_layout(_painter, component.pangoLayout.get()); + auto size = component.width() + paddingX*2; + component.x = _x; + + beginBg(); + cairo_rectangle(_painter, _x, 0, size, _bufs->height); + cairo_fill(_painter); + cairo_move_to(_painter, _x+paddingX, paddingY); + + beginFg(); + pango_cairo_show_layout(_painter, component.pangoLayout.get()); + _x += size; +} + +BarComponent Bar::createComponent(const std::string &initial) +{ + auto layout = pango_layout_new(_pangoContext.get()); + pango_layout_set_font_description(layout, barfont.description); + auto res = BarComponent {wl_unique_ptr {layout}}; + res.setText(initial); + return res; +} diff --git a/somebar/src/bar.hpp b/somebar/src/bar.hpp new file mode 100644 index 0000000..176a1bc --- /dev/null +++ b/somebar/src/bar.hpp @@ -0,0 +1,74 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include +#include +#include +#include +#include "wlr-layer-shell-unstable-v1-client-protocol.h" +#include "common.hpp" +#include "shm_buffer.hpp" + +class BarComponent { + std::unique_ptr _text; +public: + BarComponent(); + explicit BarComponent(wl_unique_ptr layout); + int width() const; + void setText(const std::string& text); + wl_unique_ptr pangoLayout; + int x {0}; +}; + +struct Tag { + int state; + int numClients; + int focusedClient; + BarComponent component; +}; + +struct Monitor; +class Bar { + static const zwlr_layer_surface_v1_listener _layerSurfaceListener; + static const wl_callback_listener _frameListener; + + wl_unique_ptr _surface; + wl_unique_ptr _layerSurface; + wl_unique_ptr _pangoContext; + std::optional _bufs; + std::vector _tags; + BarComponent _layoutCmp, _titleCmp, _statusCmp; + bool _selected; + bool _invalid {false}; + + // only vaild during render() + cairo_t* _painter {nullptr}; + int _x; + ColorScheme _colorScheme; + + void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); + void render(); + void renderTags(); + void renderStatus(); + + // low-level rendering + void setColorScheme(const ColorScheme& scheme, bool invert = false); + void beginFg(); + void beginBg(); + void renderComponent(BarComponent& component); + BarComponent createComponent(const std::string& initial = {}); +public: + Bar(); + const wl_surface* surface() const; + bool visible() const; + void show(wl_output* output); + void hide(); + void setTag(int tag, int state, int numClients, int focusedClient); + void setSelected(bool selected); + void setLayout(const std::string& layout); + void setTitle(const std::string& title); + void setStatus(const std::string& status); + void invalidate(); + void click(Monitor* mon, int x, int y, int btn); +}; diff --git a/somebar/src/common.hpp b/somebar/src/common.hpp new file mode 100644 index 0000000..c905358 --- /dev/null +++ b/somebar/src/common.hpp @@ -0,0 +1,76 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +struct Color { + Color() {} + constexpr Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255) : r(r), g(g), b(b), a(a) { } + uint8_t r, g, b, a {255}; +}; +struct ColorScheme { + Color fg, bg; +}; +union Arg { + unsigned int ui; + const void* v; +}; +struct Monitor; + +enum TagState { None, Active = 0x01, Urgent = 0x02 }; +enum Control { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText }; +struct Button { + int control; + int btn; // + void (*func)(Monitor& mon, const Arg& arg); + const Arg arg; +}; + +extern wl_display* display; +extern wl_compositor* compositor; +extern wl_shm* shm; +extern zwlr_layer_shell_v1* wlrLayerShell; + +void spawn(Monitor&, const Arg& arg); +void setCloexec(int fd); +[[noreturn]] void die(const char* why); +[[noreturn]] void diesys(const char* why); + +// wayland smart pointers +template +struct WlDeleter; +#define WL_DELETER(type, fn) template<> struct WlDeleter { \ + void operator()(type* v) { if(v) fn(v); } \ + } + +template +using wl_unique_ptr = std::unique_ptr>; + +inline void wl_output_release_checked(wl_output* output) { + if (wl_output_get_version(output) >= WL_OUTPUT_RELEASE_SINCE_VERSION) { + wl_output_release(output); + } +} + +WL_DELETER(wl_buffer, wl_buffer_destroy); +WL_DELETER(wl_output, wl_output_release_checked); +WL_DELETER(wl_pointer, wl_pointer_release); +WL_DELETER(wl_seat, wl_seat_release); +WL_DELETER(wl_surface, wl_surface_destroy); +WL_DELETER(zwlr_layer_surface_v1, zwlr_layer_surface_v1_destroy); + +WL_DELETER(cairo_t, cairo_destroy); +WL_DELETER(cairo_surface_t, cairo_surface_destroy); + +WL_DELETER(PangoContext, g_object_unref); +WL_DELETER(PangoLayout, g_object_unref); + +#undef WL_DELETER diff --git a/somebar/src/config.def.hpp b/somebar/src/config.def.hpp new file mode 100644 index 0000000..40a8c95 --- /dev/null +++ b/somebar/src/config.def.hpp @@ -0,0 +1,27 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include "common.hpp" + +constexpr bool topbar = true; + +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 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}; + +static std::vector tagNames = { + "1", "2", "3", + "4", "5", "6", + "7", "8", "9", +}; + +constexpr Button buttons[] = { + { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, +}; diff --git a/somebar/src/config.hpp b/somebar/src/config.hpp new file mode 100644 index 0000000..40a8c95 --- /dev/null +++ b/somebar/src/config.hpp @@ -0,0 +1,27 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include "common.hpp" + +constexpr bool topbar = true; + +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 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}; + +static std::vector tagNames = { + "1", "2", "3", + "4", "5", "6", + "7", "8", "9", +}; + +constexpr Button buttons[] = { + { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, +}; diff --git a/somebar/src/line_buffer.hpp b/somebar/src/line_buffer.hpp new file mode 100644 index 0000000..a5497bf --- /dev/null +++ b/somebar/src/line_buffer.hpp @@ -0,0 +1,71 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include +#include +#include + +// reads data from Reader, and passes complete lines to Consumer. +template +class LineBuffer { + using Iterator = typename std::array::iterator; + std::array _buffer; + Iterator _bufferedTo; + Iterator _consumedTo; + bool _discardLine {false}; +public: + LineBuffer() + : _bufferedTo {_buffer.begin()} + , _consumedTo {_buffer.begin()} + { + } + + template + ssize_t readLines(const Reader& reader, const Consumer& consumer) + { + while (true) { + auto bytesRead = reader(_bufferedTo, _buffer.end() - _bufferedTo); + if (bytesRead <= 0) { + return bytesRead; + } + _bufferedTo += bytesRead; + dispatchLines(consumer); + resetBuffer(); + } + } +private: + template + void dispatchLines(const Consumer& consumer) + { + while (true) { + auto separator = std::find(_consumedTo, _bufferedTo, '\n'); + if (separator == _bufferedTo) { + break; + } + size_t lineLength = separator - _consumedTo; + if (!_discardLine) { + consumer(_consumedTo, lineLength); + } + _consumedTo = separator + 1; + _discardLine = false; + } + } + + void resetBuffer() + { + size_t bytesRemaining = _bufferedTo - _consumedTo; + if (bytesRemaining == _buffer.size()) { + // line too long + _discardLine = true; + _consumedTo = _buffer.begin(); + _bufferedTo = _buffer.begin(); + } else { + // move the last partial message to the front of the buffer, so a full-sized + // message will fit + std::copy(_consumedTo, _bufferedTo, _buffer.begin()); + _consumedTo = _buffer.begin(); + _bufferedTo = _consumedTo + bytesRemaining; + } + } +}; diff --git a/somebar/src/main.cpp b/somebar/src/main.cpp new file mode 100644 index 0000000..15a749a --- /dev/null +++ b/somebar/src/main.cpp @@ -0,0 +1,613 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr-layer-shell-unstable-v1-client-protocol.h" +#include "xdg-output-unstable-v1-client-protocol.h" +#include "xdg-shell-client-protocol.h" +#include "common.hpp" +#include "config.hpp" +#include "bar.hpp" +#include "line_buffer.hpp" + +struct Monitor { + uint32_t registryName; + std::string xdgName; + wl_unique_ptr wlOutput; + Bar bar; + bool desiredVisibility {true}; + bool hasData; + uint32_t tags; +}; + +struct SeatPointer { + wl_unique_ptr wlPointer; + Monitor* focusedMonitor; + int x, y; + std::vector btns; +}; +struct Seat { + uint32_t name; + wl_unique_ptr wlSeat; + std::optional pointer; +}; + +static Monitor* monitorFromSurface(const wl_surface* surface); +static void setupMonitor(uint32_t name, wl_output* output); +static void updatemon(Monitor &mon); +static void onReady(); +static void setupStatusFifo(); +static void onStatus(); +static void onStdin(); +static void handleStdin(const std::string& line); +static void updateVisibility(const std::string& name, bool(*updater)(bool)); +static void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version); +static void onGlobalRemove(void*, wl_registry* registry, uint32_t name); +static void requireGlobal(const void* p, const char* name); +static void waylandFlush(); +static void cleanup(); + +wl_display* display; +wl_compositor* compositor; +wl_shm* shm; +zwlr_layer_shell_v1* wlrLayerShell; +static xdg_wm_base* xdgWmBase; +static zxdg_output_manager_v1* xdgOutputManager; +static wl_surface* cursorSurface; +static wl_cursor_image* cursorImage; +static bool ready; +static std::list monitors; +static std::vector> uninitializedOutputs; +static std::list seats; +static Monitor* selmon; +static std::string lastStatus; +static std::string statusFifoName; +static std::vector pollfds; +static std::array signalSelfPipe; +static int displayFd {-1}; +static int statusFifoFd {-1}; +static int statusFifoWriter {-1}; +static bool quitting {false}; + +void spawn(Monitor&, const Arg& arg) +{ + if (fork() == 0) { + auto argv = static_cast(arg.v); + setsid(); + execvp(argv[0], argv); + fprintf(stderr, "somebar: execvp %s ", argv[0]); + perror(" failed"); + exit(1); + } +} + +static const struct xdg_wm_base_listener xdgWmBaseListener = { + [](void*, xdg_wm_base* sender, uint32_t serial) { + xdg_wm_base_pong(sender, serial); + } +}; + +static const struct zxdg_output_v1_listener xdgOutputListener = { + .logical_position = [](void*, zxdg_output_v1*, int, int) { }, + .logical_size = [](void*, zxdg_output_v1*, int, int) { }, + .done = [](void*, zxdg_output_v1*) { }, + .name = [](void* mp, zxdg_output_v1* xdgOutput, const char* name) { + auto& monitor = *static_cast(mp); + monitor.xdgName = name; + zxdg_output_v1_destroy(xdgOutput); + }, + .description = [](void*, zxdg_output_v1*, const char*) { }, +}; + +Monitor* monitorFromSurface(const wl_surface* surface) +{ + auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor& mon) { + return mon.bar.surface() == surface; + }); + return mon != end(monitors) ? &*mon : nullptr; +} +static const struct wl_pointer_listener pointerListener = { + .enter = [](void* sp, wl_pointer* pointer, uint32_t serial, + wl_surface* surface, wl_fixed_t x, wl_fixed_t y) + { + auto& seat = *static_cast(sp); + seat.pointer->focusedMonitor = monitorFromSurface(surface); + if (!cursorImage) { + auto cursorTheme = wl_cursor_theme_load(nullptr, 24, shm); + cursorImage = wl_cursor_theme_get_cursor(cursorTheme, "left_ptr")->images[0]; + cursorSurface = wl_compositor_create_surface(compositor); + wl_surface_attach(cursorSurface, wl_cursor_image_get_buffer(cursorImage), 0, 0); + wl_surface_commit(cursorSurface); + } + wl_pointer_set_cursor(pointer, serial, cursorSurface, + cursorImage->hotspot_x, cursorImage->hotspot_y); + }, + .leave = [](void* sp, wl_pointer*, uint32_t serial, wl_surface*) { + auto& seat = *static_cast(sp); + seat.pointer->focusedMonitor = nullptr; + }, + .motion = [](void* sp, wl_pointer*, uint32_t, wl_fixed_t x, wl_fixed_t y) { + auto& seat = *static_cast(sp); + seat.pointer->x = wl_fixed_to_int(x); + seat.pointer->y = wl_fixed_to_int(y); + }, + .button = [](void* sp, wl_pointer*, uint32_t, uint32_t, uint32_t button, uint32_t pressed) { + auto& seat = *static_cast(sp); + auto it = std::find(begin(seat.pointer->btns), end(seat.pointer->btns), button); + if (pressed == WL_POINTER_BUTTON_STATE_PRESSED && it == end(seat.pointer->btns)) { + seat.pointer->btns.push_back(button); + } else if (pressed == WL_POINTER_BUTTON_STATE_RELEASED && it != end(seat.pointer->btns)) { + seat.pointer->btns.erase(it); + } + }, + .axis = [](void* sp, wl_pointer*, uint32_t, uint32_t, wl_fixed_t) { }, + .frame = [](void* sp, wl_pointer*) { + auto& seat = *static_cast(sp); + auto mon = seat.pointer->focusedMonitor; + if (!mon) { + return; + } + for (auto btn : seat.pointer->btns) { + mon->bar.click(mon, seat.pointer->x, seat.pointer->y, btn); + } + seat.pointer->btns.clear(); + }, + .axis_source = [](void*, wl_pointer*, uint32_t) { }, + .axis_stop = [](void*, wl_pointer*, uint32_t, uint32_t) { }, + .axis_discrete = [](void*, wl_pointer*, uint32_t, int32_t) { }, +}; + +static const struct wl_seat_listener seatListener = { + .capabilities = [](void* sp, wl_seat*, uint32_t cap) + { + auto& seat = *static_cast(sp); + auto hasPointer = cap & WL_SEAT_CAPABILITY_POINTER; + if (!seat.pointer && hasPointer) { + auto &pointer = seat.pointer.emplace(); + pointer.wlPointer = wl_unique_ptr {wl_seat_get_pointer(seat.wlSeat.get())}; + wl_pointer_add_listener(seat.pointer->wlPointer.get(), &pointerListener, &seat); + } else if (seat.pointer && !hasPointer) { + seat.pointer.reset(); + } + }, + .name = [](void*, wl_seat*, const char* name) { } +}; + +void setupMonitor(uint32_t name, wl_output* output) { + auto& monitor = monitors.emplace_back(Monitor {name, {}, wl_unique_ptr {output}}); + 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); +} + +void updatemon(Monitor& mon) +{ + if (!mon.hasData) { + return; + } + if (mon.desiredVisibility) { + if (mon.bar.visible()) { + mon.bar.invalidate(); + } else { + mon.bar.show(mon.wlOutput.get()); + } + } else if (mon.bar.visible()) { + mon.bar.hide(); + } +} + +// called after we have received the initial batch of globals +void onReady() +{ + requireGlobal(compositor, "wl_compositor"); + requireGlobal(shm, "wl_shm"); + requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1"); + requireGlobal(xdgOutputManager, "zxdg_output_manager_v1"); + setupStatusFifo(); + wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc. + + ready = true; + for (auto output : uninitializedOutputs) { + setupMonitor(output.first, output.second); + } + wl_display_roundtrip(display); // wait for xdg_output names before we read stdin +} + +bool createFifo(std::string path) +{ + auto result = mkfifo(path.c_str(), 0666); + if (result == 0) { + auto fd = open(path.c_str(), O_CLOEXEC | O_NONBLOCK | O_RDONLY); + if (fd < 0) { + diesys("open status fifo reader"); + } + statusFifoName = path; + statusFifoFd = fd; + + fd = open(path.c_str(), O_CLOEXEC | O_WRONLY); + if (fd < 0) { + diesys("open status fifo writer"); + } + statusFifoWriter = fd; + + pollfds.push_back({ + .fd = statusFifoFd, + .events = POLLIN, + }); + return true; + } else if (errno != EEXIST) { + diesys("mkfifo"); + } + + return false; +} + +void setupStatusFifo() +{ + if (!statusFifoName.empty()) { + createFifo(statusFifoName); + return; + } + + for (auto i=0; i<100; i++) { + auto path = std::string{getenv("XDG_RUNTIME_DIR")} + "/somebar-" + std::to_string(i); + if (createFifo(path)) { + return; + } + } +} + +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 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 {}; + stream >> std::ws; + std::getline(stream, title); + mon->bar.setTitle(title); + } else if (command == "selmon") { + uint32_t selected; + stream >> selected; + mon->bar.setSelected(selected); + if (selected) { + selmon = &*mon; + } else if (selmon == &*mon) { + selmon = nullptr; + } + } 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 & tagMask ? 0 : -1); + } + mon->tags = tags; + } else if (command == "layout") { + auto layout = std::string {}; + stream >> std::ws; + std::getline(stream, layout); + mon->bar.setLayout(layout); + } + mon->hasData = true; + updatemon(*mon); +} + +const std::string prefixStatus = "status "; +const std::string prefixShow = "show "; +const std::string prefixHide = "hide "; +const std::string prefixToggle = "toggle "; +const std::string argAll = "all"; +const std::string argSelected = "selected"; + +static LineBuffer<512> statusBuffer; +void onStatus() +{ + statusBuffer.readLines( + [](void* p, size_t size) { + return read(statusFifoFd, p, size); + }, + [](const char* buffer, size_t n) { + auto str = std::string {buffer, n}; + if (str.rfind(prefixStatus, 0) == 0) { + lastStatus = str.substr(prefixStatus.size()); + for (auto &monitor : monitors) { + monitor.bar.setStatus(lastStatus); + monitor.bar.invalidate(); + } + } else if (str.rfind(prefixShow, 0) == 0) { + updateVisibility(str.substr(prefixShow.size()), [](bool) { return true; }); + } else if (str.rfind(prefixHide, 0) == 0) { + updateVisibility(str.substr(prefixHide.size()), [](bool) { return false; }); + } else if (str.rfind(prefixToggle, 0) == 0) { + updateVisibility(str.substr(prefixToggle.size()), [](bool vis) { return !vis; }); + } + }); +} + +void updateVisibility(const std::string& name, bool(*updater)(bool)) +{ + auto isCurrent = name == argSelected; + auto isAll = name == argAll; + for (auto& mon : monitors) { + if (isAll || + isCurrent && &mon == selmon || + mon.xdgName == name) { + auto newVisibility = updater(mon.desiredVisibility); + if (newVisibility != mon.desiredVisibility) { + mon.desiredVisibility = newVisibility; + updatemon(mon); + } + } + } +} + +struct HandleGlobalHelper { + wl_registry* registry; + uint32_t name; + const char* interface; + + template + bool handle(T& store, const wl_interface& iface, int version) { + if (strcmp(interface, iface.name)) { + return false; + } + store = static_cast(wl_registry_bind(registry, name, &iface, version)); + return true; + } +}; +void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) +{ + auto reg = HandleGlobalHelper { registry, name, interface }; + if (reg.handle(compositor, wl_compositor_interface, 4)) return; + if (reg.handle(shm, wl_shm_interface, 1)) return; + if (reg.handle(wlrLayerShell, zwlr_layer_shell_v1_interface, 1)) return; + if (reg.handle(xdgOutputManager, zxdg_output_manager_v1_interface, 3)) return; + if (reg.handle(xdgWmBase, xdg_wm_base_interface, 2)) { + xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, 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); + return; + } + if (wl_output* output; reg.handle(output, wl_output_interface, 1)) { + if (ready) { + setupMonitor(name, output); + } else { + uninitializedOutputs.push_back({name, output}); + } + return; + } +} +void onGlobalRemove(void*, wl_registry* registry, uint32_t name) +{ + monitors.remove_if([name](const Monitor &mon) { return mon.registryName == name; }); + seats.remove_if([name](const Seat &seat) { return seat.name == name; }); +} +static const struct wl_registry_listener registry_listener = { + .global = onGlobalAdd, + .global_remove = onGlobalRemove, +}; + +int main(int argc, char* argv[]) +{ + int opt; + while ((opt = getopt(argc, argv, "chvs:")) != -1) { + switch (opt) { + case 's': + statusFifoName = optarg; + break; + case 'h': + printf("Usage: %s [-h] [-v] [-s path to the fifo] [-c command]\n", argv[0]); + printf(" -h: Show this help\n"); + printf(" -v: Show somebar version\n"); + printf(" -s: Change path to the fifo (default is \"$XDG_RUNTIME_DIR/somebar-0\")\n"); + printf(" -c: Sends a command to sombar. See README for details.\n"); + printf("If any of these are specified (except -s), somebar exits after the action.\n"); + printf("Otherwise, somebar will display itself.\n"); + exit(0); + case 'v': + printf("somebar " SOMEBAR_VERSION "\n"); + exit(0); + case 'c': + if (optind >= argc) { + die("Expected command"); + } + if (statusFifoName.empty()) { + statusFifoName = std::string {getenv("XDG_RUNTIME_DIR")} + "/somebar-0"; + } + statusFifoWriter = open(statusFifoName.c_str(), O_WRONLY | O_CLOEXEC); + if (statusFifoWriter < 0) { + fprintf(stderr, "could not open %s: ", statusFifoName.c_str()); + perror(""); + exit(1); + } + auto str = std::string {}; + for (auto i = optind; i optind) str += " "; + str += argv[i]; + } + str += "\n"; + write(statusFifoWriter, str.c_str(), str.size()); + exit(0); + } + } + + if (pipe(signalSelfPipe.data()) < 0) { + diesys("pipe"); + } + setCloexec(signalSelfPipe[0]); + setCloexec(signalSelfPipe[1]); + + struct sigaction sighandler = {}; + sighandler.sa_handler = [](int) { + if (write(signalSelfPipe[1], "0", 1) < 0) { + diesys("write"); + } + }; + if (sigaction(SIGTERM, &sighandler, nullptr) < 0) { + diesys("sigaction"); + } + if (sigaction(SIGINT, &sighandler, nullptr) < 0) { + diesys("sigaction"); + } + + struct sigaction chld_handler = {}; + chld_handler.sa_handler = SIG_IGN; + if (sigaction(SIGCHLD, &chld_handler, nullptr) < 0) { + die("sigaction"); + } + + pollfds.push_back({ + .fd = signalSelfPipe[0], + .events = POLLIN, + }); + + display = wl_display_connect(nullptr); + if (!display) { + die("Failed to connect to Wayland display"); + } + displayFd = wl_display_get_fd(display); + + auto registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, nullptr); + wl_display_roundtrip(display); + onReady(); + + pollfds.push_back({ + .fd = displayFd, + .events = POLLIN, + }); + pollfds.push_back({ + .fd = STDIN_FILENO, + .events = POLLIN, + }); + if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) { + diesys("fcntl F_SETFL"); + } + + while (!quitting) { + waylandFlush(); + if (poll(pollfds.data(), pollfds.size(), -1) < 0) { + if (errno != EINTR) { + diesys("poll"); + } + } else { + for (auto& ev : pollfds) { + if (ev.revents & POLLNVAL) { + die("poll revents contains POLLNVAL"); + } else if (ev.fd == displayFd) { + if (ev.revents & POLLIN) { + if (wl_display_dispatch(display) < 0) { + die("wl_display_dispatch"); + } + } + if (ev.revents & POLLOUT) { + ev.events = POLLIN; + waylandFlush(); + } + } else if (ev.fd == STDIN_FILENO && (ev.revents & POLLIN)) { + onStdin(); + } else if (ev.fd == statusFifoFd && (ev.revents & POLLIN)) { + onStatus(); + } else if (ev.fd == signalSelfPipe[0] && (ev.revents & POLLIN)) { + quitting = true; + } + } + } + } + cleanup(); +} + +void requireGlobal(const void* p, const char* name) +{ + if (p) return; + fprintf(stderr, "Wayland compositor does not export required global %s, aborting.\n", name); + cleanup(); + exit(1); +} + +void waylandFlush() +{ + wl_display_dispatch_pending(display); + if (wl_display_flush(display) < 0 && errno == EAGAIN) { + for (auto& ev : pollfds) { + if (ev.fd == displayFd) { + ev.events |= POLLOUT; + } + } + } +} + +void setCloexec(int fd) +{ + int flags = fcntl(fd, F_GETFD); + if (flags == -1) { + diesys("fcntl FD_GETFD"); + } + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) { + diesys("fcntl FD_SETFD"); + } +} + +void cleanup() { + if (!statusFifoName.empty()) { + unlink(statusFifoName.c_str()); + } +} + +void die(const char* why) { + fprintf(stderr, "error: %s failed, aborting\n", why); + cleanup(); + exit(1); +} + +void diesys(const char* why) { + perror(why); + cleanup(); + exit(1); +} diff --git a/somebar/src/shm_buffer.cpp b/somebar/src/shm_buffer.cpp new file mode 100644 index 0000000..59baf6f --- /dev/null +++ b/somebar/src/shm_buffer.cpp @@ -0,0 +1,85 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#include +#include +#include +#include +#include "shm_buffer.hpp" +#include "common.hpp" + +static int createAnonShm(); +constexpr int n = 2; + +ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format) + : width(w) + , height(h) + , stride(w*4) +{ + auto oneSize = stride*size_t(h); + auto totalSize = oneSize * n; + auto fd = createAnonShm(); + if (fd < 0) { + diesys("memfd_create"); + } + if (ftruncate(fd, totalSize) < 0) { + diesys("ftruncate"); + } + auto pool = wl_shm_create_pool(shm, fd, totalSize); + auto ptr = reinterpret_cast(mmap(nullptr, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + if (!ptr) { + diesys("mmap"); + } + _mapping = MemoryMapping {ptr, totalSize}; + close(fd); + for (auto i=0; i { wl_shm_pool_create_buffer(pool, offset, width, height, stride, format) }, + }; + } + wl_shm_pool_destroy(pool); +} + +uint8_t* ShmBuffer::data() +{ + return _buffers[_current].data; +} + +wl_buffer* ShmBuffer::buffer() +{ + return _buffers[_current].buffer.get(); +} + +void ShmBuffer::flip() +{ + _current = 1-_current; +} + +#if defined(__linux__) +int createAnonShm() { + return memfd_create("wl_shm", MFD_CLOEXEC); +} +#elif defined(__FreeBSD__) +int createAnonShm() { + auto fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600); + setCloexec(fd); + return fd; +} +#elif defined(__OpenBSD__) +int createAnonShm() { + char name[] = "/wl_shm-XXXXXX"; + auto fd = shm_mkstemp(name); + if (fd >= 0) { + auto res = shm_unlink(name); + if (res < 0) { + return res; + } + } + setCloexec(fd); + return fd; +} +#else +#error "your system has no sane method of creating an anonymous shared memory object. no, calling shm_open in a loop is not sane." +#endif diff --git a/somebar/src/shm_buffer.hpp b/somebar/src/shm_buffer.hpp new file mode 100644 index 0000000..f6927dd --- /dev/null +++ b/somebar/src/shm_buffer.hpp @@ -0,0 +1,45 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include +#include +#include +#include "common.hpp" + +class MemoryMapping { + void* _ptr {nullptr}; + size_t _size {0}; +public: + MemoryMapping() { } + explicit MemoryMapping(void* ptr, size_t size) : _ptr(ptr), _size(size) { } + MemoryMapping(const MemoryMapping&) = delete; + MemoryMapping(MemoryMapping&& other) { swap(other); } + MemoryMapping& operator=(const MemoryMapping& other) = delete; + MemoryMapping& operator=(MemoryMapping&& other) { swap(other); return *this; } + ~MemoryMapping() { if (_ptr) munmap(_ptr, _size); } + void swap(MemoryMapping &other) { + using std::swap; + swap(_ptr, other._ptr); + swap(_size, other._size); + } +}; + +// double buffered shm +// format is must be 32-bit +class ShmBuffer { + struct Buf { + uint8_t* data {nullptr}; + wl_unique_ptr buffer; + }; + std::array _buffers; + int _current {0}; + MemoryMapping _mapping; +public: + const uint32_t width, height, stride; + + explicit ShmBuffer(int width, int height, wl_shm_format format); + uint8_t* data(); + wl_buffer* buffer(); + void flip(); +}; -- cgit v1.2.3