[PATCH weston v1 10/17] window: add display_dispatch

Marek Chalupa mchqwerty at gmail.com
Fri Dec 5 05:36:43 PST 2014


Split display_run into two functions - display_run and display_dispatch.
display_dispatch is the part that actually does something and
display_run just calls it in a loop. Now we can dispatch toytoolkit
events in tests (display_run is blocking, so we couldn't use that)

Signed-off-by: Marek Chalupa <mchqwerty at gmail.com>
---
 clients/window.c | 76 +++++++++++++++++++++++++++++++++++---------------------
 clients/window.h |  3 +++
 2 files changed, 50 insertions(+), 29 deletions(-)

diff --git a/clients/window.c b/clients/window.c
index 7ac2b8f..afed778 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -5777,46 +5777,64 @@ display_unwatch_fd(struct display *display, int fd)
 	epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
 }
 
-void
-display_run(struct display *display)
+int
+display_dispatch(struct display *display, int timeout)
 {
 	struct task *task;
 	struct epoll_event ep[16];
 	int i, count, ret;
 
-	display->running = 1;
-	while (1) {
-		while (!wl_list_empty(&display->deferred_list)) {
-			task = container_of(display->deferred_list.prev,
-					    struct task, link);
-			wl_list_remove(&task->link);
-			task->run(task, 0);
-		}
 
-		wl_display_dispatch_pending(display->display);
+	while (!wl_list_empty(&display->deferred_list)) {
+		task = container_of(display->deferred_list.prev,
+				    struct task, link);
+		wl_list_remove(&task->link);
+		task->run(task, 0);
+	}
 
-		if (!display->running)
-			break;
+	ret = wl_display_dispatch_pending(display->display);
+	if (ret < 0)
+		return ret;
 
-		ret = wl_display_flush(display->display);
-		if (ret < 0 && errno == EAGAIN) {
-			ep[0].events =
-				EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
-			ep[0].data.ptr = &display->display_task;
+	ret = wl_display_flush(display->display);
+	if (ret < 0 && errno == EAGAIN) {
+		ep[0].events =
+			EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
+		ep[0].data.ptr = &display->display_task;
 
-			epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
-				  display->display_fd, &ep[0]);
-		} else if (ret < 0) {
-			break;
-		}
+		epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
+			  display->display_fd, &ep[0]);
+	} else if (ret < 0) {
+		return ret;
+	}
 
-		count = epoll_wait(display->epoll_fd,
-				   ep, ARRAY_LENGTH(ep), -1);
-		for (i = 0; i < count; i++) {
-			task = ep[i].data.ptr;
-			task->run(task, ep[i].events);
-		}
+	count = epoll_wait(display->epoll_fd,
+			   ep, ARRAY_LENGTH(ep), timeout);
+	if (count < 0)
+		return count;
+
+	for (i = 0; i < count; i++) {
+		task = ep[i].data.ptr;
+		task->run(task, ep[i].events);
 	}
+
+	return 1;
+}
+
+void
+display_run(struct display *display)
+{
+	int ret;
+
+	display->running = 1;
+	while(display->running) {
+		ret = display_dispatch(display, -1);
+		if (ret <= 0)
+			break;
+	}
+
+	if (ret < 0)
+		perror("Dispatching events");
 }
 
 void
diff --git a/clients/window.h b/clients/window.h
index a6675d0..a4875e9 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -176,6 +176,9 @@ display_watch_fd(struct display *display,
 void
 display_unwatch_fd(struct display *display, int fd);
 
+int
+display_dispatch(struct display *display, int timeout);
+
 void
 display_run(struct display *d);
 
-- 
2.1.0



More information about the wayland-devel mailing list