blob: 786020b402b05ca08cdd776f09ae33961314aae1 [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 {
54 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040055 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020056 struct weston_surface *keyboard_focus;
57 struct wl_list link;
58 struct wl_listener seat_destroy_listener;
59 struct wl_listener surface_destroy_listener;
60};
61
Philip Withnall648a4dd2013-11-25 18:01:44 +000062/*
63 * Surface stacking and ordering.
64 *
65 * This is handled using several linked lists of surfaces, organised into
66 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
67 * above all of the surfaces in the layer below. The set of layers is static and
68 * in the following order (top-most first):
69 * • Lock layer (only ever displayed on its own)
70 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010071 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000072 * • Fullscreen layer
73 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000074 * • Workspace layers
75 * • Background layer
76 *
77 * The list of layers may be manipulated to remove whole layers of surfaces from
78 * display. For example, when locking the screen, all layers except the lock
79 * layer are removed.
80 *
81 * A surface’s layer is modified on configuring the surface, in
82 * set_surface_type() (which is only called when the surface’s type change is
83 * _committed_). If a surface’s type changes (e.g. when making a window
84 * fullscreen) its layer changes too.
85 *
86 * In order to allow popup and transient surfaces to be correctly stacked above
87 * their parent surfaces, each surface tracks both its parent surface, and a
88 * linked list of its children. When a surface’s layer is updated, so are the
89 * layers of its children. Note that child surfaces are *not* the same as
90 * subsurfaces — child/parent surfaces are purely for maintaining stacking
91 * order.
92 *
93 * The children_link list of siblings of a surface (i.e. those surfaces which
94 * have the same parent) only contains weston_surfaces which have a
95 * shell_surface. Stacking is not implemented for non-shell_surface
96 * weston_surfaces. This means that the following implication does *not* hold:
97 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
98 */
99
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200100struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500101 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200102
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200103 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500104 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600105 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700106
Tiago Vignattibe143262012-04-16 17:31:41 +0300107 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200108
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500109 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800110 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800111 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700112 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800113 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100114
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200116 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500117 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 } rotation;
119
120 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800121 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500122 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800123 } fullscreen;
124
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200125 struct weston_transform workspace_transform;
126
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500127 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200129
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700130 struct surface_state {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100131 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200132 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700133
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500134 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500135
136 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200137};
138
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300139struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400140 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300141 struct shell_surface *shsurf;
142 struct wl_listener shsurf_destroy_listener;
143};
144
Rusty Lynch1084da52013-08-15 09:10:08 -0700145struct shell_touch_grab {
146 struct weston_touch_grab grab;
147 struct shell_surface *shsurf;
148 struct wl_listener shsurf_destroy_listener;
149 struct weston_touch *touch;
150};
151
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300152struct weston_move_grab {
153 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100154 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500155 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500156};
157
Rusty Lynch1084da52013-08-15 09:10:08 -0700158struct weston_touch_move_grab {
159 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800160 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700161 wl_fixed_t dx, dy;
162};
163
Pekka Paalanen460099f2012-01-20 16:48:25 +0200164struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300165 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500166 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200168 float x;
169 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200170 } center;
171};
172
Giulio Camuffo5085a752013-03-25 21:42:45 +0100173struct shell_seat {
174 struct weston_seat *seat;
175 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500176 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100177
Jason Ekstrand024177c2014-04-21 19:42:58 -0500178 struct wl_listener caps_changed_listener;
179 struct wl_listener pointer_focus_listener;
180 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100181};
182
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800183
Alex Wubd3354b2012-04-17 17:20:49 +0800184static struct desktop_shell *
185shell_surface_get_shell(struct shell_surface *shsurf);
186
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500187static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200188set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
189
190static void
Derek Foreman74de4692015-07-15 13:00:39 -0500191surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500192
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300193static void
194shell_fade_startup(struct desktop_shell *shell);
195
Philip Withnallbecb77e2013-11-25 18:01:30 +0000196static struct shell_seat *
197get_shell_seat(struct weston_seat *seat);
198
Jonny Lambd73c6942014-08-20 15:53:20 +0200199static void
200get_output_panel_size(struct desktop_shell *shell,
201 struct weston_output *output,
202 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700203
Philip Withnall648a4dd2013-11-25 18:01:44 +0000204static void
205shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
206
Pekka Paalanen8274d902014-08-06 19:36:51 +0300207static int
208shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
209{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300210 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200211 struct weston_desktop_surface *desktop_surface =
212 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300213
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200214 t = weston_desktop_surface_get_title(desktop_surface);
215 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300216
217 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200218 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300219 t ? " '" : "", t ?: "", t ? "'" : "",
220 c ? " of " : "", c ?: "");
221}
222
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500223static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400224destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300225{
226 struct shell_grab *grab;
227
228 grab = container_of(listener, struct shell_grab,
229 shsurf_destroy_listener);
230
231 grab->shsurf = NULL;
232}
233
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800234struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500235get_default_view(struct weston_surface *surface)
236{
237 struct shell_surface *shsurf;
238 struct weston_view *view;
239
240 if (!surface || wl_list_empty(&surface->views))
241 return NULL;
242
243 shsurf = get_shell_surface(surface);
244 if (shsurf)
245 return shsurf->view;
246
247 wl_list_for_each(view, &surface->views, surface_link)
248 if (weston_view_is_mapped(view))
249 return view;
250
251 return container_of(surface->views.next, struct weston_view, surface_link);
252}
253
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300254static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300255shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400256 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300257 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400258 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800259 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300260{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300261 struct desktop_shell *shell = shsurf->shell;
262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200263 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400264
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300265 grab->grab.interface = interface;
266 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400267 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500268 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400269 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300270
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700271 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400272 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400273 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800274 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400275 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276 weston_pointer_set_focus(pointer,
277 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400278 wl_fixed_from_int(0),
279 wl_fixed_from_int(0));
280 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300281}
282
Jonny Lambd73c6942014-08-20 15:53:20 +0200283static void
284get_output_panel_size(struct desktop_shell *shell,
285 struct weston_output *output,
286 int *width,
287 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700288{
289 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200290
291 *width = 0;
292 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700293
294 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200295 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700296
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300297 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200298 float x, y;
299
300 if (view->surface->output != output)
301 continue;
302
303 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800304 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
305 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200306 weston_view_to_global_float(view,
307 view->surface->width, 0,
308 &x, &y);
309
Derek Foreman612341f2015-04-15 12:23:55 -0500310 *width = (int)x - output->x;
311 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200312 return;
313
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800314 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
315 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200316 weston_view_to_global_float(view,
317 0, view->surface->height,
318 &x, &y);
319
Derek Foreman612341f2015-04-15 12:23:55 -0500320 *width = view->surface->width + (int)x - output->x;
321 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200322 return;
323
324 default:
325 /* we've already set width and height to
326 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700327 break;
328 }
329 }
330
Jonny Lambd73c6942014-08-20 15:53:20 +0200331 /* the correct view wasn't found */
332}
333
334static void
Quentin Glidic5c201952016-03-23 13:50:49 +0100335get_output_work_area(void *data,
Jonny Lambd73c6942014-08-20 15:53:20 +0200336 struct weston_output *output,
337 pixman_rectangle32_t *area)
338{
Quentin Glidic5c201952016-03-23 13:50:49 +0100339 struct desktop_shell *shell = data;
Jonny Lambd73c6942014-08-20 15:53:20 +0200340 int32_t panel_width = 0, panel_height = 0;
341
Derek Foremanf814c5d2015-04-15 12:23:54 -0500342 area->x = output->x;
343 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200344
345 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200346 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800347 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200348 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500349 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800350 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200351 area->width = output->width;
352 area->height = output->height - panel_height;
353 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800354 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500355 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200357 area->width = output->width - panel_width;
358 area->height = output->height;
359 break;
360 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700361}
362
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400363static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300365{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400367 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700368 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400369
370 if (grab->shsurf->resize_edges) {
371 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400372 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700373 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700375 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300376}
377
378static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700379shell_touch_grab_start(struct shell_touch_grab *grab,
380 const struct weston_touch_grab_interface *interface,
381 struct shell_surface *shsurf,
382 struct weston_touch *touch)
383{
384 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800385
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200386 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700387
Rusty Lynch1084da52013-08-15 09:10:08 -0700388 grab->grab.interface = interface;
389 grab->shsurf = shsurf;
390 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
391 wl_signal_add(&shsurf->destroy_signal,
392 &grab->shsurf_destroy_listener);
393
394 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700396
397 weston_touch_start_grab(touch, &grab->grab);
398 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500399 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700401}
402
403static void
404shell_touch_grab_end(struct shell_touch_grab *grab)
405{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700406 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700407 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700408 grab->shsurf->grabbed = 0;
409 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700410
411 weston_touch_end_grab(grab->touch);
412}
413
414static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800416 struct weston_output *output);
417
Daniel Stone496ca172012-05-30 16:31:42 +0100418static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300419get_modifier(char *modifier)
420{
421 if (!modifier)
422 return MODIFIER_SUPER;
423
424 if (!strcmp("ctrl", modifier))
425 return MODIFIER_CTRL;
426 else if (!strcmp("alt", modifier))
427 return MODIFIER_ALT;
428 else if (!strcmp("super", modifier))
429 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000430 else if (!strcmp("none", modifier))
431 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300432 else
433 return MODIFIER_SUPER;
434}
435
Juan Zhaoe10d2792012-04-25 19:09:52 +0800436static enum animation_type
437get_animation_type(char *animation)
438{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800439 if (!animation)
440 return ANIMATION_NONE;
441
Juan Zhaoe10d2792012-04-25 19:09:52 +0800442 if (!strcmp("zoom", animation))
443 return ANIMATION_ZOOM;
444 else if (!strcmp("fade", animation))
445 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100446 else if (!strcmp("dim-layer", animation))
447 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800448 else
449 return ANIMATION_NONE;
450}
451
Alex Wu4539b082012-03-01 12:57:46 +0800452static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400453shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200454{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400455 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500456 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300457 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000458 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200459
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300460 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400461 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300462 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
463 WESTON_SHELL_CLIENT);
464 if (ret < 0)
465 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400466 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500467 "client", &s, client);
468 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100469 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000470
471 weston_config_section_get_bool(section,
472 "allow-zap", &allow_zap, true);
473 shell->allow_zap = allow_zap;
474
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100475 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400476 "binding-modifier", &s, "super");
477 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200478 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800479
480 weston_config_section_get_string(section,
481 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000482 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800483 free(s);
484
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400485 weston_config_section_get_string(section, "animation", &s, "none");
486 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200487 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200488 weston_config_section_get_string(section, "close-animation", &s, "fade");
489 shell->win_close_animation_type = get_animation_type(s);
490 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700491 weston_config_section_get_string(section,
492 "startup-animation", &s, "fade");
493 shell->startup_animation_type = get_animation_type(s);
494 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700495 if (shell->startup_animation_type == ANIMATION_ZOOM)
496 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100497 weston_config_section_get_string(section, "focus-animation", &s, "none");
498 shell->focus_animation_type = get_animation_type(s);
499 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400500 weston_config_section_get_uint(section, "num-workspaces",
501 &shell->workspaces.num,
502 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200503}
504
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800505struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100506get_default_output(struct weston_compositor *compositor)
507{
Armin Krezović10b06182016-06-23 11:59:30 +0200508 if (wl_list_empty(&compositor->output_list))
509 return NULL;
510
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100511 return container_of(compositor->output_list.next,
512 struct weston_output, link);
513}
514
Pekka Paalanen8274d902014-08-06 19:36:51 +0300515static int
516focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
517{
518 return snprintf(buf, len, "focus highlight effect for output %s",
519 surface->output->name);
520}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100521
522/* no-op func for checking focus surface */
523static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200524focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100525{
526}
527
528static struct focus_surface *
529get_focus_surface(struct weston_surface *surface)
530{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200531 if (surface->committed == focus_surface_committed)
532 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100533 else
534 return NULL;
535}
536
537static bool
538is_focus_surface (struct weston_surface *es)
539{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200540 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100541}
542
543static bool
544is_focus_view (struct weston_view *view)
545{
546 return is_focus_surface (view->surface);
547}
548
549static struct focus_surface *
550create_focus_surface(struct weston_compositor *ec,
551 struct weston_output *output)
552{
553 struct focus_surface *fsurf = NULL;
554 struct weston_surface *surface = NULL;
555
556 fsurf = malloc(sizeof *fsurf);
557 if (!fsurf)
558 return NULL;
559
560 fsurf->surface = weston_surface_create(ec);
561 surface = fsurf->surface;
562 if (surface == NULL) {
563 free(fsurf);
564 return NULL;
565 }
566
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200567 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100568 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200569 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200570 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300571 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100572
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800573 fsurf->view = weston_view_create(surface);
574 if (fsurf->view == NULL) {
575 weston_surface_destroy(surface);
576 free(fsurf);
577 return NULL;
578 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100579 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200580 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100581
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600582 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600583 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100584 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
585 pixman_region32_fini(&surface->opaque);
586 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
587 output->width, output->height);
588 pixman_region32_fini(&surface->input);
589 pixman_region32_init(&surface->input);
590
591 wl_list_init(&fsurf->workspace_transform.link);
592
593 return fsurf;
594}
595
596static void
597focus_surface_destroy(struct focus_surface *fsurf)
598{
599 weston_surface_destroy(fsurf->surface);
600 free(fsurf);
601}
602
603static void
604focus_animation_done(struct weston_view_animation *animation, void *data)
605{
606 struct workspace *ws = data;
607
608 ws->focus_animation = NULL;
609}
610
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200611static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200612focus_state_destroy(struct focus_state *state)
613{
614 wl_list_remove(&state->seat_destroy_listener.link);
615 wl_list_remove(&state->surface_destroy_listener.link);
616 free(state);
617}
618
619static void
620focus_state_seat_destroy(struct wl_listener *listener, void *data)
621{
622 struct focus_state *state = container_of(listener,
623 struct focus_state,
624 seat_destroy_listener);
625
626 wl_list_remove(&state->link);
627 focus_state_destroy(state);
628}
629
630static void
631focus_state_surface_destroy(struct wl_listener *listener, void *data)
632{
633 struct focus_state *state = container_of(listener,
634 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400635 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400636 struct desktop_shell *shell;
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200637 struct weston_surface *main_surface;
638 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500639 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200640
Pekka Paalanen01388e22013-04-25 13:57:44 +0300641 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
642
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300644 wl_list_for_each(view,
645 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400647 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100648 if (is_focus_view(view))
649 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200650 if (!get_shell_surface(view->surface))
651 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400652
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200653 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400654 break;
655 }
656
Pekka Paalanen01388e22013-04-25 13:57:44 +0300657 /* if the focus was a sub-surface, activate its main surface */
658 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200659 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300660
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100661 shell = state->seat->compositor->shell_interface.shell;
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;
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200664 activate(shell, next, state->seat,
665 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400666 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100667 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
668 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 *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400683focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200684{
Jonas Ådahl04769742012-06-13 00:01:24 +0200685 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200686
687 state = malloc(sizeof *state);
688 if (state == NULL)
689 return NULL;
690
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100691 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400692 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200693 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400694 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200695
696 state->seat_destroy_listener.notify = focus_state_seat_destroy;
697 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400698 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200699 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400700 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200701
702 return state;
703}
704
Jonas Ådahl8538b222012-08-29 22:13:03 +0200705static struct focus_state *
706ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
707{
708 struct workspace *ws = get_current_workspace(shell);
709 struct focus_state *state;
710
711 wl_list_for_each(state, &ws->focus_list, link)
712 if (state->seat == seat)
713 break;
714
715 if (&state->link == &ws->focus_list)
716 state = focus_state_create(seat, ws);
717
718 return state;
719}
720
Jonas Ådahl04769742012-06-13 00:01:24 +0200721static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800722focus_state_set_focus(struct focus_state *state,
723 struct weston_surface *surface)
724{
725 if (state->keyboard_focus) {
726 wl_list_remove(&state->surface_destroy_listener.link);
727 wl_list_init(&state->surface_destroy_listener.link);
728 }
729
730 state->keyboard_focus = surface;
731 if (surface)
732 wl_signal_add(&surface->destroy_signal,
733 &state->surface_destroy_listener);
734}
735
736static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400737restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200738{
739 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400740 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100741 struct wl_list pending_seat_list;
742 struct weston_seat *seat, *next_seat;
743
744 /* Temporarily steal the list of seats so that we can keep
745 * track of the seats we've already processed */
746 wl_list_init(&pending_seat_list);
747 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
748 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200749
750 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500751 struct weston_keyboard *keyboard =
752 weston_seat_get_keyboard(state->seat);
753
Neil Roberts4237f502014-04-09 16:33:31 +0100754 wl_list_remove(&state->seat->link);
755 wl_list_insert(&shell->compositor->seat_list,
756 &state->seat->link);
757
Derek Foreman1281a362015-07-31 16:55:32 -0500758 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800759 continue;
760
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400761 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200762
Derek Foreman1281a362015-07-31 16:55:32 -0500763 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200764 }
Neil Roberts4237f502014-04-09 16:33:31 +0100765
766 /* For any remaining seats that we don't have a focus state
767 * for we'll reset the keyboard focus to NULL */
768 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500769 struct weston_keyboard *keyboard =
770 weston_seat_get_keyboard(seat);
771
Neil Roberts4237f502014-04-09 16:33:31 +0100772 wl_list_insert(&shell->compositor->seat_list, &seat->link);
773
Derek Foreman1281a362015-07-31 16:55:32 -0500774 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100775 continue;
776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100778 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200779}
780
781static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200782replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
783 struct weston_seat *seat)
784{
Derek Foreman1281a362015-07-31 16:55:32 -0500785 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200786 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787
788 wl_list_for_each(state, &ws->focus_list, link) {
789 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500790 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791 return;
792 }
793 }
794}
795
796static void
797drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
798 struct weston_surface *surface)
799{
800 struct focus_state *state;
801
802 wl_list_for_each(state, &ws->focus_list, link)
803 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800804 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200805}
806
807static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100808animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
809 struct weston_view *from, struct weston_view *to)
810{
811 struct weston_output *output;
812 bool focus_surface_created = false;
813
814 /* FIXME: Only support dim animation using two layers */
815 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
816 return;
817
818 output = get_default_output(shell->compositor);
819 if (ws->fsurf_front == NULL && (from || to)) {
820 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800821 if (ws->fsurf_front == NULL)
822 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100823 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800824
825 ws->fsurf_back = create_focus_surface(shell->compositor, output);
826 if (ws->fsurf_back == NULL) {
827 focus_surface_destroy(ws->fsurf_front);
828 return;
829 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100830 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800831
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832 focus_surface_created = true;
833 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300834 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
835 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100836 }
837
838 if (ws->focus_animation) {
839 weston_view_animation_destroy(ws->focus_animation);
840 ws->focus_animation = NULL;
841 }
842
843 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300844 weston_layer_entry_insert(&to->layer_link,
845 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300847 weston_layer_entry_insert(&ws->layer.view_list,
848 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849
850 if (focus_surface_created) {
851 ws->focus_animation = weston_fade_run(
852 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200853 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854 focus_animation_done, ws);
855 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300856 weston_layer_entry_insert(&from->layer_link,
857 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 ws->focus_animation = weston_stable_fade_run(
859 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200860 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 focus_animation_done, ws);
862 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300863 weston_layer_entry_insert(&ws->layer.view_list,
864 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 ws->focus_animation = weston_stable_fade_run(
866 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200867 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 focus_animation_done, ws);
869 }
870}
871
872static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200873workspace_destroy(struct workspace *ws)
874{
Jonas Ådahl04769742012-06-13 00:01:24 +0200875 struct focus_state *state, *next;
876
877 wl_list_for_each_safe(state, next, &ws->focus_list, link)
878 focus_state_destroy(state);
879
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100880 if (ws->fsurf_front)
881 focus_surface_destroy(ws->fsurf_front);
882 if (ws->fsurf_back)
883 focus_surface_destroy(ws->fsurf_back);
884
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200885 free(ws);
886}
887
Jonas Ådahl04769742012-06-13 00:01:24 +0200888static void
889seat_destroyed(struct wl_listener *listener, void *data)
890{
891 struct weston_seat *seat = data;
892 struct focus_state *state, *next;
893 struct workspace *ws = container_of(listener,
894 struct workspace,
895 seat_destroyed_listener);
896
897 wl_list_for_each_safe(state, next, &ws->focus_list, link)
898 if (state->seat == seat)
899 wl_list_remove(&state->link);
900}
901
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200902static struct workspace *
903workspace_create(void)
904{
905 struct workspace *ws = malloc(sizeof *ws);
906 if (ws == NULL)
907 return NULL;
908
909 weston_layer_init(&ws->layer, NULL);
910
Jonas Ådahl04769742012-06-13 00:01:24 +0200911 wl_list_init(&ws->focus_list);
912 wl_list_init(&ws->seat_destroyed_listener.link);
913 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100914 ws->fsurf_front = NULL;
915 ws->fsurf_back = NULL;
916 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200917
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918 return ws;
919}
920
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921static int
922workspace_is_empty(struct workspace *ws)
923{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300924 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200925}
926
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200927static struct workspace *
928get_workspace(struct desktop_shell *shell, unsigned int index)
929{
930 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200931 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932 pws += index;
933 return *pws;
934}
935
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800936struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937get_current_workspace(struct desktop_shell *shell)
938{
939 return get_workspace(shell, shell->workspaces.current);
940}
941
942static void
943activate_workspace(struct desktop_shell *shell, unsigned int index)
944{
945 struct workspace *ws;
946
947 ws = get_workspace(shell, index);
948 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
949
950 shell->workspaces.current = index;
951}
952
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200953static unsigned int
954get_output_height(struct weston_output *output)
955{
956 return abs(output->region.extents.y1 - output->region.extents.y2);
957}
958
959static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100960view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200961{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200962 struct weston_transform *transform;
963
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100964 if (is_focus_view(view)) {
965 struct focus_surface *fsurf = get_focus_surface(view->surface);
966 transform = &fsurf->workspace_transform;
967 } else {
968 struct shell_surface *shsurf = get_shell_surface(view->surface);
969 transform = &shsurf->workspace_transform;
970 }
971
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100974 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 weston_matrix_init(&transform->matrix);
977 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980}
981
982static void
983workspace_translate_out(struct workspace *ws, double fraction)
984{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 unsigned int height;
987 double d;
988
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300989 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 d = height * fraction;
992
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 }
995}
996
997static void
998workspace_translate_in(struct workspace *ws, double fraction)
999{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001 unsigned int height;
1002 double d;
1003
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001004 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006
1007 if (fraction > 0)
1008 d = -(height - height * fraction);
1009 else
1010 d = height + height * fraction;
1011
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 }
1014}
1015
1016static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017reverse_workspace_change_animation(struct desktop_shell *shell,
1018 unsigned int index,
1019 struct workspace *from,
1020 struct workspace *to)
1021{
1022 shell->workspaces.current = index;
1023
1024 shell->workspaces.anim_to = to;
1025 shell->workspaces.anim_from = from;
1026 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1027 shell->workspaces.anim_timestamp = 0;
1028
Scott Moreau4272e632012-08-13 09:58:41 -06001029 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001030}
1031
1032static void
1033workspace_deactivate_transforms(struct workspace *ws)
1034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001036 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001037
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001038 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 if (is_focus_view(view)) {
1040 struct focus_surface *fsurf = get_focus_surface(view->surface);
1041 transform = &fsurf->workspace_transform;
1042 } else {
1043 struct shell_surface *shsurf = get_shell_surface(view->surface);
1044 transform = &shsurf->workspace_transform;
1045 }
1046
1047 if (!wl_list_empty(&transform->link)) {
1048 wl_list_remove(&transform->link);
1049 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001050 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001052 }
1053}
1054
1055static void
1056finish_workspace_change_animation(struct desktop_shell *shell,
1057 struct workspace *from,
1058 struct workspace *to)
1059{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001060 struct weston_view *view;
1061
Scott Moreau4272e632012-08-13 09:58:41 -06001062 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001063
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001064 /* Views that extend past the bottom of the output are still
1065 * visible after the workspace animation ends but before its layer
1066 * is hidden. In that case, we need to damage below those views so
1067 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001068 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001069 weston_view_damage_below(view);
1070
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001071 wl_list_remove(&shell->workspaces.animation.link);
1072 workspace_deactivate_transforms(from);
1073 workspace_deactivate_transforms(to);
1074 shell->workspaces.anim_to = NULL;
1075
1076 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1077}
1078
1079static void
1080animate_workspace_change_frame(struct weston_animation *animation,
1081 struct weston_output *output, uint32_t msecs)
1082{
1083 struct desktop_shell *shell =
1084 container_of(animation, struct desktop_shell,
1085 workspaces.animation);
1086 struct workspace *from = shell->workspaces.anim_from;
1087 struct workspace *to = shell->workspaces.anim_to;
1088 uint32_t t;
1089 double x, y;
1090
1091 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1092 finish_workspace_change_animation(shell, from, to);
1093 return;
1094 }
1095
1096 if (shell->workspaces.anim_timestamp == 0) {
1097 if (shell->workspaces.anim_current == 0.0)
1098 shell->workspaces.anim_timestamp = msecs;
1099 else
1100 shell->workspaces.anim_timestamp =
1101 msecs -
1102 /* Invers of movement function 'y' below. */
1103 (asin(1.0 - shell->workspaces.anim_current) *
1104 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1105 M_2_PI);
1106 }
1107
1108 t = msecs - shell->workspaces.anim_timestamp;
1109
1110 /*
1111 * x = [0, π/2]
1112 * y(x) = sin(x)
1113 */
1114 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1115 y = sin(x);
1116
1117 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001118 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119
1120 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1121 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1122 shell->workspaces.anim_current = y;
1123
Scott Moreau4272e632012-08-13 09:58:41 -06001124 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001126 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128}
1129
1130static void
1131animate_workspace_change(struct desktop_shell *shell,
1132 unsigned int index,
1133 struct workspace *from,
1134 struct workspace *to)
1135{
1136 struct weston_output *output;
1137
1138 int dir;
1139
1140 if (index > shell->workspaces.current)
1141 dir = -1;
1142 else
1143 dir = 1;
1144
1145 shell->workspaces.current = index;
1146
1147 shell->workspaces.anim_dir = dir;
1148 shell->workspaces.anim_from = from;
1149 shell->workspaces.anim_to = to;
1150 shell->workspaces.anim_current = 0.0;
1151 shell->workspaces.anim_timestamp = 0;
1152
1153 output = container_of(shell->compositor->output_list.next,
1154 struct weston_output, link);
1155 wl_list_insert(&output->animation_list,
1156 &shell->workspaces.animation.link);
1157
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001158 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159
1160 workspace_translate_in(to, 0);
1161
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001162 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001163
Scott Moreau4272e632012-08-13 09:58:41 -06001164 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001165}
1166
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001167static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001168update_workspace(struct desktop_shell *shell, unsigned int index,
1169 struct workspace *from, struct workspace *to)
1170{
1171 shell->workspaces.current = index;
1172 wl_list_insert(&from->layer.link, &to->layer.link);
1173 wl_list_remove(&from->layer.link);
1174}
1175
1176static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001177change_workspace(struct desktop_shell *shell, unsigned int index)
1178{
1179 struct workspace *from;
1180 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001181 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001182
1183 if (index == shell->workspaces.current)
1184 return;
1185
1186 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001187 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001188 return;
1189
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001190 from = get_current_workspace(shell);
1191 to = get_workspace(shell, index);
1192
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193 if (shell->workspaces.anim_from == to &&
1194 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001195 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001196 reverse_workspace_change_animation(shell, index, from, to);
1197 return;
1198 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001199
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001200 if (shell->workspaces.anim_to != NULL)
1201 finish_workspace_change_animation(shell,
1202 shell->workspaces.anim_from,
1203 shell->workspaces.anim_to);
1204
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001205 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001206
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001207 if (shell->focus_animation_type != ANIMATION_NONE) {
1208 wl_list_for_each(state, &from->focus_list, link)
1209 if (state->keyboard_focus)
1210 animate_focus_change(shell, from,
1211 get_default_view(state->keyboard_focus), NULL);
1212
1213 wl_list_for_each(state, &to->focus_list, link)
1214 if (state->keyboard_focus)
1215 animate_focus_change(shell, to,
1216 NULL, get_default_view(state->keyboard_focus));
1217 }
1218
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001219 if (workspace_is_empty(to) && workspace_is_empty(from))
1220 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221 else
1222 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001223}
1224
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001225static bool
1226workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1227{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001228 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001229 struct wl_list *e;
1230
1231 if (wl_list_empty(list))
1232 return false;
1233
1234 e = list->next;
1235
1236 if (e->next != list)
1237 return false;
1238
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001239 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001240}
1241
1242static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001243surface_keyboard_focus_lost(struct weston_surface *surface)
1244{
1245 struct weston_compositor *compositor = surface->compositor;
1246 struct weston_seat *seat;
1247 struct weston_surface *focus;
1248
1249 wl_list_for_each(seat, &compositor->seat_list, link) {
1250 struct weston_keyboard *keyboard =
1251 weston_seat_get_keyboard(seat);
1252
1253 if (!keyboard)
1254 continue;
1255
1256 focus = weston_surface_get_main_surface(keyboard->focus);
1257 if (focus == surface)
1258 weston_keyboard_set_focus(keyboard, NULL);
1259 }
1260}
1261
1262static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001264 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265 unsigned int index)
1266{
Derek Foreman1281a362015-07-31 16:55:32 -05001267 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001268 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270 struct shell_surface *shsurf;
1271 struct workspace *from;
1272 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001273 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001274
Derek Foreman1281a362015-07-31 16:55:32 -05001275 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 view = get_default_view(surface);
1277 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001278 index == shell->workspaces.current ||
1279 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280 return;
1281
1282 from = get_current_workspace(shell);
1283 to = get_workspace(shell, index);
1284
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001285 weston_layer_entry_remove(&view->layer_link);
1286 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287
Philip Withnall659163d2013-11-25 18:01:36 +00001288 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001289 if (shsurf != NULL)
1290 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001291
Jonas Ådahle9d22502012-08-29 22:13:01 +02001292 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293 drop_focus_state(shell, from, surface);
1294
1295 if (shell->workspaces.anim_from == to &&
1296 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001297 wl_list_remove(&to->layer.link);
1298 wl_list_insert(from->layer.link.prev, &to->layer.link);
1299
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001301
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302 return;
1303 }
1304
1305 if (shell->workspaces.anim_to != NULL)
1306 finish_workspace_change_animation(shell,
1307 shell->workspaces.anim_from,
1308 shell->workspaces.anim_to);
1309
1310 if (workspace_is_empty(from) &&
1311 workspace_has_only(to, surface))
1312 update_workspace(shell, index, from, to);
1313 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001314 if (shsurf != NULL &&
1315 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001316 wl_list_insert(&shell->workspaces.anim_sticky_list,
1317 &shsurf->workspace_transform.link);
1318
1319 animate_workspace_change(shell, index, from, to);
1320 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001321
Jonas Ådahl8538b222012-08-29 22:13:03 +02001322 state = ensure_focus_state(shell, seat);
1323 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001324 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325}
1326
1327static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001328touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001329 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001330{
1331}
1332
1333static void
1334touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1335{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001336 struct weston_touch_move_grab *move =
1337 (struct weston_touch_move_grab *) container_of(
1338 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001339
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001340 if (touch_id == 0)
1341 move->active = 0;
1342
Jonas Ådahl9484b692013-12-02 22:05:03 +01001343 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001344 shell_touch_grab_end(&move->base);
1345 free(move);
1346 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001347}
1348
1349static void
1350touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001351 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001352{
1353 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1354 struct shell_surface *shsurf = move->base.shsurf;
1355 struct weston_surface *es;
1356 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1357 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1358
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001359 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001360 return;
1361
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001362 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001363
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001364 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001365
1366 weston_compositor_schedule_repaint(es->compositor);
1367}
1368
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001369static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001370touch_move_grab_frame(struct weston_touch_grab *grab)
1371{
1372}
1373
1374static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001375touch_move_grab_cancel(struct weston_touch_grab *grab)
1376{
1377 struct weston_touch_move_grab *move =
1378 (struct weston_touch_move_grab *) container_of(
1379 grab, struct shell_touch_grab, grab);
1380
1381 shell_touch_grab_end(&move->base);
1382 free(move);
1383}
1384
Rusty Lynch1084da52013-08-15 09:10:08 -07001385static const struct weston_touch_grab_interface touch_move_grab_interface = {
1386 touch_move_grab_down,
1387 touch_move_grab_up,
1388 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001389 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001390 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001391};
1392
1393static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001394surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001395{
1396 struct weston_touch_move_grab *move;
1397
1398 if (!shsurf)
1399 return -1;
1400
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001401 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1402 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001403 return 0;
1404
1405 move = malloc(sizeof *move);
1406 if (!move)
1407 return -1;
1408
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001409 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001411 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001413 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001414
1415 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001416 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001417
1418 return 0;
1419}
1420
1421static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001422noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001423{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001424}
1425
1426static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001427noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001428 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001429{
1430}
1431
1432static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001433noop_grab_axis_source(struct weston_pointer_grab *grab,
1434 uint32_t source)
1435{
1436}
1437
1438static void
1439noop_grab_frame(struct weston_pointer_grab *grab)
1440{
1441}
1442
1443static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001444constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1445{
1446 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001447 struct weston_surface *surface =
1448 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001449 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001450 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001451 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001452 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001453 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001454
1455 x = wl_fixed_to_int(pointer->x + move->dx);
1456 y = wl_fixed_to_int(pointer->y + move->dy);
1457
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001458 if (shsurf->shell->panel_position ==
1459 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001460 get_output_work_area(shsurf->shell, surface->output, &area);
1461 geometry =
1462 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001463
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001464 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001465 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001466 y = area.y + safety - geometry.height
1467 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001468
1469 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001470 y + geometry.y < area.y)
1471 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001472 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001473
1474 *cx = x;
1475 *cy = y;
1476}
1477
1478static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001479move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001480 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001481{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001483 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001484 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001485 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001486 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001487
Jonas Ådahld2510102014-10-05 21:39:14 +02001488 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001489 if (!shsurf)
1490 return;
1491
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001492 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1493
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001494 constrain_position(move, &cx, &cy);
1495
1496 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001497
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001498 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001499}
1500
1501static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001502move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001503 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001504{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001505 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1506 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001507 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001508 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509
Daniel Stone4dbadb12012-05-30 16:31:51 +01001510 if (pointer->button_count == 0 &&
1511 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001512 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001513 free(grab);
1514 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001515}
1516
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001517static void
1518move_grab_cancel(struct weston_pointer_grab *grab)
1519{
1520 struct shell_grab *shell_grab =
1521 container_of(grab, struct shell_grab, grab);
1522
1523 shell_grab_end(shell_grab);
1524 free(grab);
1525}
1526
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001527static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001528 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529 move_grab_motion,
1530 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001531 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001532 noop_grab_axis_source,
1533 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001534 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001535};
1536
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001537static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001538surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001539 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540{
1541 struct weston_move_grab *move;
1542
1543 if (!shsurf)
1544 return -1;
1545
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001546 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001547 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1548 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001549 return 0;
1550
1551 move = malloc(sizeof *move);
1552 if (!move)
1553 return -1;
1554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001556 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001558 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001559 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001560
1561 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001562 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001563
1564 return 0;
1565}
1566
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567struct weston_resize_grab {
1568 struct shell_grab base;
1569 uint32_t edges;
1570 int32_t width, height;
1571};
1572
1573static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001574resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001575 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576{
1577 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001578 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579 struct shell_surface *shsurf = resize->base.shsurf;
1580 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001581 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582 wl_fixed_t from_x, from_y;
1583 wl_fixed_t to_x, to_y;
1584
Jonas Ådahld2510102014-10-05 21:39:14 +02001585 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001586
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001587 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588 return;
1589
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 weston_view_from_global_fixed(shsurf->view,
1591 pointer->grab_x, pointer->grab_y,
1592 &from_x, &from_y);
1593 weston_view_from_global_fixed(shsurf->view,
1594 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595
1596 width = resize->width;
1597 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1598 width += wl_fixed_to_int(from_x - to_x);
1599 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1600 width += wl_fixed_to_int(to_x - from_x);
1601 }
1602
1603 height = resize->height;
1604 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1605 height += wl_fixed_to_int(from_y - to_y);
1606 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1607 height += wl_fixed_to_int(to_y - from_y);
1608 }
1609
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001610 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1611 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1612
1613 min_size.width = MAX(1, min_size.width);
1614 min_size.height = MAX(1, min_size.height);
1615
1616 if (width < min_size.width)
1617 width = min_size.width;
1618 else if (max_size.width > 0 && width > max_size.width)
1619 width = max_size.width;
1620 if (height < min_size.height)
1621 height = min_size.height;
1622 else if (max_size.width > 0 && width > max_size.width)
1623 width = max_size.width;
1624 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625}
1626
1627static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001628resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629 uint32_t time, uint32_t button, uint32_t state_w)
1630{
1631 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001632 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 enum wl_pointer_button_state state = state_w;
1634
1635 if (pointer->button_count == 0 &&
1636 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1637 shell_grab_end(&resize->base);
1638 free(grab);
1639 }
1640}
1641
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001642static void
1643resize_grab_cancel(struct weston_pointer_grab *grab)
1644{
1645 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1646
1647 shell_grab_end(&resize->base);
1648 free(grab);
1649}
1650
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001651static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001652 noop_grab_focus,
1653 resize_grab_motion,
1654 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001655 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001656 noop_grab_axis_source,
1657 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001658 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659};
1660
Giulio Camuffob8366642013-04-25 13:57:46 +03001661/*
1662 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001663 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001664static void
1665surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1666 int32_t *y, int32_t *w, int32_t *h) {
1667 pixman_region32_t region;
1668 pixman_box32_t *box;
1669 struct weston_subsurface *subsurface;
1670
1671 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 surface->width,
1673 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001674
1675 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1676 pixman_region32_union_rect(&region, &region,
1677 subsurface->position.x,
1678 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 subsurface->surface->width,
1680 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001681 }
1682
1683 box = pixman_region32_extents(&region);
1684 if (x)
1685 *x = box->x1;
1686 if (y)
1687 *y = box->y1;
1688 if (w)
1689 *w = box->x2 - box->x1;
1690 if (h)
1691 *h = box->y2 - box->y1;
1692
1693 pixman_region32_fini(&region);
1694}
1695
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696static int
1697surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001698 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699{
1700 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001701 const unsigned resize_topbottom =
1702 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1703 const unsigned resize_leftright =
1704 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1705 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001706 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001708 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001709 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1710 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711 return 0;
1712
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001713 /* Check for invalid edge combinations. */
1714 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1715 (edges & resize_topbottom) == resize_topbottom ||
1716 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717 return 0;
1718
1719 resize = malloc(sizeof *resize);
1720 if (!resize)
1721 return -1;
1722
1723 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001725 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1726 resize->width = geometry.width;
1727 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001728
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001729 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001731 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
1733 return 0;
1734}
1735
1736static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001737busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001738{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001739 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001740 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001741 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001743 wl_fixed_t sx, sy;
1744
Jason Ekstranda7af7042013-10-12 22:38:11 -05001745 view = weston_compositor_pick_view(pointer->seat->compositor,
1746 pointer->x, pointer->y,
1747 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001748 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001750 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001751 shell_grab_end(grab);
1752 free(grab);
1753 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001754}
1755
1756static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001757busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001758 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001759{
Jonas Ådahld2510102014-10-05 21:39:14 +02001760 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001761}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001764busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765 uint32_t time, uint32_t button, uint32_t state)
1766{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001767 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001768 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001769 struct weston_pointer *pointer = grab->grab.pointer;
1770 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001772 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001773 activate(shsurf->shell, shsurf->view, seat,
1774 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001775 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001776 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001777 activate(shsurf->shell, shsurf->view, seat,
1778 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001779 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001780 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001781}
1782
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001783static void
1784busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1785{
1786 struct shell_grab *grab = (struct shell_grab *) base;
1787
1788 shell_grab_end(grab);
1789 free(grab);
1790}
1791
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001792static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001793 busy_cursor_grab_focus,
1794 busy_cursor_grab_motion,
1795 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001796 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001797 noop_grab_axis_source,
1798 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001799 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001800};
1801
1802static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803handle_pointer_focus(struct wl_listener *listener, void *data)
1804{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001805 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001807 struct shell_surface *shsurf;
1808 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001809
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811 return;
1812
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001813 shsurf = get_shell_surface(view->surface);
1814 if (!shsurf)
1815 return;
1816
1817 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1818
1819 if (shsurf->unresponsive)
1820 set_busy_cursor(shsurf, pointer);
1821 else
1822 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001823}
1824
1825static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001826shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1827{
1828 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001829 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001830}
1831
1832static void
1833shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1834{
1835 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001836 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001837}
1838
1839static void
1840handle_keyboard_focus(struct wl_listener *listener, void *data)
1841{
1842 struct weston_keyboard *keyboard = data;
1843 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1844
1845 if (seat->focused_surface) {
1846 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1847 if (shsurf)
1848 shell_surface_lose_keyboard_focus(shsurf);
1849 }
1850
1851 seat->focused_surface = keyboard->focus;
1852
1853 if (seat->focused_surface) {
1854 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1855 if (shsurf)
1856 shell_surface_gain_keyboard_focus(shsurf);
1857 }
1858}
1859
Philip Withnall07926d92013-11-25 18:01:40 +00001860/* The surface will be inserted into the list immediately after the link
1861 * returned by this function (i.e. will be stacked immediately above the
1862 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001863static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001864shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1865{
1866 struct workspace *ws;
1867
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001868 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1869 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001870 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001871 }
1872
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001873 /* Move the surface to a normal workspace layer so that surfaces
1874 * which were previously fullscreen or transient are no longer
1875 * rendered on top. */
1876 ws = get_current_workspace(shsurf->shell);
1877 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001878}
1879
Philip Withnall648a4dd2013-11-25 18:01:44 +00001880static void
1881shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1882{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001883 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001884}
1885
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001886/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001887 * geometry to ensure the changes are redrawn.
1888 *
1889 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1890 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001891static void
1892shell_surface_update_layer(struct shell_surface *shsurf)
1893{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001894 struct weston_surface *surface =
1895 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001896 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001897
1898 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1899
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001900 if (new_layer_link == NULL)
1901 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001902 if (new_layer_link == &shsurf->view->layer_link)
1903 return;
1904
1905 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001906 weston_layer_entry_remove(&shsurf->view->layer_link);
1907 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001908 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001909 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001910
1911 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001912}
1913
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001914static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001915shell_surface_set_output(struct shell_surface *shsurf,
1916 struct weston_output *output)
1917{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001918 struct weston_surface *es =
1919 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001920
1921 /* get the default output, if the client set it as NULL
1922 check whether the ouput is available */
1923 if (output)
1924 shsurf->output = output;
1925 else if (es->output)
1926 shsurf->output = es->output;
1927 else
1928 shsurf->output = get_default_output(es->compositor);
1929}
1930
1931static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001932weston_view_set_initial_position(struct weston_view *view,
1933 struct desktop_shell *shell);
1934
1935static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001936unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001937{
Philip Withnallf85fe842013-11-25 18:01:37 +00001938 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001939 wl_list_remove(&shsurf->fullscreen.transform.link);
1940 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001941
Jason Ekstranda7af7042013-10-12 22:38:11 -05001942 if (shsurf->fullscreen.black_view)
1943 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1944 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001945
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001946 if (shsurf->saved_position_valid)
1947 weston_view_set_position(shsurf->view,
1948 shsurf->saved_x, shsurf->saved_y);
1949 else
1950 weston_view_set_initial_position(shsurf->view, shsurf->shell);
1951
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001952 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001954 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001955 shsurf->saved_rotation_valid = false;
1956 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001957
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001958 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08001959}
1960
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001961static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001962unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001963{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001964 struct weston_surface *surface =
1965 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1966
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001967 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001968 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001969
1970 if (shsurf->saved_position_valid)
1971 weston_view_set_position(shsurf->view,
1972 shsurf->saved_x, shsurf->saved_y);
1973 else
1974 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001975
1976 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 wl_list_insert(&shsurf->view->geometry.transformation_list,
1978 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001979 shsurf->saved_rotation_valid = false;
1980 }
1981
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001982 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001983}
1984
Philip Withnallbecb77e2013-11-25 18:01:30 +00001985static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001986set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001987{
1988 struct shell_surface *shsurf;
1989 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001990 struct weston_view *view;
1991
1992 view = get_default_view(surface);
1993 if (!view)
1994 return;
1995
1996 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1997
1998 shsurf = get_shell_surface(surface);
1999 current_ws = get_current_workspace(shsurf->shell);
2000
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002001 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002002 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002003
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002004 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002005 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002006
2007 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002008 weston_view_damage_below(view);
2009}
2010
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002011
Tiago Vignattibe143262012-04-16 17:31:41 +03002012static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002013shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002014{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002015 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002016}
2017
Pekka Paalanen8274d902014-08-06 19:36:51 +03002018static int
2019black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2020{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002021 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002022 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002023 int n;
2024 int rem;
2025 int ret;
2026
2027 n = snprintf(buf, len, "black background surface for ");
2028 if (n < 0)
2029 return n;
2030
2031 rem = (int)len - n;
2032 if (rem < 0)
2033 rem = 0;
2034
2035 if (fs_surface->get_label)
2036 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2037 else
2038 ret = snprintf(buf + n, rem, "<unknown>");
2039
2040 if (ret < 0)
2041 return n;
2042
2043 return n + ret;
2044}
2045
Alex Wu21858432012-04-01 20:13:08 +08002046static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002047black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002048
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002050create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002051 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002052 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002053{
2054 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002055 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002056
2057 surface = weston_surface_create(ec);
2058 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002059 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002060 return NULL;
2061 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062 view = weston_view_create(surface);
2063 if (surface == NULL) {
2064 weston_log("no memory\n");
2065 weston_surface_destroy(surface);
2066 return NULL;
2067 }
Alex Wu4539b082012-03-01 12:57:46 +08002068
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002069 surface->committed = black_surface_committed;
2070 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002071 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002072 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002073 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002074 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002075 pixman_region32_fini(&surface->input);
2076 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002077
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002078 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002079 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080
2081 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002082}
2083
Philip Withnalled8f1a92013-11-25 18:01:43 +00002084static void
2085shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2086{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002087 struct weston_surface *surface =
2088 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002089 struct weston_output *output = shsurf->fullscreen_output;
2090
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002091 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002092
2093 if (!shsurf->fullscreen.black_view)
2094 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002095 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002096 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002097 output->x, output->y,
2098 output->width,
2099 output->height);
2100
2101 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002102 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2103 weston_layer_entry_insert(&shsurf->view->layer_link,
2104 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002105 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002106 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002107
Armin Krezović4663aca2016-06-30 06:04:29 +02002108 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002109 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002110}
2111
Alex Wu4539b082012-03-01 12:57:46 +08002112/* Create black surface and append it to the associated fullscreen surface.
2113 * Handle size dismatch and positioning according to the method. */
2114static void
2115shell_configure_fullscreen(struct shell_surface *shsurf)
2116{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002117 struct weston_surface *surface =
2118 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002119 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002120
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002121 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002122 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002123 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2124 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002125
Philip Withnalled8f1a92013-11-25 18:01:43 +00002126 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002127
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002128 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002129 &surf_width, &surf_height);
2130
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002131 if (surface->buffer_ref.buffer)
2132 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002133}
2134
Alex Wu4539b082012-03-01 12:57:46 +08002135static void
2136shell_map_fullscreen(struct shell_surface *shsurf)
2137{
Alex Wubd3354b2012-04-17 17:20:49 +08002138 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002139}
2140
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002141static struct weston_output *
2142get_focused_output(struct weston_compositor *compositor)
2143{
2144 struct weston_seat *seat;
2145 struct weston_output *output = NULL;
2146
2147 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002148 struct weston_touch *touch = weston_seat_get_touch(seat);
2149 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2150 struct weston_keyboard *keyboard =
2151 weston_seat_get_keyboard(seat);
2152
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002153 /* Priority has touch focus, then pointer and
2154 * then keyboard focus. We should probably have
2155 * three for loops and check frist for touch,
2156 * then for pointer, etc. but unless somebody has some
2157 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002158 if (touch && touch->focus)
2159 output = touch->focus->output;
2160 else if (pointer && pointer->focus)
2161 output = pointer->focus->output;
2162 else if (keyboard && keyboard->focus)
2163 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002164
2165 if (output)
2166 break;
2167 }
2168
2169 return output;
2170}
2171
2172static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002173destroy_shell_seat(struct wl_listener *listener, void *data)
2174{
2175 struct shell_seat *shseat =
2176 container_of(listener,
2177 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002178
2179 wl_list_remove(&shseat->seat_destroy_listener.link);
2180 free(shseat);
2181}
2182
Jason Ekstrand024177c2014-04-21 19:42:58 -05002183static void
2184shell_seat_caps_changed(struct wl_listener *listener, void *data)
2185{
Derek Foreman1281a362015-07-31 16:55:32 -05002186 struct weston_keyboard *keyboard;
2187 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002188 struct shell_seat *seat;
2189
2190 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002191 keyboard = weston_seat_get_keyboard(seat->seat);
2192 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002193
Derek Foreman1281a362015-07-31 16:55:32 -05002194 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002195 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002196 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002197 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002198 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002199 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002200 wl_list_init(&seat->keyboard_focus_listener.link);
2201 }
2202
Derek Foreman1281a362015-07-31 16:55:32 -05002203 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002204 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002205 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002206 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002207 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002208 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002209 wl_list_init(&seat->pointer_focus_listener.link);
2210 }
2211}
2212
Giulio Camuffo5085a752013-03-25 21:42:45 +01002213static struct shell_seat *
2214create_shell_seat(struct weston_seat *seat)
2215{
2216 struct shell_seat *shseat;
2217
2218 shseat = calloc(1, sizeof *shseat);
2219 if (!shseat) {
2220 weston_log("no memory to allocate shell seat\n");
2221 return NULL;
2222 }
2223
2224 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002225
2226 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2227 wl_signal_add(&seat->destroy_signal,
2228 &shseat->seat_destroy_listener);
2229
Jason Ekstrand024177c2014-04-21 19:42:58 -05002230 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2231 wl_list_init(&shseat->keyboard_focus_listener.link);
2232
2233 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2234 wl_list_init(&shseat->pointer_focus_listener.link);
2235
2236 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2237 wl_signal_add(&seat->updated_caps_signal,
2238 &shseat->caps_changed_listener);
2239 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2240
Giulio Camuffo5085a752013-03-25 21:42:45 +01002241 return shseat;
2242}
2243
2244static struct shell_seat *
2245get_shell_seat(struct weston_seat *seat)
2246{
2247 struct wl_listener *listener;
2248
2249 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002250 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002251
2252 return container_of(listener,
2253 struct shell_seat, seat_destroy_listener);
2254}
2255
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002256static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002257fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002258{
2259 struct shell_surface *shsurf = data;
2260
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002261 weston_surface_destroy(shsurf->view->surface);
2262 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002263}
2264
2265static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002266fade_out_done(struct weston_view_animation *animation, void *data)
2267{
2268 struct shell_surface *shsurf = data;
2269 struct wl_event_loop *loop;
2270
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002271 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002273 if (weston_view_is_mapped(shsurf->view)) {
2274 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002275 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002276 }
2277}
2278
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002279struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002280get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002281{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002282 if (weston_surface_is_desktop_surface(surface)) {
2283 struct weston_desktop_surface *desktop_surface =
2284 weston_surface_get_desktop_surface(surface);
2285 return weston_desktop_surface_get_user_data(desktop_surface);
2286 }
2287 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002288}
2289
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002290/*
2291 * libweston-desktop
2292 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002293
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002294static void
2295desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2296 void *shell)
2297{
2298 struct weston_desktop_client *client =
2299 weston_desktop_surface_get_client(desktop_surface);
2300 struct wl_client *wl_client =
2301 weston_desktop_client_get_client(client);
2302 struct weston_view *view;
2303 struct shell_surface *shsurf;
2304 struct weston_surface *surface =
2305 weston_desktop_surface_get_surface(desktop_surface);
2306
2307 view = weston_desktop_surface_create_view(desktop_surface);
2308 if (!view)
2309 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002310
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002311 shsurf = calloc(1, sizeof *shsurf);
2312 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002313 if (wl_client)
2314 wl_client_post_no_memory(wl_client);
2315 else
2316 weston_log("no memory to allocate shell surface\n");
2317 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002318 }
2319
Pekka Paalanen8274d902014-08-06 19:36:51 +03002320 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002321
Tiago Vignattibc052c92012-04-19 16:18:18 +03002322 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002323 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002324 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002325 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002326 shsurf->desktop_surface = desktop_surface;
2327 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002329 wl_list_init(&shsurf->fullscreen.transform.link);
2330
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002331 shsurf->output = get_default_output(shsurf->shell->compositor);
2332
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002333 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002334
Pekka Paalanen460099f2012-01-20 16:48:25 +02002335 /* empty when not in use */
2336 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002337 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002338
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002339 wl_list_init(&shsurf->workspace_transform.link);
2340
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002341 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2342 weston_desktop_surface_set_activated(desktop_surface,
2343 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002344}
2345
Tiago Vignattibc052c92012-04-19 16:18:18 +03002346static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002347desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2348 void *shell)
2349{
2350 struct shell_surface *shsurf =
2351 weston_desktop_surface_get_user_data(desktop_surface);
2352 struct weston_surface *surface =
2353 weston_desktop_surface_get_surface(desktop_surface);
2354
2355 if (!shsurf)
2356 return;
2357
2358 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2359
2360 if (shsurf->fullscreen.black_view)
2361 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2362
2363 weston_surface_set_label_func(surface, NULL);
2364 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2365 shsurf->desktop_surface = NULL;
2366
2367 if (weston_surface_is_mapped(surface) &&
2368 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2369 pixman_region32_fini(&surface->pending.input);
2370 pixman_region32_init(&surface->pending.input);
2371 pixman_region32_fini(&surface->input);
2372 pixman_region32_init(&surface->input);
2373 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2374 fade_out_done, shsurf);
2375 } else {
2376 weston_desktop_surface_destroy_view(shsurf->view);
2377 weston_view_destroy(shsurf->view);
2378 free(shsurf);
2379 }
2380}
2381
2382static void
2383set_maximized_position(struct desktop_shell *shell,
2384 struct shell_surface *shsurf)
2385{
2386 pixman_rectangle32_t area;
2387 struct weston_geometry geometry;
2388
2389 get_output_work_area(shell, shsurf->output, &area);
2390 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2391
2392 weston_view_set_position(shsurf->view,
2393 area.x - geometry.x,
2394 area.y - geometry.y);
2395}
2396
2397static void
2398map(struct desktop_shell *shell, struct shell_surface *shsurf,
2399 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002400{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002401 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002402 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2403 struct weston_compositor *compositor = shell->compositor;
2404 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002405
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002406 /* initial positioning, see also configure() */
2407 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
2408 center_on_output(shsurf->view, shsurf->fullscreen_output);
2409 shell_map_fullscreen(shsurf);
2410 } else if (weston_desktop_surface_get_maximized(shsurf->desktop_surface)) {
2411 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002412 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002413 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002414 }
2415
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002416 /* Surface stacking order, see also activate(). */
2417 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002418
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002419 weston_view_update_transform(shsurf->view);
2420 shsurf->view->is_mapped = true;
2421 if (weston_desktop_surface_get_maximized(shsurf->desktop_surface)) {
2422 surface->output = shsurf->output;
2423 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002424 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002425
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002426 if (!shell->locked) {
2427 wl_list_for_each(seat, &compositor->seat_list, link)
2428 activate(shell, shsurf->view, seat,
2429 WESTON_ACTIVATE_FLAG_CONFIGURE);
2430 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002431
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002432 if (!weston_desktop_surface_get_maximized(shsurf->desktop_surface) &&
2433 !weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
2434 switch (shell->win_animation_type) {
2435 case ANIMATION_FADE:
2436 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2437 break;
2438 case ANIMATION_ZOOM:
2439 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2440 break;
2441 case ANIMATION_NONE:
2442 default:
2443 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002444 }
2445 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002446}
2447
2448static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002449desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2450 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002451{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452 struct shell_surface *shsurf =
2453 weston_desktop_surface_get_user_data(desktop_surface);
2454 struct weston_surface *surface =
2455 weston_desktop_surface_get_surface(desktop_surface);
2456 struct weston_view *view = shsurf->view;
2457 struct desktop_shell *shell = data;
2458
2459 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002460 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002461
2462 if (!weston_surface_is_mapped(surface)) {
2463 map(shell, shsurf, sx, sy);
2464 surface->is_mapped = true;
2465 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2466 ++surface->ref_count;
2467 return;
2468 }
2469
2470 if (sx == 0 && sy == 0 &&
2471 shsurf->last_width == surface->width &&
2472 shsurf->last_height == surface->height)
2473 return;
2474
2475 if (weston_desktop_surface_get_fullscreen(desktop_surface))
2476 shell_configure_fullscreen(shsurf);
2477 else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2478 set_maximized_position(shell, shsurf);
2479 surface->output = shsurf->output;
2480 } else {
2481 float from_x, from_y;
2482 float to_x, to_y;
2483 float x, y;
2484
2485 if (shsurf->resize_edges) {
2486 sx = 0;
2487 sy = 0;
2488 }
2489
2490 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2491 sx = shsurf->last_width - surface->width;
2492 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2493 sy = shsurf->last_height - surface->height;
2494
2495 shsurf->last_width = surface->width;
2496 shsurf->last_height = surface->height;
2497
2498 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2499 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2500 x = shsurf->view->geometry.x + to_x - from_x;
2501 y = shsurf->view->geometry.y + to_y - from_y;
2502
2503 weston_view_set_position(shsurf->view, x, y);
2504 }
2505
2506 /* XXX: would a fullscreen surface need the same handling? */
2507 if (surface->output) {
2508 wl_list_for_each(view, &surface->views, surface_link)
2509 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002510 }
2511}
2512
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002513static void
2514set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2515 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002516{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002517 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2518 struct weston_surface *surface =
2519 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2520 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002521
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002522 if (fullscreen) {
2523 /* handle clients launching in fullscreen */
2524 if (output == NULL && !weston_surface_is_mapped(surface)) {
2525 /* Set the output to the one that has focus currently. */
2526 output = get_focused_output(surface->compositor);
2527 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002528
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002529 shell_surface_set_output(shsurf, output);
2530 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002531
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002532 width = shsurf->output->width;
2533 height = shsurf->output->height;
2534 } else {
2535 unset_fullscreen(shsurf);
2536 }
2537 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2538 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002539}
2540
2541static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002542desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2543 struct weston_seat *seat, uint32_t serial, void *shell)
2544{
2545 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2546 struct weston_touch *touch = weston_seat_get_touch(seat);
2547 struct shell_surface *shsurf =
2548 weston_desktop_surface_get_user_data(desktop_surface);
2549 struct weston_surface *surface =
2550 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2551 struct wl_resource *resource = surface->resource;
2552 struct weston_surface *focus;
2553
2554 if (pointer &&
2555 pointer->focus &&
2556 pointer->button_count > 0 &&
2557 pointer->grab_serial == serial) {
2558 focus = weston_surface_get_main_surface(pointer->focus->surface);
2559 if ((focus == surface) &&
2560 (surface_move(shsurf, pointer, true) < 0))
2561 wl_resource_post_no_memory(resource);
2562 } else if (touch &&
2563 touch->focus &&
2564 touch->grab_serial == serial) {
2565 focus = weston_surface_get_main_surface(touch->focus->surface);
2566 if ((focus == surface) &&
2567 (surface_touch_move(shsurf, touch) < 0))
2568 wl_resource_post_no_memory(resource);
2569 }
2570}
2571
2572static void
2573desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2574 struct weston_seat *seat, uint32_t serial,
2575 enum weston_desktop_surface_edge edges, void *shell)
2576{
2577 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2578 struct shell_surface *shsurf =
2579 weston_desktop_surface_get_user_data(desktop_surface);
2580 struct weston_surface *surface =
2581 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2582 struct wl_resource *resource = surface->resource;
2583 struct weston_surface *focus;
2584
2585 if (!pointer ||
2586 pointer->button_count == 0 ||
2587 pointer->grab_serial != serial ||
2588 pointer->focus == NULL)
2589 return;
2590
2591 focus = weston_surface_get_main_surface(pointer->focus->surface);
2592 if (focus != surface)
2593 return;
2594
2595 if (surface_resize(shsurf, pointer, edges) < 0)
2596 wl_resource_post_no_memory(resource);
2597}
2598
2599static void
2600desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2601 bool fullscreen,
2602 struct weston_output *output, void *shell)
2603{
2604 struct shell_surface *shsurf =
2605 weston_desktop_surface_get_user_data(desktop_surface);
2606
2607 set_fullscreen(shsurf, fullscreen, output);
2608}
2609
2610static void
2611set_maximized(struct shell_surface *shsurf, bool maximized)
2612{
2613 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2614 struct weston_surface *surface =
2615 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2616 int32_t width = 0, height = 0;
2617
2618 if (maximized) {
2619 struct weston_output *output;
2620 struct desktop_shell *shell;
2621 pixman_rectangle32_t area;
2622
2623 if (!weston_surface_is_mapped(surface))
2624 output = get_focused_output(surface->compositor);
2625 else
2626 output = surface->output;
2627
2628 shell_surface_set_output(shsurf, output);
2629
2630 shell = shell_surface_get_shell(shsurf);
2631 get_output_work_area(shell, shsurf->output, &area);
2632
2633 width = area.width;
2634 height = area.height;
2635 } else {
2636 unset_maximized(shsurf);
2637 }
2638 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2639 weston_desktop_surface_set_size(desktop_surface, width, height);
2640}
2641
2642static void
2643desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2644 bool maximized, void *shell)
2645{
2646 struct shell_surface *shsurf =
2647 weston_desktop_surface_get_user_data(desktop_surface);
2648
2649 set_maximized(shsurf, maximized);
2650}
2651
2652static void
2653desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2654 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002655{
2656 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002657 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002658
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002659 /* apply compositor's own minimization logic (hide) */
2660 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002661}
2662
Rafael Antognollie2a34552013-12-03 15:35:45 -02002663static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002664set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002665{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002666 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002667
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002668 if (pointer->grab->interface == &busy_cursor_grab_interface)
2669 return;
2670
2671 grab = malloc(sizeof *grab);
2672 if (!grab)
2673 return;
2674
2675 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2676 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2677 /* Mark the shsurf as ungrabbed so that button binding is able
2678 * to move it. */
2679 shsurf->grabbed = 0;
2680}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002681
2682static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002683end_busy_cursor(struct weston_compositor *compositor,
2684 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002685{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002686 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002687 struct shell_grab *grab;
2688 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002689
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002690 wl_list_for_each(seat, &compositor->seat_list, link) {
2691 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2692 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002693
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002694 if (!pointer)
2695 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002696
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002697 if (pointer->grab->interface != &busy_cursor_grab_interface)
2698 continue;
2699
2700 grab = (struct shell_grab *) pointer->grab;
2701 shsurf = grab->shsurf;
2702 if (!shsurf)
2703 continue;
2704
2705 grab_client =
2706 weston_desktop_surface_get_client(shsurf->desktop_surface);
2707 if (grab_client == desktop_client) {
2708 shell_grab_end(grab);
2709 free(grab);
2710 }
2711 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002712}
2713
2714static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002715desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2716 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002717{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002718 struct shell_surface *shsurf =
2719 weston_desktop_surface_get_user_data(desktop_surface);
2720 bool *unresponsive = user_data;
2721
2722 shsurf->unresponsive = *unresponsive;
2723}
2724
2725static void
2726desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2727 void *shell_)
2728{
2729 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002730 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002731 struct weston_seat *seat;
2732 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002733
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002734 weston_desktop_client_for_each_surface(desktop_client,
2735 desktop_surface_set_unresponsive,
2736 &unresponsive);
2737
2738
2739 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2740 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2741 struct weston_desktop_client *grab_client;
2742
2743 if (!pointer || !pointer->focus)
2744 continue;
2745
2746 shsurf = get_shell_surface(pointer->focus->surface);
2747 if (!shsurf)
2748 continue;
2749
2750 grab_client =
2751 weston_desktop_surface_get_client(shsurf->desktop_surface);
2752 if (grab_client == desktop_client)
2753 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002754 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002755}
2756
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002757static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002758desktop_surface_pong(struct weston_desktop_client *desktop_client,
2759 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002760{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002761 struct desktop_shell *shell = shell_;
2762 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002763
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002764 weston_desktop_client_for_each_surface(desktop_client,
2765 desktop_surface_set_unresponsive,
2766 &unresponsive);
2767 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002768}
2769
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002770static const struct weston_desktop_api shell_desktop_api = {
2771 .struct_size = sizeof(struct weston_desktop_api),
2772 .surface_added = desktop_surface_added,
2773 .surface_removed = desktop_surface_removed,
2774 .committed = desktop_surface_committed,
2775 .move = desktop_surface_move,
2776 .resize = desktop_surface_resize,
2777 .fullscreen_requested = desktop_surface_fullscreen_requested,
2778 .maximized_requested = desktop_surface_maximized_requested,
2779 .minimized_requested = desktop_surface_minimized_requested,
2780 .ping_timeout = desktop_surface_ping_timeout,
2781 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002782};
2783
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002784/* ************************ *
2785 * end of libweston-desktop *
2786 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002787static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002788shell_fade(struct desktop_shell *shell, enum fade_type type);
2789
Pekka Paalanen77346a62011-11-30 16:26:35 +02002790static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002791configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002792{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002793 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002794
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002795 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002796 if (v->output == ev->output && v != ev) {
2797 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002798 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002799 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002800 }
2801 }
2802
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002803 weston_view_set_position(ev, ev->output->x, ev->output->y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002804 ev->surface->is_mapped = true;
2805 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002806
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002807 if (wl_list_empty(&ev->layer_link.link)) {
2808 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002810 }
2811}
2812
David Fort50d962f2016-06-09 17:55:52 +02002813
2814static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002815find_shell_output_from_weston_output(struct desktop_shell *shell,
2816 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002817{
2818 struct shell_output *shell_output;
2819
2820 wl_list_for_each(shell_output, &shell->output_list, link) {
2821 if (shell_output->output == output)
2822 return shell_output;
2823 }
2824
2825 return NULL;
2826}
2827
Pekka Paalanen8274d902014-08-06 19:36:51 +03002828static int
2829background_get_label(struct weston_surface *surface, char *buf, size_t len)
2830{
2831 return snprintf(buf, len, "background for output %s",
2832 surface->output->name);
2833}
2834
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002835static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002836background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002837{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002838 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002839 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002840
Jason Ekstranda7af7042013-10-12 22:38:11 -05002841 view = container_of(es->views.next, struct weston_view, surface_link);
2842
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002843 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002844}
2845
2846static void
David Fort50d962f2016-06-09 17:55:52 +02002847handle_background_surface_destroy(struct wl_listener *listener, void *data)
2848{
2849 struct shell_output *output =
2850 container_of(listener, struct shell_output, background_surface_listener);
2851
2852 weston_log("background surface gone\n");
2853 output->background_surface = NULL;
2854}
2855
2856static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002857desktop_shell_set_background(struct wl_client *client,
2858 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002859 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002860 struct wl_resource *surface_resource)
2861{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002862 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002863 struct weston_surface *surface =
2864 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002865 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002866 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002867
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002868 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002869 wl_resource_post_error(surface_resource,
2870 WL_DISPLAY_ERROR_INVALID_OBJECT,
2871 "surface role already assigned");
2872 return;
2873 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002874
Jason Ekstranda7af7042013-10-12 22:38:11 -05002875 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2876 weston_view_destroy(view);
2877 view = weston_view_create(surface);
2878
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002879 surface->committed = background_committed;
2880 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002881 weston_surface_set_label_func(surface, background_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002882 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002884 weston_desktop_shell_send_configure(resource, 0,
2885 surface_resource,
2886 surface->output->width,
2887 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002888
2889 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2890 sh_output->background_surface = surface;
2891
2892 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
2893 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002894}
2895
Pekka Paalanen8274d902014-08-06 19:36:51 +03002896static int
2897panel_get_label(struct weston_surface *surface, char *buf, size_t len)
2898{
2899 return snprintf(buf, len, "panel for output %s",
2900 surface->output->name);
2901}
2902
Kristian Høgsberg75840622011-09-06 13:48:16 -04002903static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002904panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002905{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002906 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002907 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002908
Jason Ekstranda7af7042013-10-12 22:38:11 -05002909 view = container_of(es->views.next, struct weston_view, surface_link);
2910
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002911 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002912}
2913
2914static void
David Fort50d962f2016-06-09 17:55:52 +02002915handle_panel_surface_destroy(struct wl_listener *listener, void *data)
2916{
2917 struct shell_output *output =
2918 container_of(listener, struct shell_output, panel_surface_listener);
2919
2920 weston_log("panel surface gone\n");
2921 output->panel_surface = NULL;
2922}
2923
2924
2925static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002926desktop_shell_set_panel(struct wl_client *client,
2927 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002928 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002929 struct wl_resource *surface_resource)
2930{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002931 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002932 struct weston_surface *surface =
2933 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002934 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02002935 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002936
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002937 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002938 wl_resource_post_error(surface_resource,
2939 WL_DISPLAY_ERROR_INVALID_OBJECT,
2940 "surface role already assigned");
2941 return;
2942 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002943
Jason Ekstranda7af7042013-10-12 22:38:11 -05002944 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2945 weston_view_destroy(view);
2946 view = weston_view_create(surface);
2947
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002948 surface->committed = panel_committed;
2949 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002950 weston_surface_set_label_func(surface, panel_get_label);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002951 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002952 view->output = surface->output;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08002953 weston_desktop_shell_send_configure(resource, 0,
2954 surface_resource,
2955 surface->output->width,
2956 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002957
2958 sh_output = find_shell_output_from_weston_output(shell, surface->output);
2959 sh_output->panel_surface = surface;
2960
2961 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
2962 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002963}
2964
Pekka Paalanen8274d902014-08-06 19:36:51 +03002965static int
2966lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2967{
2968 return snprintf(buf, len, "lock window");
2969}
2970
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002971static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002972lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002973{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002974 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002975 struct weston_view *view;
2976
2977 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002978
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002979 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01002980 return;
2981
Jason Ekstranda7af7042013-10-12 22:38:11 -05002982 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002983
2984 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002985 weston_layer_entry_insert(&shell->lock_layer.view_list,
2986 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002987 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02002988 surface->is_mapped = true;
2989 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002990 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002991 }
2992}
2993
2994static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002995handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002996{
Tiago Vignattibe143262012-04-16 17:31:41 +03002997 struct desktop_shell *shell =
2998 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002999
Martin Minarik6d118362012-06-07 18:01:59 +02003000 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003001 shell->lock_surface = NULL;
3002}
3003
3004static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003005desktop_shell_set_lock_surface(struct wl_client *client,
3006 struct wl_resource *resource,
3007 struct wl_resource *surface_resource)
3008{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003009 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003010 struct weston_surface *surface =
3011 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003012
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003013 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003014
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003015 if (!shell->locked)
3016 return;
3017
Pekka Paalanen98262232011-12-01 10:42:22 +02003018 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003019
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003020 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003021 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003022 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003023
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003024 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003025 surface->committed = lock_surface_committed;
3026 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003027 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003028}
3029
3030static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003031resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003032{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003033 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003034
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003035 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003036 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003037 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003038 &shell->input_panel_layer.link);
3039 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003040 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003041 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003042 wl_list_insert(&shell->compositor->cursor_layer.link,
3043 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003044 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01003045 wl_list_insert(&shell->fullscreen_layer.link,
3046 &shell->panel_layer.link);
3047 wl_list_insert(&shell->panel_layer.link,
3048 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003049
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003050 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003051
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003052 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003053 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003054 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003055}
3056
3057static void
3058desktop_shell_unlock(struct wl_client *client,
3059 struct wl_resource *resource)
3060{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003061 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003062
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003063 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003064
3065 if (shell->locked)
3066 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003067}
3068
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003069static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003070desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003071 struct wl_resource *resource,
3072 struct wl_resource *surface_resource)
3073{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003074 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003075
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003076 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003077 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003078}
3079
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003080static void
3081desktop_shell_desktop_ready(struct wl_client *client,
3082 struct wl_resource *resource)
3083{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003084 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003085
3086 shell_fade_startup(shell);
3087}
3088
Jonny Lamb765760d2014-08-20 15:53:19 +02003089static void
3090desktop_shell_set_panel_position(struct wl_client *client,
3091 struct wl_resource *resource,
3092 uint32_t position)
3093{
3094 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3095
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003096 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3097 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3098 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3099 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003100 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003101 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003102 "bad position argument");
3103 return;
3104 }
3105
3106 shell->panel_position = position;
3107}
3108
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003109static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003110 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003111 desktop_shell_set_panel,
3112 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003113 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003114 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003115 desktop_shell_desktop_ready,
3116 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003117};
3118
3119static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003120move_binding(struct weston_pointer *pointer, uint32_t time,
3121 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003122{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003123 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003124 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003125 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003126
Derek Foreman8ae2db52015-07-15 13:00:36 -05003127 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003128 return;
3129
Derek Foreman8ae2db52015-07-15 13:00:36 -05003130 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003131
Pekka Paalanen01388e22013-04-25 13:57:44 +03003132 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003133 if (surface == NULL)
3134 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003135
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003136 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003137 if (shsurf == NULL ||
3138 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3139 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003140 return;
3141
Derek Foreman794fa0e2015-07-15 13:00:38 -05003142 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003143}
3144
3145static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003146maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
3147 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003148{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003149 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003150 struct weston_surface *surface;
3151 struct shell_surface *shsurf;
3152
3153 surface = weston_surface_get_main_surface(focus);
3154 if (surface == NULL)
3155 return;
3156
3157 shsurf = get_shell_surface(surface);
3158 if (shsurf == NULL)
3159 return;
3160
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003161 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003162}
3163
3164static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003165fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
3166 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003167{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003168 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003169 struct weston_surface *surface;
3170 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003171 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003172
3173 surface = weston_surface_get_main_surface(focus);
3174 if (surface == NULL)
3175 return;
3176
3177 shsurf = get_shell_surface(surface);
3178 if (shsurf == NULL)
3179 return;
3180
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003181 fullscreen =
3182 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003183
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003184 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003185}
3186
3187static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003188touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003189{
Derek Foreman362656b2014-09-04 10:23:05 -05003190 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003191 struct weston_surface *surface;
3192 struct shell_surface *shsurf;
3193
Derek Foreman8ae2db52015-07-15 13:00:36 -05003194 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003195 return;
3196
Derek Foreman8ae2db52015-07-15 13:00:36 -05003197 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003198 surface = weston_surface_get_main_surface(focus);
3199 if (surface == NULL)
3200 return;
3201
3202 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003203 if (shsurf == NULL ||
3204 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3205 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003206 return;
3207
Derek Foremanb7674ae2015-07-15 13:00:37 -05003208 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003209}
3210
3211static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003212resize_binding(struct weston_pointer *pointer, uint32_t time,
3213 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003214{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003215 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003216 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003217 uint32_t edges = 0;
3218 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003219 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003220
Derek Foreman8ae2db52015-07-15 13:00:36 -05003221 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003222 return;
3223
Derek Foreman8ae2db52015-07-15 13:00:36 -05003224 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003225
Pekka Paalanen01388e22013-04-25 13:57:44 +03003226 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003227 if (surface == NULL)
3228 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003229
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003230 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003231 if (shsurf == NULL ||
3232 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3233 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003234 return;
3235
Jason Ekstranda7af7042013-10-12 22:38:11 -05003236 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003237 wl_fixed_to_int(pointer->grab_x),
3238 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003239 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003240
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003241 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003242 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003243 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003244 edges |= 0;
3245 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003246 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003247
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003248 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003249 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003250 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003251 edges |= 0;
3252 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003253 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003254
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003255 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003256}
3257
3258static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003259surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003260 struct weston_pointer_axis_event *event,
3261 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003262{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003263 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003264 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003265 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003266 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003267
Pekka Paalanen01388e22013-04-25 13:57:44 +03003268 /* XXX: broken for windows containing sub-surfaces */
3269 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003270 if (surface == NULL)
3271 return;
3272
3273 shsurf = get_shell_surface(surface);
3274 if (!shsurf)
3275 return;
3276
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003277 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003278
Jason Ekstranda7af7042013-10-12 22:38:11 -05003279 if (shsurf->view->alpha > 1.0)
3280 shsurf->view->alpha = 1.0;
3281 if (shsurf->view->alpha < step)
3282 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003283
Jason Ekstranda7af7042013-10-12 22:38:11 -05003284 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003285 weston_surface_damage(surface);
3286}
3287
3288static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003289do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003290 double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003291{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003292 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003293 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003294 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003295 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003296
Derek Foreman1281a362015-07-31 16:55:32 -05003297 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003298 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3299 return;
3300 }
3301
Scott Moreauccbf29d2012-02-22 14:21:41 -07003302 wl_list_for_each(output, &compositor->output_list, link) {
3303 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003304 wl_fixed_to_double(pointer->x),
3305 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003306 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003307 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003308 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003309 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003310 increment = -output->zoom.increment;
3311 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003312 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003313 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003314 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003315 else
3316 increment = 0;
3317
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003318 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003319
Scott Moreaue6603982012-06-11 13:07:51 -06003320 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003321 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003322 else if (output->zoom.level > output->zoom.max_level)
3323 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003324
3325 if (!output->zoom.active) {
3326 if (output->zoom.level <= 0.0)
3327 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003328 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003329 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003330
Scott Moreaue6603982012-06-11 13:07:51 -06003331 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003332
Jason Ekstranda7af7042013-10-12 22:38:11 -05003333 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003334 }
3335 }
3336}
3337
Scott Moreauccbf29d2012-02-22 14:21:41 -07003338static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003339zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003340 struct weston_pointer_axis_event *event,
3341 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003342{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003343 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003344}
3345
3346static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003347zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
3348 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003349{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003350 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003351}
3352
3353static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003354terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
3355 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003356{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003357 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003358
Daniel Stone325fc2d2012-05-30 16:31:58 +01003359 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003360}
3361
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003362static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003363rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003364 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003365{
3366 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003367 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003368 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003369 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003370 struct weston_surface *surface =
3371 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003372 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003373
Jonas Ådahld2510102014-10-05 21:39:14 +02003374 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003375
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003376 if (!shsurf)
3377 return;
3378
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003379 cx = 0.5f * surface->width;
3380 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003381
Daniel Stone37816df2012-05-16 18:45:18 +01003382 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3383 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003384 r = sqrtf(dx * dx + dy * dy);
3385
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003386 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003387 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003388
3389 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003390 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003391 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003392
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003393 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003394 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003395
3396 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003397 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003398 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003399 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003400 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003401
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003402 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003403 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003404 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003405 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003406 wl_list_init(&shsurf->rotation.transform.link);
3407 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003408 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003409 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003410
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003411 /* We need to adjust the position of the surface
3412 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003413 cposx = shsurf->view->geometry.x + cx;
3414 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003415 dposx = rotate->center.x - cposx;
3416 dposy = rotate->center.y - cposy;
3417 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003418 weston_view_set_position(shsurf->view,
3419 shsurf->view->geometry.x + dposx,
3420 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003421 }
3422
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003423 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003424 * lazily applies the damage due to rotation update.
3425 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003426 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003427}
3428
3429static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003430rotate_grab_button(struct weston_pointer_grab *grab,
3431 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003432{
3433 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003434 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003435 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003436 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003437 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003438
Daniel Stone4dbadb12012-05-30 16:31:51 +01003439 if (pointer->button_count == 0 &&
3440 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003441 if (shsurf)
3442 weston_matrix_multiply(&shsurf->rotation.rotation,
3443 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003444 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003445 free(rotate);
3446 }
3447}
3448
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003449static void
3450rotate_grab_cancel(struct weston_pointer_grab *grab)
3451{
3452 struct rotate_grab *rotate =
3453 container_of(grab, struct rotate_grab, base.grab);
3454
3455 shell_grab_end(&rotate->base);
3456 free(rotate);
3457}
3458
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003459static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003460 noop_grab_focus,
3461 rotate_grab_motion,
3462 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003463 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003464 noop_grab_axis_source,
3465 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003466 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003467};
3468
3469static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003470surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003471{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003472 struct weston_surface *surface =
3473 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003474 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003475 float dx, dy;
3476 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003477
Pekka Paalanen460099f2012-01-20 16:48:25 +02003478 rotate = malloc(sizeof *rotate);
3479 if (!rotate)
3480 return;
3481
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003482 weston_view_to_global_float(shsurf->view,
3483 surface->width * 0.5f,
3484 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003486
Derek Foreman74de4692015-07-15 13:00:39 -05003487 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3488 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003489 r = sqrtf(dx * dx + dy * dy);
3490 if (r > 20.0f) {
3491 struct weston_matrix inverse;
3492
3493 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003494 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003495 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003496
3497 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003498 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003499 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003500 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003501 weston_matrix_init(&rotate->rotation);
3502 }
3503
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003504 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003505 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003506}
3507
3508static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003509rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003510 void *data)
3511{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003512 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003513 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003514 struct shell_surface *surface;
3515
Derek Foreman8ae2db52015-07-15 13:00:36 -05003516 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003517 return;
3518
Derek Foreman8ae2db52015-07-15 13:00:36 -05003519 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003520
Pekka Paalanen01388e22013-04-25 13:57:44 +03003521 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003522 if (base_surface == NULL)
3523 return;
3524
3525 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003526 if (surface == NULL ||
3527 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3528 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003529 return;
3530
Derek Foreman74de4692015-07-15 13:00:39 -05003531 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003532}
3533
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003534/* Move all fullscreen layers down to the current workspace and hide their
3535 * black views. The surfaces' state is set to both fullscreen and lowered,
3536 * and this is reversed when such a surface is re-configured, see
3537 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3538 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003539 * lowering_output = NULL - Lower on all outputs, else only lower on the
3540 * specified output.
3541 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003542 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003543 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003544void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003545lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003546 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003547{
3548 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003549 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003550
3551 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003553 &shell->fullscreen_layer.view_list.link,
3554 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003555 struct shell_surface *shsurf = get_shell_surface(view->surface);
3556
3557 if (!shsurf)
3558 continue;
3559
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003560 /* Only lower surfaces which have lowering_output as their fullscreen
3561 * output, unless a NULL output asks for lowering on all outputs.
3562 */
3563 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3564 continue;
3565
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003566 /* We can have a non-fullscreen popup for a fullscreen surface
3567 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003568 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003569 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003570 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3571 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003572 weston_view_damage_below(shsurf->fullscreen.black_view);
3573
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003574 }
3575
3576 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003577 weston_layer_entry_remove(&view->layer_link);
3578 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003579 weston_view_damage_below(view);
3580 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003581
3582 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003583 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003584}
3585
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003586void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003587activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003588 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003589{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003590 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003591 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003592 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003593 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003594 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003595 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003596
Pekka Paalanen01388e22013-04-25 13:57:44 +03003597 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003598 shsurf = get_shell_surface(main_surface);
3599 assert(shsurf);
3600
3601 /* Only demote fullscreen surfaces on the output of activated shsurf.
3602 * Leave fullscreen surfaces on unrelated outputs alone. */
3603 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003604
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003605 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003606
Jonas Ådahl8538b222012-08-29 22:13:03 +02003607 state = ensure_focus_state(shell, seat);
3608 if (state == NULL)
3609 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003610
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003611 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003612 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003613
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003614 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3615 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003616 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003617
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003618 /* Update the surface’s layer. This brings it to the top of the stacking
3619 * order as appropriate. */
3620 shell_surface_update_layer(shsurf);
3621
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003622 if (shell->focus_animation_type != ANIMATION_NONE) {
3623 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003624 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003625 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003626}
3627
Alex Wu21858432012-04-01 20:13:08 +08003628/* no-op func for checking black surface */
3629static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003630black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003631{
3632}
3633
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003634static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003635is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003636{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003637 struct weston_surface *surface = view->surface;
3638
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003639 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003640 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003641 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003642 return true;
3643 }
3644 return false;
3645}
3646
Kristian Høgsberg75840622011-09-06 13:48:16 -04003647static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003648activate_binding(struct weston_seat *seat,
3649 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003650 struct weston_view *focus_view,
3651 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003652{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003653 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003654 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003655
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003656 if (!focus_view)
3657 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003658
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003659 if (is_black_surface_view(focus_view, &main_view))
3660 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003661
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003662 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003663 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003664 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003665
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003666 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003667}
3668
3669static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003670click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
3671 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003672{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003673 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003674 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003675 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003676 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003677
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003678 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003679 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003680 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003681}
3682
3683static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003684touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
3685 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003686{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003687 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003688 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003689 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003690 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003691
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003692 activate_binding(touch->seat, data, touch->focus,
3693 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003694}
3695
3696static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003697unfocus_all_seats(struct desktop_shell *shell)
3698{
3699 struct weston_seat *seat, *next;
3700
3701 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003702 struct weston_keyboard *keyboard =
3703 weston_seat_get_keyboard(seat);
3704
3705 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003706 continue;
3707
Derek Foreman1281a362015-07-31 16:55:32 -05003708 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003709 }
3710}
3711
3712static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003713lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003714{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003715 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003716
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003717 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003718 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003719 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003720 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003721
3722 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003723
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003724 /* Hide all surfaces by removing the fullscreen, panel and
3725 * toplevel layers. This way nothing else can show or receive
3726 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003727
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003728 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003729 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003730 if (shell->showing_input_panels)
3731 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003732 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003733 wl_list_insert(&shell->compositor->cursor_layer.link,
3734 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003735
Derek Foreman004b4a12015-07-20 16:28:13 -05003736 weston_compositor_sleep(shell->compositor);
3737
Neil Robertsb4a91702014-04-09 16:33:32 +01003738 /* Remove the keyboard focus on all seats. This will be
3739 * restored to the workspace's saved state via
3740 * restore_focus_state when the compositor is unlocked */
3741 unfocus_all_seats(shell);
3742
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003743 /* TODO: disable bindings that should not work while locked. */
3744
3745 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003746}
3747
3748static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003749unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003750{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003751 struct wl_resource *shell_resource;
3752
Pekka Paalanend81c2162011-11-16 13:47:34 +02003753 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003754 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003755 return;
3756 }
3757
3758 /* If desktop-shell client has gone away, unlock immediately. */
3759 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003760 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003761 return;
3762 }
3763
3764 if (shell->prepare_event_sent)
3765 return;
3766
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003767 shell_resource = shell->child.desktop_shell;
3768 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003769 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003770}
3771
3772static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003773shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003774{
3775 struct desktop_shell *shell = data;
3776
3777 shell->fade.animation = NULL;
3778
3779 switch (shell->fade.type) {
3780 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05003781 weston_surface_destroy(shell->fade.view->surface);
3782 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003783 break;
3784 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003785 lock(shell);
3786 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003787 default:
3788 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003789 }
3790}
3791
Jason Ekstranda7af7042013-10-12 22:38:11 -05003792static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003793shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003794{
3795 struct weston_compositor *compositor = shell->compositor;
3796 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003797 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003798
3799 surface = weston_surface_create(compositor);
3800 if (!surface)
3801 return NULL;
3802
Jason Ekstranda7af7042013-10-12 22:38:11 -05003803 view = weston_view_create(surface);
3804 if (!view) {
3805 weston_surface_destroy(surface);
3806 return NULL;
3807 }
3808
Jason Ekstrand5c11a332013-12-04 20:32:03 -06003809 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003810 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003811 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003812 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3813 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003814 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003815 surface->is_mapped = true;
3816 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003817
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003819}
3820
3821static void
3822shell_fade(struct desktop_shell *shell, enum fade_type type)
3823{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003824 float tint;
3825
3826 switch (type) {
3827 case FADE_IN:
3828 tint = 0.0;
3829 break;
3830 case FADE_OUT:
3831 tint = 1.0;
3832 break;
3833 default:
3834 weston_log("shell: invalid fade type\n");
3835 return;
3836 }
3837
3838 shell->fade.type = type;
3839
Jason Ekstranda7af7042013-10-12 22:38:11 -05003840 if (shell->fade.view == NULL) {
3841 shell->fade.view = shell_fade_create_surface(shell);
3842 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003843 return;
3844
Jason Ekstranda7af7042013-10-12 22:38:11 -05003845 shell->fade.view->alpha = 1.0 - tint;
3846 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003847 }
3848
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003849 if (shell->fade.view->output == NULL) {
3850 /* If the black view gets a NULL output, we lost the
3851 * last output and we'll just cancel the fade. This
3852 * happens when you close the last window under the
3853 * X11 or Wayland backends. */
3854 shell->locked = false;
3855 weston_surface_destroy(shell->fade.view->surface);
3856 shell->fade.view = NULL;
3857 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003858 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003859 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003860 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05003861 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04003862 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003863 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003864 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003865}
3866
3867static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003868do_shell_fade_startup(void *data)
3869{
3870 struct desktop_shell *shell = data;
3871
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003872 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003873 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003874 } else {
3875 weston_log("desktop shell: "
3876 "unexpected fade-in animation type %d\n",
3877 shell->startup_animation_type);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003878 weston_surface_destroy(shell->fade.view->surface);
3879 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003880 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003881}
3882
3883static void
3884shell_fade_startup(struct desktop_shell *shell)
3885{
3886 struct wl_event_loop *loop;
3887
3888 if (!shell->fade.startup_timer)
3889 return;
3890
3891 wl_event_source_remove(shell->fade.startup_timer);
3892 shell->fade.startup_timer = NULL;
3893
3894 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3895 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3896}
3897
3898static int
3899fade_startup_timeout(void *data)
3900{
3901 struct desktop_shell *shell = data;
3902
3903 shell_fade_startup(shell);
3904 return 0;
3905}
3906
3907static void
3908shell_fade_init(struct desktop_shell *shell)
3909{
3910 /* Make compositor output all black, and wait for the desktop-shell
3911 * client to signal it is ready, then fade in. The timer triggers a
3912 * fade-in, in case the desktop-shell client takes too long.
3913 */
3914
3915 struct wl_event_loop *loop;
3916
Jason Ekstranda7af7042013-10-12 22:38:11 -05003917 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003918 weston_log("%s: warning: fade surface already exists\n",
3919 __func__);
3920 return;
3921 }
3922
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003923 if (shell->startup_animation_type == ANIMATION_NONE)
3924 return;
3925
Jason Ekstranda7af7042013-10-12 22:38:11 -05003926 shell->fade.view = shell_fade_create_surface(shell);
3927 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003928 return;
3929
Jason Ekstranda7af7042013-10-12 22:38:11 -05003930 weston_view_update_transform(shell->fade.view);
3931 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003932
3933 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3934 shell->fade.startup_timer =
3935 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3936 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3937}
3938
3939static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003940idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003941{
3942 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003943 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003944
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003945 struct weston_seat *seat;
3946
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003947 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3948 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003949
3950 shell_fade(shell, FADE_OUT);
3951 /* lock() is called from shell_fade_done() */
3952}
3953
3954static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003955wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003956{
3957 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003958 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003959
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003960 unlock(shell);
3961}
3962
3963static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003964transform_handler(struct wl_listener *listener, void *data)
3965{
3966 struct weston_surface *surface = data;
3967 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003968 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003969 int x, y;
3970
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003971 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003972 return;
3973
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003974 api = shsurf->shell->xwayland_surface_api;
3975 if (!api) {
3976 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
3977 shsurf->shell->xwayland_surface_api = api;
3978 }
3979
3980 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003981 return;
3982
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003983 if (!weston_view_is_mapped(shsurf->view))
3984 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003985
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003986 x = shsurf->view->geometry.x;
3987 y = shsurf->view->geometry.y;
3988
3989 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003990}
3991
3992static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05003993center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003994{
Giulio Camuffob8366642013-04-25 13:57:46 +03003995 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003996 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003997
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03003999
4000 x = output->x + (output->width - width) / 2 - surf_x / 2;
4001 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004002
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004003 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004004}
4005
4006static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004007weston_view_set_initial_position(struct weston_view *view,
4008 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004009{
4010 struct weston_compositor *compositor = shell->compositor;
4011 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004012 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004013 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004014 struct weston_output *output, *target_output = NULL;
4015 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004016 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004017
4018 /* As a heuristic place the new window on the same output as the
4019 * pointer. Falling back to the output containing 0, 0.
4020 *
4021 * TODO: Do something clever for touch too?
4022 */
4023 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004024 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4025
4026 if (pointer) {
4027 ix = wl_fixed_to_int(pointer->x);
4028 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004029 break;
4030 }
4031 }
4032
4033 wl_list_for_each(output, &compositor->output_list, link) {
4034 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4035 target_output = output;
4036 break;
4037 }
4038 }
4039
4040 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004041 weston_view_set_position(view, 10 + random() % 400,
4042 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004043 return;
4044 }
4045
4046 /* Valid range within output where the surface will still be onscreen.
4047 * If this is negative it means that the surface is bigger than
4048 * output.
4049 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004050 get_output_work_area(shell, target_output, &area);
4051
Derek Foremanf814c5d2015-04-15 12:23:54 -05004052 x = area.x;
4053 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004054 range_x = area.width - view->surface->width;
4055 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004056
Rob Bradford4cb88c72012-08-13 15:18:44 +01004057 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004058 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004059
4060 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004061 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004062
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004064}
4065
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004066static bool
4067check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4068{
4069 struct timespec now;
4070
4071 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4072 return false;
4073
4074 /*
4075 * If the shell helper client dies before the session has been
4076 * up for roughly 30 seconds, better just make Weston shut down,
4077 * because the user likely has no way to interact with the desktop
4078 * anyway.
4079 */
4080 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4081 weston_log("Error: %s apparently cannot run at all.\n",
4082 shell->client);
4083 weston_log_continue(STAMP_SPACE "Quitting...");
4084 wl_display_terminate(shell->compositor->wl_display);
4085
4086 return true;
4087 }
4088
4089 return false;
4090}
4091
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004092static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004093
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004094static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004095respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004096{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004097 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004098
4099 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4100 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004101 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004102 shell->child.deathstamp = time;
4103 shell->child.deathcount = 0;
4104 }
4105
4106 shell->child.deathcount++;
4107 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004108 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004109 return;
4110 }
4111
Pekka Paalanen826dc142014-08-27 12:11:53 +03004112 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004113 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004114}
4115
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004116static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004117desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4118{
4119 struct desktop_shell *shell;
4120
4121 shell = container_of(listener, struct desktop_shell,
4122 child.client_destroy_listener);
4123
Pekka Paalanen826dc142014-08-27 12:11:53 +03004124 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004125 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004126 /*
4127 * unbind_desktop_shell() will reset shell->child.desktop_shell
4128 * before the respawned process has a chance to create a new
4129 * desktop_shell object, because we are being called from the
4130 * wl_client destructor which destroys all wl_resources before
4131 * returning.
4132 */
4133
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004134 if (!check_desktop_shell_crash_too_early(shell))
4135 respawn_desktop_shell_process(shell);
4136
Pekka Paalanen826dc142014-08-27 12:11:53 +03004137 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004138}
4139
4140static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004141launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004142{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004143 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004144
Pekka Paalanen826dc142014-08-27 12:11:53 +03004145 shell->child.client = weston_client_start(shell->compositor,
4146 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004147
Arnaud Vrac42631192014-08-25 20:56:46 +02004148 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004149 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004150 return;
4151 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004152
4153 shell->child.client_destroy_listener.notify =
4154 desktop_shell_client_destroy;
4155 wl_client_add_destroy_listener(shell->child.client,
4156 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004157}
4158
4159static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004160unbind_desktop_shell(struct wl_resource *resource)
4161{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004162 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004163
4164 if (shell->locked)
4165 resume_desktop(shell);
4166
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004167 shell->child.desktop_shell = NULL;
4168 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004169}
4170
4171static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004172bind_desktop_shell(struct wl_client *client,
4173 void *data, uint32_t version, uint32_t id)
4174{
Tiago Vignattibe143262012-04-16 17:31:41 +03004175 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004176 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004177
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004178 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4179 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004180
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004181 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004182 wl_resource_set_implementation(resource,
4183 &desktop_shell_implementation,
4184 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004185 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004186 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004187 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004188
4189 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4190 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004191}
4192
Kristian Høgsberg07045392012-02-19 18:52:44 -05004193struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004194 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004195 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004196 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004197 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004198 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004199};
4200
4201static void
4202switcher_next(struct switcher *switcher)
4203{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004204 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004205 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004206 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004207 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004208
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004209 /* temporary re-display minimized surfaces */
4210 struct weston_view *tmp;
4211 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004212 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4213 weston_layer_entry_remove(&view->layer_link);
4214 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004215 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4216 *minimized = view;
4217 }
4218
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004219 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004220 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004221 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004222 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004223 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004224 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004225 next = view;
4226 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004227 view->alpha = 0.25;
4228 weston_view_geometry_dirty(view);
4229 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004230 }
Alex Wu1659daa2012-04-01 20:13:09 +08004231
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004232 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004233 view->alpha = 0.25;
4234 weston_view_geometry_dirty(view);
4235 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004236 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004237 }
4238
4239 if (next == NULL)
4240 next = first;
4241
Alex Wu07b26062012-03-12 16:06:01 +08004242 if (next == NULL)
4243 return;
4244
Kristian Høgsberg07045392012-02-19 18:52:44 -05004245 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004246 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004247
4248 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004249 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004250 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004251
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004252 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004253 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004254 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004255}
4256
4257static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004258switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004259{
4260 struct switcher *switcher =
4261 container_of(listener, struct switcher, listener);
4262
4263 switcher_next(switcher);
4264}
4265
4266static void
Daniel Stone351eb612012-05-31 15:27:47 -04004267switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004268{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004269 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004270 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004271 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004272
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004273 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004274 if (is_focus_view(view))
4275 continue;
4276
Jason Ekstranda7af7042013-10-12 22:38:11 -05004277 view->alpha = 1.0;
4278 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004279 }
4280
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004281 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004282 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004283 keyboard->seat,
4284 WESTON_ACTIVATE_FLAG_CONFIGURE);
4285 }
4286
Kristian Høgsberg07045392012-02-19 18:52:44 -05004287 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004288 weston_keyboard_end_grab(keyboard);
4289 if (keyboard->input_method_resource)
4290 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004291
4292 /* re-hide surfaces that were temporary shown during the switch */
4293 struct weston_view **minimized;
4294 wl_array_for_each(minimized, &switcher->minimized_array) {
4295 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004296 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004297 weston_layer_entry_remove(&(*minimized)->layer_link);
4298 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004299 weston_view_damage_below(*minimized);
4300 }
4301 }
4302 wl_array_release(&switcher->minimized_array);
4303
Kristian Høgsberg07045392012-02-19 18:52:44 -05004304 free(switcher);
4305}
4306
4307static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004308switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004309 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004310{
4311 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004312 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004313
Daniel Stonec9785ea2012-05-30 16:31:52 +01004314 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004315 switcher_next(switcher);
4316}
4317
4318static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004319switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004320 uint32_t mods_depressed, uint32_t mods_latched,
4321 uint32_t mods_locked, uint32_t group)
4322{
4323 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004324 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004325
Daniel Stone351eb612012-05-31 15:27:47 -04004326 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4327 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004328}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004329
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004330static void
4331switcher_cancel(struct weston_keyboard_grab *grab)
4332{
4333 struct switcher *switcher = container_of(grab, struct switcher, grab);
4334
4335 switcher_destroy(switcher);
4336}
4337
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004338static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004339 switcher_key,
4340 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004341 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004342};
4343
4344static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004345switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4346 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004347{
Tiago Vignattibe143262012-04-16 17:31:41 +03004348 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004349 struct switcher *switcher;
4350
4351 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004352 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004353 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004354 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004355 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004356 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004357
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004358 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004359 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004360 weston_keyboard_start_grab(keyboard, &switcher->grab);
4361 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004362 switcher_next(switcher);
4363}
4364
Pekka Paalanen3c647232011-12-22 13:43:43 +02004365static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004366backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4367 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004368{
4369 struct weston_compositor *compositor = data;
4370 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004371 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004372
4373 /* TODO: we're limiting to simple use cases, where we assume just
4374 * control on the primary display. We'd have to extend later if we
4375 * ever get support for setting backlights on random desktop LCD
4376 * panels though */
4377 output = get_default_output(compositor);
4378 if (!output)
4379 return;
4380
4381 if (!output->set_backlight)
4382 return;
4383
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004384 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4385 backlight_new = output->backlight_current - 25;
4386 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4387 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004388
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004389 if (backlight_new < 5)
4390 backlight_new = 5;
4391 if (backlight_new > 255)
4392 backlight_new = 255;
4393
4394 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004395 output->set_backlight(output, output->backlight_current);
4396}
4397
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004398static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004399force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4400 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004401{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004402 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004403 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004404 struct desktop_shell *shell = data;
4405 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004406 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004407
Derek Foreman8ae2db52015-07-15 13:00:36 -05004408 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004409 if (!focus_surface)
4410 return;
4411
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004412 wl_signal_emit(&compositor->kill_signal, focus_surface);
4413
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004414 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004415 wl_client_get_credentials(client, &pid, NULL, NULL);
4416
4417 /* Skip clients that we launched ourselves (the credentials of
4418 * the socketpair is ours) */
4419 if (pid == getpid())
4420 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004421
Daniel Stone325fc2d2012-05-30 16:31:58 +01004422 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004423}
4424
4425static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004426workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004427 uint32_t key, void *data)
4428{
4429 struct desktop_shell *shell = data;
4430 unsigned int new_index = shell->workspaces.current;
4431
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004432 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004433 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004434 if (new_index != 0)
4435 new_index--;
4436
4437 change_workspace(shell, new_index);
4438}
4439
4440static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004441workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004442 uint32_t key, void *data)
4443{
4444 struct desktop_shell *shell = data;
4445 unsigned int new_index = shell->workspaces.current;
4446
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004447 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004448 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004449 if (new_index < shell->workspaces.num - 1)
4450 new_index++;
4451
4452 change_workspace(shell, new_index);
4453}
4454
4455static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004456workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004457 uint32_t key, void *data)
4458{
4459 struct desktop_shell *shell = data;
4460 unsigned int new_index;
4461
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004462 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004463 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004464 new_index = key - KEY_F1;
4465 if (new_index >= shell->workspaces.num)
4466 new_index = shell->workspaces.num - 1;
4467
4468 change_workspace(shell, new_index);
4469}
4470
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004471static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004472workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4473 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004474{
4475 struct desktop_shell *shell = data;
4476 unsigned int new_index = shell->workspaces.current;
4477
4478 if (shell->locked)
4479 return;
4480
4481 if (new_index != 0)
4482 new_index--;
4483
Derek Foreman8ae2db52015-07-15 13:00:36 -05004484 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004485}
4486
4487static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004488workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4489 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004490{
4491 struct desktop_shell *shell = data;
4492 unsigned int new_index = shell->workspaces.current;
4493
4494 if (shell->locked)
4495 return;
4496
4497 if (new_index < shell->workspaces.num - 1)
4498 new_index++;
4499
Derek Foreman8ae2db52015-07-15 13:00:36 -05004500 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004501}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004502
4503static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004504shell_reposition_view_on_output_destroy(struct weston_view *view)
4505{
4506 struct weston_output *output, *first_output;
4507 struct weston_compositor *ec = view->surface->compositor;
4508 struct shell_surface *shsurf;
4509 float x, y;
4510 int visible;
4511
4512 x = view->geometry.x;
4513 y = view->geometry.y;
4514
4515 /* At this point the destroyed output is not in the list anymore.
4516 * If the view is still visible somewhere, we leave where it is,
4517 * otherwise, move it to the first output. */
4518 visible = 0;
4519 wl_list_for_each(output, &ec->output_list, link) {
4520 if (pixman_region32_contains_point(&output->region,
4521 x, y, NULL)) {
4522 visible = 1;
4523 break;
4524 }
4525 }
4526
4527 if (!visible) {
4528 first_output = container_of(ec->output_list.next,
4529 struct weston_output, link);
4530
4531 x = first_output->x + first_output->width / 4;
4532 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004533
4534 weston_view_set_position(view, x, y);
4535 } else {
4536 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004537 }
4538
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004539
4540 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004541 if (!shsurf)
4542 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004543
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004544 shsurf->saved_position_valid = false;
4545 set_maximized(shsurf, false);
4546 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004547}
4548
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004549void
4550shell_for_each_layer(struct desktop_shell *shell,
4551 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004552{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004553 struct workspace **ws;
4554
4555 func(shell, &shell->fullscreen_layer, data);
4556 func(shell, &shell->panel_layer, data);
4557 func(shell, &shell->background_layer, data);
4558 func(shell, &shell->lock_layer, data);
4559 func(shell, &shell->input_panel_layer, data);
4560
4561 wl_array_for_each(ws, &shell->workspaces.array)
4562 func(shell, &(*ws)->layer, data);
4563}
4564
4565static void
4566shell_output_destroy_move_layer(struct desktop_shell *shell,
4567 struct weston_layer *layer,
4568 void *data)
4569{
4570 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004571 struct weston_view *view;
4572
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004573 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004574 if (view->output != output)
4575 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004576
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004577 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004578 }
4579}
4580
4581static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004582handle_output_destroy(struct wl_listener *listener, void *data)
4583{
4584 struct shell_output *output_listener =
4585 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004586 struct weston_output *output = output_listener->output;
4587 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004588
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004589 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004590
Xiong Zhang6b481422013-10-23 13:58:32 +08004591 wl_list_remove(&output_listener->destroy_listener.link);
4592 wl_list_remove(&output_listener->link);
4593 free(output_listener);
4594}
4595
4596static void
David Fort50d962f2016-06-09 17:55:52 +02004597shell_resize_surface_to_output(struct desktop_shell *shell,
4598 struct weston_surface *surface,
4599 const struct weston_output *output)
4600{
4601 if (!surface)
4602 return;
4603
4604 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4605 surface->resource,
4606 output->width,
4607 output->height);
4608}
4609
4610
4611static void
4612handle_output_resized(struct wl_listener *listener, void *data)
4613{
4614 struct desktop_shell *shell =
4615 container_of(listener, struct desktop_shell, resized_listener);
4616 struct weston_output *output = (struct weston_output *)data;
4617 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4618
4619 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4620 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4621}
4622
4623static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004624create_shell_output(struct desktop_shell *shell,
4625 struct weston_output *output)
4626{
4627 struct shell_output *shell_output;
4628
4629 shell_output = zalloc(sizeof *shell_output);
4630 if (shell_output == NULL)
4631 return;
4632
4633 shell_output->output = output;
4634 shell_output->shell = shell;
4635 shell_output->destroy_listener.notify = handle_output_destroy;
4636 wl_signal_add(&output->destroy_signal,
4637 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004638 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004639}
4640
4641static void
4642handle_output_create(struct wl_listener *listener, void *data)
4643{
4644 struct desktop_shell *shell =
4645 container_of(listener, struct desktop_shell, output_create_listener);
4646 struct weston_output *output = (struct weston_output *)data;
4647
4648 create_shell_output(shell, output);
4649}
4650
4651static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004652handle_output_move_layer(struct desktop_shell *shell,
4653 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004654{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004655 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004656 struct weston_view *view;
4657 float x, y;
4658
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004659 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004660 if (view->output != output)
4661 continue;
4662
4663 x = view->geometry.x + output->move_x;
4664 y = view->geometry.y + output->move_y;
4665 weston_view_set_position(view, x, y);
4666 }
4667}
4668
4669static void
4670handle_output_move(struct wl_listener *listener, void *data)
4671{
4672 struct desktop_shell *shell;
4673
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004674 shell = container_of(listener, struct desktop_shell,
4675 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004676
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004677 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004678}
4679
4680static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004681setup_output_destroy_handler(struct weston_compositor *ec,
4682 struct desktop_shell *shell)
4683{
4684 struct weston_output *output;
4685
4686 wl_list_init(&shell->output_list);
4687 wl_list_for_each(output, &ec->output_list, link)
4688 create_shell_output(shell, output);
4689
4690 shell->output_create_listener.notify = handle_output_create;
4691 wl_signal_add(&ec->output_created_signal,
4692 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004693
4694 shell->output_move_listener.notify = handle_output_move;
4695 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004696}
4697
4698static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004699shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004700{
Tiago Vignattibe143262012-04-16 17:31:41 +03004701 struct desktop_shell *shell =
4702 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004703 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004704 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004705
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004706 /* Force state to unlocked so we don't try to fade */
4707 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004708
4709 if (shell->child.client) {
4710 /* disable respawn */
4711 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004712 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004713 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004714
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004715 wl_list_remove(&shell->idle_listener.link);
4716 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004717 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004718
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004719 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004720 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004721
Xiong Zhang6b481422013-10-23 13:58:32 +08004722 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4723 wl_list_remove(&shell_output->destroy_listener.link);
4724 wl_list_remove(&shell_output->link);
4725 free(shell_output);
4726 }
4727
4728 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004729 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004730 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004731
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004732 wl_array_for_each(ws, &shell->workspaces.array)
4733 workspace_destroy(*ws);
4734 wl_array_release(&shell->workspaces.array);
4735
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004736 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004737 free(shell);
4738}
4739
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004740static void
4741shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4742{
4743 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004744 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004745
Bob Ham744e6532016-01-12 10:21:48 +00004746 if (shell->allow_zap)
4747 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4748 MODIFIER_CTRL | MODIFIER_ALT,
4749 terminate_binding, ec);
4750
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004751 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004752 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4753 click_to_activate_binding,
4754 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004755 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4756 click_to_activate_binding,
4757 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004758 weston_compositor_add_touch_binding(ec, 0,
4759 touch_to_activate_binding,
4760 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004761 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4762 MODIFIER_SUPER | MODIFIER_ALT,
4763 surface_opacity_binding, NULL);
4764 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4765 MODIFIER_SUPER, zoom_axis_binding,
4766 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004767 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4768 backlight_binding, ec);
4769 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4770 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004771
4772 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004773 if (shell->exposay_modifier)
4774 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4775 exposay_binding, shell);
4776
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004777 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004778 if (!mod)
4779 return;
4780
Daniel Stone325fc2d2012-05-30 16:31:58 +01004781 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4782 zoom_key_binding, NULL);
4783 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4784 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004785 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4786 maximize_binding, NULL);
4787 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4788 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004789 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4790 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004791 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004792 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004793 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004794 weston_compositor_add_button_binding(ec, BTN_LEFT,
4795 mod | MODIFIER_SHIFT,
4796 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004797
4798 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004799 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004800 rotate_binding, NULL);
4801
Daniel Stone325fc2d2012-05-30 16:31:58 +01004802 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4803 shell);
4804 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4805 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004806 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4807 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004808 weston_compositor_add_key_binding(ec, KEY_K, mod,
4809 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004810 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4811 workspace_up_binding, shell);
4812 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4813 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004814 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4815 workspace_move_surface_up_binding,
4816 shell);
4817 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4818 workspace_move_surface_down_binding,
4819 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004820
4821 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4822 if (shell->workspaces.num > 1) {
4823 num_workspace_bindings = shell->workspaces.num;
4824 if (num_workspace_bindings > 6)
4825 num_workspace_bindings = 6;
4826 for (i = 0; i < num_workspace_bindings; i++)
4827 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4828 workspace_f_binding,
4829 shell);
4830 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004831
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004832 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004833}
4834
Jason Ekstrand024177c2014-04-21 19:42:58 -05004835static void
4836handle_seat_created(struct wl_listener *listener, void *data)
4837{
4838 struct weston_seat *seat = data;
4839
4840 create_shell_seat(seat);
4841}
4842
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004843WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004844module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004845 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004846{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004847 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004848 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004849 struct workspace **pws;
4850 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004851 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004852
Peter Huttererf3d62272013-08-08 11:57:05 +10004853 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004854 if (shell == NULL)
4855 return -1;
4856
Kristian Høgsberg75840622011-09-06 13:48:16 -04004857 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004858
4859 shell->destroy_listener.notify = shell_destroy;
4860 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004861 shell->idle_listener.notify = idle_handler;
4862 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4863 shell->wake_listener.notify = wake_handler;
4864 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004865 shell->transform_listener.notify = transform_handler;
4866 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004867
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004868 ec->shell_interface.shell = shell;
Quentin Glidic5c201952016-03-23 13:50:49 +01004869 ec->shell_interface.get_output_work_area = get_output_work_area;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004870
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004871 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4872 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004873 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4874 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004875 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004876
4877 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004878 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004879
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004880 if (input_panel_setup(shell) < 0)
4881 return -1;
4882
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004883 shell->text_backend = text_backend_init(ec);
4884 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004885 return -1;
4886
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004887 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004888
Daniel Stonedf8133b2013-11-19 11:37:14 +01004889 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4890 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4891
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004892 for (i = 0; i < shell->workspaces.num; i++) {
4893 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4894 if (pws == NULL)
4895 return -1;
4896
4897 *pws = workspace_create();
4898 if (*pws == NULL)
4899 return -1;
4900 }
4901 activate_workspace(shell, 0);
4902
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004903 weston_layer_init(&shell->minimized_layer, NULL);
4904
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004905 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004906 wl_list_init(&shell->workspaces.animation.link);
4907 shell->workspaces.animation.frame = animate_workspace_change_frame;
4908
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004909 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4910 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004911 return -1;
4912
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004913 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004914 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004915 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004916 return -1;
4917
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004918 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004919
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004920 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004921
Xiong Zhang6b481422013-10-23 13:58:32 +08004922 setup_output_destroy_handler(ec, shell);
4923
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004924 loop = wl_display_get_event_loop(ec->wl_display);
4925 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004926
Jason Ekstrand024177c2014-04-21 19:42:58 -05004927 wl_list_for_each(seat, &ec->seat_list, link)
4928 handle_seat_created(NULL, seat);
4929 shell->seat_create_listener.notify = handle_seat_created;
4930 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004931
David Fort50d962f2016-06-09 17:55:52 +02004932 shell->resized_listener.notify = handle_output_resized;
4933 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4934
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004935 screenshooter_create(ec);
4936
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004937 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004938
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004939 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004940
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004941 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4942
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004943 return 0;
4944}