somebar/src/bar.cpp

342 lines
8.4 KiB
C++
Raw Permalink Normal View History

2021-10-27 17:24:47 +02:00
// somebar - dwl barbar
2021-10-20 20:45:23 +02:00
// See LICENSE file for copyright and license details.
2021-10-27 17:24:47 +02:00
#include <wayland-client-protocol.h>
#include <pango/pangocairo.h>
2021-10-20 20:45:23 +02:00
#include "bar.hpp"
2021-10-27 17:24:47 +02:00
#include "cairo.h"
2021-10-20 20:45:23 +02:00
#include "config.hpp"
2021-10-27 17:24:47 +02:00
#include "pango/pango-font.h"
#include "pango/pango-fontmap.h"
#include "pango/pango-layout.h"
2021-10-20 20:45:23 +02:00
const zwlr_layer_surface_v1_listener Bar::_layerSurfaceListener = {
2021-10-29 20:33:27 +02:00
[](void* owner, zwlr_layer_surface_v1*, uint32_t serial, uint32_t width, uint32_t height)
{
static_cast<Bar*>(owner)->layerSurfaceConfigure(serial, width, height);
}
2021-10-20 20:45:23 +02:00
};
2021-10-22 16:52:04 +02:00
const wl_callback_listener Bar::_frameListener = {
2021-10-29 20:33:27 +02:00
[](void* owner, wl_callback* cb, uint32_t)
{
static_cast<Bar*>(owner)->render();
wl_callback_destroy(cb);
}
2021-10-22 16:52:04 +02:00
};
2021-10-20 20:45:23 +02:00
2021-10-27 17:24:47 +02:00
struct Font {
2021-10-29 20:33:27 +02:00
PangoFontDescription* description;
int height {0};
2021-10-27 17:24:47 +02:00
};
static Font getFont()
2021-10-22 15:49:09 +02:00
{
2021-10-29 20:33:27 +02:00
auto fontMap = pango_cairo_font_map_get_default();
2021-11-17 17:40:12 +01:00
if (!fontMap) {
die("pango_cairo_font_map_get_default");
}
2021-10-29 20:33:27 +02:00
auto fontDesc = pango_font_description_from_string(font);
2021-11-17 17:40:12 +01:00
if (!fontDesc) {
die("pango_font_description_from_string");
}
2021-10-29 20:33:27 +02:00
auto tempContext = pango_font_map_create_context(fontMap);
2021-11-17 17:40:12 +01:00
if (!tempContext) {
die("pango_font_map_create_context");
}
2021-10-29 20:33:27 +02:00
auto font = pango_font_map_load_font(fontMap, tempContext, fontDesc);
2021-11-17 17:40:12 +01:00
if (!font) {
die("pango_font_map_load_font");
}
2021-10-29 20:33:27 +02:00
auto metrics = pango_font_get_metrics(font, pango_language_get_default());
2021-11-17 17:40:12 +01:00
if (!metrics) {
die("pango_font_get_metrics");
}
2021-10-27 17:24:47 +02:00
2021-10-29 20:33:27 +02:00
auto res = Font {};
res.description = fontDesc;
res.height = PANGO_PIXELS(pango_font_metrics_get_height(metrics));
2021-10-27 17:24:47 +02:00
2021-10-29 20:33:27 +02:00
pango_font_metrics_unref(metrics);
g_object_unref(font);
g_object_unref(tempContext);
return res;
2021-10-22 15:49:09 +02:00
}
2021-10-27 17:24:47 +02:00
static Font barfont = getFont();
2021-10-22 15:49:09 +02:00
2021-10-27 17:24:47 +02:00
BarComponent::BarComponent() { }
2021-11-17 17:40:12 +01:00
BarComponent::BarComponent(wl_unique_ptr<PangoLayout> layout)
: pangoLayout {std::move(layout)}
{
}
2021-10-27 17:24:47 +02:00
int BarComponent::width() const
{
2021-10-29 20:33:27 +02:00
int w, h;
pango_layout_get_size(pangoLayout.get(), &w, &h);
return PANGO_PIXELS(w);
2021-10-27 17:24:47 +02:00
}
2021-11-17 17:40:12 +01:00
void BarComponent::setText(const std::string& text)
2021-10-27 17:24:47 +02:00
{
2021-10-29 20:33:27 +02:00
_text = std::make_unique<std::string>(text);
pango_layout_set_text(pangoLayout.get(), _text->c_str(), _text->size());
2021-10-27 17:24:47 +02:00
}
2021-10-25 20:52:21 +02:00
Bar::Bar()
2021-10-25 20:52:21 +02:00
{
2021-10-29 20:33:27 +02:00
_pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default()));
2021-11-17 17:40:12 +01:00
if (!_pangoContext) {
die("pango_font_map_create_context");
}
for (const auto& tagName : tagNames) {
_tags.push_back({ TagState::None, 0, 0, createComponent(tagName) });
2021-10-29 20:33:27 +02:00
}
_layoutCmp = createComponent();
_titleCmp = createComponent();
_statusCmp = createComponent();
2021-10-25 20:52:21 +02:00
}
2021-11-17 17:40:12 +01:00
const wl_surface* Bar::surface() const
{
return _surface.get();
}
bool Bar::visible() const
{
return _surface.get();
}
2021-10-27 17:24:47 +02:00
void Bar::show(wl_output* output)
2021-10-20 20:45:23 +02:00
{
2021-11-17 17:40:12 +01:00
if (visible()) {
return;
}
2021-10-29 20:33:27 +02:00
_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"));
2021-10-29 20:33:27 +02:00
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(),
2021-10-29 23:23:35 +02:00
anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
2021-10-22 15:49:09 +02:00
2021-10-29 20:33:27 +02:00
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());
2021-10-20 20:45:23 +02:00
}
2021-10-27 17:24:47 +02:00
void Bar::hide()
{
2021-11-17 17:40:12 +01:00
if (!visible()) {
return;
}
2021-10-29 20:33:27 +02:00
_layerSurface.reset();
_surface.reset();
_bufs.reset();
2021-10-27 17:24:47 +02:00
}
2021-10-27 17:24:47 +02:00
2021-10-29 22:22:58 +02:00
void Bar::setTag(int tag, int state, int numClients, int focusedClient)
2021-10-27 17:24:47 +02:00
{
2021-10-29 20:33:27 +02:00
auto& t = _tags[tag];
t.state = state;
t.numClients = numClients;
t.focusedClient = focusedClient;
2021-10-27 17:24:47 +02:00
}
2021-11-17 17:40:12 +01:00
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);
}
2021-10-27 17:24:47 +02:00
void Bar::invalidate()
{
2021-11-17 17:40:12 +01:00
if (_invalid || !visible()) {
return;
}
2021-10-29 20:33:27 +02:00
_invalid = true;
auto frame = wl_surface_frame(_surface.get());
wl_callback_add_listener(frame, &_frameListener, this);
wl_surface_commit(_surface.get());
2021-10-27 17:24:47 +02:00
}
void Bar::click(Monitor* mon, int x, int, int btn)
2021-10-22 17:26:05 +02:00
{
2021-10-29 20:33:27 +02:00
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;
}
}
2021-11-17 17:40:12 +01:00
for (const auto& button : buttons) {
2021-10-29 20:33:27 +02:00
if (button.control == control && button.btn == btn) {
button.func(*mon, *(argp ? argp : &button.arg));
2021-10-29 20:33:27 +02:00
return;
}
}
2021-10-22 17:26:05 +02:00
}
2021-10-20 20:45:23 +02:00
void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height)
{
2021-10-29 20:33:27 +02:00
zwlr_layer_surface_v1_ack_configure(_layerSurface.get(), serial);
if (_bufs && width == _bufs->width && height == _bufs->height) {
return;
2021-11-17 17:40:12 +01:00
}
2021-10-29 20:33:27 +02:00
_bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888);
render();
2021-10-20 21:09:19 +02:00
}
void Bar::render()
{
2021-11-17 17:40:12 +01:00
if (!_bufs) {
2021-11-02 18:49:50 +01:00
return;
2021-11-17 17:40:12 +01:00
}
2021-10-29 20:33:27 +02:00
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;
2021-10-22 15:34:19 +02:00
2021-10-29 20:33:27 +02:00
renderTags();
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);
2021-10-29 20:33:27 +02:00
wl_surface_commit(_surface.get());
_bufs->flip();
_invalid = false;
2021-10-20 20:45:23 +02:00
}
2021-10-22 15:34:19 +02:00
2021-10-22 15:55:29 +02:00
void Bar::renderTags()
2021-10-22 15:34:19 +02:00
{
2023-04-09 17:59:56 +02:00
// Check if all tags are active (Mod+0)
bool allActive = true;
2021-10-29 20:33:27 +02:00
for (auto &tag : _tags) {
2023-04-09 17:59:56 +02:00
if (tag.state & TagState::Active){
if (!allActive){
allActive = true;
break;
}
allActive = false;
}
}
bool renderThis;
for (auto &tag : _tags) {
renderThis = false;
2021-10-29 20:33:27 +02:00
setColorScheme(
2021-10-29 22:22:58 +02:00
tag.state & TagState::Active ? colorActive : colorInactive,
tag.state & TagState::Urgent);
2023-04-09 17:59:56 +02:00
// Reder active tag if it's the only one active
if (!allActive && tag.state & TagState::Active)
renderThis = true;
2021-11-06 16:26:12 +01:00
auto indicators = std::min(tag.numClients, static_cast<int>(_bufs->height/2));
2021-10-29 20:33:27 +02:00
for (auto ind = 0; ind < indicators; ind++) {
2023-04-09 17:59:56 +02:00
// render tags having indicators
if (tag.focusedClient == -1)
renderThis = true;
// render tags having the focused client
if (tag.focusedClient == 0){
renderThis = true;
}
2021-10-29 20:33:27 +02:00
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);
}
2023-04-09 17:59:56 +02:00
if (renderThis)
renderComponent(tag.component);
2021-10-29 20:33:27 +02:00
}
2021-10-22 15:34:19 +02:00
}
2021-10-27 17:24:47 +02:00
void Bar::renderStatus()
2021-10-22 15:55:29 +02:00
{
2021-10-29 20:33:27 +02:00
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);
2021-10-27 17:24:47 +02:00
2021-10-29 20:33:27 +02:00
_x = start;
2023-04-09 17:59:56 +02:00
setColorScheme(colorInactive);
2021-10-29 20:33:27 +02:00
renderComponent(_statusCmp);
2023-04-09 17:59:56 +02:00
2021-10-22 15:55:29 +02:00
}
void Bar::setColorScheme(const ColorScheme& scheme, bool invert)
2021-10-27 17:24:47 +02:00
{
2021-10-29 20:33:27 +02:00
_colorScheme = invert
? ColorScheme {scheme.bg, scheme.fg}
: ColorScheme {scheme.fg, scheme.bg};
2021-10-27 17:24:47 +02:00
}
static void setColor(cairo_t* painter, const Color& color)
{
2021-10-29 23:23:35 +02:00
cairo_set_source_rgba(painter,
color.r/255.0, color.g/255.0, color.b/255.0, color.a/255.0);
}
2021-11-17 17:40:12 +01:00
void Bar::beginFg()
{
setColor(_painter, _colorScheme.fg);
}
void Bar::beginBg()
{
setColor(_painter, _colorScheme.bg);
}
2021-10-27 17:24:47 +02:00
void Bar::renderComponent(BarComponent& component)
2021-10-22 15:55:29 +02:00
{
2021-10-29 20:33:27 +02:00
pango_cairo_update_layout(_painter, component.pangoLayout.get());
auto size = component.width() + paddingX*2;
component.x = _x;
2021-10-27 17:24:47 +02:00
2021-10-29 20:33:27 +02:00
beginBg();
cairo_rectangle(_painter, _x, 0, size, _bufs->height);
cairo_fill(_painter);
cairo_move_to(_painter, _x+paddingX, paddingY);
2021-10-27 17:24:47 +02:00
2021-10-29 20:33:27 +02:00
beginFg();
pango_cairo_show_layout(_painter, component.pangoLayout.get());
_x += size;
2021-10-22 15:55:29 +02:00
}
2021-10-27 17:24:47 +02:00
BarComponent Bar::createComponent(const std::string &initial)
2021-10-22 15:34:19 +02:00
{
2021-10-29 20:33:27 +02:00
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;
2021-10-22 15:34:19 +02:00
}