2022-07-01 12:46:28 +02:00
|
|
|
#pragma once
|
2023-09-02 15:22:26 -05:00
|
|
|
|
2026-02-09 13:48:28 -06:00
|
|
|
#include <atomic>
|
2024-05-28 18:52:19 -05:00
|
|
|
#include <filesystem>
|
2022-11-24 12:28:52 +01:00
|
|
|
#include <list>
|
2021-05-27 15:40:54 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
2025-02-19 00:58:08 +01:00
|
|
|
#include <thread>
|
2023-09-02 15:22:26 -05:00
|
|
|
#include <utility>
|
2023-07-01 11:12:14 +02:00
|
|
|
|
2023-06-28 02:52:01 +03:00
|
|
|
#include "util/json.hpp"
|
2022-07-01 12:46:28 +02:00
|
|
|
|
|
|
|
|
namespace waybar::modules::hyprland {
|
2022-11-09 09:34:19 +01:00
|
|
|
|
|
|
|
|
class EventHandler {
|
2022-11-24 12:28:52 +01:00
|
|
|
public:
|
2022-11-09 09:34:19 +01:00
|
|
|
virtual void onEvent(const std::string& ev) = 0;
|
|
|
|
|
virtual ~EventHandler() = default;
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-12 19:39:36 +02:00
|
|
|
/// If you want to use the Hyprland IPC, simply use IPC::inst() to get the singleton instance.
|
|
|
|
|
/// Do not create multiple instances.
|
2022-07-01 12:46:28 +02:00
|
|
|
class IPC {
|
2025-08-12 19:39:36 +02:00
|
|
|
protected:
|
|
|
|
|
IPC(); // use IPC::inst() instead.
|
|
|
|
|
|
2021-05-27 15:40:54 +02:00
|
|
|
public:
|
2025-02-19 00:58:08 +01:00
|
|
|
~IPC();
|
|
|
|
|
static IPC& inst();
|
2022-07-01 12:46:28 +02:00
|
|
|
|
2024-02-25 12:11:22 +01:00
|
|
|
void registerForIPC(const std::string& ev, EventHandler* ev_handler);
|
|
|
|
|
void unregisterForIPC(EventHandler* handler);
|
2022-07-01 12:46:28 +02:00
|
|
|
|
2024-01-06 12:57:27 +08:00
|
|
|
static std::string getSocket1Reply(const std::string& rq);
|
2023-06-28 02:52:01 +03:00
|
|
|
Json::Value getSocket1JsonReply(const std::string& rq);
|
2024-06-08 23:52:58 -05:00
|
|
|
static std::filesystem::path getSocketFolder(const char* instanceSig);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
static std::filesystem::path socketFolder_;
|
2022-07-01 12:46:28 +02:00
|
|
|
|
2021-05-27 15:40:54 +02:00
|
|
|
private:
|
2025-02-19 00:58:08 +01:00
|
|
|
void socketListener();
|
2021-05-27 15:40:54 +02:00
|
|
|
void parseIPC(const std::string&);
|
2022-07-01 12:46:28 +02:00
|
|
|
|
2025-02-19 21:15:18 +01:00
|
|
|
std::thread ipcThread_;
|
2024-02-25 12:11:22 +01:00
|
|
|
std::mutex callbackMutex_;
|
2026-02-09 13:48:28 -06:00
|
|
|
std::mutex socketMutex_;
|
2024-02-25 12:11:22 +01:00
|
|
|
util::JsonParser parser_;
|
|
|
|
|
std::list<std::pair<std::string, EventHandler*>> callbacks_;
|
2026-02-09 13:48:28 -06:00
|
|
|
int socketfd_ = -1; // the hyprland socket file descriptor
|
|
|
|
|
pid_t socketOwnerPid_ = -1;
|
|
|
|
|
std::atomic<bool> running_ = true; // the ipcThread will stop running when this is false
|
2022-07-01 12:46:28 +02:00
|
|
|
};
|
2021-05-27 15:40:54 +02:00
|
|
|
}; // namespace waybar::modules::hyprland
|