-
-
Notifications
You must be signed in to change notification settings - Fork 733
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1244 from alebastr/swaybar-ipc
Yet another swaybar ipc implementation
- Loading branch information
Showing
13 changed files
with
623 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ jobs: | |
- name: Test in FreeBSD VM | ||
uses: vmactions/[email protected] # aka FreeBSD 13.0 | ||
with: | ||
mem: 2048 | ||
usesh: true | ||
prepare: | | ||
export CPPFLAGS=-isystem/usr/local/include LDFLAGS=-L/usr/local/lib # sndio | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
#include <string> | ||
|
||
#include "modules/sway/ipc/client.hpp" | ||
#include "util/SafeSignal.hpp" | ||
#include "util/json.hpp" | ||
|
||
namespace waybar { | ||
|
||
class Bar; | ||
|
||
namespace modules::sway { | ||
|
||
/* | ||
* Supported subset of i3/sway IPC barconfig object | ||
*/ | ||
struct swaybar_config { | ||
std::string id; | ||
std::string mode; | ||
std::string hidden_state; | ||
}; | ||
|
||
/** | ||
* swaybar IPC client | ||
*/ | ||
class BarIpcClient { | ||
public: | ||
BarIpcClient(waybar::Bar& bar); | ||
|
||
private: | ||
void onInitialConfig(const struct Ipc::ipc_response& res); | ||
void onIpcEvent(const struct Ipc::ipc_response&); | ||
void onConfigUpdate(const swaybar_config& config); | ||
void onVisibilityUpdate(bool visible_by_modifier); | ||
void update(); | ||
|
||
Bar& bar_; | ||
util::JsonParser parser_; | ||
Ipc ipc_; | ||
|
||
swaybar_config bar_config_; | ||
bool visible_by_modifier_ = false; | ||
|
||
SafeSignal<bool> signal_visible_; | ||
SafeSignal<swaybar_config> signal_config_; | ||
}; | ||
|
||
} // namespace modules::sway | ||
} // namespace waybar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#pragma once | ||
|
||
#include <glibmm/dispatcher.h> | ||
#include <sigc++/signal.h> | ||
|
||
#include <functional> | ||
#include <mutex> | ||
#include <queue> | ||
#include <thread> | ||
#include <tuple> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
namespace waybar { | ||
|
||
/** | ||
* Thread-safe signal wrapper. | ||
* Uses Glib::Dispatcher to pass events to another thread and locked queue to pass the arguments. | ||
*/ | ||
template <typename... Args> | ||
struct SafeSignal : sigc::signal<void(std::decay_t<Args>...)> { | ||
public: | ||
SafeSignal() { dp_.connect(sigc::mem_fun(*this, &SafeSignal::handle_event)); } | ||
|
||
template <typename... EmitArgs> | ||
void emit(EmitArgs&&... args) { | ||
if (main_tid_ == std::this_thread::get_id()) { | ||
/* | ||
* Bypass the queue if the method is called the main thread. | ||
* Ensures that events emitted from the main thread are processed synchronously and saves a | ||
* few CPU cycles on locking/queuing. | ||
* As a downside, this makes main thread events prioritized over the other threads and | ||
* disrupts chronological order. | ||
*/ | ||
signal_t::emit(std::forward<EmitArgs>(args)...); | ||
} else { | ||
{ | ||
std::unique_lock lock(mutex_); | ||
queue_.emplace(std::forward<EmitArgs>(args)...); | ||
} | ||
dp_.emit(); | ||
} | ||
} | ||
|
||
template <typename... EmitArgs> | ||
inline void operator()(EmitArgs&&... args) { | ||
emit(std::forward<EmitArgs>(args)...); | ||
} | ||
|
||
protected: | ||
using signal_t = sigc::signal<void(std::decay_t<Args>...)>; | ||
using slot_t = decltype(std::declval<signal_t>().make_slot()); | ||
using arg_tuple_t = std::tuple<std::decay_t<Args>...>; | ||
// ensure that unwrapped methods are not accessible | ||
using signal_t::emit_reverse; | ||
using signal_t::make_slot; | ||
|
||
void handle_event() { | ||
for (std::unique_lock lock(mutex_); !queue_.empty(); lock.lock()) { | ||
auto args = queue_.front(); | ||
queue_.pop(); | ||
lock.unlock(); | ||
std::apply(cached_fn_, args); | ||
} | ||
} | ||
|
||
Glib::Dispatcher dp_; | ||
std::mutex mutex_; | ||
std::queue<arg_tuple_t> queue_; | ||
const std::thread::id main_tid_ = std::this_thread::get_id(); | ||
// cache functor for signal emission to avoid recreating it on each event | ||
const slot_t cached_fn_ = make_slot(); | ||
}; | ||
|
||
} // namespace waybar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.