compositor: Use eglBindWaylandDisplayWL
diff --git a/compositor/compositor-drm.c b/compositor/compositor-drm.c
index 50f353b..83fc744 100644
--- a/compositor/compositor-drm.c
+++ b/compositor/compositor-drm.c
@@ -116,8 +116,7 @@
 		return -1;
 	}
 
-	wlsc_drm_init(&ec->base, fd, filename);
-
+	ec->base.drm.fd = fd;
 	ec->base.display = eglGetDRMDisplayMESA(ec->base.drm.fd);
 	if (ec->base.display == NULL) {
 		fprintf(stderr, "failed to create display\n");
@@ -294,14 +293,6 @@
 	return 0;
 }
 
-static int
-drm_authenticate(struct wlsc_compositor *c, uint32_t id)
-{
-	struct drm_compositor *ec = (struct drm_compositor *) c;
-
-	return drmAuthMagic(ec->base.drm.fd, id);
-}
-
 static void
 drm_destroy(struct wlsc_compositor *ec)
 {
@@ -357,7 +348,6 @@
 	}
 
 	ec->base.destroy = drm_destroy;
-	ec->base.authenticate = drm_authenticate;
 	ec->base.present = drm_compositor_present;
 	ec->base.create_buffer = wlsc_drm_buffer_create;
 	ec->base.focus = 1;
diff --git a/compositor/compositor-x11.c b/compositor/compositor-x11.c
index ae68a1c..f4cb129 100644
--- a/compositor/compositor-x11.c
+++ b/compositor/compositor-x11.c
@@ -106,7 +106,6 @@
 	return 0;
 }
 
-
 static int
 dri2_connect(struct x11_compositor *c)
 {
@@ -118,7 +117,6 @@
 	xcb_dri2_connect_cookie_t connect_cookie;
 	xcb_generic_error_t *error;
 	char path[256];
-	int fd;
 
 	xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
 	xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
@@ -185,37 +183,6 @@
 		 xcb_dri2_connect_device_name (connect));
 #endif
 	free(connect);
-
-	fd = open(path, O_RDWR);
-	if (fd < 0) {
-		fprintf(stderr,
-			"DRI2: could not open %s (%s)\n", path, strerror(errno));
-		return -1;
-	}
-
-	return wlsc_drm_init(&c->base, fd, path);
-}
-
-static int
-dri2_authenticate(struct x11_compositor *c, uint32_t magic)
-{
-	xcb_dri2_authenticate_reply_t *authenticate;
-	xcb_dri2_authenticate_cookie_t authenticate_cookie;
-
-	authenticate_cookie =
-		xcb_dri2_authenticate_unchecked(c->conn,
-						c->screen->root, magic);
-	authenticate =
-		xcb_dri2_authenticate_reply(c->conn,
-					    authenticate_cookie, NULL);
-	if (authenticate == NULL || !authenticate->authenticated) {
-		fprintf(stderr, "DRI2: failed to authenticate\n");
-		free(authenticate);
-		return -1;
-	}
-
-	free(authenticate);
-
 	return 0;
 }
 
@@ -725,12 +692,6 @@
 	xcb_free_pixmap(c->conn, pixmap);
 }
 
-static int
-x11_authenticate(struct wlsc_compositor *c, uint32_t id)
-{
-	return dri2_authenticate((struct x11_compositor *) c, id);
-}
-
 static void
 x11_destroy(struct wlsc_compositor *ec)
 {
@@ -767,7 +728,6 @@
 		return NULL;
 
 	c->base.destroy = x11_destroy;
-	c->base.authenticate = x11_authenticate;
 	c->base.present = x11_compositor_present;
 	c->base.create_buffer = wlsc_drm_buffer_create;
 
diff --git a/compositor/compositor.c b/compositor/compositor.c
index afff961..2fcbb70 100644
--- a/compositor/compositor.c
+++ b/compositor/compositor.c
@@ -282,6 +282,27 @@
 	return (uint32_t *) argb_pixels;
 }
 
