2018-08-08 21:54:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-19 09:09:06 +00:00
|
|
|
#include <sigc++/sigc++.h>
|
2018-08-20 12:50:45 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2019-04-18 15:52:00 +00:00
|
|
|
#include <unistd.h>
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
#include <cstring>
|
2019-04-23 09:41:49 +00:00
|
|
|
#include <memory>
|
2019-04-19 14:48:02 +00:00
|
|
|
#include <mutex>
|
2023-01-16 18:27:21 +00:00
|
|
|
#include <string>
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2018-08-08 21:54:58 +00:00
|
|
|
#include "ipc.hpp"
|
2019-08-28 02:43:03 +00:00
|
|
|
#include "util/sleeper_thread.hpp"
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-08-20 12:50:45 +00:00
|
|
|
namespace waybar::modules::sway {
|
|
|
|
|
|
|
|
class Ipc {
|
2019-04-18 15:52:00 +00:00
|
|
|
public:
|
2018-12-26 10:13:36 +00:00
|
|
|
Ipc();
|
|
|
|
~Ipc();
|
|
|
|
|
|
|
|
struct ipc_response {
|
2022-04-06 06:37:19 +00:00
|
|
|
uint32_t size;
|
|
|
|
uint32_t type;
|
2019-05-07 11:43:48 +00:00
|
|
|
std::string payload;
|
2018-12-26 10:13:36 +00:00
|
|
|
};
|
|
|
|
|
2019-05-07 11:43:48 +00:00
|
|
|
sigc::signal<void, const struct ipc_response &> signal_event;
|
|
|
|
sigc::signal<void, const struct ipc_response &> signal_cmd;
|
2019-04-19 09:09:06 +00:00
|
|
|
|
2019-04-19 14:48:02 +00:00
|
|
|
void sendCmd(uint32_t type, const std::string &payload = "");
|
|
|
|
void subscribe(const std::string &payload);
|
|
|
|
void handleEvent();
|
2019-08-28 02:43:03 +00:00
|
|
|
void setWorker(std::function<void()> &&func);
|
2018-12-26 10:13:36 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
protected:
|
2018-12-26 10:13:36 +00:00
|
|
|
static inline const std::string ipc_magic_ = "i3-ipc";
|
2022-04-06 06:37:19 +00:00
|
|
|
static inline const size_t ipc_header_size_ = ipc_magic_.size() + 8;
|
2018-12-26 10:13:36 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
const std::string getSocketPath() const;
|
|
|
|
int open(const std::string &) const;
|
2019-04-19 14:48:02 +00:00
|
|
|
struct ipc_response send(int fd, uint32_t type, const std::string &payload = "");
|
|
|
|
struct ipc_response recv(int fd);
|
2018-12-26 10:13:36 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
int fd_;
|
|
|
|
int fd_event_;
|
|
|
|
std::mutex mutex_;
|
2019-08-28 02:43:03 +00:00
|
|
|
util::SleeperThread thread_;
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
} // namespace waybar::modules::sway
|