[PATCH weston 3/8 v2] shell: Implement menu for window list items.

Scott Moreau oreaus at gmail.com
Wed Nov 14 23:37:02 PST 2012


Right-click dropdown menu for window list items. This patch introduces a simple
minimize feature. The surface is removed from the layer list on minimize and
re-added on unminimize. The close item sends the client a SIGTERM signal.
---
 clients/desktop-shell.c    | 175 +++++++++++++++++++++++++++++++++---
 protocol/desktop-shell.xml |  20 +++++
 src/compositor.c           |   1 +
 src/compositor.h           |   1 +
 src/shell.c                | 216 ++++++++++++++++++++++++++++++++++++++++++---
 5 files changed, 393 insertions(+), 20 deletions(-)

diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 564d5c4..0eac1f4 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -36,6 +36,7 @@
 #include <libgen.h>
 #include <ctype.h>
 #include <time.h>
+#include <stdbool.h>
 
 #include <wayland-client.h>
 #include "window.h"
@@ -66,10 +67,13 @@ struct desktop {
 };
 
 struct surface {
-	struct wl_list item_list;
+	struct desktop *desktop;
 	struct surface_data *surface_data;
+	struct wl_list item_list;
 	uint32_t output_mask;
 	char *title;
+	bool minimized;
+	bool focused;
 
 	struct wl_list link;
 };
@@ -190,13 +194,13 @@ sigchild_handler(int s)
 }
 
 static void
-menu_func(struct window *window, int index, void *data)
+panel_menu_func(struct window *window, int index, void *data)
 {
 	printf("Selected index %d from a panel menu.\n", index);
 }
 
 static void
-show_menu(struct panel *panel, struct input *input, uint32_t time)
+panel_show_menu(struct panel *panel, struct input *input, uint32_t time)
 {
 	int32_t x, y;
 	static const char *entries[] = {
@@ -206,7 +210,7 @@ show_menu(struct panel *panel, struct input *input, uint32_t time)
 	input_get_position(input, &x, &y);
 	window_show_menu(window_get_display(panel->window),
 			 input, time, panel->window,
-			 x - 10, y - 10, menu_func, entries, 4);
+			 x - 10, y - 10, panel_menu_func, entries, 4);
 }
 
 static void
@@ -460,7 +464,7 @@ panel_button_handler(struct widget *widget,
 	struct panel *panel = data;
 
 	if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
-		show_menu(panel, input, time);
+		panel_show_menu(panel, input, time);
 }
 
 static void
@@ -1082,7 +1086,7 @@ panel_list_item_redraw_handler(struct widget *widget, void *data)
 	surface = window_get_surface(item->panel->window);
 	cr = cairo_create(surface);
 
