mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Merge branch 'master' of github.com:Alexays/Waybar
This commit is contained in:
@ -8,6 +8,9 @@
|
||||
#include <gtkmm/window.h>
|
||||
#include <json/json.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "AModule.hpp"
|
||||
#include "xdg-output-unstable-v1-client-protocol.h"
|
||||
|
||||
@ -36,6 +39,19 @@ struct bar_margins {
|
||||
int left = 0;
|
||||
};
|
||||
|
||||
struct bar_mode {
|
||||
bar_layer layer;
|
||||
bool exclusive;
|
||||
bool passthrough;
|
||||
bool visible;
|
||||
};
|
||||
|
||||
#ifdef HAVE_SWAY
|
||||
namespace modules::sway {
|
||||
class BarIpcClient;
|
||||
}
|
||||
#endif // HAVE_SWAY
|
||||
|
||||
class BarSurface {
|
||||
protected:
|
||||
BarSurface() = default;
|
||||
@ -54,38 +70,56 @@ class BarSurface {
|
||||
|
||||
class Bar {
|
||||
public:
|
||||
using bar_mode_map = std::map<std::string_view, struct bar_mode>;
|
||||
static const bar_mode_map PRESET_MODES;
|
||||
static const std::string_view MODE_DEFAULT;
|
||||
static const std::string_view MODE_INVISIBLE;
|
||||
|
||||
Bar(struct waybar_output *w_output, const Json::Value &);
|
||||
Bar(const Bar &) = delete;
|
||||
~Bar() = default;
|
||||
~Bar();
|
||||
|
||||
void setMode(const std::string_view &);
|
||||
void setVisible(bool visible);
|
||||
void toggle();
|
||||
void handleSignal(int);
|
||||
|
||||
struct waybar_output *output;
|
||||
Json::Value config;
|
||||
struct wl_surface * surface;
|
||||
bool exclusive = true;
|
||||
struct wl_surface *surface;
|
||||
bool visible = true;
|
||||
bool vertical = false;
|
||||
Gtk::Window window;
|
||||
|
||||
#ifdef HAVE_SWAY
|
||||
std::string bar_id;
|
||||
#endif
|
||||
|
||||
private:
|
||||
void onMap(GdkEventAny *);
|
||||
auto setupWidgets() -> void;
|
||||
void getModules(const Factory &, const std::string &);
|
||||
void getModules(const Factory &, const std::string &, Gtk::Box*);
|
||||
void setupAltFormatKeyForModule(const std::string &module_name);
|
||||
void setupAltFormatKeyForModuleList(const char *module_list_name);
|
||||
void setMode(const bar_mode &);
|
||||
|
||||
/* Copy initial set of modes to allow customization */
|
||||
bar_mode_map configured_modes = PRESET_MODES;
|
||||
std::string last_mode_{MODE_DEFAULT};
|
||||
|
||||
std::unique_ptr<BarSurface> surface_impl_;
|
||||
bar_layer layer_;
|
||||
Gtk::Box left_;
|
||||
Gtk::Box center_;
|
||||
Gtk::Box right_;
|
||||
Gtk::Box box_;
|
||||
std::vector<std::unique_ptr<waybar::AModule>> modules_left_;
|
||||
std::vector<std::unique_ptr<waybar::AModule>> modules_center_;
|
||||
std::vector<std::unique_ptr<waybar::AModule>> modules_right_;
|
||||
std::vector<std::shared_ptr<waybar::AModule>> modules_left_;
|
||||
std::vector<std::shared_ptr<waybar::AModule>> modules_center_;
|
||||
std::vector<std::shared_ptr<waybar::AModule>> modules_right_;
|
||||
#ifdef HAVE_SWAY
|
||||
using BarIpcClient = modules::sway::BarIpcClient;
|
||||
std::unique_ptr<BarIpcClient> _ipc_client;
|
||||
#endif
|
||||
std::vector<std::shared_ptr<waybar::AModule>> modules_all_;
|
||||
};
|
||||
|
||||
} // namespace waybar
|
||||
|
@ -29,6 +29,7 @@ class Client {
|
||||
struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager = nullptr;
|
||||
std::vector<std::unique_ptr<Bar>> bars;
|
||||
Config config;
|
||||
std::string bar_id;
|
||||
|
||||
private:
|
||||
Client() = default;
|
||||
|
@ -14,6 +14,7 @@
|
||||
#endif
|
||||
#ifdef HAVE_WLR
|
||||
#include "modules/wlr/taskbar.hpp"
|
||||
#include "modules/wlr/workspace_manager.hpp"
|
||||
#endif
|
||||
#ifdef HAVE_RIVER
|
||||
#include "modules/river/tags.hpp"
|
||||
@ -50,6 +51,9 @@
|
||||
#ifdef HAVE_LIBSNDIO
|
||||
#include "modules/sndio.hpp"
|
||||
#endif
|
||||
#ifdef HAVE_GIO_UNIX
|
||||
#include "modules/inhibitor.hpp"
|
||||
#endif
|
||||
#include "bar.hpp"
|
||||
#include "modules/custom.hpp"
|
||||
#include "modules/temperature.hpp"
|
||||
|
21
include/group.hpp
Normal file
21
include/group.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <gtkmm/widget.h>
|
||||
#include <gtkmm/box.h>
|
||||
#include <json/json.h>
|
||||
#include "AModule.hpp"
|
||||
#include "bar.hpp"
|
||||
#include "factory.hpp"
|
||||
|
||||
namespace waybar {
|
||||
|
||||
class Group : public AModule {
|
||||
public:
|
||||
Group(const std::string&, const Bar&, const Json::Value&);
|
||||
~Group() = default;
|
||||
auto update() -> void;
|
||||
operator Gtk::Widget &();
|
||||
Gtk::Box box;
|
||||
};
|
||||
|
||||
} // namespace waybar
|
@ -17,6 +17,8 @@ struct waybar_time {
|
||||
date::zoned_seconds ztime;
|
||||
};
|
||||
|
||||
const std::string kCalendarPlaceholder = "calendar";
|
||||
|
||||
class Clock : public ALabel {
|
||||
public:
|
||||
Clock(const std::string&, const Json::Value&);
|
||||
@ -26,18 +28,19 @@ class Clock : public ALabel {
|
||||
private:
|
||||
util::SleeperThread thread_;
|
||||
std::locale locale_;
|
||||
const date::time_zone* time_zone_;
|
||||
bool fixed_time_zone_;
|
||||
int time_zone_idx_;
|
||||
std::vector<const date::time_zone*> time_zones_;
|
||||
int current_time_zone_idx_;
|
||||
date::year_month_day cached_calendar_ymd_ = date::January/1/0;
|
||||
std::string cached_calendar_text_;
|
||||
bool is_calendar_in_tooltip_;
|
||||
|
||||
bool handleScroll(GdkEventScroll* e);
|
||||
|
||||
auto calendar_text(const waybar_time& wtime) -> std::string;
|
||||
auto weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void;
|
||||
auto first_day_of_week() -> date::weekday;
|
||||
bool setTimeZone(Json::Value zone_name);
|
||||
const date::time_zone* current_timezone();
|
||||
bool is_timezone_fixed();
|
||||
};
|
||||
|
||||
} // namespace waybar::modules
|
||||
|
27
include/modules/inhibitor.hpp
Normal file
27
include/modules/inhibitor.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include "ALabel.hpp"
|
||||
#include "bar.hpp"
|
||||
|
||||
namespace waybar::modules {
|
||||
|
||||
class Inhibitor : public ALabel {
|
||||
public:
|
||||
Inhibitor(const std::string&, const waybar::Bar&, const Json::Value&);
|
||||
~Inhibitor() override;
|
||||
auto update() -> void;
|
||||
auto activated() -> bool;
|
||||
|
||||
private:
|
||||
auto handleToggle(::GdkEventButton* const& e) -> bool;
|
||||
|
||||
const std::unique_ptr<::GDBusConnection, void(*)(::GDBusConnection*)> dbus_;
|
||||
const std::string inhibitors_;
|
||||
int handle_ = -1;
|
||||
};
|
||||
|
||||
} // namespace waybar::modules
|
@ -43,6 +43,7 @@ class Network : public ALabel {
|
||||
const std::string getNetworkState() const;
|
||||
void clearIface();
|
||||
bool wildcardMatch(const std::string& pattern, const std::string& text) const;
|
||||
std::optional<std::pair<unsigned long long, unsigned long long>> readBandwidthUsage();
|
||||
|
||||
int ifid_;
|
||||
sa_family_t family_;
|
||||
@ -72,7 +73,8 @@ class Network : public ALabel {
|
||||
int cidr_;
|
||||
int32_t signal_strength_dbm_;
|
||||
uint8_t signal_strength_;
|
||||
uint32_t frequency_;
|
||||
std::string signal_strength_app_;
|
||||
float frequency_;
|
||||
uint32_t route_priority;
|
||||
|
||||
util::SleeperThread thread_;
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "AModule.hpp"
|
||||
#include "bar.hpp"
|
||||
#include "river-status-unstable-v1-client-protocol.h"
|
||||
#include "river-control-unstable-v1-client-protocol.h"
|
||||
#include "xdg-output-unstable-v1-client-protocol.h"
|
||||
|
||||
namespace waybar::modules::river {
|
||||
@ -20,7 +21,12 @@ class Tags : public waybar::AModule {
|
||||
void handle_view_tags(struct wl_array *tags);
|
||||
void handle_urgent_tags(uint32_t tags);
|
||||
|
||||
void handle_primary_clicked(uint32_t tag);
|
||||
bool handle_button_press(GdkEventButton *event_button, uint32_t tag);
|
||||
|
||||
struct zriver_status_manager_v1 *status_manager_;
|
||||
struct zriver_control_v1 *control_;
|
||||
struct wl_seat *seat_;
|
||||
|
||||
private:
|
||||
const waybar::Bar & bar_;
|
||||
|
49
include/modules/sway/bar.hpp
Normal file
49
include/modules/sway/bar.hpp
Normal file
@ -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
|
@ -32,6 +32,7 @@ class Language : public ALabel, public sigc::trackable {
|
||||
std::string short_name;
|
||||
std::string variant;
|
||||
std::string short_description;
|
||||
std::string country_flag() const;
|
||||
};
|
||||
|
||||
class XKBContext {
|
||||
@ -54,7 +55,7 @@ class Language : public ALabel, public sigc::trackable {
|
||||
|
||||
const static std::string XKB_LAYOUT_NAMES_KEY;
|
||||
const static std::string XKB_ACTIVE_LAYOUT_NAME_KEY;
|
||||
|
||||
|
||||
Layout layout_;
|
||||
std::string tooltip_format_ = "";
|
||||
std::map<std::string, Layout> layouts_map_;
|
||||
|
@ -3,11 +3,13 @@
|
||||
#include "AModule.hpp"
|
||||
#include "bar.hpp"
|
||||
#include "client.hpp"
|
||||
#include "giomm/desktopappinfo.h"
|
||||
#include "util/json.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <gdk/gdk.h>
|
||||
@ -61,15 +63,18 @@ class Task
|
||||
Gtk::Image icon_;
|
||||
Gtk::Label text_before_;
|
||||
Gtk::Label text_after_;
|
||||
bool button_visible_;
|
||||
bool ignored_;
|
||||
Glib::RefPtr<Gio::DesktopAppInfo> app_info_;
|
||||
bool button_visible_ = false;
|
||||
bool ignored_ = false;
|
||||
|
||||
bool with_icon_;
|
||||
bool with_icon_ = false;
|
||||
bool with_name_ = false;
|
||||
std::string format_before_;
|
||||
std::string format_after_;
|
||||
|
||||
std::string format_tooltip_;
|
||||
|
||||
std::string name_;
|
||||
std::string title_;
|
||||
std::string app_id_;
|
||||
uint32_t state_ = 0;
|
||||
@ -77,6 +82,9 @@ class Task
|
||||
private:
|
||||
std::string repr() const;
|
||||
std::string state_string(bool = false) const;
|
||||
void set_app_info_from_app_id_list(const std::string& app_id_list);
|
||||
bool image_load_icon(Gtk::Image& image, const Glib::RefPtr<Gtk::IconTheme>& icon_theme, Glib::RefPtr<Gio::DesktopAppInfo> app_info, int size);
|
||||
void hide_if_ignored();
|
||||
|
||||
public:
|
||||
/* Getter functions */
|
||||
@ -135,6 +143,7 @@ class Taskbar : public waybar::AModule
|
||||
|
||||
std::vector<Glib::RefPtr<Gtk::IconTheme>> icon_themes_;
|
||||
std::unordered_set<std::string> ignore_list_;
|
||||
std::map<std::string, std::string> app_ids_replace_map_;
|
||||
|
||||
struct zwlr_foreign_toplevel_manager_v1 *manager_;
|
||||
struct wl_seat *seat_;
|
||||
@ -157,8 +166,9 @@ class Taskbar : public waybar::AModule
|
||||
bool show_output(struct wl_output *) const;
|
||||
bool all_outputs() const;
|
||||
|
||||
std::vector<Glib::RefPtr<Gtk::IconTheme>> icon_themes() const;
|
||||
const std::vector<Glib::RefPtr<Gtk::IconTheme>>& icon_themes() const;
|
||||
const std::unordered_set<std::string>& ignore_list() const;
|
||||
const std::map<std::string, std::string>& app_ids_replace_map() const;
|
||||
};
|
||||
|
||||
} /* namespace waybar::modules::wlr */
|
||||
|
160
include/modules/wlr/workspace_manager.hpp
Normal file
160
include/modules/wlr/workspace_manager.hpp
Normal file
@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <gtkmm/button.h>
|
||||
#include <gtkmm/image.h>
|
||||
#include <gtkmm/label.h>
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "AModule.hpp"
|
||||
#include "bar.hpp"
|
||||
#include "ext-workspace-unstable-v1-client-protocol.h"
|
||||
|
||||
namespace waybar::modules::wlr {
|
||||
|
||||
class WorkspaceManager;
|
||||
class WorkspaceGroup;
|
||||
|
||||
class Workspace {
|
||||
public:
|
||||
Workspace(const waybar::Bar &bar, const Json::Value &config, WorkspaceGroup &workspace_group,
|
||||
zext_workspace_handle_v1 *workspace, uint32_t id);
|
||||
~Workspace();
|
||||
auto update() -> void;
|
||||
|
||||
auto id() const -> uint32_t { return id_; }
|
||||
auto is_active() const -> bool { return state_ & static_cast<uint32_t>(State::ACTIVE); }
|
||||
auto is_urgent() const -> bool { return state_ & static_cast<uint32_t>(State::URGENT); }
|
||||
auto is_hidden() const -> bool { return state_ & static_cast<uint32_t>(State::HIDDEN); }
|
||||
// wlr stuff
|
||||
auto handle_name(const std::string &name) -> void;
|
||||
auto handle_coordinates(const std::vector<uint32_t> &coordinates) -> void;
|
||||
auto handle_state(const std::vector<uint32_t> &state) -> void;
|
||||
auto handle_remove() -> void;
|
||||
|
||||
auto handle_done() -> void;
|
||||
auto handle_clicked(GdkEventButton *bt) -> bool;
|
||||
auto show() -> void;
|
||||
auto hide() -> void;
|
||||
auto get_button_ref() -> Gtk::Button & { return button_; }
|
||||
auto get_name() -> std::string & { return name_; }
|
||||
auto get_coords() -> std::vector<uint32_t> & { return coordinates_; }
|
||||
|
||||
enum class State {
|
||||
ACTIVE = (1 << 0),
|
||||
URGENT = (1 << 1),
|
||||
HIDDEN = (1 << 2),
|
||||
};
|
||||
|
||||
private:
|
||||
auto get_icon() -> std::string;
|
||||
|
||||
const Bar &bar_;
|
||||
const Json::Value &config_;
|
||||
WorkspaceGroup &workspace_group_;
|
||||
|
||||
// wlr stuff
|
||||
zext_workspace_handle_v1 *workspace_handle_;
|
||||
uint32_t state_ = 0;
|
||||
|
||||
uint32_t id_;
|
||||
std::string name_;
|
||||
std::vector<uint32_t> coordinates_;
|
||||
static std::map<std::string, std::string> icons_map_;
|
||||
std::string format_;
|
||||
bool with_icon_ = false;
|
||||
|
||||
Gtk::Button button_;
|
||||
Gtk::Box content_;
|
||||
Gtk::Label label_;
|
||||
};
|
||||
|
||||
class WorkspaceGroup {
|
||||
public:
|
||||
WorkspaceGroup(const waybar::Bar &bar, Gtk::Box &box, const Json::Value &config,
|
||||
WorkspaceManager &manager, zext_workspace_group_handle_v1 *workspace_group_handle,
|
||||
uint32_t id);
|
||||
~WorkspaceGroup();
|
||||
auto update() -> void;
|
||||
|
||||
auto id() const -> uint32_t { return id_; }
|
||||
auto is_visible() const -> bool;
|
||||
auto remove_workspace(uint32_t id_) -> void;
|
||||
auto active_only() const -> bool;
|
||||
auto creation_delayed() const -> bool;
|
||||
auto workspaces() -> std::vector<std::unique_ptr<Workspace>> & { return workspaces_; }
|
||||
|
||||
auto sort_workspaces() -> void;
|
||||
auto set_need_to_sort() -> void { need_to_sort = true; }
|
||||
auto add_button(Gtk::Button &button) -> void;
|
||||
auto remove_button(Gtk::Button &button) -> void;
|
||||
|
||||
// wlr stuff
|
||||
auto handle_workspace_create(zext_workspace_handle_v1 *workspace_handle) -> void;
|
||||
auto handle_remove() -> void;
|
||||
auto handle_output_enter(wl_output *output) -> void;
|
||||
auto handle_output_leave() -> void;
|
||||
auto handle_done() -> void;
|
||||
auto commit() -> void;
|
||||
|
||||
private:
|
||||
static uint32_t workspace_global_id;
|
||||
const waybar::Bar &bar_;
|
||||
Gtk::Box &box_;
|
||||
const Json::Value &config_;
|
||||
WorkspaceManager &workspace_manager_;
|
||||
|
||||
// wlr stuff
|
||||
zext_workspace_group_handle_v1 *workspace_group_handle_;
|
||||
wl_output *output_ = nullptr;
|
||||
|
||||
uint32_t id_;
|
||||
std::vector<std::unique_ptr<Workspace>> workspaces_;
|
||||
bool need_to_sort = false;
|
||||
};
|
||||
|
||||
class WorkspaceManager : public AModule {
|
||||
public:
|
||||
WorkspaceManager(const std::string &id, const waybar::Bar &bar, const Json::Value &config);
|
||||
~WorkspaceManager() override;
|
||||
auto update() -> void override;
|
||||
|
||||
auto all_outputs() const -> bool { return all_outputs_; }
|
||||
auto active_only() const -> bool { return active_only_; }
|
||||
auto workspace_comparator() const
|
||||
-> std::function<bool(std::unique_ptr<Workspace> &, std::unique_ptr<Workspace> &)>;
|
||||
auto creation_delayed() const -> bool { return creation_delayed_; }
|
||||
|
||||
auto sort_workspaces() -> void;
|
||||
auto remove_workspace_group(uint32_t id_) -> void;
|
||||
|
||||
// wlr stuff
|
||||
auto register_manager(wl_registry *registry, uint32_t name, uint32_t version) -> void;
|
||||
auto handle_workspace_group_create(zext_workspace_group_handle_v1 *workspace_group_handle)
|
||||
-> void;
|
||||
auto handle_done() -> void;
|
||||
auto handle_finished() -> void;
|
||||
auto commit() -> void;
|
||||
|
||||
private:
|
||||
const waybar::Bar &bar_;
|
||||
Gtk::Box box_;
|
||||
std::vector<std::unique_ptr<WorkspaceGroup>> groups_;
|
||||
|
||||
// wlr stuff
|
||||
zext_workspace_manager_v1 *workspace_manager_ = nullptr;
|
||||
|
||||
static uint32_t group_global_id;
|
||||
|
||||
bool sort_by_name_ = true;
|
||||
bool sort_by_coordinates_ = true;
|
||||
bool all_outputs_ = false;
|
||||
bool active_only_ = false;
|
||||
bool creation_delayed_ = false;
|
||||
};
|
||||
|
||||
} // namespace waybar::modules::wlr
|
8
include/modules/wlr/workspace_manager_binding.hpp
Normal file
8
include/modules/wlr/workspace_manager_binding.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "ext-workspace-unstable-v1-client-protocol.h"
|
||||
|
||||
namespace waybar::modules::wlr {
|
||||
void add_registry_listener(void *data);
|
||||
void add_workspace_listener(zext_workspace_handle_v1 *workspace_handle, void *data);
|
||||
void add_workspace_group_listener(zext_workspace_group_handle_v1 *workspace_group_handle, void *data);
|
||||
zext_workspace_manager_v1* workspace_manager_bind(wl_registry *registry, uint32_t name, uint32_t version, void *data);
|
||||
}
|
75
include/util/SafeSignal.hpp
Normal file
75
include/util/SafeSignal.hpp
Normal file
@ -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
|
Reference in New Issue
Block a user