refactor: prefer static methods

pull/3/head
Alexis 2018-08-09 20:22:01 +02:00
parent 03132bd219
commit 23f48cb8b7
8 changed files with 152 additions and 148 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
.DS_Store .DS_Store
*~ *~
vgcore.* vgcore.*
/.vscode
*.swp *.swp
packagecache packagecache
/subprojects/fmt-4.1.0 /subprojects/fmt-4.1.0

14
.vscode/settings.json vendored
View File

@ -1,14 +0,0 @@
{
"files.associations": {
"atomic": "cpp",
"array": "cpp",
"hash_map": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"initializer_list": "cpp",
"string_view": "cpp",
"valarray": "cpp"
}
}

View File

@ -20,6 +20,19 @@ namespace waybar {
auto setWidth(int) -> void; auto setWidth(int) -> void;
auto toggle() -> void; auto toggle() -> void;
private: private:
static void _handleGeometry(void *data, struct wl_output *wl_output,
int32_t x, int32_t y, int32_t physical_width, int32_t physical_height,
int32_t subpixel, const char *make, const char *model, int32_t transform);
static void _handleMode(void *data, struct wl_output *wl_output,
uint32_t f, int32_t w, int32_t h, int32_t refresh);
static void _handleDone(void *data, struct wl_output *);
static void _handleScale(void *data, struct wl_output *wl_output,
int32_t factor);
static void _layerSurfaceHandleConfigure(void *data,
struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t width,
uint32_t height);
static void _layerSurfaceHandleClosed(void *data,
struct zwlr_layer_surface_v1 *surface);
auto _setupConfig() -> void; auto _setupConfig() -> void;
auto _setupWidgets() -> void; auto _setupWidgets() -> void;
auto _setupCss() -> void; auto _setupCss() -> void;

View File

@ -37,14 +37,14 @@ namespace waybar {
struct wl_seat *seat; struct wl_seat *seat;
util::ptr_vec<Bar> bars; util::ptr_vec<Bar> bars;
struct {
sigc::signal<void(int, int)> workspace_state;
sigc::signal<void(std::string)> focused_window_name;
} signals;
Client(int argc, char* argv[]); Client(int argc, char* argv[]);
void bind_interfaces(); void bind_interfaces();
auto setup_css(); auto setup_css();
int main(int argc, char* argv[]); int main(int argc, char* argv[]);
private:
static void _handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version);
static void _handle_global_remove(void *data,
struct wl_registry *registry, uint32_t name);
}; };
} }

View File

