[PATCH 4/7] compositor: split timing related code on its own file and scope

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


Currently we have only two states for the timer ("idle" and "duration"), so
the changes introduced will be effectively useful when more states are in
place (for display control).

Anyways, we have now an easy API and internal fields are accessed only via
functions.

Signed-off-by: Tiago Vignatti <tiago.vignatti at intel.com>
---
 src/Makefile.am  |    2 +
 src/compositor.c |   14 ++------
 src/compositor.h |    4 +--
 src/shell.c      |    6 ++-
 src/timer.c      |   85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/timer.h      |   39 ++++++++++++++++++++++++
 6 files changed, 135 insertions(+), 15 deletions(-)
 create mode 100644 src/timer.c
 create mode 100644 src/timer.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 5788df3..aae0dc7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,8 @@ weston_SOURCES =			\
 	screenshooter.c				\
 	screenshooter-protocol.c		\
 	screenshooter-server-protocol.h		\
+	timer.c					\
+	timer.h					\
 	util.c					\
 	matrix.c				\
 	matrix.h				\
diff --git a/src/compositor.c b/src/compositor.c
index a607772..dcf3813 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -49,6 +49,7 @@
 
 #include <wayland-server.h>
 #include "compositor.h"
+#include "timer.h"
 
 static const char *option_socket_name = NULL;
 
@@ -1414,8 +1415,7 @@ weston_compositor_wake(struct weston_compositor *compositor)
 	compositor->state = WESTON_COMPOSITOR_ACTIVE;
 	weston_compositor_fade(compositor, 0.0);
 
-	wl_event_source_timer_update(compositor->idle_source,
-				     compositor->idle_time * 1000);
+	weston_timer_update(compositor->timer);
 }
 
 static void
@@ -2269,7 +2269,6 @@ compositor_bind(struct wl_client *client,
 WL_EXPORT int
 weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
 {
-	struct wl_event_loop *loop;
 	const char *extensions;
 
 	ec->wl_display = display;
@@ -2327,10 +2326,6 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
 			     vertex_shader, solid_fragment_shader) < 0)
 		return -1;
 
-	loop = wl_display_get_event_loop(ec->wl_display);
-	ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
-	wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
-
 	weston_compositor_schedule_repaint(ec);
 
 	return 0;
@@ -2341,7 +2336,7 @@ weston_compositor_shutdown(struct weston_compositor *ec)
 {
 	struct weston_output *output, *next;
 
-	wl_event_source_remove(ec->idle_source);
+	weston_timer_fini(ec);
 
 	if (ec->screenshooter)
 		screenshooter_destroy(ec->screenshooter);
@@ -2524,8 +2519,7 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	ec->option_idle_time = option_idle_time;
-	ec->idle_time = option_idle_time;
+	weston_timer_init(ec, idle_handler, option_idle_time);
 
 	if (xserver)
 		weston_xserver_init(ec);
diff --git a/src/compositor.h b/src/compositor.h
index fc46b10..0cef498 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -181,10 +181,8 @@ struct weston_compositor {
 	} fade;
 
 	uint32_t state;
-	struct wl_event_source *idle_source;
 	uint32_t idle_inhibit;
-	int option_idle_time;		/* default timeout, s */
-	int idle_time;			/* effective timeout, s */
+	struct weston_timer *timer;
 
 	/* Repaint state. */
 	struct timespec previous_swap;
diff --git a/src/shell.c b/src/shell.c
index 689d735..0a1efc1 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -33,6 +33,7 @@
 
 #include <wayland-server.h>
 #include "compositor.h"
+#include "timer.h"
 #include "desktop-shell-server-protocol.h"
 #include "../shared/config-parser.h"
 
@@ -928,7 +929,7 @@ resume_desktop(struct wl_shell *shell)
 
 	shell->locked = false;
 	weston_compositor_repick(shell->compositor);
-	shell->compositor->idle_time = shell->compositor->option_idle_time;
+	weston_timer_reset(shell->compositor->timer);
 	weston_compositor_wake(shell->compositor);
 	weston_compositor_damage_all(shell->compositor);
 }
@@ -1453,7 +1454,8 @@ map(struct weston_shell *base, struct weston_surface *surface,
 		/* If locked, show it. */
 		if (shell->locked) {
 			show_screensaver(shell, shsurf);
-			compositor->idle_time = shell->screensaver.duration;
+			weston_timer_set(compositor->timer,
+					 shell->screensaver.duration);
 			weston_compositor_wake(compositor);
 			if (!shell->lock_surface)
 				compositor->state = WESTON_COMPOSITOR_IDLE;
diff --git a/src/timer.c b/src/timer.c
new file mode 100644
index 0000000..5e706ef
--- /dev/null
+++ b/src/timer.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Tiago Vignatti <vignatti at freedesktop.org>
+ */
+
+#include <stdlib.h>
+#include "compositor.h"
+#include "timer.h"
+
+struct weston_timer {
+        struct wl_event_source *idle_source;
+        int option_idle_time;           /* default timeout, s */
+        int idle_time;                  /* effective timeout, s */
+};
+
+WL_EXPORT void
+weston_timer_update(struct weston_timer *time)
+{
+	wl_event_source_timer_update(time->idle_source,
+				     time->idle_time * 1000);
+}
+
+WL_EXPORT void
+weston_timer_reset(struct weston_timer *time)
+{
+	time->idle_time = time->option_idle_time;
+	/*XXX:  update timer? */
+}
+
+WL_EXPORT void
+weston_timer_set(struct weston_timer *time, int new_time)
+{
+	time->idle_time = new_time;
+	/*XXX:  update timer? */
+}
+
+WL_EXPORT int
+weston_timer_init(struct weston_compositor *ec,
+		    wl_event_loop_timer_func_t func, int option_idle_time)
+{
+	struct wl_event_loop *loop;
+	struct weston_timer *timer;
+
+	timer = malloc(sizeof *timer);
+	if (timer == NULL)
+		return -1;
+
+	ec->timer = timer;
+	timer->option_idle_time = option_idle_time;
+	weston_timer_reset(timer);
+
+	loop = wl_display_get_event_loop(ec->wl_display);
+	timer->idle_source = wl_event_loop_add_timer(loop, func, ec);
+	weston_timer_update(timer);
+
+	return 0;
+}
+
+WL_EXPORT void
+weston_timer_fini(struct weston_compositor *ec)
+{
+	wl_event_source_remove(ec->timer->idle_source);
+
+	free(ec->timer);
+	ec->timer = NULL;
+}
diff --git a/src/timer.h b/src/timer.h
new file mode 100644
index 0000000..12402e1
--- /dev/null
+++ b/src/timer.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2012 Intel
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Tiago Vignatti <vignatti at freedesktop.org>
+ *
+ */
+
+void
+weston_timer_update(struct weston_timer *time);
+
+void
+weston_timer_reset(struct weston_timer *time);
+
+void
+weston_timer_set(struct weston_timer *time, int new_time);
+
+int
+weston_timer_init(struct weston_compositor *ec,
+		  wl_event_loop_timer_func_t func, int option_idle_time);
+void
+weston_timer_fini(struct weston_compositor *ec);
-- 
1.7.5.4



More information about the wayland-devel mailing list