shell: Implement "driver" method of fullsceen.

Switching display mode may happen when:
1. The fullscreen surface is at top most in fullscreen layer and with
   "driver" method. Shell will switch output mode to match the surface
   size. If no matched mode found, fall back to "fill" method.
2. The top fullscreen surface is destroyed or unset. Switch back to the
   origin mode.
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index 169f019..8aab840 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -1309,6 +1309,7 @@
 		output_handle_pending_scanout_buffer_destroy;
 
 	output->next_fb_id = 0;
+	output->base.origin = output->base.current;
 	output->base.repaint = drm_output_repaint;
 	output->base.destroy = drm_output_destroy;
 	output->base.assign_planes = drm_assign_planes;
diff --git a/src/compositor-openwfd.c b/src/compositor-openwfd.c
index bbf1067..3548dfb 100644
--- a/src/compositor-openwfd.c
+++ b/src/compositor-openwfd.c
@@ -401,6 +401,7 @@
 
 	wfdDeviceCommit(ec->dev, WFD_COMMIT_ENTIRE_DEVICE, WFD_INVALID_HANDLE);
 
+	output->base.origin = output->base.current;
 	output->base.repaint = wfd_output_repaint;
 	output->base.prepare_scanout_surface =
 		wfd_output_prepare_scanout_surface;
diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index 93094b0..afd9121 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -456,6 +456,7 @@
 	/* FIXME: add shell_surface listener for resizing */
 	wl_shell_surface_set_toplevel(output->parent.shell_surface);
 
+	output->base.origin = output->base.current;
 	output->base.repaint = wayland_output_repaint;
 	output->base.destroy = wayland_output_destroy;
 	output->base.assign_planes = NULL;
diff --git a/src/compositor-x11.c b/src/compositor-x11.c
index 87ab5dd..b910831 100644
--- a/src/compositor-x11.c
+++ b/src/compositor-x11.c
@@ -466,6 +466,7 @@
 	output->finish_frame_timer =
 		wl_event_loop_add_timer(loop, finish_frame_handler, output);
 
+	output->base.origin = output->base.current;
 	output->base.repaint = x11_output_repaint;
 	output->base.destroy = x11_output_destroy;
 	output->base.assign_planes = NULL;
diff --git a/src/compositor.h b/src/compositor.h
index f21f769..a4f5209 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -92,6 +92,7 @@
 	uint32_t subpixel;
 	
 	struct weston_mode *current;
+	struct weston_mode *origin;
 	struct wl_list mode_list;
 
 	void (*repaint)(struct weston_output *output,
diff --git a/src/shell.c b/src/shell.c
index 02061c6..b412d8c 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -155,6 +155,29 @@
 	} center;
 };
 
