commit 17cbb4975c2b8e2fb80abc70425609e8836d1327
parent 72137eb4480752deff019f49ad1d1588a29a4557
Author: Andrea Feletto <andrea@andreafeletto.com>
Date: Sat, 29 Jan 2022 14:19:54 +0100
handle signals
Diffstat:
1 file changed, 29 insertions(+), 11 deletions(-)
diff --git a/src/event.zig b/src/event.zig
@@ -10,10 +10,18 @@ const State = @import("main.zig").State;
pub const Loop = struct {
state: *State,
- fds: [3]os.pollfd,
+ fds: [4]os.pollfd,
pub fn init(state: *State) !Loop {
- // Timer
+ // signals
+ var mask = mem.zeroes(os.linux.sigset_t);
+ os.linux.sigaddset(&mask, os.linux.SIG.INT);
+ os.linux.sigaddset(&mask, os.linux.SIG.TERM);
+ os.linux.sigaddset(&mask, os.linux.SIG.QUIT);
+ _ = os.linux.sigprocmask(os.linux.SIG.BLOCK, &mask, null);
+ const sfd = os.linux.signalfd(-1, &mask, os.linux.SFD.NONBLOCK);
+
+ // timer
const tfd = os.linux.timerfd_create(
os.CLOCK.MONOTONIC,
os.linux.TFD.CLOEXEC,
@@ -31,6 +39,11 @@ pub const Loop = struct {
.state = state,
.fds = .{
.{
+ .fd = @intCast(os.fd_t, sfd),
+ .events = os.POLL.IN,
+ .revents = 0,
+ },
+ .{
.fd = state.wayland.display.getFd(),
.events = os.POLL.IN,
.revents = 0,
@@ -51,9 +64,9 @@ pub const Loop = struct {
pub fn run(self: *Loop) !void {
const display = self.state.wayland.display;
- const tfd = self.fds[1].fd;
+ const tfd = self.fds[2].fd;
- while (true) loop: {
+ while (true) {
while (true) {
const ret = try display.dispatchPending();
_ = try display.flush();
@@ -63,23 +76,28 @@ pub const Loop = struct {
for (self.fds) |fd| {
if (fd.revents & os.POLL.HUP != 0) {
- break :loop;
+ return;
}
if (fd.revents & os.POLL.ERR != 0) {
- break :loop;
+ return;
}
}
- // wayland
+ // signals
if (self.fds[0].revents & os.POLL.IN != 0) {
+ return;
+ }
+
+ // wayland
+ if (self.fds[1].revents & os.POLL.IN != 0) {
_ = try display.dispatch();
}
- if (self.fds[0].revents & os.POLL.OUT != 0) {
+ if (self.fds[1].revents & os.POLL.OUT != 0) {
_ = try display.flush();
}
// timer
- if (self.fds[1].revents & os.POLL.IN != 0) {
+ if (self.fds[2].revents & os.POLL.IN != 0) {
var expirations = mem.zeroes([8]u8);
_ = try os.read(tfd, &expirations);
@@ -95,8 +113,8 @@ pub const Loop = struct {
}
// inotify
- if (self.fds[2].revents & os.POLL.IN != 0) {
- const ifd = self.fds[2].fd;
+ if (self.fds[3].revents & os.POLL.IN != 0) {
+ const ifd = self.fds[3].fd;
var event = mem.zeroes(os.linux.inotify_event);
_ = try os.read(ifd, mem.asBytes(&event));