replace qt with cairo/pango/epoll

This commit is contained in:
Raphael Robatsch 2021-10-27 17:24:47 +02:00
parent 31c09e24d6
commit 3db22e4a71
6 changed files with 206 additions and 125 deletions

View file

@ -3,7 +3,9 @@ project('somebar', ['c', 'cpp'],
wayland_dep = dependency('wayland-client')
wayland_cursor_dep = dependency('wayland-cursor')
qt5_dep = dependency('qt5', modules: ['Core', 'Gui'])
cairo_dep = dependency('cairo')
pango_dep = dependency('pango')
pangocairo_dep = dependency('pangocairo')
subdir('protocols')
@ -12,4 +14,10 @@ executable('somebar',
'src/shm_buffer.cpp',
'src/bar.cpp',
wayland_sources,
dependencies: [wayland_dep, wayland_cursor_dep, qt5_dep])
dependencies: [
wayland_dep,
wayland_cursor_dep,
cairo_dep,
pango_dep,
pangocairo_dep,
])

View file

@ -1,11 +1,14 @@
// somebar - dwl bar
// somebar - dwl barbar
// See LICENSE file for copyright and license details.
#include <QColor>
#include <QImage>
#include <QPainter>
#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)
@ -21,25 +24,60 @@ const wl_callback_listener Bar::_frameListener = {
}
};
static QFont getFont()
struct Font {
PangoFontDescription *description;
int height {0};
};
static Font getFont()
{
QFont font {fontFamily, fontSizePt};
font.setBold(fontBold);
return font;
}
static QFont font = getFont();
static QFontMetrics fontMetrics = QFontMetrics {font};
auto fontMap = pango_cairo_font_map_get_default();
auto fontDesc = pango_font_description_from_string(font);
auto tempContext = pango_font_map_create_context(fontMap);
auto font = pango_font_map_load_font(fontMap, tempContext, fontDesc);
auto metrics = pango_font_get_metrics(font, pango_language_get_default());
const wl_surface* Bar::surface() const { return _surface.get(); }
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);
g_object_unref(fontMap);
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)
{
auto chars = new char[text.size()];
text.copy(chars, text.size());
_text.reset(chars);
pango_layout_set_text(pangoLayout.get(), chars, text.size());
}
Bar::Bar(Monitor *mon)
{
_mon = mon;
_pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default()));
for (auto i=0u; i<tagNames.size(); i++) {
_tags.push_back({ ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_NONE, 0, 0, 0 });
_tags.push_back({ ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_NONE, 0, 0, createComponent(tagNames[i]) });
}
_layoutCmp = createComponent();
_titleCmp = createComponent();
_statusCmp = createComponent();
}
const wl_surface* Bar::surface() const { return _surface.get(); }
void Bar::create(wl_output *output)
{
_surface.reset(wl_compositor_create_surface(compositor));
@ -50,27 +88,47 @@ void Bar::create(wl_output *output)
zwlr_layer_surface_v1_set_anchor(_layerSurface.get(),
anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
auto barSize = fontMetrics.ascent() + fontMetrics.descent() + paddingY * 2;
_textY = fontMetrics.ascent() + paddingY;
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::setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient)
{
auto& t = _tags[tag];
t.state = state;
t.numClients = numClients;
t.focusedClient = focusedClient;
}
void Bar::setSelected(bool selected) { _selected = selected; }
void Bar::setLayout(int layout) { _layoutCmp.setText(layoutNames[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) 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(int x, int, int btn)
{
Arg arg = {0};
Arg *argp = nullptr;
int control = ClkNone;
if (x > _statusX) {
if (x > _statusCmp.x) {
control = ClkStatusText;
} else if (x > _titleX) {
} else if (x > _titleCmp.x) {
control = ClkWinTitle;
} else if (x > _layoutX) {
} else if (x > _layoutCmp.x) {
control = ClkLayoutSymbol;
} else for (auto tag = _tags.size()-1; tag >= 0; tag--) {
if (x > _tags[tag].x) {
if (x > _tags[tag].component.x) {
control = ClkTagBar;
arg.ui = 1<<tag;
argp = &arg;
@ -86,28 +144,6 @@ void Bar::click(int x, int, int btn)
}
}
void Bar::invalidate()
{
if (_invalid) return;
_invalid = true;
auto frame = wl_surface_frame(_surface.get());
wl_callback_add_listener(frame, &_frameListener, this);
wl_surface_commit(_surface.get());
}
void Bar::setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient)
{
auto& t = _tags[tag];
t.state = state;
t.numClients = numClients;
t.focusedClient = focusedClient;
}
void Bar::setSelected(bool selected) { _selected = selected; }
void Bar::setLayout(int layout) { _layout = layout; }
void Bar::setTitle(const char *title) { _title = title; }
void Bar::setStatus(const QString &status) { _status = status; }
void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height)
{
zwlr_layer_surface_v1_ack_configure(_layerSurface.get(), serial);
@ -117,24 +153,22 @@ void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height
void Bar::render()
{
auto img = QImage {
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,
QImage::Format_ARGB32
};
auto painter = QPainter {&img};
_painter = &painter;
_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;
painter.setFont(font);
renderTags();
setColorScheme(_selected ? colorActive : colorInactive);
_layoutX = _x;
renderText(layoutNames[_layout]);
_titleX = _x;
renderText(_title);
renderComponent(_layoutCmp);
renderComponent(_titleCmp);
renderStatus();
_painter = nullptr;
@ -145,50 +179,68 @@ void Bar::render()
_invalid = false;
}
void Bar::setColorScheme(const ColorScheme &scheme, bool invert)
{
_painter->setBrush(QBrush {invert ? scheme.fg : scheme.bg});
_painter->setPen(QPen {QBrush {invert ? scheme.bg : scheme.fg}, 1});
}
void Bar::renderTags()
{
for (auto i=0u; i<_tags.size(); i++) {
auto& tag = _tags[i];
tag.x = _x;
for (auto &tag : _tags) {
setColorScheme(
tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE ? colorActive : colorInactive,
tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_URGENT);
renderText(tagNames[i]);
auto indicators = qMin(tag.numClients, _bufs->height/2);
renderComponent(tag.component);
auto indicators = std::min(tag.numClients, _bufs->height/2);
for (auto ind = 0; ind < indicators; ind++) {
if (ind == tag.focusedClient) {
_painter->drawLine(tag.x, ind*2, tag.x+5, ind*2);
} else {
_painter->drawPoint(tag.x, ind*2);
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::renderText(const QString &text)
{
auto size = textWidth(text) + paddingX*2;
_painter->fillRect(_x, 0, size, _bufs->height, _painter->brush());
_painter->drawText(paddingX+_x, _textY, text);
_x += size;
}
void Bar::renderStatus()
{
_painter->fillRect(_x, 0, _bufs->width-_x, _bufs->height, _painter->brush());
auto size = textWidth(_status) + paddingX;
_statusX = _bufs->width - size;
_painter->drawText(paddingX+_statusX, _textY, _status);
_x = _bufs->width;
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);
}
int Bar::textWidth(const QString &text)
void Bar::setColorScheme(const ColorScheme &scheme, bool invert)
{
return fontMetrics.size(Qt::TextSingleLine, text).width();
_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;
}

View file

@ -3,20 +3,29 @@
#pragma once
#include <optional>
#include <string>
#include <vector>
#include <wayland-client.h>
#include <QString>
#include <QFontMetrics>
#include <QPainter>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "common.hpp"
#include "shm_buffer.hpp"
class BarComponent {
std::unique_ptr<char[]> _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 {
znet_tapesoftware_dwl_wm_monitor_v1_tag_state state;
int numClients;
int focusedClient;
int x;
BarComponent component;
};
struct Monitor;
@ -26,35 +35,39 @@ class Bar {
wl_unique_ptr<wl_surface> _surface;
wl_unique_ptr<zwlr_layer_surface_v1> _layerSurface;
wl_unique_ptr<PangoContext> _pangoContext;
Monitor *_mon;
QPainter *_painter {nullptr};
std::optional<ShmBuffer> _bufs;
int _textY, _x;
int _statusX, _titleX, _layoutX;
std::vector<Tag> _tags;
BarComponent _layoutCmp, _titleCmp, _statusCmp;
bool _selected;
bool _invalid {false};
std::vector<Tag> _tags;
int _layout;
bool _selected;
QString _title;
QString _status;
// 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();
void renderText(const QString &text);
int textWidth(const QString &text);
// 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(Monitor *mon);
const wl_surface* surface() const;
void create(wl_output *output);
void setSelected(bool selected);
void setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, int numClients, int focusedClient);
void setSelected(bool selected);
void setLayout(int layout);
void setTitle(const char *title);
void setStatus(const QString &status);
void setTitle(const std::string &title);
void setStatus(const std::string &status);
void invalidate();
void click(int x, int y, int btn);
};

View file

@ -3,16 +3,22 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <wayland-client.h>
#include <linux/input-event-codes.h>
#include <QColor>
#include <QString>
#include <cairo/cairo.h>
#include <pango/pango.h>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "net-tapesoftware-dwl-wm-unstable-v1-client-protocol.h"
struct Color {
Color() {}
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 {
QColor fg, bg;
Color fg, bg;
};
union Arg {
unsigned int ui;
@ -32,8 +38,8 @@ extern wl_display *display;
extern wl_compositor *compositor;
extern wl_shm *shm;
extern zwlr_layer_shell_v1 *wlrLayerShell;
extern std::vector<QString> tagNames;
extern std::vector<QString> layoutNames;
extern std::vector<std::string> tagNames;
extern std::vector<std::string> layoutNames;
void view(Monitor &m, const Arg &arg);
void toggleview(Monitor &m, const Arg &arg);
@ -57,3 +63,9 @@ 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);
WL_DELETER(cairo_surface_t, cairo_surface_destroy);
WL_DELETER(PangoContext, g_object_unref);
WL_DELETER(PangoLayout, g_object_unref);

View file

@ -9,12 +9,11 @@ constexpr bool topbar = true;
constexpr int paddingX = 10;
constexpr int paddingY = 3;
constexpr const char *fontFamily = "Source Sans Pro";
constexpr int fontSizePt = 12;
constexpr bool fontBold = false;
// See https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html
constexpr const char *font = "Source Sans Pro 12";
constexpr ColorScheme colorInactive = {QColor(0xbb, 0xbb, 0xbb), QColor(0x22, 0x22, 0x22)};
constexpr ColorScheme colorActive = {QColor(0xee, 0xee, 0xee), QColor(0x00, 0x55, 0x77)};
constexpr ColorScheme colorInactive = {Color(0xbb, 0xbb, 0xbb), Color(0x22, 0x22, 0x22)};
constexpr ColorScheme colorActive = {Color(0xee, 0xee, 0xee), Color(0x00, 0x55, 0x77)};
constexpr const char *termcmd[] = {"foot", nullptr};
constexpr Button buttons[] = {

View file

@ -1,10 +1,14 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#include <cstdio>
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <algorithm>
#include <list>
#include <optional>
#include <vector>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/signalfd.h>
@ -12,13 +16,8 @@
#include <sys/types.h>
#include <unistd.h>
#include <linux/input-event-codes.h>
#include <list>
#include <optional>
#include <vector>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include <QGuiApplication>
#include <QString>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "net-tapesoftware-dwl-wm-unstable-v1-client-protocol.h"
@ -57,15 +56,15 @@ wl_compositor *compositor;
wl_shm *shm;
zwlr_layer_shell_v1 *wlrLayerShell;
znet_tapesoftware_dwl_wm_v1 *dwlWm;
std::vector<QString> tagNames;
std::vector<QString> layoutNames;
std::vector<std::string> tagNames;
std::vector<std::string> layoutNames;
static xdg_wm_base *xdgWmBase;
static wl_surface *cursorSurface;
static wl_cursor_image *cursorImage;
static bool ready;
static std::list<Monitor> monitors;
static std::list<Seat> seats;
static QString lastStatus;
static std::string lastStatus;
static std::string statusFifoName;
static int epoll {-1};
static int displayFd {-1};
@ -277,7 +276,7 @@ static void onStatus()
{
char buffer[512];
auto n = read(statusFifoFd, buffer, sizeof(buffer));
lastStatus = QString::fromUtf8(buffer, n);
lastStatus = {buffer, (unsigned long) n};
for (auto &monitor : monitors) {
if (monitor.bar) {
monitor.bar->setStatus(lastStatus);
@ -343,8 +342,6 @@ int main(int argc, char **argv)
sigaddset(&blockedsigs, SIGTERM);
sigprocmask(SIG_BLOCK, &blockedsigs, nullptr);
QGuiApplication app {argc, argv};
epoll_event epollEv = {0};
std::array<epoll_event, 5> epollEvents;
epoll = epoll_create1(EPOLL_CLOEXEC);