Consolidate more code in clients/window.c
diff --git a/clients/flower.c b/clients/flower.c
index 5f3a85e..8b5e1bb 100644
--- a/clients/flower.c
+++ b/clients/flower.c
@@ -36,9 +36,6 @@
 #include "wayland-glib.h"
 #include "window.h"
 
-static const char gem_device[] = "/dev/dri/card0";
-static const char socket_name[] = "\0wayland";
-
 static void
 set_random_color(cairo_t *cr)
 {
@@ -120,29 +117,10 @@
 	int fd;
 	cairo_surface_t *s;
 	struct timespec ts;
-	GMainLoop *loop;
-	GSource *source;
 	struct flower flower;
 	struct display *d;
 
-	fd = open(gem_device, O_RDWR);
-	if (fd < 0) {
-		fprintf(stderr, "drm open failed: %m\n");
-		return -1;
-	}
-
-	loop = g_main_loop_new(NULL, FALSE);
-
-	display = wl_display_create(socket_name, sizeof socket_name);
-	if (display == NULL) {
-		fprintf(stderr, "failed to create display: %m\n");
-		return -1;
-	}
-
-	d = display_create(display, fd);
-
-	source = wl_glib_source_new(display);
-	g_source_attach(source, NULL);
+	d = display_create(&argc, &argv, NULL);
 
 	flower.x = 512;
 	flower.y = 384;
@@ -155,6 +133,7 @@
 	srandom(ts.tv_nsec);
 	flower.offset = random();
 
+	window_set_decoration(flower.window, 0);
 	window_draw(flower.window);
 	s = window_get_surface(flower.window);
 	if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
@@ -168,7 +147,7 @@
 	window_set_frame_handler(flower.window, handle_frame, &flower);
 	window_commit(flower.window, 0);
 
-	g_main_loop_run(loop);
+	display_run(d);
 
 	return 0;
 }
diff --git a/clients/gears.c b/clients/gears.c
index ed71447..789537b 100644
--- a/clients/gears.c
+++ b/clients/gears.c
@@ -353,9 +353,8 @@
 };
 
 static struct gears *
-gears_create(struct display *display, int drm_fd)
+gears_create(struct display *display)
 {
-	PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
 	const int x = 200, y = 200, width = 450, height = 500;
 	EGLint major, minor, count;
 	EGLConfig config;
@@ -369,19 +368,13 @@
 		EGL_NONE
 	};
 
-	get_typed_display_mesa =
-		(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
-	if (get_typed_display_mesa == NULL)
-		die("eglGetDisplayMESA() not found\n");
-
 	gears = malloc(sizeof *gears);
 	memset(gears, 0, sizeof *gears);
 	gears->d = display;
 	gears->window = window_create(display, "Wayland Gears",
 				      x, y, width, height);
 
-	gears->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
-						(void *) drm_fd);
+	gears->display = display_get_egl_display(gears->d);
 	if (gears->display == NULL)
 		die("failed to create egl display\n");
 
@@ -458,34 +451,12 @@
 
 int main(int argc, char *argv[])
 {
-	struct wl_display *display;
 	struct display *d;
-	int fd;
-	GMainLoop *loop;
-	GSource *source;
 	struct gears *gears;
 
-	fd = open(gem_device, O_RDWR);
-	if (fd < 0) {
-		fprintf(stderr, "drm open failed: %m\n");
-		return -1;
-	}
-
-	display = wl_display_create(socket_name, sizeof socket_name);
-	if (display == NULL) {
-		fprintf(stderr, "failed to create display: %m\n");
-		return -1;
-	}
-
-	d = display_create(display, fd);
-
-	loop = g_main_loop_new(NULL, FALSE);
-	source = wl_glib_source_new(display);
-	g_source_attach(source, NULL);
-
-	gears = gears_create(d, fd);
-
-	g_main_loop_run(loop);
+	d = display_create(&argc, &argv, NULL);
+	gears = gears_create(d);
+	display_run(d);
 
 	return 0;
 }
diff --git a/clients/image.c b/clients/image.c
index 3f206fc..1403f55 100644
--- a/clients/image.c
+++ b/clients/image.c
@@ -299,40 +299,10 @@
 int
 main(int argc, char *argv[])
 {
-	struct wl_display *display;
-	int fd;
-	GMainLoop *loop;
-	GSource *source;
 	struct display *d;
-	GOptionContext *context;
-	GError *error;
 	int i;
 
-	context = g_option_context_new(NULL);
-	g_option_context_add_main_entries(context, option_entries, "Wayland Image");
-	if (!g_option_context_parse(context, &argc, &argv, &error)) {
-		fprintf(stderr, "option parsing failed: %s\n", error->message);
-		exit(EXIT_FAILURE);
-	}
-
-	fd = open(gem_device, O_RDWR);
-	if (fd < 0) {
-		fprintf(stderr, "drm open failed: %m\n");
-		return -1;
-	}
-
-	display = wl_display_create(socket_name, sizeof socket_name);
-	if (display == NULL) {
-		fprintf(stderr, "failed to create display: %m\n");
-		return -1;
-	}
-
-	d = display_create(display, fd);
-	loop = g_main_loop_new(NULL, FALSE);
-	source = wl_glib_source_new(display);
-	g_source_attach(source, NULL);
-
-	g_type_init ();
+	d = display_create(&argc, &argv, option_entries);
 
 	for (i = 1; i < argc; i++) {
 		struct image *image;
@@ -340,7 +310,7 @@
 		image = image_create (d, i, argv[i]);
 	}
 
-	g_main_loop_run(loop);
+	display_run(d);
 
 	return 0;
 }
