style: pointer/reference goes to type, not name

This commit is contained in:
Raphael Robatsch 2021-10-29 20:19:09 +02:00
parent ebf06f932f
commit 30329b71f4
6 changed files with 105 additions and 103 deletions

View file

@ -11,13 +11,13 @@
#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)
[](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)
[](void* owner, wl_callback* cb, uint32_t)
{
static_cast<Bar*>(owner)->render();
wl_callback_destroy(cb);
@ -25,7 +25,7 @@ const wl_callback_listener Bar::_frameListener = {
};
struct Font {
PangoFontDescription *description;
PangoFontDescription* description;
int height {0};
};
static Font getFont()
@ -60,15 +60,13 @@ int BarComponent::width() const
pango_layout_get_size(pangoLayout.get(), &w, &h);
return PANGO_PIXELS(w);
}
void BarComponent::setText(const std::string &text)
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());
_text = std::make_unique<std::string>(text);
pango_layout_set_text(pangoLayout.get(), _text->c_str(), _text->size());
}
Bar::Bar(Monitor *mon)
Bar::Bar(Monitor* mon)
{
_mon = mon;
_pangoContext.reset(pango_font_map_create_context(pango_cairo_font_map_get_default()));
@ -84,7 +82,7 @@ Bar::Bar(Monitor *mon)
const wl_surface* Bar::surface() const { 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;
_surface.reset(wl_compositor_create_surface(compositor));
@ -118,8 +116,8 @@ void Bar::setTag(int tag, znet_tapesoftware_dwl_wm_monitor_v1_tag_state state, i
}
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::setTitle(const std::string& title) { _titleCmp.setText(title); }
void Bar::setStatus(const std::string& status) { _statusCmp.setText(status); }
void Bar::invalidate()
{
@ -133,7 +131,7 @@ void Bar::invalidate()
void Bar::click(int x, int, int btn)
{
Arg arg = {0};
Arg *argp = nullptr;
Arg* argp = nullptr;
int control = ClkNone;
if (x > _statusCmp.x) {
control = ClkStatusText;
@ -225,17 +223,20 @@ void Bar::renderStatus()
renderComponent(_statusCmp);
}
void Bar::setColorScheme(const ColorScheme &scheme, bool invert)
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); }
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)
void Bar::renderComponent(BarComponent& component)
{
pango_cairo_update_layout(_painter, component.pangoLayout.get());
auto size = component.width() + paddingX*2;

View file

@ -11,12 +11,12 @@
#include "shm_buffer.hpp"
class BarComponent {
std::unique_ptr<char[]> _text;
std::unique_ptr<std::string> _text;
public:
BarComponent();
explicit BarComponent(wl_unique_ptr<PangoLayout> layout);
int width() const;
void setText(const std::string &text);
void setText(const std::string& text);
wl_unique_ptr<PangoLayout> pangoLayout;
int x {0};
};
@ -36,7 +36,7 @@ class Bar {
wl_unique_ptr<wl_surface> _surface;
wl_unique_ptr<zwlr_layer_surface_v1> _layerSurface;
wl_unique_ptr<PangoContext> _pangoContext;
Monitor *_mon;
Monitor* _mon;
std::optional<ShmBuffer> _bufs;
std::vector<Tag> _tags;
BarComponent _layoutCmp, _titleCmp, _statusCmp;
@ -44,7 +44,7 @@ class Bar {
bool _invalid {false};
// only vaild during render()
cairo_t *_painter {nullptr};
cairo_t* _painter {nullptr};
int _x;
ColorScheme _colorScheme;
@ -54,22 +54,22 @@ class Bar {
void renderStatus();
// low-level rendering
void setColorScheme(const ColorScheme &scheme, bool invert=false);
void setColorScheme(const ColorScheme& scheme, bool invert = false);
void beginFg();
void beginBg();
void renderComponent(BarComponent &component);
BarComponent createComponent(const std::string &initial = {});
void renderComponent(BarComponent& component);
BarComponent createComponent(const std::string& initial = {});
public:
Bar(Monitor *mon);
const wl_surface* surface() const;
bool visible() const;
void show(wl_output *output);
void show(wl_output* output);
void hide();
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 std::string &title);
void setStatus(const std::string &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

@ -22,7 +22,7 @@ struct ColorScheme {
};
union Arg {
unsigned int ui;
const void *v;
const void* v;
};
struct Monitor;
@ -30,29 +30,31 @@ enum { ClkNone, ClkTagBar, ClkLayoutSymbol, ClkWinTitle, ClkStatusText };
struct Button {
int control;
int btn; // <linux/input-event-codes.h>
void (*func)(Monitor &mon, const Arg &arg);
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;
extern wl_display* display;
extern wl_compositor* compositor;
extern wl_shm* shm;
extern zwlr_layer_shell_v1* wlrLayerShell;
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);
void setlayout(Monitor &m, const Arg &arg);
void tag(Monitor &m, const Arg &arg);
void toggletag(Monitor &m, const Arg &arg);
void spawn(Monitor&, const Arg &arg);
[[noreturn]] void die(const char *why);
void view(Monitor& m, const Arg& arg);
void toggleview(Monitor& m, const Arg& arg);
void setlayout(Monitor& m, const Arg& arg);
void tag(Monitor& m, const Arg& arg);
void toggletag(Monitor& m, const Arg& arg);
void spawn(Monitor&, const Arg& arg);
[[noreturn]] void die(const char* why);
// wayland smart pointers
template<typename T>
struct wl_deleter;
#define WL_DELETER(type, fn) template<> struct wl_deleter<type> { void operator()(type *v) { if(v) fn(v); } }
#define WL_DELETER(type, fn) template<> struct wl_deleter<type> { \
void operator()(type* v) { if(v) fn(v); } \
}
template<typename T>
using wl_unique_ptr = std::unique_ptr<T, wl_deleter<T>>;

View file

@ -10,11 +10,11 @@ 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 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};
constexpr const char* termcmd[] = {"foot", nullptr};
constexpr Button buttons[] = {
{ ClkTagBar, BTN_LEFT, view, {0} },

View file

@ -1,14 +1,13 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <algorithm>
#include <cstdio>
#include <list>
#include <optional>
#include <vector>
#include <fcntl.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/signalfd.h>
@ -38,7 +37,7 @@ struct Monitor {
struct SeatPointer {
wl_unique_ptr<wl_pointer> wlPointer;
Bar *focusedBar;
Bar* focusedBar;
int x, y;
std::vector<int> btns;
};
@ -52,25 +51,25 @@ static void updatemon(Monitor &mon);
static void setupStatusFifo();
static void onStatus();
static void cleanup();
static void requireGlobal(const void *p, const char *name);
static void requireGlobal(const void* p, const char* name);
static void waylandFlush();
[[noreturn]] static void diesys(const char *why);
[[noreturn]] static void diesys(const char* why);
wl_display *display;
wl_compositor *compositor;
wl_shm *shm;
zwlr_layer_shell_v1 *wlrLayerShell;
znet_tapesoftware_dwl_wm_v1 *dwlWm;
wl_display* display;
wl_compositor* compositor;
wl_shm* shm;
zwlr_layer_shell_v1* wlrLayerShell;
znet_tapesoftware_dwl_wm_v1* dwlWm;
std::vector<std::string> tagNames;
std::vector<std::string> layoutNames;
static xdg_wm_base *xdgWmBase;
static zxdg_output_manager_v1 *xdgOutputManager;
static wl_surface *cursorSurface;
static wl_cursor_image *cursorImage;
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::list<Seat> seats;
static Monitor *selmon;
static Monitor* selmon;
static std::string lastStatus;
static std::string statusFifoName;
static int epoll {-1};
@ -79,27 +78,27 @@ static int statusFifoFd {-1};
static int statusFifoWriter {-1};
static bool quitting {false};
void view(Monitor &m, const Arg &arg)
void view(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(m.dwlMonitor.get(), arg.ui, 1);
}
void toggleview(Monitor &m, const Arg &arg)
void toggleview(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(m.dwlMonitor.get(), arg.ui, 0);
}
void setlayout(Monitor &m, const Arg &arg)
void setlayout(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_layout(m.dwlMonitor.get(), arg.ui);
}
void tag(Monitor &m, const Arg &arg)
void tag(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_client_tags(m.dwlMonitor.get(), 0, arg.ui);
}
void toggletag(Monitor &m, const Arg &arg)
void toggletag(Monitor& m, const Arg& arg)
{
znet_tapesoftware_dwl_wm_monitor_v1_set_client_tags(m.dwlMonitor.get(), 0xffffff, arg.ui);
}
void spawn(Monitor&, const Arg &arg)
void spawn(Monitor&, const Arg& arg)
{
if (fork() == 0) {
auto argv = static_cast<char* const*>(arg.v);
@ -112,7 +111,7 @@ void spawn(Monitor&, const Arg &arg)
}
static const struct xdg_wm_base_listener xdgWmBaseListener = {
[](void*, xdg_wm_base *sender, uint32_t serial) {
[](void*, xdg_wm_base* sender, uint32_t serial) {
xdg_wm_base_pong(sender, serial);
}
};
@ -121,7 +120,7 @@ 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) {
.name = [](void* mp, zxdg_output_v1* xdgOutput, const char* name) {
auto& monitor = *static_cast<Monitor*>(mp);
monitor.xdgName = name;
zxdg_output_v1_destroy(xdgOutput);
@ -131,19 +130,19 @@ static const struct zxdg_output_v1_listener xdgOutputListener = {
static Bar* barFromSurface(const wl_surface *surface)
{
auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor &mon) {
auto mon = std::find_if(begin(monitors), end(monitors), [surface](const Monitor& mon) {
return mon.bar && mon.bar->surface() == surface;
});
return mon != end(monitors) && mon->bar ? &*mon->bar : 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)
.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->focusedBar = barFromSurface(surface);
if (!cursorImage) {
auto cursorTheme = wl_cursor_theme_load(NULL, 24, shm);
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);
@ -152,16 +151,16 @@ static const struct wl_pointer_listener pointerListener = {
wl_pointer_set_cursor(pointer, serial, cursorSurface,
cursorImage->hotspot_x, cursorImage->hotspot_y);
},
.leave = [](void *sp, wl_pointer*, uint32_t serial, wl_surface*) {
.leave = [](void* sp, wl_pointer*, uint32_t serial, wl_surface*) {
auto& seat = *static_cast<Seat*>(sp);
seat.pointer->focusedBar = nullptr;
},
.motion = [](void *sp, wl_pointer*, uint32_t, wl_fixed_t x, wl_fixed_t y) {
.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) {
.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)) {
@ -170,8 +169,8 @@ static const struct wl_pointer_listener pointerListener = {
seat.pointer->btns.erase(it);
}
},
.axis = [](void *sp, wl_pointer*, uint32_t, uint32_t, wl_fixed_t) { },
.frame = [](void *sp, wl_pointer*) {
.axis = [](void* sp, wl_pointer*, uint32_t, uint32_t, wl_fixed_t) { },
.frame = [](void* sp, wl_pointer*) {
auto& seat = *static_cast<Seat*>(sp);
if (!seat.pointer->focusedBar) return;
for (auto btn : seat.pointer->btns) {
@ -185,7 +184,7 @@ static const struct wl_pointer_listener pointerListener = {
};
static const struct wl_seat_listener seatListener = {
.capabilities = [](void *sp, wl_seat*, uint32_t cap)
.capabilities = [](void* sp, wl_seat*, uint32_t cap)
{
auto& seat = *static_cast<Seat*>(sp);
auto hasPointer = cap & WL_SEAT_CAPABILITY_POINTER;
@ -201,16 +200,16 @@ static const struct wl_seat_listener seatListener = {
};
static const struct znet_tapesoftware_dwl_wm_v1_listener dwlWmListener = {
.tag = [](void*, znet_tapesoftware_dwl_wm_v1*, const char *name) {
.tag = [](void*, znet_tapesoftware_dwl_wm_v1*, const char* name) {
tagNames.push_back(name);
},
.layout = [](void*, znet_tapesoftware_dwl_wm_v1*, const char *name) {
.layout = [](void*, znet_tapesoftware_dwl_wm_v1*, const char* name) {
layoutNames.push_back(name);
},
};
static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwlWmMonitorListener {
.selected = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t selected) {
.selected = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t selected) {
auto mon = static_cast<Monitor*>(mv);
if (selected) {
selmon = mon;
@ -219,26 +218,26 @@ static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwlWmMonitorLis
}
mon->bar->setSelected(selected);
},
.tag = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t tag, uint32_t state, uint32_t numClients, int32_t focusedClient) {
.tag = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t tag, uint32_t state, uint32_t numClients, int32_t focusedClient) {
auto mon = static_cast<Monitor*>(mv);
mon->bar->setTag(tag, static_cast<znet_tapesoftware_dwl_wm_monitor_v1_tag_state>(state), numClients, focusedClient);
},
.layout = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t layout) {
.layout = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, uint32_t layout) {
auto mon = static_cast<Monitor*>(mv);
mon->bar->setLayout(layout);
},
.title = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*, const char *title) {
.title = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*, const char* title) {
auto mon = static_cast<Monitor*>(mv);
mon->bar->setTitle(title);
},
.frame = [](void *mv, znet_tapesoftware_dwl_wm_monitor_v1*) {
.frame = [](void* mv, znet_tapesoftware_dwl_wm_monitor_v1*) {
auto mon = static_cast<Monitor*>(mv);
mon->hasData = true;
updatemon(*mon);
}
};
static void setupMonitor(Monitor &monitor) {
static void setupMonitor(Monitor& monitor) {
monitor.dwlMonitor.reset(znet_tapesoftware_dwl_wm_v1_get_monitor(dwlWm, monitor.wlOutput.get()));
monitor.bar.emplace(&monitor);
monitor.bar->setStatus(lastStatus);
@ -247,7 +246,7 @@ static void setupMonitor(Monitor &monitor) {
znet_tapesoftware_dwl_wm_monitor_v1_add_listener(monitor.dwlMonitor.get(), &dwlWmMonitorListener, &monitor);
}
static void updatemon(Monitor &mon)
static void updatemon(Monitor& mon)
{
if (!mon.hasData) return;
if (mon.desiredVisibility) {
@ -317,7 +316,7 @@ const std::string argAll = "all";
const std::string argSelected = "selected";
template<typename T>
static void updateVisibility(const std::string &name, T updater)
static void updateVisibility(const std::string& name, T updater)
{
auto isCurrent = name == argSelected;
auto isAll = name == argAll;
@ -338,10 +337,10 @@ static LineBuffer<512> _statusBuffer;
static void onStatus()
{
_statusBuffer.readLines(
[](void *p, size_t size) {
[](void* p, size_t size) {
return read(statusFifoFd, p, size);
},
[](const char *buffer, size_t n) {
[](const char* buffer, size_t n) {
auto str = std::string {buffer, n};
if (str.rfind(prefixStatus, 0) == 0) {
lastStatus = str.substr(prefixStatus.size());
@ -362,18 +361,18 @@ static void onStatus()
}
struct HandleGlobalHelper {
wl_registry *registry;
wl_registry* registry;
uint32_t name;
const char *interface;
const char* interface;
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;
store = static_cast<T>(wl_registry_bind(registry, name, &iface, version));
return true;
}
};
static void registryHandleGlobal(void*, wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
static void registryHandleGlobal(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;
@ -401,7 +400,7 @@ static void registryHandleGlobal(void*, wl_registry *registry, uint32_t name, co
return;
}
}
static void registryHandleRemove(void*, wl_registry *registry, uint32_t name)
static void registryHandleRemove(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; });
@ -411,7 +410,7 @@ static const struct wl_registry_listener registry_listener = {
.global_remove = registryHandleRemove,
};
int main(int argc, char **argv)
int main(int argc, char* argv[])
{
int opt;
while ((opt = getopt(argc, argv, "chv")) != -1) {
@ -542,13 +541,13 @@ void waylandFlush()
}
}
void die(const char *why) {
void die(const char* why) {
fprintf(stderr, "%s\n", why);
cleanup();
exit(1);
}
void diesys(const char *why) {
void diesys(const char* why) {
perror(why);
cleanup();
exit(1);

View file

@ -12,11 +12,11 @@ class MemoryMapping {
size_t _size {0};
public:
MemoryMapping() { }
explicit MemoryMapping(void *ptr, size_t size) : _ptr(ptr), _size(size) { }
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(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;
@ -29,7 +29,7 @@ public:
// format is must be 32-bit
class ShmBuffer {
struct Buf {
uint8_t *data {nullptr};
uint8_t* data {nullptr};
wl_unique_ptr<wl_buffer> buffer;
};
std::array<Buf, 2> _buffers;