+static void
+wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
+{
+	struct wlsc_surface *es = (struct wlsc_surface *) surface;
+	struct wlsc_compositor *ec = es->compositor;
+	EGLImageKHR *image;
+
+	if (buffer->attach) {
+		buffer->attach(buffer, surface);
+	} else {
+		image = eglCreateImageKHR(ec->display, ec->context,
+					  EGL_WAYLAND_BUFFER_WL,
+					  buffer, NULL);
+
+		glBindTexture(GL_TEXTURE_2D, es->texture);
+		glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
+		es->visual = buffer->visual;
+		eglDestroyImageKHR(ec->display, image);
+	}
+}
+
 static struct wl_buffer *
 create_buffer_from_png(struct wlsc_compositor *ec,
 		       const char *filename, int width, int height)
@@ -354,7 +375,8 @@
 		free(background);
 		return NULL;
 	}
-	buffer->attach(buffer, &background->surface);
+
+	wlsc_buffer_attach(buffer, &background->surface);
 
 	return background;
 }
@@ -569,7 +591,8 @@
 	 * damaged by the client. */
 	wlsc_surface_damage(es);
 
-	buffer->attach(buffer, surface);
+	wlsc_buffer_attach(buffer, surface);
+
 	es->buffer = buffer;
 	es->x += x;
 	es->y += y;
@@ -694,7 +717,7 @@
 {
 	wlsc_surface_damage(device->sprite);
 
-	buffer->attach(buffer, &device->sprite->surface);
+	wlsc_buffer_attach(buffer, &device->sprite->surface);
 	device->hotspot_x = x;
 	device->hotspot_y = y;
 
@@ -1306,6 +1329,7 @@
 	wl_compositor_init(&ec->compositor, &compositor_interface, display);
 
 	wlsc_shm_init(ec);
+	eglBindWaylandDisplayWL(ec->display, ec->wl_display);
 
 	wlsc_shell_init(ec);
 
@@ -1403,6 +1427,7 @@
 
 	wl_display_run(display);
 
+	eglUnbindWaylandDisplayWL(ec->display, display);
 	wl_display_destroy(display);
 
 	ec->destroy(ec);
diff --git a/compositor/drm.c b/compositor/drm.c
index 9e25d33..05bbc4a 100644
--- a/compositor/drm.c
+++ b/compositor/drm.c
@@ -28,45 +28,6 @@
 };
 
 static void
-drm_authenticate(struct wl_client *client,
-		 struct wl_drm *drm_base, uint32_t id)
-{
-	struct wlsc_drm *drm = (struct wlsc_drm *) drm_base;
-	struct wlsc_compositor *compositor =
-		container_of(drm, struct wlsc_compositor, drm);
-
-	if (compositor->authenticate(compositor, id) < 0)
-		wl_client_post_event(client,
-				     (struct wl_object *) compositor->wl_display,
-				     WL_DISPLAY_INVALID_OBJECT, 0);
-	else
-		wl_client_post_event(client, &drm->object,
-				     WL_DRM_AUTHENTICATED);
-}
-
-static void
-destroy_buffer(struct wl_resource *resource, struct wl_client *client)
-{
-	struct wlsc_drm_buffer *buffer =
-		container_of(resource, struct wlsc_drm_buffer, buffer.resource);
-	struct wlsc_compositor *compositor =
-		(struct wlsc_compositor *) buffer->buffer.compositor;
-
-	eglDestroyImageKHR(compositor->display, buffer->image);
-	free(buffer);
-}
-
-static void
-buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
-{
-	wl_resource_destroy(&buffer->resource, client);
-}
-
-const static struct wl_buffer_interface buffer_interface = {
-	buffer_destroy
-};
-
-static void
 drm_buffer_attach(struct wl_buffer *buffer_base, struct wl_surface *surface)
 {
 	struct wlsc_surface *es = (struct wlsc_surface *) surface;
@@ -108,100 +69,6 @@
 	return buffer;
 }
 
