From af7a251806661bbe7ed7a7573ffd8e16ef4b28a2 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Wed, 24 Feb 2016 13:30:54 +0100 Subject: Poll before wl_display_dispatch --- swaybar/bar.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'swaybar/bar.c') diff --git a/swaybar/bar.c b/swaybar/bar.c index 6d2872c3..b6329123 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #include "ipc-client.h" #include "list.h" @@ -95,10 +95,22 @@ void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { } void bar_run(struct bar *bar) { - fd_set readfds; - int activity; + int pfds = bar->outputs->length + 2; + struct pollfd *pfd = malloc(pfds * sizeof(struct pollfd)); bool dirty = true; + pfd[0].fd = bar->ipc_event_socketfd; + pfd[0].events = POLLIN; + pfd[1].fd = bar->status_read_fd; + pfd[1].events = POLLIN; + + int i; + for (i = 0; i < bar->outputs->length; ++i) { + struct output *output = bar->outputs->items[i]; + pfd[i+2].fd = wl_display_get_fd(output->registry->display); + pfd[i+2].events = POLLIN; + } + while (1) { if (dirty) { int i; @@ -107,32 +119,36 @@ void bar_run(struct bar *bar) { if (window_prerender(output->window) && output->window->cairo) { render(output, bar->config, bar->status); window_render(output->window); - if (wl_display_dispatch(output->registry->display) == -1) { - break; - } + wl_display_flush(output->registry->display); } } } dirty = false; - FD_ZERO(&readfds); - FD_SET(bar->ipc_event_socketfd, &readfds); - FD_SET(bar->status_read_fd, &readfds); - activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); - if (activity < 0) { - sway_log(L_ERROR, "polling failed: %d", errno); - } + poll(pfd, pfds, -1); - if (FD_ISSET(bar->ipc_event_socketfd, &readfds)) { + if (pfd[0].revents & POLLIN) { sway_log(L_DEBUG, "Got IPC event."); dirty = handle_ipc_event(bar); } - if (bar->config->status_command && FD_ISSET(bar->status_read_fd, &readfds)) { + if (bar->config->status_command && pfd[1].revents & POLLIN) { sway_log(L_DEBUG, "Got update from status command."); dirty = handle_status_line(bar); } + + // dispatch wl_display events + for (i = 0; i < bar->outputs->length; ++i) { + struct output *output = bar->outputs->items[i]; + if (pfd[i+2].revents & POLLIN) { + if (wl_display_dispatch(output->registry->display) == -1) { + sway_log(L_ERROR, "failed to dispatch wl: %d", errno); + } + } else { + wl_display_dispatch_pending(output->registry->display); + } + } } } -- cgit v1.2.3-54-g00ecf