[Mesa-dev] [PATCH] eglut/wayland: Port to 1.0 API.

Armin K krejzi at email.com
Mon Aug 26 09:28:01 PDT 2013


---
 src/egl/eglut/eglut_wayland.c | 129 +++++++++++++++++++++++++++++++-----------
 1 file changed, 97 insertions(+), 32 deletions(-)

diff --git a/src/egl/eglut/eglut_wayland.c b/src/egl/eglut/eglut_wayland.c
index 61207d2..809d8b4 100644
--- a/src/egl/eglut/eglut_wayland.c
+++ b/src/egl/eglut/eglut_wayland.c
@@ -7,42 +7,92 @@ struct display {
    struct wl_display *display;
    struct wl_compositor *compositor;
    struct wl_shell *shell;
-   uint32_t mask;
+   struct wl_registry *registry;
 };
 
 struct window {
    struct wl_surface *surface;
    struct wl_shell_surface *shell_surface;
    struct wl_callback *callback;
+   struct wl_egl_window *native;
+   int configured;
 };
 
 static struct display display = {0, };
 static struct window window = {0, };
 
 static void
-display_handle_global(struct wl_display *display, uint32_t id,
-		      const char *interface, uint32_t version, void *data)
+registry_handle_global(void *data, struct wl_registry *registry,
+                       uint32_t name, const char *interface, uint32_t version)
 {
-   struct display *d = data;
-
    if (strcmp(interface, "wl_compositor") == 0) {
-      d->compositor =
-         wl_display_bind(display, id, &wl_compositor_interface);
+      display.compositor =
+         wl_registry_bind(registry, name,
+                          &wl_compositor_interface, 1);
    } else if (strcmp(interface, "wl_shell") == 0) {
-      d->shell = wl_display_bind(display, id, &wl_shell_interface);
+      display.shell = wl_registry_bind(registry, name,
+                                  &wl_shell_interface, 1);
    }
 }
 
-static int
-event_mask_update(uint32_t mask, void *data)
+static void
+registry_handle_global_remove(void *data, struct wl_registry *registry,
+                              uint32_t name)
+{
+}
+
+static const struct wl_registry_listener registry_listener = {
+   registry_handle_global,
+   registry_handle_global_remove
+};
+
+static void
+handle_ping(void *data, struct wl_shell_surface *shell_surface,
+            uint32_t serial)
+{
+   wl_shell_surface_pong(shell_surface, serial);
+}
+
+static void
+handle_configure(void *data, struct wl_shell_surface *shell_surface,
+                 uint32_t edges, int32_t width, int32_t height)
+{
+   struct eglut_window *win = _eglut->current;
+
+   if (win->native.u.window)
+      wl_egl_window_resize(win->native.u.window, win->native.width,
+                           win->native.height, 0, 0);
+}
+
+static void
+handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
+{
+}
+
+static const struct wl_shell_surface_listener shell_surface_listener = {
+    handle_ping,
+    handle_configure,
+    handle_popup_done
+};
+
+static void
+draw(void *data, struct wl_callback *callback, uint32_t time);
+
+static void
+configure_callback(void *data, struct wl_callback *callback, uint32_t  time)
 {
-   struct display *d = data;
+   wl_callback_destroy(callback);
 
-   d->mask = mask;
+   window.configured = 1;
 
-   return 0;
+   if (window.callback == NULL)
+      draw(&window, NULL, time);
 }
 
+static struct wl_callback_listener configure_callback_listener = {
+   configure_callback,
+};
+
 void
 _eglutNativeInitDisplay(void)
 {
@@ -51,11 +101,11 @@ _eglutNativeInitDisplay(void)
    if (!_eglut->native_dpy)
       _eglutFatal("failed to initialize native display");
 
-   wl_display_add_global_listener(_eglut->native_dpy,
-         display_handle_global, &display);
+   display.registry = wl_display_get_registry(_eglut->native_dpy);
+   wl_registry_add_listener(display.registry,
+                            &registry_listener, &display);
 
-   wl_display_get_fd(_eglut->native_dpy, event_mask_update, &display);
-   wl_display_iterate(_eglut->native_dpy, WL_DISPLAY_READABLE);
+   wl_display_dispatch(_eglut->native_dpy);
 
    _eglut->surface_type = EGL_WINDOW_BIT;
 }
@@ -63,6 +113,14 @@ _eglutNativeInitDisplay(void)
 void
 _eglutNativeFiniDisplay(void)
 {
+   if (display.shell)
+      wl_shell_destroy(display.shell);
+
+   if (display.compositor)
+      wl_compositor_destroy(display.compositor);
+
+   wl_registry_destroy(display.registry);
+
    wl_display_flush(_eglut->native_dpy);
    wl_display_disconnect(_eglut->native_dpy);
 }
@@ -71,16 +129,21 @@ void
 _eglutNativeInitWindow(struct eglut_window *win, const char *title,
                        int x, int y, int w, int h)
 {
-   struct wl_egl_window *native;
+   struct wl_callback *callback;
 
    window.surface = wl_compositor_create_surface(display.compositor);
    window.shell_surface = wl_shell_get_shell_surface(display.shell,
-         window.surface);
-   native = wl_egl_window_create(window.surface, w, h);
+                                                     window.surface);
+   window.native = wl_egl_window_create(window.surface, w, h);
 
+   wl_shell_surface_set_title(window.shell_surface, title);
    wl_shell_surface_set_toplevel(window.shell_surface);
 
-   win->native.u.window = native;
+   callback = wl_display_sync(_eglut->native_dpy);
+   wl_callback_add_listener(callback, &configure_callback_listener,
+                            &window);
+
+   win->native.u.window = window.native;
    win->native.width = w;
    win->native.height = h;
 }
@@ -88,7 +151,7 @@ _eglutNativeInitWindow(struct eglut_window *win, const char *title,
 void
 _eglutNativeFiniWindow(struct eglut_window *win)
 {
-   wl_egl_window_destroy(win->native.u.window);
+   wl_egl_window_destroy(window.native);
 
    wl_shell_surface_destroy(window.shell_surface);
    wl_surface_destroy(window.surface);
@@ -97,28 +160,29 @@ _eglutNativeFiniWindow(struct eglut_window *win)
       wl_callback_destroy(window.callback);
 }
 
-static void
-draw(void *data, struct wl_callback *callback, uint32_t time);
-
 static const struct wl_callback_listener frame_listener = {
    draw
 };
 
 static void
 draw(void *data, struct wl_callback *callback, uint32_t time)
-{	
+{
    struct window *window = (struct window *)data;
    struct eglut_window *win = _eglut->current;
 
-   if (win->display_cb)
-      win->display_cb();
-   eglSwapBuffers(_eglut->dpy, win->surface);
-
    if (callback)
       wl_callback_destroy(callback);
 
+   if (!window->configured)
+      return;
+
+   if (win->display_cb)
+      win->display_cb();
+
    window->callback = wl_surface_frame(window->surface);
    wl_callback_add_listener(window->callback, &frame_listener, window);
+
+   eglSwapBuffers(_eglut->dpy, win->surface);
 }
 
 void
@@ -126,8 +190,9 @@ _eglutNativeEventLoop(void)
 {
    draw(&window, NULL, 0);
 
-   while (1) {
-      wl_display_iterate(display.display, display.mask);
+   while (1)
+   {
+      wl_display_dispatch(display.display);
 
       if (_eglut->idle_cb)
          _eglut->idle_cb();
-- 
1.8.4



More information about the mesa-dev mailing list