feat: add idle protocol to avoid update workspace when idle

This commit is contained in:
Alexis 2018-08-09 01:10:07 +02:00
parent 193bd8a79a
commit c1e2735314
4 changed files with 43 additions and 6 deletions

View File

@ -9,6 +9,7 @@
#include <wayland-client.h> #include <wayland-client.h>
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "idle-client-protocol.h"
#include "util/ptr_vec.hpp" #include "util/ptr_vec.hpp"
@ -28,6 +29,8 @@ namespace waybar {
struct wl_display *wlDisplay; struct wl_display *wlDisplay;
struct wl_registry *registry; struct wl_registry *registry;
struct zwlr_layer_shell_v1 *layer_shell; struct zwlr_layer_shell_v1 *layer_shell;
struct org_kde_kwin_idle *idle_manager;
struct wl_seat *seat;
util::ptr_vec<Bar> bars; util::ptr_vec<Bar> bars;
struct { struct {

View File

@ -12,16 +12,18 @@ namespace waybar::modules {
public: public:
WorkspaceSelector(waybar::Bar &bar); WorkspaceSelector(waybar::Bar &bar);
auto update() -> void; auto update() -> void;
void updateThread();
operator Gtk::Widget &(); operator Gtk::Widget &();
util::SleeperThread *thread;
private: private:
void _addWorkspace(Json::Value node); void _addWorkspace(Json::Value node);
Json::Value _getWorkspaces(); Json::Value _getWorkspaces();
Bar &_bar; Bar &_bar;
Gtk::Box *_box; Gtk::Box *_box;
std::unordered_map<int, Gtk::Button> _buttons; std::unordered_map<int, Gtk::Button> _buttons;
util::SleeperThread _thread;
int _ipcSocketfd; int _ipcSocketfd;
int _ipcEventSocketfd; int _ipcEventSocketfd;
struct org_kde_kwin_idle_timeout *_idle_timer;
}; };
} }

View File

@ -12,6 +12,12 @@ static void handle_global(void *data, struct wl_registry *registry,
*output = (struct wl_output *)wl_registry_bind(registry, name, *output = (struct wl_output *)wl_registry_bind(registry, name,
&wl_output_interface, version); &wl_output_interface, version);
o->bars.emplace_back(*o, std::move(output)); 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);
} }
} }

View File

@ -1,9 +1,25 @@
#include "modules/workspaces.hpp" #include "modules/workspaces.hpp"
#include "idle-client-protocol.h"
#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::WorkspaceSelector *>(data);
if (o->thread) {
delete o->thread;
}
}
static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) {
auto o = reinterpret_cast<waybar::modules::WorkspaceSelector *>(data);
o->updateThread();
}
static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
.idle = handle_idle,
.resumed = handle_resume,
};
waybar::modules::WorkspaceSelector::WorkspaceSelector(Bar &bar) waybar::modules::WorkspaceSelector::WorkspaceSelector(Bar &bar)
: _bar(bar), _box(Gtk::manage(new Gtk::Box)) : thread(nullptr), _bar(bar), _box(Gtk::manage(new Gtk::Box))
{ {
_box->get_style_context()->add_class("workspace-selector"); _box->get_style_context()->add_class("workspace-selector");
std::string socketPath = get_socketpath(); std::string socketPath = get_socketpath();
@ -12,10 +28,20 @@ waybar::modules::WorkspaceSelector::WorkspaceSelector(Bar &bar)
const char *subscribe = "[ \"workspace\", \"mode\" ]"; const char *subscribe = "[ \"workspace\", \"mode\" ]";
uint32_t len = strlen(subscribe); uint32_t len = strlen(subscribe);
ipc_single_command(_ipcEventSocketfd, IPC_SUBSCRIBE, subscribe, &len); ipc_single_command(_ipcEventSocketfd, IPC_SUBSCRIBE, subscribe, &len);
_thread = [this] { _idle_timer =
org_kde_kwin_idle_get_idle_timeout(_bar.client.idle_manager,
_bar.client.seat, 10000); // 10 seconds
org_kde_kwin_idle_timeout_add_listener(_idle_timer,
&idle_timer_listener, this);
updateThread();
}
void waybar::modules::WorkspaceSelector::updateThread()
{
thread = new waybar::util::SleeperThread([this] {
update(); update();
_thread.sleep_for(chrono::milliseconds(250)); thread->sleep_for(waybar::chrono::milliseconds(250));
}; });
} }
auto waybar::modules::WorkspaceSelector::update() -> void auto waybar::modules::WorkspaceSelector::update() -> void