Debian Codegraph @

hyprtester/src/tests/clients/keyboard-modifiers.cpp

raw
1 #include "../../hyprctlCompat.hpp"
2 #include "../shared.hpp"
3 #include "tests.hpp"
4 #include "build.hpp"
5
6 #include <hyprutils/os/FileDescriptor.hpp>
7 #include <hyprutils/os/Process.hpp>
8
9 #include <optional>
10 #include <sys/poll.h>
11 #include <csignal>
12 #include <thread>
13
14 using namespace Hyprutils::OS;
15 using namespace Hyprutils::Memory;
16
17 #define SP CSharedPointer
18
19 namespace {
20
21 class CClient {
22 SP<CProcess> proc;
23 std::array<char, 1024> readBuf;
24 CFileDescriptor readFd, writeFd;
25 struct pollfd fds;
26
27 public:
28 CClient();
29 ~CClient();
30 uint32_t getLockedMods();
31 pid_t pid();
32 };
33 }
34
35 CClient::CClient() {
36 Tests::killAllWindows();
37 this->proc = makeShared<CProcess>(binaryDir + "/keyboard-modifiers", 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("{}keyboard-modifiers 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("{}keyboard-modifiers 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 keyboard-modifiers client, read {}", Colors::RED, ret);
74 throw std::exception();
75 }
76
77 int counter = 0;
78 while (Tests::processAlive(this->proc->pid()) && Tests::windowCount() == COUNT_BEFORE) {
79 counter++;
80 std::this_thread::sleep_for(std::chrono::milliseconds(100));
81
82 if (counter > 50) {
83 NLog::log("{}keyboard-modifiers client took too long to open", Colors::RED);
84 throw std::exception();
85 }
86 }
87
88 if (!Tests::processAlive(this->proc->pid())) {
89 NLog::log("{}keyboard-modifiers client not alive", Colors::RED);
90 throw std::exception();
91 }
92
93 if (getFromSocket(std::format("/dispatch hl.dsp.focus({{ window = 'pid:{}' }})", this->proc->pid())) != "ok") {
94 NLog::log("{}Failed to focus keyboard-modifiers client", Colors::RED);
95 throw std::exception();
96 }
97
98 NLog::log("{}Started keyboard-modifiers client", Colors::YELLOW);
99 }
100
101 CClient::~CClient() {
102 getFromSocket("/eval hl.plugin.test.set_mods(0, 0, 0, 0, 0)");
103 getFromSocket("/eval hl.plugin.test.set_mods(1, 0, 0, 0, 0)");
104
105 std::string cmd = "exit\n";
106 write(this->writeFd.get(), cmd.c_str(), cmd.length());
107
108 kill(this->proc->pid(), SIGKILL);
109 this->proc.reset();
110 }
111
112 uint32_t CClient::getLockedMods() {
113 std::string cmd = "get\n";
114 if ((size_t)write(this->writeFd.get(), cmd.c_str(), cmd.length()) != cmd.length())
115 return false;
116
117 if (poll(&this->fds, 1, 1500) != 1 || !(this->fds.revents & POLLIN))
118 return false;
119 ssize_t bytesRead = read(this->fds.fd, this->readBuf.data(), 1023);
120 if (bytesRead == -1)
121 return false;
122
123 this->readBuf[bytesRead] = 0;
124 std::string received = std::string{this->readBuf.data()};
125 received.pop_back();
126
127 try {
128 return std::stoul(received);
129 } catch (...) { return 0; }
130 }
131
132 pid_t CClient::pid() {
133 return this->proc->pid();
134 }
135
136 TEST_CASE(keyboardModifiersMergedOnFocus) {
137 NLog::log("{}Testing keyboard modifiers merged on focus", Colors::GREEN);
138
139 std::optional<CClient> client;
140
141 try {
142 client.emplace();
143 } catch (...) { FAIL_TEST("Couldn't start the client"); }
144
145 EXPECT(client->getLockedMods(), 0u);
146
147 OK(getFromSocket("/eval hl.plugin.test.nullfocus()"));
148 std::this_thread::sleep_for(std::chrono::milliseconds(50));
149
150 OK(getFromSocket("/eval hl.plugin.test.set_mods(0, 0, 0, 2, 0)"));
151 OK(getFromSocket("/eval hl.plugin.test.set_mods(1, 0, 0, 16, 0)"));
152
153 if (getFromSocket(std::format("/dispatch hl.dsp.focus({{ window = 'pid:{}' }})", client->pid())) != "ok") {
154 FAIL_TEST("Failed to refocus keyboard-modifiers client");
155 }
156
157 std::this_thread::sleep_for(std::chrono::milliseconds(50));
158
159 const uint32_t locked = client->getLockedMods();
160 NLog::log("{}Client reports locked mods: {}", Colors::BLUE, locked);
161 EXPECT(locked, 18u);
162 }