[PATCH 7/7] compositor: implement a state machine for display control

Tiago Vignatti tiago.vignatti at intel.com
Fri Feb 24 08:08:45 PST 2012


DISPLAY_ON → DISPLAY_DIM → DISPLAY_SCREENSAVER → DISPLAY_OFF

transitions between states happen in this same sequential way and it's based
on time of user idleness. Once some (input) activity is generated, then it
jumps back to the first state.

We're still using one single timer for this implementation.

Signed-off-by: Tiago Vignatti <tiago.vignatti at intel.com>
---
 src/compositor.c         |    2 +-
 src/compositor.h         |    7 ++++++
 src/shell.c              |   32 +++++++++++++++++++++-----
 src/timer.c              |   55 +++++++++++++++++++++++++++++++++++++--------
 src/timer.h              |   10 ++++++-
 weston-desktop-shell.ini |    1 +
 6 files changed, 88 insertions(+), 19 deletions(-)

diff --git a/src/compositor.c b/src/compositor.c
index 264784d..39506f8 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1415,7 +1415,7 @@ weston_compositor_wake(struct weston_compositor *compositor)
 	compositor->state = WESTON_COMPOSITOR_ACTIVE;
 	weston_compositor_fade(compositor, 0.0);
 
-	weston_timer_update(compositor->timer);
+	weston_timer_reset(compositor->timer);
 }
 
 static void
diff --git a/src/compositor.h b/src/compositor.h
index 0cef498..d6f176b 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -149,6 +149,13 @@ enum {
 	WESTON_COMPOSITOR_SLEEPING	/* no rendering, no frame events */
 };
 
