[PATCH] toytoolkit: refactor the main loop

Philipp Brüschweiler blei42 at gmail.com
Wed Jul 18 00:29:54 PDT 2012


* make the epoll fd available to users
* make it possible to iterate the main loop by hand

these two changes make it possible to integrate the display
main loop into main loops of other frameworks (such as glib)
---
 clients/window.c | 55 ++++++++++++++++++++++++++++++++++++++-----------------
 clients/window.h | 10 ++++++++++
 2 Dateien geändert, 48 Zeilen hinzugefügt(+), 17 Zeilen entfernt(-)

diff --git a/clients/window.c b/clients/window.c
index 9f41a9c..af3214a 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -3644,6 +3644,12 @@ display_get_serial(struct display *display)
 	return display->serial;
 }
 
+int
+display_get_epoll_fd(struct display *display)
+{
+	return display->epoll_fd;
+}
+
 EGLDisplay
 display_get_egl_display(struct display *d)
 {
@@ -3739,34 +3745,49 @@ display_watch_fd(struct display *display,
 }
 
 void
-display_run(struct display *display)
+display_iteration_deferred(struct display *display)
+{
+	struct task *task;
+
+	wl_display_flush(display->display);
+
+	while (!wl_list_empty(&display->deferred_list)) {
+		task = container_of(display->deferred_list.next,
+				    struct task, link);
+		wl_list_remove(&task->link);
+		task->run(task, 0);
+	}
+}
+
+void
+display_iteration_epoll(struct display *display,
+			int timeout)
 {
 	struct task *task;
 	struct epoll_event ep[16];
 	int i, count;
 
+	wl_display_flush(display->display);
+
+	count = epoll_wait(display->epoll_fd,
+			   ep, ARRAY_LENGTH(ep), timeout);
+	for (i = 0; i < count; i++) {
+		task = ep[i].data.ptr;
+		task->run(task, ep[i].events);
+	}
+}
+
+void
+display_run(struct display *display)
+{
 	display->running = 1;
 	while (1) {
-		wl_display_flush(display->display);
-
-		while (!wl_list_empty(&display->deferred_list)) {
-			task = container_of(display->deferred_list.next,
-					    struct task, link);
-			wl_list_remove(&task->link);
-			task->run(task, 0);
-		}
+		display_iteration_deferred(display);
 
 		if (!display->running)
 			break;
 
-		wl_display_flush(display->display);
-
-		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);
-		}
+		display_iteration_epoll(display, -1);
 	}
 }
 
diff --git a/clients/window.h b/clients/window.h
index 23ad37d..e74e275 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -73,6 +73,9 @@ display_get_output(struct display *display);
 uint32_t
 display_get_serial(struct display *display);
 
+int
+display_get_epoll_fd(struct display *display);
+
 typedef void (*display_output_handler_t)(struct output *output, void *data);
 
 /*
@@ -135,6 +138,13 @@ display_watch_fd(struct display *display,
 		 int fd, uint32_t events, struct task *task);
 
 void
+display_iteration_deferred(struct display *display);
+
+void
+display_iteration_epoll(struct display *display,
+			int timeout);
+
+void
 display_run(struct display *d);
 
 void
-- 
1.7.11.2



More information about the wayland-devel mailing list