Consolidate more code in clients/window.c
diff --git a/clients/window.c b/clients/window.c
index a566c9f..6d423ab 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -56,6 +56,8 @@
 	EGLContext ctx;
 	cairo_device_t *device;
 	int fd;
+	GMainLoop *loop;
+	GSource *source;
 };
 
 struct window {
@@ -756,6 +758,7 @@
 	window->saved_allocation = window->allocation;
 	window->margin = 16;
 	window->state = WINDOW_STABLE;
+	window->decoration = 1;
 
 	wl_compositor_add_listener(display->compositor,
 				   &compositor_listener, window);
@@ -799,13 +802,20 @@
 	}
 }
 
+static const char gem_device[] = "/dev/dri/card0";
+static const char socket_name[] = "\0wayland";
+
 struct display *
-display_create(struct wl_display *display, int fd)
+display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
 {
 	PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
 	struct display *d;
 	EGLint major, minor, count;
 	EGLConfig config;
+	struct wl_display *display;
+	int fd;
+	GOptionContext *context;
+	GError *error;
 
 	static const EGLint config_attribs[] = {
 		EGL_SURFACE_TYPE,		0,
@@ -814,10 +824,31 @@
 		EGL_NONE
 	};
 
+	context = g_option_context_new(NULL);
+	if (option_entries) {
+		g_option_context_add_main_entries(context, option_entries, "Wayland View");
+		if (!g_option_context_parse(context, argc, argv, &error)) {
+			fprintf(stderr, "option parsing failed: %s\n", error->message);
+			exit(EXIT_FAILURE);
+		}
+	}
+
 	d = malloc(sizeof *d);
 	if (d == NULL)
 		return NULL;
 
+	fd = open(gem_device, O_RDWR);
+	if (fd < 0) {
+		fprintf(stderr, "drm open failed: %m\n");
+		return NULL;
+	}
+
+	display = wl_display_create(socket_name, sizeof socket_name);
+	if (display == NULL) {
+		fprintf(stderr, "failed to create display: %m\n");
+		return NULL;
+	}
+
 	get_typed_display_mesa =
 		(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
 	if (get_typed_display_mesa == NULL) {
@@ -865,6 +896,10 @@
 	/* Process connection events. */
 	wl_display_iterate(display, WL_DISPLAY_READABLE);
 
+	d->loop = g_main_loop_new(NULL, FALSE);
+	d->source = wl_glib_source_new(display);
+	g_source_attach(d->source, NULL);
+
 	return d;
 }
 
@@ -873,3 +908,15 @@
 {
 	return display->compositor;
 }
+
+EGLDisplay
+display_get_egl_display(struct display *d)
+{
+	return d->dpy;
+}
+
+void
+display_run(struct display *d)
+{
+	g_main_loop_run(d->loop);
+}