+enum display_state {
+	WESTON_DISPLAY_ON,
+	WESTON_DISPLAY_DIM,
+	WESTON_DISPLAY_SCREENSAVER,
+	WESTON_DISPLAY_OFF
+};
+
 struct screenshooter;
 
 struct weston_compositor {
diff --git a/src/shell.c b/src/shell.c
index 0029a1e..b40ea5f 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -64,7 +64,6 @@ struct wl_shell {
 
 	struct {
 		char *path;
-		int duration;
 		struct wl_resource *binding;
 		struct wl_list surfaces;
 		struct weston_process process;
@@ -138,7 +137,26 @@ idle_handler(void *data)
 	if (compositor->idle_inhibit)
 		return 1;
 
-	weston_compositor_fade(compositor, 1.0);
+	switch(weston_timer_current(compositor->timer)) {
+	case WESTON_DISPLAY_ON:
+		fprintf(stderr, "DIM: libbacklight: set darker\n");
+		weston_timer_set(compositor->timer, WESTON_DISPLAY_DIM);
+		/*TODO: hook compositor-drm routine for display dimming */
+		break;
+	case WESTON_DISPLAY_DIM:
+		fprintf(stderr, "SCREENSAVER:\n");
+		weston_timer_set(compositor->timer,
+				 WESTON_DISPLAY_SCREENSAVER);
+		weston_compositor_fade(compositor, 1.0);
+		break;
+	case WESTON_DISPLAY_SCREENSAVER:
+		fprintf(stderr, "OFF: dpms: off\n");
+		weston_timer_set(compositor->timer, WESTON_DISPLAY_OFF);
+		/* TODO: hook compositor-drm routine to DPMS off */
+		break;
+	default:
+		break;
+	}
 
 	return 1;
 }
@@ -148,12 +166,14 @@ shell_configuration(struct wl_shell *shell)
 {
 	char *config_file;
 	char *path = NULL;
-	int display_off = 60;
 	int screensaver = 300;
+	int dimming = 10;
+	int display_off = 60;
 
 	struct config_key saver_keys[] = {
 		{ "path",       CONFIG_KEY_STRING,  &path },
 		{ "screensaver",   CONFIG_KEY_INTEGER, &screensaver },
+		{ "dimming",   CONFIG_KEY_INTEGER, &dimming },
 		{ "display-off",   CONFIG_KEY_INTEGER, &display_off },
 	};
 
@@ -166,8 +186,8 @@ shell_configuration(struct wl_shell *shell)
 	free(config_file);
 
 	shell->screensaver.path = path;
-	shell->screensaver.duration = display_off;
-	weston_timer_init(shell->compositor, idle_handler, screensaver);
+	weston_timer_init(shell->compositor, idle_handler, screensaver,
+			  dimming, display_off);
 }
 
 static void
@@ -1471,7 +1491,7 @@ map(struct weston_shell *base, struct weston_surface *surface,
 		if (shell->locked) {
 			show_screensaver(shell, shsurf);
 			weston_timer_set(compositor->timer,
-					 shell->screensaver.duration);
+					 WESTON_DISPLAY_SCREENSAVER);
 			weston_compositor_wake(compositor);
 			if (!shell->lock_surface)
 				compositor->state = WESTON_COMPOSITOR_IDLE;
diff --git a/src/timer.c b/src/timer.c
index 5e706ef..951a941 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -23,15 +23,27 @@
  */
 
 #include <stdlib.h>
+#include <stdio.h>
 #include "compositor.h"
 #include "timer.h"
 
 struct weston_timer {
+	enum display_state current;
+
+	int32_t screensaver_time;
+	int32_t dim_time;
+	int32_t displayoff_time;
+
         struct wl_event_source *idle_source;
-        int option_idle_time;           /* default timeout, s */
         int idle_time;                  /* effective timeout, s */
 };
 
+WL_EXPORT enum display_state
+weston_timer_current(struct weston_timer *time)
+{
+	return time->current;
+}
+
 WL_EXPORT void
 weston_timer_update(struct weston_timer *time)
 {
@@ -42,20 +54,42 @@ weston_timer_update(struct weston_timer *time)
 WL_EXPORT void
 weston_timer_reset(struct weston_timer *time)
 {
-	time->idle_time = time->option_idle_time;
-	/*XXX:  update timer? */
+	weston_timer_set(time, WESTON_DISPLAY_ON);
 }
 
 WL_EXPORT void
-weston_timer_set(struct weston_timer *time, int new_time)
+weston_timer_set(struct weston_timer *timer, enum display_state state)
 {
-	time->idle_time = new_time;
-	/*XXX:  update timer? */
+	switch(state) {
+	case WESTON_DISPLAY_ON:
+		fprintf(stderr, "is dpms on? \n");
+		fprintf(stderr, "libbacklight: is clear?\n");
+		timer->idle_time = timer->dim_time;
+		break;
+	case WESTON_DISPLAY_DIM:
+		timer->idle_time = timer->screensaver_time;
+		break;
+	case WESTON_DISPLAY_SCREENSAVER:
+		timer->idle_time = timer->displayoff_time;
+		break;
+	case WESTON_DISPLAY_OFF:
+		timer->idle_time = 0;
+		break;
+	default:
+		fprintf(stderr, "weston_timer_set: invalid display_state\n");
+		return;
+	}
+
+	timer->current = state;
+	weston_timer_update(timer);
 }
 
 WL_EXPORT int
 weston_timer_init(struct weston_compositor *ec,
-		    wl_event_loop_timer_func_t func, int option_idle_time)
+		  wl_event_loop_timer_func_t func,
+		  int screensaver,
+		  int dimming,
+		  int display_off)
 {
 	struct wl_event_loop *loop;
 	struct weston_timer *timer;
@@ -65,12 +99,13 @@ weston_timer_init(struct weston_compositor *ec,
 		return -1;
 
 	ec->timer = timer;
-	timer->option_idle_time = option_idle_time;
-	weston_timer_reset(timer);
+	timer->screensaver_time = screensaver;
+	timer->dim_time = dimming;
+	timer->displayoff_time = display_off;
 
 	loop = wl_display_get_event_loop(ec->wl_display);
 	timer->idle_source = wl_event_loop_add_timer(loop, func, ec);
-	weston_timer_update(timer);
+	weston_timer_set(ec->timer, WESTON_DISPLAY_ON);
 
 	return 0;
 }
diff --git a/src/timer.h b/src/timer.h
index 12402e1..5ca6f12 100644
--- a/src/timer.h
+++ b/src/timer.h
@@ -23,6 +23,9 @@
  *
  */
 
+enum display_state
+weston_timer_current(struct weston_timer *time);
+
 void
 weston_timer_update(struct weston_timer *time);
 
@@ -30,10 +33,13 @@ void
 weston_timer_reset(struct weston_timer *time);
 
 void
-weston_timer_set(struct weston_timer *time, int new_time);
+weston_timer_set(struct weston_timer *time, enum display_state state);
 
 int
 weston_timer_init(struct weston_compositor *ec,
-		  wl_event_loop_timer_func_t func, int option_idle_time);
+		  wl_event_loop_timer_func_t func,
+		  int screensaver,
+		  int dimming,
+		  int display_off);
 void
 weston_timer_fini(struct weston_compositor *ec);
diff --git a/weston-desktop-shell.ini b/weston-desktop-shell.ini
index 72486bc..53858a3 100644
--- a/weston-desktop-shell.ini
+++ b/weston-desktop-shell.ini
@@ -23,5 +23,6 @@ path=./clients/flower
 [screensaver]
 #path=./clients/wscreensaver
 screensaver=300
+dimming=10
 display-off=600
 
-- 
1.7.5.4



More information about the wayland-devel mailing list