-static void
-drm_create_buffer(struct wl_client *client, struct wl_drm *drm_base,
-		  uint32_t id, uint32_t name, int32_t width, int32_t height,
-		  uint32_t stride, struct wl_visual *visual)
-{
-	struct wlsc_drm *drm = (struct wlsc_drm *) drm_base;
-	struct wlsc_compositor *compositor =
-		container_of(drm, struct wlsc_compositor, drm);
-	struct wlsc_drm_buffer *buffer;
-	EGLImageKHR image;
-	EGLint attribs[] = {
-		EGL_WIDTH,		0,
-		EGL_HEIGHT,		0,
-		EGL_DRM_BUFFER_STRIDE_MESA,	0,
-		EGL_DRM_BUFFER_FORMAT_MESA,	EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
-		EGL_NONE
-	};
-
-	if (visual->object.interface != &wl_visual_interface) {
-		/* FIXME: Define a real exception event instead of
-		 * abusing this one */
-		wl_client_post_event(client,
-				     (struct wl_object *) compositor->wl_display,
-				     WL_DISPLAY_INVALID_OBJECT, 0);
-		fprintf(stderr, "invalid visual in create_buffer\n");
-		return;
-	}
-
-	attribs[1] = width;
-	attribs[3] = height;
-	attribs[5] = stride / 4;
-	image = eglCreateImageKHR(compositor->display,
-				  EGL_NO_CONTEXT,
-				  EGL_DRM_BUFFER_MESA,
-				  (EGLClientBuffer) name, attribs);
-	if (image == NULL) {
-		/* FIXME: Define a real exception event instead of
-		 * abusing this one */
-		wl_client_post_event(client,
-				     (struct wl_object *) compositor->wl_display,
-				     WL_DISPLAY_INVALID_OBJECT, 0);
-		fprintf(stderr, "failed to create image for name %d\n", name);
-		return;
-	}
-
-	buffer = wlsc_drm_buffer_create_for_image(compositor, image,
-						  width, height, visual);
-	if (buffer == NULL) {
-		eglDestroyImageKHR(compositor->display, image);
-		wl_client_post_no_memory(client);
-		return;
-	}
-
-	buffer->buffer.resource.object.id = id;
-	buffer->buffer.resource.object.interface = &wl_buffer_interface;
-	buffer->buffer.resource.object.implementation = (void (**)(void))
-		&buffer_interface;
-
-	buffer->buffer.resource.destroy = destroy_buffer;
-
-	wl_client_add_resource(client, &buffer->buffer.resource);
-}
-
-const static struct wl_drm_interface drm_interface = {
-	drm_authenticate,
-	drm_create_buffer
-};
-
-static void
-post_drm_device(struct wl_client *client, struct wl_object *global)
-{
-	struct wlsc_drm *drm = container_of(global, struct wlsc_drm, object);
-
-	wl_client_post_event(client, global, WL_DRM_DEVICE, drm->filename);
-}
-
-int
-wlsc_drm_init(struct wlsc_compositor *ec, int fd, const char *filename)
-{
-	struct wlsc_drm *drm = &ec->drm;
-
-	drm->fd = fd;
-	drm->filename = strdup(filename);
-	if (drm->filename == NULL)
-		return -1;
-
-	drm->object.interface = &wl_drm_interface;
-	drm->object.implementation = (void (**)(void)) &drm_interface;
-	wl_display_add_object(ec->wl_display, &drm->object);
-	wl_display_add_global(ec->wl_display, &drm->object, post_drm_device);
-
-	return 0;
-}
-
 struct wl_buffer *
 wlsc_drm_buffer_create(struct wlsc_compositor *ec, int width, int height,
 		       struct wl_visual *visual, const void *data)