diff options
Diffstat (limited to 'somebar/src')
| -rw-r--r-- | somebar/src/bar.cpp | 321 | ||||
| -rw-r--r-- | somebar/src/bar.hpp | 74 | ||||
| -rw-r--r-- | somebar/src/common.hpp | 76 | ||||
| -rw-r--r-- | somebar/src/config.def.hpp | 27 | ||||
| -rw-r--r-- | somebar/src/config.hpp | 31 | ||||
| -rw-r--r-- | somebar/src/line_buffer.hpp | 71 | ||||
| -rw-r--r-- | somebar/src/main.cpp | 613 | ||||
| -rw-r--r-- | somebar/src/shm_buffer.cpp | 85 | ||||
| -rw-r--r-- | somebar/src/shm_buffer.hpp | 45 |
9 files changed, 0 insertions, 1343 deletions
diff --git a/somebar/src/bar.cpp b/somebar/src/bar.cpp deleted file mode 100644 index 38164cd..0000000 --- a/somebar/src/bar.cpp +++ /dev/null @@ -1,321 +0,0 @@ -// somebar - dwl barbar -// See LICENSE file for copyright and license details. - -#include <wayland-client-protocol.h> -#include <pango/pangocairo.h> -#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<Bar*>(owner)->layerSurfaceConfigure(serial, width, height); - } -}; -const wl_callback_listener Bar::_frameListener = { - [](void* owner, wl_callback* cb, uint32_t) - { - static_cast<Bar*>(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<PangoLayout> 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<std::string>(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 + gapY); - 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 (auto tag = _tags.size()-1; tag >= 0; tag--) { - if (x > _tags[tag].component.x) { - control = ClkTagBar; - arg.ui = 1<<tag; - argp = &arg; - break; - } - } - for (const auto& button : buttons) { - if (button.control == control && button.btn == btn) { - button.func(*mon, *(argp ? argp : &button.arg)); - return; - } - } -} - -void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height) -{ - zwlr_layer_surface_v1_ack_configure(_layerSurface.get(), serial); - if (_bufs && width == _bufs->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_surface_t> {cairo_image_surface_create_for_data( - _bufs->data(), - CAIRO_FORMAT_ARGB32, - _bufs->width, - _bufs->height, - _bufs->stride - )}; - auto painter = wl_unique_ptr<cairo_t> {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); - - 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); - } - } -} - -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; - setColorScheme(colorInactive); - 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<PangoLayout> {layout}}; - res.setText(initial); - return res; -} diff --git a/somebar/src/bar.hpp b/somebar/src/bar.hpp deleted file mode 100644 index 176a1bc..0000000 --- a/somebar/src/bar.hpp +++ /dev/null @@ -1,74 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#pragma once -#include <optional> -#include <string> -#include <vector> -#include <wayland-client.h> -#include "wlr-layer-shell-unstable-v1-client-protocol.h" -#include "common.hpp" -#include "shm_buffer.hpp" - -class BarComponent { - std::unique_ptr<std::string> _text; -public: - BarComponent(); - explicit BarComponent(wl_unique_ptr<PangoLayout> layout); - int width() const; - void setText(const std::string& text); - wl_unique_ptr<PangoLayout> 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<wl_surface> _surface; - wl_unique_ptr<zwlr_layer_surface_v1> _layerSurface; - wl_unique_ptr<PangoContext> _pangoContext; - std::optional<ShmBuffer> _bufs; - std::vector<Tag> _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 deleted file mode 100644 index c905358..0000000 --- a/somebar/src/common.hpp +++ /dev/null @@ -1,76 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#pragma once -#include <memory> -#include <string> -#include <vector> -#include <wayland-client.h> -#include <linux/input-event-codes.h> -#include <cairo/cairo.h> -#include <pango/pango.h> -#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; // <linux/input-event-codes.h> - 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<typename T> -struct WlDeleter; -#define WL_DELETER(type, fn) template<> struct WlDeleter<type> { \ - void operator()(type* v) { if(v) fn(v); } \ - } - -template<typename T> -using wl_unique_ptr = std::unique_ptr<T, WlDeleter<T>>; - -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 deleted file mode 100644 index 40a8c95..0000000 --- a/somebar/src/config.def.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// 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<std::string> 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 deleted file mode 100644 index caf0178..0000000 --- a/somebar/src/config.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#pragma once -#include "common.hpp" - -constexpr bool topbar = false; - -constexpr int paddingX = 10; -constexpr int paddingY = 3; -constexpr int gapY = 3; - -// See https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html -constexpr const char* font = "BmPlus IBM VGA 9x16 8"; - -constexpr Color orange = Color(0xff, 0x8c, 0x00); -constexpr Color gray2 = Color(0x00, 0x00, 0x00); - -constexpr ColorScheme colorInactive = {orange, gray2}; -constexpr ColorScheme colorActive = {gray2, orange}; -constexpr const char* termcmd[] = {"foot", nullptr}; - -static std::vector<std::string> tagNames = { - "F", "U", "H", - ".", "V", "L", - "O", "M" -}; - -constexpr Button buttons[] = { - { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, -}; diff --git a/somebar/src/line_buffer.hpp b/somebar/src/line_buffer.hpp deleted file mode 100644 index a5497bf..0000000 --- a/somebar/src/line_buffer.hpp +++ /dev/null @@ -1,71 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#pragma once -#include <array> -#include <algorithm> -#include <sys/types.h> - -// reads data from Reader, and passes complete lines to Consumer. -template<size_t BufSize> -class LineBuffer { - using Iterator = typename std::array<char, BufSize>::iterator; - std::array<char, BufSize> _buffer; - Iterator _bufferedTo; - Iterator _consumedTo; - bool _discardLine {false}; -public: - LineBuffer() - : _bufferedTo {_buffer.begin()} - , _consumedTo {_buffer.begin()} - { - } - - template<typename Reader, typename Consumer> - 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<typename Consumer> - 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 deleted file mode 100644 index 6274959..0000000 --- a/somebar/src/main.cpp +++ /dev/null @@ -1,613 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#include <algorithm> -#include <cstdio> -#include <sstream> -#include <list> -#include <optional> -#include <utility> -#include <vector> -#include <fcntl.h> -#include <poll.h> -#include <signal.h> -#include <sys/mman.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> -#include <linux/input-event-codes.h> -#include <wayland-client.h> -#include <wayland-cursor.h> -#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<wl_output> wlOutput; - Bar bar; - bool desiredVisibility {true}; - bool hasData; - uint32_t tags; -}; - -struct SeatPointer { - wl_unique_ptr<wl_pointer> wlPointer; - Monitor* focusedMonitor; - int x, y; - std::vector<int> btns; -}; -struct Seat { - uint32_t name; - wl_unique_ptr<wl_seat> wlSeat; - std::optional<SeatPointer> 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<Monitor> monitors; -static std::vector<std::pair<uint32_t, wl_output*>> uninitializedOutputs; -static std::list<Seat> seats; -static Monitor* selmon; -static std::string lastStatus; -static std::string statusFifoName; -static std::vector<pollfd> pollfds; -static std::array<int, 2> 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<char* const*>(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<Monitor*>(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<Seat*>(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<Seat*>(sp); - seat.pointer->focusedMonitor = nullptr; - }, - .motion = [](void* sp, wl_pointer*, uint32_t, wl_fixed_t x, wl_fixed_t y) { - auto& seat = *static_cast<Seat*>(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<Seat*>(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<Seat*>(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<Seat*>(sp); - auto hasPointer = cap & WL_SEAT_CAPABILITY_POINTER; - if (!seat.pointer && hasPointer) { - auto &pointer = seat.pointer.emplace(); - pointer.wlPointer = wl_unique_ptr<wl_pointer> {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<wl_output> {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; i<tagNames.size(); i++) { - auto tagMask = 1 << i; - int state = TagState::None; - if (tags & tagMask) - state |= TagState::Active; - if (urgent & tagMask) - state |= TagState::Urgent; - mon->bar.setTag(i, state, occupied & tagMask ? 1 : 0, clientTags & 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<typename T> - bool handle(T& store, const wl_interface& iface, int version) { - if (strcmp(interface, iface.name)) { - return false; - } - store = static_cast<T>(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, 4)) 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<wl_seat> {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<argc; i++) { - if (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 deleted file mode 100644 index 59baf6f..0000000 --- a/somebar/src/shm_buffer.cpp +++ /dev/null @@ -1,85 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#include <fcntl.h> -#include <sys/mman.h> -#include <sys/stat.h> -#include <unistd.h> -#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<uint8_t*>(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<n; i++) { - auto offset = oneSize*i; - _buffers[i] = { - ptr+offset, - wl_unique_ptr<wl_buffer> { 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 deleted file mode 100644 index f6927dd..0000000 --- a/somebar/src/shm_buffer.hpp +++ /dev/null @@ -1,45 +0,0 @@ -// somebar - dwl bar -// See LICENSE file for copyright and license details. - -#pragma once -#include <array> -#include <sys/mman.h> -#include <wayland-client.h> -#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<wl_buffer> buffer; - }; - std::array<Buf, 2> _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(); -}; |
