blob: ceb45c74db0e1ebc30f34b452d9548a46cd21e2b [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"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020044#include "shared/timespec-util.h"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020045#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050046
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020048#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020049
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010050#ifndef static_assert
51#define static_assert(cond, msg)
52#endif
53
Jonas Ådahl04769742012-06-13 00:01:24 +020054struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020055 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020056 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040057 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020058 struct weston_surface *keyboard_focus;
59 struct wl_list link;
60 struct wl_listener seat_destroy_listener;
61 struct wl_listener surface_destroy_listener;
62};
63
Philip Withnall648a4dd2013-11-25 18:01:44 +000064/*
65 * Surface stacking and ordering.
66 *
67 * This is handled using several linked lists of surfaces, organised into
68 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
69 * above all of the surfaces in the layer below. The set of layers is static and
70 * in the following order (top-most first):
71 * • Lock layer (only ever displayed on its own)
72 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010073 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000074 * • Fullscreen layer
75 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000076 * • Workspace layers
77 * • Background layer
78 *
79 * The list of layers may be manipulated to remove whole layers of surfaces from
80 * display. For example, when locking the screen, all layers except the lock
81 * layer are removed.
82 *
83 * A surface’s layer is modified on configuring the surface, in
84 * set_surface_type() (which is only called when the surface’s type change is
85 * _committed_). If a surface’s type changes (e.g. when making a window
86 * fullscreen) its layer changes too.
87 *
88 * In order to allow popup and transient surfaces to be correctly stacked above
89 * their parent surfaces, each surface tracks both its parent surface, and a
90 * linked list of its children. When a surface’s layer is updated, so are the
91 * layers of its children. Note that child surfaces are *not* the same as
92 * subsurfaces — child/parent surfaces are purely for maintaining stacking
93 * order.
94 *
95 * The children_link list of siblings of a surface (i.e. those surfaces which
96 * have the same parent) only contains weston_surfaces which have a
97 * shell_surface. Stacking is not implemented for non-shell_surface
98 * weston_surfaces. This means that the following implication does *not* hold:
99 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
100 */
101
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200102struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500103 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200104
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200105 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500106 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600107 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700108
Tiago Vignattibe143262012-04-16 17:31:41 +0300109 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200110
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500111 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800112 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800113 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700114 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800115 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800123 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500124 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800125 } fullscreen;
126
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200127 struct weston_transform workspace_transform;
128
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500129 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200131
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700132 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200133 bool fullscreen;
134 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100135 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200136 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700137
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200138 struct {
139 bool is_set;
140 int32_t x;
141 int32_t y;
142 } xwayland;
143
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500144 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500145
146 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200147};
148
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300149struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400150 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300151 struct shell_surface *shsurf;
152 struct wl_listener shsurf_destroy_listener;
153};
154
Rusty Lynch1084da52013-08-15 09:10:08 -0700155struct shell_touch_grab {
156 struct weston_touch_grab grab;
157 struct shell_surface *shsurf;
158 struct wl_listener shsurf_destroy_listener;
159 struct weston_touch *touch;
160};
161
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300162struct weston_move_grab {
163 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100164 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500165 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500166};
167
Rusty Lynch1084da52013-08-15 09:10:08 -0700168struct weston_touch_move_grab {
169 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800170 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700171 wl_fixed_t dx, dy;
172};
173
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300175 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500176 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200177 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200178 float x;
179 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200180 } center;
181};
182
Giulio Camuffo5085a752013-03-25 21:42:45 +0100183struct shell_seat {
184 struct weston_seat *seat;
185 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500186 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100187
Jason Ekstrand024177c2014-04-21 19:42:58 -0500188 struct wl_listener caps_changed_listener;
189 struct wl_listener pointer_focus_listener;
190 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100191};
192
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800193
Alex Wubd3354b2012-04-17 17:20:49 +0800194static struct desktop_shell *
195shell_surface_get_shell(struct shell_surface *shsurf);
196
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500197static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200198set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
199
200static void
Derek Foreman74de4692015-07-15 13:00:39 -0500201surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500202
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300203static void
204shell_fade_startup(struct desktop_shell *shell);
205
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700206static void
207shell_fade(struct desktop_shell *shell, enum fade_type type);
208
Philip Withnallbecb77e2013-11-25 18:01:30 +0000209static struct shell_seat *
210get_shell_seat(struct weston_seat *seat);
211
Jonny Lambd73c6942014-08-20 15:53:20 +0200212static void
213get_output_panel_size(struct desktop_shell *shell,
214 struct weston_output *output,
215 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700216
Philip Withnall648a4dd2013-11-25 18:01:44 +0000217static void
218shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
219
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220static int
221shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
222{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200224 struct weston_desktop_surface *desktop_surface =
225 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300226
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200227 t = weston_desktop_surface_get_title(desktop_surface);
228 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300229
230 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200231 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300232 t ? " '" : "", t ?: "", t ? "'" : "",
233 c ? " of " : "", c ?: "");
234}
235
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500236static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400237destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300238{
239 struct shell_grab *grab;
240
241 grab = container_of(listener, struct shell_grab,
242 shsurf_destroy_listener);
243
244 grab->shsurf = NULL;
245}
246
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800247struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248get_default_view(struct weston_surface *surface)
249{
250 struct shell_surface *shsurf;
251 struct weston_view *view;
252
253 if (!surface || wl_list_empty(&surface->views))
254 return NULL;
255
256 shsurf = get_shell_surface(surface);
257 if (shsurf)
258 return shsurf->view;
259
260 wl_list_for_each(view, &surface->views, surface_link)
261 if (weston_view_is_mapped(view))
262 return view;
263
264 return container_of(surface->views.next, struct weston_view, surface_link);
265}
266
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300267static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300268shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400269 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400271 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800272 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300274 struct desktop_shell *shell = shsurf->shell;
275
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200276 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400277
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278 grab->grab.interface = interface;
279 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400280 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500281 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400282 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700284 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400285 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400286 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800287 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400288 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 weston_pointer_set_focus(pointer,
290 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400291 wl_fixed_from_int(0),
292 wl_fixed_from_int(0));
293 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294}
295
Jonny Lambd73c6942014-08-20 15:53:20 +0200296static void
Quentin Glidic581df062016-06-23 18:55:19 +0200297get_panel_size(struct desktop_shell *shell,
298 struct weston_view *view,
299 int *width,
300 int *height)
301{
302 float x1, y1;
303 float x2, y2;
304 weston_view_to_global_float(view, 0, 0, &x1, &y1);
305 weston_view_to_global_float(view,
306 view->surface->width,
307 view->surface->height,
308 &x2, &y2);
309 *width = (int)(x2 - x1);
310 *height = (int)(y2 - y1);
311}
312
313static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200314get_output_panel_size(struct desktop_shell *shell,
315 struct weston_output *output,
316 int *width,
317 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700318{
319 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200320
321 *width = 0;
322 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700323
324 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200325 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700326
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300327 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200328 if (view->surface->output == output) {
329 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200330 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700331 }
332 }
333
Jonny Lambd73c6942014-08-20 15:53:20 +0200334 /* the correct view wasn't found */
335}
336
337static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200338get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200339 struct weston_output *output,
340 pixman_rectangle32_t *area)
341{
342 int32_t panel_width = 0, panel_height = 0;
343
Derek Foremanf814c5d2015-04-15 12:23:54 -0500344 area->x = output->x;
345 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200346
347 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200348 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800349 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200350 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500351 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000352 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800353 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200354 area->width = output->width;
355 area->height = output->height - panel_height;
356 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800357 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500358 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000359 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800360 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200361 area->width = output->width - panel_width;
362 area->height = output->height;
363 break;
364 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700365}
366
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400367static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300368shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300369{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700370 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400371 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700372 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400373
374 if (grab->shsurf->resize_edges) {
375 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400376 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700377 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300378
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700379 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300380}
381
382static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700383shell_touch_grab_start(struct shell_touch_grab *grab,
384 const struct weston_touch_grab_interface *interface,
385 struct shell_surface *shsurf,
386 struct weston_touch *touch)
387{
388 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800389
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200390 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700391
Rusty Lynch1084da52013-08-15 09:10:08 -0700392 grab->grab.interface = interface;
393 grab->shsurf = shsurf;
394 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
395 wl_signal_add(&shsurf->destroy_signal,
396 &grab->shsurf_destroy_listener);
397
398 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700399 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700400
401 weston_touch_start_grab(touch, &grab->grab);
402 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500403 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700405}
406
407static void
408shell_touch_grab_end(struct shell_touch_grab *grab)
409{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700410 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700411 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700412 grab->shsurf->grabbed = 0;
413 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700414
415 weston_touch_end_grab(grab->touch);
416}
417
418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500419center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800420 struct weston_output *output);
421
Daniel Stone496ca172012-05-30 16:31:42 +0100422static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300423get_modifier(char *modifier)
424{
425 if (!modifier)
426 return MODIFIER_SUPER;
427
428 if (!strcmp("ctrl", modifier))
429 return MODIFIER_CTRL;
430 else if (!strcmp("alt", modifier))
431 return MODIFIER_ALT;
432 else if (!strcmp("super", modifier))
433 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000434 else if (!strcmp("none", modifier))
435 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300436 else
437 return MODIFIER_SUPER;
438}
439
Juan Zhaoe10d2792012-04-25 19:09:52 +0800440static enum animation_type
441get_animation_type(char *animation)
442{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800443 if (!animation)
444 return ANIMATION_NONE;
445
Juan Zhaoe10d2792012-04-25 19:09:52 +0800446 if (!strcmp("zoom", animation))
447 return ANIMATION_ZOOM;
448 else if (!strcmp("fade", animation))
449 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100450 else if (!strcmp("dim-layer", animation))
451 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800452 else
453 return ANIMATION_NONE;
454}
455
Alex Wu4539b082012-03-01 12:57:46 +0800456static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400457shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200458{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400459 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500460 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300461 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000462 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200463
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300464 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400465 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300466 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
467 WESTON_SHELL_CLIENT);
468 if (ret < 0)
469 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400470 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500471 "client", &s, client);
472 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100473 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000474
475 weston_config_section_get_bool(section,
476 "allow-zap", &allow_zap, true);
477 shell->allow_zap = allow_zap;
478
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100479 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400480 "binding-modifier", &s, "super");
481 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200482 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800483
484 weston_config_section_get_string(section,
485 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000486 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800487 free(s);
488
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400489 weston_config_section_get_string(section, "animation", &s, "none");
490 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200491 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200492 weston_config_section_get_string(section, "close-animation", &s, "fade");
493 shell->win_close_animation_type = get_animation_type(s);
494 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700495 weston_config_section_get_string(section,
496 "startup-animation", &s, "fade");
497 shell->startup_animation_type = get_animation_type(s);
498 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700499 if (shell->startup_animation_type == ANIMATION_ZOOM)
500 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100501 weston_config_section_get_string(section, "focus-animation", &s, "none");
502 shell->focus_animation_type = get_animation_type(s);
503 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400504 weston_config_section_get_uint(section, "num-workspaces",
505 &shell->workspaces.num,
506 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200507}
508
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800509struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100510get_default_output(struct weston_compositor *compositor)
511{
Armin Krezović10b06182016-06-23 11:59:30 +0200512 if (wl_list_empty(&compositor->output_list))
513 return NULL;
514
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100515 return container_of(compositor->output_list.next,
516 struct weston_output, link);
517}
518
Pekka Paalanen8274d902014-08-06 19:36:51 +0300519static int
520focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
521{
522 return snprintf(buf, len, "focus highlight effect for output %s",
523 surface->output->name);
524}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525
526/* no-op func for checking focus surface */
527static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200528focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100529{
530}
531
532static struct focus_surface *
533get_focus_surface(struct weston_surface *surface)
534{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200535 if (surface->committed == focus_surface_committed)
536 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100537 else
538 return NULL;
539}
540
541static bool
542is_focus_surface (struct weston_surface *es)
543{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200544 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100545}
546
547static bool
548is_focus_view (struct weston_view *view)
549{
550 return is_focus_surface (view->surface);
551}
552
553static struct focus_surface *
554create_focus_surface(struct weston_compositor *ec,
555 struct weston_output *output)
556{
557 struct focus_surface *fsurf = NULL;
558 struct weston_surface *surface = NULL;
559
560 fsurf = malloc(sizeof *fsurf);
561 if (!fsurf)
562 return NULL;
563
564 fsurf->surface = weston_surface_create(ec);
565 surface = fsurf->surface;
566 if (surface == NULL) {
567 free(fsurf);
568 return NULL;
569 }
570
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200571 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100572 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200573 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200574 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300575 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100576
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800577 fsurf->view = weston_view_create(surface);
578 if (fsurf->view == NULL) {
579 weston_surface_destroy(surface);
580 free(fsurf);
581 return NULL;
582 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100583 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200584 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100585
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600586 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600587 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100588 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
589 pixman_region32_fini(&surface->opaque);
590 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
591 output->width, output->height);
592 pixman_region32_fini(&surface->input);
593 pixman_region32_init(&surface->input);
594
595 wl_list_init(&fsurf->workspace_transform.link);
596
597 return fsurf;
598}
599
600static void
601focus_surface_destroy(struct focus_surface *fsurf)
602{
603 weston_surface_destroy(fsurf->surface);
604 free(fsurf);
605}
606
607static void
608focus_animation_done(struct weston_view_animation *animation, void *data)
609{
610 struct workspace *ws = data;
611
612 ws->focus_animation = NULL;
613}
614
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200615static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200616focus_state_destroy(struct focus_state *state)
617{
618 wl_list_remove(&state->seat_destroy_listener.link);
619 wl_list_remove(&state->surface_destroy_listener.link);
620 free(state);
621}
622
623static void
624focus_state_seat_destroy(struct wl_listener *listener, void *data)
625{
626 struct focus_state *state = container_of(listener,
627 struct focus_state,
628 seat_destroy_listener);
629
630 wl_list_remove(&state->link);
631 focus_state_destroy(state);
632}
633
634static void
635focus_state_surface_destroy(struct wl_listener *listener, void *data)
636{
637 struct focus_state *state = container_of(listener,
638 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400639 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200640 struct weston_surface *main_surface;
641 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500642 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200643
Pekka Paalanen01388e22013-04-25 13:57:44 +0300644 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
645
Kristian Høgsberge3778222012-07-31 17:29:30 -0400646 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300647 wl_list_for_each(view,
648 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400650 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100651 if (is_focus_view(view))
652 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200653 if (!get_shell_surface(view->surface))
654 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400655
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200656 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400657 break;
658 }
659
Pekka Paalanen01388e22013-04-25 13:57:44 +0300660 /* if the focus was a sub-surface, activate its main surface */
661 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200662 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300663
Kristian Høgsberge3778222012-07-31 17:29:30 -0400664 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100665 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200666 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200667 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400668 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200669 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100670 if (state->ws->focus_animation)
671 weston_view_animation_destroy(state->ws->focus_animation);
672
673 state->ws->focus_animation = weston_fade_run(
674 state->ws->fsurf_front->view,
675 state->ws->fsurf_front->view->alpha, 0.0, 300,
676 focus_animation_done, state->ws);
677 }
678
Kristian Høgsberge3778222012-07-31 17:29:30 -0400679 wl_list_remove(&state->link);
680 focus_state_destroy(state);
681 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200682}
683
684static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200685focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
686 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200687{
Jonas Ådahl04769742012-06-13 00:01:24 +0200688 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200689
690 state = malloc(sizeof *state);
691 if (state == NULL)
692 return NULL;
693
Quentin Glidicfff39812016-08-15 10:56:52 +0200694 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400696 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200697 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400698 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200699
700 state->seat_destroy_listener.notify = focus_state_seat_destroy;
701 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400702 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200703 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400704 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200705
706 return state;
707}
708
Jonas Ådahl8538b222012-08-29 22:13:03 +0200709static struct focus_state *
710ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
711{
712 struct workspace *ws = get_current_workspace(shell);
713 struct focus_state *state;
714
715 wl_list_for_each(state, &ws->focus_list, link)
716 if (state->seat == seat)
717 break;
718
719 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200720 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200721
722 return state;
723}
724
Jonas Ådahl04769742012-06-13 00:01:24 +0200725static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800726focus_state_set_focus(struct focus_state *state,
727 struct weston_surface *surface)
728{
729 if (state->keyboard_focus) {
730 wl_list_remove(&state->surface_destroy_listener.link);
731 wl_list_init(&state->surface_destroy_listener.link);
732 }
733
734 state->keyboard_focus = surface;
735 if (surface)
736 wl_signal_add(&surface->destroy_signal,
737 &state->surface_destroy_listener);
738}
739
740static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400741restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200742{
743 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400744 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100745 struct wl_list pending_seat_list;
746 struct weston_seat *seat, *next_seat;
747
748 /* Temporarily steal the list of seats so that we can keep
749 * track of the seats we've already processed */
750 wl_list_init(&pending_seat_list);
751 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
752 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200753
754 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500755 struct weston_keyboard *keyboard =
756 weston_seat_get_keyboard(state->seat);
757
Neil Roberts4237f502014-04-09 16:33:31 +0100758 wl_list_remove(&state->seat->link);
759 wl_list_insert(&shell->compositor->seat_list,
760 &state->seat->link);
761
Derek Foreman1281a362015-07-31 16:55:32 -0500762 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800763 continue;
764
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400765 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200766
Derek Foreman1281a362015-07-31 16:55:32 -0500767 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200768 }
Neil Roberts4237f502014-04-09 16:33:31 +0100769
770 /* For any remaining seats that we don't have a focus state
771 * for we'll reset the keyboard focus to NULL */
772 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500773 struct weston_keyboard *keyboard =
774 weston_seat_get_keyboard(seat);
775
Neil Roberts4237f502014-04-09 16:33:31 +0100776 wl_list_insert(&shell->compositor->seat_list, &seat->link);
777
Derek Foreman1281a362015-07-31 16:55:32 -0500778 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100779 continue;
780
Derek Foreman1281a362015-07-31 16:55:32 -0500781 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100782 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200783}
784
785static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
787 struct weston_seat *seat)
788{
Derek Foreman1281a362015-07-31 16:55:32 -0500789 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200790 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791
792 wl_list_for_each(state, &ws->focus_list, link) {
793 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500794 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200795 return;
796 }
797 }
798}
799
800static void
801drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
802 struct weston_surface *surface)
803{
804 struct focus_state *state;
805
806 wl_list_for_each(state, &ws->focus_list, link)
807 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800808 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200809}
810
811static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100812animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
813 struct weston_view *from, struct weston_view *to)
814{
815 struct weston_output *output;
816 bool focus_surface_created = false;
817
818 /* FIXME: Only support dim animation using two layers */
819 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
820 return;
821
822 output = get_default_output(shell->compositor);
823 if (ws->fsurf_front == NULL && (from || to)) {
824 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800825 if (ws->fsurf_front == NULL)
826 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100827 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800828
829 ws->fsurf_back = create_focus_surface(shell->compositor, output);
830 if (ws->fsurf_back == NULL) {
831 focus_surface_destroy(ws->fsurf_front);
832 return;
833 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800835
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 focus_surface_created = true;
837 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300838 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
839 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100840 }
841
842 if (ws->focus_animation) {
843 weston_view_animation_destroy(ws->focus_animation);
844 ws->focus_animation = NULL;
845 }
846
847 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_insert(&to->layer_link,
849 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300851 weston_layer_entry_insert(&ws->layer.view_list,
852 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853
854 if (focus_surface_created) {
855 ws->focus_animation = weston_fade_run(
856 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200857 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 focus_animation_done, ws);
859 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300860 weston_layer_entry_insert(&from->layer_link,
861 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100862 ws->focus_animation = weston_stable_fade_run(
863 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200864 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 focus_animation_done, ws);
866 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300867 weston_layer_entry_insert(&ws->layer.view_list,
868 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100869 ws->focus_animation = weston_stable_fade_run(
870 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200871 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 focus_animation_done, ws);
873 }
874}
875
876static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200877workspace_destroy(struct workspace *ws)
878{
Jonas Ådahl04769742012-06-13 00:01:24 +0200879 struct focus_state *state, *next;
880
881 wl_list_for_each_safe(state, next, &ws->focus_list, link)
882 focus_state_destroy(state);
883
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100884 if (ws->fsurf_front)
885 focus_surface_destroy(ws->fsurf_front);
886 if (ws->fsurf_back)
887 focus_surface_destroy(ws->fsurf_back);
888
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889 free(ws);
890}
891
Jonas Ådahl04769742012-06-13 00:01:24 +0200892static void
893seat_destroyed(struct wl_listener *listener, void *data)
894{
895 struct weston_seat *seat = data;
896 struct focus_state *state, *next;
897 struct workspace *ws = container_of(listener,
898 struct workspace,
899 seat_destroyed_listener);
900
901 wl_list_for_each_safe(state, next, &ws->focus_list, link)
902 if (state->seat == seat)
903 wl_list_remove(&state->link);
904}
905
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200906static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100907workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200908{
909 struct workspace *ws = malloc(sizeof *ws);
910 if (ws == NULL)
911 return NULL;
912
Quentin Glidic82681572016-12-17 13:40:51 +0100913 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200914
Jonas Ådahl04769742012-06-13 00:01:24 +0200915 wl_list_init(&ws->focus_list);
916 wl_list_init(&ws->seat_destroyed_listener.link);
917 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100918 ws->fsurf_front = NULL;
919 ws->fsurf_back = NULL;
920 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200921
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200922 return ws;
923}
924
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925static int
926workspace_is_empty(struct workspace *ws)
927{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300928 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200929}
930
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200931static struct workspace *
932get_workspace(struct desktop_shell *shell, unsigned int index)
933{
934 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200935 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200936 pws += index;
937 return *pws;
938}
939
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800940struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941get_current_workspace(struct desktop_shell *shell)
942{
943 return get_workspace(shell, shell->workspaces.current);
944}
945
946static void
947activate_workspace(struct desktop_shell *shell, unsigned int index)
948{
949 struct workspace *ws;
950
951 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100952 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200953
954 shell->workspaces.current = index;
955}
956
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200957static unsigned int
958get_output_height(struct weston_output *output)
959{
960 return abs(output->region.extents.y1 - output->region.extents.y2);
961}
962
963static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200966 struct weston_transform *transform;
967
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100968 if (is_focus_view(view)) {
969 struct focus_surface *fsurf = get_focus_surface(view->surface);
970 transform = &fsurf->workspace_transform;
971 } else {
972 struct shell_surface *shsurf = get_shell_surface(view->surface);
973 transform = &shsurf->workspace_transform;
974 }
975
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200979
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980 weston_matrix_init(&transform->matrix);
981 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984}
985
986static void
987workspace_translate_out(struct workspace *ws, double fraction)
988{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990 unsigned int height;
991 double d;
992
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300993 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995 d = height * fraction;
996
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998 }
999}
1000
1001static void
1002workspace_translate_in(struct workspace *ws, double fraction)
1003{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 unsigned int height;
1006 double d;
1007
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001008 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010
1011 if (fraction > 0)
1012 d = -(height - height * fraction);
1013 else
1014 d = height + height * fraction;
1015
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017 }
1018}
1019
1020static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021reverse_workspace_change_animation(struct desktop_shell *shell,
1022 unsigned int index,
1023 struct workspace *from,
1024 struct workspace *to)
1025{
1026 shell->workspaces.current = index;
1027
1028 shell->workspaces.anim_to = to;
1029 shell->workspaces.anim_from = from;
1030 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001031 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032
Quentin Glidic82681572016-12-17 13:40:51 +01001033 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1034 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1035
Scott Moreau4272e632012-08-13 09:58:41 -06001036 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001037}
1038
1039static void
1040workspace_deactivate_transforms(struct workspace *ws)
1041{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001043 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001045 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001046 if (is_focus_view(view)) {
1047 struct focus_surface *fsurf = get_focus_surface(view->surface);
1048 transform = &fsurf->workspace_transform;
1049 } else {
1050 struct shell_surface *shsurf = get_shell_surface(view->surface);
1051 transform = &shsurf->workspace_transform;
1052 }
1053
1054 if (!wl_list_empty(&transform->link)) {
1055 wl_list_remove(&transform->link);
1056 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001057 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001059 }
1060}
1061
1062static void
1063finish_workspace_change_animation(struct desktop_shell *shell,
1064 struct workspace *from,
1065 struct workspace *to)
1066{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001067 struct weston_view *view;
1068
Scott Moreau4272e632012-08-13 09:58:41 -06001069 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001071 /* Views that extend past the bottom of the output are still
1072 * visible after the workspace animation ends but before its layer
1073 * is hidden. In that case, we need to damage below those views so
1074 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001075 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001076 weston_view_damage_below(view);
1077
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 wl_list_remove(&shell->workspaces.animation.link);
1079 workspace_deactivate_transforms(from);
1080 workspace_deactivate_transforms(to);
1081 shell->workspaces.anim_to = NULL;
1082
Quentin Glidic82681572016-12-17 13:40:51 +01001083 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001084}
1085
1086static void
1087animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001088 struct weston_output *output,
1089 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090{
1091 struct desktop_shell *shell =
1092 container_of(animation, struct desktop_shell,
1093 workspaces.animation);
1094 struct workspace *from = shell->workspaces.anim_from;
1095 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001096 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 double x, y;
1098
1099 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1100 finish_workspace_change_animation(shell, from, to);
1101 return;
1102 }
1103
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001104 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001106 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001108 timespec_add_msec(&shell->workspaces.anim_timestamp,
1109 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001110 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001111 -(asin(1.0 - shell->workspaces.anim_current) *
1112 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1113 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 }
1115
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001116 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117
1118 /*
1119 * x = [0, π/2]
1120 * y(x) = sin(x)
1121 */
1122 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1123 y = sin(x);
1124
1125 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001126 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127
1128 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1129 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1130 shell->workspaces.anim_current = y;
1131
Scott Moreau4272e632012-08-13 09:58:41 -06001132 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001134 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136}
1137
1138static void
1139animate_workspace_change(struct desktop_shell *shell,
1140 unsigned int index,
1141 struct workspace *from,
1142 struct workspace *to)
1143{
1144 struct weston_output *output;
1145
1146 int dir;
1147
1148 if (index > shell->workspaces.current)
1149 dir = -1;
1150 else
1151 dir = 1;
1152
1153 shell->workspaces.current = index;
1154
1155 shell->workspaces.anim_dir = dir;
1156 shell->workspaces.anim_from = from;
1157 shell->workspaces.anim_to = to;
1158 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001159 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160
1161 output = container_of(shell->compositor->output_list.next,
1162 struct weston_output, link);
1163 wl_list_insert(&output->animation_list,
1164 &shell->workspaces.animation.link);
1165
Quentin Glidic82681572016-12-17 13:40:51 +01001166 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1167 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168
1169 workspace_translate_in(to, 0);
1170
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001171 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001172
Scott Moreau4272e632012-08-13 09:58:41 -06001173 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001174}
1175
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001176static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001177update_workspace(struct desktop_shell *shell, unsigned int index,
1178 struct workspace *from, struct workspace *to)
1179{
1180 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001181 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1182 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001183}
1184
1185static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186change_workspace(struct desktop_shell *shell, unsigned int index)
1187{
1188 struct workspace *from;
1189 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001190 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001191
1192 if (index == shell->workspaces.current)
1193 return;
1194
1195 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001196 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001197 return;
1198
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001199 from = get_current_workspace(shell);
1200 to = get_workspace(shell, index);
1201
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 if (shell->workspaces.anim_from == to &&
1203 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001204 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001205 reverse_workspace_change_animation(shell, index, from, to);
1206 return;
1207 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001208
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001209 if (shell->workspaces.anim_to != NULL)
1210 finish_workspace_change_animation(shell,
1211 shell->workspaces.anim_from,
1212 shell->workspaces.anim_to);
1213
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001215
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001216 if (shell->focus_animation_type != ANIMATION_NONE) {
1217 wl_list_for_each(state, &from->focus_list, link)
1218 if (state->keyboard_focus)
1219 animate_focus_change(shell, from,
1220 get_default_view(state->keyboard_focus), NULL);
1221
1222 wl_list_for_each(state, &to->focus_list, link)
1223 if (state->keyboard_focus)
1224 animate_focus_change(shell, to,
1225 NULL, get_default_view(state->keyboard_focus));
1226 }
1227
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 if (workspace_is_empty(to) && workspace_is_empty(from))
1229 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001230 else
1231 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001232}
1233
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001234static bool
1235workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1236{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001237 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 struct wl_list *e;
1239
1240 if (wl_list_empty(list))
1241 return false;
1242
1243 e = list->next;
1244
1245 if (e->next != list)
1246 return false;
1247
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001248 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001249}
1250
1251static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001252surface_keyboard_focus_lost(struct weston_surface *surface)
1253{
1254 struct weston_compositor *compositor = surface->compositor;
1255 struct weston_seat *seat;
1256 struct weston_surface *focus;
1257
1258 wl_list_for_each(seat, &compositor->seat_list, link) {
1259 struct weston_keyboard *keyboard =
1260 weston_seat_get_keyboard(seat);
1261
1262 if (!keyboard)
1263 continue;
1264
1265 focus = weston_surface_get_main_surface(keyboard->focus);
1266 if (focus == surface)
1267 weston_keyboard_set_focus(keyboard, NULL);
1268 }
1269}
1270
1271static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001273 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274 unsigned int index)
1275{
Derek Foreman1281a362015-07-31 16:55:32 -05001276 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001277 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279 struct shell_surface *shsurf;
1280 struct workspace *from;
1281 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001282 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283
Derek Foreman1281a362015-07-31 16:55:32 -05001284 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 view = get_default_view(surface);
1286 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001287 index == shell->workspaces.current ||
1288 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 return;
1290
1291 from = get_current_workspace(shell);
1292 to = get_workspace(shell, index);
1293
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001294 weston_layer_entry_remove(&view->layer_link);
1295 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296
Philip Withnall659163d2013-11-25 18:01:36 +00001297 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001298 if (shsurf != NULL)
1299 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001300
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302 drop_focus_state(shell, from, surface);
1303
1304 if (shell->workspaces.anim_from == to &&
1305 shell->workspaces.anim_to == from) {
1306 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001307
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 return;
1309 }
1310
1311 if (shell->workspaces.anim_to != NULL)
1312 finish_workspace_change_animation(shell,
1313 shell->workspaces.anim_from,
1314 shell->workspaces.anim_to);
1315
1316 if (workspace_is_empty(from) &&
1317 workspace_has_only(to, surface))
1318 update_workspace(shell, index, from, to);
1319 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001320 if (shsurf != NULL &&
1321 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001322 wl_list_insert(&shell->workspaces.anim_sticky_list,
1323 &shsurf->workspace_transform.link);
1324
1325 animate_workspace_change(shell, index, from, to);
1326 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
Jonas Ådahl8538b222012-08-29 22:13:03 +02001328 state = ensure_focus_state(shell, seat);
1329 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001330 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001331}
1332
1333static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001334touch_move_grab_down(struct weston_touch_grab *grab,
1335 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001336 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001337{
1338}
1339
1340static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001341touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1342 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001343{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001344 struct weston_touch_move_grab *move =
1345 (struct weston_touch_move_grab *) container_of(
1346 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001347
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001348 if (touch_id == 0)
1349 move->active = 0;
1350
Jonas Ådahl9484b692013-12-02 22:05:03 +01001351 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001352 shell_touch_grab_end(&move->base);
1353 free(move);
1354 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001355}
1356
1357static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001358touch_move_grab_motion(struct weston_touch_grab *grab,
1359 const struct timespec *time, int touch_id,
1360 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001361{
1362 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1363 struct shell_surface *shsurf = move->base.shsurf;
1364 struct weston_surface *es;
1365 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1366 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1367
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001368 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001369 return;
1370
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001371 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001372
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001373 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001374
1375 weston_compositor_schedule_repaint(es->compositor);
1376}
1377
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001378static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001379touch_move_grab_frame(struct weston_touch_grab *grab)
1380{
1381}
1382
1383static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001384touch_move_grab_cancel(struct weston_touch_grab *grab)
1385{
1386 struct weston_touch_move_grab *move =
1387 (struct weston_touch_move_grab *) container_of(
1388 grab, struct shell_touch_grab, grab);
1389
1390 shell_touch_grab_end(&move->base);
1391 free(move);
1392}
1393
Rusty Lynch1084da52013-08-15 09:10:08 -07001394static const struct weston_touch_grab_interface touch_move_grab_interface = {
1395 touch_move_grab_down,
1396 touch_move_grab_up,
1397 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001398 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001399 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001400};
1401
1402static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001403surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001404{
1405 struct weston_touch_move_grab *move;
1406
1407 if (!shsurf)
1408 return -1;
1409
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001410 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1411 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001412 return 0;
1413
1414 move = malloc(sizeof *move);
1415 if (!move)
1416 return -1;
1417
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001418 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001420 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001422 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001423
1424 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001425 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001426
1427 return 0;
1428}
1429
1430static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001431noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001432{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001433}
1434
1435static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001436noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001437 const struct timespec *time,
1438 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001439{
1440}
1441
1442static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001443noop_grab_axis_source(struct weston_pointer_grab *grab,
1444 uint32_t source)
1445{
1446}
1447
1448static void
1449noop_grab_frame(struct weston_pointer_grab *grab)
1450{
1451}
1452
1453static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001454constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1455{
1456 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001457 struct weston_surface *surface =
1458 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001459 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001460 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001461 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001462 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001463 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001464
1465 x = wl_fixed_to_int(pointer->x + move->dx);
1466 y = wl_fixed_to_int(pointer->y + move->dy);
1467
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001468 if (shsurf->shell->panel_position ==
1469 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001470 get_output_work_area(shsurf->shell, surface->output, &area);
1471 geometry =
1472 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001473
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001474 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001475 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001476 y = area.y + safety - geometry.height
1477 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001478
1479 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001480 y + geometry.y < area.y)
1481 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001482 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001483
1484 *cx = x;
1485 *cy = y;
1486}
1487
1488static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001489move_grab_motion(struct weston_pointer_grab *grab,
1490 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001491 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001492{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001494 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001495 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001496 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001497 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001498
Jonas Ådahld2510102014-10-05 21:39:14 +02001499 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001500 if (!shsurf)
1501 return;
1502
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001503 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1504
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001505 constrain_position(move, &cx, &cy);
1506
1507 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001508
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001509 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001510}
1511
1512static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001513move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001514 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001516 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1517 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001518 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001519 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001520
Daniel Stone4dbadb12012-05-30 16:31:51 +01001521 if (pointer->button_count == 0 &&
1522 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001523 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001524 free(grab);
1525 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001526}
1527
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001528static void
1529move_grab_cancel(struct weston_pointer_grab *grab)
1530{
1531 struct shell_grab *shell_grab =
1532 container_of(grab, struct shell_grab, grab);
1533
1534 shell_grab_end(shell_grab);
1535 free(grab);
1536}
1537
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001538static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001539 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001540 move_grab_motion,
1541 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001542 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001543 noop_grab_axis_source,
1544 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001545 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001546};
1547
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001548static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001549surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001550 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001551{
1552 struct weston_move_grab *move;
1553
1554 if (!shsurf)
1555 return -1;
1556
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001557 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001558 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1559 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001560 return 0;
1561
1562 move = malloc(sizeof *move);
1563 if (!move)
1564 return -1;
1565
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001567 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001569 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001570 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001571
1572 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001573 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001574
1575 return 0;
1576}
1577
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578struct weston_resize_grab {
1579 struct shell_grab base;
1580 uint32_t edges;
1581 int32_t width, height;
1582};
1583
1584static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001585resize_grab_motion(struct weston_pointer_grab *grab,
1586 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001587 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588{
1589 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001590 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591 struct shell_surface *shsurf = resize->base.shsurf;
1592 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001593 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001594 wl_fixed_t from_x, from_y;
1595 wl_fixed_t to_x, to_y;
1596
Jonas Ådahld2510102014-10-05 21:39:14 +02001597 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001598
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001599 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001600 return;
1601
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602 weston_view_from_global_fixed(shsurf->view,
1603 pointer->grab_x, pointer->grab_y,
1604 &from_x, &from_y);
1605 weston_view_from_global_fixed(shsurf->view,
1606 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607
1608 width = resize->width;
1609 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1610 width += wl_fixed_to_int(from_x - to_x);
1611 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1612 width += wl_fixed_to_int(to_x - from_x);
1613 }
1614
1615 height = resize->height;
1616 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1617 height += wl_fixed_to_int(from_y - to_y);
1618 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1619 height += wl_fixed_to_int(to_y - from_y);
1620 }
1621
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001622 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1623 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1624
1625 min_size.width = MAX(1, min_size.width);
1626 min_size.height = MAX(1, min_size.height);
1627
1628 if (width < min_size.width)
1629 width = min_size.width;
1630 else if (max_size.width > 0 && width > max_size.width)
1631 width = max_size.width;
1632 if (height < min_size.height)
1633 height = min_size.height;
1634 else if (max_size.width > 0 && width > max_size.width)
1635 width = max_size.width;
1636 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001637}
1638
1639static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001640resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001641 const struct timespec *time,
1642 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001643{
1644 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001645 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001647 struct weston_desktop_surface *desktop_surface =
1648 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001649
1650 if (pointer->button_count == 0 &&
1651 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001652 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653 shell_grab_end(&resize->base);
1654 free(grab);
1655 }
1656}
1657
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001658static void
1659resize_grab_cancel(struct weston_pointer_grab *grab)
1660{
1661 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001662 struct weston_desktop_surface *desktop_surface =
1663 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001664
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001665 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001666 shell_grab_end(&resize->base);
1667 free(grab);
1668}
1669
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001670static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001671 noop_grab_focus,
1672 resize_grab_motion,
1673 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001674 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001675 noop_grab_axis_source,
1676 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001677 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678};
1679
Giulio Camuffob8366642013-04-25 13:57:46 +03001680/*
1681 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001682 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001683static void
1684surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1685 int32_t *y, int32_t *w, int32_t *h) {
1686 pixman_region32_t region;
1687 pixman_box32_t *box;
1688 struct weston_subsurface *subsurface;
1689
1690 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 surface->width,
1692 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001693
1694 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1695 pixman_region32_union_rect(&region, &region,
1696 subsurface->position.x,
1697 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 subsurface->surface->width,
1699 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001700 }
1701
1702 box = pixman_region32_extents(&region);
1703 if (x)
1704 *x = box->x1;
1705 if (y)
1706 *y = box->y1;
1707 if (w)
1708 *w = box->x2 - box->x1;
1709 if (h)
1710 *h = box->y2 - box->y1;
1711
1712 pixman_region32_fini(&region);
1713}
1714
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715static int
1716surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001717 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001718{
1719 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001720 const unsigned resize_topbottom =
1721 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1722 const unsigned resize_leftright =
1723 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1724 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001725 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001727 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001728 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1729 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730 return 0;
1731
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001732 /* Check for invalid edge combinations. */
1733 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1734 (edges & resize_topbottom) == resize_topbottom ||
1735 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736 return 0;
1737
1738 resize = malloc(sizeof *resize);
1739 if (!resize)
1740 return -1;
1741
1742 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001744 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1745 resize->width = geometry.width;
1746 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001747
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001748 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001749 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001751 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752
1753 return 0;
1754}
1755
1756static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001757busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001759 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001760 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001761 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001763 wl_fixed_t sx, sy;
1764
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 view = weston_compositor_pick_view(pointer->seat->compositor,
1766 pointer->x, pointer->y,
1767 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001768 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001769
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001770 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001771 shell_grab_end(grab);
1772 free(grab);
1773 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001774}
1775
1776static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001777busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1778 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001779 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001780{
Jonas Ådahld2510102014-10-05 21:39:14 +02001781 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001783
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001785busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001786 const struct timespec *time,
1787 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001788{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001789 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001790 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001791 struct weston_pointer *pointer = grab->grab.pointer;
1792 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001793
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001794 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001795 activate(shsurf->shell, shsurf->view, seat,
1796 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001797 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001798 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001799 activate(shsurf->shell, shsurf->view, seat,
1800 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001801 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001802 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803}
1804
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001805static void
1806busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1807{
1808 struct shell_grab *grab = (struct shell_grab *) base;
1809
1810 shell_grab_end(grab);
1811 free(grab);
1812}
1813
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001814static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001815 busy_cursor_grab_focus,
1816 busy_cursor_grab_motion,
1817 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001818 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001819 noop_grab_axis_source,
1820 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001821 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822};
1823
1824static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825handle_pointer_focus(struct wl_listener *listener, void *data)
1826{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001827 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001829 struct shell_surface *shsurf;
1830 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001831
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001833 return;
1834
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001835 shsurf = get_shell_surface(view->surface);
1836 if (!shsurf)
1837 return;
1838
1839 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1840
1841 if (shsurf->unresponsive)
1842 set_busy_cursor(shsurf, pointer);
1843 else
1844 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001845}
1846
1847static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001848shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1849{
1850 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001851 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001852}
1853
1854static void
1855shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1856{
1857 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001858 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001859}
1860
1861static void
1862handle_keyboard_focus(struct wl_listener *listener, void *data)
1863{
1864 struct weston_keyboard *keyboard = data;
1865 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1866
1867 if (seat->focused_surface) {
1868 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1869 if (shsurf)
1870 shell_surface_lose_keyboard_focus(shsurf);
1871 }
1872
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001873 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001874
1875 if (seat->focused_surface) {
1876 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1877 if (shsurf)
1878 shell_surface_gain_keyboard_focus(shsurf);
1879 }
1880}
1881
Philip Withnall07926d92013-11-25 18:01:40 +00001882/* The surface will be inserted into the list immediately after the link
1883 * returned by this function (i.e. will be stacked immediately above the
1884 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001885static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001886shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1887{
1888 struct workspace *ws;
1889
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001890 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1891 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001892 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001893 }
1894
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001895 /* Move the surface to a normal workspace layer so that surfaces
1896 * which were previously fullscreen or transient are no longer
1897 * rendered on top. */
1898 ws = get_current_workspace(shsurf->shell);
1899 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001900}
1901
Philip Withnall648a4dd2013-11-25 18:01:44 +00001902static void
1903shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1904{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001905 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001906}
1907
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001908/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001909 * geometry to ensure the changes are redrawn.
1910 *
1911 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1912 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001913static void
1914shell_surface_update_layer(struct shell_surface *shsurf)
1915{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001916 struct weston_surface *surface =
1917 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001918 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001919
1920 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1921
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001922 if (new_layer_link == NULL)
1923 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001924 if (new_layer_link == &shsurf->view->layer_link)
1925 return;
1926
1927 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001928 weston_layer_entry_remove(&shsurf->view->layer_link);
1929 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001930 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001931 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001932
1933 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001934}
1935
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001936static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001937shell_surface_set_output(struct shell_surface *shsurf,
1938 struct weston_output *output)
1939{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001940 struct weston_surface *es =
1941 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001942
1943 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001944 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001945 if (output)
1946 shsurf->output = output;
1947 else if (es->output)
1948 shsurf->output = es->output;
1949 else
1950 shsurf->output = get_default_output(es->compositor);
1951}
1952
1953static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001954weston_view_set_initial_position(struct weston_view *view,
1955 struct desktop_shell *shell);
1956
1957static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001958unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001959{
Philip Withnallf85fe842013-11-25 18:01:37 +00001960 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001961 wl_list_remove(&shsurf->fullscreen.transform.link);
1962 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001963
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 if (shsurf->fullscreen.black_view)
1965 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1966 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001967
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001968 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;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001974
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001975 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001977 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001978 shsurf->saved_rotation_valid = false;
1979 }
Alex Wu4539b082012-03-01 12:57:46 +08001980}
1981
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001982static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001983unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001984{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001985 struct weston_surface *surface =
1986 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1987
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001988 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001989 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001990
1991 if (shsurf->saved_position_valid)
1992 weston_view_set_position(shsurf->view,
1993 shsurf->saved_x, shsurf->saved_y);
1994 else
1995 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001996 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001997
1998 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001999 wl_list_insert(&shsurf->view->geometry.transformation_list,
2000 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002001 shsurf->saved_rotation_valid = false;
2002 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002003}
2004
Philip Withnallbecb77e2013-11-25 18:01:30 +00002005static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002006set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002007{
2008 struct shell_surface *shsurf;
2009 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002010 struct weston_view *view;
2011
2012 view = get_default_view(surface);
2013 if (!view)
2014 return;
2015
2016 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2017
2018 shsurf = get_shell_surface(surface);
2019 current_ws = get_current_workspace(shsurf->shell);
2020
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002021 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002022 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002023
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002024 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002025 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002026
2027 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002028 weston_view_damage_below(view);
2029}
2030
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002031
Tiago Vignattibe143262012-04-16 17:31:41 +03002032static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002033shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002034{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002035 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002036}
2037
Pekka Paalanen8274d902014-08-06 19:36:51 +03002038static int
2039black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2040{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002041 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002042 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002043 int n;
2044 int rem;
2045 int ret;
2046
2047 n = snprintf(buf, len, "black background surface for ");
2048 if (n < 0)
2049 return n;
2050
2051 rem = (int)len - n;
2052 if (rem < 0)
2053 rem = 0;
2054
2055 if (fs_surface->get_label)
2056 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2057 else
2058 ret = snprintf(buf + n, rem, "<unknown>");
2059
2060 if (ret < 0)
2061 return n;
2062
2063 return n + ret;
2064}
2065
Alex Wu21858432012-04-01 20:13:08 +08002066static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002067black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002068
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002070create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002071 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002072 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002073{
2074 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002076
2077 surface = weston_surface_create(ec);
2078 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002079 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002080 return NULL;
2081 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082 view = weston_view_create(surface);
2083 if (surface == NULL) {
2084 weston_log("no memory\n");
2085 weston_surface_destroy(surface);
2086 return NULL;
2087 }
Alex Wu4539b082012-03-01 12:57:46 +08002088
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002089 surface->committed = black_surface_committed;
2090 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002091 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002092 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002093 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002094 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002095 pixman_region32_fini(&surface->input);
2096 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002097
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002098 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002099 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100
2101 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002102}
2103
Philip Withnalled8f1a92013-11-25 18:01:43 +00002104static void
2105shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2106{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002107 struct weston_surface *surface =
2108 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002109 struct weston_output *output = shsurf->fullscreen_output;
2110
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002111 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002112
2113 if (!shsurf->fullscreen.black_view)
2114 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002115 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002116 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002117 output->x, output->y,
2118 output->width,
2119 output->height);
2120
2121 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002122 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2123 weston_layer_entry_insert(&shsurf->view->layer_link,
2124 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002125 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002126 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002127
Armin Krezović4663aca2016-06-30 06:04:29 +02002128 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002129 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002130}
2131
Alex Wu4539b082012-03-01 12:57:46 +08002132/* Create black surface and append it to the associated fullscreen surface.
2133 * Handle size dismatch and positioning according to the method. */
2134static void
2135shell_configure_fullscreen(struct shell_surface *shsurf)
2136{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002137 struct weston_surface *surface =
2138 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002139 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002140
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002141 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002142 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002143 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2144 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002145
Philip Withnalled8f1a92013-11-25 18:01:43 +00002146 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002147
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002148 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002149 &surf_width, &surf_height);
2150
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002151 if (surface->buffer_ref.buffer)
2152 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002153}
2154
Alex Wu4539b082012-03-01 12:57:46 +08002155static void
2156shell_map_fullscreen(struct shell_surface *shsurf)
2157{
Alex Wubd3354b2012-04-17 17:20:49 +08002158 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002159}
2160
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002161static struct weston_output *
2162get_focused_output(struct weston_compositor *compositor)
2163{
2164 struct weston_seat *seat;
2165 struct weston_output *output = NULL;
2166
2167 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002168 struct weston_touch *touch = weston_seat_get_touch(seat);
2169 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2170 struct weston_keyboard *keyboard =
2171 weston_seat_get_keyboard(seat);
2172
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002173 /* Priority has touch focus, then pointer and
2174 * then keyboard focus. We should probably have
2175 * three for loops and check frist for touch,
2176 * then for pointer, etc. but unless somebody has some
2177 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002178 if (touch && touch->focus)
2179 output = touch->focus->output;
2180 else if (pointer && pointer->focus)
2181 output = pointer->focus->output;
2182 else if (keyboard && keyboard->focus)
2183 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002184
2185 if (output)
2186 break;
2187 }
2188
2189 return output;
2190}
2191
2192static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002193destroy_shell_seat(struct wl_listener *listener, void *data)
2194{
2195 struct shell_seat *shseat =
2196 container_of(listener,
2197 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002198
2199 wl_list_remove(&shseat->seat_destroy_listener.link);
2200 free(shseat);
2201}
2202
Jason Ekstrand024177c2014-04-21 19:42:58 -05002203static void
2204shell_seat_caps_changed(struct wl_listener *listener, void *data)
2205{
Derek Foreman1281a362015-07-31 16:55:32 -05002206 struct weston_keyboard *keyboard;
2207 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002208 struct shell_seat *seat;
2209
2210 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002211 keyboard = weston_seat_get_keyboard(seat->seat);
2212 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002213
Derek Foreman1281a362015-07-31 16:55:32 -05002214 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002215 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002216 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002217 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002218 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002219 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002220 wl_list_init(&seat->keyboard_focus_listener.link);
2221 }
2222
Derek Foreman1281a362015-07-31 16:55:32 -05002223 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002224 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002225 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002226 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002227 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002228 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002229 wl_list_init(&seat->pointer_focus_listener.link);
2230 }
2231}
2232
Giulio Camuffo5085a752013-03-25 21:42:45 +01002233static struct shell_seat *
2234create_shell_seat(struct weston_seat *seat)
2235{
2236 struct shell_seat *shseat;
2237
2238 shseat = calloc(1, sizeof *shseat);
2239 if (!shseat) {
2240 weston_log("no memory to allocate shell seat\n");
2241 return NULL;
2242 }
2243
2244 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002245
2246 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2247 wl_signal_add(&seat->destroy_signal,
2248 &shseat->seat_destroy_listener);
2249
Jason Ekstrand024177c2014-04-21 19:42:58 -05002250 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2251 wl_list_init(&shseat->keyboard_focus_listener.link);
2252
2253 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2254 wl_list_init(&shseat->pointer_focus_listener.link);
2255
2256 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2257 wl_signal_add(&seat->updated_caps_signal,
2258 &shseat->caps_changed_listener);
2259 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2260
Giulio Camuffo5085a752013-03-25 21:42:45 +01002261 return shseat;
2262}
2263
2264static struct shell_seat *
2265get_shell_seat(struct weston_seat *seat)
2266{
2267 struct wl_listener *listener;
2268
2269 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002270 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002271
2272 return container_of(listener,
2273 struct shell_seat, seat_destroy_listener);
2274}
2275
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002276static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002277fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002278{
2279 struct shell_surface *shsurf = data;
2280
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002281 weston_surface_destroy(shsurf->view->surface);
2282 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002283}
2284
2285static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002286fade_out_done(struct weston_view_animation *animation, void *data)
2287{
2288 struct shell_surface *shsurf = data;
2289 struct wl_event_loop *loop;
2290
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002291 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002292
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002293 if (weston_view_is_mapped(shsurf->view)) {
2294 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002295 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002296 }
2297}
2298
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002299struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002300get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002301{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002302 if (weston_surface_is_desktop_surface(surface)) {
2303 struct weston_desktop_surface *desktop_surface =
2304 weston_surface_get_desktop_surface(surface);
2305 return weston_desktop_surface_get_user_data(desktop_surface);
2306 }
2307 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002308}
2309
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002310/*
2311 * libweston-desktop
2312 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002313
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002314static void
2315desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2316 void *shell)
2317{
2318 struct weston_desktop_client *client =
2319 weston_desktop_surface_get_client(desktop_surface);
2320 struct wl_client *wl_client =
2321 weston_desktop_client_get_client(client);
2322 struct weston_view *view;
2323 struct shell_surface *shsurf;
2324 struct weston_surface *surface =
2325 weston_desktop_surface_get_surface(desktop_surface);
2326
2327 view = weston_desktop_surface_create_view(desktop_surface);
2328 if (!view)
2329 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002330
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002331 shsurf = calloc(1, sizeof *shsurf);
2332 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002333 if (wl_client)
2334 wl_client_post_no_memory(wl_client);
2335 else
2336 weston_log("no memory to allocate shell surface\n");
2337 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002338 }
2339
Pekka Paalanen8274d902014-08-06 19:36:51 +03002340 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002341
Tiago Vignattibc052c92012-04-19 16:18:18 +03002342 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002343 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002344 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002345 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002346 shsurf->desktop_surface = desktop_surface;
2347 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002349 wl_list_init(&shsurf->fullscreen.transform.link);
2350
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002351 shsurf->output = get_default_output(shsurf->shell->compositor);
2352
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002353 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002354
Pekka Paalanen460099f2012-01-20 16:48:25 +02002355 /* empty when not in use */
2356 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002357 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002358
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002359 wl_list_init(&shsurf->workspace_transform.link);
2360
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002361 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2362 weston_desktop_surface_set_activated(desktop_surface,
2363 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002364}
2365
Tiago Vignattibc052c92012-04-19 16:18:18 +03002366static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002367desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2368 void *shell)
2369{
2370 struct shell_surface *shsurf =
2371 weston_desktop_surface_get_user_data(desktop_surface);
2372 struct weston_surface *surface =
2373 weston_desktop_surface_get_surface(desktop_surface);
2374
2375 if (!shsurf)
2376 return;
2377
2378 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2379
2380 if (shsurf->fullscreen.black_view)
2381 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2382
2383 weston_surface_set_label_func(surface, NULL);
2384 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2385 shsurf->desktop_surface = NULL;
2386
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002387 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002388 if (weston_surface_is_mapped(surface) &&
2389 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2390 pixman_region32_fini(&surface->pending.input);
2391 pixman_region32_init(&surface->pending.input);
2392 pixman_region32_fini(&surface->input);
2393 pixman_region32_init(&surface->input);
2394 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2395 fade_out_done, shsurf);
2396 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002397 weston_view_destroy(shsurf->view);
2398 free(shsurf);
2399 }
2400}
2401
2402static void
2403set_maximized_position(struct desktop_shell *shell,
2404 struct shell_surface *shsurf)
2405{
2406 pixman_rectangle32_t area;
2407 struct weston_geometry geometry;
2408
2409 get_output_work_area(shell, shsurf->output, &area);
2410 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2411
2412 weston_view_set_position(shsurf->view,
2413 area.x - geometry.x,
2414 area.y - geometry.y);
2415}
2416
2417static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002418set_position_from_xwayland(struct shell_surface *shsurf)
2419{
2420 struct weston_geometry geometry;
2421 float x;
2422 float y;
2423
2424 assert(shsurf->xwayland.is_set);
2425
2426 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2427 x = shsurf->xwayland.x - geometry.x;
2428 y = shsurf->xwayland.y - geometry.y;
2429
2430 weston_view_set_position(shsurf->view, x, y);
2431
2432#ifdef WM_DEBUG
2433 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2434 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2435 geometry.x, geometry.y, x, y);
2436#endif
2437}
2438
2439static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002440map(struct desktop_shell *shell, struct shell_surface *shsurf,
2441 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002442{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002443 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002444 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2445 struct weston_compositor *compositor = shell->compositor;
2446 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002447
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002448 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002449 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002450 center_on_output(shsurf->view, shsurf->fullscreen_output);
2451 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002452 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002453 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002454 } else if (shsurf->xwayland.is_set) {
2455 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002456 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002457 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002458 }
2459
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002460 /* Surface stacking order, see also activate(). */
2461 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002462
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002463 weston_view_update_transform(shsurf->view);
2464 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002465 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002466 surface->output = shsurf->output;
2467 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002468 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002469
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002470 if (!shell->locked) {
2471 wl_list_for_each(seat, &compositor->seat_list, link)
2472 activate(shell, shsurf->view, seat,
2473 WESTON_ACTIVATE_FLAG_CONFIGURE);
2474 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002475
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002476 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002477 switch (shell->win_animation_type) {
2478 case ANIMATION_FADE:
2479 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2480 break;
2481 case ANIMATION_ZOOM:
2482 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2483 break;
2484 case ANIMATION_NONE:
2485 default:
2486 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002487 }
2488 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002489}
2490
2491static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002492desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2493 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002494{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002495 struct shell_surface *shsurf =
2496 weston_desktop_surface_get_user_data(desktop_surface);
2497 struct weston_surface *surface =
2498 weston_desktop_surface_get_surface(desktop_surface);
2499 struct weston_view *view = shsurf->view;
2500 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002501 bool was_fullscreen;
2502 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002503
2504 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002505 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002506
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002507 was_fullscreen = shsurf->state.fullscreen;
2508 was_maximized = shsurf->state.maximized;
2509
2510 shsurf->state.fullscreen =
2511 weston_desktop_surface_get_fullscreen(desktop_surface);
2512 shsurf->state.maximized =
2513 weston_desktop_surface_get_maximized(desktop_surface);
2514
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002515 if (!weston_surface_is_mapped(surface)) {
2516 map(shell, shsurf, sx, sy);
2517 surface->is_mapped = true;
2518 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2519 ++surface->ref_count;
2520 return;
2521 }
2522
2523 if (sx == 0 && sy == 0 &&
2524 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002525 shsurf->last_height == surface->height &&
2526 was_fullscreen == shsurf->state.fullscreen &&
2527 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002528 return;
2529
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002530 if (was_fullscreen)
2531 unset_fullscreen(shsurf);
2532 if (was_maximized)
2533 unset_maximized(shsurf);
2534
Quentin Glidic920cf042016-08-16 14:26:20 +02002535 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2536 !shsurf->saved_position_valid) {
2537 shsurf->saved_x = shsurf->view->geometry.x;
2538 shsurf->saved_y = shsurf->view->geometry.y;
2539 shsurf->saved_position_valid = true;
2540
2541 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2542 wl_list_remove(&shsurf->rotation.transform.link);
2543 wl_list_init(&shsurf->rotation.transform.link);
2544 weston_view_geometry_dirty(shsurf->view);
2545 shsurf->saved_rotation_valid = true;
2546 }
2547 }
2548
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002549 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002550 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002551 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 set_maximized_position(shell, shsurf);
2553 surface->output = shsurf->output;
2554 } else {
2555 float from_x, from_y;
2556 float to_x, to_y;
2557 float x, y;
2558
2559 if (shsurf->resize_edges) {
2560 sx = 0;
2561 sy = 0;
2562 }
2563
2564 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2565 sx = shsurf->last_width - surface->width;
2566 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2567 sy = shsurf->last_height - surface->height;
2568
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002569 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2570 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2571 x = shsurf->view->geometry.x + to_x - from_x;
2572 y = shsurf->view->geometry.y + to_y - from_y;
2573
2574 weston_view_set_position(shsurf->view, x, y);
2575 }
2576
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002577 shsurf->last_width = surface->width;
2578 shsurf->last_height = surface->height;
2579
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002580 /* XXX: would a fullscreen surface need the same handling? */
2581 if (surface->output) {
2582 wl_list_for_each(view, &surface->views, surface_link)
2583 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002584 }
2585}
2586
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002587static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002588get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2589{
2590 struct desktop_shell *shell;
2591 pixman_rectangle32_t area;
2592
2593 shell = shell_surface_get_shell(shsurf);
2594 get_output_work_area(shell, shsurf->output, &area);
2595
2596 *width = area.width;
2597 *height = area.height;
2598}
2599
2600static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002601set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2602 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002603{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002604 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2605 struct weston_surface *surface =
2606 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2607 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002608
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002609 if (fullscreen) {
2610 /* handle clients launching in fullscreen */
2611 if (output == NULL && !weston_surface_is_mapped(surface)) {
2612 /* Set the output to the one that has focus currently. */
2613 output = get_focused_output(surface->compositor);
2614 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002615
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002616 shell_surface_set_output(shsurf, output);
2617 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002618
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002619 width = shsurf->output->width;
2620 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002621 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2622 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002623 }
2624 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2625 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002626}
2627
2628static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002629desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2630 struct weston_seat *seat, uint32_t serial, void *shell)
2631{
2632 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2633 struct weston_touch *touch = weston_seat_get_touch(seat);
2634 struct shell_surface *shsurf =
2635 weston_desktop_surface_get_user_data(desktop_surface);
2636 struct weston_surface *surface =
2637 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2638 struct wl_resource *resource = surface->resource;
2639 struct weston_surface *focus;
2640
2641 if (pointer &&
2642 pointer->focus &&
2643 pointer->button_count > 0 &&
2644 pointer->grab_serial == serial) {
2645 focus = weston_surface_get_main_surface(pointer->focus->surface);
2646 if ((focus == surface) &&
2647 (surface_move(shsurf, pointer, true) < 0))
2648 wl_resource_post_no_memory(resource);
2649 } else if (touch &&
2650 touch->focus &&
2651 touch->grab_serial == serial) {
2652 focus = weston_surface_get_main_surface(touch->focus->surface);
2653 if ((focus == surface) &&
2654 (surface_touch_move(shsurf, touch) < 0))
2655 wl_resource_post_no_memory(resource);
2656 }
2657}
2658
2659static void
2660desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2661 struct weston_seat *seat, uint32_t serial,
2662 enum weston_desktop_surface_edge edges, void *shell)
2663{
2664 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2665 struct shell_surface *shsurf =
2666 weston_desktop_surface_get_user_data(desktop_surface);
2667 struct weston_surface *surface =
2668 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2669 struct wl_resource *resource = surface->resource;
2670 struct weston_surface *focus;
2671
2672 if (!pointer ||
2673 pointer->button_count == 0 ||
2674 pointer->grab_serial != serial ||
2675 pointer->focus == NULL)
2676 return;
2677
2678 focus = weston_surface_get_main_surface(pointer->focus->surface);
2679 if (focus != surface)
2680 return;
2681
2682 if (surface_resize(shsurf, pointer, edges) < 0)
2683 wl_resource_post_no_memory(resource);
2684}
2685
2686static void
2687desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2688 bool fullscreen,
2689 struct weston_output *output, void *shell)
2690{
2691 struct shell_surface *shsurf =
2692 weston_desktop_surface_get_user_data(desktop_surface);
2693
2694 set_fullscreen(shsurf, fullscreen, output);
2695}
2696
2697static void
2698set_maximized(struct shell_surface *shsurf, bool maximized)
2699{
2700 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2701 struct weston_surface *surface =
2702 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2703 int32_t width = 0, height = 0;
2704
2705 if (maximized) {
2706 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002707
2708 if (!weston_surface_is_mapped(surface))
2709 output = get_focused_output(surface->compositor);
2710 else
2711 output = surface->output;
2712
2713 shell_surface_set_output(shsurf, output);
2714
Derek Foreman927d9e22017-10-06 14:37:44 -05002715 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002716 }
2717 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2718 weston_desktop_surface_set_size(desktop_surface, width, height);
2719}
2720
2721static void
2722desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2723 bool maximized, void *shell)
2724{
2725 struct shell_surface *shsurf =
2726 weston_desktop_surface_get_user_data(desktop_surface);
2727
2728 set_maximized(shsurf, maximized);
2729}
2730
2731static void
2732desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2733 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002734{
2735 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002736 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002737
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002738 /* apply compositor's own minimization logic (hide) */
2739 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002740}
2741
Rafael Antognollie2a34552013-12-03 15:35:45 -02002742static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002743set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002744{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002745 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002746
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002747 if (pointer->grab->interface == &busy_cursor_grab_interface)
2748 return;
2749
2750 grab = malloc(sizeof *grab);
2751 if (!grab)
2752 return;
2753
2754 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2755 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2756 /* Mark the shsurf as ungrabbed so that button binding is able
2757 * to move it. */
2758 shsurf->grabbed = 0;
2759}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002760
2761static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002762end_busy_cursor(struct weston_compositor *compositor,
2763 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002764{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002765 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002766 struct shell_grab *grab;
2767 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002768
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002769 wl_list_for_each(seat, &compositor->seat_list, link) {
2770 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2771 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002772
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002773 if (!pointer)
2774 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002775
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002776 if (pointer->grab->interface != &busy_cursor_grab_interface)
2777 continue;
2778
2779 grab = (struct shell_grab *) pointer->grab;
2780 shsurf = grab->shsurf;
2781 if (!shsurf)
2782 continue;
2783
2784 grab_client =
2785 weston_desktop_surface_get_client(shsurf->desktop_surface);
2786 if (grab_client == desktop_client) {
2787 shell_grab_end(grab);
2788 free(grab);
2789 }
2790 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002791}
2792
2793static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002794desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2795 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002796{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002797 struct shell_surface *shsurf =
2798 weston_desktop_surface_get_user_data(desktop_surface);
2799 bool *unresponsive = user_data;
2800
2801 shsurf->unresponsive = *unresponsive;
2802}
2803
2804static void
2805desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2806 void *shell_)
2807{
2808 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002809 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002810 struct weston_seat *seat;
2811 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002812
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002813 weston_desktop_client_for_each_surface(desktop_client,
2814 desktop_surface_set_unresponsive,
2815 &unresponsive);
2816
2817
2818 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2819 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2820 struct weston_desktop_client *grab_client;
2821
2822 if (!pointer || !pointer->focus)
2823 continue;
2824
2825 shsurf = get_shell_surface(pointer->focus->surface);
2826 if (!shsurf)
2827 continue;
2828
2829 grab_client =
2830 weston_desktop_surface_get_client(shsurf->desktop_surface);
2831 if (grab_client == desktop_client)
2832 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002833 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002834}
2835
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002836static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002837desktop_surface_pong(struct weston_desktop_client *desktop_client,
2838 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002839{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002840 struct desktop_shell *shell = shell_;
2841 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002842
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002843 weston_desktop_client_for_each_surface(desktop_client,
2844 desktop_surface_set_unresponsive,
2845 &unresponsive);
2846 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002847}
2848
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002849static void
2850desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2851 int32_t x, int32_t y, void *shell_)
2852{
2853 struct shell_surface *shsurf =
2854 weston_desktop_surface_get_user_data(surface);
2855
2856 shsurf->xwayland.x = x;
2857 shsurf->xwayland.y = y;
2858 shsurf->xwayland.is_set = true;
2859}
2860
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002861static const struct weston_desktop_api shell_desktop_api = {
2862 .struct_size = sizeof(struct weston_desktop_api),
2863 .surface_added = desktop_surface_added,
2864 .surface_removed = desktop_surface_removed,
2865 .committed = desktop_surface_committed,
2866 .move = desktop_surface_move,
2867 .resize = desktop_surface_resize,
2868 .fullscreen_requested = desktop_surface_fullscreen_requested,
2869 .maximized_requested = desktop_surface_maximized_requested,
2870 .minimized_requested = desktop_surface_minimized_requested,
2871 .ping_timeout = desktop_surface_ping_timeout,
2872 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002873 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002874};
2875
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002876/* ************************ *
2877 * end of libweston-desktop *
2878 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002879static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002880configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002881{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002882 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002883
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002884 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002885 if (v->output == ev->output && v != ev) {
2886 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002887 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002888 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002889 }
2890 }
2891
Quentin Glidice8bf9592016-06-23 18:55:20 +02002892 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002893 ev->surface->is_mapped = true;
2894 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002895
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002896 if (wl_list_empty(&ev->layer_link.link)) {
2897 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002898 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002899 }
2900}
2901
David Fort50d962f2016-06-09 17:55:52 +02002902
2903static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002904find_shell_output_from_weston_output(struct desktop_shell *shell,
2905 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002906{
2907 struct shell_output *shell_output;
2908
2909 wl_list_for_each(shell_output, &shell->output_list, link) {
2910 if (shell_output->output == output)
2911 return shell_output;
2912 }
2913
2914 return NULL;
2915}
2916
Pekka Paalanen8274d902014-08-06 19:36:51 +03002917static int
2918background_get_label(struct weston_surface *surface, char *buf, size_t len)
2919{
2920 return snprintf(buf, len, "background for output %s",
2921 surface->output->name);
2922}
2923
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002924static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002925background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002926{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002927 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002929
Jason Ekstranda7af7042013-10-12 22:38:11 -05002930 view = container_of(es->views.next, struct weston_view, surface_link);
2931
Quentin Glidice8bf9592016-06-23 18:55:20 +02002932 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002933}
2934
2935static void
David Fort50d962f2016-06-09 17:55:52 +02002936handle_background_surface_destroy(struct wl_listener *listener, void *data)
2937{
2938 struct shell_output *output =
2939 container_of(listener, struct shell_output, background_surface_listener);
2940
2941 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002942 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002943 output->background_surface = NULL;
2944}
2945
2946static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002947desktop_shell_set_background(struct wl_client *client,
2948 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002949 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002950 struct wl_resource *surface_resource)
2951{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002952 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002953 struct weston_surface *surface =
2954 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002955 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002957
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002958 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002959 wl_resource_post_error(surface_resource,
2960 WL_DISPLAY_ERROR_INVALID_OBJECT,
2961 "surface role already assigned");
2962 return;
2963 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002964
Jason Ekstranda7af7042013-10-12 22:38:11 -05002965 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2966 weston_view_destroy(view);
2967 view = weston_view_create(surface);
2968
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002969 surface->committed = background_committed;
2970 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002971 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03002972 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 view->output = surface->output;
David Fort50d962f2016-06-09 17:55:52 +02002974
2975 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02002976 if (sh_output->background_surface) {
2977 /* The output already has a background, tell our helper
2978 * there is no need for another one. */
2979 weston_desktop_shell_send_configure(resource, 0,
2980 surface_resource,
2981 0, 0);
2982 } else {
2983 weston_desktop_shell_send_configure(resource, 0,
2984 surface_resource,
2985 surface->output->width,
2986 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002987
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02002988 sh_output->background_surface = surface;
2989
2990 sh_output->background_surface_listener.notify =
2991 handle_background_surface_destroy;
2992 wl_signal_add(&surface->destroy_signal,
2993 &sh_output->background_surface_listener);
2994 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002995}
2996
Pekka Paalanen8274d902014-08-06 19:36:51 +03002997static int
2998panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2999{
3000 return snprintf(buf, len, "panel for output %s",
3001 surface->output->name);
3002}
3003
Kristian Høgsberg75840622011-09-06 13:48:16 -04003004static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003005panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003006{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003007 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003009 int width, height;
3010 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003011
Jason Ekstranda7af7042013-10-12 22:38:11 -05003012 view = container_of(es->views.next, struct weston_view, surface_link);
3013
Quentin Glidice8bf9592016-06-23 18:55:20 +02003014 get_panel_size(shell, view, &width, &height);
3015 switch (shell->panel_position) {
3016 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3017 break;
3018 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3019 y = view->output->height - height;
3020 break;
3021 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3022 break;
3023 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3024 x = view->output->width - width;
3025 break;
3026 }
3027
3028 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003029}
3030
3031static void
David Fort50d962f2016-06-09 17:55:52 +02003032handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3033{
3034 struct shell_output *output =
3035 container_of(listener, struct shell_output, panel_surface_listener);
3036
3037 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003038 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003039 output->panel_surface = NULL;
3040}
3041
3042
3043static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003044desktop_shell_set_panel(struct wl_client *client,
3045 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003046 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003047 struct wl_resource *surface_resource)
3048{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003049 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003050 struct weston_surface *surface =
3051 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003053 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003054
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003055 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003056 wl_resource_post_error(surface_resource,
3057 WL_DISPLAY_ERROR_INVALID_OBJECT,
3058 "surface role already assigned");
3059 return;
3060 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003061
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3063 weston_view_destroy(view);
3064 view = weston_view_create(surface);
3065
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003066 surface->committed = panel_committed;
3067 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003068 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03003069 surface->output = weston_output_from_resource(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003070 view->output = surface->output;
David Fort50d962f2016-06-09 17:55:52 +02003071
3072 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003073 if (sh_output->panel_surface) {
3074 /* The output already has a panel, tell our helper
3075 * there is no need for another one. */
3076 weston_desktop_shell_send_configure(resource, 0,
3077 surface_resource,
3078 0, 0);
3079 } else {
3080 weston_desktop_shell_send_configure(resource, 0,
3081 surface_resource,
3082 surface->output->width,
3083 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003084
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003085 sh_output->panel_surface = surface;
3086
3087 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3088 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3089 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003090}
3091
Pekka Paalanen8274d902014-08-06 19:36:51 +03003092static int
3093lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3094{
3095 return snprintf(buf, len, "lock window");
3096}
3097
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003098static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003099lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003100{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003101 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003102 struct weston_view *view;
3103
3104 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003105
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003106 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003107 return;
3108
Jason Ekstranda7af7042013-10-12 22:38:11 -05003109 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003110
3111 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003112 weston_layer_entry_insert(&shell->lock_layer.view_list,
3113 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003114 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003115 surface->is_mapped = true;
3116 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003117 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003118 }
3119}
3120
3121static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003122handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003123{
Tiago Vignattibe143262012-04-16 17:31:41 +03003124 struct desktop_shell *shell =
3125 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003126
Martin Minarik6d118362012-06-07 18:01:59 +02003127 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003128 shell->lock_surface = NULL;
3129}
3130
3131static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003132desktop_shell_set_lock_surface(struct wl_client *client,
3133 struct wl_resource *resource,
3134 struct wl_resource *surface_resource)
3135{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003136 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003137 struct weston_surface *surface =
3138 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003139
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003140 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003141
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003142 if (!shell->locked)
3143 return;
3144
Pekka Paalanen98262232011-12-01 10:42:22 +02003145 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003146
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003147 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003148 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003149 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003150
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003151 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003152 surface->committed = lock_surface_committed;
3153 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003154 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003155}
3156
3157static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003158resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003159{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003160 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003161
Quentin Glidic82681572016-12-17 13:40:51 +01003162 weston_layer_unset_position(&shell->lock_layer);
3163
3164 if (shell->showing_input_panels)
3165 weston_layer_set_position(&shell->input_panel_layer,
3166 WESTON_LAYER_POSITION_TOP_UI);
3167 weston_layer_set_position(&shell->fullscreen_layer,
3168 WESTON_LAYER_POSITION_FULLSCREEN);
3169 weston_layer_set_position(&shell->panel_layer,
3170 WESTON_LAYER_POSITION_UI);
3171 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003172
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003173 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003174
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003175 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003176 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003177 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003178}
3179
3180static void
3181desktop_shell_unlock(struct wl_client *client,
3182 struct wl_resource *resource)
3183{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003184 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003185
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003186 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003187
3188 if (shell->locked)
3189 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003190}
3191
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003192static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003193desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003194 struct wl_resource *resource,
3195 struct wl_resource *surface_resource)
3196{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003197 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003198
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003199 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003200 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003201}
3202
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003203static void
3204desktop_shell_desktop_ready(struct wl_client *client,
3205 struct wl_resource *resource)
3206{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003207 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003208
3209 shell_fade_startup(shell);
3210}
3211
Jonny Lamb765760d2014-08-20 15:53:19 +02003212static void
3213desktop_shell_set_panel_position(struct wl_client *client,
3214 struct wl_resource *resource,
3215 uint32_t position)
3216{
3217 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3218
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003219 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3220 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3221 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3222 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003223 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003224 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003225 "bad position argument");
3226 return;
3227 }
3228
3229 shell->panel_position = position;
3230}
3231
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003232static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003233 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003234 desktop_shell_set_panel,
3235 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003236 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003237 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003238 desktop_shell_desktop_ready,
3239 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003240};
3241
3242static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003243move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003244 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003245{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003246 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003247 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003248 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003249
Derek Foreman8ae2db52015-07-15 13:00:36 -05003250 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003251 return;
3252
Derek Foreman8ae2db52015-07-15 13:00:36 -05003253 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003254
Pekka Paalanen01388e22013-04-25 13:57:44 +03003255 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003256 if (surface == NULL)
3257 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003258
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003259 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003260 if (shsurf == NULL ||
3261 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3262 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003263 return;
3264
Derek Foreman794fa0e2015-07-15 13:00:38 -05003265 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003266}
3267
3268static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003269maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003270 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003271{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003272 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003273 struct weston_surface *surface;
3274 struct shell_surface *shsurf;
3275
3276 surface = weston_surface_get_main_surface(focus);
3277 if (surface == NULL)
3278 return;
3279
3280 shsurf = get_shell_surface(surface);
3281 if (shsurf == NULL)
3282 return;
3283
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003284 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003285}
3286
3287static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003288fullscreen_binding(struct weston_keyboard *keyboard,
3289 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003290{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003291 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003292 struct weston_surface *surface;
3293 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003294 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003295
3296 surface = weston_surface_get_main_surface(focus);
3297 if (surface == NULL)
3298 return;
3299
3300 shsurf = get_shell_surface(surface);
3301 if (shsurf == NULL)
3302 return;
3303
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003304 fullscreen =
3305 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003306
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003307 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003308}
3309
3310static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003311touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003312{
Derek Foreman362656b2014-09-04 10:23:05 -05003313 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003314 struct weston_surface *surface;
3315 struct shell_surface *shsurf;
3316
Derek Foreman8ae2db52015-07-15 13:00:36 -05003317 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003318 return;
3319
Derek Foreman8ae2db52015-07-15 13:00:36 -05003320 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003321 surface = weston_surface_get_main_surface(focus);
3322 if (surface == NULL)
3323 return;
3324
3325 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003326 if (shsurf == NULL ||
3327 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3328 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003329 return;
3330
Derek Foremanb7674ae2015-07-15 13:00:37 -05003331 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003332}
3333
3334static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003335resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003336 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003337{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003338 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003339 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003340 uint32_t edges = 0;
3341 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003342 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003343
Derek Foreman8ae2db52015-07-15 13:00:36 -05003344 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003345 return;
3346
Derek Foreman8ae2db52015-07-15 13:00:36 -05003347 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003348
Pekka Paalanen01388e22013-04-25 13:57:44 +03003349 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003350 if (surface == NULL)
3351 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003352
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003353 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003354 if (shsurf == NULL ||
3355 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3356 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003357 return;
3358
Jason Ekstranda7af7042013-10-12 22:38:11 -05003359 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003360 wl_fixed_to_int(pointer->grab_x),
3361 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003362 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003363
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003364 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003365 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003366 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003367 edges |= 0;
3368 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003369 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003370
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003371 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003372 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003373 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003374 edges |= 0;
3375 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003376 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003377
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003378 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003379}
3380
3381static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003382surface_opacity_binding(struct weston_pointer *pointer,
3383 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003384 struct weston_pointer_axis_event *event,
3385 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003386{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003387 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003388 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003389 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003390 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003391
Pekka Paalanen01388e22013-04-25 13:57:44 +03003392 /* XXX: broken for windows containing sub-surfaces */
3393 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003394 if (surface == NULL)
3395 return;
3396
3397 shsurf = get_shell_surface(surface);
3398 if (!shsurf)
3399 return;
3400
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003401 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003402
Jason Ekstranda7af7042013-10-12 22:38:11 -05003403 if (shsurf->view->alpha > 1.0)
3404 shsurf->view->alpha = 1.0;
3405 if (shsurf->view->alpha < step)
3406 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003407
Jason Ekstranda7af7042013-10-12 22:38:11 -05003408 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003409 weston_surface_damage(surface);
3410}
3411
3412static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003413do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3414 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003415{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003416 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003417 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003418 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003419 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003420
Derek Foreman1281a362015-07-31 16:55:32 -05003421 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003422 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3423 return;
3424 }
3425
Scott Moreauccbf29d2012-02-22 14:21:41 -07003426 wl_list_for_each(output, &compositor->output_list, link) {
3427 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003428 wl_fixed_to_double(pointer->x),
3429 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003430 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003431 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003432 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003433 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003434 increment = -output->zoom.increment;
3435 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003436 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003437 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003438 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003439 else
3440 increment = 0;
3441
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003442 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003443
Scott Moreaue6603982012-06-11 13:07:51 -06003444 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003445 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003446 else if (output->zoom.level > output->zoom.max_level)
3447 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003448
3449 if (!output->zoom.active) {
3450 if (output->zoom.level <= 0.0)
3451 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003452 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003453 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003454
Scott Moreaue6603982012-06-11 13:07:51 -06003455 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003456
Jason Ekstranda7af7042013-10-12 22:38:11 -05003457 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003458 }
3459 }
3460}
3461
Scott Moreauccbf29d2012-02-22 14:21:41 -07003462static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003463zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003464 struct weston_pointer_axis_event *event,
3465 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003466{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003467 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003468}
3469
3470static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003471zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003472 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003473{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003474 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003475}
3476
3477static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003478terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003479 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003480{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003481 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003482
Daniel Stone325fc2d2012-05-30 16:31:58 +01003483 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003484}
3485
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003486static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003487rotate_grab_motion(struct weston_pointer_grab *grab,
3488 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003489 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003490{
3491 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003492 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003493 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003494 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003495 struct weston_surface *surface =
3496 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003497 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003498
Jonas Ådahld2510102014-10-05 21:39:14 +02003499 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003500
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003501 if (!shsurf)
3502 return;
3503
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003504 cx = 0.5f * surface->width;
3505 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003506
Daniel Stone37816df2012-05-16 18:45:18 +01003507 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3508 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003509 r = sqrtf(dx * dx + dy * dy);
3510
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003511 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003512 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003513
3514 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003515 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003516 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003517
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003518 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003519 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003520
3521 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003522 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003523 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003524 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003525 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003526
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003527 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003528 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003529 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003530 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003531 wl_list_init(&shsurf->rotation.transform.link);
3532 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003533 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003534 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003535
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003536 /* We need to adjust the position of the surface
3537 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 cposx = shsurf->view->geometry.x + cx;
3539 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003540 dposx = rotate->center.x - cposx;
3541 dposy = rotate->center.y - cposy;
3542 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003543 weston_view_set_position(shsurf->view,
3544 shsurf->view->geometry.x + dposx,
3545 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003546 }
3547
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003548 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003549 * lazily applies the damage due to rotation update.
3550 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003551 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003552}
3553
3554static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003555rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003556 const struct timespec *time,
3557 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003558{
3559 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003560 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003561 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003562 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003563 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003564
Daniel Stone4dbadb12012-05-30 16:31:51 +01003565 if (pointer->button_count == 0 &&
3566 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003567 if (shsurf)
3568 weston_matrix_multiply(&shsurf->rotation.rotation,
3569 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003570 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003571 free(rotate);
3572 }
3573}
3574
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003575static void
3576rotate_grab_cancel(struct weston_pointer_grab *grab)
3577{
3578 struct rotate_grab *rotate =
3579 container_of(grab, struct rotate_grab, base.grab);
3580
3581 shell_grab_end(&rotate->base);
3582 free(rotate);
3583}
3584
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003585static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003586 noop_grab_focus,
3587 rotate_grab_motion,
3588 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003589 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003590 noop_grab_axis_source,
3591 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003592 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003593};
3594
3595static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003596surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003597{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003598 struct weston_surface *surface =
3599 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003600 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003601 float dx, dy;
3602 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003603
Pekka Paalanen460099f2012-01-20 16:48:25 +02003604 rotate = malloc(sizeof *rotate);
3605 if (!rotate)
3606 return;
3607
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003608 weston_view_to_global_float(shsurf->view,
3609 surface->width * 0.5f,
3610 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003611 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003612
Derek Foreman74de4692015-07-15 13:00:39 -05003613 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3614 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003615 r = sqrtf(dx * dx + dy * dy);
3616 if (r > 20.0f) {
3617 struct weston_matrix inverse;
3618
3619 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003620 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003621 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003622
3623 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003624 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003625 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003626 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003627 weston_matrix_init(&rotate->rotation);
3628 }
3629
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003630 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003631 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632}
3633
3634static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003635rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3636 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003637{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003638 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003639 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003640 struct shell_surface *surface;
3641
Derek Foreman8ae2db52015-07-15 13:00:36 -05003642 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003643 return;
3644
Derek Foreman8ae2db52015-07-15 13:00:36 -05003645 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003646
Pekka Paalanen01388e22013-04-25 13:57:44 +03003647 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003648 if (base_surface == NULL)
3649 return;
3650
3651 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003652 if (surface == NULL ||
3653 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3654 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003655 return;
3656
Derek Foreman74de4692015-07-15 13:00:39 -05003657 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003658}
3659
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003660/* Move all fullscreen layers down to the current workspace and hide their
3661 * black views. The surfaces' state is set to both fullscreen and lowered,
3662 * and this is reversed when such a surface is re-configured, see
3663 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3664 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003665 * lowering_output = NULL - Lower on all outputs, else only lower on the
3666 * specified output.
3667 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003668 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003669 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003670void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003671lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003672 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003673{
3674 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003675 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003676
3677 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003678 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003679 &shell->fullscreen_layer.view_list.link,
3680 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003681 struct shell_surface *shsurf = get_shell_surface(view->surface);
3682
3683 if (!shsurf)
3684 continue;
3685
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003686 /* Only lower surfaces which have lowering_output as their fullscreen
3687 * output, unless a NULL output asks for lowering on all outputs.
3688 */
3689 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3690 continue;
3691
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003692 /* We can have a non-fullscreen popup for a fullscreen surface
3693 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003694 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003695 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003696 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3697 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003698 weston_view_damage_below(shsurf->fullscreen.black_view);
3699
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003700 }
3701
3702 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003703 weston_layer_entry_remove(&view->layer_link);
3704 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003705 weston_view_damage_below(view);
3706 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003707
3708 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003709 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003710}
3711
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003712void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003713activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003714 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003715{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003716 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003717 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003718 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003719 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003720 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003721 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003722
Pekka Paalanen01388e22013-04-25 13:57:44 +03003723 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003724 shsurf = get_shell_surface(main_surface);
3725 assert(shsurf);
3726
3727 /* Only demote fullscreen surfaces on the output of activated shsurf.
3728 * Leave fullscreen surfaces on unrelated outputs alone. */
3729 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003730
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003731 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003732
Jonas Ådahl8538b222012-08-29 22:13:03 +02003733 state = ensure_focus_state(shell, seat);
3734 if (state == NULL)
3735 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003736
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003737 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003738 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003739
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003740 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3741 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003742 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003743
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003744 /* Update the surface’s layer. This brings it to the top of the stacking
3745 * order as appropriate. */
3746 shell_surface_update_layer(shsurf);
3747
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003748 if (shell->focus_animation_type != ANIMATION_NONE) {
3749 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003750 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003751 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003752}
3753
Alex Wu21858432012-04-01 20:13:08 +08003754/* no-op func for checking black surface */
3755static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003756black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003757{
3758}
3759
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003760static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003761is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003762{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003763 struct weston_surface *surface = view->surface;
3764
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003765 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003766 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003767 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003768 return true;
3769 }
3770 return false;
3771}
3772
Kristian Høgsberg75840622011-09-06 13:48:16 -04003773static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003774activate_binding(struct weston_seat *seat,
3775 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003776 struct weston_view *focus_view,
3777 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003778{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003779 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003780 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003781
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003782 if (!focus_view)
3783 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003784
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003785 if (is_black_surface_view(focus_view, &main_view))
3786 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003787
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003788 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003789 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003790 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003791
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003792 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003793}
3794
3795static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003796click_to_activate_binding(struct weston_pointer *pointer,
3797 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003798 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003799{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003800 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003801 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003802 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003803 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003804
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003805 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003806 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003807 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003808}
3809
3810static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003811touch_to_activate_binding(struct weston_touch *touch,
3812 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003813 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003814{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003815 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003816 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003817 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003818 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003819
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003820 activate_binding(touch->seat, data, touch->focus,
3821 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003822}
3823
3824static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003825unfocus_all_seats(struct desktop_shell *shell)
3826{
3827 struct weston_seat *seat, *next;
3828
3829 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003830 struct weston_keyboard *keyboard =
3831 weston_seat_get_keyboard(seat);
3832
3833 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003834 continue;
3835
Derek Foreman1281a362015-07-31 16:55:32 -05003836 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003837 }
3838}
3839
3840static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003841lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003842{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003843 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003844
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003845 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003846 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003847 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003848 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003849
3850 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003851
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003852 /* Hide all surfaces by removing the fullscreen, panel and
3853 * toplevel layers. This way nothing else can show or receive
3854 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003855
Quentin Glidic82681572016-12-17 13:40:51 +01003856 weston_layer_unset_position(&shell->panel_layer);
3857 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003858 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003859 weston_layer_unset_position(&shell->input_panel_layer);
3860 weston_layer_unset_position(&ws->layer);
3861
3862 weston_layer_set_position(&shell->lock_layer,
3863 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003864
Derek Foreman004b4a12015-07-20 16:28:13 -05003865 weston_compositor_sleep(shell->compositor);
3866
Neil Robertsb4a91702014-04-09 16:33:32 +01003867 /* Remove the keyboard focus on all seats. This will be
3868 * restored to the workspace's saved state via
3869 * restore_focus_state when the compositor is unlocked */
3870 unfocus_all_seats(shell);
3871
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003872 /* TODO: disable bindings that should not work while locked. */
3873
3874 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003875}
3876
3877static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003878unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003879{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003880 struct wl_resource *shell_resource;
3881
Pekka Paalanend81c2162011-11-16 13:47:34 +02003882 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003883 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003884 return;
3885 }
3886
3887 /* If desktop-shell client has gone away, unlock immediately. */
3888 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003889 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003890 return;
3891 }
3892
3893 if (shell->prepare_event_sent)
3894 return;
3895
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003896 shell_resource = shell->child.desktop_shell;
3897 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003898 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003899}
3900
3901static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003902shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003903{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003904 struct shell_output *shell_output = data;
3905 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003906
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003907 shell_output->fade.animation = NULL;
3908 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003909 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003910 weston_surface_destroy(shell_output->fade.view->surface);
3911 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003912 break;
3913 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003914 lock(shell);
3915 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003916 default:
3917 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003918 }
3919}
3920
Jason Ekstranda7af7042013-10-12 22:38:11 -05003921static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003922shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003923{
3924 struct weston_compositor *compositor = shell->compositor;
3925 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003926 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003927
3928 surface = weston_surface_create(compositor);
3929 if (!surface)
3930 return NULL;
3931
Jason Ekstranda7af7042013-10-12 22:38:11 -05003932 view = weston_view_create(surface);
3933 if (!view) {
3934 weston_surface_destroy(surface);
3935 return NULL;
3936 }
3937
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003938 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3939 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003940 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003941 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3942 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003943 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003944 surface->is_mapped = true;
3945 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003946
Jason Ekstranda7af7042013-10-12 22:38:11 -05003947 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003948}
3949
3950static void
3951shell_fade(struct desktop_shell *shell, enum fade_type type)
3952{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003953 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003954 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003955
3956 switch (type) {
3957 case FADE_IN:
3958 tint = 0.0;
3959 break;
3960 case FADE_OUT:
3961 tint = 1.0;
3962 break;
3963 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003964 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003965 return;
3966 }
3967
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003968 /* Create a separate fade surface for each output */
3969 wl_list_for_each(shell_output, &shell->output_list, link) {
3970 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003971
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003972 if (shell_output->fade.view == NULL) {
3973 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3974 if (!shell_output->fade.view)
3975 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003976
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003977 shell_output->fade.view->alpha = 1.0 - tint;
3978 weston_view_update_transform(shell_output->fade.view);
3979 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003980
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003981 if (shell_output->fade.view->output == NULL) {
3982 /* If the black view gets a NULL output, we lost the
3983 * last output and we'll just cancel the fade. This
3984 * happens when you close the last window under the
3985 * X11 or Wayland backends. */
3986 shell->locked = false;
3987 weston_surface_destroy(shell_output->fade.view->surface);
3988 shell_output->fade.view = NULL;
3989 } else if (shell_output->fade.animation) {
3990 weston_fade_update(shell_output->fade.animation, tint);
3991 } else {
3992 shell_output->fade.animation =
3993 weston_fade_run(shell_output->fade.view,
3994 1.0 - tint, tint, 300.0,
3995 shell_fade_done_for_output, shell_output);
3996 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003997 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003998}
3999
4000static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004001do_shell_fade_startup(void *data)
4002{
4003 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004004 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004005
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004006 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004007 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004008 } else {
4009 weston_log("desktop shell: "
4010 "unexpected fade-in animation type %d\n",
4011 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004012 wl_list_for_each(shell_output, &shell->output_list, link) {
4013 weston_surface_destroy(shell_output->fade.view->surface);
4014 shell_output->fade.view = NULL;
4015 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004016 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004017}
4018
4019static void
4020shell_fade_startup(struct desktop_shell *shell)
4021{
4022 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004023 struct shell_output *shell_output;
4024 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004025
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004026 wl_list_for_each(shell_output, &shell->output_list, link) {
4027 if (!shell_output->fade.startup_timer)
4028 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004029
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004030 wl_event_source_remove(shell_output->fade.startup_timer);
4031 shell_output->fade.startup_timer = NULL;
4032 has_fade = true;
4033 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004034
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004035 if (has_fade) {
4036 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4037 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4038 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004039}
4040
4041static int
4042fade_startup_timeout(void *data)
4043{
4044 struct desktop_shell *shell = data;
4045
4046 shell_fade_startup(shell);
4047 return 0;
4048}
4049
4050static void
4051shell_fade_init(struct desktop_shell *shell)
4052{
4053 /* Make compositor output all black, and wait for the desktop-shell
4054 * client to signal it is ready, then fade in. The timer triggers a
4055 * fade-in, in case the desktop-shell client takes too long.
4056 */
4057
4058 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004059 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004060
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004061 if (shell->startup_animation_type == ANIMATION_NONE)
4062 return;
4063
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004064 wl_list_for_each(shell_output, &shell->output_list, link) {
4065 if (shell_output->fade.view != NULL) {
4066 weston_log("%s: warning: fade surface already exists\n",
4067 __func__);
4068 continue;
4069 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004070
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004071 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4072 if (!shell_output->fade.view)
4073 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004074
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004075 weston_view_update_transform(shell_output->fade.view);
4076 weston_surface_damage(shell_output->fade.view->surface);
4077
4078 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4079 shell_output->fade.startup_timer =
4080 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4081 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4082 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004083}
4084
4085static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004086idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004087{
4088 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004089 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004090
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004091 struct weston_seat *seat;
4092
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004093 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4094 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004095
4096 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004097 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004098}
4099
4100static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004101wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004102{
4103 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004104 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004105
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004106 unlock(shell);
4107}
4108
4109static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004110transform_handler(struct wl_listener *listener, void *data)
4111{
4112 struct weston_surface *surface = data;
4113 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004114 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004115 int x, y;
4116
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004117 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004118 return;
4119
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004120 api = shsurf->shell->xwayland_surface_api;
4121 if (!api) {
4122 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4123 shsurf->shell->xwayland_surface_api = api;
4124 }
4125
4126 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004127 return;
4128
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004129 if (!weston_view_is_mapped(shsurf->view))
4130 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004131
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004132 x = shsurf->view->geometry.x;
4133 y = shsurf->view->geometry.y;
4134
4135 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004136}
4137
4138static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004139center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004140{
Giulio Camuffob8366642013-04-25 13:57:46 +03004141 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004142 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004143
Jason Ekstranda7af7042013-10-12 22:38:11 -05004144 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004145
4146 x = output->x + (output->width - width) / 2 - surf_x / 2;
4147 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004148
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004149 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004150}
4151
4152static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004153weston_view_set_initial_position(struct weston_view *view,
4154 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004155{
4156 struct weston_compositor *compositor = shell->compositor;
4157 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004158 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004159 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004160 struct weston_output *output, *target_output = NULL;
4161 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004162 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004163
4164 /* As a heuristic place the new window on the same output as the
4165 * pointer. Falling back to the output containing 0, 0.
4166 *
4167 * TODO: Do something clever for touch too?
4168 */
4169 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004170 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4171
4172 if (pointer) {
4173 ix = wl_fixed_to_int(pointer->x);
4174 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004175 break;
4176 }
4177 }
4178
4179 wl_list_for_each(output, &compositor->output_list, link) {
4180 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4181 target_output = output;
4182 break;
4183 }
4184 }
4185
4186 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004187 weston_view_set_position(view, 10 + random() % 400,
4188 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004189 return;
4190 }
4191
4192 /* Valid range within output where the surface will still be onscreen.
4193 * If this is negative it means that the surface is bigger than
4194 * output.
4195 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004196 get_output_work_area(shell, target_output, &area);
4197
Derek Foremanf814c5d2015-04-15 12:23:54 -05004198 x = area.x;
4199 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004200 range_x = area.width - view->surface->width;
4201 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004202
Rob Bradford4cb88c72012-08-13 15:18:44 +01004203 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004204 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004205
4206 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004207 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004208
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004210}
4211
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004212static bool
4213check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4214{
4215 struct timespec now;
4216
4217 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4218 return false;
4219
4220 /*
4221 * If the shell helper client dies before the session has been
4222 * up for roughly 30 seconds, better just make Weston shut down,
4223 * because the user likely has no way to interact with the desktop
4224 * anyway.
4225 */
4226 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4227 weston_log("Error: %s apparently cannot run at all.\n",
4228 shell->client);
4229 weston_log_continue(STAMP_SPACE "Quitting...");
4230 wl_display_terminate(shell->compositor->wl_display);
4231
4232 return true;
4233 }
4234
4235 return false;
4236}
4237
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004238static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004239
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004240static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004241respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004242{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004243 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004244
4245 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004246 weston_compositor_get_time(&time);
4247 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004248 shell->child.deathstamp = time;
4249 shell->child.deathcount = 0;
4250 }
4251
4252 shell->child.deathcount++;
4253 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004254 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004255 return;
4256 }
4257
Pekka Paalanen826dc142014-08-27 12:11:53 +03004258 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004259 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004260}
4261
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004262static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004263desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4264{
4265 struct desktop_shell *shell;
4266
4267 shell = container_of(listener, struct desktop_shell,
4268 child.client_destroy_listener);
4269
Pekka Paalanen826dc142014-08-27 12:11:53 +03004270 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004271 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004272 /*
4273 * unbind_desktop_shell() will reset shell->child.desktop_shell
4274 * before the respawned process has a chance to create a new
4275 * desktop_shell object, because we are being called from the
4276 * wl_client destructor which destroys all wl_resources before
4277 * returning.
4278 */
4279
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004280 if (!check_desktop_shell_crash_too_early(shell))
4281 respawn_desktop_shell_process(shell);
4282
Pekka Paalanen826dc142014-08-27 12:11:53 +03004283 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004284}
4285
4286static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004287launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004288{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004289 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004290
Pekka Paalanen826dc142014-08-27 12:11:53 +03004291 shell->child.client = weston_client_start(shell->compositor,
4292 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004293
Arnaud Vrac42631192014-08-25 20:56:46 +02004294 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004295 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004296 return;
4297 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004298
4299 shell->child.client_destroy_listener.notify =
4300 desktop_shell_client_destroy;
4301 wl_client_add_destroy_listener(shell->child.client,
4302 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004303}
4304
4305static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004306unbind_desktop_shell(struct wl_resource *resource)
4307{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004308 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004309
4310 if (shell->locked)
4311 resume_desktop(shell);
4312
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004313 shell->child.desktop_shell = NULL;
4314 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004315}
4316
4317static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004318bind_desktop_shell(struct wl_client *client,
4319 void *data, uint32_t version, uint32_t id)
4320{
Tiago Vignattibe143262012-04-16 17:31:41 +03004321 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004322 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004323
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004324 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4325 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004326
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004327 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004328 wl_resource_set_implementation(resource,
4329 &desktop_shell_implementation,
4330 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004331 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004332 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004333 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004334
4335 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4336 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004337}
4338
Kristian Høgsberg07045392012-02-19 18:52:44 -05004339struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004340 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004341 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004342 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004343 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004344 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004345};
4346
4347static void
4348switcher_next(struct switcher *switcher)
4349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004351 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004352 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004353 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004354
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004355 /* temporary re-display minimized surfaces */
4356 struct weston_view *tmp;
4357 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004358 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4359 weston_layer_entry_remove(&view->layer_link);
4360 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004361 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4362 *minimized = view;
4363 }
4364
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004365 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004366 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004367 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004368 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004369 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004370 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004371 next = view;
4372 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004373 view->alpha = 0.25;
4374 weston_view_geometry_dirty(view);
4375 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004376 }
Alex Wu1659daa2012-04-01 20:13:09 +08004377
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004378 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004379 view->alpha = 0.25;
4380 weston_view_geometry_dirty(view);
4381 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004382 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383 }
4384
4385 if (next == NULL)
4386 next = first;
4387
Alex Wu07b26062012-03-12 16:06:01 +08004388 if (next == NULL)
4389 return;
4390
Kristian Høgsberg07045392012-02-19 18:52:44 -05004391 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004392 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004393
4394 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004395 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004397
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004398 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004399 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004401}
4402
4403static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004404switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004405{
4406 struct switcher *switcher =
4407 container_of(listener, struct switcher, listener);
4408
4409 switcher_next(switcher);
4410}
4411
4412static void
Daniel Stone351eb612012-05-31 15:27:47 -04004413switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004414{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004416 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004417 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004418
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004419 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004420 if (is_focus_view(view))
4421 continue;
4422
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 view->alpha = 1.0;
4424 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004425 }
4426
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004427 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004428 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004429 keyboard->seat,
4430 WESTON_ACTIVATE_FLAG_CONFIGURE);
4431 }
4432
Kristian Høgsberg07045392012-02-19 18:52:44 -05004433 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004434 weston_keyboard_end_grab(keyboard);
4435 if (keyboard->input_method_resource)
4436 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004437
4438 /* re-hide surfaces that were temporary shown during the switch */
4439 struct weston_view **minimized;
4440 wl_array_for_each(minimized, &switcher->minimized_array) {
4441 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004442 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004443 weston_layer_entry_remove(&(*minimized)->layer_link);
4444 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004445 weston_view_damage_below(*minimized);
4446 }
4447 }
4448 wl_array_release(&switcher->minimized_array);
4449
Kristian Høgsberg07045392012-02-19 18:52:44 -05004450 free(switcher);
4451}
4452
4453static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004454switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004455 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004456{
4457 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004458 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004459
Daniel Stonec9785ea2012-05-30 16:31:52 +01004460 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004461 switcher_next(switcher);
4462}
4463
4464static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004465switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004466 uint32_t mods_depressed, uint32_t mods_latched,
4467 uint32_t mods_locked, uint32_t group)
4468{
4469 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004470 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004471
Daniel Stone351eb612012-05-31 15:27:47 -04004472 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4473 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004474}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004475
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004476static void
4477switcher_cancel(struct weston_keyboard_grab *grab)
4478{
4479 struct switcher *switcher = container_of(grab, struct switcher, grab);
4480
4481 switcher_destroy(switcher);
4482}
4483
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004484static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004485 switcher_key,
4486 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004487 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488};
4489
4490static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004491switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004492 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004493{
Tiago Vignattibe143262012-04-16 17:31:41 +03004494 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004495 struct switcher *switcher;
4496
4497 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004498 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004499 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004500 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004501 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004502 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004503
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004504 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004505 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004506 weston_keyboard_start_grab(keyboard, &switcher->grab);
4507 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004508 switcher_next(switcher);
4509}
4510
Pekka Paalanen3c647232011-12-22 13:43:43 +02004511static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004512backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004513 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004514{
4515 struct weston_compositor *compositor = data;
4516 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004517 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004518
4519 /* TODO: we're limiting to simple use cases, where we assume just
4520 * control on the primary display. We'd have to extend later if we
4521 * ever get support for setting backlights on random desktop LCD
4522 * panels though */
4523 output = get_default_output(compositor);
4524 if (!output)
4525 return;
4526
4527 if (!output->set_backlight)
4528 return;
4529
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004530 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4531 backlight_new = output->backlight_current - 25;
4532 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4533 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004534
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004535 if (backlight_new < 5)
4536 backlight_new = 5;
4537 if (backlight_new > 255)
4538 backlight_new = 255;
4539
4540 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004541 output->set_backlight(output, output->backlight_current);
4542}
4543
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004544static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004545force_kill_binding(struct weston_keyboard *keyboard,
4546 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004547{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004548 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004549 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004550 struct desktop_shell *shell = data;
4551 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004552 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004553
Derek Foreman8ae2db52015-07-15 13:00:36 -05004554 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004555 if (!focus_surface)
4556 return;
4557
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004558 wl_signal_emit(&compositor->kill_signal, focus_surface);
4559
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004560 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004561 wl_client_get_credentials(client, &pid, NULL, NULL);
4562
4563 /* Skip clients that we launched ourselves (the credentials of
4564 * the socketpair is ours) */
4565 if (pid == getpid())
4566 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004567
Daniel Stone325fc2d2012-05-30 16:31:58 +01004568 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004569}
4570
4571static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004572workspace_up_binding(struct weston_keyboard *keyboard,
4573 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004574{
4575 struct desktop_shell *shell = data;
4576 unsigned int new_index = shell->workspaces.current;
4577
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004578 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004579 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004580 if (new_index != 0)
4581 new_index--;
4582
4583 change_workspace(shell, new_index);
4584}
4585
4586static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004587workspace_down_binding(struct weston_keyboard *keyboard,
4588 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004589{
4590 struct desktop_shell *shell = data;
4591 unsigned int new_index = shell->workspaces.current;
4592
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004593 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004594 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004595 if (new_index < shell->workspaces.num - 1)
4596 new_index++;
4597
4598 change_workspace(shell, new_index);
4599}
4600
4601static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004602workspace_f_binding(struct weston_keyboard *keyboard,
4603 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004604{
4605 struct desktop_shell *shell = data;
4606 unsigned int new_index;
4607
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004608 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004609 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004610 new_index = key - KEY_F1;
4611 if (new_index >= shell->workspaces.num)
4612 new_index = shell->workspaces.num - 1;
4613
4614 change_workspace(shell, new_index);
4615}
4616
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004617static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004618workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004619 const struct timespec *time, uint32_t key,
4620 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004621{
4622 struct desktop_shell *shell = data;
4623 unsigned int new_index = shell->workspaces.current;
4624
4625 if (shell->locked)
4626 return;
4627
4628 if (new_index != 0)
4629 new_index--;
4630
Derek Foreman8ae2db52015-07-15 13:00:36 -05004631 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004632}
4633
4634static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004635workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004636 const struct timespec *time, uint32_t key,
4637 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004638{
4639 struct desktop_shell *shell = data;
4640 unsigned int new_index = shell->workspaces.current;
4641
4642 if (shell->locked)
4643 return;
4644
4645 if (new_index < shell->workspaces.num - 1)
4646 new_index++;
4647
Derek Foreman8ae2db52015-07-15 13:00:36 -05004648 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004649}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004650
4651static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004652shell_reposition_view_on_output_destroy(struct weston_view *view)
4653{
4654 struct weston_output *output, *first_output;
4655 struct weston_compositor *ec = view->surface->compositor;
4656 struct shell_surface *shsurf;
4657 float x, y;
4658 int visible;
4659
4660 x = view->geometry.x;
4661 y = view->geometry.y;
4662
4663 /* At this point the destroyed output is not in the list anymore.
4664 * If the view is still visible somewhere, we leave where it is,
4665 * otherwise, move it to the first output. */
4666 visible = 0;
4667 wl_list_for_each(output, &ec->output_list, link) {
4668 if (pixman_region32_contains_point(&output->region,
4669 x, y, NULL)) {
4670 visible = 1;
4671 break;
4672 }
4673 }
4674
4675 if (!visible) {
4676 first_output = container_of(ec->output_list.next,
4677 struct weston_output, link);
4678
4679 x = first_output->x + first_output->width / 4;
4680 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004681
4682 weston_view_set_position(view, x, y);
4683 } else {
4684 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004685 }
4686
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004687
4688 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004689 if (!shsurf)
4690 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004691
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004692 shsurf->saved_position_valid = false;
4693 set_maximized(shsurf, false);
4694 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004695}
4696
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004697void
4698shell_for_each_layer(struct desktop_shell *shell,
4699 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004700{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004701 struct workspace **ws;
4702
4703 func(shell, &shell->fullscreen_layer, data);
4704 func(shell, &shell->panel_layer, data);
4705 func(shell, &shell->background_layer, data);
4706 func(shell, &shell->lock_layer, data);
4707 func(shell, &shell->input_panel_layer, data);
4708
4709 wl_array_for_each(ws, &shell->workspaces.array)
4710 func(shell, &(*ws)->layer, data);
4711}
4712
4713static void
4714shell_output_destroy_move_layer(struct desktop_shell *shell,
4715 struct weston_layer *layer,
4716 void *data)
4717{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004718 struct weston_view *view;
4719
Marius Vladea40d6d2018-02-08 18:46:42 +02004720 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004721 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004722}
4723
4724static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004725handle_output_destroy(struct wl_listener *listener, void *data)
4726{
4727 struct shell_output *output_listener =
4728 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004729 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004730
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004731 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004732
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004733 if (output_listener->panel_surface)
4734 wl_list_remove(&output_listener->panel_surface_listener.link);
4735 if (output_listener->background_surface)
4736 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004737 wl_list_remove(&output_listener->destroy_listener.link);
4738 wl_list_remove(&output_listener->link);
4739 free(output_listener);
4740}
4741
4742static void
David Fort50d962f2016-06-09 17:55:52 +02004743shell_resize_surface_to_output(struct desktop_shell *shell,
4744 struct weston_surface *surface,
4745 const struct weston_output *output)
4746{
4747 if (!surface)
4748 return;
4749
4750 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4751 surface->resource,
4752 output->width,
4753 output->height);
4754}
4755
4756
4757static void
4758handle_output_resized(struct wl_listener *listener, void *data)
4759{
4760 struct desktop_shell *shell =
4761 container_of(listener, struct desktop_shell, resized_listener);
4762 struct weston_output *output = (struct weston_output *)data;
4763 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4764
4765 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4766 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4767}
4768
4769static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004770create_shell_output(struct desktop_shell *shell,
4771 struct weston_output *output)
4772{
4773 struct shell_output *shell_output;
4774
4775 shell_output = zalloc(sizeof *shell_output);
4776 if (shell_output == NULL)
4777 return;
4778
4779 shell_output->output = output;
4780 shell_output->shell = shell;
4781 shell_output->destroy_listener.notify = handle_output_destroy;
4782 wl_signal_add(&output->destroy_signal,
4783 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004784 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004785}
4786
4787static void
4788handle_output_create(struct wl_listener *listener, void *data)
4789{
4790 struct desktop_shell *shell =
4791 container_of(listener, struct desktop_shell, output_create_listener);
4792 struct weston_output *output = (struct weston_output *)data;
4793
4794 create_shell_output(shell, output);
4795}
4796
4797static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004798handle_output_move_layer(struct desktop_shell *shell,
4799 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004800{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004801 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004802 struct weston_view *view;
4803 float x, y;
4804
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004805 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004806 if (view->output != output)
4807 continue;
4808
4809 x = view->geometry.x + output->move_x;
4810 y = view->geometry.y + output->move_y;
4811 weston_view_set_position(view, x, y);
4812 }
4813}
4814
4815static void
4816handle_output_move(struct wl_listener *listener, void *data)
4817{
4818 struct desktop_shell *shell;
4819
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004820 shell = container_of(listener, struct desktop_shell,
4821 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004822
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004823 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004824}
4825
4826static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004827setup_output_destroy_handler(struct weston_compositor *ec,
4828 struct desktop_shell *shell)
4829{
4830 struct weston_output *output;
4831
4832 wl_list_init(&shell->output_list);
4833 wl_list_for_each(output, &ec->output_list, link)
4834 create_shell_output(shell, output);
4835
4836 shell->output_create_listener.notify = handle_output_create;
4837 wl_signal_add(&ec->output_created_signal,
4838 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004839
4840 shell->output_move_listener.notify = handle_output_move;
4841 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004842}
4843
4844static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004845shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004846{
Tiago Vignattibe143262012-04-16 17:31:41 +03004847 struct desktop_shell *shell =
4848 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004849 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004850 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004851
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004852 /* Force state to unlocked so we don't try to fade */
4853 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004854
4855 if (shell->child.client) {
4856 /* disable respawn */
4857 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004858 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004859 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004860
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004861 wl_list_remove(&shell->idle_listener.link);
4862 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004863 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004864
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004865 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004866 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004867
Xiong Zhang6b481422013-10-23 13:58:32 +08004868 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4869 wl_list_remove(&shell_output->destroy_listener.link);
4870 wl_list_remove(&shell_output->link);
4871 free(shell_output);
4872 }
4873
4874 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004875 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004876 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004877
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004878 wl_array_for_each(ws, &shell->workspaces.array)
4879 workspace_destroy(*ws);
4880 wl_array_release(&shell->workspaces.array);
4881
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004882 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004883 free(shell);
4884}
4885
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004886static void
4887shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4888{
4889 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004890 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004891
Bob Ham744e6532016-01-12 10:21:48 +00004892 if (shell->allow_zap)
4893 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4894 MODIFIER_CTRL | MODIFIER_ALT,
4895 terminate_binding, ec);
4896
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004897 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004898 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4899 click_to_activate_binding,
4900 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004901 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4902 click_to_activate_binding,
4903 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004904 weston_compositor_add_touch_binding(ec, 0,
4905 touch_to_activate_binding,
4906 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004907 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4908 backlight_binding, ec);
4909 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4910 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004911
4912 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004913 if (shell->exposay_modifier)
4914 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4915 exposay_binding, shell);
4916
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004917 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004918 if (!mod)
4919 return;
4920
Ian Ray13404c42017-09-18 15:22:01 +03004921 /* This binding is not configurable, but is only enabled if there is a
4922 * valid binding modifier. */
4923 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4924 MODIFIER_SUPER | MODIFIER_ALT,
4925 surface_opacity_binding, NULL);
4926
Ian Rayab7c0b62017-09-18 15:22:00 +03004927 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4928 mod, zoom_axis_binding,
4929 NULL);
4930
Daniel Stone325fc2d2012-05-30 16:31:58 +01004931 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4932 zoom_key_binding, NULL);
4933 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4934 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004935 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4936 maximize_binding, NULL);
4937 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4938 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004939 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4940 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004941 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004942 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004943 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004944 weston_compositor_add_button_binding(ec, BTN_LEFT,
4945 mod | MODIFIER_SHIFT,
4946 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004947
4948 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004949 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004950 rotate_binding, NULL);
4951
Daniel Stone325fc2d2012-05-30 16:31:58 +01004952 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4953 shell);
4954 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4955 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004956 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4957 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004958 weston_compositor_add_key_binding(ec, KEY_K, mod,
4959 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004960 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4961 workspace_up_binding, shell);
4962 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4963 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004964 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4965 workspace_move_surface_up_binding,
4966 shell);
4967 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4968 workspace_move_surface_down_binding,
4969 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004970
4971 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4972 if (shell->workspaces.num > 1) {
4973 num_workspace_bindings = shell->workspaces.num;
4974 if (num_workspace_bindings > 6)
4975 num_workspace_bindings = 6;
4976 for (i = 0; i < num_workspace_bindings; i++)
4977 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4978 workspace_f_binding,
4979 shell);
4980 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004981
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004982 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004983}
4984
Jason Ekstrand024177c2014-04-21 19:42:58 -05004985static void
4986handle_seat_created(struct wl_listener *listener, void *data)
4987{
4988 struct weston_seat *seat = data;
4989
4990 create_shell_seat(seat);
4991}
4992
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004993WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01004994wet_shell_init(struct weston_compositor *ec,
4995 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004996{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004997 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004998 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004999 struct workspace **pws;
5000 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005001 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005002
Peter Huttererf3d62272013-08-08 11:57:05 +10005003 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005004 if (shell == NULL)
5005 return -1;
5006
Kristian Høgsberg75840622011-09-06 13:48:16 -04005007 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005008
5009 shell->destroy_listener.notify = shell_destroy;
5010 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005011 shell->idle_listener.notify = idle_handler;
5012 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5013 shell->wake_listener.notify = wake_handler;
5014 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005015 shell->transform_listener.notify = transform_handler;
5016 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005017
Quentin Glidic82681572016-12-17 13:40:51 +01005018 weston_layer_init(&shell->fullscreen_layer, ec);
5019 weston_layer_init(&shell->panel_layer, ec);
5020 weston_layer_init(&shell->background_layer, ec);
5021 weston_layer_init(&shell->lock_layer, ec);
5022 weston_layer_init(&shell->input_panel_layer, ec);
5023
5024 weston_layer_set_position(&shell->fullscreen_layer,
5025 WESTON_LAYER_POSITION_FULLSCREEN);
5026 weston_layer_set_position(&shell->panel_layer,
5027 WESTON_LAYER_POSITION_UI);
5028 weston_layer_set_position(&shell->background_layer,
5029 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005030
5031 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005032 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005033
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005034 if (input_panel_setup(shell) < 0)
5035 return -1;
5036
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005037 shell->text_backend = text_backend_init(ec);
5038 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005039 return -1;
5040
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005041 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005042
Daniel Stonedf8133b2013-11-19 11:37:14 +01005043 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5044 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5045
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005046 for (i = 0; i < shell->workspaces.num; i++) {
5047 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5048 if (pws == NULL)
5049 return -1;
5050
Quentin Glidic82681572016-12-17 13:40:51 +01005051 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005052 if (*pws == NULL)
5053 return -1;
5054 }
5055 activate_workspace(shell, 0);
5056
Quentin Glidic82681572016-12-17 13:40:51 +01005057 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005058
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005059 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005060 wl_list_init(&shell->workspaces.animation.link);
5061 shell->workspaces.animation.frame = animate_workspace_change_frame;
5062
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005063 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5064 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005065 return -1;
5066
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005067 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005068 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005069 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005070 return -1;
5071
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005072 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005073
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005074 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005075
Xiong Zhang6b481422013-10-23 13:58:32 +08005076 setup_output_destroy_handler(ec, shell);
5077
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005078 loop = wl_display_get_event_loop(ec->wl_display);
5079 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005080
Jason Ekstrand024177c2014-04-21 19:42:58 -05005081 wl_list_for_each(seat, &ec->seat_list, link)
5082 handle_seat_created(NULL, seat);
5083 shell->seat_create_listener.notify = handle_seat_created;
5084 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005085
David Fort50d962f2016-06-09 17:55:52 +02005086 shell->resized_listener.notify = handle_output_resized;
5087 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5088
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005089 screenshooter_create(ec);
5090
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005091 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005092
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005093 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005094
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005095 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5096
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005097 return 0;
5098}