@ -12,13 +12,17 @@ namespace waybar::modules {
public: public:
Workspaces(waybar::Bar &bar); Workspaces(waybar::Bar &bar);
auto update() -> void; auto update() -> void;
void updateThread();
operator Gtk::Widget &(); operator Gtk::Widget &();
util::SleeperThread *thread;
private: private:
void _updateThread();
static void _handle_idle(void *data,
struct org_kde_kwin_idle_timeout *timer);
static void _handle_resume(void *data,
struct org_kde_kwin_idle_timeout *timer);
void _addWorkspace(Json::Value node); void _addWorkspace(Json::Value node);
Json::Value _getWorkspaces(); Json::Value _getWorkspaces();
Bar &_bar; Bar &_bar;
util::SleeperThread *_thread;
Gtk::Box *_box; Gtk::Box *_box;
std::unordered_map<int, Gtk::Button> _buttons; std::unordered_map<int, Gtk::Button> _buttons;
int _ipcSocketfd; int _ipcSocketfd;

View File

@ -7,75 +7,16 @@
#include "factory.hpp" #include "factory.hpp"
#include "util/chrono.hpp" #include "util/chrono.hpp"
static void handleGeometry(void *data, struct wl_output *wl_output, int32_t x,
int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel,
const char *make, const char *model, int32_t transform)
{
// Nothing here
}
static void handleMode(void *data, struct wl_output *wl_output, uint32_t f,
int32_t w, int32_t h, int32_t refresh)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
o->setWidth(w);
}
static void handleDone(void *data, struct wl_output *)
{
// Nothing here
}
static void handleScale(void *data, struct wl_output *wl_output,
int32_t factor)
{
// Nothing here
}
static const struct wl_output_listener outputListener = {
.geometry = handleGeometry,
.mode = handleMode,
.done = handleDone,
.scale = handleScale,
};
static void layerSurfaceHandleConfigure(
void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial,
uint32_t width, uint32_t height)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
o->window.show_all();
zwlr_layer_surface_v1_ack_configure(surface, serial);
if (o->client.height != height)
{
height = o->client.height;
std::cout << fmt::format("New Height: {}", height) << std::endl;
zwlr_layer_surface_v1_set_size(surface, width, height);
zwlr_layer_surface_v1_set_exclusive_zone(surface, o->visible ? height : 0);
wl_surface_commit(o->surface);
}
}
static void layerSurfaceHandleClosed(void *data,
struct zwlr_layer_surface_v1 *surface)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
zwlr_layer_surface_v1_destroy(o->layerSurface);
o->layerSurface = nullptr;
wl_surface_destroy(o->surface);
o->surface = nullptr;
o->window.close();
}
static const struct zwlr_layer_surface_v1_listener layerSurfaceListener = {
.configure = layerSurfaceHandleConfigure,
.closed = layerSurfaceHandleClosed,
};
waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output) waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output)
: client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL}, : client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL},
output(std::move(p_output)) output(std::move(p_output))
{ {
static const struct wl_output_listener outputListener = {
.geometry = _handleGeometry,
.mode = _handleMode,
.done = _handleDone,
.scale = _handleScale,
};
wl_output_add_listener(*output, &outputListener, this); wl_output_add_listener(*output, &outputListener, this);
window.set_title("waybar"); window.set_title("waybar");
window.set_decorated(false); window.set_decorated(false);
@ -93,11 +34,68 @@ waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output)
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_size(layerSurface, _width, client.height); zwlr_layer_surface_v1_set_size(layerSurface, _width, client.height);
static const struct zwlr_layer_surface_v1_listener layerSurfaceListener = {
.configure = _layerSurfaceHandleConfigure,
.closed = _layerSurfaceHandleClosed,
};
zwlr_layer_surface_v1_add_listener(layerSurface, &layerSurfaceListener, zwlr_layer_surface_v1_add_listener(layerSurface, &layerSurfaceListener,
this); this);
wl_surface_commit(surface); wl_surface_commit(surface);
} }
void waybar::Bar::_handleGeometry(void *data, struct wl_output *wl_output,
int32_t x, int32_t y, int32_t physical_width, int32_t physical_height,
int32_t subpixel, const char *make, const char *model, int32_t transform)
{
// Nothing here
}
void waybar::Bar::_handleMode(void *data, struct wl_output *wl_output,
uint32_t f, int32_t w, int32_t h, int32_t refresh)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
o->setWidth(w);
}
void waybar::Bar::_handleDone(void *data, struct wl_output *)
{
// Nothing here
}
void waybar::Bar::_handleScale(void *data, struct wl_output *wl_output,
int32_t factor)
{
// Nothing here
}
void waybar::Bar::_layerSurfaceHandleConfigure(
void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial,
uint32_t width, uint32_t height)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
o->window.show_all();
zwlr_layer_surface_v1_ack_configure(surface, serial);
if (o->client.height != height)
{
height = o->client.height;
std::cout << fmt::format("New Height: {}", height) << std::endl;
zwlr_layer_surface_v1_set_size(surface, width, height);
zwlr_layer_surface_v1_set_exclusive_zone(surface, o->visible ? height : 0);
wl_surface_commit(o->surface);
}
}
void waybar::Bar::_layerSurfaceHandleClosed(void *data,
struct zwlr_layer_surface_v1 *surface)
{
auto o = reinterpret_cast<waybar::Bar *>(data);
zwlr_layer_surface_v1_destroy(o->layerSurface);
o->layerSurface = nullptr;
wl_surface_destroy(o->surface);
o->surface = nullptr;
o->window.close();
}
auto waybar::Bar::setWidth(int width) -> void auto waybar::Bar::setWidth(int width) -> void
{ {
if (width == this->_width) return; if (width == this->_width) return;

View File

@ -1,37 +1,5 @@
#include "client.hpp" #include "client.hpp"
static void handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version)
{
auto o = reinterpret_cast<waybar::Client *>(data);
if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) {
o->layer_shell = (zwlr_layer_shell_v1 *)wl_registry_bind(registry, name,
&zwlr_layer_shell_v1_interface, version);
} else if (!strcmp(interface, wl_output_interface.name)) {
auto output = std::make_unique<struct wl_output *>();
*output = (struct wl_output *)wl_registry_bind(registry, name,
&wl_output_interface, version);
o->bars.emplace_back(*o, std::move(output));
} else if (!strcmp(interface, org_kde_kwin_idle_interface.name)) {
o->idle_manager = (org_kde_kwin_idle *)wl_registry_bind(registry, name,
&org_kde_kwin_idle_interface, version);
} else if (!strcmp(interface, wl_seat_interface.name)) {
o->seat = (struct wl_seat *)wl_registry_bind(registry, name,
&wl_seat_interface, version);
}
}
static void handle_global_remove(void *data,
struct wl_registry *registry, uint32_t name)
{
// TODO
}
static const struct wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = handle_global_remove,
};
waybar::Client::Client(int argc, char* argv[]) waybar::Client::Client(int argc, char* argv[])
: gtk_main(argc, argv), : gtk_main(argc, argv),
gdk_display(Gdk::Display::get_default()), gdk_display(Gdk::Display::get_default()),
@ -70,9 +38,40 @@ waybar::Client::Client(int argc, char* argv[])
} }
void waybar::Client::_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version)
{
auto o = reinterpret_cast<waybar::Client *>(data);
if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) {
o->layer_shell = (zwlr_layer_shell_v1 *)wl_registry_bind(registry, name,
&zwlr_layer_shell_v1_interface, version);
} else if (!strcmp(interface, wl_output_interface.name)) {
auto output = std::make_unique<struct wl_output *>();
*output = (struct wl_output *)wl_registry_bind(registry, name,
&wl_output_interface, version);
o->bars.emplace_back(*o, std::move(output));
} else if (!strcmp(interface, org_kde_kwin_idle_interface.name)) {
o->idle_manager = (org_kde_kwin_idle *)wl_registry_bind(registry, name,
&org_kde_kwin_idle_interface, version);
} else if (!strcmp(interface, wl_seat_interface.name)) {
o->seat = (struct wl_seat *)wl_registry_bind(registry, name,
&wl_seat_interface, version);
}
}
void waybar::Client::_handle_global_remove(void *data,
struct wl_registry *registry, uint32_t name)
{
// TODO
}
void waybar::Client::bind_interfaces() void waybar::Client::bind_interfaces()
{ {
registry = wl_display_get_registry(wlDisplay); registry = wl_display_get_registry(wlDisplay);
static const struct wl_registry_listener registry_listener = {
.global = _handle_global,
.global_remove = _handle_global_remove,
};
wl_registry_add_listener(registry, &registry_listener, this); wl_registry_add_listener(registry, &registry_listener, this);
wl_display_roundtrip(wlDisplay); wl_display_roundtrip(wlDisplay);
} }

