| 1 |
#include "shared.hpp" |
| 2 |
#include <cassert> |
| 3 |
#include <csignal> |
| 4 |
#include <cerrno> |
| 5 |
#include <thread> |
| 6 |
#include <print> |
| 7 |
#include <fstream> |
| 8 |
#include "../shared.hpp" |
| 9 |
#include "../hyprctlCompat.hpp" |
| 10 |
|
| 11 |
using namespace Hyprutils::OS; |
| 12 |
using namespace Hyprutils::Memory; |
| 13 |
|
| 14 |
CUniquePointer<CProcess> Tests::spawnKitty(const std::string& class_, const std::vector<std::string> args) { |
| 15 |
const auto COUNT_BEFORE = windowCount(); |
| 16 |
|
| 17 |
std::vector<std::string> programArgs = args; |
| 18 |
if (!class_.empty()) { |
| 19 |
programArgs.insert(programArgs.begin(), "--class"); |
| 20 |
programArgs.insert(programArgs.begin() + 1, class_); |
| 21 |
} |
| 22 |
CUniquePointer<CProcess> kitty = makeUnique<CProcess>("kitty", programArgs); |
| 23 |
kitty->addEnv("WAYLAND_DISPLAY", WLDISPLAY); |
| 24 |
kitty->runAsync(); |
| 25 |
|
| 26 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 27 |
|
| 28 |
// wait while kitty spawns |
| 29 |
int counter = 0; |
| 30 |
while (processAlive(kitty->pid()) && windowCount() == COUNT_BEFORE) { |
| 31 |
counter++; |
| 32 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 33 |
|
| 34 |
if (counter > 50) |
| 35 |
return nullptr; |
| 36 |
} |
| 37 |
|
| 38 |
if (!processAlive(kitty->pid())) |
| 39 |
return nullptr; |
| 40 |
|
| 41 |
return kitty; |
| 42 |
} |
| 43 |
|
| 44 |
CUniquePointer<CProcess> Tests::spawnLayerKitty(const std::string& namespace_, const std::vector<std::string> args) { |
| 45 |
std::vector<std::string> programArgs = args; |
| 46 |
if (!namespace_.empty()) { |
| 47 |
programArgs.insert(programArgs.begin(), "--class"); |
| 48 |
programArgs.insert(programArgs.begin() + 1, namespace_); |
| 49 |
} |
| 50 |
|
| 51 |
programArgs.insert(programArgs.begin(), "+kitten"); |
| 52 |
programArgs.insert(programArgs.begin() + 1, "panel"); |
| 53 |
|
| 54 |
CUniquePointer<CProcess> kitty = makeUnique<CProcess>("kitty", programArgs); |
| 55 |
kitty->addEnv("WAYLAND_DISPLAY", WLDISPLAY); |
| 56 |
kitty->runAsync(); |
| 57 |
|
| 58 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 59 |
|
| 60 |
// wait while the layer spawns |
| 61 |
int counter = 0; |
| 62 |
while (processAlive(kitty->pid()) && countOccurrences(getFromSocket("/layers"), std::format("pid: {}", kitty->pid())) == 0) { |
| 63 |
counter++; |
| 64 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 65 |
|
| 66 |
if (counter > 50) |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
if (!processAlive(kitty->pid())) |
| 71 |
return nullptr; |
| 72 |
|
| 73 |
return kitty; |
| 74 |
} |
| 75 |
|
| 76 |
bool Tests::processAlive(pid_t pid) { |
| 77 |
errno = 0; |
| 78 |
int ret = kill(pid, 0); |
| 79 |
return ret != -1 || errno != ESRCH; |
| 80 |
} |
| 81 |
|
| 82 |
int Tests::windowCount() { |
| 83 |
return countOccurrences(getFromSocket("/clients"), "focusHistoryID: "); |
| 84 |
} |
| 85 |
|
| 86 |
int Tests::countOccurrences(const std::string& in, const std::string& what) { |
| 87 |
int cnt = 0; |
| 88 |
auto pos = in.find(what); |
| 89 |
while (pos != std::string::npos) { |
| 90 |
cnt++; |
| 91 |
pos = in.find(what, pos + what.length()); |
| 92 |
} |
| 93 |
|
| 94 |
return cnt; |
| 95 |
} |
| 96 |
|
| 97 |
void Tests::sync(int rounds) { |
| 98 |
for (int i = 0; i < rounds; ++i) |
| 99 |
getFromSocket("/version"); |
| 100 |
} |
| 101 |
|
| 102 |
bool Tests::killAllWindows() { |
| 103 |
auto str = getFromSocket("/clients"); |
| 104 |
auto pos = str.find("Window "); |
| 105 |
while (pos != std::string::npos) { |
| 106 |
auto pos2 = str.find(" -> ", pos); |
| 107 |
getFromSocket("/dispatch hl.dsp.window.kill({ window = 'address:0x" + str.substr(pos + 7, pos2 - pos - 7) + "' })"); |
| 108 |
pos = str.find("Window ", pos + 5); |
| 109 |
} |
| 110 |
|
| 111 |
int counter = 0; |
| 112 |
while (Tests::windowCount() != 0) { |
| 113 |
counter++; |
| 114 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 115 |
|
| 116 |
if (counter > 50) { |
| 117 |
std::println("{}Timed out waiting for windows to close", Colors::RED); |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
void Tests::waitUntilWindowsN(int n) { |
| 126 |
int counter = 0; |
| 127 |
while (Tests::windowCount() != n) { |
| 128 |
counter++; |
| 129 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 130 |
|
| 131 |
if (counter > 50) { |
| 132 |
std::println("{}Timed out waiting for windows", Colors::RED); |
| 133 |
return; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
int Tests::layerCount() { |
| 139 |
return countOccurrences(getFromSocket("/layers"), "namespace: "); |
| 140 |
} |
| 141 |
|
| 142 |
bool Tests::killAllLayers() { |
| 143 |
auto str = getFromSocket("/layers"); |
| 144 |
auto pos = str.find("pid: "); |
| 145 |
while (pos != std::string::npos) { |
| 146 |
auto pid = stoi(str.substr(pos + 5, str.find('\n', pos))); |
| 147 |
kill(pid, 15); |
| 148 |
|
| 149 |
// we need to wait for a bit because for some reason otherwise we'll end up |
| 150 |
// with layers with pid -1 if they are all removed at the same time |
| 151 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 152 |
|
| 153 |
pos = str.find("pid: ", pos + 5); |
| 154 |
} |
| 155 |
|
| 156 |
int counter = 0; |
| 157 |
while (Tests::layerCount() != 0) { |
| 158 |
counter++; |
| 159 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 160 |
|
| 161 |
if (counter > 50) { |
| 162 |
std::println("{}Timed out waiting for layers to close", Colors::RED); |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
std::string Tests::execAndGet(const std::string& cmd) { |
| 171 |
CProcess proc("/bin/sh", {"-c", cmd}); |
| 172 |
|
| 173 |
if (!proc.runSync()) { |
| 174 |
return "error"; |
| 175 |
} |
| 176 |
|
| 177 |
return proc.stdOut(); |
| 178 |
} |
| 179 |
|
| 180 |
bool Tests::writeFile(const std::string& name, const std::string& contents) { |
| 181 |
std::ofstream of(name, std::ios::trunc); |
| 182 |
if (!of.good()) |
| 183 |
return false; |
| 184 |
|
| 185 |
of << contents; |
| 186 |
of.close(); |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
std::string Tests::getAttribute(const std::string& hyprlandResponse, std::string attr) { |
| 192 |
attr += ": "; |
| 193 |
auto pos = hyprlandResponse.find(attr); |
| 194 |
if (pos == std::string::npos) { |
| 195 |
NLog::log("{}Window attribute not found: '{}'", Colors::RED, attr); |
| 196 |
return "Wrong window attribute"; |
| 197 |
} |
| 198 |
pos += attr.size(); |
| 199 |
auto pos2 = hyprlandResponse.find('\n', pos); |
| 200 |
return hyprlandResponse.substr(pos, pos2 - pos); |
| 201 |
} |