code style cleanup

This commit is contained in:
Raphael Robatsch 2021-11-17 17:40:12 +01:00
parent 155a028676
commit ee2e709317
7 changed files with 133 additions and 57 deletions

5
.editorconfig Normal file
View file

@ -0,0 +1,5 @@
root = true
[*]
indent_style = tab
indent_brace_style = K&R

View file

@ -193,7 +193,7 @@ index 0000000..390f5a1
+ </interface> + </interface>
+</protocol> +</protocol>
diff --git a/src/common.hpp b/src/common.hpp diff --git a/src/common.hpp b/src/common.hpp
index 0d900f0..5f9c753 100644 index aed4480..12a3e2e 100644
--- a/src/common.hpp --- a/src/common.hpp
+++ b/src/common.hpp +++ b/src/common.hpp
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
@ -250,7 +250,7 @@ index 40a8c95..a9560cb 100644
{ ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} },
}; };
diff --git a/src/main.cpp b/src/main.cpp diff --git a/src/main.cpp b/src/main.cpp
index 0aeadb2..6c5c992 100644 index 6198d8b..9e7549a 100644
--- a/src/main.cpp --- a/src/main.cpp
+++ b/src/main.cpp +++ b/src/main.cpp
@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
@ -325,8 +325,8 @@ index 0aeadb2..6c5c992 100644
void spawn(Monitor&, const Arg& arg) void spawn(Monitor&, const Arg& arg)
{ {
if (fork() == 0) { if (fork() == 0) {
@@ -188,11 +209,62 @@ static const struct wl_seat_listener seatListener = { @@ -189,11 +210,62 @@ static const struct wl_seat_listener seatListener = {
.name = [](void*, wl_seat*, const char *name) { } .name = [](void*, wl_seat*, const char* name) { }
}; };
+static const struct znet_tapesoftware_dwl_wm_v1_listener dwlWmListener = { +static const struct znet_tapesoftware_dwl_wm_v1_listener dwlWmListener = {
@ -388,7 +388,7 @@ index 0aeadb2..6c5c992 100644
} }
void updatemon(Monitor& mon) void updatemon(Monitor& mon)
@@ -216,6 +288,7 @@ void onReady() @@ -219,6 +291,7 @@ void onReady()
requireGlobal(shm, "wl_shm"); requireGlobal(shm, "wl_shm");
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1"); requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
requireGlobal(xdgOutputManager, "zxdg_output_manager_v1"); requireGlobal(xdgOutputManager, "zxdg_output_manager_v1");
@ -396,7 +396,7 @@ index 0aeadb2..6c5c992 100644
setupStatusFifo(); setupStatusFifo();
wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc. wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc.
@@ -223,7 +296,6 @@ void onReady() @@ -226,7 +299,6 @@ void onReady()
for (auto output : uninitializedOutputs) { for (auto output : uninitializedOutputs) {
setupMonitor(output.first, output.second); setupMonitor(output.first, output.second);
} }
@ -404,14 +404,14 @@ index 0aeadb2..6c5c992 100644
} }
void setupStatusFifo() void setupStatusFifo()
@@ -256,66 +328,6 @@ void setupStatusFifo() @@ -259,66 +331,6 @@ void setupStatusFifo()
} }
} }
-static LineBuffer<512> _stdinBuffer; -static LineBuffer<512> stdinBuffer;
-static void onStdin() -static void onStdin()
-{ -{
- auto res = _stdinBuffer.readLines( - auto res = stdinBuffer.readLines(
- [](void* p, size_t size) { return read(0, p, size); }, - [](void* p, size_t size) { return read(0, p, size); },
- [](char* p, size_t size) { handleStdin({p, size}); }); - [](char* p, size_t size) { handleStdin({p, size}); });
- if (res == 0) { - if (res == 0) {
@ -471,7 +471,7 @@ index 0aeadb2..6c5c992 100644
const std::string prefixStatus = "status "; const std::string prefixStatus = "status ";
const std::string prefixShow = "show "; const std::string prefixShow = "show ";
const std::string prefixHide = "hide "; const std::string prefixHide = "hide ";
@@ -388,6 +400,10 @@ void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interf @@ -393,6 +405,10 @@ void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interf
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr); xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
return; return;
} }
@ -479,10 +479,10 @@ index 0aeadb2..6c5c992 100644
+ znet_tapesoftware_dwl_wm_v1_add_listener(dwlWm, &dwlWmListener, nullptr); + znet_tapesoftware_dwl_wm_v1_add_listener(dwlWm, &dwlWmListener, nullptr);
+ return; + return;
+ } + }
if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) { if (wl_seat* wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) {
auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}}); auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}});
wl_seat_add_listener(wlSeat, &seatListener, &seat); wl_seat_add_listener(wlSeat, &seatListener, &seat);
@@ -489,10 +505,6 @@ int main(int argc, char* argv[]) @@ -494,10 +510,6 @@ int main(int argc, char* argv[])
.fd = displayFd, .fd = displayFd,
.events = POLLIN, .events = POLLIN,
}); });
@ -493,7 +493,7 @@ index 0aeadb2..6c5c992 100644
if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) { if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) {
diesys("fcntl F_SETFL"); diesys("fcntl F_SETFL");
} }
@@ -517,8 +529,6 @@ int main(int argc, char* argv[]) @@ -522,8 +534,6 @@ int main(int argc, char* argv[])
ev.events = POLLIN; ev.events = POLLIN;
waylandFlush(); waylandFlush();
} }

