| 1 |
#include "hyprctlCompat.hpp" |
| 2 |
#include "shared.hpp" |
| 3 |
|
| 4 |
#include <pwd.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <sys/stat.h> |
| 7 |
#include <sys/types.h> |
| 8 |
#include <sys/un.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include <filesystem> |
| 12 |
#include <fstream> |
| 13 |
#include <algorithm> |
| 14 |
#include <csignal> |
| 15 |
#include <cerrno> |
| 16 |
#include <print> |
| 17 |
#include <hyprutils/memory/Casts.hpp> |
| 18 |
using namespace Hyprutils::Memory; |
| 19 |
|
| 20 |
static int getUID() { |
| 21 |
const auto UID = getuid(); |
| 22 |
const auto PWUID = getpwuid(UID); |
| 23 |
return PWUID ? PWUID->pw_uid : UID; |
| 24 |
} |
| 25 |
|
| 26 |
static std::string getRuntimeDir() { |
| 27 |
const auto XDG = getenv("XDG_RUNTIME_DIR"); |
| 28 |
|
| 29 |
if (!XDG) { |
| 30 |
const std::string USERID = std::to_string(getUID()); |
| 31 |
return "/run/user/" + USERID + "/hypr"; |
| 32 |
} |
| 33 |
|
| 34 |
return std::string{XDG} + "/hypr"; |
| 35 |
} |
| 36 |
|
| 37 |
std::vector<SInstanceData> instances() { |
| 38 |
std::vector<SInstanceData> result; |
| 39 |
|
| 40 |
try { |
| 41 |
if (!std::filesystem::exists(getRuntimeDir())) |
| 42 |
return {}; |
| 43 |
} catch (std::exception& e) { return {}; } |
| 44 |
|
| 45 |
for (const auto& el : std::filesystem::directory_iterator(getRuntimeDir())) { |
| 46 |
if (!std::filesystem::exists(el.path() / "hyprland.lock")) |
| 47 |
continue; |
| 48 |
|
| 49 |
// read lock |
| 50 |
SInstanceData* data = &result.emplace_back(); |
| 51 |
data->id = el.path().filename().string(); |
| 52 |
|
| 53 |
try { |
| 54 |
data->time = std::stoull(data->id.substr(data->id.find_first_of('_') + 1, data->id.find_last_of('_') - (data->id.find_first_of('_') + 1))); |
| 55 |
} catch (std::exception& e) { continue; } |
| 56 |
|
| 57 |
// read file |
| 58 |
std::ifstream ifs(el.path().string() + "/hyprland.lock"); |
| 59 |
|
| 60 |
int i = 0; |
| 61 |
for (std::string line; std::getline(ifs, line); ++i) { |
| 62 |
if (i == 0) { |
| 63 |
try { |
| 64 |
data->pid = std::stoull(line); |
| 65 |
} catch (std::exception& e) { continue; } |
| 66 |
} else if (i == 1) { |
| 67 |
data->wlSocket = line; |
| 68 |
} else |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
ifs.close(); |
| 73 |
} |
| 74 |
|
| 75 |
std::erase_if(result, [&](const auto& el) { return kill(el.pid, 0) != 0 && errno == ESRCH; }); |
| 76 |
|
| 77 |
std::ranges::sort(result, [&](const auto& a, const auto& b) { return a.time < b.time; }); |
| 78 |
|
| 79 |
return result; |
| 80 |
} |
| 81 |
|
| 82 |
std::string getFromSocket(const std::string& cmd) { |
| 83 |
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); |
| 84 |
|
| 85 |
auto t = timeval{.tv_sec = 5, .tv_usec = 0}; |
| 86 |
setsockopt(SERVERSOCKET, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(struct timeval)); |
| 87 |
|
| 88 |
if (SERVERSOCKET < 0) { |
| 89 |
std::println("socket: Couldn't open a socket (1)"); |
| 90 |
return ""; |
| 91 |
} |
| 92 |
|
| 93 |
sockaddr_un serverAddress = {0}; |
| 94 |
serverAddress.sun_family = AF_UNIX; |
| 95 |
|
| 96 |
std::string socketPath = getRuntimeDir() + "/" + HIS + "/.socket.sock"; |
| 97 |
|
| 98 |
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1); |
| 99 |
|
| 100 |
if (connect(SERVERSOCKET, rc<sockaddr*>(&serverAddress), SUN_LEN(&serverAddress)) < 0) { |
| 101 |
std::println("Couldn't connect to {}. (3)", socketPath); |
| 102 |
return ""; |
| 103 |
} |
| 104 |
|
| 105 |
auto sizeWritten = write(SERVERSOCKET, cmd.c_str(), cmd.length()); |
| 106 |
|
| 107 |
if (sizeWritten < 0) { |
| 108 |
std::println("Couldn't write (4)"); |
| 109 |
return ""; |
| 110 |
} |
| 111 |
|
| 112 |
std::string reply = ""; |
| 113 |
char buffer[8192] = {0}; |
| 114 |
|
| 115 |
sizeWritten = read(SERVERSOCKET, buffer, 8192); |
| 116 |
|
| 117 |
if (sizeWritten < 0) { |
| 118 |
if (errno == EWOULDBLOCK) |
| 119 |
std::println("Hyprland IPC didn't respond in time"); |
| 120 |
std::println("Couldn't read (5)"); |
| 121 |
return ""; |
| 122 |
} |
| 123 |
|
| 124 |
reply += std::string(buffer, sizeWritten); |
| 125 |
|
| 126 |
while (sizeWritten == 8192) { |
| 127 |
sizeWritten = read(SERVERSOCKET, buffer, 8192); |
| 128 |
if (sizeWritten < 0) { |
| 129 |
std::println("Couldn't read (5)"); |
| 130 |
return ""; |
| 131 |
} |
| 132 |
reply += std::string(buffer, sizeWritten); |
| 133 |
} |
| 134 |
|
| 135 |
close(SERVERSOCKET); |
| 136 |
|
| 137 |
return reply; |
| 138 |
} |