portability: replace epoll with poll

This commit is contained in:
Raphael Robatsch 2021-11-16 19:14:45 +01:00
parent 97176b3a10
commit 4b54d4b3d1
2 changed files with 66 additions and 85 deletions

View File

@ -10,7 +10,6 @@ state.
Important! dwl must have the wayland-ipc patch applied for this to work!
https://git.sr.ht/~raphi/dwl/blob/master/patches/wayland-ipc.patch
diff --git a/protocols/meson.build b/protocols/meson.build
index 7bd222b..5752608 100644
--- a/protocols/meson.build
@ -194,7 +193,7 @@ index 0000000..390f5a1
+ </interface>
+</protocol>
diff --git a/src/common.hpp b/src/common.hpp
index 2170103..88db973 100644
index 0d900f0..5f9c753 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -10,6 +10,7 @@
@ -218,9 +217,9 @@ index 2170103..88db973 100644
+void tag(Monitor& m, const Arg& arg);
+void toggletag(Monitor& m, const Arg& arg);
void spawn(Monitor&, const Arg& arg);
void setCloexec(int fd);
[[noreturn]] void die(const char* why);
@@ -57,6 +65,7 @@ WL_DELETER(wl_output, wl_output_release);
@@ -59,6 +67,7 @@ WL_DELETER(wl_output, wl_output_release);
WL_DELETER(wl_pointer, wl_pointer_release);
WL_DELETER(wl_seat, wl_seat_release);
WL_DELETER(wl_surface, wl_surface_destroy);
@ -251,7 +250,7 @@ index 40a8c95..a9560cb 100644
{ ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} },
};
diff --git a/src/main.cpp b/src/main.cpp
index 6678ee8..22b1fad 100644
index 0aeadb2..6c5c992 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,7 +3,6 @@
@ -262,7 +261,7 @@ index 6678ee8..22b1fad 100644
#include <list>
#include <optional>
#include <utility>
@@ -22,8 +21,8 @@
@@ -21,8 +20,8 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
@ -272,7 +271,7 @@ index 6678ee8..22b1fad 100644
#include "bar.hpp"
#include "line_buffer.hpp"
@@ -35,6 +34,7 @@ struct Monitor {
@@ -34,6 +33,7 @@ struct Monitor {
bool desiredVisibility {true};
bool hasData;
uint32_t tags;
@ -280,7 +279,7 @@ index 6678ee8..22b1fad 100644
};
struct SeatPointer {
@@ -55,8 +55,6 @@ static void updatemon(Monitor &mon);
@@ -54,8 +54,6 @@ static void updatemon(Monitor &mon);
static void onReady();
static void setupStatusFifo();
static void onStatus();
@ -289,7 +288,7 @@ index 6678ee8..22b1fad 100644
static void updateVisibility(const std::string& name, bool(*updater)(bool));
static void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version);
static void onGlobalRemove(void*, wl_registry* registry, uint32_t name);
@@ -69,6 +67,9 @@ wl_display* display;
@@ -67,6 +65,9 @@ wl_display* display;
wl_compositor* compositor;
wl_shm* shm;
zwlr_layer_shell_v1* wlrLayerShell;
@ -299,7 +298,7 @@ index 6678ee8..22b1fad 100644
static xdg_wm_base* xdgWmBase;
static zxdg_output_manager_v1* xdgOutputManager;
static wl_surface* cursorSurface;
@@ -86,6 +87,26 @@ static int statusFifoFd {-1};
@@ -85,6 +86,26 @@ static int statusFifoFd {-1};
static int statusFifoWriter {-1};
static bool quitting {false};
@ -326,7 +325,7 @@ index 6678ee8..22b1fad 100644
void spawn(Monitor&, const Arg& arg)
{
if (fork() == 0) {
@@ -189,11 +210,62 @@ static const struct wl_seat_listener seatListener = {
@@ -188,11 +209,62 @@ static const struct wl_seat_listener seatListener = {
.name = [](void*, wl_seat*, const char *name) { }
};
@ -389,7 +388,7 @@ index 6678ee8..22b1fad 100644
}
void updatemon(Monitor& mon)
@@ -217,6 +289,7 @@ void onReady()
@@ -216,6 +288,7 @@ void onReady()
requireGlobal(shm, "wl_shm");
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
requireGlobal(xdgOutputManager, "zxdg_output_manager_v1");
@ -397,7 +396,7 @@ index 6678ee8..22b1fad 100644
setupStatusFifo();
wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc.
@@ -224,7 +297,6 @@ void onReady()
@@ -223,7 +296,6 @@ void onReady()
for (auto output : uninitializedOutputs) {
setupMonitor(output.first, output.second);
}
@ -405,7 +404,7 @@ index 6678ee8..22b1fad 100644
}
void setupStatusFifo()
@@ -259,66 +331,6 @@ void setupStatusFifo()
@@ -256,66 +328,6 @@ void setupStatusFifo()
}
}
@ -472,7 +471,7 @@ index 6678ee8..22b1fad 100644
const std::string prefixStatus = "status ";
const std::string prefixShow = "show ";
const std::string prefixHide = "hide ";
@@ -391,6 +403,10 @@ void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interf
@@ -388,6 +400,10 @@ void onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interf
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
return;
}
@ -483,26 +482,23 @@ index 6678ee8..22b1fad 100644
if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) {
auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}});
wl_seat_add_listener(wlSeat, &seatListener, &seat);
@@ -491,13 +507,6 @@ int main(int argc, char* argv[])
diesys("epoll_ctl add wayland_display");
@@ -489,10 +505,6 @@ int main(int argc, char* argv[])
.fd = displayFd,
.events = POLLIN,
});
- pollfds.push_back({
- .fd = STDIN_FILENO,
- .events = POLLIN,
- });
if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) {
diesys("fcntl F_SETFL");
}
- epollEv.events = EPOLLIN;
- epollEv.data.fd = STDIN_FILENO;
- if (epoll_ctl(epoll, EPOLL_CTL_ADD, STDIN_FILENO, &epollEv) < 0) {
- diesys("epoll_ctl add stdin");
- }
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
-
while (!quitting) {
waylandFlush();
auto res = epoll_wait(epoll, epollEvents.data(), epollEvents.size(), -1);
@@ -521,8 +530,6 @@ int main(int argc, char* argv[])
}
@@ -517,8 +529,6 @@ int main(int argc, char* argv[])
ev.events = POLLIN;
waylandFlush();
}
- } else if (ev.data.fd == STDIN_FILENO) {
- } else if (ev.fd == STDIN_FILENO && (ev.revents & POLLIN)) {
- onStdin();
} else if (ev.data.fd == statusFifoFd) {
} else if (ev.fd == statusFifoFd && (ev.revents & POLLIN)) {
onStatus();
} else if (ev.data.fd == sfd) {
} else if (ev.fd == signalSelfPipe[0] && (ev.revents & POLLIN)) {

View File

@ -9,8 +9,8 @@
#include <utility>
#include <vector>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -79,7 +79,7 @@ static std::list<Seat> seats;
static Monitor* selmon;
static std::string lastStatus;
static std::string statusFifoName;
static int epoll {-1};
static std::vector<pollfd> pollfds;
static std::array<int, 2> signalSelfPipe;
static int displayFd {-1};
static int statusFifoFd {-1};
@ -246,12 +246,10 @@ void setupStatusFifo()
}
statusFifoWriter = fd;
epoll_event ev = {0};
ev.events = EPOLLIN;
ev.data.fd = statusFifoFd;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, statusFifoFd, &ev) < 0) {
diesys("epoll_ctl add status fifo");
}
pollfds.push_back({
.fd = statusFifoFd,
.events = POLLIN,
});
return;
} else if (errno != EEXIST) {
diesys("mkfifo");
@ -472,18 +470,10 @@ int main(int argc, char* argv[])
diesys("sigaction");
}
epoll_event epollEv = {0};
std::array<epoll_event, 5> epollEvents;
epoll = epoll_create1(EPOLL_CLOEXEC);
if (epoll < 0) {
diesys("epoll_create1");
}
epollEv.events = EPOLLIN;
epollEv.data.fd = signalSelfPipe[0];
if (epoll_ctl(epoll, EPOLL_CTL_ADD, signalSelfPipe[0], &epollEv) < 0) {
diesys("epoll_ctl add signal pipe");
}
pollfds.push_back({
.fd = signalSelfPipe[0],
.events = POLLIN,
});
display = wl_display_connect(nullptr);
if (!display) {
@ -496,47 +486,43 @@ int main(int argc, char* argv[])
wl_display_roundtrip(display);
onReady();
epollEv.events = EPOLLIN;
epollEv.data.fd = displayFd;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, displayFd, &epollEv) < 0) {
diesys("epoll_ctl add wayland_display");
pollfds.push_back({
.fd = displayFd,
.events = POLLIN,
});
pollfds.push_back({
.fd = STDIN_FILENO,
.events = POLLIN,
});
if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) {
diesys("fcntl F_SETFL");
}
epollEv.events = EPOLLIN;
epollEv.data.fd = STDIN_FILENO;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, STDIN_FILENO, &epollEv) < 0) {
diesys("epoll_ctl add stdin");
}
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
while (!quitting) {
waylandFlush();
auto res = epoll_wait(epoll, epollEvents.data(), epollEvents.size(), -1);
if (res < 0) {
if (poll(pollfds.data(), pollfds.size(), -1) < 0) {
if (errno != EINTR) {
diesys("epoll_wait");
diesys("poll");
}
} else {
for (auto i=0; i<res; i++) {
auto &ev = epollEvents[i];
if (ev.data.fd == displayFd) {
if (ev.events & EPOLLIN) {
for (auto& ev : pollfds) {
if (ev.revents & POLLNVAL) {
die("poll revents contains POLLNVAL");
} else if (ev.fd == displayFd) {
if (ev.revents & POLLIN) {
if (wl_display_dispatch(display) < 0) {
die("wl_display_dispatch");
}
} if (ev.events & EPOLLOUT) {
epollEv.events = EPOLLIN;
epollEv.data.fd = displayFd;
if (epoll_ctl(epoll, EPOLL_CTL_MOD, displayFd, &epollEv) < 0) {
diesys("epoll_ctl");
}
}
if (ev.revents & POLLOUT) {
ev.events = POLLIN;
waylandFlush();
}
} else if (ev.data.fd == STDIN_FILENO) {
} else if (ev.fd == STDIN_FILENO && (ev.revents & POLLIN)) {
onStdin();
} else if (ev.data.fd == statusFifoFd) {
} else if (ev.fd == statusFifoFd && (ev.revents & POLLIN)) {
onStatus();
} else if (ev.data.fd == signalSelfPipe[0]) {
} else if (ev.fd == signalSelfPipe[0] && (ev.revents & POLLIN)) {
quitting = true;
}
}
@ -557,11 +543,10 @@ void waylandFlush()
{
wl_display_dispatch_pending(display);
if (wl_display_flush(display) < 0 && errno == EAGAIN) {
epoll_event ev = {0};
ev.events = EPOLLIN | EPOLLOUT;
ev.data.fd = displayFd;
if (epoll_ctl(epoll, EPOLL_CTL_MOD, displayFd, &ev) < 0) {
diesys("epoll_ctl");
for (auto& ev : pollfds) {
if (ev.fd == displayFd) {
ev.events |= POLLOUT;
}
}
}
}