+static struct shell_surface *
+get_shell_surface(struct weston_surface *surface);
+
+static struct desktop_shell *
+shell_surface_get_shell(struct shell_surface *shsurf);
+
+static bool
+shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
+{
+	struct desktop_shell *shell;
+	struct weston_surface *top_fs_es;
+
+	shell = shell_surface_get_shell(shsurf);
+	
+	if (wl_list_empty(&shell->fullscreen_layer.surface_list))
+		return false;
+
+	top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
+			         struct weston_surface, 
+				 layer_link);
+	return (shsurf == get_shell_surface(top_fs_es));
+}
+
 static void
 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
 {
@@ -189,9 +212,6 @@
 center_on_output(struct weston_surface *surface,
 		 struct weston_output *output);
 
-static struct shell_surface *
-get_shell_surface(struct weston_surface *surface);
-
 static void
 shell_configuration(struct desktop_shell *shell)
 {
@@ -434,11 +454,17 @@
 shell_unset_fullscreen(struct shell_surface *shsurf)
 {
 	/* undo all fullscreen things here */
+	if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
+	    shell_surface_is_top_fullscreen(shsurf)) {
+		weston_output_switch_mode(shsurf->fullscreen_output,
+		                          shsurf->fullscreen_output->origin);
+	}
 	shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
 	shsurf->fullscreen.framerate = 0;
 	wl_list_remove(&shsurf->fullscreen.transform.link);
 	wl_list_init(&shsurf->fullscreen.transform.link);
-	weston_surface_destroy(shsurf->fullscreen.black_surface);
+	if (shsurf->fullscreen.black_surface)
+		weston_surface_destroy(shsurf->fullscreen.black_surface);
 	shsurf->fullscreen.black_surface = NULL;
 	shsurf->fullscreen_output = NULL;
 	shsurf->force_configure = 1;
@@ -638,6 +664,21 @@
 		weston_surface_set_position(surface, output->x, output->y);
 		break;
 	case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
+		if (shell_surface_is_top_fullscreen(shsurf)) {
+			struct weston_mode mode = {0, 
+				surface->geometry.width,
+				surface->geometry.height,
+				shsurf->fullscreen.framerate};
+
+			if (weston_output_switch_mode(output, &mode) == 0) {
+				weston_surface_configure(shsurf->fullscreen.black_surface, 
+					                 output->x, output->y,
+							 output->current->width,
+							 output->current->height);
+				weston_surface_set_position(surface, output->x, output->y);
+				break;
+			}
+		}
 		break;
 	case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
 		break;
@@ -650,26 +691,34 @@
 static void
 shell_stack_fullscreen(struct shell_surface *shsurf)
 {
+	struct weston_output *output = shsurf->fullscreen_output;
 	struct weston_surface *surface = shsurf->surface;
 	struct desktop_shell *shell = shell_surface_get_shell(shsurf);
 
 	wl_list_remove(&surface->layer_link);
-	wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
-
 	wl_list_insert(&shell->fullscreen_layer.surface_list,
 		       &surface->layer_link);
+	weston_surface_damage(surface);
+
+	if (!shsurf->fullscreen.black_surface)
+		shsurf->fullscreen.black_surface =
+			create_black_surface(surface->compositor,
+					     surface,
+					     output->x, output->y,
+					     output->current->width,
+					     output->current->height);
+
+	wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
 	wl_list_insert(&surface->layer_link,
 		       &shsurf->fullscreen.black_surface->layer_link);
-
-	weston_surface_damage(surface);
 	weston_surface_damage(shsurf->fullscreen.black_surface);
 }
 
 static void
 shell_map_fullscreen(struct shell_surface *shsurf)
 {
-	shell_configure_fullscreen(shsurf);
 	shell_stack_fullscreen(shsurf);
+	shell_configure_fullscreen(shsurf);
 }
 
 static void
@@ -844,15 +893,21 @@
 	if (shsurf->popup.grab.input_device)
 		wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
 
-	/* in case cleaning up a dead client destroys shell_surface first */
-	if (shsurf->surface) {
-		wl_list_remove(&shsurf->surface_destroy_listener.link);
-		shsurf->surface->configure = NULL;
+	if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
+	    shell_surface_is_top_fullscreen(shsurf)) {
+		weston_output_switch_mode(shsurf->fullscreen_output,
+					  shsurf->fullscreen_output->origin);
 	}
 
 	if (shsurf->fullscreen.black_surface)
 		weston_surface_destroy(shsurf->fullscreen.black_surface);
 
+	/* As destroy_resource() use wl_list_for_each_safe(),
+	 * we can always remove the listener.
+	 */
+	wl_list_remove(&shsurf->surface_destroy_listener.link);
+	shsurf->surface->configure = NULL;
+
 	wl_list_remove(&shsurf->link);
 	free(shsurf);
 }
@@ -864,7 +919,6 @@
 						    struct shell_surface,
 						    surface_destroy_listener);
 
-	shsurf->surface = NULL;
 	wl_resource_destroy(&shsurf->resource);
 }
 
@@ -897,7 +951,7 @@
 	if (get_shell_surface(surface)) {
 		wl_resource_post_error(surface_resource,
 			WL_DISPLAY_ERROR_INVALID_OBJECT,
-			"wl_shell::get_shell_surface already requested");
+			"desktop_shell::get_shell_surface already requested");
 		return;
 	}
 
@@ -1499,9 +1553,10 @@
 	case SHELL_SURFACE_FULLSCREEN:
 		/* should on top of panels */
 		shell_stack_fullscreen(get_shell_surface(es));
+		shell_configure_fullscreen(get_shell_surface(es));
 		break;
 	default:
-                /* move the fullscreen surfaces down into the toplevel layer */
+		/* move the fullscreen surfaces down into the toplevel layer */
 		if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
 			wl_list_for_each_reverse_safe(surf,
 						      prev, 
@@ -1764,7 +1819,6 @@
 	  GLfloat x, GLfloat y, int32_t width, int32_t height)
 {
 	enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
-	enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
 	struct shell_surface *shsurf;
 
 	shsurf = get_shell_surface(surface);
@@ -1782,9 +1836,8 @@
 		center_on_output(surface, shsurf->fullscreen_output);
 		break;
 	case SHELL_SURFACE_FULLSCREEN:
+		shell_stack_fullscreen(shsurf);
 		shell_configure_fullscreen(shsurf);
-		if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
-			shell_stack_fullscreen(shsurf);
 		break;
 	case SHELL_SURFACE_MAXIMIZED:
 		/* setting x, y and using configure to change that geometry */