-	if (item->focused) {
+	if (item->focused || item->surface->focused) {
 		cairo_set_source_rgba(cr, item->panel->focused_item.r,
 					  item->panel->focused_item.g,
 					  item->panel->focused_item.b,
@@ -1141,7 +1145,7 @@ panel_list_item_redraw_handler(struct widget *widget, void *data)
 	cairo_show_text(cr, title);
 	cairo_move_to(cr, rect.x + icon_width + padding * 3,
 		      rect.y + 3 * (rect.height >> 2));
-	if (item->focused)
+	if (item->highlight)
 		cairo_set_source_rgb(cr, 1, 1, 1);
 	else
 		cairo_set_source_rgb(cr, 0.85, 0.85, 0.85);
@@ -1166,7 +1170,8 @@ panel_list_item_enter_handler(struct widget *widget, struct input *input,
 {
 	struct list_item *item = data;
 
-	item->focused = 1;
+	item->highlight = true;
+	item->focused = true;
 	widget_schedule_redraw(widget);
 
 	return CURSOR_LEFT_PTR;
@@ -1178,19 +1183,115 @@ panel_list_item_leave_handler(struct widget *widget,
 {
 	struct list_item *item = data;
 
-	item->focused = 0;
+	item->highlight = false;
+	item->focused = false;
 	widget_destroy_tooltip(widget);
 	widget_schedule_redraw(widget);
 }
 
 static void
+desktop_update_list_items(struct desktop *desktop, struct surface *surface);
+
+static void
+list_item_menu_handle_button(struct list_item *item, int index)
+{
+	struct surface *surface = item->surface;
+
+	switch (index) {
+	case 0: /* (Un)Minimize */
+		if (surface->minimized) {
+			surface_data_unminimize(surface->surface_data);
+			surface->minimized = false;
+		}
+		else {
+			surface_data_minimize(surface->surface_data);
+			surface->minimized = true;
+		}
+		break;
+	case 1: /* Close */
+		surface_data_close(surface->surface_data);
+		break;
+	default:
+		item->highlight = false;
+		break;
+	}
+
+	desktop_update_list_items(surface->desktop, surface);
+	widget_destroy_tooltip(item->widget);
+	widget_schedule_redraw(item->widget);
+}
+
+static void
+list_item_menu_func(struct window *window, int index, void *data)
+{
+	struct list_item *item;
+	struct panel *panel;
+
+	panel = data;
+
+	wl_list_for_each(item, &panel->window_list, link)
+		if (item->focused) {
+			list_item_menu_handle_button(item, index);
+			return;
+		}
+}
+
+#define NUM_ENTRIES 2
+
+static void
+list_item_show_menu(struct list_item *item, struct input *input, uint32_t time)
+{
+	struct panel *panel;
+	int32_t x, y;
+	static const char *entries[NUM_ENTRIES];
+
+	entries[0] = item->surface->minimized ? "Unminimize" : "Minimize";
+	entries[1] = "Close";
+
+	panel = item->panel;
+	input_get_position(input, &x, &y);
+	window_show_menu(window_get_display(panel->window), input,
+				time, panel->window, x - 10, y - 10,
+				list_item_menu_func, entries, NUM_ENTRIES);
+}
+
+static void
 panel_list_item_button_handler(struct widget *widget,
 			      struct input *input, uint32_t time,
 			      uint32_t button,
 			      enum wl_pointer_button_state state, void *data)
 {
+	struct list_item *item;
+	struct surface *surface;
+
+	item = data;
+
 	widget_schedule_redraw(widget);
-	/* TODO: Toggle minimize */
+
+	if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
+		widget_destroy_tooltip(item->widget);
+		widget_schedule_redraw(item->widget);
+		list_item_show_menu(item, input, time);
+		return;
+	}
+
+	if ((button != BTN_LEFT) || (state != WL_POINTER_BUTTON_STATE_RELEASED))
+		return;
+
+	surface = item->surface;
+	if (!surface->focused && !surface->minimized) {
+		surface_data_focus(surface->surface_data);
+		surface->focused = true;
+		return;
+	}
+	if (surface->minimized) {
+		surface_data_unminimize(surface->surface_data);
+		surface->minimized = false;
+	}
+	else {
+		surface_data_minimize(surface->surface_data);
+		surface->minimized = true;
+	}
 }
 
 static struct list_item *
@@ -1286,9 +1387,12 @@ desktop_create_surface(struct desktop *desktop,
 		exit(EXIT_FAILURE);
 	}
 
+	surface->desktop = desktop;
 	surface->surface_data = surface_data;
 	surface->title = strdup("unknown");
 	surface->output_mask = 1;
+	surface->minimized = false;
+	surface->focused = false;
 	wl_list_init(&surface->item_list);
 	wl_list_insert(&desktop->surfaces, &surface->link);
 
@@ -1376,6 +1480,55 @@ surface_data_set_title(void *data,
 }
 
 static void
+surface_data_set_minimized_state(void *data,
+				struct surface_data *surface_data,
+				int minimized)
+{
+	struct desktop *desktop;
+	struct surface *surface;
+
+	desktop = data;
+
+	surface = desktop_get_surface(desktop, surface_data);
+
+	if (!surface)
+		surface = desktop_create_surface(desktop, surface_data);
+
+	surface->minimized = minimized;
+
+	desktop_update_list_items(desktop, surface);
+}
+
+static void
+surface_data_set_focused_state(void *data,
+				struct surface_data *surface_data,
+				int focused)
+{
+	struct desktop *desktop;
+	struct surface *surface;
+	struct list_item *item;
+
+	desktop = data;
+
+	wl_list_for_each(surface, &desktop->surfaces, link)
+		if (surface->surface_data != surface_data && focused) {
+			surface->focused = false;
+			wl_list_for_each(item, &surface->item_list, surface_link)
+				if (!item->focused)
+					item->highlight = false;
+		}
+
+	surface = desktop_get_surface(desktop, surface_data);
+
+	if (!surface)
+		surface = desktop_create_surface(desktop, surface_data);
+
+	surface->focused = focused;
+
+	desktop_update_list_items(desktop, surface);
+}
+
+static void
 surface_data_destroy_handler(void *data, struct surface_data *surface_data)
 {
 	struct list_item *item, *next;
@@ -1401,6 +1554,8 @@ surface_data_destroy_handler(void *data, struct surface_data *surface_data)
 static const struct surface_data_listener surface_data_listener = {
 	surface_data_set_output_mask,
 	surface_data_set_title,
+	surface_data_set_minimized_state,
+	surface_data_set_focused_state,
 	surface_data_destroy_handler
 };
 
diff --git a/protocol/desktop-shell.xml b/protocol/desktop-shell.xml
index 16060a5..254825e 100644
--- a/protocol/desktop-shell.xml
+++ b/protocol/desktop-shell.xml
@@ -87,6 +87,18 @@
 	The shell can use this interface to receive surface information or make
 	requests for this surface.
     </description>
+    <request name="minimize">
+      <description summary="ask the compositor to minimize the surface"/>
+    </request>
+    <request name="unminimize">
+      <description summary="ask the compositor to unminimize the surface"/>
+    </request>
+    <request name="focus">
+      <description summary="ask the compositor to focus the surface"/>
+    </request>
+    <request name="close">
+      <description summary="ask the compositor to close the surface"/>
+    </request>
     <request name="destroy" type="destructor">
       <description summary="destroy surface request">
 	The shell must send this request in response to a gone event so	the
@@ -101,6 +113,14 @@
       <description summary="send the surface object title to the shell"/>
       <arg name="title" type="string"/>
     </event>
+    <event name="minimized">
+      <description summary="send the surface object minimize state to the shell"/>
+      <arg name="minimized" type="int"/>
+    </event>
+    <event name="focused">
+      <description summary="send the surface object focus state to the shell"/>
+      <arg name="focused" type="int"/>
+    </event>
     <event name="gone">
       <description summary="destroy surface notification">
 	The compositor should send this event to notify the shell that a
diff --git a/src/compositor.c b/src/compositor.c
index 686a11b..a91e5fd 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1059,6 +1059,7 @@ WL_EXPORT void
 weston_layer_init(struct weston_layer *layer, struct wl_list *below)
 {
 	wl_list_init(&layer->surface_list);
+	wl_list_init(&layer->minimized_list);
 	if (below != NULL)
 		wl_list_insert(below, &layer->link);
 }
diff --git a/src/compositor.h b/src/compositor.h
index a60563a..fe42969 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -252,6 +252,7 @@ enum {
 
 struct weston_layer {
 	struct wl_list surface_list;
+	struct wl_list minimized_list;
 	struct wl_list link;
 };
 
diff --git a/src/shell.c b/src/shell.c
index 86f2f6f..40b77e1 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -145,6 +145,7 @@ enum shell_surface_type {
 	SHELL_SURFACE_TRANSIENT,
 	SHELL_SURFACE_FULLSCREEN,
 	SHELL_SURFACE_MAXIMIZED,
+	SHELL_SURFACE_MINIMIZED,
 	SHELL_SURFACE_POPUP
 };
 
@@ -161,7 +162,7 @@ struct shell_surface {
 	struct weston_surface *parent;
 	struct desktop_shell *shell;
 
-	enum shell_surface_type type, next_type;
+	enum shell_surface_type type, next_type, saved_type;
 	char *title, *class;
 	int32_t saved_x, saved_y;
 	bool saved_position_valid;
@@ -1399,30 +1400,185 @@ shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
 }
 
 static void
-surface_data_object_destroy(struct wl_resource *resource)
+surface_data_destroy_handler(struct wl_client *client,
+				struct wl_resource *resource)
+{
+	wl_resource_destroy(resource);
+}
+
+static void
+send_surface_data_focused_state(struct weston_surface *surface);
+
+static void
+activate(struct desktop_shell *shell, struct weston_surface *es,
+	 struct weston_seat *seat);
+
+static void
+shell_surface_focus(struct shell_surface *shsurf)
+{
+	struct desktop_shell *shell;
+	struct weston_compositor *compositor;
+	struct weston_surface *surface;
+	struct weston_seat *seat;
+
+	shell = shsurf->shell;
+	compositor = shell->compositor;
+	surface = shsurf->surface;
+
+	wl_list_for_each(seat, &surface->compositor->seat_list, link)
+		if (seat->seat.keyboard) {
+			wl_keyboard_set_focus(seat->seat.keyboard,
+							&surface->surface);
+			activate(shell, surface, seat);
+		}
+
+	weston_compositor_damage_all(compositor);
+}
+
+static void
+shell_surface_minimize(struct shell_surface *shsurf)
+{
+	struct desktop_shell *shell;
+	struct weston_compositor *compositor;
+	struct weston_surface *surface;
+	struct workspace *ws;
+	struct weston_seat *seat;
+	struct weston_surface *focus;
+
+	shell = shsurf->shell;
+	compositor = shell->compositor;
+	surface = shsurf->surface;
+	ws = get_current_workspace(shell);
+
+	wl_list_remove(&surface->layer_link);
+	wl_list_insert(ws->layer.minimized_list.prev, &surface->layer_link);
+	shsurf->saved_type = shsurf->type;
+	shsurf->type = SHELL_SURFACE_MINIMIZED;
+
+	/* Focus next surface in stack */
+	wl_list_for_each(seat, &compositor->seat_list, link)
+		if (seat->seat.keyboard &&
+		    seat->keyboard.focus == &surface->surface) {
+			if (!wl_list_empty(&ws->layer.surface_list)) {
+				focus = container_of(ws->layer.surface_list.next,
+							 struct weston_surface,
+							 layer_link);
+				shsurf = get_shell_surface(focus);
+				if (!shsurf)
+					break;
+				shell_surface_focus(shsurf);
+			} else
+				wl_keyboard_set_focus(seat->seat.keyboard, NULL);
+		}
+
+	send_surface_data_focused_state(surface);
+	weston_compositor_damage_all(compositor);
+}
+
+static void
+shell_surface_unminimize(struct shell_surface *shsurf)
+{
+	struct desktop_shell *shell;
+	struct workspace *ws;
+	struct weston_compositor *compositor;
+	struct weston_surface *surface;
+
+	shell = shsurf->shell;
+	ws = get_current_workspace(shell);
+	compositor = shell->compositor;
+	surface = shsurf->surface;
+
+	if (shsurf->type == SHELL_SURFACE_MINIMIZED) {
+		wl_list_remove(&surface->layer_link);
+		wl_list_insert(ws->layer.surface_list.prev, &surface->layer_link);
+		shsurf->type = shsurf->saved_type;
+		shell_surface_focus(shsurf);
+		send_surface_data_focused_state(surface);
+		weston_compositor_damage_all(compositor);
+	}
+}
+
+static void
+surface_data_minimize_handler(struct wl_client *client,
+				struct wl_resource *resource)
 {
 	struct shell_surface *shsurf = resource->data;
 
-	free(resource);
+	shell_surface_minimize(shsurf);
+}
 
-	if (!shsurf)
-		return;
+static void
+surface_data_unminimize_handler(struct wl_client *client,
+				struct wl_resource *resource)
+{
+	struct shell_surface *shsurf = resource->data;
 
-	shsurf->surface_data = NULL;
+	shell_surface_unminimize(shsurf);
 }
 
 static void
-surface_data_destroy_handler(struct wl_client *client,
-					struct wl_resource *resource)
+surface_data_focus_handler(struct wl_client *client,
+				struct wl_resource *resource)
 {
-	wl_resource_destroy(resource);
+	struct shell_surface *shsurf = resource->data;
+
+	shell_surface_focus(shsurf);
+}
+
+static void
+surface_data_close_handler(struct wl_client *client,
+				struct wl_resource *resource)
+{
+	struct shell_surface *shsurf;
+	struct wl_surface *target_surface;
+	struct wl_client *target_client;
+	struct desktop_shell *shell;
+	struct weston_compositor *compositor;
+	pid_t pid;
+
+	shsurf = resource->data;
+	target_surface = &shsurf->surface->surface;
+	shell = shsurf->shell;
+	compositor = shell->compositor;
+
+	if (!target_surface)
+		return;
+
+	wl_signal_emit(&compositor->kill_signal, target_surface);
+
+	target_client = target_surface->resource.client;
+	wl_client_get_credentials(target_client, &pid, NULL, NULL);
+
+	/* Skip clients that we launched ourselves (the credentials of
+	 * the socketpair is ours) */
+	if (pid == getpid())
+		return;
+
+	kill(pid, SIGTERM);
 }
 
 static const struct surface_data_interface
 					surface_data_implementation = {
+	surface_data_minimize_handler,
+	surface_data_unminimize_handler,
+	surface_data_focus_handler,
+	surface_data_close_handler,
 	surface_data_destroy_handler
 };
 
+static void
+surface_data_object_destroy(struct wl_resource *resource)
+{
+	struct shell_surface *shsurf = resource->data;
+
+	free(resource);
+
+	if (!shsurf)
+		return;
+
+	shsurf->surface_data = NULL;
+}
+
 static int
 create_surface_data(struct desktop_shell *shell, struct shell_surface *shsurf)
 {
@@ -1478,6 +1634,7 @@ surface_is_window_list_candidate(struct weston_surface *surface,
 		return false;
 	case SHELL_SURFACE_FULLSCREEN:
 	case SHELL_SURFACE_MAXIMIZED:
+	case SHELL_SURFACE_MINIMIZED:
 	case SHELL_SURFACE_TOPLEVEL:
 		if (!shsurf->surface_data) {
 			if (create_surface_data(shell, shsurf))
@@ -1511,6 +1668,35 @@ send_surface_data_title(struct weston_surface *surface)
 }
 
 static void
+send_surface_data_minimized_state(struct weston_surface *surface)
+{
+	struct shell_surface shsurf;
+
+	if (surface_is_window_list_candidate(surface, &shsurf))
+		surface_data_send_minimized(shsurf.surface_data,
+					shsurf.type == SHELL_SURFACE_MINIMIZED ?
+					true : false);
+}
+
+static void
+send_surface_data_focused_state(struct weston_surface *surface)
+{
+	struct shell_surface shsurf;
+	struct focus_state *state;
+	struct workspace *ws;
+	bool focused = false;
+
+	if (surface_is_window_list_candidate(surface, &shsurf)) {
+		ws = get_current_workspace(shsurf.shell);
+		wl_list_for_each(state, &ws->focus_list, link)
+			if (state->keyboard_focus == shsurf.surface)
+				focused = true;
+		surface_data_send_focused(shsurf.surface_data,
+					focused);
+	}
+}
+
+static void
 shell_surface_set_title(struct wl_client *client,
 			struct wl_resource *resource, const char *title)
 {
@@ -1585,6 +1771,7 @@ reset_shell_surface_type(struct shell_surface *surface)
 	case SHELL_SURFACE_NONE:
 	case SHELL_SURFACE_TOPLEVEL:
 	case SHELL_SURFACE_TRANSIENT:
+	case SHELL_SURFACE_MINIMIZED:
 	case SHELL_SURFACE_POPUP:
 		break;
 	}
@@ -2443,6 +2630,14 @@ surface_data_send_all_info(struct desktop_shell *shell)
 	ws = get_current_workspace(shell);
 
 	wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
+		send_surface_data_minimized_state(surface);
+		send_surface_data_focused_state(surface);
+		send_surface_data_output_mask(surface);
+		send_surface_data_title(surface);
+	}
+	wl_list_for_each(surface, &ws->layer.minimized_list, layer_link) {
+		send_surface_data_minimized_state(surface);
+		send_surface_data_focused_state(surface);
 		send_surface_data_output_mask(surface);
 		send_surface_data_title(surface);
 	}
@@ -2783,6 +2978,7 @@ activate(struct desktop_shell *shell, struct weston_surface *es,
 		return;
 
 	state->keyboard_focus = es;
+	send_surface_data_focused_state(es);
 	wl_list_remove(&state->surface_destroy_listener.link);
 	wl_signal_add(&es->surface.resource.destroy_signal,
 		      &state->surface_destroy_listener);
@@ -2807,7 +3003,7 @@ black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
 {
 }
 
-static bool 
+static bool
 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
 {
 	if (es->configure == black_surface_configure) {
-- 
1.7.11.7



More information about the wayland-devel mailing list