blob: 35dbbaa2bd080898f0599133bae3e581f1acd70a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020044#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020047#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010049#ifndef static_assert
50#define static_assert(cond, msg)
51#endif
52
Jonas Ådahl04769742012-06-13 00:01:24 +020053struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020054 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040056 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020057 struct weston_surface *keyboard_focus;
58 struct wl_list link;
59 struct wl_listener seat_destroy_listener;
60 struct wl_listener surface_destroy_listener;
61};
62
Philip Withnall648a4dd2013-11-25 18:01:44 +000063/*
64 * Surface stacking and ordering.
65 *
66 * This is handled using several linked lists of surfaces, organised into
67 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
68 * above all of the surfaces in the layer below. The set of layers is static and
69 * in the following order (top-most first):
70 * • Lock layer (only ever displayed on its own)
71 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010072 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Fullscreen layer
74 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000075 * • Workspace layers
76 * • Background layer
77 *
78 * The list of layers may be manipulated to remove whole layers of surfaces from
79 * display. For example, when locking the screen, all layers except the lock
80 * layer are removed.
81 *
82 * A surface’s layer is modified on configuring the surface, in
83 * set_surface_type() (which is only called when the surface’s type change is
84 * _committed_). If a surface’s type changes (e.g. when making a window
85 * fullscreen) its layer changes too.
86 *
87 * In order to allow popup and transient surfaces to be correctly stacked above
88 * their parent surfaces, each surface tracks both its parent surface, and a
89 * linked list of its children. When a surface’s layer is updated, so are the
90 * layers of its children. Note that child surfaces are *not* the same as
91 * subsurfaces — child/parent surfaces are purely for maintaining stacking
92 * order.
93 *
94 * The children_link list of siblings of a surface (i.e. those surfaces which
95 * have the same parent) only contains weston_surfaces which have a
96 * shell_surface. Stacking is not implemented for non-shell_surface
97 * weston_surfaces. This means that the following implication does *not* hold:
98 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
99 */
100
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200101struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500102 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200103
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200104 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600106 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700107
Tiago Vignattibe143262012-04-16 17:31:41 +0300108 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800111 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800112 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700113 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800114 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800122 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800124 } fullscreen;
125
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 struct weston_transform workspace_transform;
127
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500128 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200130
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700131 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200132 bool fullscreen;
133 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100134 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200135 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700136
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500137 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500138
139 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200140};
141
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300142struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400143 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300144 struct shell_surface *shsurf;
145 struct wl_listener shsurf_destroy_listener;
146};
147
Rusty Lynch1084da52013-08-15 09:10:08 -0700148struct shell_touch_grab {
149 struct weston_touch_grab grab;
150 struct shell_surface *shsurf;
151 struct wl_listener shsurf_destroy_listener;
152 struct weston_touch *touch;
153};
154
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300155struct weston_move_grab {
156 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100157 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500158 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159};
160
Rusty Lynch1084da52013-08-15 09:10:08 -0700161struct weston_touch_move_grab {
162 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800163 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700164 wl_fixed_t dx, dy;
165};
166
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300168 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500169 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200170 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200171 float x;
172 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200173 } center;
174};
175
Giulio Camuffo5085a752013-03-25 21:42:45 +0100176struct shell_seat {
177 struct weston_seat *seat;
178 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500179 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100180
Jason Ekstrand024177c2014-04-21 19:42:58 -0500181 struct wl_listener caps_changed_listener;
182 struct wl_listener pointer_focus_listener;
183 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184};
185
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800186
Alex Wubd3354b2012-04-17 17:20:49 +0800187static struct desktop_shell *
188shell_surface_get_shell(struct shell_surface *shsurf);
189
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500190static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200191set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
192
193static void
Derek Foreman74de4692015-07-15 13:00:39 -0500194surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500195
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300196static void
197shell_fade_startup(struct desktop_shell *shell);
198
Philip Withnallbecb77e2013-11-25 18:01:30 +0000199static struct shell_seat *
200get_shell_seat(struct weston_seat *seat);
201
Jonny Lambd73c6942014-08-20 15:53:20 +0200202static void
203get_output_panel_size(struct desktop_shell *shell,
204 struct weston_output *output,
205 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700206
Philip Withnall648a4dd2013-11-25 18:01:44 +0000207static void
208shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
209
Pekka Paalanen8274d902014-08-06 19:36:51 +0300210static int
211shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
212{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300213 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200214 struct weston_desktop_surface *desktop_surface =
215 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300216
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200217 t = weston_desktop_surface_get_title(desktop_surface);
218 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219
220 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200221 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300222 t ? " '" : "", t ?: "", t ? "'" : "",
223 c ? " of " : "", c ?: "");
224}
225
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500226static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400227destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228{
229 struct shell_grab *grab;
230
231 grab = container_of(listener, struct shell_grab,
232 shsurf_destroy_listener);
233
234 grab->shsurf = NULL;
235}
236
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800237struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500238get_default_view(struct weston_surface *surface)
239{
240 struct shell_surface *shsurf;
241 struct weston_view *view;
242
243 if (!surface || wl_list_empty(&surface->views))
244 return NULL;
245
246 shsurf = get_shell_surface(surface);
247 if (shsurf)
248 return shsurf->view;
249
250 wl_list_for_each(view, &surface->views, surface_link)
251 if (weston_view_is_mapped(view))
252 return view;
253
254 return container_of(surface->views.next, struct weston_view, surface_link);
255}
256
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300257static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300258shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400259 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300260 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400261 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800262 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264 struct desktop_shell *shell = shsurf->shell;
265
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200266 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400267
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268 grab->grab.interface = interface;
269 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400270 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500271 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400272 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700274 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400275 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400276 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800277 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400278 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 weston_pointer_set_focus(pointer,
280 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400281 wl_fixed_from_int(0),
282 wl_fixed_from_int(0));
283 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300284}
285
Jonny Lambd73c6942014-08-20 15:53:20 +0200286static void
287get_output_panel_size(struct desktop_shell *shell,
288 struct weston_output *output,
289 int *width,
290 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700291{
292 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200293
294 *width = 0;
295 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700296
297 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200298 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700299
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300300 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200301 float x, y;
302
303 if (view->surface->output != output)
304 continue;
305
306 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800307 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
308 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200309 weston_view_to_global_float(view,
310 view->surface->width, 0,
311 &x, &y);
312
Derek Foreman612341f2015-04-15 12:23:55 -0500313 *width = (int)x - output->x;
314 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200315 return;
316
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800317 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
318 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200319 weston_view_to_global_float(view,
320 0, view->surface->height,
321 &x, &y);
322
Derek Foreman612341f2015-04-15 12:23:55 -0500323 *width = view->surface->width + (int)x - output->x;
324 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200325 return;
326
327 default:
328 /* we've already set width and height to
329 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700330 break;
331 }
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;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800352 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200353 area->width = output->width;
354 area->height = output->height - panel_height;
355 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500357 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800358 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200359 area->width = output->width - panel_width;
360 area->height = output->height;
361 break;
362 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700363}
364
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400365static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300366shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300367{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700368 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400369 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700370 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400371
372 if (grab->shsurf->resize_edges) {
373 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400374 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700375 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300376
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700377 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378}
379
380static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700381shell_touch_grab_start(struct shell_touch_grab *grab,
382 const struct weston_touch_grab_interface *interface,
383 struct shell_surface *shsurf,
384 struct weston_touch *touch)
385{
386 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800387
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200388 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700389
Rusty Lynch1084da52013-08-15 09:10:08 -0700390 grab->grab.interface = interface;
391 grab->shsurf = shsurf;
392 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
393 wl_signal_add(&shsurf->destroy_signal,
394 &grab->shsurf_destroy_listener);
395
396 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700397 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700398
399 weston_touch_start_grab(touch, &grab->grab);
400 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500401 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500402 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700403}
404
405static void
406shell_touch_grab_end(struct shell_touch_grab *grab)
407{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700408 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700409 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700410 grab->shsurf->grabbed = 0;
411 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700412
413 weston_touch_end_grab(grab->touch);
414}
415
416static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800418 struct weston_output *output);
419
Daniel Stone496ca172012-05-30 16:31:42 +0100420static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300421get_modifier(char *modifier)
422{
423 if (!modifier)
424 return MODIFIER_SUPER;
425
426 if (!strcmp("ctrl", modifier))
427 return MODIFIER_CTRL;
428 else if (!strcmp("alt", modifier))
429 return MODIFIER_ALT;
430 else if (!strcmp("super", modifier))
431 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000432 else if (!strcmp("none", modifier))
433 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300434 else
435 return MODIFIER_SUPER;
436}
437
Juan Zhaoe10d2792012-04-25 19:09:52 +0800438static enum animation_type
439get_animation_type(char *animation)
440{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800441 if (!animation)
442 return ANIMATION_NONE;
443
Juan Zhaoe10d2792012-04-25 19:09:52 +0800444 if (!strcmp("zoom", animation))
445 return ANIMATION_ZOOM;
446 else if (!strcmp("fade", animation))
447 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100448 else if (!strcmp("dim-layer", animation))
449 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800450 else
451 return ANIMATION_NONE;
452}
453
Alex Wu4539b082012-03-01 12:57:46 +0800454static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400455shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200456{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400457 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500458 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300459 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000460 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200461
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300462 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400463 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300464 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
465 WESTON_SHELL_CLIENT);
466 if (ret < 0)
467 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400468 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500469 "client", &s, client);
470 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100471 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000472
473 weston_config_section_get_bool(section,
474 "allow-zap", &allow_zap, true);
475 shell->allow_zap = allow_zap;
476
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100477 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400478 "binding-modifier", &s, "super");
479 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200480 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800481
482 weston_config_section_get_string(section,
483 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000484 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800485 free(s);
486
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400487 weston_config_section_get_string(section, "animation", &s, "none");
488 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200489 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200490 weston_config_section_get_string(section, "close-animation", &s, "fade");
491 shell->win_close_animation_type = get_animation_type(s);
492 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700493 weston_config_section_get_string(section,
494 "startup-animation", &s, "fade");
495 shell->startup_animation_type = get_animation_type(s);
496 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700497 if (shell->startup_animation_type == ANIMATION_ZOOM)
498 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100499 weston_config_section_get_string(section, "focus-animation", &s, "none");
500 shell->focus_animation_type = get_animation_type(s);
501 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400502 weston_config_section_get_uint(section, "num-workspaces",
503 &shell->workspaces.num,
504 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200505}
506
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800507struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100508get_default_output(struct weston_compositor *compositor)
509{
Armin Krezović10b06182016-06-23 11:59:30 +0200510 if (wl_list_empty(&compositor->output_list))
511 return NULL;
512
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100513 return container_of(compositor->output_list.next,
514 struct weston_output, link);
515}
516
Pekka Paalanen8274d902014-08-06 19:36:51 +0300517static int
518focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
519{
520 return snprintf(buf, len, "focus highlight effect for output %s",
521 surface->output->name);
522}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100523
524/* no-op func for checking focus surface */
525static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200526focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100527{
528}
529
530static struct focus_surface *
531get_focus_surface(struct weston_surface *surface)
532{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200533 if (surface->committed == focus_surface_committed)
534 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100535 else
536 return NULL;
537}
538
539static bool
540is_focus_surface (struct weston_surface *es)
541{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200542 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100543}
544
545static bool
546is_focus_view (struct weston_view *view)
547{
548 return is_focus_surface (view->surface);
549}
550
551static struct focus_surface *
552create_focus_surface(struct weston_compositor *ec,
553 struct weston_output *output)
554{
555 struct focus_surface *fsurf = NULL;
556 struct weston_surface *surface = NULL;
557
558 fsurf = malloc(sizeof *fsurf);
559 if (!fsurf)
560 return NULL;
561
562 fsurf->surface = weston_surface_create(ec);
563 surface = fsurf->surface;
564 if (surface == NULL) {
565 free(fsurf);
566 return NULL;
567 }
568
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200569 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100570 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200571 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200572 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300573 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100574
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800575 fsurf->view = weston_view_create(surface);
576 if (fsurf->view == NULL) {
577 weston_surface_destroy(surface);
578 free(fsurf);
579 return NULL;
580 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100581 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200582 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100583
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600584 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600585 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
587 pixman_region32_fini(&surface->opaque);
588 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
589 output->width, output->height);
590 pixman_region32_fini(&surface->input);
591 pixman_region32_init(&surface->input);
592
593 wl_list_init(&fsurf->workspace_transform.link);
594
595 return fsurf;
596}
597
598static void
599focus_surface_destroy(struct focus_surface *fsurf)
600{
601 weston_surface_destroy(fsurf->surface);
602 free(fsurf);
603}
604
605static void
606focus_animation_done(struct weston_view_animation *animation, void *data)
607{
608 struct workspace *ws = data;
609
610 ws->focus_animation = NULL;
611}
612
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200613static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200614focus_state_destroy(struct focus_state *state)
615{
616 wl_list_remove(&state->seat_destroy_listener.link);
617 wl_list_remove(&state->surface_destroy_listener.link);
618 free(state);
619}
620
621static void
622focus_state_seat_destroy(struct wl_listener *listener, void *data)
623{
624 struct focus_state *state = container_of(listener,
625 struct focus_state,
626 seat_destroy_listener);
627
628 wl_list_remove(&state->link);
629 focus_state_destroy(state);
630}
631
632static void
633focus_state_surface_destroy(struct wl_listener *listener, void *data)
634{
635 struct focus_state *state = container_of(listener,
636 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400637 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200638 struct weston_surface *main_surface;
639 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500640 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200641
Pekka Paalanen01388e22013-04-25 13:57:44 +0300642 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
643
Kristian Høgsberge3778222012-07-31 17:29:30 -0400644 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300645 wl_list_for_each(view,
646 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400648 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100649 if (is_focus_view(view))
650 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200651 if (!get_shell_surface(view->surface))
652 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400653
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200654 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400655 break;
656 }
657
Pekka Paalanen01388e22013-04-25 13:57:44 +0300658 /* if the focus was a sub-surface, activate its main surface */
659 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200660 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300661
Kristian Høgsberge3778222012-07-31 17:29:30 -0400662 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100663 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200664 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200665 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400666 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200667 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100668 if (state->ws->focus_animation)
669 weston_view_animation_destroy(state->ws->focus_animation);
670
671 state->ws->focus_animation = weston_fade_run(
672 state->ws->fsurf_front->view,
673 state->ws->fsurf_front->view->alpha, 0.0, 300,
674 focus_animation_done, state->ws);
675 }
676
Kristian Høgsberge3778222012-07-31 17:29:30 -0400677 wl_list_remove(&state->link);
678 focus_state_destroy(state);
679 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200680}
681
682static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200683focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
684 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200685{
Jonas Ådahl04769742012-06-13 00:01:24 +0200686 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200687
688 state = malloc(sizeof *state);
689 if (state == NULL)
690 return NULL;
691
Quentin Glidicfff39812016-08-15 10:56:52 +0200692 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100693 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400694 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200695 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400696 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200697
698 state->seat_destroy_listener.notify = focus_state_seat_destroy;
699 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400700 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200701 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400702 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200703
704 return state;
705}
706
Jonas Ådahl8538b222012-08-29 22:13:03 +0200707static struct focus_state *
708ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
709{
710 struct workspace *ws = get_current_workspace(shell);
711 struct focus_state *state;
712
713 wl_list_for_each(state, &ws->focus_list, link)
714 if (state->seat == seat)
715 break;
716
717 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200718 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200719
720 return state;
721}
722
Jonas Ådahl04769742012-06-13 00:01:24 +0200723static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800724focus_state_set_focus(struct focus_state *state,
725 struct weston_surface *surface)
726{
727 if (state->keyboard_focus) {
728 wl_list_remove(&state->surface_destroy_listener.link);
729 wl_list_init(&state->surface_destroy_listener.link);
730 }
731
732 state->keyboard_focus = surface;
733 if (surface)
734 wl_signal_add(&surface->destroy_signal,
735 &state->surface_destroy_listener);
736}
737
738static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400739restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200740{
741 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400742 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100743 struct wl_list pending_seat_list;
744 struct weston_seat *seat, *next_seat;
745
746 /* Temporarily steal the list of seats so that we can keep
747 * track of the seats we've already processed */
748 wl_list_init(&pending_seat_list);
749 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
750 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200751
752 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500753 struct weston_keyboard *keyboard =
754 weston_seat_get_keyboard(state->seat);
755
Neil Roberts4237f502014-04-09 16:33:31 +0100756 wl_list_remove(&state->seat->link);
757 wl_list_insert(&shell->compositor->seat_list,
758 &state->seat->link);
759
Derek Foreman1281a362015-07-31 16:55:32 -0500760 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800761 continue;
762
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400763 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200764
Derek Foreman1281a362015-07-31 16:55:32 -0500765 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200766 }
Neil Roberts4237f502014-04-09 16:33:31 +0100767
768 /* For any remaining seats that we don't have a focus state
769 * for we'll reset the keyboard focus to NULL */
770 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500771 struct weston_keyboard *keyboard =
772 weston_seat_get_keyboard(seat);
773
Neil Roberts4237f502014-04-09 16:33:31 +0100774 wl_list_insert(&shell->compositor->seat_list, &seat->link);
775
Derek Foreman1281a362015-07-31 16:55:32 -0500776 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100777 continue;
778
Derek Foreman1281a362015-07-31 16:55:32 -0500779 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100780 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200781}
782
783static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200784replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
785 struct weston_seat *seat)
786{
Derek Foreman1281a362015-07-31 16:55:32 -0500787 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200788 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789
790 wl_list_for_each(state, &ws->focus_list, link) {
791 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500792 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200793 return;
794 }
795 }
796}
797
798static void
799drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
800 struct weston_surface *surface)
801{
802 struct focus_state *state;
803
804 wl_list_for_each(state, &ws->focus_list, link)
805 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800806 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200807}
808
809static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100810animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
811 struct weston_view *from, struct weston_view *to)
812{
813 struct weston_output *output;
814 bool focus_surface_created = false;
815
816 /* FIXME: Only support dim animation using two layers */
817 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
818 return;
819
820 output = get_default_output(shell->compositor);
821 if (ws->fsurf_front == NULL && (from || to)) {
822 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800823 if (ws->fsurf_front == NULL)
824 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100825 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800826
827 ws->fsurf_back = create_focus_surface(shell->compositor, output);
828 if (ws->fsurf_back == NULL) {
829 focus_surface_destroy(ws->fsurf_front);
830 return;
831 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800833
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834 focus_surface_created = true;
835 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300836 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
837 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100838 }
839
840 if (ws->focus_animation) {
841 weston_view_animation_destroy(ws->focus_animation);
842 ws->focus_animation = NULL;
843 }
844
845 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300846 weston_layer_entry_insert(&to->layer_link,
847 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100848 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300849 weston_layer_entry_insert(&ws->layer.view_list,
850 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851
852 if (focus_surface_created) {
853 ws->focus_animation = weston_fade_run(
854 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200855 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100856 focus_animation_done, ws);
857 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300858 weston_layer_entry_insert(&from->layer_link,
859 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 ws->focus_animation = weston_stable_fade_run(
861 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200862 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 focus_animation_done, ws);
864 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300865 weston_layer_entry_insert(&ws->layer.view_list,
866 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100867 ws->focus_animation = weston_stable_fade_run(
868 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200869 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 focus_animation_done, ws);
871 }
872}
873
874static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200875workspace_destroy(struct workspace *ws)
876{
Jonas Ådahl04769742012-06-13 00:01:24 +0200877 struct focus_state *state, *next;
878
879 wl_list_for_each_safe(state, next, &ws->focus_list, link)
880 focus_state_destroy(state);
881
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 if (ws->fsurf_front)
883 focus_surface_destroy(ws->fsurf_front);
884 if (ws->fsurf_back)
885 focus_surface_destroy(ws->fsurf_back);
886
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200887 free(ws);
888}
889
Jonas Ådahl04769742012-06-13 00:01:24 +0200890static void
891seat_destroyed(struct wl_listener *listener, void *data)
892{
893 struct weston_seat *seat = data;
894 struct focus_state *state, *next;
895 struct workspace *ws = container_of(listener,
896 struct workspace,
897 seat_destroyed_listener);
898
899 wl_list_for_each_safe(state, next, &ws->focus_list, link)
900 if (state->seat == seat)
901 wl_list_remove(&state->link);
902}
903
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200904static struct workspace *
905workspace_create(void)
906{
907 struct workspace *ws = malloc(sizeof *ws);
908 if (ws == NULL)
909 return NULL;
910
911 weston_layer_init(&ws->layer, NULL);
912
Jonas Ådahl04769742012-06-13 00:01:24 +0200913 wl_list_init(&ws->focus_list);
914 wl_list_init(&ws->seat_destroyed_listener.link);
915 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100916 ws->fsurf_front = NULL;
917 ws->fsurf_back = NULL;
918 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200919
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200920 return ws;
921}
922
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200923static int
924workspace_is_empty(struct workspace *ws)
925{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300926 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200927}
928
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200929static struct workspace *
930get_workspace(struct desktop_shell *shell, unsigned int index)
931{
932 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200933 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200934 pws += index;
935 return *pws;
936}
937
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800938struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200939get_current_workspace(struct desktop_shell *shell)
940{
941 return get_workspace(shell, shell->workspaces.current);
942}
943
944static void
945activate_workspace(struct desktop_shell *shell, unsigned int index)
946{
947 struct workspace *ws;
948
949 ws = get_workspace(shell, index);
950 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
951
952 shell->workspaces.current = index;
953}
954
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200955static unsigned int
956get_output_height(struct weston_output *output)
957{
958 return abs(output->region.extents.y1 - output->region.extents.y2);
959}
960
961static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100962view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200963{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964 struct weston_transform *transform;
965
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100966 if (is_focus_view(view)) {
967 struct focus_surface *fsurf = get_focus_surface(view->surface);
968 transform = &fsurf->workspace_transform;
969 } else {
970 struct shell_surface *shsurf = get_shell_surface(view->surface);
971 transform = &shsurf->workspace_transform;
972 }
973
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200974 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100978 weston_matrix_init(&transform->matrix);
979 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982}
983
984static void
985workspace_translate_out(struct workspace *ws, double fraction)
986{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988 unsigned int height;
989 double d;
990
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300991 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993 d = height * fraction;
994
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100995 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 }
997}
998
999static void
1000workspace_translate_in(struct workspace *ws, double fraction)
1001{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001003 unsigned int height;
1004 double d;
1005
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001006 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001008
1009 if (fraction > 0)
1010 d = -(height - height * fraction);
1011 else
1012 d = height + height * fraction;
1013
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001014 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 }
1016}
1017
1018static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019reverse_workspace_change_animation(struct desktop_shell *shell,
1020 unsigned int index,
1021 struct workspace *from,
1022 struct workspace *to)
1023{
1024 shell->workspaces.current = index;
1025
1026 shell->workspaces.anim_to = to;
1027 shell->workspaces.anim_from = from;
1028 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1029 shell->workspaces.anim_timestamp = 0;
1030
Scott Moreau4272e632012-08-13 09:58:41 -06001031 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001032}
1033
1034static void
1035workspace_deactivate_transforms(struct workspace *ws)
1036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001038 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001039
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001040 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001041 if (is_focus_view(view)) {
1042 struct focus_surface *fsurf = get_focus_surface(view->surface);
1043 transform = &fsurf->workspace_transform;
1044 } else {
1045 struct shell_surface *shsurf = get_shell_surface(view->surface);
1046 transform = &shsurf->workspace_transform;
1047 }
1048
1049 if (!wl_list_empty(&transform->link)) {
1050 wl_list_remove(&transform->link);
1051 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001052 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001054 }
1055}
1056
1057static void
1058finish_workspace_change_animation(struct desktop_shell *shell,
1059 struct workspace *from,
1060 struct workspace *to)
1061{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001062 struct weston_view *view;
1063
Scott Moreau4272e632012-08-13 09:58:41 -06001064 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001066 /* Views that extend past the bottom of the output are still
1067 * visible after the workspace animation ends but before its layer
1068 * is hidden. In that case, we need to damage below those views so
1069 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001070 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001071 weston_view_damage_below(view);
1072
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001073 wl_list_remove(&shell->workspaces.animation.link);
1074 workspace_deactivate_transforms(from);
1075 workspace_deactivate_transforms(to);
1076 shell->workspaces.anim_to = NULL;
1077
1078 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1079}
1080
1081static void
1082animate_workspace_change_frame(struct weston_animation *animation,
1083 struct weston_output *output, uint32_t msecs)
1084{
1085 struct desktop_shell *shell =
1086 container_of(animation, struct desktop_shell,
1087 workspaces.animation);
1088 struct workspace *from = shell->workspaces.anim_from;
1089 struct workspace *to = shell->workspaces.anim_to;
1090 uint32_t t;
1091 double x, y;
1092
1093 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1094 finish_workspace_change_animation(shell, from, to);
1095 return;
1096 }
1097
1098 if (shell->workspaces.anim_timestamp == 0) {
1099 if (shell->workspaces.anim_current == 0.0)
1100 shell->workspaces.anim_timestamp = msecs;
1101 else
1102 shell->workspaces.anim_timestamp =
1103 msecs -
1104 /* Invers of movement function 'y' below. */
1105 (asin(1.0 - shell->workspaces.anim_current) *
1106 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1107 M_2_PI);
1108 }
1109
1110 t = msecs - shell->workspaces.anim_timestamp;
1111
1112 /*
1113 * x = [0, π/2]
1114 * y(x) = sin(x)
1115 */
1116 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1117 y = sin(x);
1118
1119 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001120 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121
1122 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1123 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1124 shell->workspaces.anim_current = y;
1125
Scott Moreau4272e632012-08-13 09:58:41 -06001126 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001128 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001129 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130}
1131
1132static void
1133animate_workspace_change(struct desktop_shell *shell,
1134 unsigned int index,
1135 struct workspace *from,
1136 struct workspace *to)
1137{
1138 struct weston_output *output;
1139
1140 int dir;
1141
1142 if (index > shell->workspaces.current)
1143 dir = -1;
1144 else
1145 dir = 1;
1146
1147 shell->workspaces.current = index;
1148
1149 shell->workspaces.anim_dir = dir;
1150 shell->workspaces.anim_from = from;
1151 shell->workspaces.anim_to = to;
1152 shell->workspaces.anim_current = 0.0;
1153 shell->workspaces.anim_timestamp = 0;
1154
1155 output = container_of(shell->compositor->output_list.next,
1156 struct weston_output, link);
1157 wl_list_insert(&output->animation_list,
1158 &shell->workspaces.animation.link);
1159
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161
1162 workspace_translate_in(to, 0);
1163
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001164 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001165
Scott Moreau4272e632012-08-13 09:58:41 -06001166 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001167}
1168
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001169static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001170update_workspace(struct desktop_shell *shell, unsigned int index,
1171 struct workspace *from, struct workspace *to)
1172{
1173 shell->workspaces.current = index;
1174 wl_list_insert(&from->layer.link, &to->layer.link);
1175 wl_list_remove(&from->layer.link);
1176}
1177
1178static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001179change_workspace(struct desktop_shell *shell, unsigned int index)
1180{
1181 struct workspace *from;
1182 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001183 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184
1185 if (index == shell->workspaces.current)
1186 return;
1187
1188 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001189 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001190 return;
1191
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192 from = get_current_workspace(shell);
1193 to = get_workspace(shell, index);
1194
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001195 if (shell->workspaces.anim_from == to &&
1196 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001197 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001198 reverse_workspace_change_animation(shell, index, from, to);
1199 return;
1200 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001202 if (shell->workspaces.anim_to != NULL)
1203 finish_workspace_change_animation(shell,
1204 shell->workspaces.anim_from,
1205 shell->workspaces.anim_to);
1206
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001207 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001208
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001209 if (shell->focus_animation_type != ANIMATION_NONE) {
1210 wl_list_for_each(state, &from->focus_list, link)
1211 if (state->keyboard_focus)
1212 animate_focus_change(shell, from,
1213 get_default_view(state->keyboard_focus), NULL);
1214
1215 wl_list_for_each(state, &to->focus_list, link)
1216 if (state->keyboard_focus)
1217 animate_focus_change(shell, to,
1218 NULL, get_default_view(state->keyboard_focus));
1219 }
1220
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 if (workspace_is_empty(to) && workspace_is_empty(from))
1222 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001223 else
1224 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001225}
1226
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001227static bool
1228workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1229{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001230 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 struct wl_list *e;
1232
1233 if (wl_list_empty(list))
1234 return false;
1235
1236 e = list->next;
1237
1238 if (e->next != list)
1239 return false;
1240
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001241 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242}
1243
1244static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001245surface_keyboard_focus_lost(struct weston_surface *surface)
1246{
1247 struct weston_compositor *compositor = surface->compositor;
1248 struct weston_seat *seat;
1249 struct weston_surface *focus;
1250
1251 wl_list_for_each(seat, &compositor->seat_list, link) {
1252 struct weston_keyboard *keyboard =
1253 weston_seat_get_keyboard(seat);
1254
1255 if (!keyboard)
1256 continue;
1257
1258 focus = weston_surface_get_main_surface(keyboard->focus);
1259 if (focus == surface)
1260 weston_keyboard_set_focus(keyboard, NULL);
1261 }
1262}
1263
1264static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001266 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 unsigned int index)
1268{
Derek Foreman1281a362015-07-31 16:55:32 -05001269 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001270 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001272 struct shell_surface *shsurf;
1273 struct workspace *from;
1274 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001275 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001276
Derek Foreman1281a362015-07-31 16:55:32 -05001277 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 view = get_default_view(surface);
1279 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001280 index == shell->workspaces.current ||
1281 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282 return;
1283
1284 from = get_current_workspace(shell);
1285 to = get_workspace(shell, index);
1286
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001287 weston_layer_entry_remove(&view->layer_link);
1288 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289
Philip Withnall659163d2013-11-25 18:01:36 +00001290 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001291 if (shsurf != NULL)
1292 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001293
Jonas Ådahle9d22502012-08-29 22:13:01 +02001294 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 drop_focus_state(shell, from, surface);
1296
1297 if (shell->workspaces.anim_from == to &&
1298 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299 wl_list_remove(&to->layer.link);
1300 wl_list_insert(from->layer.link.prev, &to->layer.link);
1301
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304 return;
1305 }
1306
1307 if (shell->workspaces.anim_to != NULL)
1308 finish_workspace_change_animation(shell,
1309 shell->workspaces.anim_from,
1310 shell->workspaces.anim_to);
1311
1312 if (workspace_is_empty(from) &&
1313 workspace_has_only(to, surface))
1314 update_workspace(shell, index, from, to);
1315 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001316 if (shsurf != NULL &&
1317 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318 wl_list_insert(&shell->workspaces.anim_sticky_list,
1319 &shsurf->workspace_transform.link);
1320
1321 animate_workspace_change(shell, index, from, to);
1322 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
Jonas Ådahl8538b222012-08-29 22:13:03 +02001324 state = ensure_focus_state(shell, seat);
1325 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001326 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327}
1328
1329static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001330touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001331 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001332{
1333}
1334
1335static void
1336touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1337{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001338 struct weston_touch_move_grab *move =
1339 (struct weston_touch_move_grab *) container_of(
1340 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001341
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001342 if (touch_id == 0)
1343 move->active = 0;
1344
Jonas Ådahl9484b692013-12-02 22:05:03 +01001345 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001346 shell_touch_grab_end(&move->base);
1347 free(move);
1348 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001349}
1350
1351static void
1352touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001353 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001354{
1355 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1356 struct shell_surface *shsurf = move->base.shsurf;
1357 struct weston_surface *es;
1358 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1359 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1360
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001361 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001362 return;
1363
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001364 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001365
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001366 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001367
1368 weston_compositor_schedule_repaint(es->compositor);
1369}
1370
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001371static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001372touch_move_grab_frame(struct weston_touch_grab *grab)
1373{
1374}
1375
1376static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001377touch_move_grab_cancel(struct weston_touch_grab *grab)
1378{
1379 struct weston_touch_move_grab *move =
1380 (struct weston_touch_move_grab *) container_of(
1381 grab, struct shell_touch_grab, grab);
1382
1383 shell_touch_grab_end(&move->base);
1384 free(move);
1385}
1386
Rusty Lynch1084da52013-08-15 09:10:08 -07001387static const struct weston_touch_grab_interface touch_move_grab_interface = {
1388 touch_move_grab_down,
1389 touch_move_grab_up,
1390 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001391 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001392 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001393};
1394
1395static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001396surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001397{
1398 struct weston_touch_move_grab *move;
1399
1400 if (!shsurf)
1401 return -1;
1402
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001403 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1404 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001405 return 0;
1406
1407 move = malloc(sizeof *move);
1408 if (!move)
1409 return -1;
1410
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001411 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001413 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001415 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001416
1417 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001418 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001419
1420 return 0;
1421}
1422
1423static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001424noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001425{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001426}
1427
1428static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001429noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001430 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001431{
1432}
1433
1434static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001435noop_grab_axis_source(struct weston_pointer_grab *grab,
1436 uint32_t source)
1437{
1438}
1439
1440static void
1441noop_grab_frame(struct weston_pointer_grab *grab)
1442{
1443}
1444
1445static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001446constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1447{
1448 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001449 struct weston_surface *surface =
1450 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001451 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001452 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001453 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001454 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001455 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001456
1457 x = wl_fixed_to_int(pointer->x + move->dx);
1458 y = wl_fixed_to_int(pointer->y + move->dy);
1459
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001460 if (shsurf->shell->panel_position ==
1461 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001462 get_output_work_area(shsurf->shell, surface->output, &area);
1463 geometry =
1464 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001465
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001466 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001467 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001468 y = area.y + safety - geometry.height
1469 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001470
1471 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001472 y + geometry.y < area.y)
1473 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001474 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001475
1476 *cx = x;
1477 *cy = y;
1478}
1479
1480static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001481move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001482 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001484 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001485 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001486 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001487 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001488 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001489
Jonas Ådahld2510102014-10-05 21:39:14 +02001490 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001491 if (!shsurf)
1492 return;
1493
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001494 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1495
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001496 constrain_position(move, &cx, &cy);
1497
1498 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001500 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001501}
1502
1503static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001504move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001505 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001506{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001507 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1508 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001509 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001510 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001511
Daniel Stone4dbadb12012-05-30 16:31:51 +01001512 if (pointer->button_count == 0 &&
1513 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001514 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001515 free(grab);
1516 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001517}
1518
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001519static void
1520move_grab_cancel(struct weston_pointer_grab *grab)
1521{
1522 struct shell_grab *shell_grab =
1523 container_of(grab, struct shell_grab, grab);
1524
1525 shell_grab_end(shell_grab);
1526 free(grab);
1527}
1528
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001529static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001530 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531 move_grab_motion,
1532 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001533 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001534 noop_grab_axis_source,
1535 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001536 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001537};
1538
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001539static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001540surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001541 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001542{
1543 struct weston_move_grab *move;
1544
1545 if (!shsurf)
1546 return -1;
1547
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001548 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001549 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1550 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001551 return 0;
1552
1553 move = malloc(sizeof *move);
1554 if (!move)
1555 return -1;
1556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001558 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001560 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001561 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562
1563 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001564 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001565
1566 return 0;
1567}
1568
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569struct weston_resize_grab {
1570 struct shell_grab base;
1571 uint32_t edges;
1572 int32_t width, height;
1573};
1574
1575static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001576resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001577 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001578{
1579 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001580 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581 struct shell_surface *shsurf = resize->base.shsurf;
1582 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001583 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584 wl_fixed_t from_x, from_y;
1585 wl_fixed_t to_x, to_y;
1586
Jonas Ådahld2510102014-10-05 21:39:14 +02001587 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001588
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001589 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590 return;
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 weston_view_from_global_fixed(shsurf->view,
1593 pointer->grab_x, pointer->grab_y,
1594 &from_x, &from_y);
1595 weston_view_from_global_fixed(shsurf->view,
1596 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597
1598 width = resize->width;
1599 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1600 width += wl_fixed_to_int(from_x - to_x);
1601 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1602 width += wl_fixed_to_int(to_x - from_x);
1603 }
1604
1605 height = resize->height;
1606 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1607 height += wl_fixed_to_int(from_y - to_y);
1608 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1609 height += wl_fixed_to_int(to_y - from_y);
1610 }
1611
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001612 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1613 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1614
1615 min_size.width = MAX(1, min_size.width);
1616 min_size.height = MAX(1, min_size.height);
1617
1618 if (width < min_size.width)
1619 width = min_size.width;
1620 else if (max_size.width > 0 && width > max_size.width)
1621 width = max_size.width;
1622 if (height < min_size.height)
1623 height = min_size.height;
1624 else if (max_size.width > 0 && width > max_size.width)
1625 width = max_size.width;
1626 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001627}
1628
1629static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001630resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001631 uint32_t time, uint32_t button, uint32_t state_w)
1632{
1633 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001634 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001635 enum wl_pointer_button_state state = state_w;
1636
1637 if (pointer->button_count == 0 &&
1638 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1639 shell_grab_end(&resize->base);
1640 free(grab);
1641 }
1642}
1643
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001644static void
1645resize_grab_cancel(struct weston_pointer_grab *grab)
1646{
1647 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1648
1649 shell_grab_end(&resize->base);
1650 free(grab);
1651}
1652
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001653static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654 noop_grab_focus,
1655 resize_grab_motion,
1656 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001657 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001658 noop_grab_axis_source,
1659 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001660 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001661};
1662
Giulio Camuffob8366642013-04-25 13:57:46 +03001663/*
1664 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001665 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001666static void
1667surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1668 int32_t *y, int32_t *w, int32_t *h) {
1669 pixman_region32_t region;
1670 pixman_box32_t *box;
1671 struct weston_subsurface *subsurface;
1672
1673 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 surface->width,
1675 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001676
1677 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1678 pixman_region32_union_rect(&region, &region,
1679 subsurface->position.x,
1680 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 subsurface->surface->width,
1682 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001683 }
1684
1685 box = pixman_region32_extents(&region);
1686 if (x)
1687 *x = box->x1;
1688 if (y)
1689 *y = box->y1;
1690 if (w)
1691 *w = box->x2 - box->x1;
1692 if (h)
1693 *h = box->y2 - box->y1;
1694
1695 pixman_region32_fini(&region);
1696}
1697
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001698static int
1699surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001700 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701{
1702 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001703 const unsigned resize_topbottom =
1704 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1705 const unsigned resize_leftright =
1706 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1707 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001708 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001709
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001710 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001711 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1712 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713 return 0;
1714
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001715 /* Check for invalid edge combinations. */
1716 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1717 (edges & resize_topbottom) == resize_topbottom ||
1718 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 return 0;
1720
1721 resize = malloc(sizeof *resize);
1722 if (!resize)
1723 return -1;
1724
1725 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001727 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1728 resize->width = geometry.width;
1729 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001730
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001731 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001733 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734
1735 return 0;
1736}
1737
1738static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001739busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001740{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001742 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001743 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001745 wl_fixed_t sx, sy;
1746
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 view = weston_compositor_pick_view(pointer->seat->compositor,
1748 pointer->x, pointer->y,
1749 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001750 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001751
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001752 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001753 shell_grab_end(grab);
1754 free(grab);
1755 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001756}
1757
1758static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001759busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001760 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761{
Jonas Ådahld2510102014-10-05 21:39:14 +02001762 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001766busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001767 uint32_t time, uint32_t button, uint32_t state)
1768{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001769 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001770 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001771 struct weston_pointer *pointer = grab->grab.pointer;
1772 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001773
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001774 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001775 activate(shsurf->shell, shsurf->view, seat,
1776 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001777 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001778 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001779 activate(shsurf->shell, shsurf->view, seat,
1780 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001781 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001782 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783}
1784
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001785static void
1786busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1787{
1788 struct shell_grab *grab = (struct shell_grab *) base;
1789
1790 shell_grab_end(grab);
1791 free(grab);
1792}
1793
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001794static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001795 busy_cursor_grab_focus,
1796 busy_cursor_grab_motion,
1797 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001798 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001799 noop_grab_axis_source,
1800 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001801 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802};
1803
1804static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001805handle_pointer_focus(struct wl_listener *listener, void *data)
1806{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001807 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001809 struct shell_surface *shsurf;
1810 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813 return;
1814
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001815 shsurf = get_shell_surface(view->surface);
1816 if (!shsurf)
1817 return;
1818
1819 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1820
1821 if (shsurf->unresponsive)
1822 set_busy_cursor(shsurf, pointer);
1823 else
1824 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825}
1826
1827static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001828shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1829{
1830 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001831 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001832}
1833
1834static void
1835shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1836{
1837 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001838 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001839}
1840
1841static void
1842handle_keyboard_focus(struct wl_listener *listener, void *data)
1843{
1844 struct weston_keyboard *keyboard = data;
1845 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1846
1847 if (seat->focused_surface) {
1848 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1849 if (shsurf)
1850 shell_surface_lose_keyboard_focus(shsurf);
1851 }
1852
1853 seat->focused_surface = keyboard->focus;
1854
1855 if (seat->focused_surface) {
1856 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1857 if (shsurf)
1858 shell_surface_gain_keyboard_focus(shsurf);
1859 }
1860}
1861
Philip Withnall07926d92013-11-25 18:01:40 +00001862/* The surface will be inserted into the list immediately after the link
1863 * returned by this function (i.e. will be stacked immediately above the
1864 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001865static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001866shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1867{
1868 struct workspace *ws;
1869
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001870 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1871 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001872 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001873 }
1874
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 /* Move the surface to a normal workspace layer so that surfaces
1876 * which were previously fullscreen or transient are no longer
1877 * rendered on top. */
1878 ws = get_current_workspace(shsurf->shell);
1879 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001880}
1881
Philip Withnall648a4dd2013-11-25 18:01:44 +00001882static void
1883shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1884{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001885 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001886}
1887
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001888/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001889 * geometry to ensure the changes are redrawn.
1890 *
1891 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1892 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001893static void
1894shell_surface_update_layer(struct shell_surface *shsurf)
1895{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001896 struct weston_surface *surface =
1897 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001898 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001899
1900 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1901
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001902 if (new_layer_link == NULL)
1903 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001904 if (new_layer_link == &shsurf->view->layer_link)
1905 return;
1906
1907 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001908 weston_layer_entry_remove(&shsurf->view->layer_link);
1909 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001910 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001911 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001912
1913 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001914}
1915
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001916static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001917shell_surface_set_output(struct shell_surface *shsurf,
1918 struct weston_output *output)
1919{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001920 struct weston_surface *es =
1921 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001922
1923 /* get the default output, if the client set it as NULL
1924 check whether the ouput is available */
1925 if (output)
1926 shsurf->output = output;
1927 else if (es->output)
1928 shsurf->output = es->output;
1929 else
1930 shsurf->output = get_default_output(es->compositor);
1931}
1932
1933static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001934weston_view_set_initial_position(struct weston_view *view,
1935 struct desktop_shell *shell);
1936
1937static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001938unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001939{
Philip Withnallf85fe842013-11-25 18:01:37 +00001940 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001941 wl_list_remove(&shsurf->fullscreen.transform.link);
1942 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001943
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 if (shsurf->fullscreen.black_view)
1945 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1946 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001947
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001948 if (shsurf->saved_position_valid)
1949 weston_view_set_position(shsurf->view,
1950 shsurf->saved_x, shsurf->saved_y);
1951 else
1952 weston_view_set_initial_position(shsurf->view, shsurf->shell);
1953
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001954 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001956 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001957 shsurf->saved_rotation_valid = false;
1958 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001959
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001960 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08001961}
1962
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001963static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001964unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001965{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001966 struct weston_surface *surface =
1967 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1968
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001969 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001970 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001971
1972 if (shsurf->saved_position_valid)
1973 weston_view_set_position(shsurf->view,
1974 shsurf->saved_x, shsurf->saved_y);
1975 else
1976 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001977
1978 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 wl_list_insert(&shsurf->view->geometry.transformation_list,
1980 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001981 shsurf->saved_rotation_valid = false;
1982 }
1983
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001984 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001985}
1986
Philip Withnallbecb77e2013-11-25 18:01:30 +00001987static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001988set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001989{
1990 struct shell_surface *shsurf;
1991 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001992 struct weston_view *view;
1993
1994 view = get_default_view(surface);
1995 if (!view)
1996 return;
1997
1998 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1999
2000 shsurf = get_shell_surface(surface);
2001 current_ws = get_current_workspace(shsurf->shell);
2002
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002003 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002004 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002005
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002006 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002007 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002008
2009 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002010 weston_view_damage_below(view);
2011}
2012
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002013
Tiago Vignattibe143262012-04-16 17:31:41 +03002014static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002015shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002016{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002017 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002018}
2019
Pekka Paalanen8274d902014-08-06 19:36:51 +03002020static int
2021black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2022{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002023 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002024 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002025 int n;
2026 int rem;
2027 int ret;
2028
2029 n = snprintf(buf, len, "black background surface for ");
2030 if (n < 0)
2031 return n;
2032
2033 rem = (int)len - n;
2034 if (rem < 0)
2035 rem = 0;
2036
2037 if (fs_surface->get_label)
2038 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2039 else
2040 ret = snprintf(buf + n, rem, "<unknown>");
2041
2042 if (ret < 0)
2043 return n;
2044
2045 return n + ret;
2046}
2047
Alex Wu21858432012-04-01 20:13:08 +08002048static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002049black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002050
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002052create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002053 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002054 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002055{
2056 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002058
2059 surface = weston_surface_create(ec);
2060 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002061 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002062 return NULL;
2063 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 view = weston_view_create(surface);
2065 if (surface == NULL) {
2066 weston_log("no memory\n");
2067 weston_surface_destroy(surface);
2068 return NULL;
2069 }
Alex Wu4539b082012-03-01 12:57:46 +08002070
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002071 surface->committed = black_surface_committed;
2072 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002073 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002074 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002075 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002076 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002077 pixman_region32_fini(&surface->input);
2078 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002079
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002080 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002081 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082
2083 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002084}
2085
Philip Withnalled8f1a92013-11-25 18:01:43 +00002086static void
2087shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2088{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002089 struct weston_surface *surface =
2090 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002091 struct weston_output *output = shsurf->fullscreen_output;
2092
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002093 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002094
2095 if (!shsurf->fullscreen.black_view)
2096 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002097 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002098 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002099 output->x, output->y,
2100 output->width,
2101 output->height);
2102
2103 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002104 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2105 weston_layer_entry_insert(&shsurf->view->layer_link,
2106 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002107 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002108 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002109
Armin Krezović4663aca2016-06-30 06:04:29 +02002110 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002111 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002112}
2113
Alex Wu4539b082012-03-01 12:57:46 +08002114/* Create black surface and append it to the associated fullscreen surface.
2115 * Handle size dismatch and positioning according to the method. */
2116static void
2117shell_configure_fullscreen(struct shell_surface *shsurf)
2118{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002119 struct weston_surface *surface =
2120 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002121 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002122
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002123 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002124 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002125 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2126 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002127
Philip Withnalled8f1a92013-11-25 18:01:43 +00002128 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002129
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002130 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002131 &surf_width, &surf_height);
2132
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002133 if (surface->buffer_ref.buffer)
2134 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002135}
2136
Alex Wu4539b082012-03-01 12:57:46 +08002137static void
2138shell_map_fullscreen(struct shell_surface *shsurf)
2139{
Alex Wubd3354b2012-04-17 17:20:49 +08002140 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002141}
2142
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002143static struct weston_output *
2144get_focused_output(struct weston_compositor *compositor)
2145{
2146 struct weston_seat *seat;
2147 struct weston_output *output = NULL;
2148
2149 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002150 struct weston_touch *touch = weston_seat_get_touch(seat);
2151 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2152 struct weston_keyboard *keyboard =
2153 weston_seat_get_keyboard(seat);
2154
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002155 /* Priority has touch focus, then pointer and
2156 * then keyboard focus. We should probably have
2157 * three for loops and check frist for touch,
2158 * then for pointer, etc. but unless somebody has some
2159 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002160 if (touch && touch->focus)
2161 output = touch->focus->output;
2162 else if (pointer && pointer->focus)
2163 output = pointer->focus->output;
2164 else if (keyboard && keyboard->focus)
2165 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002166
2167 if (output)
2168 break;
2169 }
2170
2171 return output;
2172}
2173
2174static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002175destroy_shell_seat(struct wl_listener *listener, void *data)
2176{
2177 struct shell_seat *shseat =
2178 container_of(listener,
2179 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002180
2181 wl_list_remove(&shseat->seat_destroy_listener.link);
2182 free(shseat);
2183}
2184
Jason Ekstrand024177c2014-04-21 19:42:58 -05002185static void
2186shell_seat_caps_changed(struct wl_listener *listener, void *data)
2187{
Derek Foreman1281a362015-07-31 16:55:32 -05002188 struct weston_keyboard *keyboard;
2189 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002190 struct shell_seat *seat;
2191
2192 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002193 keyboard = weston_seat_get_keyboard(seat->seat);
2194 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002195
Derek Foreman1281a362015-07-31 16:55:32 -05002196 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002198 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002199 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002200 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002201 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002202 wl_list_init(&seat->keyboard_focus_listener.link);
2203 }
2204
Derek Foreman1281a362015-07-31 16:55:32 -05002205 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002206 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002207 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002208 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002209 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002210 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002211 wl_list_init(&seat->pointer_focus_listener.link);
2212 }
2213}
2214
Giulio Camuffo5085a752013-03-25 21:42:45 +01002215static struct shell_seat *
2216create_shell_seat(struct weston_seat *seat)
2217{
2218 struct shell_seat *shseat;
2219
2220 shseat = calloc(1, sizeof *shseat);
2221 if (!shseat) {
2222 weston_log("no memory to allocate shell seat\n");
2223 return NULL;
2224 }
2225
2226 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002227
2228 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2229 wl_signal_add(&seat->destroy_signal,
2230 &shseat->seat_destroy_listener);
2231
Jason Ekstrand024177c2014-04-21 19:42:58 -05002232 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2233 wl_list_init(&shseat->keyboard_focus_listener.link);
2234
2235 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2236 wl_list_init(&shseat->pointer_focus_listener.link);
2237
2238 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2239 wl_signal_add(&seat->updated_caps_signal,
2240 &shseat->caps_changed_listener);
2241 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2242
Giulio Camuffo5085a752013-03-25 21:42:45 +01002243 return shseat;
2244}
2245
2246static struct shell_seat *
2247get_shell_seat(struct weston_seat *seat)
2248{
2249 struct wl_listener *listener;
2250
2251 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002252 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002253
2254 return container_of(listener,
2255 struct shell_seat, seat_destroy_listener);
2256}
2257
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002258static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002259fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002260{
2261 struct shell_surface *shsurf = data;
2262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002263 weston_surface_destroy(shsurf->view->surface);
2264 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002265}
2266
2267static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002268fade_out_done(struct weston_view_animation *animation, void *data)
2269{
2270 struct shell_surface *shsurf = data;
2271 struct wl_event_loop *loop;
2272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002273 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002275 if (weston_view_is_mapped(shsurf->view)) {
2276 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002277 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002278 }
2279}
2280
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002281struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002282get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002283{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002284 if (weston_surface_is_desktop_surface(surface)) {
2285 struct weston_desktop_surface *desktop_surface =
2286 weston_surface_get_desktop_surface(surface);
2287 return weston_desktop_surface_get_user_data(desktop_surface);
2288 }
2289 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002290}
2291
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002292/*
2293 * libweston-desktop
2294 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002295
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002296static void
2297desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2298 void *shell)
2299{
2300 struct weston_desktop_client *client =
2301 weston_desktop_surface_get_client(desktop_surface);
2302 struct wl_client *wl_client =
2303 weston_desktop_client_get_client(client);
2304 struct weston_view *view;
2305 struct shell_surface *shsurf;
2306 struct weston_surface *surface =
2307 weston_desktop_surface_get_surface(desktop_surface);
2308
2309 view = weston_desktop_surface_create_view(desktop_surface);
2310 if (!view)
2311 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002312
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002313 shsurf = calloc(1, sizeof *shsurf);
2314 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002315 if (wl_client)
2316 wl_client_post_no_memory(wl_client);
2317 else
2318 weston_log("no memory to allocate shell surface\n");
2319 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002320 }
2321
Pekka Paalanen8274d902014-08-06 19:36:51 +03002322 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002323
Tiago Vignattibc052c92012-04-19 16:18:18 +03002324 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002325 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002326 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002327 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002328 shsurf->desktop_surface = desktop_surface;
2329 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002331 wl_list_init(&shsurf->fullscreen.transform.link);
2332
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002333 shsurf->output = get_default_output(shsurf->shell->compositor);
2334
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002335 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002336
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337 /* empty when not in use */
2338 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002339 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002340
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002341 wl_list_init(&shsurf->workspace_transform.link);
2342
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002343 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2344 weston_desktop_surface_set_activated(desktop_surface,
2345 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002346}
2347
Tiago Vignattibc052c92012-04-19 16:18:18 +03002348static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002349desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2350 void *shell)
2351{
2352 struct shell_surface *shsurf =
2353 weston_desktop_surface_get_user_data(desktop_surface);
2354 struct weston_surface *surface =
2355 weston_desktop_surface_get_surface(desktop_surface);
2356
2357 if (!shsurf)
2358 return;
2359
2360 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2361
2362 if (shsurf->fullscreen.black_view)
2363 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2364
2365 weston_surface_set_label_func(surface, NULL);
2366 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2367 shsurf->desktop_surface = NULL;
2368
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002369 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002370 if (weston_surface_is_mapped(surface) &&
2371 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2372 pixman_region32_fini(&surface->pending.input);
2373 pixman_region32_init(&surface->pending.input);
2374 pixman_region32_fini(&surface->input);
2375 pixman_region32_init(&surface->input);
2376 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2377 fade_out_done, shsurf);
2378 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002379 weston_view_destroy(shsurf->view);
2380 free(shsurf);
2381 }
2382}
2383
2384static void
2385set_maximized_position(struct desktop_shell *shell,
2386 struct shell_surface *shsurf)
2387{
2388 pixman_rectangle32_t area;
2389 struct weston_geometry geometry;
2390
2391 get_output_work_area(shell, shsurf->output, &area);
2392 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2393
2394 weston_view_set_position(shsurf->view,
2395 area.x - geometry.x,
2396 area.y - geometry.y);
2397}
2398
2399static void
2400map(struct desktop_shell *shell, struct shell_surface *shsurf,
2401 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002402{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002403 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002404 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2405 struct weston_compositor *compositor = shell->compositor;
2406 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002407
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002408 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002409 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002410 center_on_output(shsurf->view, shsurf->fullscreen_output);
2411 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002412 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002413 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002414 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002415 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002416 }
2417
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002418 /* Surface stacking order, see also activate(). */
2419 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002420
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002421 weston_view_update_transform(shsurf->view);
2422 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002423 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002424 surface->output = shsurf->output;
2425 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002426 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002427
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002428 if (!shell->locked) {
2429 wl_list_for_each(seat, &compositor->seat_list, link)
2430 activate(shell, shsurf->view, seat,
2431 WESTON_ACTIVATE_FLAG_CONFIGURE);
2432 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002433
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002434 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435 switch (shell->win_animation_type) {
2436 case ANIMATION_FADE:
2437 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2438 break;
2439 case ANIMATION_ZOOM:
2440 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2441 break;
2442 case ANIMATION_NONE:
2443 default:
2444 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002445 }
2446 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002447}
2448
2449static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002450desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2451 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002452{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002453 struct shell_surface *shsurf =
2454 weston_desktop_surface_get_user_data(desktop_surface);
2455 struct weston_surface *surface =
2456 weston_desktop_surface_get_surface(desktop_surface);
2457 struct weston_view *view = shsurf->view;
2458 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002459 bool was_fullscreen;
2460 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002461
2462 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002463 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002464
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002465 was_fullscreen = shsurf->state.fullscreen;
2466 was_maximized = shsurf->state.maximized;
2467
2468 shsurf->state.fullscreen =
2469 weston_desktop_surface_get_fullscreen(desktop_surface);
2470 shsurf->state.maximized =
2471 weston_desktop_surface_get_maximized(desktop_surface);
2472
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002473 if (!weston_surface_is_mapped(surface)) {
2474 map(shell, shsurf, sx, sy);
2475 surface->is_mapped = true;
2476 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2477 ++surface->ref_count;
2478 return;
2479 }
2480
2481 if (sx == 0 && sy == 0 &&
2482 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002483 shsurf->last_height == surface->height &&
2484 was_fullscreen == shsurf->state.fullscreen &&
2485 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002486 return;
2487
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002488 if (was_fullscreen)
2489 unset_fullscreen(shsurf);
2490 if (was_maximized)
2491 unset_maximized(shsurf);
2492
2493 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002494 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002495 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002496 set_maximized_position(shell, shsurf);
2497 surface->output = shsurf->output;
2498 } else {
2499 float from_x, from_y;
2500 float to_x, to_y;
2501 float x, y;
2502
2503 if (shsurf->resize_edges) {
2504 sx = 0;
2505 sy = 0;
2506 }
2507
2508 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2509 sx = shsurf->last_width - surface->width;
2510 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2511 sy = shsurf->last_height - surface->height;
2512
2513 shsurf->last_width = surface->width;
2514 shsurf->last_height = surface->height;
2515
2516 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2517 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2518 x = shsurf->view->geometry.x + to_x - from_x;
2519 y = shsurf->view->geometry.y + to_y - from_y;
2520
2521 weston_view_set_position(shsurf->view, x, y);
2522 }
2523
2524 /* XXX: would a fullscreen surface need the same handling? */
2525 if (surface->output) {
2526 wl_list_for_each(view, &surface->views, surface_link)
2527 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002528 }
2529}
2530
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002531static void
2532set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2533 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002534{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002535 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2536 struct weston_surface *surface =
2537 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2538 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002539
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002540 if (fullscreen) {
2541 /* handle clients launching in fullscreen */
2542 if (output == NULL && !weston_surface_is_mapped(surface)) {
2543 /* Set the output to the one that has focus currently. */
2544 output = get_focused_output(surface->compositor);
2545 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002546
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002547 shell_surface_set_output(shsurf, output);
2548 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002549
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002550 width = shsurf->output->width;
2551 height = shsurf->output->height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 }
2553 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2554 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002555}
2556
2557static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002558desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2559 struct weston_seat *seat, uint32_t serial, void *shell)
2560{
2561 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2562 struct weston_touch *touch = weston_seat_get_touch(seat);
2563 struct shell_surface *shsurf =
2564 weston_desktop_surface_get_user_data(desktop_surface);
2565 struct weston_surface *surface =
2566 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2567 struct wl_resource *resource = surface->resource;
2568 struct weston_surface *focus;
2569
2570 if (pointer &&
2571 pointer->focus &&
2572 pointer->button_count > 0 &&
2573 pointer->grab_serial == serial) {
2574 focus = weston_surface_get_main_surface(pointer->focus->surface);
2575 if ((focus == surface) &&
2576 (surface_move(shsurf, pointer, true) < 0))
2577 wl_resource_post_no_memory(resource);
2578 } else if (touch &&
2579 touch->focus &&
2580 touch->grab_serial == serial) {
2581 focus = weston_surface_get_main_surface(touch->focus->surface);
2582 if ((focus == surface) &&
2583 (surface_touch_move(shsurf, touch) < 0))
2584 wl_resource_post_no_memory(resource);
2585 }
2586}
2587
2588static void
2589desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2590 struct weston_seat *seat, uint32_t serial,
2591 enum weston_desktop_surface_edge edges, void *shell)
2592{
2593 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2594 struct shell_surface *shsurf =
2595 weston_desktop_surface_get_user_data(desktop_surface);
2596 struct weston_surface *surface =
2597 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2598 struct wl_resource *resource = surface->resource;
2599 struct weston_surface *focus;
2600
2601 if (!pointer ||
2602 pointer->button_count == 0 ||
2603 pointer->grab_serial != serial ||
2604 pointer->focus == NULL)
2605 return;
2606
2607 focus = weston_surface_get_main_surface(pointer->focus->surface);
2608 if (focus != surface)
2609 return;
2610
2611 if (surface_resize(shsurf, pointer, edges) < 0)
2612 wl_resource_post_no_memory(resource);
2613}
2614
2615static void
2616desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2617 bool fullscreen,
2618 struct weston_output *output, void *shell)
2619{
2620 struct shell_surface *shsurf =
2621 weston_desktop_surface_get_user_data(desktop_surface);
2622
2623 set_fullscreen(shsurf, fullscreen, output);
2624}
2625
2626static void
2627set_maximized(struct shell_surface *shsurf, bool maximized)
2628{
2629 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2630 struct weston_surface *surface =
2631 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2632 int32_t width = 0, height = 0;
2633
2634 if (maximized) {
2635 struct weston_output *output;
2636 struct desktop_shell *shell;
2637 pixman_rectangle32_t area;
2638
2639 if (!weston_surface_is_mapped(surface))
2640 output = get_focused_output(surface->compositor);
2641 else
2642 output = surface->output;
2643
2644 shell_surface_set_output(shsurf, output);
2645
2646 shell = shell_surface_get_shell(shsurf);
2647 get_output_work_area(shell, shsurf->output, &area);
2648
2649 width = area.width;
2650 height = area.height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002651 }
2652 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2653 weston_desktop_surface_set_size(desktop_surface, width, height);
2654}
2655
2656static void
2657desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2658 bool maximized, void *shell)
2659{
2660 struct shell_surface *shsurf =
2661 weston_desktop_surface_get_user_data(desktop_surface);
2662
2663 set_maximized(shsurf, maximized);
2664}
2665
2666static void
2667desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2668 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002669{
2670 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002671 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002672
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002673 /* apply compositor's own minimization logic (hide) */
2674 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002675}
2676
Rafael Antognollie2a34552013-12-03 15:35:45 -02002677static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002678set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002679{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002680 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002681
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002682 if (pointer->grab->interface == &busy_cursor_grab_interface)
2683 return;
2684
2685 grab = malloc(sizeof *grab);
2686 if (!grab)
2687 return;
2688
2689 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2690 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2691 /* Mark the shsurf as ungrabbed so that button binding is able
2692 * to move it. */
2693 shsurf->grabbed = 0;
2694}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002695
2696static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002697end_busy_cursor(struct weston_compositor *compositor,
2698 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002699{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002700 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002701 struct shell_grab *grab;
2702 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002703
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002704 wl_list_for_each(seat, &compositor->seat_list, link) {
2705 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2706 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002707
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002708 if (!pointer)
2709 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002710
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711 if (pointer->grab->interface != &busy_cursor_grab_interface)
2712 continue;
2713
2714 grab = (struct shell_grab *) pointer->grab;
2715 shsurf = grab->shsurf;
2716 if (!shsurf)
2717 continue;
2718
2719 grab_client =
2720 weston_desktop_surface_get_client(shsurf->desktop_surface);
2721 if (grab_client == desktop_client) {
2722 shell_grab_end(grab);
2723 free(grab);
2724 }
2725 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002726}
2727
2728static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002729desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2730 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002731{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002732 struct shell_surface *shsurf =
2733 weston_desktop_surface_get_user_data(desktop_surface);
2734 bool *unresponsive = user_data;
2735
2736 shsurf->unresponsive = *unresponsive;
2737}
2738
2739static void
2740desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2741 void *shell_)
2742{
2743 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002744 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002745 struct weston_seat *seat;
2746 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002747
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002748 weston_desktop_client_for_each_surface(desktop_client,
2749 desktop_surface_set_unresponsive,
2750 &unresponsive);
2751
2752
2753 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2754 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2755 struct weston_desktop_client *grab_client;
2756
2757 if (!pointer || !pointer->focus)
2758 continue;
2759
2760 shsurf = get_shell_surface(pointer->focus->surface);
2761 if (!shsurf)
2762 continue;
2763
2764 grab_client =
2765 weston_desktop_surface_get_client(shsurf->desktop_surface);
2766 if (grab_client == desktop_client)
2767 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002768 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002769}
2770
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002771static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002772desktop_surface_pong(struct weston_desktop_client *desktop_client,
2773 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002774{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002775 struct desktop_shell *shell = shell_;
2776 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002777
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002778 weston_desktop_client_for_each_surface(desktop_client,
2779 desktop_surface_set_unresponsive,
2780 &unresponsive);
2781 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002782}
2783
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002784static const struct weston_desktop_api shell_desktop_api = {
2785 .struct_size = sizeof(struct weston_desktop_api),
2786 .surface_added = desktop_surface_added,
2787 .surface_removed = desktop_surface_removed,
2788 .committed = desktop_surface_committed,
2789 .move = desktop_surface_move,
2790 .resize = desktop_surface_resize,
2791 .fullscreen_requested = desktop_surface_fullscreen_requested,
2792 .maximized_requested = desktop_surface_maximized_requested,
2793 .minimized_requested = desktop_surface_minimized_requested,
2794 .ping_timeout = desktop_surface_ping_timeout,
2795 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002796};
2797
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798/* ************************ *
2799 * end of libweston-desktop *
2800 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002801static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07002802shell_fade(struct desktop_shell *shell, enum fade_type type);
2803
2804static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002805configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002806{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002807 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002808
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002809 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 if (v->output == ev->output && v != ev) {
2811 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002812 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002813 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002814 }
2815 }
2816
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002817 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002818 ev->surface->is_mapped = true;
2819 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002820
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002821 if (wl_list_empty(&ev->layer_link.link)) {
2822 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002824 }
2825}
2826
David Fort50d962f2016-06-09 17:55:52 +02002827
2828static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002829find_shell_output_from_weston_output(struct desktop_shell *shell,
2830 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002831{
2832 struct shell_output *shell_output;
2833
2834 wl_list_for_each(shell_output, &shell->output_list, link) {
2835 if (shell_output->output == output)
2836 return shell_output;
2837 }
2838
2839 return NULL;
2840}
2841
Pekka Paalanen8274d902014-08-06 19:36:51 +03002842static int
2843background_get_label(struct weston_surface *surface, char *buf, size_t len)
2844{
2845 return snprintf(buf, len, "background for output %s",
2846 surface->output->name);
2847}
2848
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002849static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002850background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002851{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002852 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002854
Jason Ekstranda7af7042013-10-12 22:38:11 -05002855 view = container_of(es->views.next, struct weston_view, surface_link);
2856
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002857 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002858}
2859
2860static void
David Fort50d962f2016-06-09 17:55:52 +02002861handle_background_surface_destroy(struct wl_listener *listener, void *data)
2862{
2863 struct shell_output *output =
2864 container_of(listener, struct shell_output, background_surface_listener);
2865
2866 weston_log("background surface gone\n");
2867 output->background_surface = NULL;
2868}
2869
2870static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002871desktop_shell_set_background(struct wl_client *client,
2872 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002873 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002874 struct wl_resource *surface_resource)
2875{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002876 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002877 struct weston_surface *surface =
2878 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002879 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002880 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002881
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002882 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002883 wl_resource_post_error(surface_resource,
2884 WL_DISPLAY_ERROR_INVALID_OBJECT,
2885 "surface role already assigned");
2886 return;
2887 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002888
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2890 weston_view_destroy(view);
2891 view = weston_view_create(surface);
2892
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002893 surface->committed = background_committed;
2894 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002895 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002896 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002897 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002898 weston_desktop_shell_send_configure(resource, 0,
2899 surface_resource,
2900 surface->output->width,
2901 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002902
2903 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2904 sh_output->background_surface = surface;
2905
2906 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2907 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002908}
2909
Pekka Paalanen8274d902014-08-06 19:36:51 +03002910static int
2911panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2912{
2913 return snprintf(buf, len, "panel for output %s",
2914 surface->output->name);
2915}
2916
Kristian Høgsberg75840622011-09-06 13:48:16 -04002917static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002918panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002919{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002920 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002921 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002922
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 view = container_of(es->views.next, struct weston_view, surface_link);
2924
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002925 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002926}
2927
2928static void
David Fort50d962f2016-06-09 17:55:52 +02002929handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2930{
2931 struct shell_output *output =
2932 container_of(listener, struct shell_output, panel_surface_listener);
2933
2934 weston_log("panel surface gone\n");
2935 output->panel_surface = NULL;
2936}
2937
2938
2939static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002940desktop_shell_set_panel(struct wl_client *client,
2941 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002942 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002943 struct wl_resource *surface_resource)
2944{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002945 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002946 struct weston_surface *surface =
2947 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002948 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02002949 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002950
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002951 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002952 wl_resource_post_error(surface_resource,
2953 WL_DISPLAY_ERROR_INVALID_OBJECT,
2954 "surface role already assigned");
2955 return;
2956 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002957
Jason Ekstranda7af7042013-10-12 22:38:11 -05002958 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2959 weston_view_destroy(view);
2960 view = weston_view_create(surface);
2961
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002962 surface->committed = panel_committed;
2963 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002964 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002965 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002966 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002967 weston_desktop_shell_send_configure(resource, 0,
2968 surface_resource,
2969 surface->output->width,
2970 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002971
2972 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2973 sh_output->panel_surface = surface;
2974
2975 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
2976 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002977}
2978
Pekka Paalanen8274d902014-08-06 19:36:51 +03002979static int
2980lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2981{
2982 return snprintf(buf, len, "lock window");
2983}
2984
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002985static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002986lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002987{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002988 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002989 struct weston_view *view;
2990
2991 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002992
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002993 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01002994 return;
2995
Jason Ekstranda7af7042013-10-12 22:38:11 -05002996 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002997
2998 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002999 weston_layer_entry_insert(&shell->lock_layer.view_list,
3000 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003001 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003002 surface->is_mapped = true;
3003 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003004 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003005 }
3006}
3007
3008static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003009handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003010{
Tiago Vignattibe143262012-04-16 17:31:41 +03003011 struct desktop_shell *shell =
3012 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003013
Martin Minarik6d118362012-06-07 18:01:59 +02003014 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003015 shell->lock_surface = NULL;
3016}
3017
3018static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003019desktop_shell_set_lock_surface(struct wl_client *client,
3020 struct wl_resource *resource,
3021 struct wl_resource *surface_resource)
3022{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003023 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003024 struct weston_surface *surface =
3025 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003026
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003027 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003028
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003029 if (!shell->locked)
3030 return;
3031
Pekka Paalanen98262232011-12-01 10:42:22 +02003032 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003033
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003034 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003035 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003036 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003037
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003038 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003039 surface->committed = lock_surface_committed;
3040 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003041 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003042}
3043
3044static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003045resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003046{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003047 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003048
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003049 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003050 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003051 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003052 &shell->input_panel_layer.link);
3053 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003054 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003055 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003056 wl_list_insert(&shell->compositor->cursor_layer.link,
3057 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003058 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003059 wl_list_insert(&shell->fullscreen_layer.link,
3060 &shell->panel_layer.link);
3061 wl_list_insert(&shell->panel_layer.link,
3062 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003063
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003064 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003065
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003066 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003067 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003068 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003069}
3070
3071static void
3072desktop_shell_unlock(struct wl_client *client,
3073 struct wl_resource *resource)
3074{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003075 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003076
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003077 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003078
3079 if (shell->locked)
3080 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003081}
3082
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003083static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003084desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003085 struct wl_resource *resource,
3086 struct wl_resource *surface_resource)
3087{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003088 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003089
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003090 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003091 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003092}
3093
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003094static void
3095desktop_shell_desktop_ready(struct wl_client *client,
3096 struct wl_resource *resource)
3097{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003098 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003099
3100 shell_fade_startup(shell);
3101}
3102
Jonny Lamb765760d2014-08-20 15:53:19 +02003103static void
3104desktop_shell_set_panel_position(struct wl_client *client,
3105 struct wl_resource *resource,
3106 uint32_t position)
3107{
3108 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3109
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003110 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3111 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3112 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3113 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003114 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003115 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003116 "bad position argument");
3117 return;
3118 }
3119
3120 shell->panel_position = position;
3121}
3122
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003123static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003124 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003125 desktop_shell_set_panel,
3126 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003127 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003128 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003129 desktop_shell_desktop_ready,
3130 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003131};
3132
3133static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003134move_binding(struct weston_pointer *pointer, uint32_t time,
3135 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003136{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003137 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003138 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003139 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003140
Derek Foreman8ae2db52015-07-15 13:00:36 -05003141 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003142 return;
3143
Derek Foreman8ae2db52015-07-15 13:00:36 -05003144 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003145
Pekka Paalanen01388e22013-04-25 13:57:44 +03003146 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003147 if (surface == NULL)
3148 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003149
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003150 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003151 if (shsurf == NULL ||
3152 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3153 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003154 return;
3155
Derek Foreman794fa0e2015-07-15 13:00:38 -05003156 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003157}
3158
3159static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003160maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3161 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003162{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003163 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003164 struct weston_surface *surface;
3165 struct shell_surface *shsurf;
3166
3167 surface = weston_surface_get_main_surface(focus);
3168 if (surface == NULL)
3169 return;
3170
3171 shsurf = get_shell_surface(surface);
3172 if (shsurf == NULL)
3173 return;
3174
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003175 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003176}
3177
3178static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003179fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3180 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003181{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003182 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003183 struct weston_surface *surface;
3184 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003185 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003186
3187 surface = weston_surface_get_main_surface(focus);
3188 if (surface == NULL)
3189 return;
3190
3191 shsurf = get_shell_surface(surface);
3192 if (shsurf == NULL)
3193 return;
3194
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003195 fullscreen =
3196 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003197
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003198 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003199}
3200
3201static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003202touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003203{
Derek Foreman362656b2014-09-04 10:23:05 -05003204 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003205 struct weston_surface *surface;
3206 struct shell_surface *shsurf;
3207
Derek Foreman8ae2db52015-07-15 13:00:36 -05003208 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003209 return;
3210
Derek Foreman8ae2db52015-07-15 13:00:36 -05003211 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003212 surface = weston_surface_get_main_surface(focus);
3213 if (surface == NULL)
3214 return;
3215
3216 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003217 if (shsurf == NULL ||
3218 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3219 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003220 return;
3221
Derek Foremanb7674ae2015-07-15 13:00:37 -05003222 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003223}
3224
3225static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003226resize_binding(struct weston_pointer *pointer, uint32_t time,
3227 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003228{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003229 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003230 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003231 uint32_t edges = 0;
3232 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003233 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003234
Derek Foreman8ae2db52015-07-15 13:00:36 -05003235 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003236 return;
3237
Derek Foreman8ae2db52015-07-15 13:00:36 -05003238 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003239
Pekka Paalanen01388e22013-04-25 13:57:44 +03003240 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003241 if (surface == NULL)
3242 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003243
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003244 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003245 if (shsurf == NULL ||
3246 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3247 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003248 return;
3249
Jason Ekstranda7af7042013-10-12 22:38:11 -05003250 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003251 wl_fixed_to_int(pointer->grab_x),
3252 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003253 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003254
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003255 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003256 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003257 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003258 edges |= 0;
3259 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003260 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003261
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003262 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003264 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003265 edges |= 0;
3266 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003267 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003268
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003269 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003270}
3271
3272static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003273surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003274 struct weston_pointer_axis_event *event,
3275 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003276{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003277 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003278 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003279 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003280 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003281
Pekka Paalanen01388e22013-04-25 13:57:44 +03003282 /* XXX: broken for windows containing sub-surfaces */
3283 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003284 if (surface == NULL)
3285 return;
3286
3287 shsurf = get_shell_surface(surface);
3288 if (!shsurf)
3289 return;
3290
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003291 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003292
Jason Ekstranda7af7042013-10-12 22:38:11 -05003293 if (shsurf->view->alpha > 1.0)
3294 shsurf->view->alpha = 1.0;
3295 if (shsurf->view->alpha < step)
3296 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003297
Jason Ekstranda7af7042013-10-12 22:38:11 -05003298 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003299 weston_surface_damage(surface);
3300}
3301
3302static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003303do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003304 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003305{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003306 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003307 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003308 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003309 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003310
Derek Foreman1281a362015-07-31 16:55:32 -05003311 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003312 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3313 return;
3314 }
3315
Scott Moreauccbf29d2012-02-22 14:21:41 -07003316 wl_list_for_each(output, &compositor->output_list, link) {
3317 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003318 wl_fixed_to_double(pointer->x),
3319 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003320 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003321 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003322 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003323 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003324 increment = -output->zoom.increment;
3325 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003326 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003327 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003328 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003329 else
3330 increment = 0;
3331
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003332 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003333
Scott Moreaue6603982012-06-11 13:07:51 -06003334 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003335 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003336 else if (output->zoom.level > output->zoom.max_level)
3337 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003338
3339 if (!output->zoom.active) {
3340 if (output->zoom.level <= 0.0)
3341 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003342 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003343 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003344
Scott Moreaue6603982012-06-11 13:07:51 -06003345 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003346
Jason Ekstranda7af7042013-10-12 22:38:11 -05003347 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003348 }
3349 }
3350}
3351
Scott Moreauccbf29d2012-02-22 14:21:41 -07003352static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003353zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003354 struct weston_pointer_axis_event *event,
3355 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003356{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003357 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003358}
3359
3360static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003361zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3362 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003363{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003364 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003365}
3366
3367static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003368terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3369 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003371 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003372
Daniel Stone325fc2d2012-05-30 16:31:58 +01003373 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003374}
3375
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003376static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003377rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003378 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003379{
3380 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003381 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003382 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003383 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003384 struct weston_surface *surface =
3385 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003386 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003387
Jonas Ådahld2510102014-10-05 21:39:14 +02003388 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003389
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003390 if (!shsurf)
3391 return;
3392
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003393 cx = 0.5f * surface->width;
3394 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003395
Daniel Stone37816df2012-05-16 18:45:18 +01003396 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3397 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003398 r = sqrtf(dx * dx + dy * dy);
3399
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003400 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003401 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003402
3403 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003404 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003405 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003406
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003407 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003408 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003409
3410 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003411 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003412 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003413 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003414 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003415
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003416 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003417 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003418 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003419 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003420 wl_list_init(&shsurf->rotation.transform.link);
3421 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003422 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003423 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003424
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003425 /* We need to adjust the position of the surface
3426 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003427 cposx = shsurf->view->geometry.x + cx;
3428 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003429 dposx = rotate->center.x - cposx;
3430 dposy = rotate->center.y - cposy;
3431 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003432 weston_view_set_position(shsurf->view,
3433 shsurf->view->geometry.x + dposx,
3434 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003435 }
3436
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003437 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003438 * lazily applies the damage due to rotation update.
3439 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003440 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003441}
3442
3443static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003444rotate_grab_button(struct weston_pointer_grab *grab,
3445 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003446{
3447 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003448 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003449 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003450 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003451 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003452
Daniel Stone4dbadb12012-05-30 16:31:51 +01003453 if (pointer->button_count == 0 &&
3454 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003455 if (shsurf)
3456 weston_matrix_multiply(&shsurf->rotation.rotation,
3457 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003458 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003459 free(rotate);
3460 }
3461}
3462
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003463static void
3464rotate_grab_cancel(struct weston_pointer_grab *grab)
3465{
3466 struct rotate_grab *rotate =
3467 container_of(grab, struct rotate_grab, base.grab);
3468
3469 shell_grab_end(&rotate->base);
3470 free(rotate);
3471}
3472
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003473static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003474 noop_grab_focus,
3475 rotate_grab_motion,
3476 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003477 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003478 noop_grab_axis_source,
3479 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003480 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003481};
3482
3483static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003484surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003485{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003486 struct weston_surface *surface =
3487 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003488 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003489 float dx, dy;
3490 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003491
Pekka Paalanen460099f2012-01-20 16:48:25 +02003492 rotate = malloc(sizeof *rotate);
3493 if (!rotate)
3494 return;
3495
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003496 weston_view_to_global_float(shsurf->view,
3497 surface->width * 0.5f,
3498 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003499 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003500
Derek Foreman74de4692015-07-15 13:00:39 -05003501 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3502 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003503 r = sqrtf(dx * dx + dy * dy);
3504 if (r > 20.0f) {
3505 struct weston_matrix inverse;
3506
3507 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003508 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003509 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003510
3511 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003512 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003513 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003514 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003515 weston_matrix_init(&rotate->rotation);
3516 }
3517
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003518 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003519 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003520}
3521
3522static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003523rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003524 void *data)
3525{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003526 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003527 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003528 struct shell_surface *surface;
3529
Derek Foreman8ae2db52015-07-15 13:00:36 -05003530 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003531 return;
3532
Derek Foreman8ae2db52015-07-15 13:00:36 -05003533 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003534
Pekka Paalanen01388e22013-04-25 13:57:44 +03003535 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003536 if (base_surface == NULL)
3537 return;
3538
3539 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003540 if (surface == NULL ||
3541 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3542 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003543 return;
3544
Derek Foreman74de4692015-07-15 13:00:39 -05003545 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003546}
3547
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003548/* Move all fullscreen layers down to the current workspace and hide their
3549 * black views. The surfaces' state is set to both fullscreen and lowered,
3550 * and this is reversed when such a surface is re-configured, see
3551 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3552 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003553 * lowering_output = NULL - Lower on all outputs, else only lower on the
3554 * specified output.
3555 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003556 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003557 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003558void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003559lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003560 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003561{
3562 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003563 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003564
3565 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003566 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003567 &shell->fullscreen_layer.view_list.link,
3568 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003569 struct shell_surface *shsurf = get_shell_surface(view->surface);
3570
3571 if (!shsurf)
3572 continue;
3573
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003574 /* Only lower surfaces which have lowering_output as their fullscreen
3575 * output, unless a NULL output asks for lowering on all outputs.
3576 */
3577 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3578 continue;
3579
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003580 /* We can have a non-fullscreen popup for a fullscreen surface
3581 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003582 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003583 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003584 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3585 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003586 weston_view_damage_below(shsurf->fullscreen.black_view);
3587
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003588 }
3589
3590 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003591 weston_layer_entry_remove(&view->layer_link);
3592 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003593 weston_view_damage_below(view);
3594 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003595
3596 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003597 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003598}
3599
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003600void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003601activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003602 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003603{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003604 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003605 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003606 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003607 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003608 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003609 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003610
Pekka Paalanen01388e22013-04-25 13:57:44 +03003611 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003612 shsurf = get_shell_surface(main_surface);
3613 assert(shsurf);
3614
3615 /* Only demote fullscreen surfaces on the output of activated shsurf.
3616 * Leave fullscreen surfaces on unrelated outputs alone. */
3617 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003618
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003619 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003620
Jonas Ådahl8538b222012-08-29 22:13:03 +02003621 state = ensure_focus_state(shell, seat);
3622 if (state == NULL)
3623 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003624
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003625 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003626 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003627
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003628 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3629 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003630 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003631
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003632 /* Update the surface’s layer. This brings it to the top of the stacking
3633 * order as appropriate. */
3634 shell_surface_update_layer(shsurf);
3635
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003636 if (shell->focus_animation_type != ANIMATION_NONE) {
3637 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003638 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003639 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003640}
3641
Alex Wu21858432012-04-01 20:13:08 +08003642/* no-op func for checking black surface */
3643static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003644black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003645{
3646}
3647
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003648static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003649is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003650{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003651 struct weston_surface *surface = view->surface;
3652
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003653 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003654 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003655 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003656 return true;
3657 }
3658 return false;
3659}
3660
Kristian Høgsberg75840622011-09-06 13:48:16 -04003661static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003662activate_binding(struct weston_seat *seat,
3663 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003664 struct weston_view *focus_view,
3665 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003666{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003667 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003668 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003669
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003670 if (!focus_view)
3671 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003672
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003673 if (is_black_surface_view(focus_view, &main_view))
3674 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003675
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003676 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003677 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003678 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003679
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003680 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003681}
3682
3683static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003684click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3685 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003686{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003687 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003688 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003689 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003690 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003691
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003692 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003693 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003694 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003695}
3696
3697static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003698touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3699 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003700{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003701 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003702 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003703 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003704 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003705
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003706 activate_binding(touch->seat, data, touch->focus,
3707 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003708}
3709
3710static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003711unfocus_all_seats(struct desktop_shell *shell)
3712{
3713 struct weston_seat *seat, *next;
3714
3715 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003716 struct weston_keyboard *keyboard =
3717 weston_seat_get_keyboard(seat);
3718
3719 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003720 continue;
3721
Derek Foreman1281a362015-07-31 16:55:32 -05003722 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003723 }
3724}
3725
3726static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003727lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003728{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003729 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003730
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003731 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003732 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003733 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003734 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003735
3736 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003737
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003738 /* Hide all surfaces by removing the fullscreen, panel and
3739 * toplevel layers. This way nothing else can show or receive
3740 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003741
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003742 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003743 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003744 if (shell->showing_input_panels)
3745 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003746 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003747 wl_list_insert(&shell->compositor->cursor_layer.link,
3748 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003749
Derek Foreman004b4a12015-07-20 16:28:13 -05003750 weston_compositor_sleep(shell->compositor);
3751
Neil Robertsb4a91702014-04-09 16:33:32 +01003752 /* Remove the keyboard focus on all seats. This will be
3753 * restored to the workspace's saved state via
3754 * restore_focus_state when the compositor is unlocked */
3755 unfocus_all_seats(shell);
3756
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003757 /* TODO: disable bindings that should not work while locked. */
3758
3759 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003760}
3761
3762static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003763unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003764{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003765 struct wl_resource *shell_resource;
3766
Pekka Paalanend81c2162011-11-16 13:47:34 +02003767 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003768 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003769 return;
3770 }
3771
3772 /* If desktop-shell client has gone away, unlock immediately. */
3773 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003774 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003775 return;
3776 }
3777
3778 if (shell->prepare_event_sent)
3779 return;
3780
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003781 shell_resource = shell->child.desktop_shell;
3782 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003783 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003784}
3785
3786static void
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003787shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003788{
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003789 struct desktop_shell *shell = data;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003790
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003791 shell->fade.animation = NULL;
3792
3793 switch (shell->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003794 case FADE_IN:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003795 weston_surface_destroy(shell->fade.view->surface);
3796 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003797 break;
3798 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003799 lock(shell);
3800 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003801 default:
3802 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003803 }
3804}
3805
Jason Ekstranda7af7042013-10-12 22:38:11 -05003806static struct weston_view *
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003807shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003808{
3809 struct weston_compositor *compositor = shell->compositor;
3810 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003811 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003812
3813 surface = weston_surface_create(compositor);
3814 if (!surface)
3815 return NULL;
3816
Jason Ekstranda7af7042013-10-12 22:38:11 -05003817 view = weston_view_create(surface);
3818 if (!view) {
3819 weston_surface_destroy(surface);
3820 return NULL;
3821 }
3822
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003823 weston_surface_set_size(surface, 8192, 8192);
3824 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003825 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003826 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3827 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003828 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003829 surface->is_mapped = true;
3830 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003831
Jason Ekstranda7af7042013-10-12 22:38:11 -05003832 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003833}
3834
3835static void
3836shell_fade(struct desktop_shell *shell, enum fade_type type)
3837{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003838 float tint;
3839
3840 switch (type) {
3841 case FADE_IN:
3842 tint = 0.0;
3843 break;
3844 case FADE_OUT:
3845 tint = 1.0;
3846 break;
3847 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003848 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003849 return;
3850 }
3851
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003852 shell->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003853
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003854 if (shell->fade.view == NULL) {
3855 shell->fade.view = shell_fade_create_surface(shell);
3856 if (!shell->fade.view)
3857 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003858
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003859 shell->fade.view->alpha = 1.0 - tint;
3860 weston_view_update_transform(shell->fade.view);
3861 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003862
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003863 if (shell->fade.view->output == NULL) {
3864 /* If the black view gets a NULL output, we lost the
3865 * last output and we'll just cancel the fade. This
3866 * happens when you close the last window under the
3867 * X11 or Wayland backends. */
3868 shell->locked = false;
3869 weston_surface_destroy(shell->fade.view->surface);
3870 shell->fade.view = NULL;
3871 } else if (shell->fade.animation) {
3872 weston_fade_update(shell->fade.animation, tint);
3873 } else {
3874 shell->fade.animation =
3875 weston_fade_run(shell->fade.view,
3876 1.0 - tint, tint, 300.0,
3877 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003878 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003879}
3880
3881static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003882do_shell_fade_startup(void *data)
3883{
3884 struct desktop_shell *shell = data;
3885
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003886 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003887 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003888 } else {
3889 weston_log("desktop shell: "
3890 "unexpected fade-in animation type %d\n",
3891 shell->startup_animation_type);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003892 weston_surface_destroy(shell->fade.view->surface);
3893 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003894 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003895}
3896
3897static void
3898shell_fade_startup(struct desktop_shell *shell)
3899{
3900 struct wl_event_loop *loop;
3901
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003902 if (!shell->fade.startup_timer)
3903 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003904
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003905 wl_event_source_remove(shell->fade.startup_timer);
3906 shell->fade.startup_timer = NULL;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003907
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003908 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3909 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003910}
3911
3912static int
3913fade_startup_timeout(void *data)
3914{
3915 struct desktop_shell *shell = data;
3916
3917 shell_fade_startup(shell);
3918 return 0;
3919}
3920
3921static void
3922shell_fade_init(struct desktop_shell *shell)
3923{
3924 /* Make compositor output all black, and wait for the desktop-shell
3925 * client to signal it is ready, then fade in. The timer triggers a
3926 * fade-in, in case the desktop-shell client takes too long.
3927 */
3928
3929 struct wl_event_loop *loop;
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003930
3931 if (shell->fade.view != NULL) {
3932 weston_log("%s: warning: fade surface already exists\n",
3933 __func__);
3934 return;
3935 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003936
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003937 if (shell->startup_animation_type == ANIMATION_NONE)
3938 return;
3939
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003940 shell->fade.view = shell_fade_create_surface(shell);
3941 if (!shell->fade.view)
3942 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003943
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003944 weston_view_update_transform(shell->fade.view);
3945 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003946
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003947 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3948 shell->fade.startup_timer =
3949 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3950 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003951}
3952
3953static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003954idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003955{
3956 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003957 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003958
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003959 struct weston_seat *seat;
3960
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003961 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3962 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003963
3964 shell_fade(shell, FADE_OUT);
Bryce Harringtonb3197f42016-08-30 12:05:27 -07003965 /* lock() is called from shell_fade_done() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003966}
3967
3968static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003969wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003970{
3971 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003972 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003973
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003974 unlock(shell);
3975}
3976
3977static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003978transform_handler(struct wl_listener *listener, void *data)
3979{
3980 struct weston_surface *surface = data;
3981 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003982 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003983 int x, y;
3984
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003985 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003986 return;
3987
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003988 api = shsurf->shell->xwayland_surface_api;
3989 if (!api) {
3990 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
3991 shsurf->shell->xwayland_surface_api = api;
3992 }
3993
3994 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003995 return;
3996
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003997 if (!weston_view_is_mapped(shsurf->view))
3998 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003999
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004000 x = shsurf->view->geometry.x;
4001 y = shsurf->view->geometry.y;
4002
4003 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004004}
4005
4006static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004007center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004008{
Giulio Camuffob8366642013-04-25 13:57:46 +03004009 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004010 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004011
Jason Ekstranda7af7042013-10-12 22:38:11 -05004012 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004013
4014 x = output->x + (output->width - width) / 2 - surf_x / 2;
4015 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004016
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004017 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004018}
4019
4020static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021weston_view_set_initial_position(struct weston_view *view,
4022 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004023{
4024 struct weston_compositor *compositor = shell->compositor;
4025 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004026 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004027 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004028 struct weston_output *output, *target_output = NULL;
4029 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004030 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004031
4032 /* As a heuristic place the new window on the same output as the
4033 * pointer. Falling back to the output containing 0, 0.
4034 *
4035 * TODO: Do something clever for touch too?
4036 */
4037 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004038 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4039
4040 if (pointer) {
4041 ix = wl_fixed_to_int(pointer->x);
4042 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004043 break;
4044 }
4045 }
4046
4047 wl_list_for_each(output, &compositor->output_list, link) {
4048 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4049 target_output = output;
4050 break;
4051 }
4052 }
4053
4054 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004055 weston_view_set_position(view, 10 + random() % 400,
4056 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004057 return;
4058 }
4059
4060 /* Valid range within output where the surface will still be onscreen.
4061 * If this is negative it means that the surface is bigger than
4062 * output.
4063 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004064 get_output_work_area(shell, target_output, &area);
4065
Derek Foremanf814c5d2015-04-15 12:23:54 -05004066 x = area.x;
4067 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004068 range_x = area.width - view->surface->width;
4069 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004070
Rob Bradford4cb88c72012-08-13 15:18:44 +01004071 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004072 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004073
4074 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004075 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004076
Jason Ekstranda7af7042013-10-12 22:38:11 -05004077 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004078}
4079
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004080static bool
4081check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4082{
4083 struct timespec now;
4084
4085 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4086 return false;
4087
4088 /*
4089 * If the shell helper client dies before the session has been
4090 * up for roughly 30 seconds, better just make Weston shut down,
4091 * because the user likely has no way to interact with the desktop
4092 * anyway.
4093 */
4094 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4095 weston_log("Error: %s apparently cannot run at all.\n",
4096 shell->client);
4097 weston_log_continue(STAMP_SPACE "Quitting...");
4098 wl_display_terminate(shell->compositor->wl_display);
4099
4100 return true;
4101 }
4102
4103 return false;
4104}
4105
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004106static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004107
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004108static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004109respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004110{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004111 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004112
4113 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4114 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004115 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004116 shell->child.deathstamp = time;
4117 shell->child.deathcount = 0;
4118 }
4119
4120 shell->child.deathcount++;
4121 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004122 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004123 return;
4124 }
4125
Pekka Paalanen826dc142014-08-27 12:11:53 +03004126 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004127 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004128}
4129
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004130static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004131desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4132{
4133 struct desktop_shell *shell;
4134
4135 shell = container_of(listener, struct desktop_shell,
4136 child.client_destroy_listener);
4137
Pekka Paalanen826dc142014-08-27 12:11:53 +03004138 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004139 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004140 /*
4141 * unbind_desktop_shell() will reset shell->child.desktop_shell
4142 * before the respawned process has a chance to create a new
4143 * desktop_shell object, because we are being called from the
4144 * wl_client destructor which destroys all wl_resources before
4145 * returning.
4146 */
4147
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004148 if (!check_desktop_shell_crash_too_early(shell))
4149 respawn_desktop_shell_process(shell);
4150
Pekka Paalanen826dc142014-08-27 12:11:53 +03004151 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004152}
4153
4154static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004155launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004156{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004157 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004158
Pekka Paalanen826dc142014-08-27 12:11:53 +03004159 shell->child.client = weston_client_start(shell->compositor,
4160 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004161
Arnaud Vrac42631192014-08-25 20:56:46 +02004162 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004163 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004164 return;
4165 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004166
4167 shell->child.client_destroy_listener.notify =
4168 desktop_shell_client_destroy;
4169 wl_client_add_destroy_listener(shell->child.client,
4170 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004171}
4172
4173static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004174unbind_desktop_shell(struct wl_resource *resource)
4175{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004176 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004177
4178 if (shell->locked)
4179 resume_desktop(shell);
4180
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004181 shell->child.desktop_shell = NULL;
4182 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004183}
4184
4185static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004186bind_desktop_shell(struct wl_client *client,
4187 void *data, uint32_t version, uint32_t id)
4188{
Tiago Vignattibe143262012-04-16 17:31:41 +03004189 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004190 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004191
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004192 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4193 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004194
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004195 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004196 wl_resource_set_implementation(resource,
4197 &desktop_shell_implementation,
4198 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004199 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004200 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004201 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004202
4203 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4204 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004205}
4206
Kristian Høgsberg07045392012-02-19 18:52:44 -05004207struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004208 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004209 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004210 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004211 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004212 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004213};
4214
4215static void
4216switcher_next(struct switcher *switcher)
4217{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004218 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004219 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004220 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004221 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004222
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004223 /* temporary re-display minimized surfaces */
4224 struct weston_view *tmp;
4225 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004226 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4227 weston_layer_entry_remove(&view->layer_link);
4228 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004229 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4230 *minimized = view;
4231 }
4232
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004233 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004234 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004235 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004236 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004237 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004238 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004239 next = view;
4240 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241 view->alpha = 0.25;
4242 weston_view_geometry_dirty(view);
4243 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004244 }
Alex Wu1659daa2012-04-01 20:13:09 +08004245
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004246 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247 view->alpha = 0.25;
4248 weston_view_geometry_dirty(view);
4249 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004250 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004251 }
4252
4253 if (next == NULL)
4254 next = first;
4255
Alex Wu07b26062012-03-12 16:06:01 +08004256 if (next == NULL)
4257 return;
4258
Kristian Høgsberg07045392012-02-19 18:52:44 -05004259 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004260 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004261
4262 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004263 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004264 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004265
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004266 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004267 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004269}
4270
4271static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004272switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004273{
4274 struct switcher *switcher =
4275 container_of(listener, struct switcher, listener);
4276
4277 switcher_next(switcher);
4278}
4279
4280static void
Daniel Stone351eb612012-05-31 15:27:47 -04004281switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004282{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004283 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004284 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004285 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004286
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004287 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004288 if (is_focus_view(view))
4289 continue;
4290
Jason Ekstranda7af7042013-10-12 22:38:11 -05004291 view->alpha = 1.0;
4292 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004293 }
4294
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004295 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004296 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004297 keyboard->seat,
4298 WESTON_ACTIVATE_FLAG_CONFIGURE);
4299 }
4300
Kristian Høgsberg07045392012-02-19 18:52:44 -05004301 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004302 weston_keyboard_end_grab(keyboard);
4303 if (keyboard->input_method_resource)
4304 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004305
4306 /* re-hide surfaces that were temporary shown during the switch */
4307 struct weston_view **minimized;
4308 wl_array_for_each(minimized, &switcher->minimized_array) {
4309 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004310 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004311 weston_layer_entry_remove(&(*minimized)->layer_link);
4312 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004313 weston_view_damage_below(*minimized);
4314 }
4315 }
4316 wl_array_release(&switcher->minimized_array);
4317
Kristian Høgsberg07045392012-02-19 18:52:44 -05004318 free(switcher);
4319}
4320
4321static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004322switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004323 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004324{
4325 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004326 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004327
Daniel Stonec9785ea2012-05-30 16:31:52 +01004328 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004329 switcher_next(switcher);
4330}
4331
4332static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004333switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004334 uint32_t mods_depressed, uint32_t mods_latched,
4335 uint32_t mods_locked, uint32_t group)
4336{
4337 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004338 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004339
Daniel Stone351eb612012-05-31 15:27:47 -04004340 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4341 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004342}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004343
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004344static void
4345switcher_cancel(struct weston_keyboard_grab *grab)
4346{
4347 struct switcher *switcher = container_of(grab, struct switcher, grab);
4348
4349 switcher_destroy(switcher);
4350}
4351
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004352static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004353 switcher_key,
4354 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004355 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004356};
4357
4358static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004359switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4360 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004361{
Tiago Vignattibe143262012-04-16 17:31:41 +03004362 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004363 struct switcher *switcher;
4364
4365 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004366 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004367 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004368 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004369 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004370 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004371
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004372 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004374 weston_keyboard_start_grab(keyboard, &switcher->grab);
4375 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004376 switcher_next(switcher);
4377}
4378
Pekka Paalanen3c647232011-12-22 13:43:43 +02004379static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004380backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4381 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004382{
4383 struct weston_compositor *compositor = data;
4384 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004385 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004386
4387 /* TODO: we're limiting to simple use cases, where we assume just
4388 * control on the primary display. We'd have to extend later if we
4389 * ever get support for setting backlights on random desktop LCD
4390 * panels though */
4391 output = get_default_output(compositor);
4392 if (!output)
4393 return;
4394
4395 if (!output->set_backlight)
4396 return;
4397
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004398 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4399 backlight_new = output->backlight_current - 25;
4400 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4401 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004402
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004403 if (backlight_new < 5)
4404 backlight_new = 5;
4405 if (backlight_new > 255)
4406 backlight_new = 255;
4407
4408 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004409 output->set_backlight(output, output->backlight_current);
4410}
4411
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004412static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004413force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4414 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004415{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004416 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004417 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004418 struct desktop_shell *shell = data;
4419 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004420 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004421
Derek Foreman8ae2db52015-07-15 13:00:36 -05004422 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004423 if (!focus_surface)
4424 return;
4425
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004426 wl_signal_emit(&compositor->kill_signal, focus_surface);
4427
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004428 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004429 wl_client_get_credentials(client, &pid, NULL, NULL);
4430
4431 /* Skip clients that we launched ourselves (the credentials of
4432 * the socketpair is ours) */
4433 if (pid == getpid())
4434 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004435
Daniel Stone325fc2d2012-05-30 16:31:58 +01004436 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004437}
4438
4439static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004440workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004441 uint32_t key, void *data)
4442{
4443 struct desktop_shell *shell = data;
4444 unsigned int new_index = shell->workspaces.current;
4445
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004446 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004447 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004448 if (new_index != 0)
4449 new_index--;
4450
4451 change_workspace(shell, new_index);
4452}
4453
4454static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004455workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004456 uint32_t key, void *data)
4457{
4458 struct desktop_shell *shell = data;
4459 unsigned int new_index = shell->workspaces.current;
4460
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004461 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004462 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004463 if (new_index < shell->workspaces.num - 1)
4464 new_index++;
4465
4466 change_workspace(shell, new_index);
4467}
4468
4469static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004470workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004471 uint32_t key, void *data)
4472{
4473 struct desktop_shell *shell = data;
4474 unsigned int new_index;
4475
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004476 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004477 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004478 new_index = key - KEY_F1;
4479 if (new_index >= shell->workspaces.num)
4480 new_index = shell->workspaces.num - 1;
4481
4482 change_workspace(shell, new_index);
4483}
4484
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004485static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004486workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4487 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004488{
4489 struct desktop_shell *shell = data;
4490 unsigned int new_index = shell->workspaces.current;
4491
4492 if (shell->locked)
4493 return;
4494
4495 if (new_index != 0)
4496 new_index--;
4497
Derek Foreman8ae2db52015-07-15 13:00:36 -05004498 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004499}
4500
4501static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004502workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4503 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004504{
4505 struct desktop_shell *shell = data;
4506 unsigned int new_index = shell->workspaces.current;
4507
4508 if (shell->locked)
4509 return;
4510
4511 if (new_index < shell->workspaces.num - 1)
4512 new_index++;
4513
Derek Foreman8ae2db52015-07-15 13:00:36 -05004514 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004515}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004516
4517static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004518shell_reposition_view_on_output_destroy(struct weston_view *view)
4519{
4520 struct weston_output *output, *first_output;
4521 struct weston_compositor *ec = view->surface->compositor;
4522 struct shell_surface *shsurf;
4523 float x, y;
4524 int visible;
4525
4526 x = view->geometry.x;
4527 y = view->geometry.y;
4528
4529 /* At this point the destroyed output is not in the list anymore.
4530 * If the view is still visible somewhere, we leave where it is,
4531 * otherwise, move it to the first output. */
4532 visible = 0;
4533 wl_list_for_each(output, &ec->output_list, link) {
4534 if (pixman_region32_contains_point(&output->region,
4535 x, y, NULL)) {
4536 visible = 1;
4537 break;
4538 }
4539 }
4540
4541 if (!visible) {
4542 first_output = container_of(ec->output_list.next,
4543 struct weston_output, link);
4544
4545 x = first_output->x + first_output->width / 4;
4546 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004547
4548 weston_view_set_position(view, x, y);
4549 } else {
4550 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004551 }
4552
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004553
4554 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004555 if (!shsurf)
4556 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004557
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004558 shsurf->saved_position_valid = false;
4559 set_maximized(shsurf, false);
4560 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004561}
4562
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004563void
4564shell_for_each_layer(struct desktop_shell *shell,
4565 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004566{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004567 struct workspace **ws;
4568
4569 func(shell, &shell->fullscreen_layer, data);
4570 func(shell, &shell->panel_layer, data);
4571 func(shell, &shell->background_layer, data);
4572 func(shell, &shell->lock_layer, data);
4573 func(shell, &shell->input_panel_layer, data);
4574
4575 wl_array_for_each(ws, &shell->workspaces.array)
4576 func(shell, &(*ws)->layer, data);
4577}
4578
4579static void
4580shell_output_destroy_move_layer(struct desktop_shell *shell,
4581 struct weston_layer *layer,
4582 void *data)
4583{
4584 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004585 struct weston_view *view;
4586
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004587 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004588 if (view->output != output)
4589 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004590
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004591 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004592 }
4593}
4594
4595static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004596handle_output_destroy(struct wl_listener *listener, void *data)
4597{
4598 struct shell_output *output_listener =
4599 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004600 struct weston_output *output = output_listener->output;
4601 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004602
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004603 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004604
Quentin Glidic561201c2016-08-20 18:19:51 +02004605 wl_list_remove(&output_listener->panel_surface_listener.link);
4606 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004607 wl_list_remove(&output_listener->destroy_listener.link);
4608 wl_list_remove(&output_listener->link);
4609 free(output_listener);
4610}
4611
4612static void
David Fort50d962f2016-06-09 17:55:52 +02004613shell_resize_surface_to_output(struct desktop_shell *shell,
4614 struct weston_surface *surface,
4615 const struct weston_output *output)
4616{
4617 if (!surface)
4618 return;
4619
4620 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4621 surface->resource,
4622 output->width,
4623 output->height);
4624}
4625
4626
4627static void
4628handle_output_resized(struct wl_listener *listener, void *data)
4629{
4630 struct desktop_shell *shell =
4631 container_of(listener, struct desktop_shell, resized_listener);
4632 struct weston_output *output = (struct weston_output *)data;
4633 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4634
4635 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4636 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4637}
4638
4639static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004640create_shell_output(struct desktop_shell *shell,
4641 struct weston_output *output)
4642{
4643 struct shell_output *shell_output;
4644
4645 shell_output = zalloc(sizeof *shell_output);
4646 if (shell_output == NULL)
4647 return;
4648
4649 shell_output->output = output;
4650 shell_output->shell = shell;
4651 shell_output->destroy_listener.notify = handle_output_destroy;
4652 wl_signal_add(&output->destroy_signal,
4653 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004654 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004655}
4656
4657static void
4658handle_output_create(struct wl_listener *listener, void *data)
4659{
4660 struct desktop_shell *shell =
4661 container_of(listener, struct desktop_shell, output_create_listener);
4662 struct weston_output *output = (struct weston_output *)data;
4663
4664 create_shell_output(shell, output);
4665}
4666
4667static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004668handle_output_move_layer(struct desktop_shell *shell,
4669 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004670{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004671 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004672 struct weston_view *view;
4673 float x, y;
4674
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004675 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004676 if (view->output != output)
4677 continue;
4678
4679 x = view->geometry.x + output->move_x;
4680 y = view->geometry.y + output->move_y;
4681 weston_view_set_position(view, x, y);
4682 }
4683}
4684
4685static void
4686handle_output_move(struct wl_listener *listener, void *data)
4687{
4688 struct desktop_shell *shell;
4689
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004690 shell = container_of(listener, struct desktop_shell,
4691 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004692
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004693 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004694}
4695
4696static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004697setup_output_destroy_handler(struct weston_compositor *ec,
4698 struct desktop_shell *shell)
4699{
4700 struct weston_output *output;
4701
4702 wl_list_init(&shell->output_list);
4703 wl_list_for_each(output, &ec->output_list, link)
4704 create_shell_output(shell, output);
4705
4706 shell->output_create_listener.notify = handle_output_create;
4707 wl_signal_add(&ec->output_created_signal,
4708 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004709
4710 shell->output_move_listener.notify = handle_output_move;
4711 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004712}
4713
4714static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004715shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004716{
Tiago Vignattibe143262012-04-16 17:31:41 +03004717 struct desktop_shell *shell =
4718 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004719 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004720 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004721
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004722 /* Force state to unlocked so we don't try to fade */
4723 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004724
4725 if (shell->child.client) {
4726 /* disable respawn */
4727 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004728 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004729 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004730
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004731 wl_list_remove(&shell->idle_listener.link);
4732 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004733 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004734
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004735 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004736 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004737
Xiong Zhang6b481422013-10-23 13:58:32 +08004738 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4739 wl_list_remove(&shell_output->destroy_listener.link);
4740 wl_list_remove(&shell_output->link);
4741 free(shell_output);
4742 }
4743
4744 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004745 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004746 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004747
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004748 wl_array_for_each(ws, &shell->workspaces.array)
4749 workspace_destroy(*ws);
4750 wl_array_release(&shell->workspaces.array);
4751
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004752 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004753 free(shell);
4754}
4755
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004756static void
4757shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4758{
4759 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004760 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004761
Bob Ham744e6532016-01-12 10:21:48 +00004762 if (shell->allow_zap)
4763 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4764 MODIFIER_CTRL | MODIFIER_ALT,
4765 terminate_binding, ec);
4766
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004767 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004768 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4769 click_to_activate_binding,
4770 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004771 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4772 click_to_activate_binding,
4773 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004774 weston_compositor_add_touch_binding(ec, 0,
4775 touch_to_activate_binding,
4776 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004777 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4778 MODIFIER_SUPER | MODIFIER_ALT,
4779 surface_opacity_binding, NULL);
4780 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4781 MODIFIER_SUPER, zoom_axis_binding,
4782 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004783 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4784 backlight_binding, ec);
4785 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4786 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004787
4788 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004789 if (shell->exposay_modifier)
4790 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4791 exposay_binding, shell);
4792
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004793 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004794 if (!mod)
4795 return;
4796
Daniel Stone325fc2d2012-05-30 16:31:58 +01004797 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4798 zoom_key_binding, NULL);
4799 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4800 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004801 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4802 maximize_binding, NULL);
4803 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4804 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004805 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4806 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004807 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004808 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004809 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004810 weston_compositor_add_button_binding(ec, BTN_LEFT,
4811 mod | MODIFIER_SHIFT,
4812 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004813
4814 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004815 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004816 rotate_binding, NULL);
4817
Daniel Stone325fc2d2012-05-30 16:31:58 +01004818 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4819 shell);
4820 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4821 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004822 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4823 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_compositor_add_key_binding(ec, KEY_K, mod,
4825 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004826 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4827 workspace_up_binding, shell);
4828 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4829 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004830 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4831 workspace_move_surface_up_binding,
4832 shell);
4833 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4834 workspace_move_surface_down_binding,
4835 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004836
4837 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4838 if (shell->workspaces.num > 1) {
4839 num_workspace_bindings = shell->workspaces.num;
4840 if (num_workspace_bindings > 6)
4841 num_workspace_bindings = 6;
4842 for (i = 0; i < num_workspace_bindings; i++)
4843 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4844 workspace_f_binding,
4845 shell);
4846 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004847
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004848 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004849}
4850
Jason Ekstrand024177c2014-04-21 19:42:58 -05004851static void
4852handle_seat_created(struct wl_listener *listener, void *data)
4853{
4854 struct weston_seat *seat = data;
4855
4856 create_shell_seat(seat);
4857}
4858
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004859WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004860module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004861 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004862{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004863 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004864 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004865 struct workspace **pws;
4866 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004867 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004868
Peter Huttererf3d62272013-08-08 11:57:05 +10004869 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004870 if (shell == NULL)
4871 return -1;
4872
Kristian Høgsberg75840622011-09-06 13:48:16 -04004873 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004874
4875 shell->destroy_listener.notify = shell_destroy;
4876 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004877 shell->idle_listener.notify = idle_handler;
4878 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4879 shell->wake_listener.notify = wake_handler;
4880 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004881 shell->transform_listener.notify = transform_handler;
4882 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004883
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004884 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4885 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004886 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4887 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004888 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004889
4890 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004891 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004892
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004893 if (input_panel_setup(shell) < 0)
4894 return -1;
4895
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004896 shell->text_backend = text_backend_init(ec);
4897 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004898 return -1;
4899
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004900 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004901
Daniel Stonedf8133b2013-11-19 11:37:14 +01004902 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4903 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4904
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004905 for (i = 0; i < shell->workspaces.num; i++) {
4906 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4907 if (pws == NULL)
4908 return -1;
4909
4910 *pws = workspace_create();
4911 if (*pws == NULL)
4912 return -1;
4913 }
4914 activate_workspace(shell, 0);
4915
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004916 weston_layer_init(&shell->minimized_layer, NULL);
4917
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004918 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004919 wl_list_init(&shell->workspaces.animation.link);
4920 shell->workspaces.animation.frame = animate_workspace_change_frame;
4921
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004922 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4923 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004924 return -1;
4925
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004926 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004927 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004928 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004929 return -1;
4930
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004931 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004932
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004933 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004934
Xiong Zhang6b481422013-10-23 13:58:32 +08004935 setup_output_destroy_handler(ec, shell);
4936
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004937 loop = wl_display_get_event_loop(ec->wl_display);
4938 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004939
Jason Ekstrand024177c2014-04-21 19:42:58 -05004940 wl_list_for_each(seat, &ec->seat_list, link)
4941 handle_seat_created(NULL, seat);
4942 shell->seat_create_listener.notify = handle_seat_created;
4943 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004944
David Fort50d962f2016-06-09 17:55:52 +02004945 shell->resized_listener.notify = handle_output_resized;
4946 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4947
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004948 screenshooter_create(ec);
4949
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004950 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004951
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004952 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004953
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004954 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4955
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004956 return 0;
4957}