2016-11-13 18:05:30 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "utils/concurrency.hpp"
|
2016-11-25 07:42:31 +00:00
|
|
|
#include "utils/functional.hpp"
|
2016-11-13 18:05:30 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-13 18:05:30 +00:00
|
|
|
|
2016-11-14 08:21:18 +00:00
|
|
|
/**
|
|
|
|
* Message types
|
|
|
|
*/
|
|
|
|
struct ipc_command {
|
2016-11-20 22:29:42 +00:00
|
|
|
static constexpr const char* prefix{"cmd:"};
|
2016-11-14 08:21:18 +00:00
|
|
|
string payload;
|
|
|
|
};
|
|
|
|
struct ipc_hook {
|
2016-11-20 22:29:42 +00:00
|
|
|
static constexpr const char* prefix{"hook:"};
|
2016-11-14 08:21:18 +00:00
|
|
|
string payload;
|
|
|
|
};
|
2016-11-18 17:37:52 +00:00
|
|
|
struct ipc_action {
|
2016-11-20 22:29:42 +00:00
|
|
|
static constexpr const char* prefix{"action:"};
|
2016-11-18 17:37:52 +00:00
|
|
|
string payload;
|
|
|
|
};
|
2016-11-14 08:21:18 +00:00
|
|
|
|
2016-11-13 18:05:30 +00:00
|
|
|
/**
|
|
|
|
* Component used for inter-process communication.
|
|
|
|
*
|
|
|
|
* A unique messaging channel will be setup for each
|
|
|
|
* running process which will allow messages and
|
|
|
|
* events to be sent to the process externally.
|
|
|
|
*/
|
|
|
|
class ipc {
|
|
|
|
public:
|
|
|
|
explicit ipc(const logger& logger) : m_log(logger) {}
|
|
|
|
~ipc();
|
|
|
|
|
2016-11-14 08:21:18 +00:00
|
|
|
void attach_callback(callback<const ipc_command&>&& cb);
|
|
|
|
void attach_callback(callback<const ipc_hook&>&& cb);
|
2016-11-18 17:37:52 +00:00
|
|
|
void attach_callback(callback<const ipc_action&>&& cb);
|
2016-11-13 18:05:30 +00:00
|
|
|
void receive_messages();
|
|
|
|
|
|
|
|
protected:
|
2016-11-14 08:21:18 +00:00
|
|
|
void parse(const string& payload) const;
|
|
|
|
void delegate(const ipc_command& msg) const;
|
|
|
|
void delegate(const ipc_hook& msg) const;
|
2016-11-18 17:37:52 +00:00
|
|
|
void delegate(const ipc_action& msg) const;
|
2016-11-13 18:05:30 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const logger& m_log;
|
|
|
|
|
2016-11-14 08:21:18 +00:00
|
|
|
vector<callback<const ipc_command&>> m_command_callbacks;
|
|
|
|
vector<callback<const ipc_hook&>> m_hook_callbacks;
|
2016-11-18 17:37:52 +00:00
|
|
|
vector<callback<const ipc_action&>> m_action_callbacks;
|
2016-11-14 08:21:18 +00:00
|
|
|
|
2016-11-13 18:05:30 +00:00
|
|
|
stateflag m_running{false};
|
|
|
|
|
|
|
|
string m_fifo;
|
|
|
|
int m_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Configure injection module
|
|
|
|
*/
|
|
|
|
template <typename T = unique_ptr<ipc>>
|
|
|
|
di::injector<T> configure_ipc() {
|
|
|
|
return di::make_injector(configure_logger());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|