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! 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 https://git.sr.ht/~raphi/dwl/blob/master/patches/wayland-ipc.patch
diff --git a/protocols/meson.build b/protocols/meson.build diff --git a/protocols/meson.build b/protocols/meson.build
index 7bd222b..5752608 100644 index 7bd222b..5752608 100644
--- a/protocols/meson.build --- a/protocols/meson.build
@ -194,7 +193,7 @@ index 0000000..390f5a1
+ </interface> + </interface>
+</protocol> +</protocol>
diff --git a/src/common.hpp b/src/common.hpp diff --git a/src/common.hpp b/src/common.hpp
index 2170103..88db973 100644 index 0d900f0..5f9c753 100644
--- a/src/common.hpp --- a/src/common.hpp
+++ b/src/common.hpp +++ b/src/common.hpp
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
@ -218,9 +217,9 @@ index 2170103..88db973 100644
+void tag(Monitor& m, const Arg& arg); +void tag(Monitor& m, const Arg& arg);
+void toggletag(Monitor& m, const Arg& arg); +void toggletag(Monitor& m, const Arg& arg);
void spawn(Monitor&, const Arg& arg); void spawn(Monitor&, const Arg& arg);
void setCloexec(int fd);
[[noreturn]] void die(const char* why); [[noreturn]] void die(const char* why);
@@ -59,6 +67,7 @@ WL_DELETER(wl_output, wl_output_release);
@@ -57,6 +65,7 @@ WL_DELETER(wl_output, wl_output_release);
WL_DELETER(wl_pointer, wl_pointer_release); WL_DELETER(wl_pointer, wl_pointer_release);
WL_DELETER(wl_seat, wl_seat_release); WL_DELETER(wl_seat, wl_seat_release);
WL_DELETER(wl_surface, wl_surface_destroy); WL_DELETER(wl_surface, wl_surface_destroy);
@ -251,7 +250,7 @@ index 40a8c95..a9560cb 100644
{ ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} }, { ClkStatusText, BTN_RIGHT, spawn, {.v = termcmd} },
}; };
diff --git a/src/main.cpp b/src/main.cpp diff --git a/src/main.cpp b/src/main.cpp
index 6678ee8..22b1fad 100644 index 0aeadb2..6c5c992 100644
--- a/src/main.cpp --- a/src/main.cpp
+++ b/src/main.cpp +++ b/src/main.cpp
@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
@ -262,7 +261,7 @@ index 6678ee8..22b1fad 100644
#include <list> #include <list>
#include <optional> #include <optional>
#include <utility> #include <utility>
@@ -22,8 +21,8 @@ @@ -21,8 +20,8 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h" #include "xdg-output-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h" #include "xdg-shell-client-protocol.h"
@ -272,7 +271,7 @@ index 6678ee8..22b1fad 100644
#include "bar.hpp" #include "bar.hpp"
#include "line_buffer.hpp" #include "line_buffer.hpp"
@@ -35,6 +34,7 @@ struct Monitor { @@ -34,6 +33,7 @@ struct Monitor {
bool desiredVisibility {true}; bool desiredVisibility {true};
bool hasData; bool hasData;
uint32_t tags; uint32_t tags;
@ -280,7 +279,7 @@ index 6678ee8..22b1fad 100644
}; };
struct SeatPointer { 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 onReady();
static void setupStatusFifo(); static void setupStatusFifo();
static void onStatus(); static void onStatus();
@ -289,7 +288,7 @@ index 6678ee8..22b1fad 100644
static void updateVisibility(const std::string& name, bool(*updater)(bool)); 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 onGlobalAdd(void*, wl_registry* registry, uint32_t name, const char* interface, uint32_t version);
static void onGlobalRemove(void*, wl_registry* registry, uint32_t name); 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_compositor* compositor;
wl_shm* shm; wl_shm* shm;
zwlr_layer_shell_v1* wlrLayerShell; zwlr_layer_shell_v1* wlrLayerShell;
@ -299,7 +298,7 @@ index 6678ee8..22b1fad 100644
static xdg_wm_base* xdgWmBase; static xdg_wm_base* xdgWmBase;
static zxdg_output_manager_v1* xdgOutputManager; static zxdg_output_manager_v1* xdgOutputManager;
static wl_surface* cursorSurface; 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 int statusFifoWriter {-1};
static bool quitting {false}; static bool quitting {false};
@ -326,7 +325,7 @@ index 6678ee8..22b1fad 100644
void spawn(Monitor&, const Arg& arg) void spawn(Monitor&, const Arg& arg)
{ {
if (fork() == 0) { 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) { } .name = [](void*, wl_seat*, const char *name) { }
}; };
@ -389,7 +388,7 @@ index 6678ee8..22b1fad 100644
} }
void updatemon(Monitor& mon) void updatemon(Monitor& mon)
@@ -217,6 +289,7 @@ void onReady() @@ -216,6 +288,7 @@ void onReady()
requireGlobal(shm, "wl_shm"); requireGlobal(shm, "wl_shm");
requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1"); requireGlobal(wlrLayerShell, "zwlr_layer_shell_v1");
requireGlobal(xdgOutputManager, "zxdg_output_manager_v1"); requireGlobal(xdgOutputManager, "zxdg_output_manager_v1");
@ -397,7 +396,7 @@ index 6678ee8..22b1fad 100644
setupStatusFifo(); setupStatusFifo();
wl_display_roundtrip(display); // roundtrip so we receive all dwl tags etc. 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) { for (auto output : uninitializedOutputs) {
setupMonitor(output.first, output.second); setupMonitor(output.first, output.second);
} }
@ -405,7 +404,7 @@ index 6678ee8..22b1fad 100644
} }
void setupStatusFifo() 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 prefixStatus = "status ";
const std::string prefixShow = "show "; const std::string prefixShow = "show ";
const std::string prefixHide = "hide "; 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); xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
return; return;
} }
@ -483,26 +482,23 @@ index 6678ee8..22b1fad 100644
if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) { if (wl_seat *wlSeat; reg.handle(wlSeat, wl_seat_interface, 7)) {
auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}}); auto& seat = seats.emplace_back(Seat {name, wl_unique_ptr<wl_seat> {wlSeat}});
wl_seat_add_listener(wlSeat, &seatListener, &seat); wl_seat_add_listener(wlSeat, &seatListener, &seat);
@@ -491,13 +507,6 @@ int main(int argc, char* argv[]) @@ -489,10 +505,6 @@ int main(int argc, char* argv[])
diesys("epoll_ctl add wayland_display"); .fd = displayFd,
} .events = POLLIN,
});
- epollEv.events = EPOLLIN; - pollfds.push_back({
- epollEv.data.fd = STDIN_FILENO; - .fd = STDIN_FILENO,
- if (epoll_ctl(epoll, EPOLL_CTL_ADD, STDIN_FILENO, &epollEv) < 0) { - .events = POLLIN,
- diesys("epoll_ctl add stdin"); - });
- } if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) {
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); diesys("fcntl F_SETFL");
-
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(); waylandFlush();
} }
- } else if (ev.data.fd == STDIN_FILENO) { - } else if (ev.fd == STDIN_FILENO && (ev.revents & POLLIN)) {
- onStdin(); - onStdin();
} else if (ev.data.fd == statusFifoFd) { } else if (ev.fd == statusFifoFd && (ev.revents & POLLIN)) {
onStatus(); 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 <utility>
#include <vector> #include <vector>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h>
#include <signal.h> #include <signal.h>
#include <sys/epoll.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -79,7 +79,7 @@ static std::list<Seat> seats;
static Monitor* selmon; static Monitor* selmon;
static std::string lastStatus; static std::string lastStatus;
static std::string statusFifoName; static std::string statusFifoName;
static int epoll {-1}; static std::vector<pollfd> pollfds;
static std::array<int, 2> signalSelfPipe; static std::array<int, 2> signalSelfPipe;
static int displayFd {-1}; static int displayFd {-1};
static int statusFifoFd {-1}; static int statusFifoFd {-1};
@ -246,12 +246,10 @@ void setupStatusFifo()
} }
statusFifoWriter = fd; statusFifoWriter = fd;
epoll_event ev = {0}; pollfds.push_back({
ev.events = EPOLLIN; .fd = statusFifoFd,
ev.data.fd = statusFifoFd; .events = POLLIN,
if (epoll_ctl(epoll, EPOLL_CTL_ADD, statusFifoFd, &ev) < 0) { });
diesys("epoll_ctl add status fifo");
}
return; return;
} else if (errno != EEXIST) { } else if (errno != EEXIST) {
diesys("mkfifo"); diesys("mkfifo");
@ -472,18 +470,10 @@ int main(int argc, char* argv[])
diesys("sigaction"); diesys("sigaction");
} }
epoll_event epollEv = {0}; pollfds.push_back({
std::array<epoll_event, 5> epollEvents; .fd = signalSelfPipe[0],
epoll = epoll_create1(EPOLL_CLOEXEC); .events = POLLIN,
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");
}
display = wl_display_connect(nullptr); display = wl_display_connect(nullptr);
if (!display) { if (!display) {
@ -496,47 +486,43 @@ int main(int argc, char* argv[])
wl_display_roundtrip(display); wl_display_roundtrip(display);
onReady(); onReady();
epollEv.events = EPOLLIN; pollfds.push_back({
epollEv.data.fd = displayFd; .fd = displayFd,
if (epoll_ctl(epoll, EPOLL_CTL_ADD, displayFd, &epollEv) < 0) { .events = POLLIN,
diesys("epoll_ctl add wayland_display"); });
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) { while (!quitting) {
waylandFlush(); waylandFlush();
auto res = epoll_wait(epoll, epollEvents.data(), epollEvents.size(), -1); if (poll(pollfds.data(), pollfds.size(), -1) < 0) {
if (res < 0) {
if (errno != EINTR) { if (errno != EINTR) {
diesys("epoll_wait"); diesys("poll");
} }
} else { } else {
for (auto i=0; i<res; i++) { for (auto& ev : pollfds) {
auto &ev = epollEvents[i]; if (ev.revents & POLLNVAL) {
if (ev.data.fd == displayFd) { die("poll revents contains POLLNVAL");
if (ev.events & EPOLLIN) { } else if (ev.fd == displayFd) {
if (ev.revents & POLLIN) {
if (wl_display_dispatch(display) < 0) { if (wl_display_dispatch(display) < 0) {
die("wl_display_dispatch"); 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(); waylandFlush();
} }
} else if (ev.data.fd == STDIN_FILENO) { } else if (ev.fd == STDIN_FILENO && (ev.revents & POLLIN)) {
onStdin(); onStdin();
} else if (ev.data.fd == statusFifoFd) { } else if (ev.fd == statusFifoFd && (ev.revents & POLLIN)) {
onStatus(); onStatus();
} else if (ev.data.fd == signalSelfPipe[0]) { } else if (ev.fd == signalSelfPipe[0] && (ev.revents & POLLIN)) {
quitting = true; quitting = true;
} }
} }
@ -557,11 +543,10 @@ void waylandFlush()
{ {
wl_display_dispatch_pending(display); wl_display_dispatch_pending(display);
if (wl_display_flush(display) < 0 && errno == EAGAIN) { if (wl_display_flush(display) < 0 && errno == EAGAIN) {
epoll_event ev = {0}; for (auto& ev : pollfds) {
ev.events = EPOLLIN | EPOLLOUT; if (ev.fd == displayFd) {
ev.data.fd = displayFd; ev.events |= POLLOUT;
if (epoll_ctl(epoll, EPOLL_CTL_MOD, displayFd, &ev) < 0) { }
diesys("epoll_ctl");
} }
} }
} }