Debian Codegraph @

hyprtester/src/tests/clients/pointer-warp.cpp

raw
1 #include "../../hyprctlCompat.hpp"
2 #include "../../Log.hpp"
3 #include "../../shared.hpp"
4 #include "../shared.hpp"
5 #include "tests.hpp"
6 #include "build.hpp"
7
8 #include <hyprutils/os/FileDescriptor.hpp>
9 #include <hyprutils/os/Process.hpp>
10
11 #include <optional>
12 #include <sys/poll.h>
13 #include <unistd.h>
14 #include <csignal>
15 #include <thread>
16
17 using namespace Hyprutils::OS;
18 using namespace Hyprutils::Memory;
19
20 #define SP CSharedPointer
21
22 namespace {
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 bool sendWarp(int x, int y);
33 };
34 }
35
36 CClient::CClient() {
37 this->proc = makeShared<CProcess>(binaryDir + "/pointer-warp", 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 throw std::exception();
62
63 this->readBuf.fill(0);
64 if (read(this->readFd.get(), this->readBuf.data(), this->readBuf.size() - 1) == -1)
65 throw std::exception();
66
67 std::string ret = std::string{this->readBuf.data()};
68 if (ret.find("started") == std::string::npos) {
69 NLog::log("{}Failed to start pointer-warp client, read {}", Colors::RED, ret);
70 throw std::exception();
71 }
72
73 // wait for window to appear
74 int counter = 0;
75 while (Tests::processAlive(this->proc->pid()) && Tests::windowCount() == COUNT_BEFORE) {
76 counter++;
77 std::this_thread::sleep_for(std::chrono::milliseconds(100));
78
79 if (counter > 100) {
80 NLog::log("{}pointer-warp client took too long to open, continuing", Colors::YELLOW);
81 throw std::exception();
82 }
83 }
84
85 if (getFromSocket(std::format("/dispatch hl.dsp.window.set_prop({{ window = 'pid:{}', prop = 'no_anim', value = '1' }})", this->proc->pid())) != "ok") {
86 NLog::log("{}Failed to disable animations for client window", Colors::RED, ret);
87 throw std::exception();
88 }
89
90 if (getFromSocket(std::format("/dispatch hl.dsp.focus({{ window = 'pid:{}' }})", this->proc->pid())) != "ok") {
91 NLog::log("{}Failed to focus pointer-warp client", Colors::RED, ret);
92 throw std::exception();
93 }
94
95 NLog::log("{}Started pointer-warp client", Colors::YELLOW);
96 }
97
98 CClient::~CClient() {
99 std::string cmd = "exit\n";
100 write(this->writeFd.get(), cmd.c_str(), cmd.length());
101
102 kill(this->proc->pid(), SIGKILL);
103 this->proc.reset();
104 }
105
106 // format is like below
107 // "warp 20 20\n" would ask to warp cursor to x=20,y=20 in surface local coords
108 bool CClient::sendWarp(int x, int y) {
109 std::string cmd = std::format("warp {} {}\n", x, y);
110 if ((size_t)write(this->writeFd.get(), cmd.c_str(), cmd.length()) != cmd.length())
111 return false;
112
113 if (poll(&this->fds, 1, 1500) != 1 || !(this->fds.revents & POLLIN))
114 return false;
115 ssize_t bytesRead = read(this->fds.fd, this->readBuf.data(), 1023);
116 if (bytesRead == -1)
117 return false;
118
119 this->readBuf[bytesRead] = 0;
120 std::string recieved = std::string{this->readBuf.data()};
121 recieved.pop_back();
122
123 return true;
124 }
125
126 static bool isCursorPos(int x, int y) {
127 // TODO: add a better way to do this using test plugin?
128 std::string res = getFromSocket("/cursorpos");
129 if (res == "error") {
130 NLog::log("{}Cursorpos err'd: {}", Colors::RED, res);
131 return false;
132 }
133
134 auto it = res.find_first_of(' ');
135 if (res.at(it - 1) != ',') {
136 NLog::log("{}Cursorpos err'd: {}", Colors::RED, res);
137 return false;
138 }
139
140 int cursorX = std::stoi(res.substr(0, it - 1));
141 int cursorY = std::stoi(res.substr(it + 1));
142
143 // somehow this is always gives 1 less than surfbox->pos()??
144 res = getFromSocket("/activewindow");
145 it = res.find("at: ") + 4;
146 res = res.substr(it, res.find_first_of('\n', it) - it);
147
148 it = res.find_first_of(',');
149 int clientX = cursorX - std::stoi(res.substr(0, it)) + 1;
150 int clientY = cursorY - std::stoi(res.substr(it + 1)) + 1;
151
152 return clientX == x && clientY == y;
153 }
154
155 TEST_CASE(pointerWarp) {
156 NLog::log("{}Skipping pointerWarp test (unstable in CI / headless environments)", Colors::YELLOW);
157 return;
158
159 std::optional<CClient> client;
160 try {
161 client.emplace();
162 } catch (...) { FAIL_TEST("Couldn't start the client"); }
163
164 EXPECT(client->sendWarp(100, 100), true);
165 EXPECT(isCursorPos(100, 100), true);
166
167 EXPECT(client->sendWarp(0, 0), true);
168 EXPECT(isCursorPos(0, 0), true);
169
170 EXPECT(client->sendWarp(200, 200), true);
171 EXPECT(isCursorPos(200, 200), true);
172
173 EXPECT(client->sendWarp(100, -100), true);
174 EXPECT(isCursorPos(200, 200), true);
175
176 EXPECT(client->sendWarp(234, 345), true);
177 EXPECT(isCursorPos(234, 345), true);
178
179 EXPECT(client->sendWarp(-1, -1), true);
180 EXPECT(isCursorPos(234, 345), true);
181
182 EXPECT(client->sendWarp(1, -1), true);
183 EXPECT(isCursorPos(234, 345), true);
184
185 EXPECT(client->sendWarp(13, 37), true);
186 EXPECT(isCursorPos(13, 37), true);
187
188 EXPECT(client->sendWarp(-100, 100), true);
189 EXPECT(isCursorPos(13, 37), true);
190
191 EXPECT(client->sendWarp(-1, 1), true);
192 EXPECT(isCursorPos(13, 37), true);
193 }