View File

@ -1,28 +1,8 @@
#include "modules/workspaces.hpp" #include "modules/workspaces.hpp"
#include "ipc/client.hpp" #include "ipc/client.hpp"
static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) {
auto o = reinterpret_cast<waybar::modules::Workspaces *>(data);
if (o->thread) {
delete o->thread;
o->thread = nullptr;
}
}
static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) {
auto o = reinterpret_cast<waybar::modules::Workspaces *>(data);
if (!o->thread) {
o->updateThread();
}
}
static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
.idle = handle_idle,
.resumed = handle_resume,
};
waybar::modules::Workspaces::Workspaces(Bar &bar) waybar::modules::Workspaces::Workspaces(Bar &bar)
: thread(nullptr), _bar(bar), _box(Gtk::manage(new Gtk::Box)) : _bar(bar), _thread(nullptr), _box(Gtk::manage(new Gtk::Box))
{ {
_box->get_style_context()->add_class("workspaces"); _box->get_style_context()->add_class("workspaces");
std::string socketPath = get_socketpath(); std::string socketPath = get_socketpath();
@ -34,17 +14,13 @@ waybar::modules::Workspaces::Workspaces(Bar &bar)
_idle_timer = _idle_timer =
org_kde_kwin_idle_get_idle_timeout(_bar.client.idle_manager, org_kde_kwin_idle_get_idle_timeout(_bar.client.idle_manager,
_bar.client.seat, 10000); // 10 seconds _bar.client.seat, 10000); // 10 seconds
static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
.idle = _handle_idle,
.resumed = _handle_resume,
};
org_kde_kwin_idle_timeout_add_listener(_idle_timer, org_kde_kwin_idle_timeout_add_listener(_idle_timer,
&idle_timer_listener, this); &idle_timer_listener, this);
updateThread(); _updateThread();
}
void waybar::modules::Workspaces::updateThread()
{
thread = new waybar::util::SleeperThread([this] {
update();
thread->sleep_for(waybar::chrono::milliseconds(250));
});
} }
auto waybar::modules::Workspaces::update() -> void auto waybar::modules::Workspaces::update() -> void
@ -79,6 +55,33 @@ auto waybar::modules::Workspaces::update() -> void
} }
} }
void waybar::modules::Workspaces::_updateThread()
{
_thread = new waybar::util::SleeperThread([this] {
update();
_thread->sleep_for(waybar::chrono::milliseconds(250));
});
}
void waybar::modules::Workspaces::_handle_idle(void *data,
struct org_kde_kwin_idle_timeout *timer) {
auto o = reinterpret_cast<waybar::modules::Workspaces *>(data);
if (o->_thread) {
delete o->_thread;
o->_thread = nullptr;
std::cout << "IDLE" << std::endl;
}
}
void waybar::modules::Workspaces::_handle_resume(void *data,
struct org_kde_kwin_idle_timeout *timer) {
auto o = reinterpret_cast<waybar::modules::Workspaces *>(data);
if (!o->_thread) {
o->_updateThread();
std::cout << "RESUME" << std::endl;
}
}
void waybar::modules::Workspaces::_addWorkspace(Json::Value node) void waybar::modules::Workspaces::_addWorkspace(Json::Value node)
{ {
auto pair = _buttons.emplace(node["num"].asInt(), node["name"].asString()); auto pair = _buttons.emplace(node["num"].asInt(), node["name"].asString());