refactor: proper modules destruction

This commit is contained in:
Alexis
2018-12-26 11:13:36 +01:00
parent 8af813c2f1
commit 399f61df98
25 changed files with 284 additions and 216 deletions

View File

@ -18,6 +18,7 @@ class Bar {
public:
Bar(const Client&, std::unique_ptr<struct wl_output *>&&, uint32_t);
Bar(const Bar&) = delete;
~Bar() = default;
auto toggle() -> void;
@ -54,6 +55,10 @@ class Bar {
Glib::RefPtr<Gtk::StyleContext> style_context_;
Glib::RefPtr<Gtk::CssProvider> css_provider_;
struct zxdg_output_v1 *xdg_output_;
Gtk::Box left_;
Gtk::Box center_;
Gtk::Box right_;
Gtk::Box box_;
std::vector<std::unique_ptr<waybar::IModule>> modules_left_;
std::vector<std::unique_ptr<waybar::IModule>> modules_center_;
std::vector<std::unique_ptr<waybar::IModule>> modules_right_;

View File

@ -10,7 +10,7 @@
#include <fmt/format.h>
#include <sys/inotify.h>
#include <algorithm>
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "ALabel.hpp"
namespace waybar::modules {

View File

@ -2,7 +2,7 @@
#include <fmt/format.h>
#include "fmt/time.h"
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
@ -10,6 +10,7 @@ namespace waybar::modules {
class Clock : public ALabel {
public:
Clock(const std::string&, const Json::Value&);
~Clock() = default;
auto update() -> void;
private:
waybar::util::SleeperThread thread_;

View File

@ -6,7 +6,7 @@
#include <vector>
#include <numeric>
#include <iostream>
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
@ -14,6 +14,7 @@ namespace waybar::modules {
class Cpu : public ALabel {
public:
Cpu(const std::string&, const Json::Value&);
~Cpu() = default;
auto update() -> void;
private:
static inline const std::string data_dir_ = "/proc/stat";

View File

@ -2,7 +2,7 @@
#include <fmt/format.h>
#include <iostream>
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "util/command.hpp"
#include "util/json.hpp"
#include "ALabel.hpp"

View File

@ -2,7 +2,7 @@
#include <fmt/format.h>
#include <fstream>
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
@ -10,6 +10,7 @@ namespace waybar::modules {
class Memory : public ALabel {
public:
Memory(const std::string&, const Json::Value&);
~Memory() = default;
auto update() -> void;
private:
static inline const std::string data_dir_ = "/proc/meminfo";

View File

@ -7,8 +7,9 @@
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/nl80211.h>
#include <sys/epoll.h>
#include <fmt/format.h>
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
@ -19,15 +20,17 @@ class Network : public ALabel {
~Network();
auto update() -> void;
private:
static int netlinkRequest(int, void*, uint32_t, uint32_t groups = 0);
static int netlinkResponse(int, void*, uint32_t, uint32_t groups = 0);
static int scanCb(struct nl_msg*, void*);
static int handleEvents(struct nl_msg*, void*);
static int handleScan(struct nl_msg*, void*);
void worker();
void disconnected();
void initNL80211();
void createInfoSocket();
void createEventSocket();
int getExternalInterface();
void getInterfaceAddress();
int netlinkRequest(void*, uint32_t, uint32_t groups = 0);
int netlinkResponse(void*, uint32_t, uint32_t groups = 0);
void parseEssid(struct nlattr**);
void parseSignal(struct nlattr**);
bool associatedOrJoined(struct nlattr**);
@ -37,9 +40,11 @@ class Network : public ALabel {
waybar::util::SleeperThread thread_timer_;
int ifid_;
sa_family_t family_;
int sock_fd_;
struct sockaddr_nl nladdr_ = {0};
struct nl_sock* sk_ = nullptr;
struct nl_sock* info_sock_ = nullptr;
int efd_;
int ev_fd_;
int nl80211_id_;
std::string essid_;

View File

@ -1,7 +1,6 @@
#pragma once
#include <fmt/format.h>
#include <thread>
#include "bar.hpp"
#include "util/json.hpp"
#include "IModule.hpp"
@ -13,6 +12,7 @@ namespace waybar::modules::SNI {
class Tray : public IModule {
public:
Tray(const std::string&, const Json::Value&);
~Tray() = default;
auto update() -> void;
operator Gtk::Widget &();
private:
@ -20,7 +20,6 @@ class Tray : public IModule {
void onRemove(std::unique_ptr<Item>& item);
static inline std::size_t nb_hosts_ = 0;
std::thread thread_;
const Json::Value& config_;
Gtk::Box box_;
SNI::Watcher watcher_ ;

View File

@ -10,33 +10,31 @@
namespace waybar::modules::sway {
class Ipc {
public:
Ipc();
~Ipc();
public:
Ipc();
~Ipc();
struct ipc_response {
uint32_t size;
uint32_t type;
std::string payload;
};
struct ipc_response {
uint32_t size;
uint32_t type;
std::string payload;
};
void connect();
struct ipc_response sendCmd(uint32_t type,
const std::string& payload = "") const;
void subscribe(const std::string& payload) const;
struct ipc_response handleEvent() const;
protected:
static inline const std::string ipc_magic_ = "i3-ipc";
static inline const size_t ipc_header_size_ = ipc_magic_.size() + 8;
struct ipc_response sendCmd(uint32_t type, const std::string &payload = "") const;
void subscribe(const std::string &payload) const;
struct ipc_response handleEvent() const;
const std::string getSocketPath() const;
int open(const std::string&) const;
struct ipc_response send(int fd, uint32_t type,
const std::string& payload = "") const;
struct ipc_response recv(int fd) const;
protected:
static inline const std::string ipc_magic_ = "i3-ipc";
static inline const size_t ipc_header_size_ = ipc_magic_.size() + 8;
int fd_;
int fd_event_;
const std::string getSocketPath() const;
int open(const std::string &) const;
struct ipc_response send(int fd, uint32_t type, const std::string &payload = "") const;
struct ipc_response recv(int fd) const;
int fd_;
int fd_event_;
};
}
} // namespace waybar::modules::sway

View File

@ -3,30 +3,30 @@
#define event_mask(ev) (1u << (ev & 0x7F))
enum ipc_command_type {
// i3 command types - see i3's I3_REPLY_TYPE constants
IPC_COMMAND = 0,
IPC_GET_WORKSPACES = 1,
IPC_SUBSCRIBE = 2,
IPC_GET_OUTPUTS = 3,
IPC_GET_TREE = 4,
IPC_GET_MARKS = 5,
IPC_GET_BAR_CONFIG = 6,
IPC_GET_VERSION = 7,
IPC_GET_BINDING_MODES = 8,
IPC_GET_CONFIG = 9,
IPC_SEND_TICK = 10,
// i3 command types - see i3's I3_REPLY_TYPE constants
IPC_COMMAND = 0,
IPC_GET_WORKSPACES = 1,
IPC_SUBSCRIBE = 2,
IPC_GET_OUTPUTS = 3,
IPC_GET_TREE = 4,
IPC_GET_MARKS = 5,
IPC_GET_BAR_CONFIG = 6,
IPC_GET_VERSION = 7,
IPC_GET_BINDING_MODES = 8,
IPC_GET_CONFIG = 9,
IPC_SEND_TICK = 10,
// sway-specific command types
IPC_GET_INPUTS = 100,
IPC_GET_SEATS = 101,
// sway-specific command types
IPC_GET_INPUTS = 100,
IPC_GET_SEATS = 101,
// Events sent from sway to clients. Events have the highest bits set.
IPC_EVENT_WORKSPACE = ((1<<31) | 0),
IPC_EVENT_OUTPUT = ((1<<31) | 1),
IPC_EVENT_MODE = ((1<<31) | 2),
IPC_EVENT_WINDOW = ((1<<31) | 3),
IPC_EVENT_BARCONFIG_UPDATE = ((1<<31) | 4),
IPC_EVENT_BINDING = ((1<<31) | 5),
IPC_EVENT_SHUTDOWN = ((1<<31) | 6),
IPC_EVENT_TICK = ((1<<31) | 7),
// Events sent from sway to clients. Events have the highest bits set.
IPC_EVENT_WORKSPACE = ((1 << 31) | 0),
IPC_EVENT_OUTPUT = ((1 << 31) | 1),
IPC_EVENT_MODE = ((1 << 31) | 2),
IPC_EVENT_WINDOW = ((1 << 31) | 3),
IPC_EVENT_BARCONFIG_UPDATE = ((1 << 31) | 4),
IPC_EVENT_BINDING = ((1 << 31) | 5),
IPC_EVENT_SHUTDOWN = ((1 << 31) | 6),
IPC_EVENT_TICK = ((1 << 31) | 7),
};

View File

@ -3,7 +3,7 @@
#include <fmt/format.h>
#include "bar.hpp"
#include "client.hpp"
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "util/json.hpp"
#include "ALabel.hpp"
#include "modules/sway/ipc/client.hpp"
@ -13,6 +13,7 @@ namespace waybar::modules::sway {
class Mode : public ALabel {
public:
Mode(const std::string&, const waybar::Bar&, const Json::Value&);
~Mode() = default;
auto update() -> void;
private:
void worker();

View File

@ -4,7 +4,7 @@
#include <tuple>
#include "bar.hpp"
#include "client.hpp"
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "util/json.hpp"
#include "ALabel.hpp"
#include "modules/sway/ipc/client.hpp"
@ -14,6 +14,7 @@ namespace waybar::modules::sway {
class Window : public ALabel {
public:
Window(const std::string&, const waybar::Bar&, const Json::Value&);
~Window() = default;
auto update() -> void;
private:
void worker();

View File

@ -3,7 +3,7 @@
#include <fmt/format.h>
#include "bar.hpp"
#include "client.hpp"
#include "util/chrono.hpp"
#include "util/sleeper_thread.hpp"
#include "util/json.hpp"
#include "IModule.hpp"
#include "modules/sway/ipc/client.hpp"
@ -14,6 +14,7 @@ namespace waybar::modules::sway {
class Workspaces : public IModule {
public:
Workspaces(const std::string&, const waybar::Bar&, const Json::Value&);
~Workspaces() = default;
auto update() -> void;
operator Gtk::Widget &();
private:
@ -27,14 +28,14 @@ class Workspaces : public IModule {
const Bar& bar_;
const Json::Value& config_;
Json::Value workspaces_;
waybar::util::SleeperThread thread_;
Gtk::Box box_;
util::JsonParser parser_;
Ipc ipc_;
std::mutex mutex_;
bool scrolling_;
std::unordered_map<std::string, Gtk::Button> buttons_;
Json::Value workspaces_;
Ipc ipc_;
};
}

View File

@ -2,6 +2,7 @@
#include <sys/wait.h>
#include <giomm.h>
#include <unistd.h>
namespace waybar::util::command {

View File

@ -10,7 +10,7 @@ struct JsonParser {
: reader_(builder_.newCharReader())
{}
const Json::Value parse(const std::string data) const
const Json::Value parse(const std::string& data) const
{
Json::Value root;
std::string err;

View File

@ -6,30 +6,20 @@
#include <condition_variable>
#include <thread>
namespace waybar::chrono {
using namespace std::chrono;
using clock = std::chrono::system_clock;
using duration = clock::duration;
using time_point = std::chrono::time_point<clock, duration>;
}
namespace waybar::util {
struct SleeperThread {
class SleeperThread {
public:
SleeperThread() = default;
SleeperThread(std::function<void()> func)
: do_run_(true), thread_{[this, func] {
: thread_{[this, func] {
while (do_run_) func();
}}
{}
SleeperThread& operator=(std::function<void()> func)
{
do_run_ = true;
thread_ = std::thread([this, func] {
while (do_run_) func();
});
@ -41,16 +31,17 @@ struct SleeperThread {
return do_run_;
}
auto sleep_for(chrono::duration dur)
auto sleep_for(std::chrono::system_clock::duration dur)
{
auto lock = std::unique_lock(mutex_);
return condvar_.wait_for(lock, dur);
std::unique_lock lk(mutex_);
return condvar_.wait_for(lk, dur, [this] { return !do_run_; });
}
auto sleep_until(chrono::time_point time)
auto sleep_until(std::chrono::time_point<std::chrono::system_clock,
std::chrono::system_clock::duration> time_point)
{
auto lock = std::unique_lock(mutex_);
return condvar_.wait_until(lock, time);
std::unique_lock lk(mutex_);
return condvar_.wait_until(lk, time_point, [this] { return !do_run_; });
}
auto wake_up()
@ -60,7 +51,10 @@ struct SleeperThread {
auto stop()
{
do_run_ = false;
{
std::lock_guard<std::mutex> lck(mutex_);
do_run_ = false;
}
condvar_.notify_all();
}
@ -68,15 +62,15 @@ struct SleeperThread {
{
stop();
if (thread_.joinable()) {
thread_.detach();
thread_.join();
}
}
private:
bool do_run_ = false;
std::thread thread_;
std::condition_variable condvar_;
std::mutex mutex_;
bool do_run_ = true;
};
}