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

Scott Moreau oreaus at gmail.com
Sat Nov 3 22:31:33 PDT 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                | 221 ++++++++++++++++++++++++++++++++++++++++++---
 5 files changed, 396 insertions(+), 22 deletions(-)

diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 3a52b06..580fbc1 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
@@ -1081,7 +1085,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->highlight || item->surface->focused) {
 		cairo_set_source_rgba(cr,
 					item->panel->focused_item.r,
 					item->panel->focused_item.g,
@@ -1134,7 +1138,7 @@ panel_list_item_redraw_handler(struct widget *widget, void *data)
 	cairo_show_text(cr, title);
 	cairo_move_to(cr, rect.x + 9 + icon_width,
 		      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);
@@ -1159,7 +1163,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;
@@ -1171,19 +1176,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 *
@@ -1279,9 +1380,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);
 
@@ -1369,6 +1473,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;
@@ -1394,6 +1547,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 85a9de5..403f910 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1058,6 +1058,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 e3560a0..31f1949 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -266,6 +266,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 7303390..fce4925 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -146,6 +146,7 @@ enum shell_surface_type {
 	SHELL_SURFACE_TRANSIENT,
 	SHELL_SURFACE_FULLSCREEN,
 	SHELL_SURFACE_MAXIMIZED,
+	SHELL_SURFACE_MINIMIZED,
 	SHELL_SURFACE_POPUP
 };
 
@@ -167,7 +168,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;
@@ -1405,33 +1406,189 @@ 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 */
+	if (!wl_list_empty(&ws->layer.surface_list)) {
+		focus = container_of(ws->layer.surface_list.next,
+					 struct weston_surface,
+					 layer_link);
+		wl_list_for_each(seat, &compositor->seat_list, link)
+			if (seat->seat.keyboard &&
+			    seat->keyboard.focus == &surface->surface) {
+				shsurf = get_shell_surface(focus);
+				if (!shsurf)
+					break;
+				shell_surface_focus(shsurf);
+			}
+	}
+
+	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;
 	struct surface_data *surface_data = resource->data;
 
-	shsurf = surface_data->shsurf;
+	shell_surface_minimize(surface_data->shsurf);
+}
 
-	free(surface_data);
+static void
+surface_data_unminimize_handler(struct wl_client *client,
+				struct wl_resource *resource)
+{
+	struct surface_data *surface_data = resource->data;
 
-	if (!shsurf)
-		return;
+	shell_surface_unminimize(surface_data->shsurf);
+}
 
-	shsurf->surface_data = NULL;
+static void
+surface_data_focus_handler(struct wl_client *client,
+				struct wl_resource *resource)
+{
+	struct surface_data *surface_data = resource->data;
+
+	shell_surface_focus(surface_data->shsurf);
 }
 
 static void
-surface_data_destroy_handler(struct wl_client *client,
-					struct wl_resource *resource)
+surface_data_close_handler(struct wl_client *client,
+				struct wl_resource *resource)
 {
-	wl_resource_destroy(resource);
+	struct surface_data *surface_data;
+	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;
+
+	surface_data = resource->data;
+	shsurf = surface_data->shsurf;
+	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;
+	struct surface_data *surface_data = resource->data;
+
+	shsurf = surface_data->shsurf;
+
+	free(surface_data);
+
+	if (!shsurf)
+		return;
+
+	shsurf->surface_data = NULL;
+}
+
 static int
 create_surface_data(struct desktop_shell *shell, struct shell_surface *shsurf)
 {
@@ -1489,6 +1646,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))
@@ -1522,6 +1680,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->resource,
+					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->resource,
+					focused);
+	}
+}
+
+static void
 shell_surface_set_title(struct wl_client *client,
 			struct wl_resource *resource, const char *title)
 {
@@ -1596,6 +1783,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;
 	}
@@ -2456,6 +2644,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);
 	}
@@ -2796,6 +2992,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);
@@ -2820,7 +3017,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