blob: 92b4bee59613fb6aff9b270d8443b33f3a2d649d [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020044#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020047#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010049#ifndef static_assert
50#define static_assert(cond, msg)
51#endif
52
Jonas Ådahl04769742012-06-13 00:01:24 +020053struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020054 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040056 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020057 struct weston_surface *keyboard_focus;
58 struct wl_list link;
59 struct wl_listener seat_destroy_listener;
60 struct wl_listener surface_destroy_listener;
61};
62
Philip Withnall648a4dd2013-11-25 18:01:44 +000063/*
64 * Surface stacking and ordering.
65 *
66 * This is handled using several linked lists of surfaces, organised into
67 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
68 * above all of the surfaces in the layer below. The set of layers is static and
69 * in the following order (top-most first):
70 * • Lock layer (only ever displayed on its own)
71 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010072 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Fullscreen layer
74 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000075 * • Workspace layers
76 * • Background layer
77 *
78 * The list of layers may be manipulated to remove whole layers of surfaces from
79 * display. For example, when locking the screen, all layers except the lock
80 * layer are removed.
81 *
82 * A surface’s layer is modified on configuring the surface, in
83 * set_surface_type() (which is only called when the surface’s type change is
84 * _committed_). If a surface’s type changes (e.g. when making a window
85 * fullscreen) its layer changes too.
86 *
87 * In order to allow popup and transient surfaces to be correctly stacked above
88 * their parent surfaces, each surface tracks both its parent surface, and a
89 * linked list of its children. When a surface’s layer is updated, so are the
90 * layers of its children. Note that child surfaces are *not* the same as
91 * subsurfaces — child/parent surfaces are purely for maintaining stacking
92 * order.
93 *
94 * The children_link list of siblings of a surface (i.e. those surfaces which
95 * have the same parent) only contains weston_surfaces which have a
96 * shell_surface. Stacking is not implemented for non-shell_surface
97 * weston_surfaces. This means that the following implication does *not* hold:
98 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
99 */
100
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200101struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500102 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200103
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200104 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600106 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700107
Tiago Vignattibe143262012-04-16 17:31:41 +0300108 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800111 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800112 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700113 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800114 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800122 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800124 } fullscreen;
125
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 struct weston_transform workspace_transform;
127
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500128 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200130
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700131 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200132 bool fullscreen;
133 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100134 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200135 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700136
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500137 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500138
139 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200140};
141
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300142struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400143 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300144 struct shell_surface *shsurf;
145 struct wl_listener shsurf_destroy_listener;
146};
147
Rusty Lynch1084da52013-08-15 09:10:08 -0700148struct shell_touch_grab {
149 struct weston_touch_grab grab;
150 struct shell_surface *shsurf;
151 struct wl_listener shsurf_destroy_listener;
152 struct weston_touch *touch;
153};
154
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300155struct weston_move_grab {
156 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100157 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500158 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159};
160
Rusty Lynch1084da52013-08-15 09:10:08 -0700161struct weston_touch_move_grab {
162 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800163 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700164 wl_fixed_t dx, dy;
165};
166
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300168 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500169 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200170 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200171 float x;
172 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173 } center;
174};
175
Giulio Camuffo5085a752013-03-25 21:42:45 +0100176struct shell_seat {
177 struct weston_seat *seat;
178 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100180
Jason Ekstrand024177c2014-04-21 19:42:58 -0500181 struct wl_listener caps_changed_listener;
182 struct wl_listener pointer_focus_listener;
183 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184};
185
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800186
Alex Wubd3354b2012-04-17 17:20:49 +0800187static struct desktop_shell *
188shell_surface_get_shell(struct shell_surface *shsurf);
189
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500190static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200191set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
192
193static void
Derek Foreman74de4692015-07-15 13:00:39 -0500194surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500195
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300196static void
197shell_fade_startup(struct desktop_shell *shell);
198
Philip Withnallbecb77e2013-11-25 18:01:30 +0000199static struct shell_seat *
200get_shell_seat(struct weston_seat *seat);
201
Jonny Lambd73c6942014-08-20 15:53:20 +0200202static void
203get_output_panel_size(struct desktop_shell *shell,
204 struct weston_output *output,
205 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700206
Philip Withnall648a4dd2013-11-25 18:01:44 +0000207static void
208shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
209
Pekka Paalanen8274d902014-08-06 19:36:51 +0300210static int
211shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
212{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300213 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200214 struct weston_desktop_surface *desktop_surface =
215 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300216
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200217 t = weston_desktop_surface_get_title(desktop_surface);
218 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219
220 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200221 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222 t ? " '" : "", t ?: "", t ? "'" : "",
223 c ? " of " : "", c ?: "");
224}
225
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500226static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400227destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228{
229 struct shell_grab *grab;
230
231 grab = container_of(listener, struct shell_grab,
232 shsurf_destroy_listener);
233
234 grab->shsurf = NULL;
235}
236
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800237struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500238get_default_view(struct weston_surface *surface)
239{
240 struct shell_surface *shsurf;
241 struct weston_view *view;
242
243 if (!surface || wl_list_empty(&surface->views))
244 return NULL;
245
246 shsurf = get_shell_surface(surface);
247 if (shsurf)
248 return shsurf->view;
249
250 wl_list_for_each(view, &surface->views, surface_link)
251 if (weston_view_is_mapped(view))
252 return view;
253
254 return container_of(surface->views.next, struct weston_view, surface_link);
255}
256
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300257static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300258shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400259 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300260 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400261 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800262 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264 struct desktop_shell *shell = shsurf->shell;
265
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200266 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400267
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268 grab->grab.interface = interface;
269 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400270 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500271 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400272 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700274 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400275 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400276 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800277 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400278 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 weston_pointer_set_focus(pointer,
280 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400281 wl_fixed_from_int(0),
282 wl_fixed_from_int(0));
283 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284}
285
Jonny Lambd73c6942014-08-20 15:53:20 +0200286static void
Quentin Glidic581df062016-06-23 18:55:19 +0200287get_panel_size(struct desktop_shell *shell,
288 struct weston_view *view,
289 int *width,
290 int *height)
291{
292 float x1, y1;
293 float x2, y2;
294 weston_view_to_global_float(view, 0, 0, &x1, &y1);
295 weston_view_to_global_float(view,
296 view->surface->width,
297 view->surface->height,
298 &x2, &y2);
299 *width = (int)(x2 - x1);
300 *height = (int)(y2 - y1);
301}
302
303static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200304get_output_panel_size(struct desktop_shell *shell,
305 struct weston_output *output,
306 int *width,
307 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700308{
309 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200310
311 *width = 0;
312 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700313
314 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200315 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700316
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300317 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200318 if (view->surface->output == output) {
319 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200320 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700321 }
322 }
323
Jonny Lambd73c6942014-08-20 15:53:20 +0200324 /* the correct view wasn't found */
325}
326
327static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200328get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200329 struct weston_output *output,
330 pixman_rectangle32_t *area)
331{
332 int32_t panel_width = 0, panel_height = 0;
333
Derek Foremanf814c5d2015-04-15 12:23:54 -0500334 area->x = output->x;
335 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200336
337 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200338 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800339 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200340 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500341 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800342 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200343 area->width = output->width;
344 area->height = output->height - panel_height;
345 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800346 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500347 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800348 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 area->width = output->width - panel_width;
350 area->height = output->height;
351 break;
352 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700353}
354
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400355static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300356shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300357{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700358 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400359 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700360 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400361
362 if (grab->shsurf->resize_edges) {
363 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400364 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700365 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700367 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368}
369
370static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700371shell_touch_grab_start(struct shell_touch_grab *grab,
372 const struct weston_touch_grab_interface *interface,
373 struct shell_surface *shsurf,
374 struct weston_touch *touch)
375{
376 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800377
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200378 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700379
Rusty Lynch1084da52013-08-15 09:10:08 -0700380 grab->grab.interface = interface;
381 grab->shsurf = shsurf;
382 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
383 wl_signal_add(&shsurf->destroy_signal,
384 &grab->shsurf_destroy_listener);
385
386 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700387 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700388
389 weston_touch_start_grab(touch, &grab->grab);
390 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500391 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700393}
394
395static void
396shell_touch_grab_end(struct shell_touch_grab *grab)
397{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700398 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700399 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700400 grab->shsurf->grabbed = 0;
401 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700402
403 weston_touch_end_grab(grab->touch);
404}
405
406static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500407center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800408 struct weston_output *output);
409
Daniel Stone496ca172012-05-30 16:31:42 +0100410static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300411get_modifier(char *modifier)
412{
413 if (!modifier)
414 return MODIFIER_SUPER;
415
416 if (!strcmp("ctrl", modifier))
417 return MODIFIER_CTRL;
418 else if (!strcmp("alt", modifier))
419 return MODIFIER_ALT;
420 else if (!strcmp("super", modifier))
421 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000422 else if (!strcmp("none", modifier))
423 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300424 else
425 return MODIFIER_SUPER;
426}
427
Juan Zhaoe10d2792012-04-25 19:09:52 +0800428static enum animation_type
429get_animation_type(char *animation)
430{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800431 if (!animation)
432 return ANIMATION_NONE;
433
Juan Zhaoe10d2792012-04-25 19:09:52 +0800434 if (!strcmp("zoom", animation))
435 return ANIMATION_ZOOM;
436 else if (!strcmp("fade", animation))
437 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100438 else if (!strcmp("dim-layer", animation))
439 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800440 else
441 return ANIMATION_NONE;
442}
443
Alex Wu4539b082012-03-01 12:57:46 +0800444static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400445shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200446{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400447 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500448 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300449 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000450 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200451
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300452 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300454 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
455 WESTON_SHELL_CLIENT);
456 if (ret < 0)
457 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400458 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500459 "client", &s, client);
460 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100461 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000462
463 weston_config_section_get_bool(section,
464 "allow-zap", &allow_zap, true);
465 shell->allow_zap = allow_zap;
466
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100467 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400468 "binding-modifier", &s, "super");
469 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200470 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800471
472 weston_config_section_get_string(section,
473 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000474 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800475 free(s);
476
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400477 weston_config_section_get_string(section, "animation", &s, "none");
478 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200479 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200480 weston_config_section_get_string(section, "close-animation", &s, "fade");
481 shell->win_close_animation_type = get_animation_type(s);
482 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700483 weston_config_section_get_string(section,
484 "startup-animation", &s, "fade");
485 shell->startup_animation_type = get_animation_type(s);
486 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700487 if (shell->startup_animation_type == ANIMATION_ZOOM)
488 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100489 weston_config_section_get_string(section, "focus-animation", &s, "none");
490 shell->focus_animation_type = get_animation_type(s);
491 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400492 weston_config_section_get_uint(section, "num-workspaces",
493 &shell->workspaces.num,
494 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200495}
496
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800497struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100498get_default_output(struct weston_compositor *compositor)
499{
Armin Krezović10b06182016-06-23 11:59:30 +0200500 if (wl_list_empty(&compositor->output_list))
501 return NULL;
502
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100503 return container_of(compositor->output_list.next,
504 struct weston_output, link);
505}
506
Pekka Paalanen8274d902014-08-06 19:36:51 +0300507static int
508focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
509{
510 return snprintf(buf, len, "focus highlight effect for output %s",
511 surface->output->name);
512}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100513
514/* no-op func for checking focus surface */
515static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200516focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100517{
518}
519
520static struct focus_surface *
521get_focus_surface(struct weston_surface *surface)
522{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200523 if (surface->committed == focus_surface_committed)
524 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525 else
526 return NULL;
527}
528
529static bool
530is_focus_surface (struct weston_surface *es)
531{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200532 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100533}
534
535static bool
536is_focus_view (struct weston_view *view)
537{
538 return is_focus_surface (view->surface);
539}
540
541static struct focus_surface *
542create_focus_surface(struct weston_compositor *ec,
543 struct weston_output *output)
544{
545 struct focus_surface *fsurf = NULL;
546 struct weston_surface *surface = NULL;
547
548 fsurf = malloc(sizeof *fsurf);
549 if (!fsurf)
550 return NULL;
551
552 fsurf->surface = weston_surface_create(ec);
553 surface = fsurf->surface;
554 if (surface == NULL) {
555 free(fsurf);
556 return NULL;
557 }
558
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200559 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100560 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200561 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200562 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300563 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100564
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800565 fsurf->view = weston_view_create(surface);
566 if (fsurf->view == NULL) {
567 weston_surface_destroy(surface);
568 free(fsurf);
569 return NULL;
570 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100571 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200572 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100573
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600574 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600575 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100576 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
577 pixman_region32_fini(&surface->opaque);
578 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
579 output->width, output->height);
580 pixman_region32_fini(&surface->input);
581 pixman_region32_init(&surface->input);
582
583 wl_list_init(&fsurf->workspace_transform.link);
584
585 return fsurf;
586}
587
588static void
589focus_surface_destroy(struct focus_surface *fsurf)
590{
591 weston_surface_destroy(fsurf->surface);
592 free(fsurf);
593}
594
595static void
596focus_animation_done(struct weston_view_animation *animation, void *data)
597{
598 struct workspace *ws = data;
599
600 ws->focus_animation = NULL;
601}
602
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200603static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200604focus_state_destroy(struct focus_state *state)
605{
606 wl_list_remove(&state->seat_destroy_listener.link);
607 wl_list_remove(&state->surface_destroy_listener.link);
608 free(state);
609}
610
611static void
612focus_state_seat_destroy(struct wl_listener *listener, void *data)
613{
614 struct focus_state *state = container_of(listener,
615 struct focus_state,
616 seat_destroy_listener);
617
618 wl_list_remove(&state->link);
619 focus_state_destroy(state);
620}
621
622static void
623focus_state_surface_destroy(struct wl_listener *listener, void *data)
624{
625 struct focus_state *state = container_of(listener,
626 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400627 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200628 struct weston_surface *main_surface;
629 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200631
Pekka Paalanen01388e22013-04-25 13:57:44 +0300632 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
633
Kristian Høgsberge3778222012-07-31 17:29:30 -0400634 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300635 wl_list_for_each(view,
636 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500637 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400638 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100639 if (is_focus_view(view))
640 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200641 if (!get_shell_surface(view->surface))
642 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200644 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 break;
646 }
647
Pekka Paalanen01388e22013-04-25 13:57:44 +0300648 /* if the focus was a sub-surface, activate its main surface */
649 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200650 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300651
Kristian Høgsberge3778222012-07-31 17:29:30 -0400652 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100653 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200654 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200655 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200657 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100658 if (state->ws->focus_animation)
659 weston_view_animation_destroy(state->ws->focus_animation);
660
661 state->ws->focus_animation = weston_fade_run(
662 state->ws->fsurf_front->view,
663 state->ws->fsurf_front->view->alpha, 0.0, 300,
664 focus_animation_done, state->ws);
665 }
666
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 wl_list_remove(&state->link);
668 focus_state_destroy(state);
669 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200670}
671
672static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200673focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
674 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200675{
Jonas Ådahl04769742012-06-13 00:01:24 +0200676 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200677
678 state = malloc(sizeof *state);
679 if (state == NULL)
680 return NULL;
681
Quentin Glidicfff39812016-08-15 10:56:52 +0200682 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400684 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200685 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400686 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200687
688 state->seat_destroy_listener.notify = focus_state_seat_destroy;
689 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400690 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200691 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400692 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200693
694 return state;
695}
696
Jonas Ådahl8538b222012-08-29 22:13:03 +0200697static struct focus_state *
698ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
699{
700 struct workspace *ws = get_current_workspace(shell);
701 struct focus_state *state;
702
703 wl_list_for_each(state, &ws->focus_list, link)
704 if (state->seat == seat)
705 break;
706
707 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200708 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200709
710 return state;
711}
712
Jonas Ådahl04769742012-06-13 00:01:24 +0200713static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800714focus_state_set_focus(struct focus_state *state,
715 struct weston_surface *surface)
716{
717 if (state->keyboard_focus) {
718 wl_list_remove(&state->surface_destroy_listener.link);
719 wl_list_init(&state->surface_destroy_listener.link);
720 }
721
722 state->keyboard_focus = surface;
723 if (surface)
724 wl_signal_add(&surface->destroy_signal,
725 &state->surface_destroy_listener);
726}
727
728static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400729restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200730{
731 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400732 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100733 struct wl_list pending_seat_list;
734 struct weston_seat *seat, *next_seat;
735
736 /* Temporarily steal the list of seats so that we can keep
737 * track of the seats we've already processed */
738 wl_list_init(&pending_seat_list);
739 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
740 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200741
742 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500743 struct weston_keyboard *keyboard =
744 weston_seat_get_keyboard(state->seat);
745
Neil Roberts4237f502014-04-09 16:33:31 +0100746 wl_list_remove(&state->seat->link);
747 wl_list_insert(&shell->compositor->seat_list,
748 &state->seat->link);
749
Derek Foreman1281a362015-07-31 16:55:32 -0500750 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800751 continue;
752
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400753 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200754
Derek Foreman1281a362015-07-31 16:55:32 -0500755 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200756 }
Neil Roberts4237f502014-04-09 16:33:31 +0100757
758 /* For any remaining seats that we don't have a focus state
759 * for we'll reset the keyboard focus to NULL */
760 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500761 struct weston_keyboard *keyboard =
762 weston_seat_get_keyboard(seat);
763
Neil Roberts4237f502014-04-09 16:33:31 +0100764 wl_list_insert(&shell->compositor->seat_list, &seat->link);
765
Derek Foreman1281a362015-07-31 16:55:32 -0500766 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100767 continue;
768
Derek Foreman1281a362015-07-31 16:55:32 -0500769 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100770 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200771}
772
773static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200774replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
775 struct weston_seat *seat)
776{
Derek Foreman1281a362015-07-31 16:55:32 -0500777 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200778 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200779
780 wl_list_for_each(state, &ws->focus_list, link) {
781 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500782 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200783 return;
784 }
785 }
786}
787
788static void
789drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
790 struct weston_surface *surface)
791{
792 struct focus_state *state;
793
794 wl_list_for_each(state, &ws->focus_list, link)
795 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800796 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200797}
798
799static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100800animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
801 struct weston_view *from, struct weston_view *to)
802{
803 struct weston_output *output;
804 bool focus_surface_created = false;
805
806 /* FIXME: Only support dim animation using two layers */
807 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
808 return;
809
810 output = get_default_output(shell->compositor);
811 if (ws->fsurf_front == NULL && (from || to)) {
812 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800813 if (ws->fsurf_front == NULL)
814 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100815 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800816
817 ws->fsurf_back = create_focus_surface(shell->compositor, output);
818 if (ws->fsurf_back == NULL) {
819 focus_surface_destroy(ws->fsurf_front);
820 return;
821 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100822 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800823
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100824 focus_surface_created = true;
825 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300826 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
827 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 }
829
830 if (ws->focus_animation) {
831 weston_view_animation_destroy(ws->focus_animation);
832 ws->focus_animation = NULL;
833 }
834
835 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300836 weston_layer_entry_insert(&to->layer_link,
837 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100838 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300839 weston_layer_entry_insert(&ws->layer.view_list,
840 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841
842 if (focus_surface_created) {
843 ws->focus_animation = weston_fade_run(
844 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200845 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 focus_animation_done, ws);
847 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_insert(&from->layer_link,
849 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 ws->focus_animation = weston_stable_fade_run(
851 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200852 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 focus_animation_done, ws);
854 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300855 weston_layer_entry_insert(&ws->layer.view_list,
856 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 ws->focus_animation = weston_stable_fade_run(
858 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200859 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 focus_animation_done, ws);
861 }
862}
863
864static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200865workspace_destroy(struct workspace *ws)
866{
Jonas Ådahl04769742012-06-13 00:01:24 +0200867 struct focus_state *state, *next;
868
869 wl_list_for_each_safe(state, next, &ws->focus_list, link)
870 focus_state_destroy(state);
871
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 if (ws->fsurf_front)
873 focus_surface_destroy(ws->fsurf_front);
874 if (ws->fsurf_back)
875 focus_surface_destroy(ws->fsurf_back);
876
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877 free(ws);
878}
879
Jonas Ådahl04769742012-06-13 00:01:24 +0200880static void
881seat_destroyed(struct wl_listener *listener, void *data)
882{
883 struct weston_seat *seat = data;
884 struct focus_state *state, *next;
885 struct workspace *ws = container_of(listener,
886 struct workspace,
887 seat_destroyed_listener);
888
889 wl_list_for_each_safe(state, next, &ws->focus_list, link)
890 if (state->seat == seat)
891 wl_list_remove(&state->link);
892}
893
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200894static struct workspace *
895workspace_create(void)
896{
897 struct workspace *ws = malloc(sizeof *ws);
898 if (ws == NULL)
899 return NULL;
900
901 weston_layer_init(&ws->layer, NULL);
902
Jonas Ådahl04769742012-06-13 00:01:24 +0200903 wl_list_init(&ws->focus_list);
904 wl_list_init(&ws->seat_destroyed_listener.link);
905 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100906 ws->fsurf_front = NULL;
907 ws->fsurf_back = NULL;
908 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200909
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200910 return ws;
911}
912
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200913static int
914workspace_is_empty(struct workspace *ws)
915{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300916 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200917}
918
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919static struct workspace *
920get_workspace(struct desktop_shell *shell, unsigned int index)
921{
922 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200923 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200924 pws += index;
925 return *pws;
926}
927
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800928struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200929get_current_workspace(struct desktop_shell *shell)
930{
931 return get_workspace(shell, shell->workspaces.current);
932}
933
934static void
935activate_workspace(struct desktop_shell *shell, unsigned int index)
936{
937 struct workspace *ws;
938
939 ws = get_workspace(shell, index);
940 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
941
942 shell->workspaces.current = index;
943}
944
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945static unsigned int
946get_output_height(struct weston_output *output)
947{
948 return abs(output->region.extents.y1 - output->region.extents.y2);
949}
950
951static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100952view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200954 struct weston_transform *transform;
955
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100956 if (is_focus_view(view)) {
957 struct focus_surface *fsurf = get_focus_surface(view->surface);
958 transform = &fsurf->workspace_transform;
959 } else {
960 struct shell_surface *shsurf = get_shell_surface(view->surface);
961 transform = &shsurf->workspace_transform;
962 }
963
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 weston_matrix_init(&transform->matrix);
969 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972}
973
974static void
975workspace_translate_out(struct workspace *ws, double fraction)
976{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 unsigned int height;
979 double d;
980
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300981 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983 d = height * fraction;
984
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100985 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 }
987}
988
989static void
990workspace_translate_in(struct workspace *ws, double fraction)
991{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993 unsigned int height;
994 double d;
995
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300996 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
999 if (fraction > 0)
1000 d = -(height - height * fraction);
1001 else
1002 d = height + height * fraction;
1003
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001004 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 }
1006}
1007
1008static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009reverse_workspace_change_animation(struct desktop_shell *shell,
1010 unsigned int index,
1011 struct workspace *from,
1012 struct workspace *to)
1013{
1014 shell->workspaces.current = index;
1015
1016 shell->workspaces.anim_to = to;
1017 shell->workspaces.anim_from = from;
1018 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1019 shell->workspaces.anim_timestamp = 0;
1020
Scott Moreau4272e632012-08-13 09:58:41 -06001021 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022}
1023
1024static void
1025workspace_deactivate_transforms(struct workspace *ws)
1026{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001029
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001030 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001031 if (is_focus_view(view)) {
1032 struct focus_surface *fsurf = get_focus_surface(view->surface);
1033 transform = &fsurf->workspace_transform;
1034 } else {
1035 struct shell_surface *shsurf = get_shell_surface(view->surface);
1036 transform = &shsurf->workspace_transform;
1037 }
1038
1039 if (!wl_list_empty(&transform->link)) {
1040 wl_list_remove(&transform->link);
1041 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001042 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044 }
1045}
1046
1047static void
1048finish_workspace_change_animation(struct desktop_shell *shell,
1049 struct workspace *from,
1050 struct workspace *to)
1051{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001052 struct weston_view *view;
1053
Scott Moreau4272e632012-08-13 09:58:41 -06001054 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001056 /* Views that extend past the bottom of the output are still
1057 * visible after the workspace animation ends but before its layer
1058 * is hidden. In that case, we need to damage below those views so
1059 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001060 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001061 weston_view_damage_below(view);
1062
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063 wl_list_remove(&shell->workspaces.animation.link);
1064 workspace_deactivate_transforms(from);
1065 workspace_deactivate_transforms(to);
1066 shell->workspaces.anim_to = NULL;
1067
1068 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1069}
1070
1071static void
1072animate_workspace_change_frame(struct weston_animation *animation,
1073 struct weston_output *output, uint32_t msecs)
1074{
1075 struct desktop_shell *shell =
1076 container_of(animation, struct desktop_shell,
1077 workspaces.animation);
1078 struct workspace *from = shell->workspaces.anim_from;
1079 struct workspace *to = shell->workspaces.anim_to;
1080 uint32_t t;
1081 double x, y;
1082
1083 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1084 finish_workspace_change_animation(shell, from, to);
1085 return;
1086 }
1087
1088 if (shell->workspaces.anim_timestamp == 0) {
1089 if (shell->workspaces.anim_current == 0.0)
1090 shell->workspaces.anim_timestamp = msecs;
1091 else
1092 shell->workspaces.anim_timestamp =
1093 msecs -
1094 /* Invers of movement function 'y' below. */
1095 (asin(1.0 - shell->workspaces.anim_current) *
1096 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1097 M_2_PI);
1098 }
1099
1100 t = msecs - shell->workspaces.anim_timestamp;
1101
1102 /*
1103 * x = [0, π/2]
1104 * y(x) = sin(x)
1105 */
1106 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1107 y = sin(x);
1108
1109 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001110 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001111
1112 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1113 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1114 shell->workspaces.anim_current = y;
1115
Scott Moreau4272e632012-08-13 09:58:41 -06001116 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001118 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120}
1121
1122static void
1123animate_workspace_change(struct desktop_shell *shell,
1124 unsigned int index,
1125 struct workspace *from,
1126 struct workspace *to)
1127{
1128 struct weston_output *output;
1129
1130 int dir;
1131
1132 if (index > shell->workspaces.current)
1133 dir = -1;
1134 else
1135 dir = 1;
1136
1137 shell->workspaces.current = index;
1138
1139 shell->workspaces.anim_dir = dir;
1140 shell->workspaces.anim_from = from;
1141 shell->workspaces.anim_to = to;
1142 shell->workspaces.anim_current = 0.0;
1143 shell->workspaces.anim_timestamp = 0;
1144
1145 output = container_of(shell->compositor->output_list.next,
1146 struct weston_output, link);
1147 wl_list_insert(&output->animation_list,
1148 &shell->workspaces.animation.link);
1149
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001150 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151
1152 workspace_translate_in(to, 0);
1153
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001154 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001155
Scott Moreau4272e632012-08-13 09:58:41 -06001156 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157}
1158
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001159static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160update_workspace(struct desktop_shell *shell, unsigned int index,
1161 struct workspace *from, struct workspace *to)
1162{
1163 shell->workspaces.current = index;
1164 wl_list_insert(&from->layer.link, &to->layer.link);
1165 wl_list_remove(&from->layer.link);
1166}
1167
1168static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169change_workspace(struct desktop_shell *shell, unsigned int index)
1170{
1171 struct workspace *from;
1172 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001173 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174
1175 if (index == shell->workspaces.current)
1176 return;
1177
1178 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001179 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180 return;
1181
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001182 from = get_current_workspace(shell);
1183 to = get_workspace(shell, index);
1184
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185 if (shell->workspaces.anim_from == to &&
1186 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001188 reverse_workspace_change_animation(shell, index, from, to);
1189 return;
1190 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001191
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001192 if (shell->workspaces.anim_to != NULL)
1193 finish_workspace_change_animation(shell,
1194 shell->workspaces.anim_from,
1195 shell->workspaces.anim_to);
1196
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001198
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001199 if (shell->focus_animation_type != ANIMATION_NONE) {
1200 wl_list_for_each(state, &from->focus_list, link)
1201 if (state->keyboard_focus)
1202 animate_focus_change(shell, from,
1203 get_default_view(state->keyboard_focus), NULL);
1204
1205 wl_list_for_each(state, &to->focus_list, link)
1206 if (state->keyboard_focus)
1207 animate_focus_change(shell, to,
1208 NULL, get_default_view(state->keyboard_focus));
1209 }
1210
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001211 if (workspace_is_empty(to) && workspace_is_empty(from))
1212 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001213 else
1214 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001215}
1216
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001217static bool
1218workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1219{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001220 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 struct wl_list *e;
1222
1223 if (wl_list_empty(list))
1224 return false;
1225
1226 e = list->next;
1227
1228 if (e->next != list)
1229 return false;
1230
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001231 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232}
1233
1234static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001235surface_keyboard_focus_lost(struct weston_surface *surface)
1236{
1237 struct weston_compositor *compositor = surface->compositor;
1238 struct weston_seat *seat;
1239 struct weston_surface *focus;
1240
1241 wl_list_for_each(seat, &compositor->seat_list, link) {
1242 struct weston_keyboard *keyboard =
1243 weston_seat_get_keyboard(seat);
1244
1245 if (!keyboard)
1246 continue;
1247
1248 focus = weston_surface_get_main_surface(keyboard->focus);
1249 if (focus == surface)
1250 weston_keyboard_set_focus(keyboard, NULL);
1251 }
1252}
1253
1254static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001256 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 unsigned int index)
1258{
Derek Foreman1281a362015-07-31 16:55:32 -05001259 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001260 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001262 struct shell_surface *shsurf;
1263 struct workspace *from;
1264 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001265 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266
Derek Foreman1281a362015-07-31 16:55:32 -05001267 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view = get_default_view(surface);
1269 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001270 index == shell->workspaces.current ||
1271 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 return;
1273
1274 from = get_current_workspace(shell);
1275 to = get_workspace(shell, index);
1276
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001277 weston_layer_entry_remove(&view->layer_link);
1278 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279
Philip Withnall659163d2013-11-25 18:01:36 +00001280 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001281 if (shsurf != NULL)
1282 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001283
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001285 drop_focus_state(shell, from, surface);
1286
1287 if (shell->workspaces.anim_from == to &&
1288 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001289 wl_list_remove(&to->layer.link);
1290 wl_list_insert(from->layer.link.prev, &to->layer.link);
1291
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001292 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294 return;
1295 }
1296
1297 if (shell->workspaces.anim_to != NULL)
1298 finish_workspace_change_animation(shell,
1299 shell->workspaces.anim_from,
1300 shell->workspaces.anim_to);
1301
1302 if (workspace_is_empty(from) &&
1303 workspace_has_only(to, surface))
1304 update_workspace(shell, index, from, to);
1305 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001306 if (shsurf != NULL &&
1307 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 wl_list_insert(&shell->workspaces.anim_sticky_list,
1309 &shsurf->workspace_transform.link);
1310
1311 animate_workspace_change(shell, index, from, to);
1312 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001313
Jonas Ådahl8538b222012-08-29 22:13:03 +02001314 state = ensure_focus_state(shell, seat);
1315 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001316 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317}
1318
1319static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001320touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001321 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001322{
1323}
1324
1325static void
1326touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1327{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001328 struct weston_touch_move_grab *move =
1329 (struct weston_touch_move_grab *) container_of(
1330 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001331
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001332 if (touch_id == 0)
1333 move->active = 0;
1334
Jonas Ådahl9484b692013-12-02 22:05:03 +01001335 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001336 shell_touch_grab_end(&move->base);
1337 free(move);
1338 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001339}
1340
1341static void
1342touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001343 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001344{
1345 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1346 struct shell_surface *shsurf = move->base.shsurf;
1347 struct weston_surface *es;
1348 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1349 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1350
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001351 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001352 return;
1353
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001354 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001355
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001356 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001357
1358 weston_compositor_schedule_repaint(es->compositor);
1359}
1360
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001361static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001362touch_move_grab_frame(struct weston_touch_grab *grab)
1363{
1364}
1365
1366static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001367touch_move_grab_cancel(struct weston_touch_grab *grab)
1368{
1369 struct weston_touch_move_grab *move =
1370 (struct weston_touch_move_grab *) container_of(
1371 grab, struct shell_touch_grab, grab);
1372
1373 shell_touch_grab_end(&move->base);
1374 free(move);
1375}
1376
Rusty Lynch1084da52013-08-15 09:10:08 -07001377static const struct weston_touch_grab_interface touch_move_grab_interface = {
1378 touch_move_grab_down,
1379 touch_move_grab_up,
1380 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001381 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001382 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001383};
1384
1385static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001386surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001387{
1388 struct weston_touch_move_grab *move;
1389
1390 if (!shsurf)
1391 return -1;
1392
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001393 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1394 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001395 return 0;
1396
1397 move = malloc(sizeof *move);
1398 if (!move)
1399 return -1;
1400
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001401 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001403 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001405 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001406
1407 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001408 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001409
1410 return 0;
1411}
1412
1413static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001414noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001415{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001416}
1417
1418static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001419noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001420 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001421{
1422}
1423
1424static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001425noop_grab_axis_source(struct weston_pointer_grab *grab,
1426 uint32_t source)
1427{
1428}
1429
1430static void
1431noop_grab_frame(struct weston_pointer_grab *grab)
1432{
1433}
1434
1435static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001436constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1437{
1438 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001439 struct weston_surface *surface =
1440 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001441 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001442 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001443 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001444 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001445 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001446
1447 x = wl_fixed_to_int(pointer->x + move->dx);
1448 y = wl_fixed_to_int(pointer->y + move->dy);
1449
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001450 if (shsurf->shell->panel_position ==
1451 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001452 get_output_work_area(shsurf->shell, surface->output, &area);
1453 geometry =
1454 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001455
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001456 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001457 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001458 y = area.y + safety - geometry.height
1459 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001460
1461 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001462 y + geometry.y < area.y)
1463 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001464 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001465
1466 *cx = x;
1467 *cy = y;
1468}
1469
1470static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001471move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001472 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001473{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001474 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001475 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001476 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001477 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001478 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001479
Jonas Ådahld2510102014-10-05 21:39:14 +02001480 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001481 if (!shsurf)
1482 return;
1483
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1485
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001486 constrain_position(move, &cx, &cy);
1487
1488 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001489
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001491}
1492
1493static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001494move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001495 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001496{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001497 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1498 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001499 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001500 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001501
Daniel Stone4dbadb12012-05-30 16:31:51 +01001502 if (pointer->button_count == 0 &&
1503 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001504 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001505 free(grab);
1506 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507}
1508
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001509static void
1510move_grab_cancel(struct weston_pointer_grab *grab)
1511{
1512 struct shell_grab *shell_grab =
1513 container_of(grab, struct shell_grab, grab);
1514
1515 shell_grab_end(shell_grab);
1516 free(grab);
1517}
1518
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001519static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001520 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001521 move_grab_motion,
1522 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001523 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001524 noop_grab_axis_source,
1525 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001526 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527};
1528
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001529static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001530surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001531 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001532{
1533 struct weston_move_grab *move;
1534
1535 if (!shsurf)
1536 return -1;
1537
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001538 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001539 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1540 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001541 return 0;
1542
1543 move = malloc(sizeof *move);
1544 if (!move)
1545 return -1;
1546
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001548 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001550 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001551 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552
1553 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001554 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001555
1556 return 0;
1557}
1558
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001559struct weston_resize_grab {
1560 struct shell_grab base;
1561 uint32_t edges;
1562 int32_t width, height;
1563};
1564
1565static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001566resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001567 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568{
1569 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001570 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571 struct shell_surface *shsurf = resize->base.shsurf;
1572 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001573 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574 wl_fixed_t from_x, from_y;
1575 wl_fixed_t to_x, to_y;
1576
Jonas Ådahld2510102014-10-05 21:39:14 +02001577 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001578
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001579 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580 return;
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 weston_view_from_global_fixed(shsurf->view,
1583 pointer->grab_x, pointer->grab_y,
1584 &from_x, &from_y);
1585 weston_view_from_global_fixed(shsurf->view,
1586 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587
1588 width = resize->width;
1589 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1590 width += wl_fixed_to_int(from_x - to_x);
1591 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1592 width += wl_fixed_to_int(to_x - from_x);
1593 }
1594
1595 height = resize->height;
1596 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1597 height += wl_fixed_to_int(from_y - to_y);
1598 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1599 height += wl_fixed_to_int(to_y - from_y);
1600 }
1601
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001602 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1603 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1604
1605 min_size.width = MAX(1, min_size.width);
1606 min_size.height = MAX(1, min_size.height);
1607
1608 if (width < min_size.width)
1609 width = min_size.width;
1610 else if (max_size.width > 0 && width > max_size.width)
1611 width = max_size.width;
1612 if (height < min_size.height)
1613 height = min_size.height;
1614 else if (max_size.width > 0 && width > max_size.width)
1615 width = max_size.width;
1616 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617}
1618
1619static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001620resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 uint32_t time, uint32_t button, uint32_t state_w)
1622{
1623 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001624 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625 enum wl_pointer_button_state state = state_w;
1626
1627 if (pointer->button_count == 0 &&
1628 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1629 shell_grab_end(&resize->base);
1630 free(grab);
1631 }
1632}
1633
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001634static void
1635resize_grab_cancel(struct weston_pointer_grab *grab)
1636{
1637 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1638
1639 shell_grab_end(&resize->base);
1640 free(grab);
1641}
1642
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001643static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644 noop_grab_focus,
1645 resize_grab_motion,
1646 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001647 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001648 noop_grab_axis_source,
1649 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001650 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651};
1652
Giulio Camuffob8366642013-04-25 13:57:46 +03001653/*
1654 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001655 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001656static void
1657surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1658 int32_t *y, int32_t *w, int32_t *h) {
1659 pixman_region32_t region;
1660 pixman_box32_t *box;
1661 struct weston_subsurface *subsurface;
1662
1663 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 surface->width,
1665 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001666
1667 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1668 pixman_region32_union_rect(&region, &region,
1669 subsurface->position.x,
1670 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 subsurface->surface->width,
1672 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001673 }
1674
1675 box = pixman_region32_extents(&region);
1676 if (x)
1677 *x = box->x1;
1678 if (y)
1679 *y = box->y1;
1680 if (w)
1681 *w = box->x2 - box->x1;
1682 if (h)
1683 *h = box->y2 - box->y1;
1684
1685 pixman_region32_fini(&region);
1686}
1687
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688static int
1689surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001690 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001691{
1692 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001693 const unsigned resize_topbottom =
1694 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1695 const unsigned resize_leftright =
1696 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1697 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001698 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001700 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001701 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1702 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 return 0;
1704
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001705 /* Check for invalid edge combinations. */
1706 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1707 (edges & resize_topbottom) == resize_topbottom ||
1708 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709 return 0;
1710
1711 resize = malloc(sizeof *resize);
1712 if (!resize)
1713 return -1;
1714
1715 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001717 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1718 resize->width = geometry.width;
1719 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001720
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001721 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001722 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001723 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724
1725 return 0;
1726}
1727
1728static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001729busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001730{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001731 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001732 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001733 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001734 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001735 wl_fixed_t sx, sy;
1736
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737 view = weston_compositor_pick_view(pointer->seat->compositor,
1738 pointer->x, pointer->y,
1739 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001740 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001741
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001742 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001743 shell_grab_end(grab);
1744 free(grab);
1745 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001746}
1747
1748static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001749busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001750 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001751{
Jonas Ådahld2510102014-10-05 21:39:14 +02001752 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001753}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001755static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001756busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001757 uint32_t time, uint32_t button, uint32_t state)
1758{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001759 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001760 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001761 struct weston_pointer *pointer = grab->grab.pointer;
1762 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001763
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001764 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001765 activate(shsurf->shell, shsurf->view, seat,
1766 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001767 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001768 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001769 activate(shsurf->shell, shsurf->view, seat,
1770 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001771 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001772 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001773}
1774
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001775static void
1776busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1777{
1778 struct shell_grab *grab = (struct shell_grab *) base;
1779
1780 shell_grab_end(grab);
1781 free(grab);
1782}
1783
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001784static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785 busy_cursor_grab_focus,
1786 busy_cursor_grab_motion,
1787 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001788 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001789 noop_grab_axis_source,
1790 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001791 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792};
1793
1794static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795handle_pointer_focus(struct wl_listener *listener, void *data)
1796{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001797 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001799 struct shell_surface *shsurf;
1800 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001801
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803 return;
1804
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001805 shsurf = get_shell_surface(view->surface);
1806 if (!shsurf)
1807 return;
1808
1809 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1810
1811 if (shsurf->unresponsive)
1812 set_busy_cursor(shsurf, pointer);
1813 else
1814 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815}
1816
1817static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001818shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1819{
1820 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001821 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001822}
1823
1824static void
1825shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1826{
1827 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001828 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001829}
1830
1831static void
1832handle_keyboard_focus(struct wl_listener *listener, void *data)
1833{
1834 struct weston_keyboard *keyboard = data;
1835 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1836
1837 if (seat->focused_surface) {
1838 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1839 if (shsurf)
1840 shell_surface_lose_keyboard_focus(shsurf);
1841 }
1842
1843 seat->focused_surface = keyboard->focus;
1844
1845 if (seat->focused_surface) {
1846 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1847 if (shsurf)
1848 shell_surface_gain_keyboard_focus(shsurf);
1849 }
1850}
1851
Philip Withnall07926d92013-11-25 18:01:40 +00001852/* The surface will be inserted into the list immediately after the link
1853 * returned by this function (i.e. will be stacked immediately above the
1854 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001855static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001856shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1857{
1858 struct workspace *ws;
1859
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001860 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1861 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001862 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001863 }
1864
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001865 /* Move the surface to a normal workspace layer so that surfaces
1866 * which were previously fullscreen or transient are no longer
1867 * rendered on top. */
1868 ws = get_current_workspace(shsurf->shell);
1869 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001870}
1871
Philip Withnall648a4dd2013-11-25 18:01:44 +00001872static void
1873shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1874{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001876}
1877
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001878/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001879 * geometry to ensure the changes are redrawn.
1880 *
1881 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1882 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001883static void
1884shell_surface_update_layer(struct shell_surface *shsurf)
1885{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001886 struct weston_surface *surface =
1887 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001888 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001889
1890 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1891
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001892 if (new_layer_link == NULL)
1893 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001894 if (new_layer_link == &shsurf->view->layer_link)
1895 return;
1896
1897 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001898 weston_layer_entry_remove(&shsurf->view->layer_link);
1899 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001900 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001901 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001902
1903 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001904}
1905
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001906static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001907shell_surface_set_output(struct shell_surface *shsurf,
1908 struct weston_output *output)
1909{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001910 struct weston_surface *es =
1911 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001912
1913 /* get the default output, if the client set it as NULL
1914 check whether the ouput is available */
1915 if (output)
1916 shsurf->output = output;
1917 else if (es->output)
1918 shsurf->output = es->output;
1919 else
1920 shsurf->output = get_default_output(es->compositor);
1921}
1922
1923static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001924weston_view_set_initial_position(struct weston_view *view,
1925 struct desktop_shell *shell);
1926
1927static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001928unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001929{
Philip Withnallf85fe842013-11-25 18:01:37 +00001930 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001931 wl_list_remove(&shsurf->fullscreen.transform.link);
1932 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001933
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934 if (shsurf->fullscreen.black_view)
1935 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1936 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001937
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001938 if (shsurf->saved_position_valid)
1939 weston_view_set_position(shsurf->view,
1940 shsurf->saved_x, shsurf->saved_y);
1941 else
1942 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001943 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001944
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001945 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001947 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001948 shsurf->saved_rotation_valid = false;
1949 }
Alex Wu4539b082012-03-01 12:57:46 +08001950}
1951
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001952static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001953unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001954{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001955 struct weston_surface *surface =
1956 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1957
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001958 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001959 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001960
1961 if (shsurf->saved_position_valid)
1962 weston_view_set_position(shsurf->view,
1963 shsurf->saved_x, shsurf->saved_y);
1964 else
1965 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001966 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001967
1968 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001969 wl_list_insert(&shsurf->view->geometry.transformation_list,
1970 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001971 shsurf->saved_rotation_valid = false;
1972 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001973}
1974
Philip Withnallbecb77e2013-11-25 18:01:30 +00001975static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001976set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001977{
1978 struct shell_surface *shsurf;
1979 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001980 struct weston_view *view;
1981
1982 view = get_default_view(surface);
1983 if (!view)
1984 return;
1985
1986 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1987
1988 shsurf = get_shell_surface(surface);
1989 current_ws = get_current_workspace(shsurf->shell);
1990
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001991 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001992 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001993
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001994 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02001995 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001996
1997 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001998 weston_view_damage_below(view);
1999}
2000
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002001
Tiago Vignattibe143262012-04-16 17:31:41 +03002002static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002003shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002004{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002005 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002006}
2007
Pekka Paalanen8274d902014-08-06 19:36:51 +03002008static int
2009black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2010{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002011 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002012 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002013 int n;
2014 int rem;
2015 int ret;
2016
2017 n = snprintf(buf, len, "black background surface for ");
2018 if (n < 0)
2019 return n;
2020
2021 rem = (int)len - n;
2022 if (rem < 0)
2023 rem = 0;
2024
2025 if (fs_surface->get_label)
2026 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2027 else
2028 ret = snprintf(buf + n, rem, "<unknown>");
2029
2030 if (ret < 0)
2031 return n;
2032
2033 return n + ret;
2034}
2035
Alex Wu21858432012-04-01 20:13:08 +08002036static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002037black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002038
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002040create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002041 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002042 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002043{
2044 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002046
2047 surface = weston_surface_create(ec);
2048 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002049 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002050 return NULL;
2051 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 view = weston_view_create(surface);
2053 if (surface == NULL) {
2054 weston_log("no memory\n");
2055 weston_surface_destroy(surface);
2056 return NULL;
2057 }
Alex Wu4539b082012-03-01 12:57:46 +08002058
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002059 surface->committed = black_surface_committed;
2060 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002061 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002062 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002063 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002064 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002065 pixman_region32_fini(&surface->input);
2066 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002067
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002068 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002069 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070
2071 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002072}
2073
Philip Withnalled8f1a92013-11-25 18:01:43 +00002074static void
2075shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2076{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002077 struct weston_surface *surface =
2078 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002079 struct weston_output *output = shsurf->fullscreen_output;
2080
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002081 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002082
2083 if (!shsurf->fullscreen.black_view)
2084 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002085 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002086 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002087 output->x, output->y,
2088 output->width,
2089 output->height);
2090
2091 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002092 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2093 weston_layer_entry_insert(&shsurf->view->layer_link,
2094 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002095 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002096 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002097
Armin Krezović4663aca2016-06-30 06:04:29 +02002098 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002099 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002100}
2101
Alex Wu4539b082012-03-01 12:57:46 +08002102/* Create black surface and append it to the associated fullscreen surface.
2103 * Handle size dismatch and positioning according to the method. */
2104static void
2105shell_configure_fullscreen(struct shell_surface *shsurf)
2106{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002107 struct weston_surface *surface =
2108 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002109 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002110
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002111 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002112 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002113 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2114 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002115
Philip Withnalled8f1a92013-11-25 18:01:43 +00002116 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002117
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002118 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002119 &surf_width, &surf_height);
2120
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002121 if (surface->buffer_ref.buffer)
2122 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002123}
2124
Alex Wu4539b082012-03-01 12:57:46 +08002125static void
2126shell_map_fullscreen(struct shell_surface *shsurf)
2127{
Alex Wubd3354b2012-04-17 17:20:49 +08002128 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002129}
2130
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002131static struct weston_output *
2132get_focused_output(struct weston_compositor *compositor)
2133{
2134 struct weston_seat *seat;
2135 struct weston_output *output = NULL;
2136
2137 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002138 struct weston_touch *touch = weston_seat_get_touch(seat);
2139 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2140 struct weston_keyboard *keyboard =
2141 weston_seat_get_keyboard(seat);
2142
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002143 /* Priority has touch focus, then pointer and
2144 * then keyboard focus. We should probably have
2145 * three for loops and check frist for touch,
2146 * then for pointer, etc. but unless somebody has some
2147 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002148 if (touch && touch->focus)
2149 output = touch->focus->output;
2150 else if (pointer && pointer->focus)
2151 output = pointer->focus->output;
2152 else if (keyboard && keyboard->focus)
2153 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002154
2155 if (output)
2156 break;
2157 }
2158
2159 return output;
2160}
2161
2162static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002163destroy_shell_seat(struct wl_listener *listener, void *data)
2164{
2165 struct shell_seat *shseat =
2166 container_of(listener,
2167 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002168
2169 wl_list_remove(&shseat->seat_destroy_listener.link);
2170 free(shseat);
2171}
2172
Jason Ekstrand024177c2014-04-21 19:42:58 -05002173static void
2174shell_seat_caps_changed(struct wl_listener *listener, void *data)
2175{
Derek Foreman1281a362015-07-31 16:55:32 -05002176 struct weston_keyboard *keyboard;
2177 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002178 struct shell_seat *seat;
2179
2180 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002181 keyboard = weston_seat_get_keyboard(seat->seat);
2182 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002183
Derek Foreman1281a362015-07-31 16:55:32 -05002184 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002185 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002186 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002187 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002188 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002189 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002190 wl_list_init(&seat->keyboard_focus_listener.link);
2191 }
2192
Derek Foreman1281a362015-07-31 16:55:32 -05002193 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002194 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002195 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002196 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002197 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002198 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002199 wl_list_init(&seat->pointer_focus_listener.link);
2200 }
2201}
2202
Giulio Camuffo5085a752013-03-25 21:42:45 +01002203static struct shell_seat *
2204create_shell_seat(struct weston_seat *seat)
2205{
2206 struct shell_seat *shseat;
2207
2208 shseat = calloc(1, sizeof *shseat);
2209 if (!shseat) {
2210 weston_log("no memory to allocate shell seat\n");
2211 return NULL;
2212 }
2213
2214 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002215
2216 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2217 wl_signal_add(&seat->destroy_signal,
2218 &shseat->seat_destroy_listener);
2219
Jason Ekstrand024177c2014-04-21 19:42:58 -05002220 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2221 wl_list_init(&shseat->keyboard_focus_listener.link);
2222
2223 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2224 wl_list_init(&shseat->pointer_focus_listener.link);
2225
2226 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2227 wl_signal_add(&seat->updated_caps_signal,
2228 &shseat->caps_changed_listener);
2229 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2230
Giulio Camuffo5085a752013-03-25 21:42:45 +01002231 return shseat;
2232}
2233
2234static struct shell_seat *
2235get_shell_seat(struct weston_seat *seat)
2236{
2237 struct wl_listener *listener;
2238
2239 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002240 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002241
2242 return container_of(listener,
2243 struct shell_seat, seat_destroy_listener);
2244}
2245
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002246static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002247fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002248{
2249 struct shell_surface *shsurf = data;
2250
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002251 weston_surface_destroy(shsurf->view->surface);
2252 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002253}
2254
2255static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002256fade_out_done(struct weston_view_animation *animation, void *data)
2257{
2258 struct shell_surface *shsurf = data;
2259 struct wl_event_loop *loop;
2260
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002261 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002263 if (weston_view_is_mapped(shsurf->view)) {
2264 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002265 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002266 }
2267}
2268
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002269struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002270get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002271{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002272 if (weston_surface_is_desktop_surface(surface)) {
2273 struct weston_desktop_surface *desktop_surface =
2274 weston_surface_get_desktop_surface(surface);
2275 return weston_desktop_surface_get_user_data(desktop_surface);
2276 }
2277 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002278}
2279
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002280/*
2281 * libweston-desktop
2282 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002283
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002284static void
2285desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2286 void *shell)
2287{
2288 struct weston_desktop_client *client =
2289 weston_desktop_surface_get_client(desktop_surface);
2290 struct wl_client *wl_client =
2291 weston_desktop_client_get_client(client);
2292 struct weston_view *view;
2293 struct shell_surface *shsurf;
2294 struct weston_surface *surface =
2295 weston_desktop_surface_get_surface(desktop_surface);
2296
2297 view = weston_desktop_surface_create_view(desktop_surface);
2298 if (!view)
2299 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002300
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301 shsurf = calloc(1, sizeof *shsurf);
2302 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002303 if (wl_client)
2304 wl_client_post_no_memory(wl_client);
2305 else
2306 weston_log("no memory to allocate shell surface\n");
2307 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002308 }
2309
Pekka Paalanen8274d902014-08-06 19:36:51 +03002310 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002311
Tiago Vignattibc052c92012-04-19 16:18:18 +03002312 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002313 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002314 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002315 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002316 shsurf->desktop_surface = desktop_surface;
2317 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002318 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002319 wl_list_init(&shsurf->fullscreen.transform.link);
2320
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002321 shsurf->output = get_default_output(shsurf->shell->compositor);
2322
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002323 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002324
Pekka Paalanen460099f2012-01-20 16:48:25 +02002325 /* empty when not in use */
2326 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002327 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002328
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002329 wl_list_init(&shsurf->workspace_transform.link);
2330
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002331 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2332 weston_desktop_surface_set_activated(desktop_surface,
2333 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002334}
2335
Tiago Vignattibc052c92012-04-19 16:18:18 +03002336static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002337desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2338 void *shell)
2339{
2340 struct shell_surface *shsurf =
2341 weston_desktop_surface_get_user_data(desktop_surface);
2342 struct weston_surface *surface =
2343 weston_desktop_surface_get_surface(desktop_surface);
2344
2345 if (!shsurf)
2346 return;
2347
2348 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2349
2350 if (shsurf->fullscreen.black_view)
2351 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2352
2353 weston_surface_set_label_func(surface, NULL);
2354 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2355 shsurf->desktop_surface = NULL;
2356
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002357 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002358 if (weston_surface_is_mapped(surface) &&
2359 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2360 pixman_region32_fini(&surface->pending.input);
2361 pixman_region32_init(&surface->pending.input);
2362 pixman_region32_fini(&surface->input);
2363 pixman_region32_init(&surface->input);
2364 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2365 fade_out_done, shsurf);
2366 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002367 weston_view_destroy(shsurf->view);
2368 free(shsurf);
2369 }
2370}
2371
2372static void
2373set_maximized_position(struct desktop_shell *shell,
2374 struct shell_surface *shsurf)
2375{
2376 pixman_rectangle32_t area;
2377 struct weston_geometry geometry;
2378
2379 get_output_work_area(shell, shsurf->output, &area);
2380 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2381
2382 weston_view_set_position(shsurf->view,
2383 area.x - geometry.x,
2384 area.y - geometry.y);
2385}
2386
2387static void
2388map(struct desktop_shell *shell, struct shell_surface *shsurf,
2389 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002390{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002391 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002392 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2393 struct weston_compositor *compositor = shell->compositor;
2394 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002395
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002396 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002397 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002398 center_on_output(shsurf->view, shsurf->fullscreen_output);
2399 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002400 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002401 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002402 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002403 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002404 }
2405
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002406 /* Surface stacking order, see also activate(). */
2407 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002408
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002409 weston_view_update_transform(shsurf->view);
2410 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002411 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002412 surface->output = shsurf->output;
2413 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002414 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002415
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002416 if (!shell->locked) {
2417 wl_list_for_each(seat, &compositor->seat_list, link)
2418 activate(shell, shsurf->view, seat,
2419 WESTON_ACTIVATE_FLAG_CONFIGURE);
2420 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002421
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002422 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002423 switch (shell->win_animation_type) {
2424 case ANIMATION_FADE:
2425 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2426 break;
2427 case ANIMATION_ZOOM:
2428 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2429 break;
2430 case ANIMATION_NONE:
2431 default:
2432 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002433 }
2434 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002435}
2436
2437static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002438desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2439 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002440{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002441 struct shell_surface *shsurf =
2442 weston_desktop_surface_get_user_data(desktop_surface);
2443 struct weston_surface *surface =
2444 weston_desktop_surface_get_surface(desktop_surface);
2445 struct weston_view *view = shsurf->view;
2446 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002447 bool was_fullscreen;
2448 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002449
2450 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002451 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002453 was_fullscreen = shsurf->state.fullscreen;
2454 was_maximized = shsurf->state.maximized;
2455
2456 shsurf->state.fullscreen =
2457 weston_desktop_surface_get_fullscreen(desktop_surface);
2458 shsurf->state.maximized =
2459 weston_desktop_surface_get_maximized(desktop_surface);
2460
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002461 if (!weston_surface_is_mapped(surface)) {
2462 map(shell, shsurf, sx, sy);
2463 surface->is_mapped = true;
2464 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2465 ++surface->ref_count;
2466 return;
2467 }
2468
2469 if (sx == 0 && sy == 0 &&
2470 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002471 shsurf->last_height == surface->height &&
2472 was_fullscreen == shsurf->state.fullscreen &&
2473 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 return;
2475
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002476 if (was_fullscreen)
2477 unset_fullscreen(shsurf);
2478 if (was_maximized)
2479 unset_maximized(shsurf);
2480
Quentin Glidic920cf042016-08-16 14:26:20 +02002481 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2482 !shsurf->saved_position_valid) {
2483 shsurf->saved_x = shsurf->view->geometry.x;
2484 shsurf->saved_y = shsurf->view->geometry.y;
2485 shsurf->saved_position_valid = true;
2486
2487 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2488 wl_list_remove(&shsurf->rotation.transform.link);
2489 wl_list_init(&shsurf->rotation.transform.link);
2490 weston_view_geometry_dirty(shsurf->view);
2491 shsurf->saved_rotation_valid = true;
2492 }
2493 }
2494
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002495 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002496 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002497 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002498 set_maximized_position(shell, shsurf);
2499 surface->output = shsurf->output;
2500 } else {
2501 float from_x, from_y;
2502 float to_x, to_y;
2503 float x, y;
2504
2505 if (shsurf->resize_edges) {
2506 sx = 0;
2507 sy = 0;
2508 }
2509
2510 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2511 sx = shsurf->last_width - surface->width;
2512 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2513 sy = shsurf->last_height - surface->height;
2514
2515 shsurf->last_width = surface->width;
2516 shsurf->last_height = surface->height;
2517
2518 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2519 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2520 x = shsurf->view->geometry.x + to_x - from_x;
2521 y = shsurf->view->geometry.y + to_y - from_y;
2522
2523 weston_view_set_position(shsurf->view, x, y);
2524 }
2525
2526 /* XXX: would a fullscreen surface need the same handling? */
2527 if (surface->output) {
2528 wl_list_for_each(view, &surface->views, surface_link)
2529 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002530 }
2531}
2532
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002533static void
2534set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2535 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002536{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002537 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2538 struct weston_surface *surface =
2539 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2540 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002541
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002542 if (fullscreen) {
2543 /* handle clients launching in fullscreen */
2544 if (output == NULL && !weston_surface_is_mapped(surface)) {
2545 /* Set the output to the one that has focus currently. */
2546 output = get_focused_output(surface->compositor);
2547 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002548
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002549 shell_surface_set_output(shsurf, output);
2550 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002551
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 width = shsurf->output->width;
2553 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002554 }
2555 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2556 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002557}
2558
2559static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002560desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2561 struct weston_seat *seat, uint32_t serial, void *shell)
2562{
2563 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2564 struct weston_touch *touch = weston_seat_get_touch(seat);
2565 struct shell_surface *shsurf =
2566 weston_desktop_surface_get_user_data(desktop_surface);
2567 struct weston_surface *surface =
2568 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2569 struct wl_resource *resource = surface->resource;
2570 struct weston_surface *focus;
2571
2572 if (pointer &&
2573 pointer->focus &&
2574 pointer->button_count > 0 &&
2575 pointer->grab_serial == serial) {
2576 focus = weston_surface_get_main_surface(pointer->focus->surface);
2577 if ((focus == surface) &&
2578 (surface_move(shsurf, pointer, true) < 0))
2579 wl_resource_post_no_memory(resource);
2580 } else if (touch &&
2581 touch->focus &&
2582 touch->grab_serial == serial) {
2583 focus = weston_surface_get_main_surface(touch->focus->surface);
2584 if ((focus == surface) &&
2585 (surface_touch_move(shsurf, touch) < 0))
2586 wl_resource_post_no_memory(resource);
2587 }
2588}
2589
2590static void
2591desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2592 struct weston_seat *seat, uint32_t serial,
2593 enum weston_desktop_surface_edge edges, void *shell)
2594{
2595 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2596 struct shell_surface *shsurf =
2597 weston_desktop_surface_get_user_data(desktop_surface);
2598 struct weston_surface *surface =
2599 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2600 struct wl_resource *resource = surface->resource;
2601 struct weston_surface *focus;
2602
2603 if (!pointer ||
2604 pointer->button_count == 0 ||
2605 pointer->grab_serial != serial ||
2606 pointer->focus == NULL)
2607 return;
2608
2609 focus = weston_surface_get_main_surface(pointer->focus->surface);
2610 if (focus != surface)
2611 return;
2612
2613 if (surface_resize(shsurf, pointer, edges) < 0)
2614 wl_resource_post_no_memory(resource);
2615}
2616
2617static void
2618desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2619 bool fullscreen,
2620 struct weston_output *output, void *shell)
2621{
2622 struct shell_surface *shsurf =
2623 weston_desktop_surface_get_user_data(desktop_surface);
2624
2625 set_fullscreen(shsurf, fullscreen, output);
2626}
2627
2628static void
2629set_maximized(struct shell_surface *shsurf, bool maximized)
2630{
2631 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2632 struct weston_surface *surface =
2633 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2634 int32_t width = 0, height = 0;
2635
2636 if (maximized) {
2637 struct weston_output *output;
2638 struct desktop_shell *shell;
2639 pixman_rectangle32_t area;
2640
2641 if (!weston_surface_is_mapped(surface))
2642 output = get_focused_output(surface->compositor);
2643 else
2644 output = surface->output;
2645
2646 shell_surface_set_output(shsurf, output);
2647
2648 shell = shell_surface_get_shell(shsurf);
2649 get_output_work_area(shell, shsurf->output, &area);
2650
2651 width = area.width;
2652 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002653 }
2654 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2655 weston_desktop_surface_set_size(desktop_surface, width, height);
2656}
2657
2658static void
2659desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2660 bool maximized, void *shell)
2661{
2662 struct shell_surface *shsurf =
2663 weston_desktop_surface_get_user_data(desktop_surface);
2664
2665 set_maximized(shsurf, maximized);
2666}
2667
2668static void
2669desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2670 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002671{
2672 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002673 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002674
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002675 /* apply compositor's own minimization logic (hide) */
2676 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002677}
2678
Rafael Antognollie2a34552013-12-03 15:35:45 -02002679static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002680set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002681{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002682 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002683
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002684 if (pointer->grab->interface == &busy_cursor_grab_interface)
2685 return;
2686
2687 grab = malloc(sizeof *grab);
2688 if (!grab)
2689 return;
2690
2691 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2692 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2693 /* Mark the shsurf as ungrabbed so that button binding is able
2694 * to move it. */
2695 shsurf->grabbed = 0;
2696}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002697
2698static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002699end_busy_cursor(struct weston_compositor *compositor,
2700 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002701{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002702 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002703 struct shell_grab *grab;
2704 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002705
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002706 wl_list_for_each(seat, &compositor->seat_list, link) {
2707 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2708 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002709
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002710 if (!pointer)
2711 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002712
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002713 if (pointer->grab->interface != &busy_cursor_grab_interface)
2714 continue;
2715
2716 grab = (struct shell_grab *) pointer->grab;
2717 shsurf = grab->shsurf;
2718 if (!shsurf)
2719 continue;
2720
2721 grab_client =
2722 weston_desktop_surface_get_client(shsurf->desktop_surface);
2723 if (grab_client == desktop_client) {
2724 shell_grab_end(grab);
2725 free(grab);
2726 }
2727 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002728}
2729
2730static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002731desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2732 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002733{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002734 struct shell_surface *shsurf =
2735 weston_desktop_surface_get_user_data(desktop_surface);
2736 bool *unresponsive = user_data;
2737
2738 shsurf->unresponsive = *unresponsive;
2739}
2740
2741static void
2742desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2743 void *shell_)
2744{
2745 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002746 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002747 struct weston_seat *seat;
2748 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002749
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002750 weston_desktop_client_for_each_surface(desktop_client,
2751 desktop_surface_set_unresponsive,
2752 &unresponsive);
2753
2754
2755 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2756 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2757 struct weston_desktop_client *grab_client;
2758
2759 if (!pointer || !pointer->focus)
2760 continue;
2761
2762 shsurf = get_shell_surface(pointer->focus->surface);
2763 if (!shsurf)
2764 continue;
2765
2766 grab_client =
2767 weston_desktop_surface_get_client(shsurf->desktop_surface);
2768 if (grab_client == desktop_client)
2769 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002770 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002771}
2772
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002773static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002774desktop_surface_pong(struct weston_desktop_client *desktop_client,
2775 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002776{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002777 struct desktop_shell *shell = shell_;
2778 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002780 weston_desktop_client_for_each_surface(desktop_client,
2781 desktop_surface_set_unresponsive,
2782 &unresponsive);
2783 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002784}
2785
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002786static const struct weston_desktop_api shell_desktop_api = {
2787 .struct_size = sizeof(struct weston_desktop_api),
2788 .surface_added = desktop_surface_added,
2789 .surface_removed = desktop_surface_removed,
2790 .committed = desktop_surface_committed,
2791 .move = desktop_surface_move,
2792 .resize = desktop_surface_resize,
2793 .fullscreen_requested = desktop_surface_fullscreen_requested,
2794 .maximized_requested = desktop_surface_maximized_requested,
2795 .minimized_requested = desktop_surface_minimized_requested,
2796 .ping_timeout = desktop_surface_ping_timeout,
2797 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002798};
2799
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002800/* ************************ *
2801 * end of libweston-desktop *
2802 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002803static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002804shell_fade(struct desktop_shell *shell, enum fade_type type);
2805
2806static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002807configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002808{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002810
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002811 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812 if (v->output == ev->output && v != ev) {
2813 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002814 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002815 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002816 }
2817 }
2818
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002819 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002820 ev->surface->is_mapped = true;
2821 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002822
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002823 if (wl_list_empty(&ev->layer_link.link)) {
2824 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002826 }
2827}
2828
David Fort50d962f2016-06-09 17:55:52 +02002829
2830static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002831find_shell_output_from_weston_output(struct desktop_shell *shell,
2832 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002833{
2834 struct shell_output *shell_output;
2835
2836 wl_list_for_each(shell_output, &shell->output_list, link) {
2837 if (shell_output->output == output)
2838 return shell_output;
2839 }
2840
2841 return NULL;
2842}
2843
Pekka Paalanen8274d902014-08-06 19:36:51 +03002844static int
2845background_get_label(struct weston_surface *surface, char *buf, size_t len)
2846{
2847 return snprintf(buf, len, "background for output %s",
2848 surface->output->name);
2849}
2850
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002851static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002852background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002853{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002854 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002856
Jason Ekstranda7af7042013-10-12 22:38:11 -05002857 view = container_of(es->views.next, struct weston_view, surface_link);
2858
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002859 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002860}
2861
2862static void
David Fort50d962f2016-06-09 17:55:52 +02002863handle_background_surface_destroy(struct wl_listener *listener, void *data)
2864{
2865 struct shell_output *output =
2866 container_of(listener, struct shell_output, background_surface_listener);
2867
2868 weston_log("background surface gone\n");
2869 output->background_surface = NULL;
2870}
2871
2872static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002873desktop_shell_set_background(struct wl_client *client,
2874 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002875 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002876 struct wl_resource *surface_resource)
2877{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002878 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002879 struct weston_surface *surface =
2880 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002881 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002883
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002884 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002885 wl_resource_post_error(surface_resource,
2886 WL_DISPLAY_ERROR_INVALID_OBJECT,
2887 "surface role already assigned");
2888 return;
2889 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002890
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2892 weston_view_destroy(view);
2893 view = weston_view_create(surface);
2894
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002895 surface->committed = background_committed;
2896 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002897 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002898 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002899 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002900 weston_desktop_shell_send_configure(resource, 0,
2901 surface_resource,
2902 surface->output->width,
2903 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002904
2905 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2906 sh_output->background_surface = surface;
2907
2908 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2909 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002910}
2911
Pekka Paalanen8274d902014-08-06 19:36:51 +03002912static int
2913panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2914{
2915 return snprintf(buf, len, "panel for output %s",
2916 surface->output->name);
2917}
2918
Kristian Høgsberg75840622011-09-06 13:48:16 -04002919static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002920panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002921{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002922 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002924
Jason Ekstranda7af7042013-10-12 22:38:11 -05002925 view = container_of(es->views.next, struct weston_view, surface_link);
2926
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002927 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002928}
2929
2930static void
David Fort50d962f2016-06-09 17:55:52 +02002931handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2932{
2933 struct shell_output *output =
2934 container_of(listener, struct shell_output, panel_surface_listener);
2935
2936 weston_log("panel surface gone\n");
2937 output->panel_surface = NULL;
2938}
2939
2940
2941static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002942desktop_shell_set_panel(struct wl_client *client,
2943 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002944 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002945 struct wl_resource *surface_resource)
2946{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002947 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002948 struct weston_surface *surface =
2949 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002950 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02002951 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002952
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002953 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002954 wl_resource_post_error(surface_resource,
2955 WL_DISPLAY_ERROR_INVALID_OBJECT,
2956 "surface role already assigned");
2957 return;
2958 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002959
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2961 weston_view_destroy(view);
2962 view = weston_view_create(surface);
2963
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002964 surface->committed = panel_committed;
2965 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002966 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002967 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002969 weston_desktop_shell_send_configure(resource, 0,
2970 surface_resource,
2971 surface->output->width,
2972 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002973
2974 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2975 sh_output->panel_surface = surface;
2976
2977 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
2978 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002979}
2980
Pekka Paalanen8274d902014-08-06 19:36:51 +03002981static int
2982lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2983{
2984 return snprintf(buf, len, "lock window");
2985}
2986
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002987static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002988lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002989{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002990 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002991 struct weston_view *view;
2992
2993 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002994
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002995 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01002996 return;
2997
Jason Ekstranda7af7042013-10-12 22:38:11 -05002998 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002999
3000 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003001 weston_layer_entry_insert(&shell->lock_layer.view_list,
3002 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003003 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003004 surface->is_mapped = true;
3005 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003006 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003007 }
3008}
3009
3010static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003011handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003012{
Tiago Vignattibe143262012-04-16 17:31:41 +03003013 struct desktop_shell *shell =
3014 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003015
Martin Minarik6d118362012-06-07 18:01:59 +02003016 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003017 shell->lock_surface = NULL;
3018}
3019
3020static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003021desktop_shell_set_lock_surface(struct wl_client *client,
3022 struct wl_resource *resource,
3023 struct wl_resource *surface_resource)
3024{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003025 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003026 struct weston_surface *surface =
3027 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003028
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003029 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003030
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003031 if (!shell->locked)
3032 return;
3033
Pekka Paalanen98262232011-12-01 10:42:22 +02003034 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003035
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003036 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003037 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003038 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003039
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003040 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003041 surface->committed = lock_surface_committed;
3042 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003043 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003044}
3045
3046static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003047resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003048{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003049 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003050
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003051 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003052 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003053 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003054 &shell->input_panel_layer.link);
3055 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003056 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003057 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003058 wl_list_insert(&shell->compositor->cursor_layer.link,
3059 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003060 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003061 wl_list_insert(&shell->fullscreen_layer.link,
3062 &shell->panel_layer.link);
3063 wl_list_insert(&shell->panel_layer.link,
3064 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003065
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003066 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003067
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003068 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003069 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003070 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003071}
3072
3073static void
3074desktop_shell_unlock(struct wl_client *client,
3075 struct wl_resource *resource)
3076{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003077 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003078
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003079 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003080
3081 if (shell->locked)
3082 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003083}
3084
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003085static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003086desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003087 struct wl_resource *resource,
3088 struct wl_resource *surface_resource)
3089{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003090 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003091
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003092 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003093 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003094}
3095
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003096static void
3097desktop_shell_desktop_ready(struct wl_client *client,
3098 struct wl_resource *resource)
3099{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003100 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003101
3102 shell_fade_startup(shell);
3103}
3104
Jonny Lamb765760d2014-08-20 15:53:19 +02003105static void
3106desktop_shell_set_panel_position(struct wl_client *client,
3107 struct wl_resource *resource,
3108 uint32_t position)
3109{
3110 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3111
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003112 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3113 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3114 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3115 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003116 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003117 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003118 "bad position argument");
3119 return;
3120 }
3121
3122 shell->panel_position = position;
3123}
3124
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003125static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003126 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003127 desktop_shell_set_panel,
3128 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003129 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003130 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003131 desktop_shell_desktop_ready,
3132 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003133};
3134
3135static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003136move_binding(struct weston_pointer *pointer, uint32_t time,
3137 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003138{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003139 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003140 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003141 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003142
Derek Foreman8ae2db52015-07-15 13:00:36 -05003143 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003144 return;
3145
Derek Foreman8ae2db52015-07-15 13:00:36 -05003146 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003147
Pekka Paalanen01388e22013-04-25 13:57:44 +03003148 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003149 if (surface == NULL)
3150 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003151
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003152 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003153 if (shsurf == NULL ||
3154 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3155 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003156 return;
3157
Derek Foreman794fa0e2015-07-15 13:00:38 -05003158 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003159}
3160
3161static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003162maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3163 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003164{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003165 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003166 struct weston_surface *surface;
3167 struct shell_surface *shsurf;
3168
3169 surface = weston_surface_get_main_surface(focus);
3170 if (surface == NULL)
3171 return;
3172
3173 shsurf = get_shell_surface(surface);
3174 if (shsurf == NULL)
3175 return;
3176
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003177 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003178}
3179
3180static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003181fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3182 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003183{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003184 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003185 struct weston_surface *surface;
3186 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003187 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003188
3189 surface = weston_surface_get_main_surface(focus);
3190 if (surface == NULL)
3191 return;
3192
3193 shsurf = get_shell_surface(surface);
3194 if (shsurf == NULL)
3195 return;
3196
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003197 fullscreen =
3198 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003199
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003200 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003201}
3202
3203static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003204touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003205{
Derek Foreman362656b2014-09-04 10:23:05 -05003206 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003207 struct weston_surface *surface;
3208 struct shell_surface *shsurf;
3209
Derek Foreman8ae2db52015-07-15 13:00:36 -05003210 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003211 return;
3212
Derek Foreman8ae2db52015-07-15 13:00:36 -05003213 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003214 surface = weston_surface_get_main_surface(focus);
3215 if (surface == NULL)
3216 return;
3217
3218 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003219 if (shsurf == NULL ||
3220 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3221 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003222 return;
3223
Derek Foremanb7674ae2015-07-15 13:00:37 -05003224 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003225}
3226
3227static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003228resize_binding(struct weston_pointer *pointer, uint32_t time,
3229 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003230{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003231 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003232 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003233 uint32_t edges = 0;
3234 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003235 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003236
Derek Foreman8ae2db52015-07-15 13:00:36 -05003237 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003238 return;
3239
Derek Foreman8ae2db52015-07-15 13:00:36 -05003240 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003241
Pekka Paalanen01388e22013-04-25 13:57:44 +03003242 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003243 if (surface == NULL)
3244 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003245
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003246 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003247 if (shsurf == NULL ||
3248 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3249 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003250 return;
3251
Jason Ekstranda7af7042013-10-12 22:38:11 -05003252 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003253 wl_fixed_to_int(pointer->grab_x),
3254 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003255 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003256
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003257 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003258 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003259 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003260 edges |= 0;
3261 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003262 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003263
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003264 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003265 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003266 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003267 edges |= 0;
3268 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003269 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003270
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003271 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003272}
3273
3274static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003275surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003276 struct weston_pointer_axis_event *event,
3277 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003278{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003279 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003280 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003281 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003282 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003283
Pekka Paalanen01388e22013-04-25 13:57:44 +03003284 /* XXX: broken for windows containing sub-surfaces */
3285 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003286 if (surface == NULL)
3287 return;
3288
3289 shsurf = get_shell_surface(surface);
3290 if (!shsurf)
3291 return;
3292
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003293 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003294
Jason Ekstranda7af7042013-10-12 22:38:11 -05003295 if (shsurf->view->alpha > 1.0)
3296 shsurf->view->alpha = 1.0;
3297 if (shsurf->view->alpha < step)
3298 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003299
Jason Ekstranda7af7042013-10-12 22:38:11 -05003300 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003301 weston_surface_damage(surface);
3302}
3303
3304static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003305do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003306 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003307{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003308 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003309 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003310 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003311 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003312
Derek Foreman1281a362015-07-31 16:55:32 -05003313 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003314 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3315 return;
3316 }
3317
Scott Moreauccbf29d2012-02-22 14:21:41 -07003318 wl_list_for_each(output, &compositor->output_list, link) {
3319 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003320 wl_fixed_to_double(pointer->x),
3321 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003322 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003323 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003324 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003325 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003326 increment = -output->zoom.increment;
3327 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003328 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003329 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003330 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003331 else
3332 increment = 0;
3333
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003334 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003335
Scott Moreaue6603982012-06-11 13:07:51 -06003336 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003337 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003338 else if (output->zoom.level > output->zoom.max_level)
3339 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003340
3341 if (!output->zoom.active) {
3342 if (output->zoom.level <= 0.0)
3343 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003344 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003345 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003346
Scott Moreaue6603982012-06-11 13:07:51 -06003347 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003348
Jason Ekstranda7af7042013-10-12 22:38:11 -05003349 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003350 }
3351 }
3352}
3353
Scott Moreauccbf29d2012-02-22 14:21:41 -07003354static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003355zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003356 struct weston_pointer_axis_event *event,
3357 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003358{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003359 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003360}
3361
3362static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003363zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3364 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003365{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003366 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003367}
3368
3369static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003370terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3371 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003372{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003373 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003374
Daniel Stone325fc2d2012-05-30 16:31:58 +01003375 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003376}
3377
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003378static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003379rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003380 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003381{
3382 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003383 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003384 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003385 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003386 struct weston_surface *surface =
3387 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003388 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003389
Jonas Ådahld2510102014-10-05 21:39:14 +02003390 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003391
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003392 if (!shsurf)
3393 return;
3394
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003395 cx = 0.5f * surface->width;
3396 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003397
Daniel Stone37816df2012-05-16 18:45:18 +01003398 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3399 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003400 r = sqrtf(dx * dx + dy * dy);
3401
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003402 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003403 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003404
3405 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003406 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003407 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003408
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003409 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003410 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003411
3412 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003413 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003414 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003415 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003416 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003417
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003418 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003419 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003420 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003421 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003422 wl_list_init(&shsurf->rotation.transform.link);
3423 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003424 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003425 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003426
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003427 /* We need to adjust the position of the surface
3428 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003429 cposx = shsurf->view->geometry.x + cx;
3430 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003431 dposx = rotate->center.x - cposx;
3432 dposy = rotate->center.y - cposy;
3433 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003434 weston_view_set_position(shsurf->view,
3435 shsurf->view->geometry.x + dposx,
3436 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003437 }
3438
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003439 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003440 * lazily applies the damage due to rotation update.
3441 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003442 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003443}
3444
3445static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003446rotate_grab_button(struct weston_pointer_grab *grab,
3447 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003448{
3449 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003450 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003451 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003452 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003453 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003454
Daniel Stone4dbadb12012-05-30 16:31:51 +01003455 if (pointer->button_count == 0 &&
3456 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003457 if (shsurf)
3458 weston_matrix_multiply(&shsurf->rotation.rotation,
3459 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003460 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003461 free(rotate);
3462 }
3463}
3464
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003465static void
3466rotate_grab_cancel(struct weston_pointer_grab *grab)
3467{
3468 struct rotate_grab *rotate =
3469 container_of(grab, struct rotate_grab, base.grab);
3470
3471 shell_grab_end(&rotate->base);
3472 free(rotate);
3473}
3474
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003475static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003476 noop_grab_focus,
3477 rotate_grab_motion,
3478 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003479 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003480 noop_grab_axis_source,
3481 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003482 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003483};
3484
3485static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003486surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003487{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003488 struct weston_surface *surface =
3489 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003490 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003491 float dx, dy;
3492 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003493
Pekka Paalanen460099f2012-01-20 16:48:25 +02003494 rotate = malloc(sizeof *rotate);
3495 if (!rotate)
3496 return;
3497
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003498 weston_view_to_global_float(shsurf->view,
3499 surface->width * 0.5f,
3500 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003501 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003502
Derek Foreman74de4692015-07-15 13:00:39 -05003503 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3504 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003505 r = sqrtf(dx * dx + dy * dy);
3506 if (r > 20.0f) {
3507 struct weston_matrix inverse;
3508
3509 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003510 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003511 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003512
3513 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003514 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003515 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003516 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003517 weston_matrix_init(&rotate->rotation);
3518 }
3519
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003520 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003521 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003522}
3523
3524static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003525rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003526 void *data)
3527{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003528 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003529 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003530 struct shell_surface *surface;
3531
Derek Foreman8ae2db52015-07-15 13:00:36 -05003532 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003533 return;
3534
Derek Foreman8ae2db52015-07-15 13:00:36 -05003535 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003536
Pekka Paalanen01388e22013-04-25 13:57:44 +03003537 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003538 if (base_surface == NULL)
3539 return;
3540
3541 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003542 if (surface == NULL ||
3543 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3544 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003545 return;
3546
Derek Foreman74de4692015-07-15 13:00:39 -05003547 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003548}
3549
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003550/* Move all fullscreen layers down to the current workspace and hide their
3551 * black views. The surfaces' state is set to both fullscreen and lowered,
3552 * and this is reversed when such a surface is re-configured, see
3553 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3554 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003555 * lowering_output = NULL - Lower on all outputs, else only lower on the
3556 * specified output.
3557 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003558 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003559 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003560void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003561lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003562 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003563{
3564 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003565 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003566
3567 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003569 &shell->fullscreen_layer.view_list.link,
3570 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003571 struct shell_surface *shsurf = get_shell_surface(view->surface);
3572
3573 if (!shsurf)
3574 continue;
3575
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003576 /* Only lower surfaces which have lowering_output as their fullscreen
3577 * output, unless a NULL output asks for lowering on all outputs.
3578 */
3579 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3580 continue;
3581
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003582 /* We can have a non-fullscreen popup for a fullscreen surface
3583 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003584 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003585 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003586 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3587 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003588 weston_view_damage_below(shsurf->fullscreen.black_view);
3589
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003590 }
3591
3592 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003593 weston_layer_entry_remove(&view->layer_link);
3594 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003595 weston_view_damage_below(view);
3596 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003597
3598 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003599 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003600}
3601
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003602void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003603activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003604 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003605{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003606 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003607 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003608 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003609 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003610 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003611 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003612
Pekka Paalanen01388e22013-04-25 13:57:44 +03003613 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003614 shsurf = get_shell_surface(main_surface);
3615 assert(shsurf);
3616
3617 /* Only demote fullscreen surfaces on the output of activated shsurf.
3618 * Leave fullscreen surfaces on unrelated outputs alone. */
3619 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003620
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003621 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003622
Jonas Ådahl8538b222012-08-29 22:13:03 +02003623 state = ensure_focus_state(shell, seat);
3624 if (state == NULL)
3625 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003626
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003627 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003628 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003629
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003630 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3631 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003632 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003633
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003634 /* Update the surface’s layer. This brings it to the top of the stacking
3635 * order as appropriate. */
3636 shell_surface_update_layer(shsurf);
3637
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003638 if (shell->focus_animation_type != ANIMATION_NONE) {
3639 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003640 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003641 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003642}
3643
Alex Wu21858432012-04-01 20:13:08 +08003644/* no-op func for checking black surface */
3645static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003646black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003647{
3648}
3649
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003650static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003651is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003652{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003653 struct weston_surface *surface = view->surface;
3654
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003655 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003656 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003657 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003658 return true;
3659 }
3660 return false;
3661}
3662
Kristian Høgsberg75840622011-09-06 13:48:16 -04003663static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003664activate_binding(struct weston_seat *seat,
3665 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003666 struct weston_view *focus_view,
3667 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003668{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003669 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003670 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003671
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003672 if (!focus_view)
3673 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003674
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003675 if (is_black_surface_view(focus_view, &main_view))
3676 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003677
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003678 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003679 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003680 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003681
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003682 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003683}
3684
3685static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003686click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3687 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003688{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003689 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003690 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003691 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003692 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003693
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003694 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003695 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003696 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003697}
3698
3699static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003700touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3701 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003702{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003703 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003704 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003705 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003706 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003707
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003708 activate_binding(touch->seat, data, touch->focus,
3709 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003710}
3711
3712static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003713unfocus_all_seats(struct desktop_shell *shell)
3714{
3715 struct weston_seat *seat, *next;
3716
3717 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003718 struct weston_keyboard *keyboard =
3719 weston_seat_get_keyboard(seat);
3720
3721 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003722 continue;
3723
Derek Foreman1281a362015-07-31 16:55:32 -05003724 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003725 }
3726}
3727
3728static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003729lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003730{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003731 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003732
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003733 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003734 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003735 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003736 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003737
3738 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003739
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003740 /* Hide all surfaces by removing the fullscreen, panel and
3741 * toplevel layers. This way nothing else can show or receive
3742 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003743
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003744 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003745 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003746 if (shell->showing_input_panels)
3747 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003748 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003749 wl_list_insert(&shell->compositor->cursor_layer.link,
3750 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003751
Derek Foreman004b4a12015-07-20 16:28:13 -05003752 weston_compositor_sleep(shell->compositor);
3753
Neil Robertsb4a91702014-04-09 16:33:32 +01003754 /* Remove the keyboard focus on all seats. This will be
3755 * restored to the workspace's saved state via
3756 * restore_focus_state when the compositor is unlocked */
3757 unfocus_all_seats(shell);
3758
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003759 /* TODO: disable bindings that should not work while locked. */
3760
3761 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003762}
3763
3764static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003765unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003766{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003767 struct wl_resource *shell_resource;
3768
Pekka Paalanend81c2162011-11-16 13:47:34 +02003769 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003770 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003771 return;
3772 }
3773
3774 /* If desktop-shell client has gone away, unlock immediately. */
3775 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003776 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003777 return;
3778 }
3779
3780 if (shell->prepare_event_sent)
3781 return;
3782
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003783 shell_resource = shell->child.desktop_shell;
3784 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003785 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003786}
3787
3788static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003789shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003790{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003791 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003792
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003793 shell->fade.animation = NULL;
3794
3795 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003796 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003797 weston_surface_destroy(shell->fade.view->surface);
3798 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003799 break;
3800 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003801 lock(shell);
3802 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003803 default:
3804 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003805 }
3806}
3807
Jason Ekstranda7af7042013-10-12 22:38:11 -05003808static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003809shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003810{
3811 struct weston_compositor *compositor = shell->compositor;
3812 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003813 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003814
3815 surface = weston_surface_create(compositor);
3816 if (!surface)
3817 return NULL;
3818
Jason Ekstranda7af7042013-10-12 22:38:11 -05003819 view = weston_view_create(surface);
3820 if (!view) {
3821 weston_surface_destroy(surface);
3822 return NULL;
3823 }
3824
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003825 weston_surface_set_size(surface, 8192, 8192);
3826 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003827 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003828 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3829 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003830 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003831 surface->is_mapped = true;
3832 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003833
Jason Ekstranda7af7042013-10-12 22:38:11 -05003834 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003835}
3836
3837static void
3838shell_fade(struct desktop_shell *shell, enum fade_type type)
3839{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003840 float tint;
3841
3842 switch (type) {
3843 case FADE_IN:
3844 tint = 0.0;
3845 break;
3846 case FADE_OUT:
3847 tint = 1.0;
3848 break;
3849 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003850 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003851 return;
3852 }
3853
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003854 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003855
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003856 if (shell->fade.view == NULL) {
3857 shell->fade.view = shell_fade_create_surface(shell);
3858 if (!shell->fade.view)
3859 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003860
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003861 shell->fade.view->alpha = 1.0 - tint;
3862 weston_view_update_transform(shell->fade.view);
3863 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003864
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003865 if (shell->fade.view->output == NULL) {
3866 /* If the black view gets a NULL output, we lost the
3867 * last output and we'll just cancel the fade. This
3868 * happens when you close the last window under the
3869 * X11 or Wayland backends. */
3870 shell->locked = false;
3871 weston_surface_destroy(shell->fade.view->surface);
3872 shell->fade.view = NULL;
3873 } else if (shell->fade.animation) {
3874 weston_fade_update(shell->fade.animation, tint);
3875 } else {
3876 shell->fade.animation =
3877 weston_fade_run(shell->fade.view,
3878 1.0 - tint, tint, 300.0,
3879 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003880 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003881}
3882
3883static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003884do_shell_fade_startup(void *data)
3885{
3886 struct desktop_shell *shell = data;
3887
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003888 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003889 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003890 } else {
3891 weston_log("desktop shell: "
3892 "unexpected fade-in animation type %d\n",
3893 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003894 weston_surface_destroy(shell->fade.view->surface);
3895 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003896 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003897}
3898
3899static void
3900shell_fade_startup(struct desktop_shell *shell)
3901{
3902 struct wl_event_loop *loop;
3903
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003904 if (!shell->fade.startup_timer)
3905 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003906
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003907 wl_event_source_remove(shell->fade.startup_timer);
3908 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003909
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003910 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3911 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003912}
3913
3914static int
3915fade_startup_timeout(void *data)
3916{
3917 struct desktop_shell *shell = data;
3918
3919 shell_fade_startup(shell);
3920 return 0;
3921}
3922
3923static void
3924shell_fade_init(struct desktop_shell *shell)
3925{
3926 /* Make compositor output all black, and wait for the desktop-shell
3927 * client to signal it is ready, then fade in. The timer triggers a
3928 * fade-in, in case the desktop-shell client takes too long.
3929 */
3930
3931 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003932
3933 if (shell->fade.view != NULL) {
3934 weston_log("%s: warning: fade surface already exists\n",
3935 __func__);
3936 return;
3937 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003938
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003939 if (shell->startup_animation_type == ANIMATION_NONE)
3940 return;
3941
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003942 shell->fade.view = shell_fade_create_surface(shell);
3943 if (!shell->fade.view)
3944 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003945
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003946 weston_view_update_transform(shell->fade.view);
3947 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003948
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003949 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3950 shell->fade.startup_timer =
3951 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3952 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003953}
3954
3955static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003956idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003957{
3958 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003959 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003960
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003961 struct weston_seat *seat;
3962
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003963 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3964 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003965
3966 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003967 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003968}
3969
3970static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003971wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003972{
3973 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003974 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003975
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003976 unlock(shell);
3977}
3978
3979static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003980transform_handler(struct wl_listener *listener, void *data)
3981{
3982 struct weston_surface *surface = data;
3983 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003984 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003985 int x, y;
3986
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003987 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003988 return;
3989
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003990 api = shsurf->shell->xwayland_surface_api;
3991 if (!api) {
3992 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
3993 shsurf->shell->xwayland_surface_api = api;
3994 }
3995
3996 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003997 return;
3998
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003999 if (!weston_view_is_mapped(shsurf->view))
4000 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004001
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004002 x = shsurf->view->geometry.x;
4003 y = shsurf->view->geometry.y;
4004
4005 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004006}
4007
4008static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004009center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004010{
Giulio Camuffob8366642013-04-25 13:57:46 +03004011 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004012 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004013
Jason Ekstranda7af7042013-10-12 22:38:11 -05004014 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004015
4016 x = output->x + (output->width - width) / 2 - surf_x / 2;
4017 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004018
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004019 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004020}
4021
4022static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004023weston_view_set_initial_position(struct weston_view *view,
4024 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004025{
4026 struct weston_compositor *compositor = shell->compositor;
4027 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004028 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004029 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004030 struct weston_output *output, *target_output = NULL;
4031 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004032 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004033
4034 /* As a heuristic place the new window on the same output as the
4035 * pointer. Falling back to the output containing 0, 0.
4036 *
4037 * TODO: Do something clever for touch too?
4038 */
4039 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004040 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4041
4042 if (pointer) {
4043 ix = wl_fixed_to_int(pointer->x);
4044 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004045 break;
4046 }
4047 }
4048
4049 wl_list_for_each(output, &compositor->output_list, link) {
4050 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4051 target_output = output;
4052 break;
4053 }
4054 }
4055
4056 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 weston_view_set_position(view, 10 + random() % 400,
4058 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004059 return;
4060 }
4061
4062 /* Valid range within output where the surface will still be onscreen.
4063 * If this is negative it means that the surface is bigger than
4064 * output.
4065 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004066 get_output_work_area(shell, target_output, &area);
4067
Derek Foremanf814c5d2015-04-15 12:23:54 -05004068 x = area.x;
4069 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004070 range_x = area.width - view->surface->width;
4071 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004072
Rob Bradford4cb88c72012-08-13 15:18:44 +01004073 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004074 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004075
4076 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004077 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004078
Jason Ekstranda7af7042013-10-12 22:38:11 -05004079 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004080}
4081
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004082static bool
4083check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4084{
4085 struct timespec now;
4086
4087 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4088 return false;
4089
4090 /*
4091 * If the shell helper client dies before the session has been
4092 * up for roughly 30 seconds, better just make Weston shut down,
4093 * because the user likely has no way to interact with the desktop
4094 * anyway.
4095 */
4096 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4097 weston_log("Error: %s apparently cannot run at all.\n",
4098 shell->client);
4099 weston_log_continue(STAMP_SPACE "Quitting...");
4100 wl_display_terminate(shell->compositor->wl_display);
4101
4102 return true;
4103 }
4104
4105 return false;
4106}
4107
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004108static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004109
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004110static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004111respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004112{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004113 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004114
4115 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4116 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004117 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004118 shell->child.deathstamp = time;
4119 shell->child.deathcount = 0;
4120 }
4121
4122 shell->child.deathcount++;
4123 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004124 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004125 return;
4126 }
4127
Pekka Paalanen826dc142014-08-27 12:11:53 +03004128 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004129 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004130}
4131
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004132static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004133desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4134{
4135 struct desktop_shell *shell;
4136
4137 shell = container_of(listener, struct desktop_shell,
4138 child.client_destroy_listener);
4139
Pekka Paalanen826dc142014-08-27 12:11:53 +03004140 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004141 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004142 /*
4143 * unbind_desktop_shell() will reset shell->child.desktop_shell
4144 * before the respawned process has a chance to create a new
4145 * desktop_shell object, because we are being called from the
4146 * wl_client destructor which destroys all wl_resources before
4147 * returning.
4148 */
4149
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004150 if (!check_desktop_shell_crash_too_early(shell))
4151 respawn_desktop_shell_process(shell);
4152
Pekka Paalanen826dc142014-08-27 12:11:53 +03004153 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004154}
4155
4156static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004157launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004158{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004159 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004160
Pekka Paalanen826dc142014-08-27 12:11:53 +03004161 shell->child.client = weston_client_start(shell->compositor,
4162 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004163
Arnaud Vrac42631192014-08-25 20:56:46 +02004164 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004165 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004166 return;
4167 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004168
4169 shell->child.client_destroy_listener.notify =
4170 desktop_shell_client_destroy;
4171 wl_client_add_destroy_listener(shell->child.client,
4172 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004173}
4174
4175static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004176unbind_desktop_shell(struct wl_resource *resource)
4177{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004178 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004179
4180 if (shell->locked)
4181 resume_desktop(shell);
4182
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004183 shell->child.desktop_shell = NULL;
4184 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004185}
4186
4187static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004188bind_desktop_shell(struct wl_client *client,
4189 void *data, uint32_t version, uint32_t id)
4190{
Tiago Vignattibe143262012-04-16 17:31:41 +03004191 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004192 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004193
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004194 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4195 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004196
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004197 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004198 wl_resource_set_implementation(resource,
4199 &desktop_shell_implementation,
4200 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004201 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004202 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004203 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004204
4205 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4206 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004207}
4208
Kristian Høgsberg07045392012-02-19 18:52:44 -05004209struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004210 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004211 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004212 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004213 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004214 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004215};
4216
4217static void
4218switcher_next(struct switcher *switcher)
4219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004220 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004221 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004222 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004223 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004224
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004225 /* temporary re-display minimized surfaces */
4226 struct weston_view *tmp;
4227 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004228 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4229 weston_layer_entry_remove(&view->layer_link);
4230 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004231 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4232 *minimized = view;
4233 }
4234
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004235 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004236 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004237 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004238 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004239 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004240 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004241 next = view;
4242 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 view->alpha = 0.25;
4244 weston_view_geometry_dirty(view);
4245 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004246 }
Alex Wu1659daa2012-04-01 20:13:09 +08004247
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004248 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 view->alpha = 0.25;
4250 weston_view_geometry_dirty(view);
4251 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004252 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004253 }
4254
4255 if (next == NULL)
4256 next = first;
4257
Alex Wu07b26062012-03-12 16:06:01 +08004258 if (next == NULL)
4259 return;
4260
Kristian Høgsberg07045392012-02-19 18:52:44 -05004261 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004262 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004263
4264 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004265 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004266 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004267
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004268 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004269 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004271}
4272
4273static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004274switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004275{
4276 struct switcher *switcher =
4277 container_of(listener, struct switcher, listener);
4278
4279 switcher_next(switcher);
4280}
4281
4282static void
Daniel Stone351eb612012-05-31 15:27:47 -04004283switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004284{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004285 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004286 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004287 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004288
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004289 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004290 if (is_focus_view(view))
4291 continue;
4292
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 view->alpha = 1.0;
4294 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004295 }
4296
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004297 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004298 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004299 keyboard->seat,
4300 WESTON_ACTIVATE_FLAG_CONFIGURE);
4301 }
4302
Kristian Høgsberg07045392012-02-19 18:52:44 -05004303 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004304 weston_keyboard_end_grab(keyboard);
4305 if (keyboard->input_method_resource)
4306 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004307
4308 /* re-hide surfaces that were temporary shown during the switch */
4309 struct weston_view **minimized;
4310 wl_array_for_each(minimized, &switcher->minimized_array) {
4311 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004312 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004313 weston_layer_entry_remove(&(*minimized)->layer_link);
4314 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004315 weston_view_damage_below(*minimized);
4316 }
4317 }
4318 wl_array_release(&switcher->minimized_array);
4319
Kristian Høgsberg07045392012-02-19 18:52:44 -05004320 free(switcher);
4321}
4322
4323static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004324switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004325 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004326{
4327 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004328 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004329
Daniel Stonec9785ea2012-05-30 16:31:52 +01004330 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004331 switcher_next(switcher);
4332}
4333
4334static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004335switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004336 uint32_t mods_depressed, uint32_t mods_latched,
4337 uint32_t mods_locked, uint32_t group)
4338{
4339 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004340 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004341
Daniel Stone351eb612012-05-31 15:27:47 -04004342 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4343 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004344}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004345
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004346static void
4347switcher_cancel(struct weston_keyboard_grab *grab)
4348{
4349 struct switcher *switcher = container_of(grab, struct switcher, grab);
4350
4351 switcher_destroy(switcher);
4352}
4353
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004354static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004355 switcher_key,
4356 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004357 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004358};
4359
4360static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004361switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4362 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004363{
Tiago Vignattibe143262012-04-16 17:31:41 +03004364 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004365 struct switcher *switcher;
4366
4367 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004368 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004369 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004370 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004371 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004372 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004374 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004375 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004376 weston_keyboard_start_grab(keyboard, &switcher->grab);
4377 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004378 switcher_next(switcher);
4379}
4380
Pekka Paalanen3c647232011-12-22 13:43:43 +02004381static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004382backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4383 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004384{
4385 struct weston_compositor *compositor = data;
4386 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004387 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004388
4389 /* TODO: we're limiting to simple use cases, where we assume just
4390 * control on the primary display. We'd have to extend later if we
4391 * ever get support for setting backlights on random desktop LCD
4392 * panels though */
4393 output = get_default_output(compositor);
4394 if (!output)
4395 return;
4396
4397 if (!output->set_backlight)
4398 return;
4399
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004400 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4401 backlight_new = output->backlight_current - 25;
4402 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4403 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004404
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004405 if (backlight_new < 5)
4406 backlight_new = 5;
4407 if (backlight_new > 255)
4408 backlight_new = 255;
4409
4410 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004411 output->set_backlight(output, output->backlight_current);
4412}
4413
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004414static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004415force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4416 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004417{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004418 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004419 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004420 struct desktop_shell *shell = data;
4421 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004422 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004423
Derek Foreman8ae2db52015-07-15 13:00:36 -05004424 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004425 if (!focus_surface)
4426 return;
4427
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004428 wl_signal_emit(&compositor->kill_signal, focus_surface);
4429
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004430 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004431 wl_client_get_credentials(client, &pid, NULL, NULL);
4432
4433 /* Skip clients that we launched ourselves (the credentials of
4434 * the socketpair is ours) */
4435 if (pid == getpid())
4436 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004437
Daniel Stone325fc2d2012-05-30 16:31:58 +01004438 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004439}
4440
4441static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004442workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004443 uint32_t key, void *data)
4444{
4445 struct desktop_shell *shell = data;
4446 unsigned int new_index = shell->workspaces.current;
4447
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004448 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004449 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004450 if (new_index != 0)
4451 new_index--;
4452
4453 change_workspace(shell, new_index);
4454}
4455
4456static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004457workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004458 uint32_t key, void *data)
4459{
4460 struct desktop_shell *shell = data;
4461 unsigned int new_index = shell->workspaces.current;
4462
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004463 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004464 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004465 if (new_index < shell->workspaces.num - 1)
4466 new_index++;
4467
4468 change_workspace(shell, new_index);
4469}
4470
4471static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004472workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004473 uint32_t key, void *data)
4474{
4475 struct desktop_shell *shell = data;
4476 unsigned int new_index;
4477
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004478 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004479 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004480 new_index = key - KEY_F1;
4481 if (new_index >= shell->workspaces.num)
4482 new_index = shell->workspaces.num - 1;
4483
4484 change_workspace(shell, new_index);
4485}
4486
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004487static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004488workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4489 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004490{
4491 struct desktop_shell *shell = data;
4492 unsigned int new_index = shell->workspaces.current;
4493
4494 if (shell->locked)
4495 return;
4496
4497 if (new_index != 0)
4498 new_index--;
4499
Derek Foreman8ae2db52015-07-15 13:00:36 -05004500 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004501}
4502
4503static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004504workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4505 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004506{
4507 struct desktop_shell *shell = data;
4508 unsigned int new_index = shell->workspaces.current;
4509
4510 if (shell->locked)
4511 return;
4512
4513 if (new_index < shell->workspaces.num - 1)
4514 new_index++;
4515
Derek Foreman8ae2db52015-07-15 13:00:36 -05004516 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004517}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004518
4519static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004520shell_reposition_view_on_output_destroy(struct weston_view *view)
4521{
4522 struct weston_output *output, *first_output;
4523 struct weston_compositor *ec = view->surface->compositor;
4524 struct shell_surface *shsurf;
4525 float x, y;
4526 int visible;
4527
4528 x = view->geometry.x;
4529 y = view->geometry.y;
4530
4531 /* At this point the destroyed output is not in the list anymore.
4532 * If the view is still visible somewhere, we leave where it is,
4533 * otherwise, move it to the first output. */
4534 visible = 0;
4535 wl_list_for_each(output, &ec->output_list, link) {
4536 if (pixman_region32_contains_point(&output->region,
4537 x, y, NULL)) {
4538 visible = 1;
4539 break;
4540 }
4541 }
4542
4543 if (!visible) {
4544 first_output = container_of(ec->output_list.next,
4545 struct weston_output, link);
4546
4547 x = first_output->x + first_output->width / 4;
4548 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004549
4550 weston_view_set_position(view, x, y);
4551 } else {
4552 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004553 }
4554
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004555
4556 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004557 if (!shsurf)
4558 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004559
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004560 shsurf->saved_position_valid = false;
4561 set_maximized(shsurf, false);
4562 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004563}
4564
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004565void
4566shell_for_each_layer(struct desktop_shell *shell,
4567 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004568{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004569 struct workspace **ws;
4570
4571 func(shell, &shell->fullscreen_layer, data);
4572 func(shell, &shell->panel_layer, data);
4573 func(shell, &shell->background_layer, data);
4574 func(shell, &shell->lock_layer, data);
4575 func(shell, &shell->input_panel_layer, data);
4576
4577 wl_array_for_each(ws, &shell->workspaces.array)
4578 func(shell, &(*ws)->layer, data);
4579}
4580
4581static void
4582shell_output_destroy_move_layer(struct desktop_shell *shell,
4583 struct weston_layer *layer,
4584 void *data)
4585{
4586 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004587 struct weston_view *view;
4588
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004589 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004590 if (view->output != output)
4591 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004592
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004593 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004594 }
4595}
4596
4597static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004598handle_output_destroy(struct wl_listener *listener, void *data)
4599{
4600 struct shell_output *output_listener =
4601 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004602 struct weston_output *output = output_listener->output;
4603 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004604
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004605 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004606
Quentin Glidic561201c2016-08-20 18:19:51 +02004607 wl_list_remove(&output_listener->panel_surface_listener.link);
4608 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004609 wl_list_remove(&output_listener->destroy_listener.link);
4610 wl_list_remove(&output_listener->link);
4611 free(output_listener);
4612}
4613
4614static void
David Fort50d962f2016-06-09 17:55:52 +02004615shell_resize_surface_to_output(struct desktop_shell *shell,
4616 struct weston_surface *surface,
4617 const struct weston_output *output)
4618{
4619 if (!surface)
4620 return;
4621
4622 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4623 surface->resource,
4624 output->width,
4625 output->height);
4626}
4627
4628
4629static void
4630handle_output_resized(struct wl_listener *listener, void *data)
4631{
4632 struct desktop_shell *shell =
4633 container_of(listener, struct desktop_shell, resized_listener);
4634 struct weston_output *output = (struct weston_output *)data;
4635 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4636
4637 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4638 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4639}
4640
4641static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004642create_shell_output(struct desktop_shell *shell,
4643 struct weston_output *output)
4644{
4645 struct shell_output *shell_output;
4646
4647 shell_output = zalloc(sizeof *shell_output);
4648 if (shell_output == NULL)
4649 return;
4650
4651 shell_output->output = output;
4652 shell_output->shell = shell;
4653 shell_output->destroy_listener.notify = handle_output_destroy;
4654 wl_signal_add(&output->destroy_signal,
4655 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004656 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004657}
4658
4659static void
4660handle_output_create(struct wl_listener *listener, void *data)
4661{
4662 struct desktop_shell *shell =
4663 container_of(listener, struct desktop_shell, output_create_listener);
4664 struct weston_output *output = (struct weston_output *)data;
4665
4666 create_shell_output(shell, output);
4667}
4668
4669static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004670handle_output_move_layer(struct desktop_shell *shell,
4671 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004672{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004673 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004674 struct weston_view *view;
4675 float x, y;
4676
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004677 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004678 if (view->output != output)
4679 continue;
4680
4681 x = view->geometry.x + output->move_x;
4682 y = view->geometry.y + output->move_y;
4683 weston_view_set_position(view, x, y);
4684 }
4685}
4686
4687static void
4688handle_output_move(struct wl_listener *listener, void *data)
4689{
4690 struct desktop_shell *shell;
4691
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004692 shell = container_of(listener, struct desktop_shell,
4693 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004694
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004695 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004696}
4697
4698static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004699setup_output_destroy_handler(struct weston_compositor *ec,
4700 struct desktop_shell *shell)
4701{
4702 struct weston_output *output;
4703
4704 wl_list_init(&shell->output_list);
4705 wl_list_for_each(output, &ec->output_list, link)
4706 create_shell_output(shell, output);
4707
4708 shell->output_create_listener.notify = handle_output_create;
4709 wl_signal_add(&ec->output_created_signal,
4710 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004711
4712 shell->output_move_listener.notify = handle_output_move;
4713 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004714}
4715
4716static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004717shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004718{
Tiago Vignattibe143262012-04-16 17:31:41 +03004719 struct desktop_shell *shell =
4720 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004721 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004722 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004723
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004724 /* Force state to unlocked so we don't try to fade */
4725 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004726
4727 if (shell->child.client) {
4728 /* disable respawn */
4729 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004730 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004731 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004732
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004733 wl_list_remove(&shell->idle_listener.link);
4734 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004735 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004736
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004737 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004738 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004739
Xiong Zhang6b481422013-10-23 13:58:32 +08004740 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4741 wl_list_remove(&shell_output->destroy_listener.link);
4742 wl_list_remove(&shell_output->link);
4743 free(shell_output);
4744 }
4745
4746 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004747 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004748 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004749
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004750 wl_array_for_each(ws, &shell->workspaces.array)
4751 workspace_destroy(*ws);
4752 wl_array_release(&shell->workspaces.array);
4753
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004754 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004755 free(shell);
4756}
4757
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004758static void
4759shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4760{
4761 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004762 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004763
Bob Ham744e6532016-01-12 10:21:48 +00004764 if (shell->allow_zap)
4765 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4766 MODIFIER_CTRL | MODIFIER_ALT,
4767 terminate_binding, ec);
4768
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004769 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004770 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4771 click_to_activate_binding,
4772 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004773 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4774 click_to_activate_binding,
4775 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004776 weston_compositor_add_touch_binding(ec, 0,
4777 touch_to_activate_binding,
4778 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004779 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4780 MODIFIER_SUPER | MODIFIER_ALT,
4781 surface_opacity_binding, NULL);
4782 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4783 MODIFIER_SUPER, zoom_axis_binding,
4784 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004785 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4786 backlight_binding, ec);
4787 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4788 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004789
4790 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004791 if (shell->exposay_modifier)
4792 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4793 exposay_binding, shell);
4794
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004795 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004796 if (!mod)
4797 return;
4798
Daniel Stone325fc2d2012-05-30 16:31:58 +01004799 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4800 zoom_key_binding, NULL);
4801 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4802 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004803 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4804 maximize_binding, NULL);
4805 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4806 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004807 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4808 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004809 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004810 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004811 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004812 weston_compositor_add_button_binding(ec, BTN_LEFT,
4813 mod | MODIFIER_SHIFT,
4814 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004815
4816 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004817 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004818 rotate_binding, NULL);
4819
Daniel Stone325fc2d2012-05-30 16:31:58 +01004820 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4821 shell);
4822 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4823 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4825 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826 weston_compositor_add_key_binding(ec, KEY_K, mod,
4827 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004828 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4829 workspace_up_binding, shell);
4830 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4831 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004832 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4833 workspace_move_surface_up_binding,
4834 shell);
4835 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4836 workspace_move_surface_down_binding,
4837 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004838
4839 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4840 if (shell->workspaces.num > 1) {
4841 num_workspace_bindings = shell->workspaces.num;
4842 if (num_workspace_bindings > 6)
4843 num_workspace_bindings = 6;
4844 for (i = 0; i < num_workspace_bindings; i++)
4845 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4846 workspace_f_binding,
4847 shell);
4848 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004849
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004850 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004851}
4852
Jason Ekstrand024177c2014-04-21 19:42:58 -05004853static void
4854handle_seat_created(struct wl_listener *listener, void *data)
4855{
4856 struct weston_seat *seat = data;
4857
4858 create_shell_seat(seat);
4859}
4860
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004861WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004862module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004863 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004864{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004865 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004866 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004867 struct workspace **pws;
4868 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004869 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004870
Peter Huttererf3d62272013-08-08 11:57:05 +10004871 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004872 if (shell == NULL)
4873 return -1;
4874
Kristian Høgsberg75840622011-09-06 13:48:16 -04004875 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004876
4877 shell->destroy_listener.notify = shell_destroy;
4878 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004879 shell->idle_listener.notify = idle_handler;
4880 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4881 shell->wake_listener.notify = wake_handler;
4882 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004883 shell->transform_listener.notify = transform_handler;
4884 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004885
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004886 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4887 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004888 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4889 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004890 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004891
4892 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004893 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004894
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004895 if (input_panel_setup(shell) < 0)
4896 return -1;
4897
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004898 shell->text_backend = text_backend_init(ec);
4899 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004900 return -1;
4901
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004902 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004903
Daniel Stonedf8133b2013-11-19 11:37:14 +01004904 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4905 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4906
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004907 for (i = 0; i < shell->workspaces.num; i++) {
4908 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4909 if (pws == NULL)
4910 return -1;
4911
4912 *pws = workspace_create();
4913 if (*pws == NULL)
4914 return -1;
4915 }
4916 activate_workspace(shell, 0);
4917
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004918 weston_layer_init(&shell->minimized_layer, NULL);
4919
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004920 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004921 wl_list_init(&shell->workspaces.animation.link);
4922 shell->workspaces.animation.frame = animate_workspace_change_frame;
4923
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004924 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4925 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004926 return -1;
4927
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004928 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004929 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004930 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004931 return -1;
4932
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004933 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004934
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004935 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004936
Xiong Zhang6b481422013-10-23 13:58:32 +08004937 setup_output_destroy_handler(ec, shell);
4938
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004939 loop = wl_display_get_event_loop(ec->wl_display);
4940 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004941
Jason Ekstrand024177c2014-04-21 19:42:58 -05004942 wl_list_for_each(seat, &ec->seat_list, link)
4943 handle_seat_created(NULL, seat);
4944 shell->seat_create_listener.notify = handle_seat_created;
4945 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004946
David Fort50d962f2016-06-09 17:55:52 +02004947 shell->resized_listener.notify = handle_output_resized;
4948 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4949
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004950 screenshooter_create(ec);
4951
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004952 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004953
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004954 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004955
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004956 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4957
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004958 return 0;
4959}