View file

@ -31,15 +31,25 @@ struct Font {
static Font getFont() static Font getFont()
{ {
auto fontMap = pango_cairo_font_map_get_default(); auto fontMap = pango_cairo_font_map_get_default();
if (!fontMap) die("pango_cairo_font_map_get_default"); if (!fontMap) {
die("pango_cairo_font_map_get_default");
}
auto fontDesc = pango_font_description_from_string(font); auto fontDesc = pango_font_description_from_string(font);
if (!fontDesc) die("pango_font_description_from_string"); if (!fontDesc) {
die("pango_font_description_from_string");
}
auto tempContext = pango_font_map_create_context(fontMap); auto tempContext = pango_font_map_create_context(fontMap);
if (!tempContext) die("pango_font_map_create_context"); if (!tempContext) {
die("pango_font_map_create_context");
}
auto font = pango_font_map_load_font(fontMap, tempContext, fontDesc); auto font = pango_font_map_load_font(fontMap, tempContext, fontDesc);
if (!font) die("pango_font_map_load_font"); if (!font) {
die("pango_font_map_load_font");
}
auto metrics = pango_font_get_metrics(font, pango_language_get_default()); auto metrics = pango_font_get_metrics(font, pango_language_get_default());
if (!metrics) die("pango_font_get_metrics"); if (!metrics) {
die("pango_font_get_metrics");
}
auto res = Font {}; auto res = Font {};
res.description = fontDesc; res.description = fontDesc;
@ -53,13 +63,18 @@ static Font getFont()
static Font barfont = getFont(); static Font barfont = getFont();
BarComponent::BarComponent() { } BarComponent::BarComponent() { }
BarComponent::BarComponent(wl_unique_ptr<PangoLayout> layout) : pangoLayout {std::move(layout)} {} BarComponent::BarComponent(wl_unique_ptr<PangoLayout> layout)
: pangoLayout {std::move(layout)}
{
}
int BarComponent::width() const int BarComponent::width() const
{ {
int w, h; int w, h;
pango_layout_get_size(pangoLayout.get(), &w, &h); pango_layout_get_size(pangoLayout.get(), &w, &h);
return PANGO_PIXELS(w); return PANGO_PIXELS(w);
} }
void BarComponent::setText(const std::string& text) void BarComponent::setText(const std::string& text)
{ {
_text = std::make_unique<std::string>(text); _text = std::make_unique<std::string>(text);
@ -69,7 +84,9 @@ void BarComponent::setText(const std::string& text)
Bar::Bar() Bar::Bar()
{ {
_pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default())); _pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default()));
if (!_pangoContext) die("pango_font_map_create_context"); if (!_pangoContext) {
die("pango_font_map_create_context");
}
for (const auto& tagName : tagNames) { for (const auto& tagName : tagNames) {
_tags.push_back({ TagState::None, 0, 0, createComponent(tagName) }); _tags.push_back({ TagState::None, 0, 0, createComponent(tagName) });
} }
@ -78,12 +95,21 @@ Bar::Bar()
_statusCmp = createComponent(); _statusCmp = createComponent();
} }
const wl_surface* Bar::surface() const { return _surface.get(); } const wl_surface* Bar::surface() const
bool Bar::visible() const { return _surface.get(); } {
return _surface.get();
}
bool Bar::visible() const
{
return _surface.get();
}
void Bar::show(wl_output* output) void Bar::show(wl_output* output)
{ {
if (visible()) return; if (visible()) {
return;
}
_surface.reset(wl_compositor_create_surface(compositor)); _surface.reset(wl_compositor_create_surface(compositor));
_layerSurface.reset(zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell, _layerSurface.reset(zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell,
_surface.get(), output, ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "net.tapesoftware.Somebar")); _surface.get(), output, ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "net.tapesoftware.Somebar"));
@ -100,7 +126,9 @@ void Bar::show(wl_output* output)
void Bar::hide() void Bar::hide()
{ {
if (!visible()) return; if (!visible()) {
return;
}
_layerSurface.reset(); _layerSurface.reset();
_surface.reset(); _surface.reset();
_bufs.reset(); _bufs.reset();
@ -113,14 +141,29 @@ void Bar::setTag(int tag, int state, int numClients, int focusedClient)
t.numClients = numClients; t.numClients = numClients;
t.focusedClient = focusedClient; t.focusedClient = focusedClient;
} }
void Bar::setSelected(bool selected) { _selected = selected; }
void Bar::setLayout(const std::string& layout) { _layoutCmp.setText(layout); } void Bar::setSelected(bool selected)
void Bar::setTitle(const std::string& title) { _titleCmp.setText(title); } {
void Bar::setStatus(const std::string& status) { _statusCmp.setText(status); } _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() void Bar::invalidate()
{ {
if (_invalid || !visible()) return; if (_invalid || !visible()) {
return;
}
_invalid = true; _invalid = true;
auto frame = wl_surface_frame(_surface.get()); auto frame = wl_surface_frame(_surface.get());
wl_callback_add_listener(frame, &_frameListener, this); wl_callback_add_listener(frame, &_frameListener, this);
@ -146,8 +189,7 @@ void Bar::click(Monitor* mon, int x, int, int btn)
break; break;
} }
} }
for (auto i = 0u; i < sizeof(buttons)/sizeof(buttons[0]); i++) { for (const auto& button : buttons) {
const auto& button = buttons[i];
if (button.control == control && button.btn == btn) { if (button.control == control && button.btn == btn) {
button.func(*mon, *(argp ? argp : &button.arg)); button.func(*mon, *(argp ? argp : &button.arg));
return; return;
@ -158,16 +200,18 @@ void Bar::click(Monitor* mon, int x, int, int btn)
void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height) void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height)
{ {
zwlr_layer_surface_v1_ack_configure(_layerSurface.get(), serial); zwlr_layer_surface_v1_ack_configure(_layerSurface.get(), serial);
if (width == _bufs->width && height == _bufs->height) if (width == _bufs->width && height == _bufs->height) {
return; return;
}
_bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888); _bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888);
render(); render();
} }
void Bar::render() void Bar::render()
{ {
if (!_bufs) if (!_bufs) {
return; return;
}
auto img = wl_unique_ptr<cairo_surface_t> {cairo_image_surface_create_for_data( auto img = wl_unique_ptr<cairo_surface_t> {cairo_image_surface_create_for_data(
_bufs->data(), _bufs->data(),
CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_ARGB32,
@ -236,8 +280,14 @@ static void setColor(cairo_t* painter, const Color& color)
cairo_set_source_rgba(painter, cairo_set_source_rgba(painter,
color.r/255.0, color.g/255.0, color.b/255.0, color.a/255.0); 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::beginFg()
void Bar::beginBg() { setColor(_painter, _colorScheme.bg); } {
setColor(_painter, _colorScheme.fg);
}
void Bar::beginBg()
{
setColor(_painter, _colorScheme.bg);
}
void Bar::renderComponent(BarComponent& component) void Bar::renderComponent(BarComponent& component)
{ {

View file

@ -26,7 +26,7 @@ union Arg {
struct Monitor; struct Monitor;
enum TagState { None, Active = 0x01, Urgent = 0x02 }; enum TagState { None, Active = 0x01, Urgent = 0x02 };
enum { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText }; enum Control { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText };
struct Button { struct Button {
int control; int control;
int btn; // <linux/input-event-codes.h> int btn; // <linux/input-event-codes.h>
@ -46,13 +46,13 @@ void setCloexec(int fd);
// wayland smart pointers // wayland smart pointers
template<typename T> template<typename T>
struct wl_deleter; struct WlDeleter;
#define WL_DELETER(type, fn) template<> struct wl_deleter<type> { \ #define WL_DELETER(type, fn) template<> struct WlDeleter<type> { \
void operator()(type* v) { if(v) fn(v); } \ void operator()(type* v) { if(v) fn(v); } \
} }
template<typename T> template<typename T>
using wl_unique_ptr = std::unique_ptr<T, wl_deleter<T>>; using wl_unique_ptr = std::unique_ptr<T, WlDeleter<T>>;
WL_DELETER(wl_buffer, wl_buffer_destroy); WL_DELETER(wl_buffer, wl_buffer_destroy);
WL_DELETER(wl_output, wl_output_release); WL_DELETER(wl_output, wl_output_release);
@ -66,3 +66,5 @@ WL_DELETER(cairo_surface_t, cairo_surface_destroy);
WL_DELETER(PangoContext, g_object_unref); WL_DELETER(PangoContext, g_object_unref);
WL_DELETER(PangoLayout, g_object_unref); WL_DELETER(PangoLayout, g_object_unref);
#undef WL_DELETER

View file

@ -160,8 +160,9 @@ static const struct wl_pointer_listener pointerListener = {
.frame = [](void* sp, wl_pointer*) { .frame = [](void* sp, wl_pointer*) {
auto& seat = *static_cast<Seat*>(sp); auto& seat = *static_cast<Seat*>(sp);
auto mon = seat.pointer->focusedMonitor; auto mon = seat.pointer->focusedMonitor;
if (!mon) if (!mon) {
return; return;
}
for (auto btn : seat.pointer->btns) { for (auto btn : seat.pointer->btns) {
mon->bar.click(mon, seat.pointer->x, seat.pointer->y, btn); mon->bar.click(mon, seat.pointer->x, seat.pointer->y, btn);
} }
@ -185,7 +186,7 @@ static const struct wl_seat_listener seatListener = {
seat.pointer.reset(); seat.pointer.reset();
} }
}, },
.name = [](void*, wl_seat*, const char *name) { } .name = [](void*, wl_seat*, const char* name) { }
}; };
void setupMonitor(uint32_t name, wl_output* output) { void setupMonitor(uint32_t name, wl_output* output) {
@ -197,7 +198,9 @@ void setupMonitor(uint32_t name, wl_output* output) {
void updatemon(Monitor& mon) void updatemon(Monitor& mon)
{ {
if (!mon.hasData) return; if (!mon.hasData) {
return;
}
if (mon.desiredVisibility) { if (mon.desiredVisibility) {
if (mon.bar.visible()) { if (mon.bar.visible()) {
mon.bar.invalidate(); mon.bar.invalidate();
@ -256,10 +259,10 @@ void setupStatusFifo()
} }
} }
static LineBuffer<512> _stdinBuffer; static LineBuffer<512> stdinBuffer;
static void onStdin() static void onStdin()
{ {
auto res = _stdinBuffer.readLines( auto res = stdinBuffer.readLines(
[](void* p, size_t size) { return read(0, p, size); }, [](void* p, size_t size) { return read(0, p, size); },
[](char* p, size_t size) { handleStdin({p, size}); }); [](char* p, size_t size) { handleStdin({p, size}); });
if (res == 0) { if (res == 0) {
@ -323,10 +326,10 @@ const std::string prefixToggle = "toggle ";
const std::string argAll = "all"; const std::string argAll = "all";
const std::string argSelected = "selected"; const std::string argSelected = "selected";
static LineBuffer<512> _statusBuffer; static LineBuffer<512> statusBuffer;
void onStatus() void onStatus()
{ {
_statusBuffer.readLines( statusBuffer.readLines(
[](void* p, size_t size) { [](void* p, size_t size) {
return read(statusFifoFd, p, size); return read(statusFifoFd, p, size);
}, },
@ -372,7 +375,9 @@ struct HandleGlobalHelper {
template<typename T> template<typename T>
bool handle(T& store, const wl_interface& iface, int version) { bool handle(T& store, const wl_interface& iface, int version) {
if (strcmp(interface, iface.name)) return false; if (strcmp(interface, iface.name)) {
return false;
}
store = static_cast<T>(wl_registry_bind(registry, name, &iface, version)); store = static_cast<T>(wl_registry_bind(registry, name, &iface, version));
return true; return true;
} }
@ -388,12 +393,12 @@ void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interf
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr); xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
return; return;
} }
if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) { if (wl_seat* wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) {
auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}}); auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}});
wl_seat_add_listener(wlSeat, &seatListener, &seat); wl_seat_add_listener(wlSeat, &seatListener, &seat);
return; return;
} }
if (wl_output *output; reg.handle(output, wl_output_interface, 1)) { if (wl_output* output; reg.handle(output, wl_output_interface, 1)) {
if (ready) { if (ready) {
setupMonitor(name, output); setupMonitor(name, output);
} else { } else {
@ -530,7 +535,7 @@ int main(int argc, char* argv[])
cleanup(); cleanup();
} }
void requireGlobal(const void *p, const char *name) void requireGlobal(const void* p, const char* name)
{ {
if (p) return; if (p) return;
fprintf(stderr, "Wayland compositor does not export required global %s, aborting.\n", name); fprintf(stderr, "Wayland compositor does not export required global %s, aborting.\n", name);

View file

@ -27,6 +27,9 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format)
} }
auto pool = wl_shm_create_pool(shm, fd, totalSize); 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)); 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}; _mapping = MemoryMapping {ptr, totalSize};
close(fd); close(fd);
for (auto i=0; i<n; i++) { for (auto i=0; i<n; i++) {
@ -39,22 +42,33 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format)
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
} }
uint8_t* ShmBuffer::data() { return _buffers[_current].data; } uint8_t* ShmBuffer::data()
wl_buffer* ShmBuffer::buffer() { return _buffers[_current].buffer.get(); } {
void ShmBuffer::flip() { _current = 1-_current; } return _buffers[_current].data;
}
wl_buffer* ShmBuffer::buffer()
{
return _buffers[_current].buffer.get();
}
void ShmBuffer::flip()
{
_current = 1-_current;
}
#if defined(__linux__) #if defined(__linux__)
static int createAnonShm() { int createAnonShm() {
return memfd_create("wl_shm", MFD_CLOEXEC); return memfd_create("wl_shm", MFD_CLOEXEC);
} }
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
static int createAnonShm() { int createAnonShm() {
auto fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600); auto fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
setCloexec(fd); setCloexec(fd);
return fd; return fd;
} }
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
static int createAnonShm() { int createAnonShm() {
char name[] = "/wl_shm-XXXXXX"; char name[] = "/wl_shm-XXXXXX";
auto fd = shm_mkstemp(name); auto fd = shm_mkstemp(name);
if (fd >= 0) { if (fd >= 0) {

View file

@ -36,7 +36,7 @@ class ShmBuffer {
int _current {0}; int _current {0};
MemoryMapping _mapping; MemoryMapping _mapping;
public: public:
uint32_t width, height, stride; const uint32_t width, height, stride;
explicit ShmBuffer(int width, int height, wl_shm_format format); explicit ShmBuffer(int width, int height, wl_shm_format format);
uint8_t* data(); uint8_t* data();