diff --git a/clients/terminal.c b/clients/terminal.c
index b4800d5..48e137b 100644
--- a/clients/terminal.c
+++ b/clients/terminal.c
@@ -42,8 +42,6 @@
 #include "window.h"
 
 static int option_fullscreen;
-static const char gem_device[] = "/dev/dri/card0";
-static const char socket_name[] = "\0wayland";
 
 #define MOD_SHIFT	0x01
 #define MOD_ALT		0x02
@@ -564,44 +562,16 @@
 
 int main(int argc, char *argv[])
 {
-	struct wl_display *display;
-	int fd;
-	GMainLoop *loop;
-	GSource *source;
 	struct display *d;
 	struct terminal *terminal;
-	GOptionContext *context;
-	GError *error;
 
-	context = g_option_context_new(NULL);
-	g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
-	if (!g_option_context_parse(context, &argc, &argv, &error)) {
-		fprintf(stderr, "option parsing failed: %s\n", error->message);
-		exit(EXIT_FAILURE);
-	}
-
-	fd = open(gem_device, O_RDWR);
-	if (fd < 0) {
-		fprintf(stderr, "drm open failed: %m\n");
-		return -1;
-	}
-
-	display = wl_display_create(socket_name, sizeof socket_name);
-	if (display == NULL) {
-		fprintf(stderr, "failed to create display: %m\n");
-		return -1;
-	}
-
-	d = display_create(display, fd);
-	loop = g_main_loop_new(NULL, FALSE);
-	source = wl_glib_source_new(display);
-	g_source_attach(source, NULL);
+	d = display_create(&argc, &argv, option_entries);
 
 	terminal = terminal_create(d, option_fullscreen);
 	if (terminal_run(terminal, "/bin/bash"))
 		exit(EXIT_FAILURE);
 
-	g_main_loop_run(loop);
+	display_run(d);
 
 	return 0;
 }
diff --git a/clients/view.c b/clients/view.c
index 45cfafe..8567b84 100644
--- a/clients/view.c
+++ b/clients/view.c
@@ -266,40 +266,10 @@
 int
 main(int argc, char *argv[])
 {
-	struct wl_display *display;
-	int fd;
-	GMainLoop *loop;
-	GSource *source;
 	struct display *d;
-	GOptionContext *context;
-	GError *error = NULL;
 	int i;
 
-	context = g_option_context_new(NULL);
-	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);
-	}
-
-	fd = open(gem_device, O_RDWR);
-	if (fd < 0) {
-		fprintf(stderr, "drm open failed: %m\n");
-		return -1;
-	}
-
-	display = wl_display_create(socket_name, sizeof socket_name);
-	if (display == NULL) {
-		fprintf(stderr, "failed to create display: %m\n");
-		return -1;
-	}
-
-	d = display_create(display, fd);
-	loop = g_main_loop_new(NULL, FALSE);
-	source = wl_glib_source_new(display);
-	g_source_attach(source, NULL);
-
-	g_type_init ();
+	d = display_create(&argc, &argv, option_entries);
 
 	for (i = 1; i < argc; i++) {
 		struct view *view;
@@ -307,7 +277,7 @@
 		view = view_create (d, i, argv[i]);
 	}
 
-	g_main_loop_run(loop);
+	display_run(d);
 
 	return 0;
 }
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);
+}
diff --git a/clients/window.h b/clients/window.h
index 95d619a..e913ca3 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -35,10 +35,19 @@
 struct display;
 
 struct display *
-display_create(struct wl_display *display, int fd);
+display_create(int *argc, char **argv[], const GOptionEntry *option_entries);
+
 struct wl_compositor *
 display_get_compositor(struct display *display);
 
+#ifdef EGL_NO_DISPLAY
+EGLDisplay
+display_get_egl_display(struct display *d);
+#endif
+
+void
+display_run(struct display *d);
+
 enum {
 	WINDOW_MODIFIER_SHIFT = 0x01,
 	WINDOW_MODIFIER_ALT = 0x02,
@@ -114,4 +123,5 @@
 void
 window_set_frame_handler(struct window *window,
 			 window_frame_handler_t handler, void *data);
+
 #endif