compositor: Move state tracking complexity into shell

Previously, when coming back from idle the compositor would try to
track if the unlock signal needed to be sent, and the shell would
change the compositor state in order to track when to display or
hide the screensaver.

This patch finishes moving this out of the compositor. With this, the
compositor state should be changed only using the exported functions
weston_compositor_wake() and weston_compositor_sleep(). The unlock
signal will be sent if the compositor wasn't in the ACTIVE state
previously. The lock signal is sent when the compositor becomes idle.

The calls to weston_compositor_wake() in the shell where there to allow
it to trigger the fade in only after the lock surface was configured.
Now the shell has full control of the fade and does not needed to
change the compositor state to do that, so those calls were replaced
with shell_fade() calls.
diff --git a/src/compositor.c b/src/compositor.c
index dab9db7..eb453d0 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1573,40 +1573,48 @@
 	compositor_create_region
 };
 
-WL_EXPORT void
-weston_compositor_wake(struct weston_compositor *compositor)
-{
-	compositor->state = WESTON_COMPOSITOR_ACTIVE;
-
-	wl_event_source_timer_update(compositor->idle_source,
-				     compositor->idle_time * 1000);
-}
-
 static void
-weston_compositor_dpms_on(struct weston_compositor *compositor)
+weston_compositor_dpms(struct weston_compositor *compositor,
+		       enum dpms_enum state)
 {
         struct weston_output *output;
 
         wl_list_for_each(output, &compositor->output_list, link)
 		if (output->set_dpms)
-			output->set_dpms(output, WESTON_DPMS_ON);
+			output->set_dpms(output, state);
 }
 
 WL_EXPORT void
-weston_compositor_activity(struct weston_compositor *compositor)
+weston_compositor_wake(struct weston_compositor *compositor)
 {
-	if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
-		weston_compositor_wake(compositor);
-	} else {
-		weston_compositor_dpms_on(compositor);
+	switch (compositor->state) {
+	case WESTON_COMPOSITOR_SLEEPING:
+		weston_compositor_dpms(compositor, WESTON_DPMS_ON);
+		/* fall through */
+	case WESTON_COMPOSITOR_IDLE:
 		wl_signal_emit(&compositor->unlock_signal, compositor);
+		/* fall through */
+	default:
+		compositor->state = WESTON_COMPOSITOR_ACTIVE;
+		wl_event_source_timer_update(compositor->idle_source,
+					     compositor->idle_time * 1000);
 	}
 }
 
+WL_EXPORT void
+weston_compositor_sleep(struct weston_compositor *compositor)
+{
+	if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
+		return;
+
+	compositor->state = WESTON_COMPOSITOR_SLEEPING;
+	weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
+}
+
 static void
 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
 {
-	weston_compositor_activity(compositor);
+	weston_compositor_wake(compositor);
 	compositor->idle_inhibit++;
 }
 
@@ -1614,7 +1622,7 @@
 weston_compositor_idle_release(struct weston_compositor *compositor)
 {
 	compositor->idle_inhibit--;
-	weston_compositor_activity(compositor);
+	weston_compositor_wake(compositor);
 }
 
 static int
@@ -1694,7 +1702,7 @@
 	struct wl_pointer *pointer = seat->seat.pointer;
 	int32_t ix, iy;
 
-	weston_compositor_activity(ec);
+	weston_compositor_wake(ec);
 
 	clip_pointer_motion(seat, &x, &y);
 
@@ -1788,7 +1796,7 @@
 	if (compositor->ping_handler && focus)
 		compositor->ping_handler(focus, serial);
 
-	weston_compositor_activity(compositor);
+	weston_compositor_wake(compositor);
 
 	if (value)
 		weston_compositor_run_axis_binding(compositor, seat,
@@ -3481,7 +3489,6 @@
 		goto out;
 	}
 
-	weston_compositor_dpms_on(ec);
 	weston_compositor_wake(ec);
 
 	wl_display_run(display);
diff --git a/src/compositor.h b/src/compositor.h
index ef3d2cd..fdee03c 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -593,7 +593,7 @@
 void
 weston_compositor_wake(struct weston_compositor *compositor);
 void
-weston_compositor_activity(struct weston_compositor *compositor);
+weston_compositor_sleep(struct weston_compositor *compositor);
 void
 weston_compositor_update_drag_surfaces(struct weston_compositor *compositor);
 
diff --git a/src/shell.c b/src/shell.c
index 8021012..8175f9a 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -2292,7 +2292,7 @@
 		wl_list_insert(&shell->lock_layer.surface_list,
 			       &surface->layer_link);
 		weston_surface_update_transform(surface);
-		weston_compositor_wake(shell->compositor);
+		shell_fade(shell, FADE_IN);
 	}
 }
 
@@ -2353,7 +2353,7 @@
 	restore_focus_state(shell, get_current_workspace(shell));
 
 	shell->locked = false;
-	weston_compositor_wake(shell->compositor);
+	shell_fade(shell, FADE_IN);
 	weston_compositor_damage_all(shell->compositor);
 }
 
@@ -2780,14 +2780,10 @@
 static void
 lock(struct desktop_shell *shell)
 {
-	struct weston_output *output;
 	struct workspace *ws = get_current_workspace(shell);
 
 	if (shell->locked) {
-		wl_list_for_each(output, &shell->compositor->output_list, link)
-			/* TODO: find a way to jump to other DPMS levels */
-			if (output->set_dpms)
-				output->set_dpms(output, WESTON_DPMS_STANDBY);
+		weston_compositor_sleep(shell->compositor);
 		return;
 	}
 
@@ -2816,7 +2812,7 @@
 unlock(struct desktop_shell *shell)
 {
 	if (!shell->locked || shell->lock_surface) {
-		weston_compositor_wake(shell->compositor);
+		shell_fade(shell, FADE_IN);
 		return;
 	}
 
@@ -2846,7 +2842,6 @@
 		shell->fade.surface = NULL;
 		break;
 	case FADE_OUT:
-		shell->compositor->state = WESTON_COMPOSITOR_SLEEPING;
 		lock(shell);
 		break;
 	}
@@ -2914,7 +2909,6 @@
 	struct desktop_shell *shell =
 		container_of(listener, struct desktop_shell, unlock_listener);
 
-	shell_fade(shell, FADE_IN);
 	unlock(shell);
 }
 
@@ -3322,7 +3316,6 @@
 		wl_event_source_timer_update(shell->screensaver.timer,
 					     shell->screensaver.duration);
 		shell_fade(shell, FADE_IN);
-		shell->compositor->state = WESTON_COMPOSITOR_IDLE;
 	}
 }