| 1 |
#include "../../hyprctlCompat.hpp" |
| 2 |
#include "../../Log.hpp" |
| 3 |
#include "../shared.hpp" |
| 4 |
#include "tests.hpp" |
| 5 |
#include "build.hpp" |
| 6 |
|
| 7 |
#include <hyprutils/os/FileDescriptor.hpp> |
| 8 |
#include <hyprutils/os/Process.hpp> |
| 9 |
|
| 10 |
#include <optional> |
| 11 |
#include <sys/poll.h> |
| 12 |
#include <csignal> |
| 13 |
#include <thread> |
| 14 |
#include <filesystem> |
| 15 |
|
| 16 |
using namespace Hyprutils::OS; |
| 17 |
using namespace Hyprutils::Memory; |
| 18 |
|
| 19 |
#define SP CSharedPointer |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
class CClient { |
| 24 |
SP<CProcess> proc; |
| 25 |
std::array<char, 1024> readBuf; |
| 26 |
CFileDescriptor readFd, writeFd; |
| 27 |
struct pollfd fds; |
| 28 |
|
| 29 |
public: |
| 30 |
CClient(); |
| 31 |
~CClient(); |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
CClient::CClient() { |
| 36 |
Tests::killAllWindows(); |
| 37 |
this->proc = makeShared<CProcess>(binaryDir + "/shortcut-inhibitor", std::vector<std::string>{}); |
| 38 |
|
| 39 |
this->proc->addEnv("WAYLAND_DISPLAY", WLDISPLAY); |
| 40 |
|
| 41 |
int pipeFds1[2], pipeFds2[2]; |
| 42 |
if (pipe(pipeFds1) != 0 || pipe(pipeFds2) != 0) { |
| 43 |
NLog::log("{}Unable to open pipe to client", Colors::RED); |
| 44 |
throw std::exception(); |
| 45 |
} |
| 46 |
|
| 47 |
this->writeFd = CFileDescriptor(pipeFds1[1]); |
| 48 |
this->proc->setStdinFD(pipeFds1[0]); |
| 49 |
|
| 50 |
this->readFd = CFileDescriptor(pipeFds2[0]); |
| 51 |
this->proc->setStdoutFD(pipeFds2[1]); |
| 52 |
|
| 53 |
const int COUNT_BEFORE = Tests::windowCount(); |
| 54 |
this->proc->runAsync(); |
| 55 |
|
| 56 |
close(pipeFds1[0]); |
| 57 |
close(pipeFds2[1]); |
| 58 |
|
| 59 |
this->fds = {.fd = this->readFd.get(), .events = POLLIN}; |
| 60 |
if (poll(&this->fds, 1, 1000) != 1 || !(this->fds.revents & POLLIN)) { |
| 61 |
NLog::log("{}shortcut-inhibitor client failed poll", Colors::RED); |
| 62 |
throw std::exception(); |
| 63 |
} |
| 64 |
|
| 65 |
this->readBuf.fill(0); |
| 66 |
if (read(this->readFd.get(), this->readBuf.data(), this->readBuf.size() - 1) == -1) { |
| 67 |
NLog::log("{}shortcut-inhibitor client read failed", Colors::RED); |
| 68 |
throw std::exception(); |
| 69 |
} |
| 70 |
|
| 71 |
std::string ret = std::string{this->readBuf.data()}; |
| 72 |
if (ret.find("started") == std::string::npos) { |
| 73 |
NLog::log("{}Failed to start shortcut-inhibitor client, read {}", Colors::RED, ret); |
| 74 |
throw std::exception(); |
| 75 |
} |
| 76 |
|
| 77 |
// wait for window to appear |
| 78 |
int counter = 0; |
| 79 |
while (Tests::processAlive(this->proc->pid()) && Tests::windowCount() == COUNT_BEFORE) { |
| 80 |
counter++; |
| 81 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 82 |
|
| 83 |
if (counter > 50) { |
| 84 |
NLog::log("{}shortcut-inhibitor client took too long to open", Colors::RED); |
| 85 |
throw std::exception(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (!Tests::processAlive(this->proc->pid())) { |
| 90 |
NLog::log("{}shortcut-inhibitor client not alive", Colors::RED); |
| 91 |
throw std::exception(); |
| 92 |
} |
| 93 |
|
| 94 |
if (getFromSocket(std::format("/dispatch hl.dsp.focus({{ window = 'pid:{}' }})", this->proc->pid())) != "ok") { |
| 95 |
NLog::log("{}Failed to focus shortcut-inhibitor client", Colors::RED, ret); |
| 96 |
throw std::exception(); |
| 97 |
} |
| 98 |
|
| 99 |
std::string command = "on\n"; |
| 100 |
if (write(this->writeFd.get(), command.c_str(), command.length()) == -1) { |
| 101 |
NLog::log("{}shortcut-inhibitor client write failed", Colors::RED); |
| 102 |
throw std::exception(); |
| 103 |
} |
| 104 |
|
| 105 |
this->readBuf.fill(0); |
| 106 |
if (read(this->readFd.get(), this->readBuf.data(), this->readBuf.size() - 1) == -1) |
| 107 |
throw std::exception(); |
| 108 |
|
| 109 |
ret = std::string{this->readBuf.data()}; |
| 110 |
if (!ret.contains("inhibiting")) { |
| 111 |
NLog::log("{}shortcut-inhibitor client didn't return inhibiting", Colors::RED); |
| 112 |
throw std::exception(); |
| 113 |
} |
| 114 |
|
| 115 |
NLog::log("{}Started shortcut-inhibitor client", Colors::YELLOW); |
| 116 |
} |
| 117 |
|
| 118 |
CClient::~CClient() { |
| 119 |
std::string cmd = "off\n"; |
| 120 |
write(this->writeFd.get(), cmd.c_str(), cmd.length()); |
| 121 |
|
| 122 |
kill(this->proc->pid(), SIGKILL); |
| 123 |
this->proc.reset(); |
| 124 |
} |
| 125 |
|
| 126 |
static std::string flagFile = "/tmp/hyprtester-keybinds.txt"; |
| 127 |
|
| 128 |
static bool checkFlag() { |
| 129 |
bool exists = std::filesystem::exists(flagFile); |
| 130 |
std::filesystem::remove(flagFile); |
| 131 |
return exists; |
| 132 |
} |
| 133 |
|
| 134 |
static bool attemptCheckFlag(int attempts, int intervalMs) { |
| 135 |
for (int i = 0; i < attempts; i++) { |
| 136 |
if (checkFlag()) |
| 137 |
return true; |
| 138 |
|
| 139 |
std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs)); |
| 140 |
} |
| 141 |
|
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
TEST_CASE(shortcutInhibitor) { |
| 146 |
std::optional<CClient> client; |
| 147 |
|
| 148 |
try { |
| 149 |
client.emplace(); |
| 150 |
} catch (...) { FAIL_TEST("Couldn't start the client"); } |
| 151 |
|
| 152 |
NLog::log("{}Testing keybinds", Colors::GREEN); |
| 153 |
|
| 154 |
// wait until flag becomes false (CI timing can vary) |
| 155 |
bool ok = false; |
| 156 |
for (int i = 0; i < 20; ++i) { |
| 157 |
if (!checkFlag()) { |
| 158 |
ok = true; |
| 159 |
break; |
| 160 |
} |
| 161 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 162 |
} |
| 163 |
EXPECT(ok, true); |
| 164 |
|
| 165 |
//basic keybind test |
| 166 |
EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'))"), "ok"); |
| 167 |
OK(getFromSocket("/eval hl.plugin.test.keybind(1, 7, 29)")); |
| 168 |
EXPECT(attemptCheckFlag(20, 50), false); |
| 169 |
OK(getFromSocket("/eval hl.plugin.test.keybind(0, 0, 29)")); |
| 170 |
EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok"); |
| 171 |
|
| 172 |
//keybind bypass flag test |
| 173 |
EXPECT(checkFlag(), false); |
| 174 |
EXPECT(getFromSocket("/eval hl.bind('SUPER + Y', hl.dsp.exec_cmd('touch " + flagFile + "'), { dont_inhibit = true })"), "ok"); |
| 175 |
OK(getFromSocket("/eval hl.plugin.test.keybind(1, 7, 29)")); |
| 176 |
EXPECT(attemptCheckFlag(20, 50), true); |
| 177 |
OK(getFromSocket("/eval hl.plugin.test.keybind(0, 0, 29)")); |
| 178 |
EXPECT(getFromSocket("/eval hl.unbind('SUPER + Y')"), "ok"); |
| 179 |
|
| 180 |
NLog::log("{}Testing gestures", Colors::GREEN); |
| 181 |
//basic gesture test |
| 182 |
OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)")); |
| 183 |
EXPECT_NOT_CONTAINS(getFromSocket("/activewindow"), "floating: 1"); |
| 184 |
|
| 185 |
//gesture bypass flag test |
| 186 |
OK(getFromSocket("/eval hl.plugin.test.gesture('right', 2)")); |
| 187 |
EXPECT_CONTAINS(getFromSocket("/activewindow"), "floating: 1"); |
| 188 |
} |