blob: f80088f7642a4eb3cfb33ab217147b94f2c917bb [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
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200137 struct {
138 bool is_set;
139 int32_t x;
140 int32_t y;
141 } xwayland;
142
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500143 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500144
145 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200146};
147
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300148struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400149 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150 struct shell_surface *shsurf;
151 struct wl_listener shsurf_destroy_listener;
152};
153
Rusty Lynch1084da52013-08-15 09:10:08 -0700154struct shell_touch_grab {
155 struct weston_touch_grab grab;
156 struct shell_surface *shsurf;
157 struct wl_listener shsurf_destroy_listener;
158 struct weston_touch *touch;
159};
160
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300161struct weston_move_grab {
162 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100163 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500164 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165};
166
Rusty Lynch1084da52013-08-15 09:10:08 -0700167struct weston_touch_move_grab {
168 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800169 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700170 wl_fixed_t dx, dy;
171};
172
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300174 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500175 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200176 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200177 float x;
178 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200179 } center;
180};
181
Giulio Camuffo5085a752013-03-25 21:42:45 +0100182struct shell_seat {
183 struct weston_seat *seat;
184 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500185 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100186
Jason Ekstrand024177c2014-04-21 19:42:58 -0500187 struct wl_listener caps_changed_listener;
188 struct wl_listener pointer_focus_listener;
189 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100190};
191
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800192
Alex Wubd3354b2012-04-17 17:20:49 +0800193static struct desktop_shell *
194shell_surface_get_shell(struct shell_surface *shsurf);
195
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500196static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200197set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
198
199static void
Derek Foreman74de4692015-07-15 13:00:39 -0500200surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500201
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300202static void
203shell_fade_startup(struct desktop_shell *shell);
204
Philip Withnallbecb77e2013-11-25 18:01:30 +0000205static struct shell_seat *
206get_shell_seat(struct weston_seat *seat);
207
Jonny Lambd73c6942014-08-20 15:53:20 +0200208static void
209get_output_panel_size(struct desktop_shell *shell,
210 struct weston_output *output,
211 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700212
Philip Withnall648a4dd2013-11-25 18:01:44 +0000213static void
214shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
215
Pekka Paalanen8274d902014-08-06 19:36:51 +0300216static int
217shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
218{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200220 struct weston_desktop_surface *desktop_surface =
221 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200223 t = weston_desktop_surface_get_title(desktop_surface);
224 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300225
226 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200227 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300228 t ? " '" : "", t ?: "", t ? "'" : "",
229 c ? " of " : "", c ?: "");
230}
231
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500232static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400233destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234{
235 struct shell_grab *grab;
236
237 grab = container_of(listener, struct shell_grab,
238 shsurf_destroy_listener);
239
240 grab->shsurf = NULL;
241}
242
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800243struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500244get_default_view(struct weston_surface *surface)
245{
246 struct shell_surface *shsurf;
247 struct weston_view *view;
248
249 if (!surface || wl_list_empty(&surface->views))
250 return NULL;
251
252 shsurf = get_shell_surface(surface);
253 if (shsurf)
254 return shsurf->view;
255
256 wl_list_for_each(view, &surface->views, surface_link)
257 if (weston_view_is_mapped(view))
258 return view;
259
260 return container_of(surface->views.next, struct weston_view, surface_link);
261}
262
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400265 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300266 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400267 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800268 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270 struct desktop_shell *shell = shsurf->shell;
271
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200272 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400273
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274 grab->grab.interface = interface;
275 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400276 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500277 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400278 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700280 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400281 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400282 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800283 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400284 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 weston_pointer_set_focus(pointer,
286 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400287 wl_fixed_from_int(0),
288 wl_fixed_from_int(0));
289 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300290}
291
Jonny Lambd73c6942014-08-20 15:53:20 +0200292static void
Quentin Glidic581df062016-06-23 18:55:19 +0200293get_panel_size(struct desktop_shell *shell,
294 struct weston_view *view,
295 int *width,
296 int *height)
297{
298 float x1, y1;
299 float x2, y2;
300 weston_view_to_global_float(view, 0, 0, &x1, &y1);
301 weston_view_to_global_float(view,
302 view->surface->width,
303 view->surface->height,
304 &x2, &y2);
305 *width = (int)(x2 - x1);
306 *height = (int)(y2 - y1);
307}
308
309static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200310get_output_panel_size(struct desktop_shell *shell,
311 struct weston_output *output,
312 int *width,
313 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700314{
315 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200316
317 *width = 0;
318 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700319
320 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200321 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700322
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300323 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200324 if (view->surface->output == output) {
325 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200326 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700327 }
328 }
329
Jonny Lambd73c6942014-08-20 15:53:20 +0200330 /* the correct view wasn't found */
331}
332
333static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200334get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200335 struct weston_output *output,
336 pixman_rectangle32_t *area)
337{
338 int32_t panel_width = 0, panel_height = 0;
339
Derek Foremanf814c5d2015-04-15 12:23:54 -0500340 area->x = output->x;
341 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200342
343 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200344 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800345 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200346 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500347 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800348 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 area->width = output->width;
350 area->height = output->height - panel_height;
351 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800352 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500353 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800354 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200355 area->width = output->width - panel_width;
356 area->height = output->height;
357 break;
358 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700359}
360
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400361static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300362shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300363{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700364 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400365 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400367
368 if (grab->shsurf->resize_edges) {
369 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400370 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700371 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300372
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700373 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300374}
375
376static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700377shell_touch_grab_start(struct shell_touch_grab *grab,
378 const struct weston_touch_grab_interface *interface,
379 struct shell_surface *shsurf,
380 struct weston_touch *touch)
381{
382 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800383
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200384 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700385
Rusty Lynch1084da52013-08-15 09:10:08 -0700386 grab->grab.interface = interface;
387 grab->shsurf = shsurf;
388 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
389 wl_signal_add(&shsurf->destroy_signal,
390 &grab->shsurf_destroy_listener);
391
392 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700393 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700394
395 weston_touch_start_grab(touch, &grab->grab);
396 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500397 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700399}
400
401static void
402shell_touch_grab_end(struct shell_touch_grab *grab)
403{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700404 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700405 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700406 grab->shsurf->grabbed = 0;
407 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700408
409 weston_touch_end_grab(grab->touch);
410}
411
412static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800414 struct weston_output *output);
415
Daniel Stone496ca172012-05-30 16:31:42 +0100416static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300417get_modifier(char *modifier)
418{
419 if (!modifier)
420 return MODIFIER_SUPER;
421
422 if (!strcmp("ctrl", modifier))
423 return MODIFIER_CTRL;
424 else if (!strcmp("alt", modifier))
425 return MODIFIER_ALT;
426 else if (!strcmp("super", modifier))
427 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000428 else if (!strcmp("none", modifier))
429 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300430 else
431 return MODIFIER_SUPER;
432}
433
Juan Zhaoe10d2792012-04-25 19:09:52 +0800434static enum animation_type
435get_animation_type(char *animation)
436{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800437 if (!animation)
438 return ANIMATION_NONE;
439
Juan Zhaoe10d2792012-04-25 19:09:52 +0800440 if (!strcmp("zoom", animation))
441 return ANIMATION_ZOOM;
442 else if (!strcmp("fade", animation))
443 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100444 else if (!strcmp("dim-layer", animation))
445 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800446 else
447 return ANIMATION_NONE;
448}
449
Alex Wu4539b082012-03-01 12:57:46 +0800450static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400451shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200452{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500454 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300455 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000456 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200457
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300458 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400459 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300460 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
461 WESTON_SHELL_CLIENT);
462 if (ret < 0)
463 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400464 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500465 "client", &s, client);
466 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100467 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000468
469 weston_config_section_get_bool(section,
470 "allow-zap", &allow_zap, true);
471 shell->allow_zap = allow_zap;
472
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100473 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400474 "binding-modifier", &s, "super");
475 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200476 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800477
478 weston_config_section_get_string(section,
479 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000480 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800481 free(s);
482
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400483 weston_config_section_get_string(section, "animation", &s, "none");
484 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200485 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200486 weston_config_section_get_string(section, "close-animation", &s, "fade");
487 shell->win_close_animation_type = get_animation_type(s);
488 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700489 weston_config_section_get_string(section,
490 "startup-animation", &s, "fade");
491 shell->startup_animation_type = get_animation_type(s);
492 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700493 if (shell->startup_animation_type == ANIMATION_ZOOM)
494 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100495 weston_config_section_get_string(section, "focus-animation", &s, "none");
496 shell->focus_animation_type = get_animation_type(s);
497 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400498 weston_config_section_get_uint(section, "num-workspaces",
499 &shell->workspaces.num,
500 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200501}
502
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800503struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100504get_default_output(struct weston_compositor *compositor)
505{
Armin Krezović10b06182016-06-23 11:59:30 +0200506 if (wl_list_empty(&compositor->output_list))
507 return NULL;
508
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100509 return container_of(compositor->output_list.next,
510 struct weston_output, link);
511}
512
Pekka Paalanen8274d902014-08-06 19:36:51 +0300513static int
514focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
515{
516 return snprintf(buf, len, "focus highlight effect for output %s",
517 surface->output->name);
518}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100519
520/* no-op func for checking focus surface */
521static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200522focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100523{
524}
525
526static struct focus_surface *
527get_focus_surface(struct weston_surface *surface)
528{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200529 if (surface->committed == focus_surface_committed)
530 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100531 else
532 return NULL;
533}
534
535static bool
536is_focus_surface (struct weston_surface *es)
537{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200538 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100539}
540
541static bool
542is_focus_view (struct weston_view *view)
543{
544 return is_focus_surface (view->surface);
545}
546
547static struct focus_surface *
548create_focus_surface(struct weston_compositor *ec,
549 struct weston_output *output)
550{
551 struct focus_surface *fsurf = NULL;
552 struct weston_surface *surface = NULL;
553
554 fsurf = malloc(sizeof *fsurf);
555 if (!fsurf)
556 return NULL;
557
558 fsurf->surface = weston_surface_create(ec);
559 surface = fsurf->surface;
560 if (surface == NULL) {
561 free(fsurf);
562 return NULL;
563 }
564
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200565 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100566 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200567 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200568 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300569 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100570
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800571 fsurf->view = weston_view_create(surface);
572 if (fsurf->view == NULL) {
573 weston_surface_destroy(surface);
574 free(fsurf);
575 return NULL;
576 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100577 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200578 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100579
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600580 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600581 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100582 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
583 pixman_region32_fini(&surface->opaque);
584 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
585 output->width, output->height);
586 pixman_region32_fini(&surface->input);
587 pixman_region32_init(&surface->input);
588
589 wl_list_init(&fsurf->workspace_transform.link);
590
591 return fsurf;
592}
593
594static void
595focus_surface_destroy(struct focus_surface *fsurf)
596{
597 weston_surface_destroy(fsurf->surface);
598 free(fsurf);
599}
600
601static void
602focus_animation_done(struct weston_view_animation *animation, void *data)
603{
604 struct workspace *ws = data;
605
606 ws->focus_animation = NULL;
607}
608
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200609static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200610focus_state_destroy(struct focus_state *state)
611{
612 wl_list_remove(&state->seat_destroy_listener.link);
613 wl_list_remove(&state->surface_destroy_listener.link);
614 free(state);
615}
616
617static void
618focus_state_seat_destroy(struct wl_listener *listener, void *data)
619{
620 struct focus_state *state = container_of(listener,
621 struct focus_state,
622 seat_destroy_listener);
623
624 wl_list_remove(&state->link);
625 focus_state_destroy(state);
626}
627
628static void
629focus_state_surface_destroy(struct wl_listener *listener, void *data)
630{
631 struct focus_state *state = container_of(listener,
632 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400633 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200634 struct weston_surface *main_surface;
635 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500636 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200637
Pekka Paalanen01388e22013-04-25 13:57:44 +0300638 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
639
Kristian Høgsberge3778222012-07-31 17:29:30 -0400640 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300641 wl_list_for_each(view,
642 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400644 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100645 if (is_focus_view(view))
646 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200647 if (!get_shell_surface(view->surface))
648 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400649
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200650 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400651 break;
652 }
653
Pekka Paalanen01388e22013-04-25 13:57:44 +0300654 /* if the focus was a sub-surface, activate its main surface */
655 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200656 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300657
Kristian Høgsberge3778222012-07-31 17:29:30 -0400658 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100659 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200660 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200661 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400662 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200663 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100664 if (state->ws->focus_animation)
665 weston_view_animation_destroy(state->ws->focus_animation);
666
667 state->ws->focus_animation = weston_fade_run(
668 state->ws->fsurf_front->view,
669 state->ws->fsurf_front->view->alpha, 0.0, 300,
670 focus_animation_done, state->ws);
671 }
672
Kristian Høgsberge3778222012-07-31 17:29:30 -0400673 wl_list_remove(&state->link);
674 focus_state_destroy(state);
675 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200676}
677
678static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200679focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
680 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200681{
Jonas Ådahl04769742012-06-13 00:01:24 +0200682 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200683
684 state = malloc(sizeof *state);
685 if (state == NULL)
686 return NULL;
687
Quentin Glidicfff39812016-08-15 10:56:52 +0200688 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100689 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400690 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200691 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400692 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200693
694 state->seat_destroy_listener.notify = focus_state_seat_destroy;
695 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400696 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200697 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400698 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200699
700 return state;
701}
702
Jonas Ådahl8538b222012-08-29 22:13:03 +0200703static struct focus_state *
704ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
705{
706 struct workspace *ws = get_current_workspace(shell);
707 struct focus_state *state;
708
709 wl_list_for_each(state, &ws->focus_list, link)
710 if (state->seat == seat)
711 break;
712
713 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200714 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200715
716 return state;
717}
718
Jonas Ådahl04769742012-06-13 00:01:24 +0200719static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800720focus_state_set_focus(struct focus_state *state,
721 struct weston_surface *surface)
722{
723 if (state->keyboard_focus) {
724 wl_list_remove(&state->surface_destroy_listener.link);
725 wl_list_init(&state->surface_destroy_listener.link);
726 }
727
728 state->keyboard_focus = surface;
729 if (surface)
730 wl_signal_add(&surface->destroy_signal,
731 &state->surface_destroy_listener);
732}
733
734static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400735restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200736{
737 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400738 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100739 struct wl_list pending_seat_list;
740 struct weston_seat *seat, *next_seat;
741
742 /* Temporarily steal the list of seats so that we can keep
743 * track of the seats we've already processed */
744 wl_list_init(&pending_seat_list);
745 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
746 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200747
748 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500749 struct weston_keyboard *keyboard =
750 weston_seat_get_keyboard(state->seat);
751
Neil Roberts4237f502014-04-09 16:33:31 +0100752 wl_list_remove(&state->seat->link);
753 wl_list_insert(&shell->compositor->seat_list,
754 &state->seat->link);
755
Derek Foreman1281a362015-07-31 16:55:32 -0500756 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800757 continue;
758
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400759 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200760
Derek Foreman1281a362015-07-31 16:55:32 -0500761 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200762 }
Neil Roberts4237f502014-04-09 16:33:31 +0100763
764 /* For any remaining seats that we don't have a focus state
765 * for we'll reset the keyboard focus to NULL */
766 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500767 struct weston_keyboard *keyboard =
768 weston_seat_get_keyboard(seat);
769
Neil Roberts4237f502014-04-09 16:33:31 +0100770 wl_list_insert(&shell->compositor->seat_list, &seat->link);
771
Derek Foreman1281a362015-07-31 16:55:32 -0500772 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100773 continue;
774
Derek Foreman1281a362015-07-31 16:55:32 -0500775 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100776 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200777}
778
779static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200780replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
781 struct weston_seat *seat)
782{
Derek Foreman1281a362015-07-31 16:55:32 -0500783 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200784 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200785
786 wl_list_for_each(state, &ws->focus_list, link) {
787 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500788 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789 return;
790 }
791 }
792}
793
794static void
795drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
796 struct weston_surface *surface)
797{
798 struct focus_state *state;
799
800 wl_list_for_each(state, &ws->focus_list, link)
801 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800802 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200803}
804
805static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100806animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
807 struct weston_view *from, struct weston_view *to)
808{
809 struct weston_output *output;
810 bool focus_surface_created = false;
811
812 /* FIXME: Only support dim animation using two layers */
813 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
814 return;
815
816 output = get_default_output(shell->compositor);
817 if (ws->fsurf_front == NULL && (from || to)) {
818 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800819 if (ws->fsurf_front == NULL)
820 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100821 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800822
823 ws->fsurf_back = create_focus_surface(shell->compositor, output);
824 if (ws->fsurf_back == NULL) {
825 focus_surface_destroy(ws->fsurf_front);
826 return;
827 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800829
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100830 focus_surface_created = true;
831 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300832 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
833 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 }
835
836 if (ws->focus_animation) {
837 weston_view_animation_destroy(ws->focus_animation);
838 ws->focus_animation = NULL;
839 }
840
841 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300842 weston_layer_entry_insert(&to->layer_link,
843 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300845 weston_layer_entry_insert(&ws->layer.view_list,
846 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100847
848 if (focus_surface_created) {
849 ws->focus_animation = weston_fade_run(
850 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200851 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100852 focus_animation_done, ws);
853 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300854 weston_layer_entry_insert(&from->layer_link,
855 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100856 ws->focus_animation = weston_stable_fade_run(
857 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200858 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 focus_animation_done, ws);
860 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_insert(&ws->layer.view_list,
862 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 ws->focus_animation = weston_stable_fade_run(
864 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200865 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 focus_animation_done, ws);
867 }
868}
869
870static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200871workspace_destroy(struct workspace *ws)
872{
Jonas Ådahl04769742012-06-13 00:01:24 +0200873 struct focus_state *state, *next;
874
875 wl_list_for_each_safe(state, next, &ws->focus_list, link)
876 focus_state_destroy(state);
877
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100878 if (ws->fsurf_front)
879 focus_surface_destroy(ws->fsurf_front);
880 if (ws->fsurf_back)
881 focus_surface_destroy(ws->fsurf_back);
882
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200883 free(ws);
884}
885
Jonas Ådahl04769742012-06-13 00:01:24 +0200886static void
887seat_destroyed(struct wl_listener *listener, void *data)
888{
889 struct weston_seat *seat = data;
890 struct focus_state *state, *next;
891 struct workspace *ws = container_of(listener,
892 struct workspace,
893 seat_destroyed_listener);
894
895 wl_list_for_each_safe(state, next, &ws->focus_list, link)
896 if (state->seat == seat)
897 wl_list_remove(&state->link);
898}
899
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200900static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100901workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200902{
903 struct workspace *ws = malloc(sizeof *ws);
904 if (ws == NULL)
905 return NULL;
906
Quentin Glidic82681572016-12-17 13:40:51 +0100907 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200908
Jonas Ådahl04769742012-06-13 00:01:24 +0200909 wl_list_init(&ws->focus_list);
910 wl_list_init(&ws->seat_destroyed_listener.link);
911 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100912 ws->fsurf_front = NULL;
913 ws->fsurf_back = NULL;
914 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200915
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200916 return ws;
917}
918
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200919static int
920workspace_is_empty(struct workspace *ws)
921{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300922 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200923}
924
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200925static struct workspace *
926get_workspace(struct desktop_shell *shell, unsigned int index)
927{
928 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200929 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930 pws += index;
931 return *pws;
932}
933
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800934struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200935get_current_workspace(struct desktop_shell *shell)
936{
937 return get_workspace(shell, shell->workspaces.current);
938}
939
940static void
941activate_workspace(struct desktop_shell *shell, unsigned int index)
942{
943 struct workspace *ws;
944
945 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100946 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200947
948 shell->workspaces.current = index;
949}
950
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200951static unsigned int
952get_output_height(struct weston_output *output)
953{
954 return abs(output->region.extents.y1 - output->region.extents.y2);
955}
956
957static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100958view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200959{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200960 struct weston_transform *transform;
961
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100962 if (is_focus_view(view)) {
963 struct focus_surface *fsurf = get_focus_surface(view->surface);
964 transform = &fsurf->workspace_transform;
965 } else {
966 struct shell_surface *shsurf = get_shell_surface(view->surface);
967 transform = &shsurf->workspace_transform;
968 }
969
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200970 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100972 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200973
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100974 weston_matrix_init(&transform->matrix);
975 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978}
979
980static void
981workspace_translate_out(struct workspace *ws, double fraction)
982{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984 unsigned int height;
985 double d;
986
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300987 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 d = height * fraction;
990
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100991 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200992 }
993}
994
995static void
996workspace_translate_in(struct workspace *ws, double fraction)
997{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999 unsigned int height;
1000 double d;
1001
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001002 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004
1005 if (fraction > 0)
1006 d = -(height - height * fraction);
1007 else
1008 d = height + height * fraction;
1009
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001010 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001011 }
1012}
1013
1014static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015reverse_workspace_change_animation(struct desktop_shell *shell,
1016 unsigned int index,
1017 struct workspace *from,
1018 struct workspace *to)
1019{
1020 shell->workspaces.current = index;
1021
1022 shell->workspaces.anim_to = to;
1023 shell->workspaces.anim_from = from;
1024 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1025 shell->workspaces.anim_timestamp = 0;
1026
Quentin Glidic82681572016-12-17 13:40:51 +01001027 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1028 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1029
Scott Moreau4272e632012-08-13 09:58:41 -06001030 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031}
1032
1033static void
1034workspace_deactivate_transforms(struct workspace *ws)
1035{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001039 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001040 if (is_focus_view(view)) {
1041 struct focus_surface *fsurf = get_focus_surface(view->surface);
1042 transform = &fsurf->workspace_transform;
1043 } else {
1044 struct shell_surface *shsurf = get_shell_surface(view->surface);
1045 transform = &shsurf->workspace_transform;
1046 }
1047
1048 if (!wl_list_empty(&transform->link)) {
1049 wl_list_remove(&transform->link);
1050 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001051 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053 }
1054}
1055
1056static void
1057finish_workspace_change_animation(struct desktop_shell *shell,
1058 struct workspace *from,
1059 struct workspace *to)
1060{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001061 struct weston_view *view;
1062
Scott Moreau4272e632012-08-13 09:58:41 -06001063 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001065 /* Views that extend past the bottom of the output are still
1066 * visible after the workspace animation ends but before its layer
1067 * is hidden. In that case, we need to damage below those views so
1068 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001069 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001070 weston_view_damage_below(view);
1071
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072 wl_list_remove(&shell->workspaces.animation.link);
1073 workspace_deactivate_transforms(from);
1074 workspace_deactivate_transforms(to);
1075 shell->workspaces.anim_to = NULL;
1076
Quentin Glidic82681572016-12-17 13:40:51 +01001077 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078}
1079
1080static void
1081animate_workspace_change_frame(struct weston_animation *animation,
1082 struct weston_output *output, uint32_t msecs)
1083{
1084 struct desktop_shell *shell =
1085 container_of(animation, struct desktop_shell,
1086 workspaces.animation);
1087 struct workspace *from = shell->workspaces.anim_from;
1088 struct workspace *to = shell->workspaces.anim_to;
1089 uint32_t t;
1090 double x, y;
1091
1092 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1093 finish_workspace_change_animation(shell, from, to);
1094 return;
1095 }
1096
1097 if (shell->workspaces.anim_timestamp == 0) {
1098 if (shell->workspaces.anim_current == 0.0)
1099 shell->workspaces.anim_timestamp = msecs;
1100 else
1101 shell->workspaces.anim_timestamp =
1102 msecs -
1103 /* Invers of movement function 'y' below. */
1104 (asin(1.0 - shell->workspaces.anim_current) *
1105 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1106 M_2_PI);
1107 }
1108
1109 t = msecs - shell->workspaces.anim_timestamp;
1110
1111 /*
1112 * x = [0, π/2]
1113 * y(x) = sin(x)
1114 */
1115 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1116 y = sin(x);
1117
1118 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001119 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120
1121 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1122 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1123 shell->workspaces.anim_current = y;
1124
Scott Moreau4272e632012-08-13 09:58:41 -06001125 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001127 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129}
1130
1131static void
1132animate_workspace_change(struct desktop_shell *shell,
1133 unsigned int index,
1134 struct workspace *from,
1135 struct workspace *to)
1136{
1137 struct weston_output *output;
1138
1139 int dir;
1140
1141 if (index > shell->workspaces.current)
1142 dir = -1;
1143 else
1144 dir = 1;
1145
1146 shell->workspaces.current = index;
1147
1148 shell->workspaces.anim_dir = dir;
1149 shell->workspaces.anim_from = from;
1150 shell->workspaces.anim_to = to;
1151 shell->workspaces.anim_current = 0.0;
1152 shell->workspaces.anim_timestamp = 0;
1153
1154 output = container_of(shell->compositor->output_list.next,
1155 struct weston_output, link);
1156 wl_list_insert(&output->animation_list,
1157 &shell->workspaces.animation.link);
1158
Quentin Glidic82681572016-12-17 13:40:51 +01001159 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1160 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161
1162 workspace_translate_in(to, 0);
1163
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001164 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001165
Scott Moreau4272e632012-08-13 09:58:41 -06001166 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167}
1168
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001170update_workspace(struct desktop_shell *shell, unsigned int index,
1171 struct workspace *from, struct workspace *to)
1172{
1173 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001174 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1175 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001176}
1177
1178static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001179change_workspace(struct desktop_shell *shell, unsigned int index)
1180{
1181 struct workspace *from;
1182 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001183 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184
1185 if (index == shell->workspaces.current)
1186 return;
1187
1188 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001189 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001190 return;
1191
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192 from = get_current_workspace(shell);
1193 to = get_workspace(shell, index);
1194
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195 if (shell->workspaces.anim_from == to &&
1196 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198 reverse_workspace_change_animation(shell, index, from, to);
1199 return;
1200 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 if (shell->workspaces.anim_to != NULL)
1203 finish_workspace_change_animation(shell,
1204 shell->workspaces.anim_from,
1205 shell->workspaces.anim_to);
1206
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001207 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001208
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001209 if (shell->focus_animation_type != ANIMATION_NONE) {
1210 wl_list_for_each(state, &from->focus_list, link)
1211 if (state->keyboard_focus)
1212 animate_focus_change(shell, from,
1213 get_default_view(state->keyboard_focus), NULL);
1214
1215 wl_list_for_each(state, &to->focus_list, link)
1216 if (state->keyboard_focus)
1217 animate_focus_change(shell, to,
1218 NULL, get_default_view(state->keyboard_focus));
1219 }
1220
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 if (workspace_is_empty(to) && workspace_is_empty(from))
1222 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001223 else
1224 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001225}
1226
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227static bool
1228workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1229{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001230 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 struct wl_list *e;
1232
1233 if (wl_list_empty(list))
1234 return false;
1235
1236 e = list->next;
1237
1238 if (e->next != list)
1239 return false;
1240
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001241 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242}
1243
1244static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001245surface_keyboard_focus_lost(struct weston_surface *surface)
1246{
1247 struct weston_compositor *compositor = surface->compositor;
1248 struct weston_seat *seat;
1249 struct weston_surface *focus;
1250
1251 wl_list_for_each(seat, &compositor->seat_list, link) {
1252 struct weston_keyboard *keyboard =
1253 weston_seat_get_keyboard(seat);
1254
1255 if (!keyboard)
1256 continue;
1257
1258 focus = weston_surface_get_main_surface(keyboard->focus);
1259 if (focus == surface)
1260 weston_keyboard_set_focus(keyboard, NULL);
1261 }
1262}
1263
1264static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001266 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 unsigned int index)
1268{
Derek Foreman1281a362015-07-31 16:55:32 -05001269 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001270 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 struct shell_surface *shsurf;
1273 struct workspace *from;
1274 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001275 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276
Derek Foreman1281a362015-07-31 16:55:32 -05001277 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 view = get_default_view(surface);
1279 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001280 index == shell->workspaces.current ||
1281 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282 return;
1283
1284 from = get_current_workspace(shell);
1285 to = get_workspace(shell, index);
1286
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001287 weston_layer_entry_remove(&view->layer_link);
1288 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289
Philip Withnall659163d2013-11-25 18:01:36 +00001290 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001291 if (shsurf != NULL)
1292 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001293
Jonas Ådahle9d22502012-08-29 22:13:01 +02001294 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 drop_focus_state(shell, from, surface);
1296
1297 if (shell->workspaces.anim_from == to &&
1298 shell->workspaces.anim_to == from) {
1299 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001300
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001301 return;
1302 }
1303
1304 if (shell->workspaces.anim_to != NULL)
1305 finish_workspace_change_animation(shell,
1306 shell->workspaces.anim_from,
1307 shell->workspaces.anim_to);
1308
1309 if (workspace_is_empty(from) &&
1310 workspace_has_only(to, surface))
1311 update_workspace(shell, index, from, to);
1312 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001313 if (shsurf != NULL &&
1314 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001315 wl_list_insert(&shell->workspaces.anim_sticky_list,
1316 &shsurf->workspace_transform.link);
1317
1318 animate_workspace_change(shell, index, from, to);
1319 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320
Jonas Ådahl8538b222012-08-29 22:13:03 +02001321 state = ensure_focus_state(shell, seat);
1322 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001323 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324}
1325
1326static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001327touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001328 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001329{
1330}
1331
1332static void
1333touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1334{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001335 struct weston_touch_move_grab *move =
1336 (struct weston_touch_move_grab *) container_of(
1337 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001338
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001339 if (touch_id == 0)
1340 move->active = 0;
1341
Jonas Ådahl9484b692013-12-02 22:05:03 +01001342 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001343 shell_touch_grab_end(&move->base);
1344 free(move);
1345 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001346}
1347
1348static void
1349touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001350 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001351{
1352 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1353 struct shell_surface *shsurf = move->base.shsurf;
1354 struct weston_surface *es;
1355 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1356 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1357
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001358 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001359 return;
1360
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001361 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001362
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001363 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001364
1365 weston_compositor_schedule_repaint(es->compositor);
1366}
1367
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001368static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001369touch_move_grab_frame(struct weston_touch_grab *grab)
1370{
1371}
1372
1373static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001374touch_move_grab_cancel(struct weston_touch_grab *grab)
1375{
1376 struct weston_touch_move_grab *move =
1377 (struct weston_touch_move_grab *) container_of(
1378 grab, struct shell_touch_grab, grab);
1379
1380 shell_touch_grab_end(&move->base);
1381 free(move);
1382}
1383
Rusty Lynch1084da52013-08-15 09:10:08 -07001384static const struct weston_touch_grab_interface touch_move_grab_interface = {
1385 touch_move_grab_down,
1386 touch_move_grab_up,
1387 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001388 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001389 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001390};
1391
1392static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001393surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001394{
1395 struct weston_touch_move_grab *move;
1396
1397 if (!shsurf)
1398 return -1;
1399
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001400 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1401 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001402 return 0;
1403
1404 move = malloc(sizeof *move);
1405 if (!move)
1406 return -1;
1407
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001408 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001410 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001412 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001413
1414 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001415 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001416
1417 return 0;
1418}
1419
1420static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001421noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001422{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001423}
1424
1425static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001426noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001427 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001428{
1429}
1430
1431static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001432noop_grab_axis_source(struct weston_pointer_grab *grab,
1433 uint32_t source)
1434{
1435}
1436
1437static void
1438noop_grab_frame(struct weston_pointer_grab *grab)
1439{
1440}
1441
1442static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001443constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1444{
1445 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001446 struct weston_surface *surface =
1447 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001448 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001449 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001450 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001451 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001452 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001453
1454 x = wl_fixed_to_int(pointer->x + move->dx);
1455 y = wl_fixed_to_int(pointer->y + move->dy);
1456
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001457 if (shsurf->shell->panel_position ==
1458 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001459 get_output_work_area(shsurf->shell, surface->output, &area);
1460 geometry =
1461 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001462
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001463 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001464 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001465 y = area.y + safety - geometry.height
1466 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001467
1468 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001469 y + geometry.y < area.y)
1470 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001471 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001472
1473 *cx = x;
1474 *cy = y;
1475}
1476
1477static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001478move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001479 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001480{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001481 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001482 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001483 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001485 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001486
Jonas Ådahld2510102014-10-05 21:39:14 +02001487 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001488 if (!shsurf)
1489 return;
1490
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001491 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1492
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001493 constrain_position(move, &cx, &cy);
1494
1495 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001496
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001497 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001498}
1499
1500static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001501move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001502 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001503{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001504 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1505 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001506 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001507 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001508
Daniel Stone4dbadb12012-05-30 16:31:51 +01001509 if (pointer->button_count == 0 &&
1510 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001511 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001512 free(grab);
1513 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001514}
1515
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001516static void
1517move_grab_cancel(struct weston_pointer_grab *grab)
1518{
1519 struct shell_grab *shell_grab =
1520 container_of(grab, struct shell_grab, grab);
1521
1522 shell_grab_end(shell_grab);
1523 free(grab);
1524}
1525
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001526static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001527 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001528 move_grab_motion,
1529 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001530 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001531 noop_grab_axis_source,
1532 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001533 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001534};
1535
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001536static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001537surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001538 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001539{
1540 struct weston_move_grab *move;
1541
1542 if (!shsurf)
1543 return -1;
1544
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001545 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001546 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1547 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548 return 0;
1549
1550 move = malloc(sizeof *move);
1551 if (!move)
1552 return -1;
1553
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001555 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001557 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001558 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001559
1560 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001561 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562
1563 return 0;
1564}
1565
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001566struct weston_resize_grab {
1567 struct shell_grab base;
1568 uint32_t edges;
1569 int32_t width, height;
1570};
1571
1572static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001573resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001574 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001575{
1576 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001577 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578 struct shell_surface *shsurf = resize->base.shsurf;
1579 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001580 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 wl_fixed_t from_x, from_y;
1582 wl_fixed_t to_x, to_y;
1583
Jonas Ådahld2510102014-10-05 21:39:14 +02001584 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001585
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001586 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587 return;
1588
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 weston_view_from_global_fixed(shsurf->view,
1590 pointer->grab_x, pointer->grab_y,
1591 &from_x, &from_y);
1592 weston_view_from_global_fixed(shsurf->view,
1593 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001594
1595 width = resize->width;
1596 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1597 width += wl_fixed_to_int(from_x - to_x);
1598 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1599 width += wl_fixed_to_int(to_x - from_x);
1600 }
1601
1602 height = resize->height;
1603 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1604 height += wl_fixed_to_int(from_y - to_y);
1605 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1606 height += wl_fixed_to_int(to_y - from_y);
1607 }
1608
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001609 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1610 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1611
1612 min_size.width = MAX(1, min_size.width);
1613 min_size.height = MAX(1, min_size.height);
1614
1615 if (width < min_size.width)
1616 width = min_size.width;
1617 else if (max_size.width > 0 && width > max_size.width)
1618 width = max_size.width;
1619 if (height < min_size.height)
1620 height = min_size.height;
1621 else if (max_size.width > 0 && width > max_size.width)
1622 width = max_size.width;
1623 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001624}
1625
1626static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001627resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001628 uint32_t time, uint32_t button, uint32_t state_w)
1629{
1630 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001631 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001632 enum wl_pointer_button_state state = state_w;
1633
1634 if (pointer->button_count == 0 &&
1635 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1636 shell_grab_end(&resize->base);
1637 free(grab);
1638 }
1639}
1640
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001641static void
1642resize_grab_cancel(struct weston_pointer_grab *grab)
1643{
1644 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1645
1646 shell_grab_end(&resize->base);
1647 free(grab);
1648}
1649
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001650static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651 noop_grab_focus,
1652 resize_grab_motion,
1653 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001654 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001655 noop_grab_axis_source,
1656 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001657 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001658};
1659
Giulio Camuffob8366642013-04-25 13:57:46 +03001660/*
1661 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001662 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001663static void
1664surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1665 int32_t *y, int32_t *w, int32_t *h) {
1666 pixman_region32_t region;
1667 pixman_box32_t *box;
1668 struct weston_subsurface *subsurface;
1669
1670 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 surface->width,
1672 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001673
1674 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1675 pixman_region32_union_rect(&region, &region,
1676 subsurface->position.x,
1677 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 subsurface->surface->width,
1679 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001680 }
1681
1682 box = pixman_region32_extents(&region);
1683 if (x)
1684 *x = box->x1;
1685 if (y)
1686 *y = box->y1;
1687 if (w)
1688 *w = box->x2 - box->x1;
1689 if (h)
1690 *h = box->y2 - box->y1;
1691
1692 pixman_region32_fini(&region);
1693}
1694
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695static int
1696surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001697 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001698{
1699 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001700 const unsigned resize_topbottom =
1701 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1702 const unsigned resize_leftright =
1703 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1704 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001705 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001706
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001707 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001708 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1709 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710 return 0;
1711
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001712 /* Check for invalid edge combinations. */
1713 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1714 (edges & resize_topbottom) == resize_topbottom ||
1715 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001716 return 0;
1717
1718 resize = malloc(sizeof *resize);
1719 if (!resize)
1720 return -1;
1721
1722 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001723
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001724 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1725 resize->width = geometry.width;
1726 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001727
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001728 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001729 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001730 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731
1732 return 0;
1733}
1734
1735static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001736busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001737{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001738 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001740 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 wl_fixed_t sx, sy;
1743
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 view = weston_compositor_pick_view(pointer->seat->compositor,
1745 pointer->x, pointer->y,
1746 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001747 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001749 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001750 shell_grab_end(grab);
1751 free(grab);
1752 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753}
1754
1755static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001756busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001757 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758{
Jonas Ådahld2510102014-10-05 21:39:14 +02001759 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001760}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001762static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001763busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001764 uint32_t time, uint32_t button, uint32_t state)
1765{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001766 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001767 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001768 struct weston_pointer *pointer = grab->grab.pointer;
1769 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001772 activate(shsurf->shell, shsurf->view, seat,
1773 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001774 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001775 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001776 activate(shsurf->shell, shsurf->view, seat,
1777 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001778 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001779 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001780}
1781
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001782static void
1783busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1784{
1785 struct shell_grab *grab = (struct shell_grab *) base;
1786
1787 shell_grab_end(grab);
1788 free(grab);
1789}
1790
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001791static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792 busy_cursor_grab_focus,
1793 busy_cursor_grab_motion,
1794 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001795 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001796 noop_grab_axis_source,
1797 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001798 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001799};
1800
1801static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802handle_pointer_focus(struct wl_listener *listener, void *data)
1803{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001804 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001806 struct shell_surface *shsurf;
1807 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001808
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810 return;
1811
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001812 shsurf = get_shell_surface(view->surface);
1813 if (!shsurf)
1814 return;
1815
1816 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1817
1818 if (shsurf->unresponsive)
1819 set_busy_cursor(shsurf, pointer);
1820 else
1821 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822}
1823
1824static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001825shell_surface_lose_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, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001829}
1830
1831static void
1832shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1833{
1834 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001835 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001836}
1837
1838static void
1839handle_keyboard_focus(struct wl_listener *listener, void *data)
1840{
1841 struct weston_keyboard *keyboard = data;
1842 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1843
1844 if (seat->focused_surface) {
1845 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1846 if (shsurf)
1847 shell_surface_lose_keyboard_focus(shsurf);
1848 }
1849
1850 seat->focused_surface = keyboard->focus;
1851
1852 if (seat->focused_surface) {
1853 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1854 if (shsurf)
1855 shell_surface_gain_keyboard_focus(shsurf);
1856 }
1857}
1858
Philip Withnall07926d92013-11-25 18:01:40 +00001859/* The surface will be inserted into the list immediately after the link
1860 * returned by this function (i.e. will be stacked immediately above the
1861 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001862static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001863shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1864{
1865 struct workspace *ws;
1866
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001867 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1868 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001869 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001870 }
1871
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001872 /* Move the surface to a normal workspace layer so that surfaces
1873 * which were previously fullscreen or transient are no longer
1874 * rendered on top. */
1875 ws = get_current_workspace(shsurf->shell);
1876 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001877}
1878
Philip Withnall648a4dd2013-11-25 18:01:44 +00001879static void
1880shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1881{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001882 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001883}
1884
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001885/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001886 * geometry to ensure the changes are redrawn.
1887 *
1888 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1889 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001890static void
1891shell_surface_update_layer(struct shell_surface *shsurf)
1892{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001893 struct weston_surface *surface =
1894 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001895 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001896
1897 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1898
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001899 if (new_layer_link == NULL)
1900 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001901 if (new_layer_link == &shsurf->view->layer_link)
1902 return;
1903
1904 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001905 weston_layer_entry_remove(&shsurf->view->layer_link);
1906 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001907 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001908 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001909
1910 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001911}
1912
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001913static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001914shell_surface_set_output(struct shell_surface *shsurf,
1915 struct weston_output *output)
1916{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001917 struct weston_surface *es =
1918 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001919
1920 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001921 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001922 if (output)
1923 shsurf->output = output;
1924 else if (es->output)
1925 shsurf->output = es->output;
1926 else
1927 shsurf->output = get_default_output(es->compositor);
1928}
1929
1930static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001931weston_view_set_initial_position(struct weston_view *view,
1932 struct desktop_shell *shell);
1933
1934static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001935unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001936{
Philip Withnallf85fe842013-11-25 18:01:37 +00001937 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001938 wl_list_remove(&shsurf->fullscreen.transform.link);
1939 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001940
Jason Ekstranda7af7042013-10-12 22:38:11 -05001941 if (shsurf->fullscreen.black_view)
1942 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1943 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001944
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001945 if (shsurf->saved_position_valid)
1946 weston_view_set_position(shsurf->view,
1947 shsurf->saved_x, shsurf->saved_y);
1948 else
1949 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001950 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001951
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001952 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001954 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001955 shsurf->saved_rotation_valid = false;
1956 }
Alex Wu4539b082012-03-01 12:57:46 +08001957}
1958
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001959static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001960unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001961{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001962 struct weston_surface *surface =
1963 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1964
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001965 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001966 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001967
1968 if (shsurf->saved_position_valid)
1969 weston_view_set_position(shsurf->view,
1970 shsurf->saved_x, shsurf->saved_y);
1971 else
1972 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001973 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001974
1975 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 wl_list_insert(&shsurf->view->geometry.transformation_list,
1977 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001978 shsurf->saved_rotation_valid = false;
1979 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001980}
1981
Philip Withnallbecb77e2013-11-25 18:01:30 +00001982static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001983set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001984{
1985 struct shell_surface *shsurf;
1986 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001987 struct weston_view *view;
1988
1989 view = get_default_view(surface);
1990 if (!view)
1991 return;
1992
1993 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1994
1995 shsurf = get_shell_surface(surface);
1996 current_ws = get_current_workspace(shsurf->shell);
1997
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001998 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001999 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002000
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002001 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002002 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002003
2004 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002005 weston_view_damage_below(view);
2006}
2007
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002008
Tiago Vignattibe143262012-04-16 17:31:41 +03002009static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002010shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002011{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002012 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002013}
2014
Pekka Paalanen8274d902014-08-06 19:36:51 +03002015static int
2016black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2017{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002018 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002019 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002020 int n;
2021 int rem;
2022 int ret;
2023
2024 n = snprintf(buf, len, "black background surface for ");
2025 if (n < 0)
2026 return n;
2027
2028 rem = (int)len - n;
2029 if (rem < 0)
2030 rem = 0;
2031
2032 if (fs_surface->get_label)
2033 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2034 else
2035 ret = snprintf(buf + n, rem, "<unknown>");
2036
2037 if (ret < 0)
2038 return n;
2039
2040 return n + ret;
2041}
2042
Alex Wu21858432012-04-01 20:13:08 +08002043static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002044black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002045
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002047create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002048 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002049 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002050{
2051 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002053
2054 surface = weston_surface_create(ec);
2055 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002056 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002057 return NULL;
2058 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 view = weston_view_create(surface);
2060 if (surface == NULL) {
2061 weston_log("no memory\n");
2062 weston_surface_destroy(surface);
2063 return NULL;
2064 }
Alex Wu4539b082012-03-01 12:57:46 +08002065
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002066 surface->committed = black_surface_committed;
2067 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002068 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002069 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002070 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002071 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002072 pixman_region32_fini(&surface->input);
2073 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002074
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002075 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002076 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077
2078 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002079}
2080
Philip Withnalled8f1a92013-11-25 18:01:43 +00002081static void
2082shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2083{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002084 struct weston_surface *surface =
2085 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002086 struct weston_output *output = shsurf->fullscreen_output;
2087
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002088 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002089
2090 if (!shsurf->fullscreen.black_view)
2091 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002092 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002093 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002094 output->x, output->y,
2095 output->width,
2096 output->height);
2097
2098 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002099 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2100 weston_layer_entry_insert(&shsurf->view->layer_link,
2101 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002102 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002103 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002104
Armin Krezović4663aca2016-06-30 06:04:29 +02002105 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002106 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002107}
2108
Alex Wu4539b082012-03-01 12:57:46 +08002109/* Create black surface and append it to the associated fullscreen surface.
2110 * Handle size dismatch and positioning according to the method. */
2111static void
2112shell_configure_fullscreen(struct shell_surface *shsurf)
2113{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002114 struct weston_surface *surface =
2115 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002116 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002117
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002118 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002119 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002120 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2121 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002122
Philip Withnalled8f1a92013-11-25 18:01:43 +00002123 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002124
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002125 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002126 &surf_width, &surf_height);
2127
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002128 if (surface->buffer_ref.buffer)
2129 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002130}
2131
Alex Wu4539b082012-03-01 12:57:46 +08002132static void
2133shell_map_fullscreen(struct shell_surface *shsurf)
2134{
Alex Wubd3354b2012-04-17 17:20:49 +08002135 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002136}
2137
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002138static struct weston_output *
2139get_focused_output(struct weston_compositor *compositor)
2140{
2141 struct weston_seat *seat;
2142 struct weston_output *output = NULL;
2143
2144 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002145 struct weston_touch *touch = weston_seat_get_touch(seat);
2146 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2147 struct weston_keyboard *keyboard =
2148 weston_seat_get_keyboard(seat);
2149
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002150 /* Priority has touch focus, then pointer and
2151 * then keyboard focus. We should probably have
2152 * three for loops and check frist for touch,
2153 * then for pointer, etc. but unless somebody has some
2154 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002155 if (touch && touch->focus)
2156 output = touch->focus->output;
2157 else if (pointer && pointer->focus)
2158 output = pointer->focus->output;
2159 else if (keyboard && keyboard->focus)
2160 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002161
2162 if (output)
2163 break;
2164 }
2165
2166 return output;
2167}
2168
2169static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002170destroy_shell_seat(struct wl_listener *listener, void *data)
2171{
2172 struct shell_seat *shseat =
2173 container_of(listener,
2174 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002175
2176 wl_list_remove(&shseat->seat_destroy_listener.link);
2177 free(shseat);
2178}
2179
Jason Ekstrand024177c2014-04-21 19:42:58 -05002180static void
2181shell_seat_caps_changed(struct wl_listener *listener, void *data)
2182{
Derek Foreman1281a362015-07-31 16:55:32 -05002183 struct weston_keyboard *keyboard;
2184 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002185 struct shell_seat *seat;
2186
2187 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002188 keyboard = weston_seat_get_keyboard(seat->seat);
2189 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002190
Derek Foreman1281a362015-07-31 16:55:32 -05002191 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002192 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002193 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002194 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002195 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002196 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 wl_list_init(&seat->keyboard_focus_listener.link);
2198 }
2199
Derek Foreman1281a362015-07-31 16:55:32 -05002200 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002201 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002202 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002203 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002204 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002205 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002206 wl_list_init(&seat->pointer_focus_listener.link);
2207 }
2208}
2209
Giulio Camuffo5085a752013-03-25 21:42:45 +01002210static struct shell_seat *
2211create_shell_seat(struct weston_seat *seat)
2212{
2213 struct shell_seat *shseat;
2214
2215 shseat = calloc(1, sizeof *shseat);
2216 if (!shseat) {
2217 weston_log("no memory to allocate shell seat\n");
2218 return NULL;
2219 }
2220
2221 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002222
2223 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2224 wl_signal_add(&seat->destroy_signal,
2225 &shseat->seat_destroy_listener);
2226
Jason Ekstrand024177c2014-04-21 19:42:58 -05002227 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2228 wl_list_init(&shseat->keyboard_focus_listener.link);
2229
2230 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2231 wl_list_init(&shseat->pointer_focus_listener.link);
2232
2233 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2234 wl_signal_add(&seat->updated_caps_signal,
2235 &shseat->caps_changed_listener);
2236 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2237
Giulio Camuffo5085a752013-03-25 21:42:45 +01002238 return shseat;
2239}
2240
2241static struct shell_seat *
2242get_shell_seat(struct weston_seat *seat)
2243{
2244 struct wl_listener *listener;
2245
2246 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002247 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002248
2249 return container_of(listener,
2250 struct shell_seat, seat_destroy_listener);
2251}
2252
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002253static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002254fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002255{
2256 struct shell_surface *shsurf = data;
2257
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002258 weston_surface_destroy(shsurf->view->surface);
2259 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002260}
2261
2262static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002263fade_out_done(struct weston_view_animation *animation, void *data)
2264{
2265 struct shell_surface *shsurf = data;
2266 struct wl_event_loop *loop;
2267
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002268 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002269
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002270 if (weston_view_is_mapped(shsurf->view)) {
2271 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002272 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002273 }
2274}
2275
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002276struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002277get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002278{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002279 if (weston_surface_is_desktop_surface(surface)) {
2280 struct weston_desktop_surface *desktop_surface =
2281 weston_surface_get_desktop_surface(surface);
2282 return weston_desktop_surface_get_user_data(desktop_surface);
2283 }
2284 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002285}
2286
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002287/*
2288 * libweston-desktop
2289 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002290
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002291static void
2292desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2293 void *shell)
2294{
2295 struct weston_desktop_client *client =
2296 weston_desktop_surface_get_client(desktop_surface);
2297 struct wl_client *wl_client =
2298 weston_desktop_client_get_client(client);
2299 struct weston_view *view;
2300 struct shell_surface *shsurf;
2301 struct weston_surface *surface =
2302 weston_desktop_surface_get_surface(desktop_surface);
2303
2304 view = weston_desktop_surface_create_view(desktop_surface);
2305 if (!view)
2306 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002307
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002308 shsurf = calloc(1, sizeof *shsurf);
2309 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002310 if (wl_client)
2311 wl_client_post_no_memory(wl_client);
2312 else
2313 weston_log("no memory to allocate shell surface\n");
2314 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002315 }
2316
Pekka Paalanen8274d902014-08-06 19:36:51 +03002317 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002318
Tiago Vignattibc052c92012-04-19 16:18:18 +03002319 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002320 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002321 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002322 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002323 shsurf->desktop_surface = desktop_surface;
2324 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002325 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002326 wl_list_init(&shsurf->fullscreen.transform.link);
2327
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002328 shsurf->output = get_default_output(shsurf->shell->compositor);
2329
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002330 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002331
Pekka Paalanen460099f2012-01-20 16:48:25 +02002332 /* empty when not in use */
2333 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002334 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002335
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002336 wl_list_init(&shsurf->workspace_transform.link);
2337
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002338 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2339 weston_desktop_surface_set_activated(desktop_surface,
2340 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002341}
2342
Tiago Vignattibc052c92012-04-19 16:18:18 +03002343static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002344desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2345 void *shell)
2346{
2347 struct shell_surface *shsurf =
2348 weston_desktop_surface_get_user_data(desktop_surface);
2349 struct weston_surface *surface =
2350 weston_desktop_surface_get_surface(desktop_surface);
2351
2352 if (!shsurf)
2353 return;
2354
2355 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2356
2357 if (shsurf->fullscreen.black_view)
2358 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2359
2360 weston_surface_set_label_func(surface, NULL);
2361 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2362 shsurf->desktop_surface = NULL;
2363
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002364 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002365 if (weston_surface_is_mapped(surface) &&
2366 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2367 pixman_region32_fini(&surface->pending.input);
2368 pixman_region32_init(&surface->pending.input);
2369 pixman_region32_fini(&surface->input);
2370 pixman_region32_init(&surface->input);
2371 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2372 fade_out_done, shsurf);
2373 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002374 weston_view_destroy(shsurf->view);
2375 free(shsurf);
2376 }
2377}
2378
2379static void
2380set_maximized_position(struct desktop_shell *shell,
2381 struct shell_surface *shsurf)
2382{
2383 pixman_rectangle32_t area;
2384 struct weston_geometry geometry;
2385
2386 get_output_work_area(shell, shsurf->output, &area);
2387 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2388
2389 weston_view_set_position(shsurf->view,
2390 area.x - geometry.x,
2391 area.y - geometry.y);
2392}
2393
2394static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002395set_position_from_xwayland(struct shell_surface *shsurf)
2396{
2397 struct weston_geometry geometry;
2398 float x;
2399 float y;
2400
2401 assert(shsurf->xwayland.is_set);
2402
2403 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2404 x = shsurf->xwayland.x - geometry.x;
2405 y = shsurf->xwayland.y - geometry.y;
2406
2407 weston_view_set_position(shsurf->view, x, y);
2408
2409#ifdef WM_DEBUG
2410 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2411 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2412 geometry.x, geometry.y, x, y);
2413#endif
2414}
2415
2416static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002417map(struct desktop_shell *shell, struct shell_surface *shsurf,
2418 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002419{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002420 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002421 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2422 struct weston_compositor *compositor = shell->compositor;
2423 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002424
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002425 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002426 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002427 center_on_output(shsurf->view, shsurf->fullscreen_output);
2428 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002429 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002430 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002431 } else if (shsurf->xwayland.is_set) {
2432 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002433 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002434 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002435 }
2436
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002437 /* Surface stacking order, see also activate(). */
2438 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002439
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002440 weston_view_update_transform(shsurf->view);
2441 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002442 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002443 surface->output = shsurf->output;
2444 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002445 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002446
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002447 if (!shell->locked) {
2448 wl_list_for_each(seat, &compositor->seat_list, link)
2449 activate(shell, shsurf->view, seat,
2450 WESTON_ACTIVATE_FLAG_CONFIGURE);
2451 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002452
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002453 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002454 switch (shell->win_animation_type) {
2455 case ANIMATION_FADE:
2456 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2457 break;
2458 case ANIMATION_ZOOM:
2459 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2460 break;
2461 case ANIMATION_NONE:
2462 default:
2463 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002464 }
2465 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002466}
2467
2468static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002469desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2470 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002471{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002472 struct shell_surface *shsurf =
2473 weston_desktop_surface_get_user_data(desktop_surface);
2474 struct weston_surface *surface =
2475 weston_desktop_surface_get_surface(desktop_surface);
2476 struct weston_view *view = shsurf->view;
2477 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002478 bool was_fullscreen;
2479 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002480
2481 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002482 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002483
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002484 was_fullscreen = shsurf->state.fullscreen;
2485 was_maximized = shsurf->state.maximized;
2486
2487 shsurf->state.fullscreen =
2488 weston_desktop_surface_get_fullscreen(desktop_surface);
2489 shsurf->state.maximized =
2490 weston_desktop_surface_get_maximized(desktop_surface);
2491
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002492 if (!weston_surface_is_mapped(surface)) {
2493 map(shell, shsurf, sx, sy);
2494 surface->is_mapped = true;
2495 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2496 ++surface->ref_count;
2497 return;
2498 }
2499
2500 if (sx == 0 && sy == 0 &&
2501 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002502 shsurf->last_height == surface->height &&
2503 was_fullscreen == shsurf->state.fullscreen &&
2504 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002505 return;
2506
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002507 if (was_fullscreen)
2508 unset_fullscreen(shsurf);
2509 if (was_maximized)
2510 unset_maximized(shsurf);
2511
Quentin Glidic920cf042016-08-16 14:26:20 +02002512 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2513 !shsurf->saved_position_valid) {
2514 shsurf->saved_x = shsurf->view->geometry.x;
2515 shsurf->saved_y = shsurf->view->geometry.y;
2516 shsurf->saved_position_valid = true;
2517
2518 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2519 wl_list_remove(&shsurf->rotation.transform.link);
2520 wl_list_init(&shsurf->rotation.transform.link);
2521 weston_view_geometry_dirty(shsurf->view);
2522 shsurf->saved_rotation_valid = true;
2523 }
2524 }
2525
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002526 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002527 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002528 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002529 set_maximized_position(shell, shsurf);
2530 surface->output = shsurf->output;
2531 } else {
2532 float from_x, from_y;
2533 float to_x, to_y;
2534 float x, y;
2535
2536 if (shsurf->resize_edges) {
2537 sx = 0;
2538 sy = 0;
2539 }
2540
2541 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2542 sx = shsurf->last_width - surface->width;
2543 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2544 sy = shsurf->last_height - surface->height;
2545
2546 shsurf->last_width = surface->width;
2547 shsurf->last_height = surface->height;
2548
2549 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2550 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2551 x = shsurf->view->geometry.x + to_x - from_x;
2552 y = shsurf->view->geometry.y + to_y - from_y;
2553
2554 weston_view_set_position(shsurf->view, x, y);
2555 }
2556
2557 /* XXX: would a fullscreen surface need the same handling? */
2558 if (surface->output) {
2559 wl_list_for_each(view, &surface->views, surface_link)
2560 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002561 }
2562}
2563
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002564static void
2565set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2566 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002567{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002568 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2569 struct weston_surface *surface =
2570 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2571 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002572
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002573 if (fullscreen) {
2574 /* handle clients launching in fullscreen */
2575 if (output == NULL && !weston_surface_is_mapped(surface)) {
2576 /* Set the output to the one that has focus currently. */
2577 output = get_focused_output(surface->compositor);
2578 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002579
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002580 shell_surface_set_output(shsurf, output);
2581 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002582
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002583 width = shsurf->output->width;
2584 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 }
2586 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2587 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002588}
2589
2590static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002591desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2592 struct weston_seat *seat, uint32_t serial, void *shell)
2593{
2594 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2595 struct weston_touch *touch = weston_seat_get_touch(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->focus &&
2605 pointer->button_count > 0 &&
2606 pointer->grab_serial == serial) {
2607 focus = weston_surface_get_main_surface(pointer->focus->surface);
2608 if ((focus == surface) &&
2609 (surface_move(shsurf, pointer, true) < 0))
2610 wl_resource_post_no_memory(resource);
2611 } else if (touch &&
2612 touch->focus &&
2613 touch->grab_serial == serial) {
2614 focus = weston_surface_get_main_surface(touch->focus->surface);
2615 if ((focus == surface) &&
2616 (surface_touch_move(shsurf, touch) < 0))
2617 wl_resource_post_no_memory(resource);
2618 }
2619}
2620
2621static void
2622desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2623 struct weston_seat *seat, uint32_t serial,
2624 enum weston_desktop_surface_edge edges, void *shell)
2625{
2626 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2627 struct shell_surface *shsurf =
2628 weston_desktop_surface_get_user_data(desktop_surface);
2629 struct weston_surface *surface =
2630 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2631 struct wl_resource *resource = surface->resource;
2632 struct weston_surface *focus;
2633
2634 if (!pointer ||
2635 pointer->button_count == 0 ||
2636 pointer->grab_serial != serial ||
2637 pointer->focus == NULL)
2638 return;
2639
2640 focus = weston_surface_get_main_surface(pointer->focus->surface);
2641 if (focus != surface)
2642 return;
2643
2644 if (surface_resize(shsurf, pointer, edges) < 0)
2645 wl_resource_post_no_memory(resource);
2646}
2647
2648static void
2649desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2650 bool fullscreen,
2651 struct weston_output *output, void *shell)
2652{
2653 struct shell_surface *shsurf =
2654 weston_desktop_surface_get_user_data(desktop_surface);
2655
2656 set_fullscreen(shsurf, fullscreen, output);
2657}
2658
2659static void
2660set_maximized(struct shell_surface *shsurf, bool maximized)
2661{
2662 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2663 struct weston_surface *surface =
2664 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2665 int32_t width = 0, height = 0;
2666
2667 if (maximized) {
2668 struct weston_output *output;
2669 struct desktop_shell *shell;
2670 pixman_rectangle32_t area;
2671
2672 if (!weston_surface_is_mapped(surface))
2673 output = get_focused_output(surface->compositor);
2674 else
2675 output = surface->output;
2676
2677 shell_surface_set_output(shsurf, output);
2678
2679 shell = shell_surface_get_shell(shsurf);
2680 get_output_work_area(shell, shsurf->output, &area);
2681
2682 width = area.width;
2683 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002684 }
2685 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2686 weston_desktop_surface_set_size(desktop_surface, width, height);
2687}
2688
2689static void
2690desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2691 bool maximized, void *shell)
2692{
2693 struct shell_surface *shsurf =
2694 weston_desktop_surface_get_user_data(desktop_surface);
2695
2696 set_maximized(shsurf, maximized);
2697}
2698
2699static void
2700desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2701 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002702{
2703 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002704 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002705
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002706 /* apply compositor's own minimization logic (hide) */
2707 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002708}
2709
Rafael Antognollie2a34552013-12-03 15:35:45 -02002710static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002712{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002713 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002714
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002715 if (pointer->grab->interface == &busy_cursor_grab_interface)
2716 return;
2717
2718 grab = malloc(sizeof *grab);
2719 if (!grab)
2720 return;
2721
2722 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2723 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2724 /* Mark the shsurf as ungrabbed so that button binding is able
2725 * to move it. */
2726 shsurf->grabbed = 0;
2727}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002728
2729static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002730end_busy_cursor(struct weston_compositor *compositor,
2731 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002732{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002733 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002734 struct shell_grab *grab;
2735 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002736
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002737 wl_list_for_each(seat, &compositor->seat_list, link) {
2738 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2739 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002740
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002741 if (!pointer)
2742 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002743
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002744 if (pointer->grab->interface != &busy_cursor_grab_interface)
2745 continue;
2746
2747 grab = (struct shell_grab *) pointer->grab;
2748 shsurf = grab->shsurf;
2749 if (!shsurf)
2750 continue;
2751
2752 grab_client =
2753 weston_desktop_surface_get_client(shsurf->desktop_surface);
2754 if (grab_client == desktop_client) {
2755 shell_grab_end(grab);
2756 free(grab);
2757 }
2758 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002759}
2760
2761static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002762desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2763 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002764{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002765 struct shell_surface *shsurf =
2766 weston_desktop_surface_get_user_data(desktop_surface);
2767 bool *unresponsive = user_data;
2768
2769 shsurf->unresponsive = *unresponsive;
2770}
2771
2772static void
2773desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2774 void *shell_)
2775{
2776 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002777 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002778 struct weston_seat *seat;
2779 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002780
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002781 weston_desktop_client_for_each_surface(desktop_client,
2782 desktop_surface_set_unresponsive,
2783 &unresponsive);
2784
2785
2786 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2787 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2788 struct weston_desktop_client *grab_client;
2789
2790 if (!pointer || !pointer->focus)
2791 continue;
2792
2793 shsurf = get_shell_surface(pointer->focus->surface);
2794 if (!shsurf)
2795 continue;
2796
2797 grab_client =
2798 weston_desktop_surface_get_client(shsurf->desktop_surface);
2799 if (grab_client == desktop_client)
2800 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002801 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002802}
2803
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002804static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002805desktop_surface_pong(struct weston_desktop_client *desktop_client,
2806 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002807{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002808 struct desktop_shell *shell = shell_;
2809 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002810
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002811 weston_desktop_client_for_each_surface(desktop_client,
2812 desktop_surface_set_unresponsive,
2813 &unresponsive);
2814 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002815}
2816
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002817static void
2818desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2819 int32_t x, int32_t y, void *shell_)
2820{
2821 struct shell_surface *shsurf =
2822 weston_desktop_surface_get_user_data(surface);
2823
2824 shsurf->xwayland.x = x;
2825 shsurf->xwayland.y = y;
2826 shsurf->xwayland.is_set = true;
2827}
2828
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002829static const struct weston_desktop_api shell_desktop_api = {
2830 .struct_size = sizeof(struct weston_desktop_api),
2831 .surface_added = desktop_surface_added,
2832 .surface_removed = desktop_surface_removed,
2833 .committed = desktop_surface_committed,
2834 .move = desktop_surface_move,
2835 .resize = desktop_surface_resize,
2836 .fullscreen_requested = desktop_surface_fullscreen_requested,
2837 .maximized_requested = desktop_surface_maximized_requested,
2838 .minimized_requested = desktop_surface_minimized_requested,
2839 .ping_timeout = desktop_surface_ping_timeout,
2840 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002841 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002842};
2843
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002844/* ************************ *
2845 * end of libweston-desktop *
2846 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002847static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002848shell_fade(struct desktop_shell *shell, enum fade_type type);
2849
2850static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002851configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002852{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002854
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002855 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 if (v->output == ev->output && v != ev) {
2857 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002858 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002859 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002860 }
2861 }
2862
Quentin Glidice8bf9592016-06-23 18:55:20 +02002863 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002864 ev->surface->is_mapped = true;
2865 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002866
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002867 if (wl_list_empty(&ev->layer_link.link)) {
2868 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002869 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002870 }
2871}
2872
David Fort50d962f2016-06-09 17:55:52 +02002873
2874static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002875find_shell_output_from_weston_output(struct desktop_shell *shell,
2876 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002877{
2878 struct shell_output *shell_output;
2879
2880 wl_list_for_each(shell_output, &shell->output_list, link) {
2881 if (shell_output->output == output)
2882 return shell_output;
2883 }
2884
2885 return NULL;
2886}
2887
Pekka Paalanen8274d902014-08-06 19:36:51 +03002888static int
2889background_get_label(struct weston_surface *surface, char *buf, size_t len)
2890{
2891 return snprintf(buf, len, "background for output %s",
2892 surface->output->name);
2893}
2894
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002895static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002896background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002897{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002898 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002899 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002900
Jason Ekstranda7af7042013-10-12 22:38:11 -05002901 view = container_of(es->views.next, struct weston_view, surface_link);
2902
Quentin Glidice8bf9592016-06-23 18:55:20 +02002903 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002904}
2905
2906static void
David Fort50d962f2016-06-09 17:55:52 +02002907handle_background_surface_destroy(struct wl_listener *listener, void *data)
2908{
2909 struct shell_output *output =
2910 container_of(listener, struct shell_output, background_surface_listener);
2911
2912 weston_log("background surface gone\n");
2913 output->background_surface = NULL;
2914}
2915
2916static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002917desktop_shell_set_background(struct wl_client *client,
2918 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002919 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002920 struct wl_resource *surface_resource)
2921{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002922 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002923 struct weston_surface *surface =
2924 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002925 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002926 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002927
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002928 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002929 wl_resource_post_error(surface_resource,
2930 WL_DISPLAY_ERROR_INVALID_OBJECT,
2931 "surface role already assigned");
2932 return;
2933 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002934
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2936 weston_view_destroy(view);
2937 view = weston_view_create(surface);
2938
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002939 surface->committed = background_committed;
2940 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002941 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002942 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002943 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002944 weston_desktop_shell_send_configure(resource, 0,
2945 surface_resource,
2946 surface->output->width,
2947 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002948
2949 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2950 sh_output->background_surface = surface;
2951
2952 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2953 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002954}
2955
Pekka Paalanen8274d902014-08-06 19:36:51 +03002956static int
2957panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2958{
2959 return snprintf(buf, len, "panel for output %s",
2960 surface->output->name);
2961}
2962
Kristian Høgsberg75840622011-09-06 13:48:16 -04002963static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002964panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002965{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002966 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002967 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02002968 int width, height;
2969 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002970
Jason Ekstranda7af7042013-10-12 22:38:11 -05002971 view = container_of(es->views.next, struct weston_view, surface_link);
2972
Quentin Glidice8bf9592016-06-23 18:55:20 +02002973 get_panel_size(shell, view, &width, &height);
2974 switch (shell->panel_position) {
2975 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
2976 break;
2977 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
2978 y = view->output->height - height;
2979 break;
2980 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
2981 break;
2982 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
2983 x = view->output->width - width;
2984 break;
2985 }
2986
2987 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002988}
2989
2990static void
David Fort50d962f2016-06-09 17:55:52 +02002991handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2992{
2993 struct shell_output *output =
2994 container_of(listener, struct shell_output, panel_surface_listener);
2995
2996 weston_log("panel surface gone\n");
2997 output->panel_surface = NULL;
2998}
2999
3000
3001static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003002desktop_shell_set_panel(struct wl_client *client,
3003 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003004 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003005 struct wl_resource *surface_resource)
3006{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003007 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003008 struct weston_surface *surface =
3009 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003010 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003011 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003012
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003013 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003014 wl_resource_post_error(surface_resource,
3015 WL_DISPLAY_ERROR_INVALID_OBJECT,
3016 "surface role already assigned");
3017 return;
3018 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003019
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3021 weston_view_destroy(view);
3022 view = weston_view_create(surface);
3023
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003024 surface->committed = panel_committed;
3025 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003026 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003027 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003028 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003029 weston_desktop_shell_send_configure(resource, 0,
3030 surface_resource,
3031 surface->output->width,
3032 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003033
3034 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3035 sh_output->panel_surface = surface;
3036
3037 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3038 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003039}
3040
Pekka Paalanen8274d902014-08-06 19:36:51 +03003041static int
3042lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3043{
3044 return snprintf(buf, len, "lock window");
3045}
3046
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003047static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003048lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003049{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003050 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003051 struct weston_view *view;
3052
3053 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003054
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003055 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003056 return;
3057
Jason Ekstranda7af7042013-10-12 22:38:11 -05003058 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003059
3060 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003061 weston_layer_entry_insert(&shell->lock_layer.view_list,
3062 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003063 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003064 surface->is_mapped = true;
3065 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003066 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003067 }
3068}
3069
3070static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003071handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003072{
Tiago Vignattibe143262012-04-16 17:31:41 +03003073 struct desktop_shell *shell =
3074 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003075
Martin Minarik6d118362012-06-07 18:01:59 +02003076 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003077 shell->lock_surface = NULL;
3078}
3079
3080static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003081desktop_shell_set_lock_surface(struct wl_client *client,
3082 struct wl_resource *resource,
3083 struct wl_resource *surface_resource)
3084{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003085 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003086 struct weston_surface *surface =
3087 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003088
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003089 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003090
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003091 if (!shell->locked)
3092 return;
3093
Pekka Paalanen98262232011-12-01 10:42:22 +02003094 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003095
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003096 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003097 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003098 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003099
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003100 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003101 surface->committed = lock_surface_committed;
3102 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003103 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003104}
3105
3106static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003107resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003108{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003109 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003110
Quentin Glidic82681572016-12-17 13:40:51 +01003111 weston_layer_unset_position(&shell->lock_layer);
3112
3113 if (shell->showing_input_panels)
3114 weston_layer_set_position(&shell->input_panel_layer,
3115 WESTON_LAYER_POSITION_TOP_UI);
3116 weston_layer_set_position(&shell->fullscreen_layer,
3117 WESTON_LAYER_POSITION_FULLSCREEN);
3118 weston_layer_set_position(&shell->panel_layer,
3119 WESTON_LAYER_POSITION_UI);
3120 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003121
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003122 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003123
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003124 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003125 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003126 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003127}
3128
3129static void
3130desktop_shell_unlock(struct wl_client *client,
3131 struct wl_resource *resource)
3132{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003133 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003134
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003135 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003136
3137 if (shell->locked)
3138 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003139}
3140
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003141static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003142desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143 struct wl_resource *resource,
3144 struct wl_resource *surface_resource)
3145{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003146 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003147
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003148 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003149 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003150}
3151
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003152static void
3153desktop_shell_desktop_ready(struct wl_client *client,
3154 struct wl_resource *resource)
3155{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003156 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003157
3158 shell_fade_startup(shell);
3159}
3160
Jonny Lamb765760d2014-08-20 15:53:19 +02003161static void
3162desktop_shell_set_panel_position(struct wl_client *client,
3163 struct wl_resource *resource,
3164 uint32_t position)
3165{
3166 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3167
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003168 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3169 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3170 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3171 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003172 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003173 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003174 "bad position argument");
3175 return;
3176 }
3177
3178 shell->panel_position = position;
3179}
3180
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003181static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003182 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003183 desktop_shell_set_panel,
3184 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003185 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003186 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003187 desktop_shell_desktop_ready,
3188 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003189};
3190
3191static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003192move_binding(struct weston_pointer *pointer, uint32_t time,
3193 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003194{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003195 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003196 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003197 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003198
Derek Foreman8ae2db52015-07-15 13:00:36 -05003199 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003200 return;
3201
Derek Foreman8ae2db52015-07-15 13:00:36 -05003202 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003203
Pekka Paalanen01388e22013-04-25 13:57:44 +03003204 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003205 if (surface == NULL)
3206 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003207
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003208 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003209 if (shsurf == NULL ||
3210 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3211 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003212 return;
3213
Derek Foreman794fa0e2015-07-15 13:00:38 -05003214 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003215}
3216
3217static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003218maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3219 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003220{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003221 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003222 struct weston_surface *surface;
3223 struct shell_surface *shsurf;
3224
3225 surface = weston_surface_get_main_surface(focus);
3226 if (surface == NULL)
3227 return;
3228
3229 shsurf = get_shell_surface(surface);
3230 if (shsurf == NULL)
3231 return;
3232
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003233 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003234}
3235
3236static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003237fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3238 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003239{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003240 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003241 struct weston_surface *surface;
3242 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003243 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003244
3245 surface = weston_surface_get_main_surface(focus);
3246 if (surface == NULL)
3247 return;
3248
3249 shsurf = get_shell_surface(surface);
3250 if (shsurf == NULL)
3251 return;
3252
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003253 fullscreen =
3254 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003255
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003256 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003257}
3258
3259static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003260touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003261{
Derek Foreman362656b2014-09-04 10:23:05 -05003262 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003263 struct weston_surface *surface;
3264 struct shell_surface *shsurf;
3265
Derek Foreman8ae2db52015-07-15 13:00:36 -05003266 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003267 return;
3268
Derek Foreman8ae2db52015-07-15 13:00:36 -05003269 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003270 surface = weston_surface_get_main_surface(focus);
3271 if (surface == NULL)
3272 return;
3273
3274 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003275 if (shsurf == NULL ||
3276 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3277 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003278 return;
3279
Derek Foremanb7674ae2015-07-15 13:00:37 -05003280 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003281}
3282
3283static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003284resize_binding(struct weston_pointer *pointer, uint32_t time,
3285 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003286{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003287 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003288 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003289 uint32_t edges = 0;
3290 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003291 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003292
Derek Foreman8ae2db52015-07-15 13:00:36 -05003293 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003294 return;
3295
Derek Foreman8ae2db52015-07-15 13:00:36 -05003296 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003297
Pekka Paalanen01388e22013-04-25 13:57:44 +03003298 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003299 if (surface == NULL)
3300 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003301
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003302 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003303 if (shsurf == NULL ||
3304 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3305 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003306 return;
3307
Jason Ekstranda7af7042013-10-12 22:38:11 -05003308 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003309 wl_fixed_to_int(pointer->grab_x),
3310 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003311 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003312
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003313 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003314 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003315 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003316 edges |= 0;
3317 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003318 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003319
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003320 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003321 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003322 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003323 edges |= 0;
3324 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003325 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003326
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003327 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003328}
3329
3330static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003331surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003332 struct weston_pointer_axis_event *event,
3333 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003334{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003335 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003336 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003337 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003338 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003339
Pekka Paalanen01388e22013-04-25 13:57:44 +03003340 /* XXX: broken for windows containing sub-surfaces */
3341 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003342 if (surface == NULL)
3343 return;
3344
3345 shsurf = get_shell_surface(surface);
3346 if (!shsurf)
3347 return;
3348
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003349 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003350
Jason Ekstranda7af7042013-10-12 22:38:11 -05003351 if (shsurf->view->alpha > 1.0)
3352 shsurf->view->alpha = 1.0;
3353 if (shsurf->view->alpha < step)
3354 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003355
Jason Ekstranda7af7042013-10-12 22:38:11 -05003356 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003357 weston_surface_damage(surface);
3358}
3359
3360static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003361do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003362 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003363{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003364 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003365 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003366 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003367 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003368
Derek Foreman1281a362015-07-31 16:55:32 -05003369 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003370 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3371 return;
3372 }
3373
Scott Moreauccbf29d2012-02-22 14:21:41 -07003374 wl_list_for_each(output, &compositor->output_list, link) {
3375 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003376 wl_fixed_to_double(pointer->x),
3377 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003378 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003379 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003380 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003381 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003382 increment = -output->zoom.increment;
3383 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003384 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003385 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003386 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003387 else
3388 increment = 0;
3389
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003390 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003391
Scott Moreaue6603982012-06-11 13:07:51 -06003392 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003393 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003394 else if (output->zoom.level > output->zoom.max_level)
3395 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003396
3397 if (!output->zoom.active) {
3398 if (output->zoom.level <= 0.0)
3399 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003400 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003401 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003402
Scott Moreaue6603982012-06-11 13:07:51 -06003403 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003404
Jason Ekstranda7af7042013-10-12 22:38:11 -05003405 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003406 }
3407 }
3408}
3409
Scott Moreauccbf29d2012-02-22 14:21:41 -07003410static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003411zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003412 struct weston_pointer_axis_event *event,
3413 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003414{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003415 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003416}
3417
3418static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003419zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3420 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003421{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003422 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003423}
3424
3425static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003426terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3427 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003428{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003429 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003430
Daniel Stone325fc2d2012-05-30 16:31:58 +01003431 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003432}
3433
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003434static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003435rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003436 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003437{
3438 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003439 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003440 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003441 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003442 struct weston_surface *surface =
3443 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003444 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003445
Jonas Ådahld2510102014-10-05 21:39:14 +02003446 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003447
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003448 if (!shsurf)
3449 return;
3450
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003451 cx = 0.5f * surface->width;
3452 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003453
Daniel Stone37816df2012-05-16 18:45:18 +01003454 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3455 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003456 r = sqrtf(dx * dx + dy * dy);
3457
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003458 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003459 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003460
3461 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003462 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003463 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003464
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003465 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003466 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003467
3468 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003469 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003470 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003471 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003472 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003473
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003474 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003475 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003476 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003477 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003478 wl_list_init(&shsurf->rotation.transform.link);
3479 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003480 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003481 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003482
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003483 /* We need to adjust the position of the surface
3484 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 cposx = shsurf->view->geometry.x + cx;
3486 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003487 dposx = rotate->center.x - cposx;
3488 dposy = rotate->center.y - cposy;
3489 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003490 weston_view_set_position(shsurf->view,
3491 shsurf->view->geometry.x + dposx,
3492 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003493 }
3494
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003495 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003496 * lazily applies the damage due to rotation update.
3497 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003498 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003499}
3500
3501static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003502rotate_grab_button(struct weston_pointer_grab *grab,
3503 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003504{
3505 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003506 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003507 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003508 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003509 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003510
Daniel Stone4dbadb12012-05-30 16:31:51 +01003511 if (pointer->button_count == 0 &&
3512 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003513 if (shsurf)
3514 weston_matrix_multiply(&shsurf->rotation.rotation,
3515 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003516 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003517 free(rotate);
3518 }
3519}
3520
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003521static void
3522rotate_grab_cancel(struct weston_pointer_grab *grab)
3523{
3524 struct rotate_grab *rotate =
3525 container_of(grab, struct rotate_grab, base.grab);
3526
3527 shell_grab_end(&rotate->base);
3528 free(rotate);
3529}
3530
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003531static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003532 noop_grab_focus,
3533 rotate_grab_motion,
3534 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003535 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003536 noop_grab_axis_source,
3537 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003538 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003539};
3540
3541static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003542surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003543{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003544 struct weston_surface *surface =
3545 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003546 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003547 float dx, dy;
3548 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003549
Pekka Paalanen460099f2012-01-20 16:48:25 +02003550 rotate = malloc(sizeof *rotate);
3551 if (!rotate)
3552 return;
3553
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003554 weston_view_to_global_float(shsurf->view,
3555 surface->width * 0.5f,
3556 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003557 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003558
Derek Foreman74de4692015-07-15 13:00:39 -05003559 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3560 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003561 r = sqrtf(dx * dx + dy * dy);
3562 if (r > 20.0f) {
3563 struct weston_matrix inverse;
3564
3565 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003566 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003567 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003568
3569 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003570 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003571 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003572 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003573 weston_matrix_init(&rotate->rotation);
3574 }
3575
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003576 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003577 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003578}
3579
3580static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003581rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003582 void *data)
3583{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003584 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003585 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003586 struct shell_surface *surface;
3587
Derek Foreman8ae2db52015-07-15 13:00:36 -05003588 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003589 return;
3590
Derek Foreman8ae2db52015-07-15 13:00:36 -05003591 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003592
Pekka Paalanen01388e22013-04-25 13:57:44 +03003593 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003594 if (base_surface == NULL)
3595 return;
3596
3597 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003598 if (surface == NULL ||
3599 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3600 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003601 return;
3602
Derek Foreman74de4692015-07-15 13:00:39 -05003603 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003604}
3605
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003606/* Move all fullscreen layers down to the current workspace and hide their
3607 * black views. The surfaces' state is set to both fullscreen and lowered,
3608 * and this is reversed when such a surface is re-configured, see
3609 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3610 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003611 * lowering_output = NULL - Lower on all outputs, else only lower on the
3612 * specified output.
3613 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003614 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003615 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003616void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003617lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003618 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003619{
3620 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003621 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003622
3623 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003624 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003625 &shell->fullscreen_layer.view_list.link,
3626 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003627 struct shell_surface *shsurf = get_shell_surface(view->surface);
3628
3629 if (!shsurf)
3630 continue;
3631
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003632 /* Only lower surfaces which have lowering_output as their fullscreen
3633 * output, unless a NULL output asks for lowering on all outputs.
3634 */
3635 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3636 continue;
3637
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003638 /* We can have a non-fullscreen popup for a fullscreen surface
3639 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003640 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003641 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003642 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3643 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003644 weston_view_damage_below(shsurf->fullscreen.black_view);
3645
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003646 }
3647
3648 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003649 weston_layer_entry_remove(&view->layer_link);
3650 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003651 weston_view_damage_below(view);
3652 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003653
3654 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003655 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003656}
3657
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003658void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003659activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003660 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003661{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003662 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003663 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003664 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003665 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003666 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003667 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003668
Pekka Paalanen01388e22013-04-25 13:57:44 +03003669 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003670 shsurf = get_shell_surface(main_surface);
3671 assert(shsurf);
3672
3673 /* Only demote fullscreen surfaces on the output of activated shsurf.
3674 * Leave fullscreen surfaces on unrelated outputs alone. */
3675 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003676
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003677 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003678
Jonas Ådahl8538b222012-08-29 22:13:03 +02003679 state = ensure_focus_state(shell, seat);
3680 if (state == NULL)
3681 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003682
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003683 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003684 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003685
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003686 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3687 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003688 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003689
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003690 /* Update the surface’s layer. This brings it to the top of the stacking
3691 * order as appropriate. */
3692 shell_surface_update_layer(shsurf);
3693
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003694 if (shell->focus_animation_type != ANIMATION_NONE) {
3695 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003696 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003697 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003698}
3699
Alex Wu21858432012-04-01 20:13:08 +08003700/* no-op func for checking black surface */
3701static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003702black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003703{
3704}
3705
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003706static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003707is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003708{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003709 struct weston_surface *surface = view->surface;
3710
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003711 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003712 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003713 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003714 return true;
3715 }
3716 return false;
3717}
3718
Kristian Høgsberg75840622011-09-06 13:48:16 -04003719static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003720activate_binding(struct weston_seat *seat,
3721 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003722 struct weston_view *focus_view,
3723 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003724{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003725 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003726 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003727
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003728 if (!focus_view)
3729 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003730
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003731 if (is_black_surface_view(focus_view, &main_view))
3732 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003733
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003734 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003735 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003736 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003737
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003738 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003739}
3740
3741static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003742click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3743 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003744{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003745 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003746 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003747 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003748 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003749
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003750 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003751 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003752 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003753}
3754
3755static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003756touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3757 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003758{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003759 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003760 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003761 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003762 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003763
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003764 activate_binding(touch->seat, data, touch->focus,
3765 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003766}
3767
3768static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003769unfocus_all_seats(struct desktop_shell *shell)
3770{
3771 struct weston_seat *seat, *next;
3772
3773 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003774 struct weston_keyboard *keyboard =
3775 weston_seat_get_keyboard(seat);
3776
3777 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003778 continue;
3779
Derek Foreman1281a362015-07-31 16:55:32 -05003780 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003781 }
3782}
3783
3784static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003785lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003786{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003787 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003788
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003789 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003790 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003791 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003792 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003793
3794 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003795
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003796 /* Hide all surfaces by removing the fullscreen, panel and
3797 * toplevel layers. This way nothing else can show or receive
3798 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003799
Quentin Glidic82681572016-12-17 13:40:51 +01003800 weston_layer_unset_position(&shell->panel_layer);
3801 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003802 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003803 weston_layer_unset_position(&shell->input_panel_layer);
3804 weston_layer_unset_position(&ws->layer);
3805
3806 weston_layer_set_position(&shell->lock_layer,
3807 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003808
Derek Foreman004b4a12015-07-20 16:28:13 -05003809 weston_compositor_sleep(shell->compositor);
3810
Neil Robertsb4a91702014-04-09 16:33:32 +01003811 /* Remove the keyboard focus on all seats. This will be
3812 * restored to the workspace's saved state via
3813 * restore_focus_state when the compositor is unlocked */
3814 unfocus_all_seats(shell);
3815
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003816 /* TODO: disable bindings that should not work while locked. */
3817
3818 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003819}
3820
3821static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003822unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003823{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003824 struct wl_resource *shell_resource;
3825
Pekka Paalanend81c2162011-11-16 13:47:34 +02003826 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003827 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003828 return;
3829 }
3830
3831 /* If desktop-shell client has gone away, unlock immediately. */
3832 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003833 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003834 return;
3835 }
3836
3837 if (shell->prepare_event_sent)
3838 return;
3839
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003840 shell_resource = shell->child.desktop_shell;
3841 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003842 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003843}
3844
3845static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003846shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003847{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003848 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003849
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003850 shell->fade.animation = NULL;
3851
3852 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003853 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003854 weston_surface_destroy(shell->fade.view->surface);
3855 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003856 break;
3857 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003858 lock(shell);
3859 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003860 default:
3861 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003862 }
3863}
3864
Jason Ekstranda7af7042013-10-12 22:38:11 -05003865static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003866shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003867{
3868 struct weston_compositor *compositor = shell->compositor;
3869 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003870 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003871
3872 surface = weston_surface_create(compositor);
3873 if (!surface)
3874 return NULL;
3875
Jason Ekstranda7af7042013-10-12 22:38:11 -05003876 view = weston_view_create(surface);
3877 if (!view) {
3878 weston_surface_destroy(surface);
3879 return NULL;
3880 }
3881
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003882 weston_surface_set_size(surface, 8192, 8192);
3883 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003884 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003885 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3886 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003887 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003888 surface->is_mapped = true;
3889 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003890
Jason Ekstranda7af7042013-10-12 22:38:11 -05003891 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003892}
3893
3894static void
3895shell_fade(struct desktop_shell *shell, enum fade_type type)
3896{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003897 float tint;
3898
3899 switch (type) {
3900 case FADE_IN:
3901 tint = 0.0;
3902 break;
3903 case FADE_OUT:
3904 tint = 1.0;
3905 break;
3906 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003907 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003908 return;
3909 }
3910
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003911 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003912
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003913 if (shell->fade.view == NULL) {
3914 shell->fade.view = shell_fade_create_surface(shell);
3915 if (!shell->fade.view)
3916 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003917
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003918 shell->fade.view->alpha = 1.0 - tint;
3919 weston_view_update_transform(shell->fade.view);
3920 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003921
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003922 if (shell->fade.view->output == NULL) {
3923 /* If the black view gets a NULL output, we lost the
3924 * last output and we'll just cancel the fade. This
3925 * happens when you close the last window under the
3926 * X11 or Wayland backends. */
3927 shell->locked = false;
3928 weston_surface_destroy(shell->fade.view->surface);
3929 shell->fade.view = NULL;
3930 } else if (shell->fade.animation) {
3931 weston_fade_update(shell->fade.animation, tint);
3932 } else {
3933 shell->fade.animation =
3934 weston_fade_run(shell->fade.view,
3935 1.0 - tint, tint, 300.0,
3936 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003937 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003938}
3939
3940static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003941do_shell_fade_startup(void *data)
3942{
3943 struct desktop_shell *shell = data;
3944
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003945 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003946 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003947 } else {
3948 weston_log("desktop shell: "
3949 "unexpected fade-in animation type %d\n",
3950 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003951 weston_surface_destroy(shell->fade.view->surface);
3952 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003953 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003954}
3955
3956static void
3957shell_fade_startup(struct desktop_shell *shell)
3958{
3959 struct wl_event_loop *loop;
3960
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003961 if (!shell->fade.startup_timer)
3962 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003963
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003964 wl_event_source_remove(shell->fade.startup_timer);
3965 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003966
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003967 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3968 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003969}
3970
3971static int
3972fade_startup_timeout(void *data)
3973{
3974 struct desktop_shell *shell = data;
3975
3976 shell_fade_startup(shell);
3977 return 0;
3978}
3979
3980static void
3981shell_fade_init(struct desktop_shell *shell)
3982{
3983 /* Make compositor output all black, and wait for the desktop-shell
3984 * client to signal it is ready, then fade in. The timer triggers a
3985 * fade-in, in case the desktop-shell client takes too long.
3986 */
3987
3988 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003989
3990 if (shell->fade.view != NULL) {
3991 weston_log("%s: warning: fade surface already exists\n",
3992 __func__);
3993 return;
3994 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003995
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003996 if (shell->startup_animation_type == ANIMATION_NONE)
3997 return;
3998
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003999 shell->fade.view = shell_fade_create_surface(shell);
4000 if (!shell->fade.view)
4001 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004002
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004003 weston_view_update_transform(shell->fade.view);
4004 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004005
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004006 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4007 shell->fade.startup_timer =
4008 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4009 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004010}
4011
4012static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004013idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004014{
4015 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004016 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004017
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004018 struct weston_seat *seat;
4019
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004020 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4021 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004022
4023 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004024 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004025}
4026
4027static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004028wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004029{
4030 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004031 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004032
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004033 unlock(shell);
4034}
4035
4036static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004037transform_handler(struct wl_listener *listener, void *data)
4038{
4039 struct weston_surface *surface = data;
4040 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004041 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004042 int x, y;
4043
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004044 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004045 return;
4046
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004047 api = shsurf->shell->xwayland_surface_api;
4048 if (!api) {
4049 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4050 shsurf->shell->xwayland_surface_api = api;
4051 }
4052
4053 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004054 return;
4055
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004056 if (!weston_view_is_mapped(shsurf->view))
4057 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004058
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004059 x = shsurf->view->geometry.x;
4060 y = shsurf->view->geometry.y;
4061
4062 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004063}
4064
4065static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004066center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004067{
Giulio Camuffob8366642013-04-25 13:57:46 +03004068 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004069 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004070
Jason Ekstranda7af7042013-10-12 22:38:11 -05004071 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004072
4073 x = output->x + (output->width - width) / 2 - surf_x / 2;
4074 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004075
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004076 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004077}
4078
4079static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004080weston_view_set_initial_position(struct weston_view *view,
4081 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004082{
4083 struct weston_compositor *compositor = shell->compositor;
4084 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004085 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004086 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004087 struct weston_output *output, *target_output = NULL;
4088 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004089 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004090
4091 /* As a heuristic place the new window on the same output as the
4092 * pointer. Falling back to the output containing 0, 0.
4093 *
4094 * TODO: Do something clever for touch too?
4095 */
4096 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004097 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4098
4099 if (pointer) {
4100 ix = wl_fixed_to_int(pointer->x);
4101 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004102 break;
4103 }
4104 }
4105
4106 wl_list_for_each(output, &compositor->output_list, link) {
4107 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4108 target_output = output;
4109 break;
4110 }
4111 }
4112
4113 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004114 weston_view_set_position(view, 10 + random() % 400,
4115 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004116 return;
4117 }
4118
4119 /* Valid range within output where the surface will still be onscreen.
4120 * If this is negative it means that the surface is bigger than
4121 * output.
4122 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004123 get_output_work_area(shell, target_output, &area);
4124
Derek Foremanf814c5d2015-04-15 12:23:54 -05004125 x = area.x;
4126 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004127 range_x = area.width - view->surface->width;
4128 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004129
Rob Bradford4cb88c72012-08-13 15:18:44 +01004130 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004131 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004132
4133 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004134 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004135
Jason Ekstranda7af7042013-10-12 22:38:11 -05004136 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004137}
4138
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004139static bool
4140check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4141{
4142 struct timespec now;
4143
4144 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4145 return false;
4146
4147 /*
4148 * If the shell helper client dies before the session has been
4149 * up for roughly 30 seconds, better just make Weston shut down,
4150 * because the user likely has no way to interact with the desktop
4151 * anyway.
4152 */
4153 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4154 weston_log("Error: %s apparently cannot run at all.\n",
4155 shell->client);
4156 weston_log_continue(STAMP_SPACE "Quitting...");
4157 wl_display_terminate(shell->compositor->wl_display);
4158
4159 return true;
4160 }
4161
4162 return false;
4163}
4164
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004165static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004166
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004167static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004168respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004169{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004170 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004171
4172 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4173 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004174 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004175 shell->child.deathstamp = time;
4176 shell->child.deathcount = 0;
4177 }
4178
4179 shell->child.deathcount++;
4180 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004181 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004182 return;
4183 }
4184
Pekka Paalanen826dc142014-08-27 12:11:53 +03004185 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004186 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004187}
4188
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004189static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004190desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4191{
4192 struct desktop_shell *shell;
4193
4194 shell = container_of(listener, struct desktop_shell,
4195 child.client_destroy_listener);
4196
Pekka Paalanen826dc142014-08-27 12:11:53 +03004197 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004198 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004199 /*
4200 * unbind_desktop_shell() will reset shell->child.desktop_shell
4201 * before the respawned process has a chance to create a new
4202 * desktop_shell object, because we are being called from the
4203 * wl_client destructor which destroys all wl_resources before
4204 * returning.
4205 */
4206
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004207 if (!check_desktop_shell_crash_too_early(shell))
4208 respawn_desktop_shell_process(shell);
4209
Pekka Paalanen826dc142014-08-27 12:11:53 +03004210 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004211}
4212
4213static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004214launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004215{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004216 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004217
Pekka Paalanen826dc142014-08-27 12:11:53 +03004218 shell->child.client = weston_client_start(shell->compositor,
4219 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004220
Arnaud Vrac42631192014-08-25 20:56:46 +02004221 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004222 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004223 return;
4224 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004225
4226 shell->child.client_destroy_listener.notify =
4227 desktop_shell_client_destroy;
4228 wl_client_add_destroy_listener(shell->child.client,
4229 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004230}
4231
4232static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004233unbind_desktop_shell(struct wl_resource *resource)
4234{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004235 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004236
4237 if (shell->locked)
4238 resume_desktop(shell);
4239
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004240 shell->child.desktop_shell = NULL;
4241 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004242}
4243
4244static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004245bind_desktop_shell(struct wl_client *client,
4246 void *data, uint32_t version, uint32_t id)
4247{
Tiago Vignattibe143262012-04-16 17:31:41 +03004248 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004249 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004250
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004251 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4252 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004253
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004254 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004255 wl_resource_set_implementation(resource,
4256 &desktop_shell_implementation,
4257 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004258 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004259 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004260 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004261
4262 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4263 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004264}
4265
Kristian Høgsberg07045392012-02-19 18:52:44 -05004266struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004267 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004268 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004269 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004270 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004271 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004272};
4273
4274static void
4275switcher_next(struct switcher *switcher)
4276{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004277 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004278 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004279 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004280 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004281
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004282 /* temporary re-display minimized surfaces */
4283 struct weston_view *tmp;
4284 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004285 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4286 weston_layer_entry_remove(&view->layer_link);
4287 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004288 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4289 *minimized = view;
4290 }
4291
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004292 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004293 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004294 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004295 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004296 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004297 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004298 next = view;
4299 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004300 view->alpha = 0.25;
4301 weston_view_geometry_dirty(view);
4302 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004303 }
Alex Wu1659daa2012-04-01 20:13:09 +08004304
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004305 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004306 view->alpha = 0.25;
4307 weston_view_geometry_dirty(view);
4308 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004309 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004310 }
4311
4312 if (next == NULL)
4313 next = first;
4314
Alex Wu07b26062012-03-12 16:06:01 +08004315 if (next == NULL)
4316 return;
4317
Kristian Høgsberg07045392012-02-19 18:52:44 -05004318 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004319 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004320
4321 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004322 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004323 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004324
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004325 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004326 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004327 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004328}
4329
4330static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004331switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004332{
4333 struct switcher *switcher =
4334 container_of(listener, struct switcher, listener);
4335
4336 switcher_next(switcher);
4337}
4338
4339static void
Daniel Stone351eb612012-05-31 15:27:47 -04004340switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004341{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004342 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004343 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004344 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004345
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004346 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004347 if (is_focus_view(view))
4348 continue;
4349
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 view->alpha = 1.0;
4351 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004352 }
4353
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004354 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004355 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004356 keyboard->seat,
4357 WESTON_ACTIVATE_FLAG_CONFIGURE);
4358 }
4359
Kristian Høgsberg07045392012-02-19 18:52:44 -05004360 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004361 weston_keyboard_end_grab(keyboard);
4362 if (keyboard->input_method_resource)
4363 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004364
4365 /* re-hide surfaces that were temporary shown during the switch */
4366 struct weston_view **minimized;
4367 wl_array_for_each(minimized, &switcher->minimized_array) {
4368 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004369 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004370 weston_layer_entry_remove(&(*minimized)->layer_link);
4371 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004372 weston_view_damage_below(*minimized);
4373 }
4374 }
4375 wl_array_release(&switcher->minimized_array);
4376
Kristian Høgsberg07045392012-02-19 18:52:44 -05004377 free(switcher);
4378}
4379
4380static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004381switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004382 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383{
4384 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004385 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004386
Daniel Stonec9785ea2012-05-30 16:31:52 +01004387 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004388 switcher_next(switcher);
4389}
4390
4391static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004392switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004393 uint32_t mods_depressed, uint32_t mods_latched,
4394 uint32_t mods_locked, uint32_t group)
4395{
4396 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004397 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004398
Daniel Stone351eb612012-05-31 15:27:47 -04004399 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4400 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004401}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004402
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004403static void
4404switcher_cancel(struct weston_keyboard_grab *grab)
4405{
4406 struct switcher *switcher = container_of(grab, struct switcher, grab);
4407
4408 switcher_destroy(switcher);
4409}
4410
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004411static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004412 switcher_key,
4413 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004414 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004415};
4416
4417static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004418switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4419 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004420{
Tiago Vignattibe143262012-04-16 17:31:41 +03004421 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004422 struct switcher *switcher;
4423
4424 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004425 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004426 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004427 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004428 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004429 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004430
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004431 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004432 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004433 weston_keyboard_start_grab(keyboard, &switcher->grab);
4434 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004435 switcher_next(switcher);
4436}
4437
Pekka Paalanen3c647232011-12-22 13:43:43 +02004438static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004439backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4440 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004441{
4442 struct weston_compositor *compositor = data;
4443 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004444 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004445
4446 /* TODO: we're limiting to simple use cases, where we assume just
4447 * control on the primary display. We'd have to extend later if we
4448 * ever get support for setting backlights on random desktop LCD
4449 * panels though */
4450 output = get_default_output(compositor);
4451 if (!output)
4452 return;
4453
4454 if (!output->set_backlight)
4455 return;
4456
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004457 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4458 backlight_new = output->backlight_current - 25;
4459 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4460 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004461
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004462 if (backlight_new < 5)
4463 backlight_new = 5;
4464 if (backlight_new > 255)
4465 backlight_new = 255;
4466
4467 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004468 output->set_backlight(output, output->backlight_current);
4469}
4470
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004471static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004472force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4473 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004474{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004475 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004476 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004477 struct desktop_shell *shell = data;
4478 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004479 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004480
Derek Foreman8ae2db52015-07-15 13:00:36 -05004481 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004482 if (!focus_surface)
4483 return;
4484
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004485 wl_signal_emit(&compositor->kill_signal, focus_surface);
4486
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004487 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004488 wl_client_get_credentials(client, &pid, NULL, NULL);
4489
4490 /* Skip clients that we launched ourselves (the credentials of
4491 * the socketpair is ours) */
4492 if (pid == getpid())
4493 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004494
Daniel Stone325fc2d2012-05-30 16:31:58 +01004495 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004496}
4497
4498static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004499workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004500 uint32_t key, void *data)
4501{
4502 struct desktop_shell *shell = data;
4503 unsigned int new_index = shell->workspaces.current;
4504
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004505 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004506 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004507 if (new_index != 0)
4508 new_index--;
4509
4510 change_workspace(shell, new_index);
4511}
4512
4513static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004514workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004515 uint32_t key, void *data)
4516{
4517 struct desktop_shell *shell = data;
4518 unsigned int new_index = shell->workspaces.current;
4519
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004520 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004521 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004522 if (new_index < shell->workspaces.num - 1)
4523 new_index++;
4524
4525 change_workspace(shell, new_index);
4526}
4527
4528static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004529workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004530 uint32_t key, void *data)
4531{
4532 struct desktop_shell *shell = data;
4533 unsigned int new_index;
4534
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004535 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004536 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004537 new_index = key - KEY_F1;
4538 if (new_index >= shell->workspaces.num)
4539 new_index = shell->workspaces.num - 1;
4540
4541 change_workspace(shell, new_index);
4542}
4543
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004544static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004545workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4546 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004547{
4548 struct desktop_shell *shell = data;
4549 unsigned int new_index = shell->workspaces.current;
4550
4551 if (shell->locked)
4552 return;
4553
4554 if (new_index != 0)
4555 new_index--;
4556
Derek Foreman8ae2db52015-07-15 13:00:36 -05004557 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004558}
4559
4560static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004561workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4562 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004563{
4564 struct desktop_shell *shell = data;
4565 unsigned int new_index = shell->workspaces.current;
4566
4567 if (shell->locked)
4568 return;
4569
4570 if (new_index < shell->workspaces.num - 1)
4571 new_index++;
4572
Derek Foreman8ae2db52015-07-15 13:00:36 -05004573 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004574}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004575
4576static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004577shell_reposition_view_on_output_destroy(struct weston_view *view)
4578{
4579 struct weston_output *output, *first_output;
4580 struct weston_compositor *ec = view->surface->compositor;
4581 struct shell_surface *shsurf;
4582 float x, y;
4583 int visible;
4584
4585 x = view->geometry.x;
4586 y = view->geometry.y;
4587
4588 /* At this point the destroyed output is not in the list anymore.
4589 * If the view is still visible somewhere, we leave where it is,
4590 * otherwise, move it to the first output. */
4591 visible = 0;
4592 wl_list_for_each(output, &ec->output_list, link) {
4593 if (pixman_region32_contains_point(&output->region,
4594 x, y, NULL)) {
4595 visible = 1;
4596 break;
4597 }
4598 }
4599
4600 if (!visible) {
4601 first_output = container_of(ec->output_list.next,
4602 struct weston_output, link);
4603
4604 x = first_output->x + first_output->width / 4;
4605 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004606
4607 weston_view_set_position(view, x, y);
4608 } else {
4609 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004610 }
4611
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004612
4613 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004614 if (!shsurf)
4615 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004616
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004617 shsurf->saved_position_valid = false;
4618 set_maximized(shsurf, false);
4619 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004620}
4621
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004622void
4623shell_for_each_layer(struct desktop_shell *shell,
4624 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004625{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004626 struct workspace **ws;
4627
4628 func(shell, &shell->fullscreen_layer, data);
4629 func(shell, &shell->panel_layer, data);
4630 func(shell, &shell->background_layer, data);
4631 func(shell, &shell->lock_layer, data);
4632 func(shell, &shell->input_panel_layer, data);
4633
4634 wl_array_for_each(ws, &shell->workspaces.array)
4635 func(shell, &(*ws)->layer, data);
4636}
4637
4638static void
4639shell_output_destroy_move_layer(struct desktop_shell *shell,
4640 struct weston_layer *layer,
4641 void *data)
4642{
4643 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004644 struct weston_view *view;
4645
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004646 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004647 if (view->output != output)
4648 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004649
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004650 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004651 }
4652}
4653
4654static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004655handle_output_destroy(struct wl_listener *listener, void *data)
4656{
4657 struct shell_output *output_listener =
4658 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004659 struct weston_output *output = output_listener->output;
4660 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004661
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004662 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004663
Quentin Glidic561201c2016-08-20 18:19:51 +02004664 wl_list_remove(&output_listener->panel_surface_listener.link);
4665 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004666 wl_list_remove(&output_listener->destroy_listener.link);
4667 wl_list_remove(&output_listener->link);
4668 free(output_listener);
4669}
4670
4671static void
David Fort50d962f2016-06-09 17:55:52 +02004672shell_resize_surface_to_output(struct desktop_shell *shell,
4673 struct weston_surface *surface,
4674 const struct weston_output *output)
4675{
4676 if (!surface)
4677 return;
4678
4679 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4680 surface->resource,
4681 output->width,
4682 output->height);
4683}
4684
4685
4686static void
4687handle_output_resized(struct wl_listener *listener, void *data)
4688{
4689 struct desktop_shell *shell =
4690 container_of(listener, struct desktop_shell, resized_listener);
4691 struct weston_output *output = (struct weston_output *)data;
4692 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4693
4694 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4695 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4696}
4697
4698static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004699create_shell_output(struct desktop_shell *shell,
4700 struct weston_output *output)
4701{
4702 struct shell_output *shell_output;
4703
4704 shell_output = zalloc(sizeof *shell_output);
4705 if (shell_output == NULL)
4706 return;
4707
4708 shell_output->output = output;
4709 shell_output->shell = shell;
4710 shell_output->destroy_listener.notify = handle_output_destroy;
Daniel Stone8de91492017-01-16 13:22:15 +00004711 wl_list_init(&shell_output->panel_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004712 wl_signal_add(&output->destroy_signal,
4713 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004714 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004715}
4716
4717static void
4718handle_output_create(struct wl_listener *listener, void *data)
4719{
4720 struct desktop_shell *shell =
4721 container_of(listener, struct desktop_shell, output_create_listener);
4722 struct weston_output *output = (struct weston_output *)data;
4723
4724 create_shell_output(shell, output);
4725}
4726
4727static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004728handle_output_move_layer(struct desktop_shell *shell,
4729 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004730{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004731 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004732 struct weston_view *view;
4733 float x, y;
4734
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004735 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004736 if (view->output != output)
4737 continue;
4738
4739 x = view->geometry.x + output->move_x;
4740 y = view->geometry.y + output->move_y;
4741 weston_view_set_position(view, x, y);
4742 }
4743}
4744
4745static void
4746handle_output_move(struct wl_listener *listener, void *data)
4747{
4748 struct desktop_shell *shell;
4749
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004750 shell = container_of(listener, struct desktop_shell,
4751 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004752
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004753 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004754}
4755
4756static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004757setup_output_destroy_handler(struct weston_compositor *ec,
4758 struct desktop_shell *shell)
4759{
4760 struct weston_output *output;
4761
4762 wl_list_init(&shell->output_list);
4763 wl_list_for_each(output, &ec->output_list, link)
4764 create_shell_output(shell, output);
4765
4766 shell->output_create_listener.notify = handle_output_create;
4767 wl_signal_add(&ec->output_created_signal,
4768 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004769
4770 shell->output_move_listener.notify = handle_output_move;
4771 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004772}
4773
4774static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004775shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004776{
Tiago Vignattibe143262012-04-16 17:31:41 +03004777 struct desktop_shell *shell =
4778 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004779 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004780 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004781
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004782 /* Force state to unlocked so we don't try to fade */
4783 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004784
4785 if (shell->child.client) {
4786 /* disable respawn */
4787 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004788 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004789 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004790
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004791 wl_list_remove(&shell->idle_listener.link);
4792 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004793 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004794
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004795 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004796 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004797
Xiong Zhang6b481422013-10-23 13:58:32 +08004798 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4799 wl_list_remove(&shell_output->destroy_listener.link);
4800 wl_list_remove(&shell_output->link);
4801 free(shell_output);
4802 }
4803
4804 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004805 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004806 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004807
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004808 wl_array_for_each(ws, &shell->workspaces.array)
4809 workspace_destroy(*ws);
4810 wl_array_release(&shell->workspaces.array);
4811
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004812 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004813 free(shell);
4814}
4815
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004816static void
4817shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4818{
4819 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004820 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004821
Bob Ham744e6532016-01-12 10:21:48 +00004822 if (shell->allow_zap)
4823 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4824 MODIFIER_CTRL | MODIFIER_ALT,
4825 terminate_binding, ec);
4826
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004827 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004828 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4829 click_to_activate_binding,
4830 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004831 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4832 click_to_activate_binding,
4833 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004834 weston_compositor_add_touch_binding(ec, 0,
4835 touch_to_activate_binding,
4836 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004837 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4838 MODIFIER_SUPER | MODIFIER_ALT,
4839 surface_opacity_binding, NULL);
4840 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4841 MODIFIER_SUPER, zoom_axis_binding,
4842 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004843 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4844 backlight_binding, ec);
4845 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4846 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004847
4848 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004849 if (shell->exposay_modifier)
4850 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4851 exposay_binding, shell);
4852
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004853 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004854 if (!mod)
4855 return;
4856
Daniel Stone325fc2d2012-05-30 16:31:58 +01004857 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4858 zoom_key_binding, NULL);
4859 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4860 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004861 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4862 maximize_binding, NULL);
4863 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4864 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004865 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4866 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004867 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004868 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004869 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004870 weston_compositor_add_button_binding(ec, BTN_LEFT,
4871 mod | MODIFIER_SHIFT,
4872 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004873
4874 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004875 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004876 rotate_binding, NULL);
4877
Daniel Stone325fc2d2012-05-30 16:31:58 +01004878 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4879 shell);
4880 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4881 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004882 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4883 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004884 weston_compositor_add_key_binding(ec, KEY_K, mod,
4885 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004886 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4887 workspace_up_binding, shell);
4888 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4889 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004890 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4891 workspace_move_surface_up_binding,
4892 shell);
4893 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4894 workspace_move_surface_down_binding,
4895 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004896
4897 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4898 if (shell->workspaces.num > 1) {
4899 num_workspace_bindings = shell->workspaces.num;
4900 if (num_workspace_bindings > 6)
4901 num_workspace_bindings = 6;
4902 for (i = 0; i < num_workspace_bindings; i++)
4903 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4904 workspace_f_binding,
4905 shell);
4906 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004907
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004908 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004909}
4910
Jason Ekstrand024177c2014-04-21 19:42:58 -05004911static void
4912handle_seat_created(struct wl_listener *listener, void *data)
4913{
4914 struct weston_seat *seat = data;
4915
4916 create_shell_seat(seat);
4917}
4918
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004919WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004920wet_shell_init(struct weston_compositor *ec,
4921 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004922{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004923 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004924 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004925 struct workspace **pws;
4926 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004927 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004928
Peter Huttererf3d62272013-08-08 11:57:05 +10004929 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004930 if (shell == NULL)
4931 return -1;
4932
Kristian Høgsberg75840622011-09-06 13:48:16 -04004933 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004934
4935 shell->destroy_listener.notify = shell_destroy;
4936 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004937 shell->idle_listener.notify = idle_handler;
4938 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4939 shell->wake_listener.notify = wake_handler;
4940 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004941 shell->transform_listener.notify = transform_handler;
4942 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004943
Quentin Glidic82681572016-12-17 13:40:51 +01004944 weston_layer_init(&shell->fullscreen_layer, ec);
4945 weston_layer_init(&shell->panel_layer, ec);
4946 weston_layer_init(&shell->background_layer, ec);
4947 weston_layer_init(&shell->lock_layer, ec);
4948 weston_layer_init(&shell->input_panel_layer, ec);
4949
4950 weston_layer_set_position(&shell->fullscreen_layer,
4951 WESTON_LAYER_POSITION_FULLSCREEN);
4952 weston_layer_set_position(&shell->panel_layer,
4953 WESTON_LAYER_POSITION_UI);
4954 weston_layer_set_position(&shell->background_layer,
4955 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004956
4957 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004958 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004959
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004960 if (input_panel_setup(shell) < 0)
4961 return -1;
4962
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004963 shell->text_backend = text_backend_init(ec);
4964 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004965 return -1;
4966
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004967 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004968
Daniel Stonedf8133b2013-11-19 11:37:14 +01004969 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4970 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4971
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004972 for (i = 0; i < shell->workspaces.num; i++) {
4973 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4974 if (pws == NULL)
4975 return -1;
4976
Quentin Glidic82681572016-12-17 13:40:51 +01004977 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004978 if (*pws == NULL)
4979 return -1;
4980 }
4981 activate_workspace(shell, 0);
4982
Quentin Glidic82681572016-12-17 13:40:51 +01004983 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004984
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004985 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004986 wl_list_init(&shell->workspaces.animation.link);
4987 shell->workspaces.animation.frame = animate_workspace_change_frame;
4988
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004989 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4990 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004991 return -1;
4992
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004993 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004994 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004995 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004996 return -1;
4997
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004998 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004999
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005000 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005001
Xiong Zhang6b481422013-10-23 13:58:32 +08005002 setup_output_destroy_handler(ec, shell);
5003
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005004 loop = wl_display_get_event_loop(ec->wl_display);
5005 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005006
Jason Ekstrand024177c2014-04-21 19:42:58 -05005007 wl_list_for_each(seat, &ec->seat_list, link)
5008 handle_seat_created(NULL, seat);
5009 shell->seat_create_listener.notify = handle_seat_created;
5010 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005011
David Fort50d962f2016-06-09 17:55:52 +02005012 shell->resized_listener.notify = handle_output_resized;
5013 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5014
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005015 screenshooter_create(ec);
5016
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005017 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005018
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005019 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005020
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005021 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5022
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005023 return 0;
5024}