From 2eee9aa445e3f9dc6a7ca115489f87b10f60b9ba Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Mon, 20 Sep 2021 23:43:10 +0200 Subject: seatd: Implement ping request to wake up later When device open or close messages are sent to seatd, libseat must read messages from the socket until it sees the associated response message. This means that it may drain enable/disable seat events from the socket, queueing them internally for deferred processing. As the socket is drained, the caller will not wake from a poll and have no reason to dispatch libseat. To ensure that these messages would not be left in the queue, 6fa82930d0c5660eea3102989c765dc864514e36 made it so that open/close calls would execute all queued events just before returning. Unfortunately, this had the side-effect of having events fire from the stack of libseat_open_device or libseat_close_device, which we now see cause problems in compositors. Specifically, an issue has been observed where libinput end up calling libseat_close_device, which in turn dispatch a disable seat event that calls libinput_suspend. libinput does not like this. Instead, remove the execution from libseat_open_device and libseat_close_device, and instead make a "ping" request to seatd if events have been queued. The response to this will wake us up and ensure that dispatch is called. --- libseat/backend/seatd.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'libseat/backend/seatd.c') diff --git a/libseat/backend/seatd.c b/libseat/backend/seatd.c index 85df9f5..26308d1 100644 --- a/libseat/backend/seatd.c +++ b/libseat/backend/seatd.c @@ -36,6 +36,7 @@ struct backend_seatd { const struct libseat_seat_listener *seat_listener; void *seat_listener_data; struct linked_list pending_events; + bool awaiting_pong; bool error; char seat_name[MAX_SEAT_LEN]; @@ -243,6 +244,12 @@ static int dispatch_pending(struct backend_seatd *backend, int *opcode) { while (connection_get(&backend->connection, &header, sizeof header) != -1) { packets++; switch (header.opcode) { + case SERVER_PONG: + // We care about whether or not the answer has been + // read from the connection, so handle it here instead + // of pushing it to the pending event list. + backend->awaiting_pong = false; + break; case SERVER_DISABLE_SEAT: case SERVER_ENABLE_SEAT: if (queue_event(backend, header.opcode) == -1) { @@ -450,6 +457,36 @@ static const char *seat_name(struct libseat *base) { return backend->seat_name; } +static int send_ping(struct backend_seatd *backend) { + struct proto_header header = { + .opcode = CLIENT_PING, + .size = 0, + }; + if (conn_put(backend, &header, sizeof header) == -1 || conn_flush(backend) == -1) { + return -1; + } + return 0; +} + +static void check_pending_events(struct backend_seatd *backend) { + if (linked_list_empty(&backend->pending_events)) { + return; + } + if (backend->awaiting_pong) { + return; + } + + // We have events pending execution, so a dispatch is required. + // However, we likely already drained our socket, so there will not be + // anything to read. Instead, send a ping request to seatd, so that the + // user will be woken up by its response. + if (send_ping(backend) == -1) { + log_errorf("Could not send ping request: %s", strerror(errno)); + return; + } + backend->awaiting_pong = true; +} + static int open_device(struct libseat *base, const char *path, int *fd) { struct backend_seatd *backend = backend_seatd_from_libseat_backend(base); if (backend->error) { @@ -481,11 +518,11 @@ static int open_device(struct libseat *base, const char *path, int *fd) { goto error; } - execute_events(backend); + check_pending_events(backend); return rmsg.device_id; error: - execute_events(backend); + check_pending_events(backend); return -1; } @@ -516,11 +553,11 @@ static int close_device(struct libseat *base, int device_id) { goto error; } - execute_events(backend); + check_pending_events(backend); return 0; error: - execute_events(backend); + check_pending_events(backend); return -1; } -- cgit v1.2.3