From ee2e70931708460d4a2fe3f0ebab9fa4d8699ea9 Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Wed, 17 Nov 2021 17:40:12 +0100 Subject: [PATCH] code style cleanup --- .editorconfig | 5 +++ contrib/ipc.patch | 26 ++++++------- src/bar.cpp | 94 +++++++++++++++++++++++++++++++++++----------- src/common.hpp | 10 +++-- src/main.cpp | 27 +++++++------ src/shm_buffer.cpp | 26 ++++++++++--- src/shm_buffer.hpp | 2 +- 7 files changed, 133 insertions(+), 57 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5c4a037 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*] +indent_style = tab +indent_brace_style = K&R diff --git a/contrib/ipc.patch b/contrib/ipc.patch index fd434e0..0b11123 100644 --- a/contrib/ipc.patch +++ b/contrib/ipc.patch @@ -193,7 +193,7 @@ index 0000000..390f5a1 + + diff --git a/src/common.hpp b/src/common.hpp -index 0d900f0..5f9c753 100644 +index aed4480..12a3e2e 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -10,6 +10,7 @@ @@ -250,7 +250,7 @@ index 40a8c95..a9560cb 100644 { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, }; diff --git a/src/main.cpp b/src/main.cpp -index 0aeadb2..6c5c992 100644 +index 6198d8b..9e7549a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,6 @@ @@ -325,8 +325,8 @@ index 0aeadb2..6c5c992 100644 void spawn(Monitor&, const Arg& arg) { if (fork() == 0) { -@@ -188,11 +209,62 @@ static const struct wl_seat_listener seatListener = { - .name = [](void*, wl_seat*, const char *name) { } +@@ -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 = { @@ -388,7 +388,7 @@ index 0aeadb2..6c5c992 100644 } void updatemon(Monitor& mon) -@@ -216,6 +288,7 @@ void onReady() +@@ -219,6 +291,7 @@ void onReady() requireGlobal(shm, "wl_shm"); requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1"); requireGlobal(xdgOutputManager, "zxdg_output_manager_v1"); @@ -396,7 +396,7 @@ index 0aeadb2..6c5c992 100644 setupStatusFifo(); 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) { setupMonitor(output.first, output.second); } @@ -404,14 +404,14 @@ index 0aeadb2..6c5c992 100644 } 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() -{ -- auto res = _stdinBuffer.readLines( +- 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) { @@ -471,7 +471,7 @@ index 0aeadb2..6c5c992 100644 const std::string prefixStatus = "status "; const std::string prefixShow = "show "; 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); return; } @@ -479,10 +479,10 @@ index 0aeadb2..6c5c992 100644 + znet_tapesoftware_dwl_wm_v1_add_listener(dwlWm, &dwlWmListener, nullptr); + 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 {wlSeat}}); 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, .events = POLLIN, }); @@ -493,7 +493,7 @@ index 0aeadb2..6c5c992 100644 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) { 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; waylandFlush(); } diff --git a/src/bar.cpp b/src/bar.cpp index 4f28a36..fab5a8f 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -31,15 +31,25 @@ struct Font { static Font getFont() { 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); - if (!fontDesc) die("pango_font_description_from_string"); + if (!fontDesc) { + die("pango_font_description_from_string"); + } 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); - 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()); - if (!metrics) die("pango_font_get_metrics"); + if (!metrics) { + die("pango_font_get_metrics"); + } auto res = Font {}; res.description = fontDesc; @@ -53,13 +63,18 @@ static Font getFont() static Font barfont = getFont(); BarComponent::BarComponent() { } -BarComponent::BarComponent(wl_unique_ptr layout) : pangoLayout {std::move(layout)} {} +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); @@ -69,7 +84,9 @@ void BarComponent::setText(const std::string& text) Bar::Bar() { _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) { _tags.push_back({ TagState::None, 0, 0, createComponent(tagName) }); } @@ -78,12 +95,21 @@ Bar::Bar() _statusCmp = createComponent(); } -const wl_surface* Bar::surface() const { return _surface.get(); } -bool Bar::visible() const { return _surface.get(); } +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; + 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")); @@ -100,7 +126,9 @@ void Bar::show(wl_output* output) void Bar::hide() { - if (!visible()) return; + if (!visible()) { + return; + } _layerSurface.reset(); _surface.reset(); _bufs.reset(); @@ -113,14 +141,29 @@ void Bar::setTag(int tag, int state, int numClients, int focusedClient) 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::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; + if (_invalid || !visible()) { + return; + } _invalid = true; auto frame = wl_surface_frame(_surface.get()); wl_callback_add_listener(frame, &_frameListener, this); @@ -146,8 +189,7 @@ void Bar::click(Monitor* mon, int x, int, int btn) break; } } - for (auto i = 0u; i < sizeof(buttons)/sizeof(buttons[0]); i++) { - const auto& button = buttons[i]; + for (const auto& button : buttons) { if (button.control == control && button.btn == btn) { button.func(*mon, *(argp ? argp : &button.arg)); 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) { zwlr_layer_surface_v1_ack_configure(_layerSurface.get(), serial); - if (width == _bufs->width && height == _bufs->height) + if (width == _bufs->width && height == _bufs->height) { return; + } _bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888); render(); } void Bar::render() { - if (!_bufs) + if (!_bufs) { return; + } auto img = wl_unique_ptr {cairo_image_surface_create_for_data( _bufs->data(), CAIRO_FORMAT_ARGB32, @@ -236,8 +280,14 @@ 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::beginFg() +{ + setColor(_painter, _colorScheme.fg); +} +void Bar::beginBg() +{ + setColor(_painter, _colorScheme.bg); +} void Bar::renderComponent(BarComponent& component) { diff --git a/src/common.hpp b/src/common.hpp index 0d900f0..aed4480 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -26,7 +26,7 @@ union Arg { struct Monitor; enum TagState { None, Active = 0x01, Urgent = 0x02 }; -enum { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText }; +enum Control { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText }; struct Button { int control; int btn; // @@ -46,13 +46,13 @@ void setCloexec(int fd); // wayland smart pointers template -struct wl_deleter; -#define WL_DELETER(type, fn) template<> struct wl_deleter { \ +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>; +using wl_unique_ptr = std::unique_ptr>; WL_DELETER(wl_buffer, wl_buffer_destroy); 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(PangoLayout, g_object_unref); + +#undef WL_DELETER diff --git a/src/main.cpp b/src/main.cpp index 0aeadb2..6198d8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,8 +160,9 @@ static const struct wl_pointer_listener pointerListener = { .frame = [](void* sp, wl_pointer*) { auto& seat = *static_cast(sp); auto mon = seat.pointer->focusedMonitor; - if (!mon) + if (!mon) { return; + } for (auto btn : seat.pointer->btns) { 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(); } }, - .name = [](void*, wl_seat*, const char *name) { } + .name = [](void*, wl_seat*, const char* name) { } }; void setupMonitor(uint32_t name, wl_output* output) { @@ -197,7 +198,9 @@ void setupMonitor(uint32_t name, wl_output* output) { void updatemon(Monitor& mon) { - if (!mon.hasData) return; + if (!mon.hasData) { + return; + } if (mon.desiredVisibility) { if (mon.bar.visible()) { mon.bar.invalidate(); @@ -256,10 +259,10 @@ void setupStatusFifo() } } -static LineBuffer<512> _stdinBuffer; +static LineBuffer<512> stdinBuffer; static void onStdin() { - auto res = _stdinBuffer.readLines( + 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) { @@ -323,10 +326,10 @@ const std::string prefixToggle = "toggle "; const std::string argAll = "all"; const std::string argSelected = "selected"; -static LineBuffer<512> _statusBuffer; +static LineBuffer<512> statusBuffer; void onStatus() { - _statusBuffer.readLines( + statusBuffer.readLines( [](void* p, size_t size) { return read(statusFifoFd, p, size); }, @@ -372,7 +375,9 @@ struct HandleGlobalHelper { template 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(wl_registry_bind(registry, name, &iface, version)); 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); 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 {wlSeat}}); wl_seat_add_listener(wlSeat, &seatListener, &seat); 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) { setupMonitor(name, output); } else { @@ -530,7 +535,7 @@ int main(int argc, char* argv[]) cleanup(); } -void requireGlobal(const void *p, const char *name) +void requireGlobal(const void* p, const char* name) { if (p) return; fprintf(stderr, "Wayland compositor does not export required global %s, aborting.\n", name); diff --git a/src/shm_buffer.cpp b/src/shm_buffer.cpp index 18a28f3..59baf6f 100644 --- a/src/shm_buffer.cpp +++ b/src/shm_buffer.cpp @@ -27,6 +27,9 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format) } 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= 0) { diff --git a/src/shm_buffer.hpp b/src/shm_buffer.hpp index 4b41644..f6927dd 100644 --- a/src/shm_buffer.hpp +++ b/src/shm_buffer.hpp @@ -36,7 +36,7 @@ class ShmBuffer { int _current {0}; MemoryMapping _mapping; public: - uint32_t width, height, stride; + const uint32_t width, height, stride; explicit ShmBuffer(int width, int height, wl_shm_format format); uint8_t* data();