[PATCH weston 02/11] shell: Move workspace set related fields into its own container struct

Jonas Ådahl jadahl at gmail.com
Sat Jan 26 06:33:32 PST 2013


As a step towards implementing multiple workspace sets all workspace set
parameters are moved to its own non-anonymous struct. Only one such
struct is ever instantiated meaning no functional changes were made.

Signed-off-by: Jonas Ådahl <jadahl at gmail.com>
---
 src/shell.c |  268 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 156 insertions(+), 112 deletions(-)

diff --git a/src/shell.c b/src/shell.c
index 67cdf63..6d1d6eb 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -57,9 +57,24 @@ struct focus_state {
 	struct wl_listener surface_destroy_listener;
 };
 
+struct workspace_container {
+	struct wl_array workspaces;
+	unsigned int current;
+	unsigned int num;
+
+	struct weston_animation animation;
+	struct wl_list anim_sticky_list;
+	int anim_dir;
+	uint32_t anim_timestamp;
+	double anim_current;
+	struct workspace *anim_from;
+	struct workspace *anim_to;
+};
+
 struct workspace {
 	struct weston_layer layer;
 
+	struct workspace_container *container;
 	struct wl_list focus_list;
 	struct wl_listener seat_destroyed_listener;
 };
@@ -105,19 +120,10 @@ struct desktop_shell {
 	struct wl_listener lock_surface_listener;
 
 	struct {
-		struct wl_array array;
-		unsigned int current;
-		unsigned int num;
+		struct workspace_container container;
+		unsigned int default_num;
 
 		struct wl_list client_list;
-
-		struct weston_animation animation;
-		struct wl_list anim_sticky_list;
-		int anim_dir;
-		uint32_t anim_timestamp;
-		double anim_current;
-		struct workspace *anim_from;
-		struct workspace *anim_to;
 	} workspaces;
 
 	struct {
@@ -372,7 +378,7 @@ shell_configuration(struct desktop_shell *shell)
 	shell->screensaver.duration = duration;
 	shell->binding_modifier = get_modifier(modifier);
 	shell->win_animation_type = get_animation_type(win_animation);
-	shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
+	shell->workspaces.default_num = num_workspaces > 0 ? num_workspaces : 1;
 }
 
 static void
@@ -527,7 +533,7 @@ seat_destroyed(struct wl_listener *listener, void *data)
 }
 
 static struct workspace *
-workspace_create(void)
+workspace_create(struct workspace_container *container)
 {
 	struct workspace *ws = malloc(sizeof *ws);
 	if (ws == NULL)
@@ -535,6 +541,7 @@ workspace_create(void)
 
 	weston_layer_init(&ws->layer, NULL);
 
+	ws->container = container;
 	wl_list_init(&ws->focus_list);
 	wl_list_init(&ws->seat_destroyed_listener.link);
 	ws->seat_destroyed_listener.notify = seat_destroyed;
@@ -570,8 +577,8 @@ workspace_restack_surface(struct workspace *ws, struct shell_surface *shsurf)
 static struct workspace *
 get_workspace(struct desktop_shell *shell, unsigned int index)
 {
-	struct workspace **pws = shell->workspaces.array.data;
-	assert(index < shell->workspaces.num);
+	struct workspace **pws = shell->workspaces.container.workspaces.data;
+	assert(index < shell->workspaces.container.num);
 	pws += index;
 	return *pws;
 }
@@ -579,7 +586,7 @@ get_workspace(struct desktop_shell *shell, unsigned int index)
 static struct workspace *
 get_current_workspace(struct desktop_shell *shell)
 {
-	return get_workspace(shell, shell->workspaces.current);
+	return get_workspace(shell, shell->workspaces.container.current);
 }
 
 static void
@@ -590,7 +597,49 @@ activate_workspace(struct desktop_shell *shell, unsigned int index)
 	ws = get_workspace(shell, index);
 	wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
 
-	shell->workspaces.current = index;
+	shell->workspaces.container.current = index;
+}
+
+static void
+workspace_container_release(struct workspace_container *container)
+{
+	struct workspace **pws;
+
+	wl_array_for_each(pws, &container->workspaces)
+		workspace_destroy(*pws);
+	wl_array_release(&container->workspaces);
+}
+
+static int
+workspace_container_init(struct workspace_container *container,
+			 struct desktop_shell *shell)
+{
+	struct workspace **pws;
+	unsigned int i;
+
+	container->current = 0;
+	container->num = shell->workspaces.default_num;
+	wl_array_init(&container->workspaces);
+
+	container->anim_to = NULL;
+	wl_list_init(&container->anim_sticky_list);
+
+	for (i = 0; i < container->num; i++) {
+		pws = wl_array_add(&container->workspaces, sizeof *pws);
+		if (pws == NULL) {
+			goto err;
+		}
+
+		*pws = workspace_create(container);
+		if (*pws == NULL)
+			goto err;
+	}
+
+	return 0;
+
+err:
+	workspace_container_release(container);
+	return -1;
 }
 
 static unsigned int
