mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Clean (#31)
This commit is contained in:
@ -7,13 +7,13 @@ namespace waybar {
|
||||
|
||||
class ALabel : public IModule {
|
||||
public:
|
||||
ALabel(Json::Value);
|
||||
ALabel(const Json::Value&);
|
||||
virtual ~ALabel() = default;
|
||||
virtual auto update() -> void;
|
||||
virtual operator Gtk::Widget &();
|
||||
protected:
|
||||
Gtk::Label label_;
|
||||
Json::Value config_;
|
||||
const Json::Value& config_;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -6,9 +6,10 @@ namespace waybar {
|
||||
|
||||
class IModule {
|
||||
public:
|
||||
virtual ~IModule() {}
|
||||
virtual ~IModule() = default;
|
||||
virtual auto update() -> void = 0;
|
||||
virtual operator Gtk::Widget &() = 0;
|
||||
Glib::Dispatcher dp; // Hmmm Maybe I should create an abstract class ?
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ class Factory;
|
||||
|
||||
class Bar {
|
||||
public:
|
||||
Bar(Client&, std::unique_ptr<struct wl_output *>&&, uint32_t wl_name);
|
||||
Bar(const Client&, std::unique_ptr<struct wl_output *>&&, uint32_t);
|
||||
Bar(const Bar&) = delete;
|
||||
|
||||
auto toggle() -> void;
|
||||
|
||||
Client& client;
|
||||
const Client& client;
|
||||
Gtk::Window window;
|
||||
struct wl_surface *surface;
|
||||
struct zwlr_layer_surface_v1 *layer_surface;
|
||||
@ -43,7 +43,7 @@ class Bar {
|
||||
auto setupConfig() -> void;
|
||||
auto setupWidgets() -> void;
|
||||
auto setupCss() -> void;
|
||||
void getModules(Factory factory, const std::string& pos);
|
||||
void getModules(const Factory&, const std::string&);
|
||||
|
||||
uint32_t width_ = 0;
|
||||
uint32_t height_ = 30;
|
||||
|
@ -15,11 +15,11 @@ namespace waybar {
|
||||
|
||||
class Factory {
|
||||
public:
|
||||
Factory(Bar &bar, Json::Value config);
|
||||
IModule* makeModule(const std::string &name);
|
||||
Factory(Bar& bar, const Json::Value& config);
|
||||
IModule* makeModule(const std::string &name) const;
|
||||
private:
|
||||
Bar &_bar;
|
||||
Json::Value _config;
|
||||
Bar& bar_;
|
||||
const Json::Value& config_;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -15,13 +15,14 @@ namespace fs = std::filesystem;
|
||||
|
||||
class Battery : public ALabel {
|
||||
public:
|
||||
Battery(Json::Value);
|
||||
Battery(const Json::Value&);
|
||||
~Battery();
|
||||
auto update() -> void;
|
||||
private:
|
||||
std::string getIcon(uint16_t percentage);
|
||||
|
||||
static inline const fs::path data_dir_ = "/sys/class/power_supply/";
|
||||
|
||||
void worker();
|
||||
std::string getIcon(uint16_t percentage);
|
||||
|
||||
util::SleeperThread thread_;
|
||||
std::vector<fs::path> batteries_;
|
||||
|
@ -9,7 +9,7 @@ namespace waybar::modules {
|
||||
|
||||
class Clock : public ALabel {
|
||||
public:
|
||||
Clock(Json::Value);
|
||||
Clock(const Json::Value&);
|
||||
auto update() -> void;
|
||||
private:
|
||||
waybar::util::SleeperThread thread_;
|
||||
|
@ -9,7 +9,7 @@ namespace waybar::modules {
|
||||
|
||||
class Cpu : public ALabel {
|
||||
public:
|
||||
Cpu(Json::Value);
|
||||
Cpu(const Json::Value&);
|
||||
auto update() -> void;
|
||||
private:
|
||||
waybar::util::SleeperThread thread_;
|
||||
|
@ -10,10 +10,12 @@ namespace waybar::modules {
|
||||
|
||||
class Custom : public ALabel {
|
||||
public:
|
||||
Custom(std::string, Json::Value);
|
||||
Custom(const std::string&, const Json::Value&);
|
||||
auto update() -> void;
|
||||
private:
|
||||
std::string name_;
|
||||
void worker();
|
||||
|
||||
const std::string& name_;
|
||||
waybar::util::SleeperThread thread_;
|
||||
};
|
||||
|
||||
|
@ -9,7 +9,7 @@ namespace waybar::modules {
|
||||
|
||||
class Memory : public ALabel {
|
||||
public:
|
||||
Memory(Json::Value);
|
||||
Memory(const Json::Value&);
|
||||
auto update() -> void;
|
||||
private:
|
||||
waybar::util::SleeperThread thread_;
|
||||
|
@ -13,7 +13,7 @@ namespace waybar::modules {
|
||||
|
||||
class Network : public ALabel {
|
||||
public:
|
||||
Network(Json::Value);
|
||||
Network(const Json::Value&);
|
||||
~Network();
|
||||
auto update() -> void;
|
||||
private:
|
||||
|
@ -9,7 +9,7 @@ namespace waybar::modules {
|
||||
|
||||
class Pulseaudio : public ALabel {
|
||||
public:
|
||||
Pulseaudio(Json::Value);
|
||||
Pulseaudio(const Json::Value&);
|
||||
~Pulseaudio();
|
||||
auto update() -> void;
|
||||
private:
|
||||
|
@ -1,33 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include "ipc.hpp"
|
||||
|
||||
/**
|
||||
* IPC response including type of IPC response, size of payload and the json
|
||||
* encoded payload string.
|
||||
*/
|
||||
struct ipc_response {
|
||||
uint32_t size;
|
||||
uint32_t type;
|
||||
std::string payload;
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
class Ipc {
|
||||
public:
|
||||
Ipc();
|
||||
~Ipc();
|
||||
|
||||
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;
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the path to the IPC socket from sway.
|
||||
*/
|
||||
std::string getSocketPath(void);
|
||||
/**
|
||||
* Opens the sway socket.
|
||||
*/
|
||||
int ipcOpenSocket(const std::string &socketPath);
|
||||
/**
|
||||
* Issues a single IPC command and returns the buffer. len will be updated with
|
||||
* the length of the buffer returned from sway.
|
||||
*/
|
||||
struct ipc_response ipcSingleCommand(int socketfd, uint32_t type,
|
||||
const std::string& payload);
|
||||
/**
|
||||
* Receives a single IPC response and returns an ipc_response.
|
||||
*/
|
||||
struct ipc_response ipcRecvResponse(int socketfd);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#define event_mask(ev) (1 << (ev & 0x7F))
|
||||
#define event_mask(ev) (1u << (ev & 0x7F))
|
||||
|
||||
enum ipc_command_type {
|
||||
// i3 command types - see i3's I3_REPLY_TYPE constants
|
||||
|
@ -6,23 +6,23 @@
|
||||
#include "util/chrono.hpp"
|
||||
#include "util/json.hpp"
|
||||
#include "ALabel.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
class Window : public ALabel {
|
||||
public:
|
||||
Window(waybar::Bar&, Json::Value);
|
||||
~Window();
|
||||
Window(waybar::Bar&, const Json::Value&);
|
||||
auto update() -> void;
|
||||
private:
|
||||
void worker();
|
||||
std::string getFocusedNode(Json::Value nodes);
|
||||
void getFocusedWindow();
|
||||
|
||||
Bar& bar_;
|
||||
waybar::util::SleeperThread thread_;
|
||||
util::JsonParser parser_;
|
||||
int ipcfd_;
|
||||
int ipc_eventfd_;
|
||||
Ipc ipc_;
|
||||
std::string window_;
|
||||
};
|
||||
|
||||
|
@ -6,16 +6,17 @@
|
||||
#include "util/chrono.hpp"
|
||||
#include "util/json.hpp"
|
||||
#include "IModule.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
class Workspaces : public IModule {
|
||||
public:
|
||||
Workspaces(waybar::Bar&, Json::Value);
|
||||
~Workspaces();
|
||||
Workspaces(waybar::Bar&, const Json::Value&);
|
||||
auto update() -> void;
|
||||
operator Gtk::Widget &();
|
||||
private:
|
||||
void worker();
|
||||
void addWorkspace(Json::Value);
|
||||
std::string getIcon(std::string);
|
||||
bool handleScroll(GdkEventScroll*);
|
||||
@ -23,7 +24,7 @@ class Workspaces : public IModule {
|
||||
int getNextWorkspace();
|
||||
|
||||
Bar& bar_;
|
||||
Json::Value config_;
|
||||
const Json::Value& config_;
|
||||
waybar::util::SleeperThread thread_;
|
||||
Gtk::Box box_;
|
||||
util::JsonParser parser_;
|
||||
@ -31,8 +32,7 @@ class Workspaces : public IModule {
|
||||
bool scrolling_;
|
||||
std::unordered_map<int, Gtk::Button> buttons_;
|
||||
Json::Value workspaces_;
|
||||
int ipcfd_;
|
||||
int ipc_eventfd_;
|
||||
Ipc ipc_;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -70,26 +70,13 @@ struct SleeperThread {
|
||||
condvar_.notify_all();
|
||||
}
|
||||
|
||||
void emit()
|
||||
{
|
||||
Glib::signal_idle().connect_once([this] {
|
||||
sig_update.emit();
|
||||
});
|
||||
}
|
||||
|
||||
~SleeperThread()
|
||||
{
|
||||
do_run_ = false;
|
||||
condvar_.notify_all();
|
||||
auto native_handle = thread_.native_handle();
|
||||
pthread_cancel(native_handle);
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
thread_.detach();
|
||||
}
|
||||
|
||||
sigc::signal<void> sig_update;
|
||||
|
||||
private:
|
||||
std::thread thread_;
|
||||
std::condition_variable condvar_;
|
||||
|
@ -7,32 +7,25 @@ namespace waybar::util {
|
||||
struct JsonParser {
|
||||
|
||||
JsonParser()
|
||||
: _reader(_builder.newCharReader())
|
||||
: reader_(builder_.newCharReader())
|
||||
{}
|
||||
|
||||
Json::Value parse(const std::string data)
|
||||
const Json::Value parse(const std::string data) const
|
||||
{
|
||||
Json::Value root;
|
||||
std::string err;
|
||||
if (_reader == nullptr) {
|
||||
throw std::runtime_error("Unable to parse");
|
||||
}
|
||||
bool res =
|
||||
_reader->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||
reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||
if (!res)
|
||||
throw std::runtime_error(err);
|
||||
return root;
|
||||
}
|
||||
|
||||
~JsonParser()
|
||||
{
|
||||
delete _reader;
|
||||
_reader = nullptr;
|
||||
}
|
||||
~JsonParser() = default;
|
||||
|
||||
private:
|
||||
Json::CharReaderBuilder _builder;
|
||||
Json::CharReader *_reader;
|
||||
Json::CharReaderBuilder builder_;
|
||||
std::unique_ptr<Json::CharReader> const reader_;
|
||||
};
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user