Debian Codegraph @

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

raw
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 <unistd.h>
13 #include <csignal>
14 #include <thread>
15
16 using namespace Hyprutils::OS;
17 using namespace Hyprutils::Memory;
18
19 #define SP CSharedPointer
20
21 namespace {
22 class CClient {
23 SP<CProcess> proc;
24 std::array<char, 1024> readBuf;
25 CFileDescriptor readFd, writeFd;
26 struct pollfd fds;
27
28 public:
29 CClient();
30 ~CClient();
31 int getLastDelta();
32 };
33 }
34
35 CClient::CClient() {
36 this->proc = makeShared<CProcess>(binaryDir + "/pointer-scroll", std::vector<std::string>{});
37
38 this->proc->addEnv("WAYLAND_DISPLAY", WLDISPLAY);
39
40 int pipeFds1[2], pipeFds2[2];
41 if (pipe(pipeFds1) != 0 || pipe(pipeFds2) != 0) {
42 NLog::log("{}Unable to open pipe to client", Colors::RED);
43 throw std::exception();
44 }
45
46 this->writeFd = CFileDescriptor(pipeFds1[1]);
47 this->proc->setStdinFD(pipeFds1[0]);
48
49 this->readFd = CFileDescriptor(pipeFds2[0]);
50 this->proc->setStdoutFD(pipeFds2[1]);
51
52 const int COUNT_BEFORE = Tests::windowCount();
53 this->proc->runAsync();
54
55 close(pipeFds1[0]);
56 close(pipeFds2[1]);
57
58 this->fds = {.fd = this->readFd.get(), .events = POLLIN};
59 if (poll(&this->fds, 1, 1000) != 1 || !(this->fds.revents & POLLIN))
60 throw std::exception();
61
62 this->readBuf.fill(0);
63 if (read(this->readFd.get(), this->readBuf.data(), this->readBuf.size() - 1) == -1)
64 throw std::exception();
65
66 std::string ret = std::string{this->readBuf.data()};
67 if (ret.find("started") == std::string::npos) {
68 NLog::log("{}Failed to start pointer-scroll client, read {}", Colors::RED, ret);
69 throw std::exception();
70 }
71
72 // wait for window to appear
73 int counter = 0;
74 while (Tests::processAlive(this->proc->pid()) && Tests::windowCount() == COUNT_BEFORE) {
75 counter++;
76 std::this_thread::sleep_for(std::chrono::milliseconds(100));
77
78 if (counter > 50) {
79 NLog::log("{}pointer-scroll client took too long to open", Colors::RED);
80 throw std::exception();
81 }
82 }
83
84 if (getFromSocket(std::format("/dispatch hl.dsp.window.set_prop({{ window = 'pid:{}', prop = 'no_anim', value = '1' }})", this->proc->pid())) != "ok") {
85 NLog::log("{}Failed to disable animations for client window", Colors::RED, ret);
86 throw std::exception();
87 }
88
89 if (getFromSocket(std::format("/dispatch hl.dsp.focus({{ window = 'pid:{}' }})", this->proc->pid())) != "ok") {
90 NLog::log("{}Failed to focus pointer-scroll client", Colors::RED, ret);
91 throw std::exception();
92 }
93
94 NLog::log("{}Started pointer-scroll client", Colors::YELLOW);
95 }
96
97 CClient::~CClient() {
98 std::string cmd = "exit\n";
99 write(this->writeFd.get(), cmd.c_str(), cmd.length());
100
101 kill(this->proc->pid(), SIGKILL);
102 this->proc.reset();
103 }
104
105 int CClient::getLastDelta() {
106 std::string cmd = "hypr";
107 if ((size_t)write(this->writeFd.get(), cmd.c_str(), cmd.length()) != cmd.length())
108 return false;
109
110 if (poll(&this->fds, 1, 1500) != 1 || !(this->fds.revents & POLLIN))
111 return false;
112 ssize_t bytesRead = read(this->fds.fd, this->readBuf.data(), 1023);
113 if (bytesRead == -1)
114 return false;
115
116 this->readBuf[bytesRead] = 0;
117 std::string received = std::string{this->readBuf.data()};
118 received.pop_back();
119
120 try {
121 return std::stoi(received);
122 } catch (...) { return -1; }
123 }
124
125 static bool sendScroll(int delta) {
126 return getFromSocket(std::format("/eval hl.plugin.test.scroll({})", delta)) == "ok";
127 }
128
129 TEST_CASE(pointerScroll) {
130 NLog::log("{}Skipping pointerScroll test (unstable in CI / headless environments)", Colors::YELLOW);
131 return;
132
133 std::optional<CClient> client;
134 try {
135 client.emplace();
136 } catch (...) { FAIL_TEST("Couldn't start the client"); }
137
138 EXPECT(getFromSocket("r/eval hl.config({ input = { emulate_discrete_scroll = 0 } })"), "ok");
139
140 EXPECT(sendScroll(10), true);
141 EXPECT(client->getLastDelta(), 10);
142
143 EXPECT(getFromSocket("r/eval hl.config({ input = { scroll_factor = 2 } })"), "ok");
144 EXPECT(sendScroll(10), true);
145 EXPECT(client->getLastDelta(), 20);
146
147 EXPECT(getFromSocket("r/eval hl.device({ name = 'test-mouse-1', scroll_factor = 3 })"), "ok");
148 EXPECT(sendScroll(10), true);
149 EXPECT(client->getLastDelta(), 30);
150
151 EXPECT(getFromSocket("r/dispatch hl.dsp.window.set_prop({ window = 'active', prop = 'scroll_mouse', value = '4' })"), "ok");
152 EXPECT(sendScroll(10), true);
153 EXPECT(client->getLastDelta(), 40);
154
155 NLog::log("{}Reloading the config", Colors::YELLOW);
156 OK(getFromSocket("/reload"));
157 }