@@ -654,25 +703,27 @@ static void
 broadcast_current_workspace_state(struct desktop_shell *shell)
 {
 	struct wl_resource *resource;
+	struct workspace_container *container = &shell->workspaces.container;
 
 	wl_list_for_each(resource, &shell->workspaces.client_list, link)
 		workspace_manager_send_state(resource,
-					     shell->workspaces.current,
-					     shell->workspaces.num);
+					     container->current,
+					     container->num);
 }
 
 static void
 reverse_workspace_change_animation(struct desktop_shell *shell,
+				   struct workspace_container *container,
 				   unsigned int index,
 				   struct workspace *from,
 				   struct workspace *to)
 {
-	shell->workspaces.current = index;
+	container->current = index;
 
-	shell->workspaces.anim_to = to;
-	shell->workspaces.anim_from = from;
-	shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
-	shell->workspaces.anim_timestamp = 0;
+	container->anim_to = to;
+	container->anim_from = from;
+	container->anim_dir = -1 * container->anim_dir;
+	container->anim_timestamp = 0;
 
 	weston_compositor_schedule_repaint(shell->compositor);
 }
@@ -695,49 +746,52 @@ workspace_deactivate_transforms(struct workspace *ws)
 
 static void
 finish_workspace_change_animation(struct desktop_shell *shell,
+				  struct workspace_container *container,
 				  struct workspace *from,
 				  struct workspace *to)
 {
 	weston_compositor_schedule_repaint(shell->compositor);
 
-	wl_list_remove(&shell->workspaces.animation.link);
+	wl_list_remove(&container->animation.link);
 	workspace_deactivate_transforms(from);
 	workspace_deactivate_transforms(to);
-	shell->workspaces.anim_to = NULL;
+	container->anim_to = NULL;
 
-	wl_list_remove(&shell->workspaces.anim_from->layer.link);
+	wl_list_remove(&container->anim_from->layer.link);
 }
 
 static void
 animate_workspace_change_frame(struct weston_animation *animation,
 			       struct weston_output *output, uint32_t msecs)
 {
-	struct desktop_shell *shell =
-		container_of(animation, struct desktop_shell,
-			     workspaces.animation);
-	struct workspace *from = shell->workspaces.anim_from;
-	struct workspace *to = shell->workspaces.anim_to;
+	struct workspace_container *container =
+		container_of(animation, struct workspace_container,
+			     animation);
+	struct desktop_shell *shell = output->compositor->shell_interface.shell;
+	struct workspace *from = container->anim_from;
+	struct workspace *to = container->anim_to;
 	uint32_t t;
 	double x, y;
 
 	if (workspace_is_empty(from) && workspace_is_empty(to)) {
-		finish_workspace_change_animation(shell, from, to);
+		finish_workspace_change_animation(shell, container,
+						  from, to);
 		return;
 	}
 
-	if (shell->workspaces.anim_timestamp == 0) {
-		if (shell->workspaces.anim_current == 0.0)
-			shell->workspaces.anim_timestamp = msecs;
+	if (container->anim_timestamp == 0) {
+		if (container->anim_current == 0.0)
+			container->anim_timestamp = msecs;
 		else
-			shell->workspaces.anim_timestamp =
+			container->anim_timestamp =
 				msecs -
 				/* Invers of movement function 'y' below. */
-				(asin(1.0 - shell->workspaces.anim_current) *
+				(asin(1.0 - container->anim_current) *
 				 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
 				 M_2_PI);
 	}
 
-	t = msecs - shell->workspaces.anim_timestamp;
+	t = msecs - container->anim_timestamp;
 
 	/*
 	 * x = [0, π/2]
@@ -749,18 +803,20 @@ animate_workspace_change_frame(struct weston_animation *animation,
 	if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
 		weston_compositor_schedule_repaint(shell->compositor);
 
-		workspace_translate_out(from, shell->workspaces.anim_dir * y);
-		workspace_translate_in(to, shell->workspaces.anim_dir * y);
-		shell->workspaces.anim_current = y;
+		workspace_translate_out(from, container->anim_dir * y);
+		workspace_translate_in(to, container->anim_dir * y);
+		container->anim_current = y;
 
 		weston_compositor_schedule_repaint(shell->compositor);
 	}
 	else
-		finish_workspace_change_animation(shell, from, to);
+		finish_workspace_change_animation(shell, container,
+						  from, to);
 }
 
 static void
 animate_workspace_change(struct desktop_shell *shell,
+			 struct workspace_container *container,
 			 unsigned int index,
 			 struct workspace *from,
 			 struct workspace *to)
@@ -769,23 +825,24 @@ animate_workspace_change(struct desktop_shell *shell,
 
 	int dir;
 
-	if (index > shell->workspaces.current)
+	if (index > container->current)
 		dir = -1;
 	else
 		dir = 1;
 
-	shell->workspaces.current = index;
+	container->current = index;
 
-	shell->workspaces.anim_dir = dir;
-	shell->workspaces.anim_from = from;
-	shell->workspaces.anim_to = to;
-	shell->workspaces.anim_current = 0.0;
-	shell->workspaces.anim_timestamp = 0;
+	container->anim_dir = dir;
+	container->anim_from = from;
+	container->anim_to = to;
+	container->anim_current = 0.0;
+	container->anim_timestamp = 0;
+	container->animation.frame = animate_workspace_change_frame;
 
 	output = container_of(shell->compositor->output_list.next,
 			      struct weston_output, link);
 	wl_list_insert(&output->animation_list,
-		       &shell->workspaces.animation.link);
+		       &container->animation.link);
 
 	wl_list_insert(from->layer.link.prev, &to->layer.link);
 
@@ -797,10 +854,12 @@ animate_workspace_change(struct desktop_shell *shell,
 }
 
 static void
-update_workspace(struct desktop_shell *shell, unsigned int index,
-		 struct workspace *from, struct workspace *to)
+update_workspace(struct workspace_container *container,
+		 unsigned int index,
+		 struct workspace *from,
+		 struct workspace *to)
 {
-	shell->workspaces.current = index;
+	container->current = index;
 	wl_list_insert(&from->layer.link, &to->layer.link);
 	wl_list_remove(&from->layer.link);
 }
@@ -808,10 +867,11 @@ update_workspace(struct desktop_shell *shell, unsigned int index,
 static void
 change_workspace(struct desktop_shell *shell, unsigned int index)
 {
+	struct workspace_container *container = &shell->workspaces.container;
 	struct workspace *from;
 	struct workspace *to;
 
-	if (index == shell->workspaces.current)
+	if (index == container->current)
 		return;
 
 	/* Don't change workspace when there is any fullscreen surfaces. */
@@ -821,25 +881,26 @@ change_workspace(struct desktop_shell *shell, unsigned int index)
 	from = get_current_workspace(shell);
 	to = get_workspace(shell, index);
 
-	if (shell->workspaces.anim_from == to &&
-	    shell->workspaces.anim_to == from) {
+	if (container->anim_from == to &&
+	    container->anim_to == from) {
 		restore_focus_state(shell, to);
-		reverse_workspace_change_animation(shell, index, from, to);
+		reverse_workspace_change_animation(shell, container, index,
+						   from, to);
 		broadcast_current_workspace_state(shell);
 		return;
 	}
 
-	if (shell->workspaces.anim_to != NULL)
-		finish_workspace_change_animation(shell,
-						  shell->workspaces.anim_from,
-						  shell->workspaces.anim_to);
+	if (container->anim_to != NULL)
+		finish_workspace_change_animation(shell, container,
+						  container->anim_from,
+						  container->anim_to);
 
 	restore_focus_state(shell, to);
 
 	if (workspace_is_empty(to) && workspace_is_empty(from))
-		update_workspace(shell, index, from, to);
+		update_workspace(container, index, from, to);
 	else
-		animate_workspace_change(shell, index, from, to);
+		animate_workspace_change(shell, container, index, from, to);
 
 	broadcast_current_workspace_state(shell);
 }
@@ -866,17 +927,17 @@ move_surface_to_workspace(struct desktop_shell *shell,
 			  struct weston_surface *surface,
 			  uint32_t workspace)
 {
+	struct workspace_container *container = &shell->workspaces.container;
 	struct workspace *from;
 	struct workspace *to;
 	struct shell_surface *shsurf = get_shell_surface(surface);
 	struct weston_seat *seat;
 
-	if (!shsurf || workspace == shell->workspaces.current)
+	if (!shsurf ||
+	    workspace >= container->num ||
+	    workspace == container->current)
 		return;
 
-	if (workspace >= shell->workspaces.num)
-		workspace = shell->workspaces.num - 1;
-
 	from = shsurf->workspace;
 	to = get_workspace(shell, workspace);
 
@@ -896,6 +957,7 @@ take_surface_to_workspace_by_seat(struct desktop_shell *shell,
 				  struct wl_seat *wl_seat,
 				  unsigned int index)
 {
+	struct workspace_container *container = &shell->workspaces.container;
 	struct weston_seat *seat = (struct weston_seat *) wl_seat;
 	struct weston_surface *surface =
 		(struct weston_surface *) wl_seat->keyboard->focus;
@@ -905,7 +967,7 @@ take_surface_to_workspace_by_seat(struct desktop_shell *shell,
 	struct focus_state *state;
 
 	if (surface == NULL ||
-	    index == shell->workspaces.current)
+	    index == container->current)
 		return;
 
 	shsurf = get_shell_surface(surface);
@@ -920,32 +982,32 @@ take_surface_to_workspace_by_seat(struct desktop_shell *shell,
 	replace_focus_state(shell, to, seat);
 	drop_focus_state(shell, from, surface);
 
-	if (shell->workspaces.anim_from == to &&
-	    shell->workspaces.anim_to == from) {
+	if (container->anim_from == to &&
+	    container->anim_to == from) {
 		wl_list_remove(&to->layer.link);
 		wl_list_insert(from->layer.link.prev, &to->layer.link);
 
-		reverse_workspace_change_animation(shell, index, from, to);
+		reverse_workspace_change_animation(shell, container, index,
+						   from, to);
 		broadcast_current_workspace_state(shell);
 
 		return;
 	}
 
-	if (shell->workspaces.anim_to != NULL)
-		finish_workspace_change_animation(shell,
-						  shell->workspaces.anim_from,
-						  shell->workspaces.anim_to);
+	if (container->anim_to != NULL)
+		finish_workspace_change_animation(shell, container,
+						  container->anim_from,
+						  container->anim_to);
 
 	if (workspace_is_empty(from) &&
 	    workspace_has_only(to, surface))
-		update_workspace(shell, index, from, to);
+		update_workspace(container, index, from, to);
 	else {
-		shsurf = get_shell_surface(surface);
 		if (wl_list_empty(&shsurf->workspace_transform.link))
-			wl_list_insert(&shell->workspaces.anim_sticky_list,
+			wl_list_insert(&container->anim_sticky_list,
 				       &shsurf->workspace_transform.link);
 
-		animate_workspace_change(shell, index, from, to);
+		animate_workspace_change(shell, container, index, from, to);
 	}
 
 	broadcast_current_workspace_state(shell);
@@ -999,8 +1061,8 @@ bind_workspace_manager(struct wl_client *client,
 	wl_list_insert(&shell->workspaces.client_list, &resource->link);
 
 	workspace_manager_send_state(resource,
-				     shell->workspaces.current,
-				     shell->workspaces.num);
+				     shell->workspaces.container.current,
+				     shell->workspaces.container.num);
 }
 
 static void
@@ -3669,7 +3731,7 @@ workspace_up_binding(struct wl_seat *seat, uint32_t time,
 		     uint32_t key, void *data)
 {
 	struct desktop_shell *shell = data;
-	unsigned int new_index = shell->workspaces.current;
+	unsigned int new_index = shell->workspaces.container.current;
 
 	if (shell->locked)
 		return;
@@ -3684,11 +3746,11 @@ workspace_down_binding(struct wl_seat *seat, uint32_t time,
 		       uint32_t key, void *data)
 {
 	struct desktop_shell *shell = data;
-	unsigned int new_index = shell->workspaces.current;
+	unsigned int new_index = shell->workspaces.container.current;
 
 	if (shell->locked)
 		return;
-	if (new_index < shell->workspaces.num - 1)
+	if (new_index < shell->workspaces.container.num - 1)
 		new_index++;
 
 	change_workspace(shell, new_index);
@@ -3704,8 +3766,8 @@ workspace_f_binding(struct wl_seat *seat, uint32_t time,
 	if (shell->locked)
 		return;
 	new_index = key - KEY_F1;
-	if (new_index >= shell->workspaces.num)
-		new_index = shell->workspaces.num - 1;
+	if (new_index >= shell->workspaces.container.num)
+		new_index = shell->workspaces.container.num - 1;
 
 	change_workspace(shell, new_index);
 }
@@ -3715,7 +3777,7 @@ workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
 				  uint32_t key, void *data)
 {
 	struct desktop_shell *shell = data;
-	unsigned int new_index = shell->workspaces.current;
+	unsigned int new_index = shell->workspaces.container.current;
 
 	if (shell->locked)
 		return;
@@ -3731,12 +3793,12 @@ workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
 				    uint32_t key, void *data)
 {
 	struct desktop_shell *shell = data;
-	unsigned int new_index = shell->workspaces.current;
+	unsigned int new_index = shell->workspaces.container.current;
 
 	if (shell->locked)
 		return;
 
-	if (new_index < shell->workspaces.num - 1)
+	if (new_index < shell->workspaces.container.num - 1)
 		new_index++;
 
 	take_surface_to_workspace_by_seat(shell, seat, new_index);
@@ -3747,7 +3809,6 @@ shell_destroy(struct wl_listener *listener, void *data)
 {
 	struct desktop_shell *shell =
 		container_of(listener, struct desktop_shell, destroy_listener);
-	struct workspace **ws;
 
 	if (shell->child.client)
 		wl_client_destroy(shell->child.client);
@@ -3757,9 +3818,7 @@ shell_destroy(struct wl_listener *listener, void *data)
 	wl_list_remove(&shell->show_input_panel_listener.link);
 	wl_list_remove(&shell->hide_input_panel_listener.link);
 
-	wl_array_for_each(ws, &shell->workspaces.array)
-		workspace_destroy(*ws);
-	wl_array_release(&shell->workspaces.array);
+	workspace_container_release(&shell->workspaces.container);
 
 	free(shell->screensaver.path);
 	free(shell);
@@ -3821,8 +3880,8 @@ shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
 					  shell);
 
 	/* Add bindings for mod+F[1-6] for workspace 1 to 6. */
-	if (shell->workspaces.num > 1) {
-		num_workspace_bindings = shell->workspaces.num;
+	if (shell->workspaces.container.num > 1) {
+		num_workspace_bindings = shell->workspaces.container.num;
 		if (num_workspace_bindings > 6)
 			num_workspace_bindings = 6;
 		for (i = 0; i < num_workspace_bindings; i++)
@@ -3843,8 +3902,6 @@ module_init(struct weston_compositor *ec)
 {
 	struct weston_seat *seat;
 	struct desktop_shell *shell;
-	struct workspace **pws;
-	unsigned int i;
 	struct wl_event_loop *loop;
 
 	shell = malloc(sizeof *shell);
@@ -3880,26 +3937,13 @@ module_init(struct weston_compositor *ec)
 	weston_layer_init(&shell->lock_layer, NULL);
 	weston_layer_init(&shell->input_panel_layer, NULL);
 
-	wl_array_init(&shell->workspaces.array);
 	wl_list_init(&shell->workspaces.client_list);
 
 	shell_configuration(shell);
 
-	for (i = 0; i < shell->workspaces.num; i++) {
-		pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
-		if (pws == NULL)
-			return -1;
-
-		*pws = workspace_create();
-		if (*pws == NULL)
-			return -1;
-	}
+	workspace_container_init(&shell->workspaces.container, shell);
 	activate_workspace(shell, 0);
 
-	wl_list_init(&shell->workspaces.anim_sticky_list);
-	wl_list_init(&shell->workspaces.animation.link);
-	shell->workspaces.animation.frame = animate_workspace_change_frame;
-
 	if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
 				  shell, bind_shell) == NULL)
 		return -1;
-- 
1.7.10.4



More information about the wayland-devel mailing list