blob: 9a965608ab93d64c4cbeadea171adae2d1e737e4 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020044#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050045
Jonas Ådahle3cddce2012-06-13 00:01:22 +020046#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020047#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010049#ifndef static_assert
50#define static_assert(cond, msg)
51#endif
52
Jonas Ådahl04769742012-06-13 00:01:24 +020053struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020054 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040056 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020057 struct weston_surface *keyboard_focus;
58 struct wl_list link;
59 struct wl_listener seat_destroy_listener;
60 struct wl_listener surface_destroy_listener;
61};
62
Philip Withnall648a4dd2013-11-25 18:01:44 +000063/*
64 * Surface stacking and ordering.
65 *
66 * This is handled using several linked lists of surfaces, organised into
67 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
68 * above all of the surfaces in the layer below. The set of layers is static and
69 * in the following order (top-most first):
70 * • Lock layer (only ever displayed on its own)
71 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010072 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Fullscreen layer
74 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000075 * • Workspace layers
76 * • Background layer
77 *
78 * The list of layers may be manipulated to remove whole layers of surfaces from
79 * display. For example, when locking the screen, all layers except the lock
80 * layer are removed.
81 *
82 * A surface’s layer is modified on configuring the surface, in
83 * set_surface_type() (which is only called when the surface’s type change is
84 * _committed_). If a surface’s type changes (e.g. when making a window
85 * fullscreen) its layer changes too.
86 *
87 * In order to allow popup and transient surfaces to be correctly stacked above
88 * their parent surfaces, each surface tracks both its parent surface, and a
89 * linked list of its children. When a surface’s layer is updated, so are the
90 * layers of its children. Note that child surfaces are *not* the same as
91 * subsurfaces — child/parent surfaces are purely for maintaining stacking
92 * order.
93 *
94 * The children_link list of siblings of a surface (i.e. those surfaces which
95 * have the same parent) only contains weston_surfaces which have a
96 * shell_surface. Stacking is not implemented for non-shell_surface
97 * weston_surfaces. This means that the following implication does *not* hold:
98 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
99 */
100
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200101struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500102 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200103
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200104 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600106 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700107
Tiago Vignattibe143262012-04-16 17:31:41 +0300108 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800111 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800112 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700113 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800114 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800122 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800124 } fullscreen;
125
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 struct weston_transform workspace_transform;
127
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500128 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_output *output;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200130
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700131 struct surface_state {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100132 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200133 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700134
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500135 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500136
137 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200138};
139
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300140struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400141 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300142 struct shell_surface *shsurf;
143 struct wl_listener shsurf_destroy_listener;
144};
145
Rusty Lynch1084da52013-08-15 09:10:08 -0700146struct shell_touch_grab {
147 struct weston_touch_grab grab;
148 struct shell_surface *shsurf;
149 struct wl_listener shsurf_destroy_listener;
150 struct weston_touch *touch;
151};
152
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300153struct weston_move_grab {
154 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100155 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500156 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500157};
158
Rusty Lynch1084da52013-08-15 09:10:08 -0700159struct weston_touch_move_grab {
160 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800161 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700162 wl_fixed_t dx, dy;
163};
164
Pekka Paalanen460099f2012-01-20 16:48:25 +0200165struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300166 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500167 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200168 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200169 float x;
170 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171 } center;
172};
173
Giulio Camuffo5085a752013-03-25 21:42:45 +0100174struct shell_seat {
175 struct weston_seat *seat;
176 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500177 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100178
Jason Ekstrand024177c2014-04-21 19:42:58 -0500179 struct wl_listener caps_changed_listener;
180 struct wl_listener pointer_focus_listener;
181 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100182};
183
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800184
Alex Wubd3354b2012-04-17 17:20:49 +0800185static struct desktop_shell *
186shell_surface_get_shell(struct shell_surface *shsurf);
187
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500188static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200189set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
190
191static void
Derek Foreman74de4692015-07-15 13:00:39 -0500192surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500193
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300194static void
195shell_fade_startup(struct desktop_shell *shell);
196
Bryce Harringtonfde5adb2016-08-10 17:25:24 -0700197static void
198shell_fade(struct desktop_shell *shell, enum fade_type type);
199
Philip Withnallbecb77e2013-11-25 18:01:30 +0000200static struct shell_seat *
201get_shell_seat(struct weston_seat *seat);
202
Jonny Lambd73c6942014-08-20 15:53:20 +0200203static void
204get_output_panel_size(struct desktop_shell *shell,
205 struct weston_output *output,
206 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700207
Philip Withnall648a4dd2013-11-25 18:01:44 +0000208static void
209shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
210
Pekka Paalanen8274d902014-08-06 19:36:51 +0300211static int
212shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
213{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300214 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200215 struct weston_desktop_surface *desktop_surface =
216 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300217
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200218 t = weston_desktop_surface_get_title(desktop_surface);
219 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220
221 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200222 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223 t ? " '" : "", t ?: "", t ? "'" : "",
224 c ? " of " : "", c ?: "");
225}
226
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500227static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400228destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300229{
230 struct shell_grab *grab;
231
232 grab = container_of(listener, struct shell_grab,
233 shsurf_destroy_listener);
234
235 grab->shsurf = NULL;
236}
237
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800238struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500239get_default_view(struct weston_surface *surface)
240{
241 struct shell_surface *shsurf;
242 struct weston_view *view;
243
244 if (!surface || wl_list_empty(&surface->views))
245 return NULL;
246
247 shsurf = get_shell_surface(surface);
248 if (shsurf)
249 return shsurf->view;
250
251 wl_list_for_each(view, &surface->views, surface_link)
252 if (weston_view_is_mapped(view))
253 return view;
254
255 return container_of(surface->views.next, struct weston_view, surface_link);
256}
257
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300259shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400260 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300261 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400262 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800263 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300264{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300265 struct desktop_shell *shell = shsurf->shell;
266
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200267 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400268
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269 grab->grab.interface = interface;
270 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400271 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500272 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400273 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700275 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400276 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400277 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800278 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400279 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280 weston_pointer_set_focus(pointer,
281 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400282 wl_fixed_from_int(0),
283 wl_fixed_from_int(0));
284 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300285}
286
Jonny Lambd73c6942014-08-20 15:53:20 +0200287static void
288get_output_panel_size(struct desktop_shell *shell,
289 struct weston_output *output,
290 int *width,
291 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700292{
293 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200294
295 *width = 0;
296 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700297
298 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200299 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700300
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300301 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jonny Lambd73c6942014-08-20 15:53:20 +0200302 float x, y;
303
304 if (view->surface->output != output)
305 continue;
306
307 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800308 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
309 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200310 weston_view_to_global_float(view,
311 view->surface->width, 0,
312 &x, &y);
313
Derek Foreman612341f2015-04-15 12:23:55 -0500314 *width = (int)x - output->x;
315 *height = view->surface->height + (int) y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200316 return;
317
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800318 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
319 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200320 weston_view_to_global_float(view,
321 0, view->surface->height,
322 &x, &y);
323
Derek Foreman612341f2015-04-15 12:23:55 -0500324 *width = view->surface->width + (int)x - output->x;
325 *height = (int)y - output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200326 return;
327
328 default:
329 /* we've already set width and height to
330 * fallback values. */
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700331 break;
332 }
333 }
334
Jonny Lambd73c6942014-08-20 15:53:20 +0200335 /* the correct view wasn't found */
336}
337
338static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200339get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200340 struct weston_output *output,
341 pixman_rectangle32_t *area)
342{
343 int32_t panel_width = 0, panel_height = 0;
344
Derek Foremanf814c5d2015-04-15 12:23:54 -0500345 area->x = output->x;
346 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200347
348 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200349 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800350 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200351 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500352 area->y += panel_height;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800353 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200354 area->width = output->width;
355 area->height = output->height - panel_height;
356 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800357 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500358 area->x += panel_width;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 area->width = output->width - panel_width;
361 area->height = output->height;
362 break;
363 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700364}
365
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400366static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300367shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300368{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700369 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400370 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700371 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400372
373 if (grab->shsurf->resize_edges) {
374 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400375 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700376 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300377
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700378 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300379}
380
381static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700382shell_touch_grab_start(struct shell_touch_grab *grab,
383 const struct weston_touch_grab_interface *interface,
384 struct shell_surface *shsurf,
385 struct weston_touch *touch)
386{
387 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800388
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200389 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700390
Rusty Lynch1084da52013-08-15 09:10:08 -0700391 grab->grab.interface = interface;
392 grab->shsurf = shsurf;
393 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
394 wl_signal_add(&shsurf->destroy_signal,
395 &grab->shsurf_destroy_listener);
396
397 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700398 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700399
400 weston_touch_start_grab(touch, &grab->grab);
401 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500402 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700404}
405
406static void
407shell_touch_grab_end(struct shell_touch_grab *grab)
408{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700409 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700410 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700411 grab->shsurf->grabbed = 0;
412 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700413
414 weston_touch_end_grab(grab->touch);
415}
416
417static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800419 struct weston_output *output);
420
Daniel Stone496ca172012-05-30 16:31:42 +0100421static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300422get_modifier(char *modifier)
423{
424 if (!modifier)
425 return MODIFIER_SUPER;
426
427 if (!strcmp("ctrl", modifier))
428 return MODIFIER_CTRL;
429 else if (!strcmp("alt", modifier))
430 return MODIFIER_ALT;
431 else if (!strcmp("super", modifier))
432 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000433 else if (!strcmp("none", modifier))
434 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300435 else
436 return MODIFIER_SUPER;
437}
438
Juan Zhaoe10d2792012-04-25 19:09:52 +0800439static enum animation_type
440get_animation_type(char *animation)
441{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800442 if (!animation)
443 return ANIMATION_NONE;
444
Juan Zhaoe10d2792012-04-25 19:09:52 +0800445 if (!strcmp("zoom", animation))
446 return ANIMATION_ZOOM;
447 else if (!strcmp("fade", animation))
448 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100449 else if (!strcmp("dim-layer", animation))
450 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800451 else
452 return ANIMATION_NONE;
453}
454
Alex Wu4539b082012-03-01 12:57:46 +0800455static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400456shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200457{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400458 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500459 char *s, *client;
Pekka Paalanen974c0942014-09-05 14:45:09 +0300460 int ret;
Bob Ham744e6532016-01-12 10:21:48 +0000461 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200462
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300463 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400464 "shell", NULL, NULL);
Pekka Paalanen974c0942014-09-05 14:45:09 +0300465 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
466 WESTON_SHELL_CLIENT);
467 if (ret < 0)
468 client = NULL;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400469 weston_config_section_get_string(section,
Derek Foremanc7210432014-08-21 11:32:38 -0500470 "client", &s, client);
471 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100472 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000473
474 weston_config_section_get_bool(section,
475 "allow-zap", &allow_zap, true);
476 shell->allow_zap = allow_zap;
477
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100478 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400479 "binding-modifier", &s, "super");
480 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200481 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800482
483 weston_config_section_get_string(section,
484 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000485 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800486 free(s);
487
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400488 weston_config_section_get_string(section, "animation", &s, "none");
489 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200490 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200491 weston_config_section_get_string(section, "close-animation", &s, "fade");
492 shell->win_close_animation_type = get_animation_type(s);
493 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700494 weston_config_section_get_string(section,
495 "startup-animation", &s, "fade");
496 shell->startup_animation_type = get_animation_type(s);
497 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700498 if (shell->startup_animation_type == ANIMATION_ZOOM)
499 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100500 weston_config_section_get_string(section, "focus-animation", &s, "none");
501 shell->focus_animation_type = get_animation_type(s);
502 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400503 weston_config_section_get_uint(section, "num-workspaces",
504 &shell->workspaces.num,
505 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200506}
507
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800508struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100509get_default_output(struct weston_compositor *compositor)
510{
Armin Krezović10b06182016-06-23 11:59:30 +0200511 if (wl_list_empty(&compositor->output_list))
512 return NULL;
513
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100514 return container_of(compositor->output_list.next,
515 struct weston_output, link);
516}
517
Pekka Paalanen8274d902014-08-06 19:36:51 +0300518static int
519focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
520{
521 return snprintf(buf, len, "focus highlight effect for output %s",
522 surface->output->name);
523}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100524
525/* no-op func for checking focus surface */
526static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200527focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100528{
529}
530
531static struct focus_surface *
532get_focus_surface(struct weston_surface *surface)
533{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200534 if (surface->committed == focus_surface_committed)
535 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100536 else
537 return NULL;
538}
539
540static bool
541is_focus_surface (struct weston_surface *es)
542{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200543 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100544}
545
546static bool
547is_focus_view (struct weston_view *view)
548{
549 return is_focus_surface (view->surface);
550}
551
552static struct focus_surface *
553create_focus_surface(struct weston_compositor *ec,
554 struct weston_output *output)
555{
556 struct focus_surface *fsurf = NULL;
557 struct weston_surface *surface = NULL;
558
559 fsurf = malloc(sizeof *fsurf);
560 if (!fsurf)
561 return NULL;
562
563 fsurf->surface = weston_surface_create(ec);
564 surface = fsurf->surface;
565 if (surface == NULL) {
566 free(fsurf);
567 return NULL;
568 }
569
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200570 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100571 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200572 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200573 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300574 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100575
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800576 fsurf->view = weston_view_create(surface);
577 if (fsurf->view == NULL) {
578 weston_surface_destroy(surface);
579 free(fsurf);
580 return NULL;
581 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100582 fsurf->view->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200583 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100584
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600585 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600586 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100587 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
588 pixman_region32_fini(&surface->opaque);
589 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
590 output->width, output->height);
591 pixman_region32_fini(&surface->input);
592 pixman_region32_init(&surface->input);
593
594 wl_list_init(&fsurf->workspace_transform.link);
595
596 return fsurf;
597}
598
599static void
600focus_surface_destroy(struct focus_surface *fsurf)
601{
602 weston_surface_destroy(fsurf->surface);
603 free(fsurf);
604}
605
606static void
607focus_animation_done(struct weston_view_animation *animation, void *data)
608{
609 struct workspace *ws = data;
610
611 ws->focus_animation = NULL;
612}
613
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200614static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200615focus_state_destroy(struct focus_state *state)
616{
617 wl_list_remove(&state->seat_destroy_listener.link);
618 wl_list_remove(&state->surface_destroy_listener.link);
619 free(state);
620}
621
622static void
623focus_state_seat_destroy(struct wl_listener *listener, void *data)
624{
625 struct focus_state *state = container_of(listener,
626 struct focus_state,
627 seat_destroy_listener);
628
629 wl_list_remove(&state->link);
630 focus_state_destroy(state);
631}
632
633static void
634focus_state_surface_destroy(struct wl_listener *listener, void *data)
635{
636 struct focus_state *state = container_of(listener,
637 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400638 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200639 struct weston_surface *main_surface;
640 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
Pekka Paalanen01388e22013-04-25 13:57:44 +0300643 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
644
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300646 wl_list_for_each(view,
647 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400649 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100650 if (is_focus_view(view))
651 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200652 if (!get_shell_surface(view->surface))
653 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400654
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200655 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 break;
657 }
658
Pekka Paalanen01388e22013-04-25 13:57:44 +0300659 /* if the focus was a sub-surface, activate its main surface */
660 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200661 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300662
Kristian Høgsberge3778222012-07-31 17:29:30 -0400663 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100664 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200665 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200666 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400667 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200668 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100669 if (state->ws->focus_animation)
670 weston_view_animation_destroy(state->ws->focus_animation);
671
672 state->ws->focus_animation = weston_fade_run(
673 state->ws->fsurf_front->view,
674 state->ws->fsurf_front->view->alpha, 0.0, 300,
675 focus_animation_done, state->ws);
676 }
677
Kristian Høgsberge3778222012-07-31 17:29:30 -0400678 wl_list_remove(&state->link);
679 focus_state_destroy(state);
680 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200681}
682
683static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200684focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
685 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200686{
Jonas Ådahl04769742012-06-13 00:01:24 +0200687 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200688
689 state = malloc(sizeof *state);
690 if (state == NULL)
691 return NULL;
692
Quentin Glidicfff39812016-08-15 10:56:52 +0200693 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400695 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200696 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200698
699 state->seat_destroy_listener.notify = focus_state_seat_destroy;
700 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400701 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200702 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400703 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200704
705 return state;
706}
707
Jonas Ådahl8538b222012-08-29 22:13:03 +0200708static struct focus_state *
709ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
710{
711 struct workspace *ws = get_current_workspace(shell);
712 struct focus_state *state;
713
714 wl_list_for_each(state, &ws->focus_list, link)
715 if (state->seat == seat)
716 break;
717
718 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200719 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200720
721 return state;
722}
723
Jonas Ådahl04769742012-06-13 00:01:24 +0200724static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800725focus_state_set_focus(struct focus_state *state,
726 struct weston_surface *surface)
727{
728 if (state->keyboard_focus) {
729 wl_list_remove(&state->surface_destroy_listener.link);
730 wl_list_init(&state->surface_destroy_listener.link);
731 }
732
733 state->keyboard_focus = surface;
734 if (surface)
735 wl_signal_add(&surface->destroy_signal,
736 &state->surface_destroy_listener);
737}
738
739static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400740restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200741{
742 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400743 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100744 struct wl_list pending_seat_list;
745 struct weston_seat *seat, *next_seat;
746
747 /* Temporarily steal the list of seats so that we can keep
748 * track of the seats we've already processed */
749 wl_list_init(&pending_seat_list);
750 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
751 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200752
753 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500754 struct weston_keyboard *keyboard =
755 weston_seat_get_keyboard(state->seat);
756
Neil Roberts4237f502014-04-09 16:33:31 +0100757 wl_list_remove(&state->seat->link);
758 wl_list_insert(&shell->compositor->seat_list,
759 &state->seat->link);
760
Derek Foreman1281a362015-07-31 16:55:32 -0500761 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800762 continue;
763
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400764 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200765
Derek Foreman1281a362015-07-31 16:55:32 -0500766 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200767 }
Neil Roberts4237f502014-04-09 16:33:31 +0100768
769 /* For any remaining seats that we don't have a focus state
770 * for we'll reset the keyboard focus to NULL */
771 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500772 struct weston_keyboard *keyboard =
773 weston_seat_get_keyboard(seat);
774
Neil Roberts4237f502014-04-09 16:33:31 +0100775 wl_list_insert(&shell->compositor->seat_list, &seat->link);
776
Derek Foreman1281a362015-07-31 16:55:32 -0500777 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100778 continue;
779
Derek Foreman1281a362015-07-31 16:55:32 -0500780 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100781 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200782}
783
784static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200785replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
786 struct weston_seat *seat)
787{
Derek Foreman1281a362015-07-31 16:55:32 -0500788 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200790
791 wl_list_for_each(state, &ws->focus_list, link) {
792 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500793 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794 return;
795 }
796 }
797}
798
799static void
800drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
801 struct weston_surface *surface)
802{
803 struct focus_state *state;
804
805 wl_list_for_each(state, &ws->focus_list, link)
806 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800807 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200808}
809
810static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100811animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
812 struct weston_view *from, struct weston_view *to)
813{
814 struct weston_output *output;
815 bool focus_surface_created = false;
816
817 /* FIXME: Only support dim animation using two layers */
818 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
819 return;
820
821 output = get_default_output(shell->compositor);
822 if (ws->fsurf_front == NULL && (from || to)) {
823 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800824 if (ws->fsurf_front == NULL)
825 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100826 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800827
828 ws->fsurf_back = create_focus_surface(shell->compositor, output);
829 if (ws->fsurf_back == NULL) {
830 focus_surface_destroy(ws->fsurf_front);
831 return;
832 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800834
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 focus_surface_created = true;
836 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300837 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
838 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 }
840
841 if (ws->focus_animation) {
842 weston_view_animation_destroy(ws->focus_animation);
843 ws->focus_animation = NULL;
844 }
845
846 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300847 weston_layer_entry_insert(&to->layer_link,
848 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100849 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300850 weston_layer_entry_insert(&ws->layer.view_list,
851 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100852
853 if (focus_surface_created) {
854 ws->focus_animation = weston_fade_run(
855 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200856 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 focus_animation_done, ws);
858 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300859 weston_layer_entry_insert(&from->layer_link,
860 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 ws->focus_animation = weston_stable_fade_run(
862 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200863 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100864 focus_animation_done, ws);
865 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300866 weston_layer_entry_insert(&ws->layer.view_list,
867 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 ws->focus_animation = weston_stable_fade_run(
869 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200870 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100871 focus_animation_done, ws);
872 }
873}
874
875static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200876workspace_destroy(struct workspace *ws)
877{
Jonas Ådahl04769742012-06-13 00:01:24 +0200878 struct focus_state *state, *next;
879
880 wl_list_for_each_safe(state, next, &ws->focus_list, link)
881 focus_state_destroy(state);
882
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100883 if (ws->fsurf_front)
884 focus_surface_destroy(ws->fsurf_front);
885 if (ws->fsurf_back)
886 focus_surface_destroy(ws->fsurf_back);
887
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200888 free(ws);
889}
890
Jonas Ådahl04769742012-06-13 00:01:24 +0200891static void
892seat_destroyed(struct wl_listener *listener, void *data)
893{
894 struct weston_seat *seat = data;
895 struct focus_state *state, *next;
896 struct workspace *ws = container_of(listener,
897 struct workspace,
898 seat_destroyed_listener);
899
900 wl_list_for_each_safe(state, next, &ws->focus_list, link)
901 if (state->seat == seat)
902 wl_list_remove(&state->link);
903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static struct workspace *
906workspace_create(void)
907{
908 struct workspace *ws = malloc(sizeof *ws);
909 if (ws == NULL)
910 return NULL;
911
912 weston_layer_init(&ws->layer, NULL);
913
Jonas Ådahl04769742012-06-13 00:01:24 +0200914 wl_list_init(&ws->focus_list);
915 wl_list_init(&ws->seat_destroyed_listener.link);
916 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100917 ws->fsurf_front = NULL;
918 ws->fsurf_back = NULL;
919 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200920
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921 return ws;
922}
923
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924static int
925workspace_is_empty(struct workspace *ws)
926{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300927 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200928}
929
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930static struct workspace *
931get_workspace(struct desktop_shell *shell, unsigned int index)
932{
933 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200934 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200935 pws += index;
936 return *pws;
937}
938
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800939struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940get_current_workspace(struct desktop_shell *shell)
941{
942 return get_workspace(shell, shell->workspaces.current);
943}
944
945static void
946activate_workspace(struct desktop_shell *shell, unsigned int index)
947{
948 struct workspace *ws;
949
950 ws = get_workspace(shell, index);
951 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
952
953 shell->workspaces.current = index;
954}
955
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956static unsigned int
957get_output_height(struct weston_output *output)
958{
959 return abs(output->region.extents.y1 - output->region.extents.y2);
960}
961
962static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100963view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965 struct weston_transform *transform;
966
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100967 if (is_focus_view(view)) {
968 struct focus_surface *fsurf = get_focus_surface(view->surface);
969 transform = &fsurf->workspace_transform;
970 } else {
971 struct shell_surface *shsurf = get_shell_surface(view->surface);
972 transform = &shsurf->workspace_transform;
973 }
974
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100979 weston_matrix_init(&transform->matrix);
980 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200981 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200983}
984
985static void
986workspace_translate_out(struct workspace *ws, double fraction)
987{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 unsigned int height;
990 double d;
991
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300992 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 d = height * fraction;
995
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997 }
998}
999
1000static void
1001workspace_translate_in(struct workspace *ws, double fraction)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 unsigned int height;
1005 double d;
1006
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001007 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009
1010 if (fraction > 0)
1011 d = -(height - height * fraction);
1012 else
1013 d = height + height * fraction;
1014
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001015 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 }
1017}
1018
1019static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001020reverse_workspace_change_animation(struct desktop_shell *shell,
1021 unsigned int index,
1022 struct workspace *from,
1023 struct workspace *to)
1024{
1025 shell->workspaces.current = index;
1026
1027 shell->workspaces.anim_to = to;
1028 shell->workspaces.anim_from = from;
1029 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1030 shell->workspaces.anim_timestamp = 0;
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033}
1034
1035static void
1036workspace_deactivate_transforms(struct workspace *ws)
1037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001041 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001042 if (is_focus_view(view)) {
1043 struct focus_surface *fsurf = get_focus_surface(view->surface);
1044 transform = &fsurf->workspace_transform;
1045 } else {
1046 struct shell_surface *shsurf = get_shell_surface(view->surface);
1047 transform = &shsurf->workspace_transform;
1048 }
1049
1050 if (!wl_list_empty(&transform->link)) {
1051 wl_list_remove(&transform->link);
1052 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001053 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055 }
1056}
1057
1058static void
1059finish_workspace_change_animation(struct desktop_shell *shell,
1060 struct workspace *from,
1061 struct workspace *to)
1062{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001063 struct weston_view *view;
1064
Scott Moreau4272e632012-08-13 09:58:41 -06001065 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001066
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001067 /* Views that extend past the bottom of the output are still
1068 * visible after the workspace animation ends but before its layer
1069 * is hidden. In that case, we need to damage below those views so
1070 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001071 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001072 weston_view_damage_below(view);
1073
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001074 wl_list_remove(&shell->workspaces.animation.link);
1075 workspace_deactivate_transforms(from);
1076 workspace_deactivate_transforms(to);
1077 shell->workspaces.anim_to = NULL;
1078
1079 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1080}
1081
1082static void
1083animate_workspace_change_frame(struct weston_animation *animation,
1084 struct weston_output *output, uint32_t msecs)
1085{
1086 struct desktop_shell *shell =
1087 container_of(animation, struct desktop_shell,
1088 workspaces.animation);
1089 struct workspace *from = shell->workspaces.anim_from;
1090 struct workspace *to = shell->workspaces.anim_to;
1091 uint32_t t;
1092 double x, y;
1093
1094 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1095 finish_workspace_change_animation(shell, from, to);
1096 return;
1097 }
1098
1099 if (shell->workspaces.anim_timestamp == 0) {
1100 if (shell->workspaces.anim_current == 0.0)
1101 shell->workspaces.anim_timestamp = msecs;
1102 else
1103 shell->workspaces.anim_timestamp =
1104 msecs -
1105 /* Invers of movement function 'y' below. */
1106 (asin(1.0 - shell->workspaces.anim_current) *
1107 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1108 M_2_PI);
1109 }
1110
1111 t = msecs - shell->workspaces.anim_timestamp;
1112
1113 /*
1114 * x = [0, π/2]
1115 * y(x) = sin(x)
1116 */
1117 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1118 y = sin(x);
1119
1120 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001121 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122
1123 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1124 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1125 shell->workspaces.anim_current = y;
1126
Scott Moreau4272e632012-08-13 09:58:41 -06001127 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001129 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131}
1132
1133static void
1134animate_workspace_change(struct desktop_shell *shell,
1135 unsigned int index,
1136 struct workspace *from,
1137 struct workspace *to)
1138{
1139 struct weston_output *output;
1140
1141 int dir;
1142
1143 if (index > shell->workspaces.current)
1144 dir = -1;
1145 else
1146 dir = 1;
1147
1148 shell->workspaces.current = index;
1149
1150 shell->workspaces.anim_dir = dir;
1151 shell->workspaces.anim_from = from;
1152 shell->workspaces.anim_to = to;
1153 shell->workspaces.anim_current = 0.0;
1154 shell->workspaces.anim_timestamp = 0;
1155
1156 output = container_of(shell->compositor->output_list.next,
1157 struct weston_output, link);
1158 wl_list_insert(&output->animation_list,
1159 &shell->workspaces.animation.link);
1160
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001161 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162
1163 workspace_translate_in(to, 0);
1164
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001165 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001166
Scott Moreau4272e632012-08-13 09:58:41 -06001167 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168}
1169
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001170static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001171update_workspace(struct desktop_shell *shell, unsigned int index,
1172 struct workspace *from, struct workspace *to)
1173{
1174 shell->workspaces.current = index;
1175 wl_list_insert(&from->layer.link, &to->layer.link);
1176 wl_list_remove(&from->layer.link);
1177}
1178
1179static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001180change_workspace(struct desktop_shell *shell, unsigned int index)
1181{
1182 struct workspace *from;
1183 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001184 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001185
1186 if (index == shell->workspaces.current)
1187 return;
1188
1189 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001190 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001191 return;
1192
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001193 from = get_current_workspace(shell);
1194 to = get_workspace(shell, index);
1195
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001196 if (shell->workspaces.anim_from == to &&
1197 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001199 reverse_workspace_change_animation(shell, index, from, to);
1200 return;
1201 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001202
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203 if (shell->workspaces.anim_to != NULL)
1204 finish_workspace_change_animation(shell,
1205 shell->workspaces.anim_from,
1206 shell->workspaces.anim_to);
1207
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001208 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001209
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001210 if (shell->focus_animation_type != ANIMATION_NONE) {
1211 wl_list_for_each(state, &from->focus_list, link)
1212 if (state->keyboard_focus)
1213 animate_focus_change(shell, from,
1214 get_default_view(state->keyboard_focus), NULL);
1215
1216 wl_list_for_each(state, &to->focus_list, link)
1217 if (state->keyboard_focus)
1218 animate_focus_change(shell, to,
1219 NULL, get_default_view(state->keyboard_focus));
1220 }
1221
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222 if (workspace_is_empty(to) && workspace_is_empty(from))
1223 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001224 else
1225 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001226}
1227
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228static bool
1229workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1230{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001231 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232 struct wl_list *e;
1233
1234 if (wl_list_empty(list))
1235 return false;
1236
1237 e = list->next;
1238
1239 if (e->next != list)
1240 return false;
1241
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001242 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001243}
1244
1245static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001246surface_keyboard_focus_lost(struct weston_surface *surface)
1247{
1248 struct weston_compositor *compositor = surface->compositor;
1249 struct weston_seat *seat;
1250 struct weston_surface *focus;
1251
1252 wl_list_for_each(seat, &compositor->seat_list, link) {
1253 struct weston_keyboard *keyboard =
1254 weston_seat_get_keyboard(seat);
1255
1256 if (!keyboard)
1257 continue;
1258
1259 focus = weston_surface_get_main_surface(keyboard->focus);
1260 if (focus == surface)
1261 weston_keyboard_set_focus(keyboard, NULL);
1262 }
1263}
1264
1265static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001267 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001268 unsigned int index)
1269{
Derek Foreman1281a362015-07-31 16:55:32 -05001270 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001271 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001273 struct shell_surface *shsurf;
1274 struct workspace *from;
1275 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001276 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277
Derek Foreman1281a362015-07-31 16:55:32 -05001278 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 view = get_default_view(surface);
1280 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001281 index == shell->workspaces.current ||
1282 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001283 return;
1284
1285 from = get_current_workspace(shell);
1286 to = get_workspace(shell, index);
1287
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001288 weston_layer_entry_remove(&view->layer_link);
1289 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290
Philip Withnall659163d2013-11-25 18:01:36 +00001291 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001292 if (shsurf != NULL)
1293 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001294
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296 drop_focus_state(shell, from, surface);
1297
1298 if (shell->workspaces.anim_from == to &&
1299 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001300 wl_list_remove(&to->layer.link);
1301 wl_list_insert(from->layer.link.prev, &to->layer.link);
1302
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001303 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001304
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001305 return;
1306 }
1307
1308 if (shell->workspaces.anim_to != NULL)
1309 finish_workspace_change_animation(shell,
1310 shell->workspaces.anim_from,
1311 shell->workspaces.anim_to);
1312
1313 if (workspace_is_empty(from) &&
1314 workspace_has_only(to, surface))
1315 update_workspace(shell, index, from, to);
1316 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001317 if (shsurf != NULL &&
1318 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001319 wl_list_insert(&shell->workspaces.anim_sticky_list,
1320 &shsurf->workspace_transform.link);
1321
1322 animate_workspace_change(shell, index, from, to);
1323 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324
Jonas Ådahl8538b222012-08-29 22:13:03 +02001325 state = ensure_focus_state(shell, seat);
1326 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001327 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001328}
1329
1330static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001331touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001332 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001333{
1334}
1335
1336static void
1337touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1338{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001339 struct weston_touch_move_grab *move =
1340 (struct weston_touch_move_grab *) container_of(
1341 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001342
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001343 if (touch_id == 0)
1344 move->active = 0;
1345
Jonas Ådahl9484b692013-12-02 22:05:03 +01001346 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001347 shell_touch_grab_end(&move->base);
1348 free(move);
1349 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001350}
1351
1352static void
1353touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001354 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001355{
1356 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1357 struct shell_surface *shsurf = move->base.shsurf;
1358 struct weston_surface *es;
1359 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1360 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1361
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001362 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001363 return;
1364
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001365 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001366
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001367 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001368
1369 weston_compositor_schedule_repaint(es->compositor);
1370}
1371
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001372static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001373touch_move_grab_frame(struct weston_touch_grab *grab)
1374{
1375}
1376
1377static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001378touch_move_grab_cancel(struct weston_touch_grab *grab)
1379{
1380 struct weston_touch_move_grab *move =
1381 (struct weston_touch_move_grab *) container_of(
1382 grab, struct shell_touch_grab, grab);
1383
1384 shell_touch_grab_end(&move->base);
1385 free(move);
1386}
1387
Rusty Lynch1084da52013-08-15 09:10:08 -07001388static const struct weston_touch_grab_interface touch_move_grab_interface = {
1389 touch_move_grab_down,
1390 touch_move_grab_up,
1391 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001392 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001393 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001394};
1395
1396static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001397surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001398{
1399 struct weston_touch_move_grab *move;
1400
1401 if (!shsurf)
1402 return -1;
1403
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001404 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1405 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001406 return 0;
1407
1408 move = malloc(sizeof *move);
1409 if (!move)
1410 return -1;
1411
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001412 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001414 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001416 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001417
1418 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001419 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001420
1421 return 0;
1422}
1423
1424static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001425noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001426{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001427}
1428
1429static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001430noop_grab_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +10001431 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001432{
1433}
1434
1435static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001436noop_grab_axis_source(struct weston_pointer_grab *grab,
1437 uint32_t source)
1438{
1439}
1440
1441static void
1442noop_grab_frame(struct weston_pointer_grab *grab)
1443{
1444}
1445
1446static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001447constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1448{
1449 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001450 struct weston_surface *surface =
1451 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001452 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001453 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001454 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001455 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001456 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001457
1458 x = wl_fixed_to_int(pointer->x + move->dx);
1459 y = wl_fixed_to_int(pointer->y + move->dy);
1460
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001461 if (shsurf->shell->panel_position ==
1462 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001463 get_output_work_area(shsurf->shell, surface->output, &area);
1464 geometry =
1465 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001466
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001467 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001468 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001469 y = area.y + safety - geometry.height
1470 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001471
1472 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001473 y + geometry.y < area.y)
1474 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001475 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001476
1477 *cx = x;
1478 *cy = y;
1479}
1480
1481static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001482move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001483 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001486 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001487 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001488 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001489 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001490
Jonas Ådahld2510102014-10-05 21:39:14 +02001491 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001492 if (!shsurf)
1493 return;
1494
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001495 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1496
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001497 constrain_position(move, &cx, &cy);
1498
1499 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001501 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502}
1503
1504static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001505move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001506 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001508 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1509 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001511 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512
Daniel Stone4dbadb12012-05-30 16:31:51 +01001513 if (pointer->button_count == 0 &&
1514 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001515 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001516 free(grab);
1517 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518}
1519
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001520static void
1521move_grab_cancel(struct weston_pointer_grab *grab)
1522{
1523 struct shell_grab *shell_grab =
1524 container_of(grab, struct shell_grab, grab);
1525
1526 shell_grab_end(shell_grab);
1527 free(grab);
1528}
1529
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001530static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001531 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001532 move_grab_motion,
1533 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001534 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001535 noop_grab_axis_source,
1536 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001537 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001538};
1539
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001541surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001542 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001543{
1544 struct weston_move_grab *move;
1545
1546 if (!shsurf)
1547 return -1;
1548
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001549 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001550 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1551 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552 return 0;
1553
1554 move = malloc(sizeof *move);
1555 if (!move)
1556 return -1;
1557
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001559 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001561 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001562 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001563
1564 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001565 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001566
1567 return 0;
1568}
1569
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570struct weston_resize_grab {
1571 struct shell_grab base;
1572 uint32_t edges;
1573 int32_t width, height;
1574};
1575
1576static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001577resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001578 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579{
1580 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001581 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582 struct shell_surface *shsurf = resize->base.shsurf;
1583 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001584 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001585 wl_fixed_t from_x, from_y;
1586 wl_fixed_t to_x, to_y;
1587
Jonas Ådahld2510102014-10-05 21:39:14 +02001588 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001589
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001590 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591 return;
1592
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 weston_view_from_global_fixed(shsurf->view,
1594 pointer->grab_x, pointer->grab_y,
1595 &from_x, &from_y);
1596 weston_view_from_global_fixed(shsurf->view,
1597 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598
1599 width = resize->width;
1600 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1601 width += wl_fixed_to_int(from_x - to_x);
1602 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1603 width += wl_fixed_to_int(to_x - from_x);
1604 }
1605
1606 height = resize->height;
1607 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1608 height += wl_fixed_to_int(from_y - to_y);
1609 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1610 height += wl_fixed_to_int(to_y - from_y);
1611 }
1612
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001613 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1614 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1615
1616 min_size.width = MAX(1, min_size.width);
1617 min_size.height = MAX(1, min_size.height);
1618
1619 if (width < min_size.width)
1620 width = min_size.width;
1621 else if (max_size.width > 0 && width > max_size.width)
1622 width = max_size.width;
1623 if (height < min_size.height)
1624 height = min_size.height;
1625 else if (max_size.width > 0 && width > max_size.width)
1626 width = max_size.width;
1627 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001628}
1629
1630static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001631resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001632 uint32_t time, uint32_t button, uint32_t state_w)
1633{
1634 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001635 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001636 enum wl_pointer_button_state state = state_w;
1637
1638 if (pointer->button_count == 0 &&
1639 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1640 shell_grab_end(&resize->base);
1641 free(grab);
1642 }
1643}
1644
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001645static void
1646resize_grab_cancel(struct weston_pointer_grab *grab)
1647{
1648 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1649
1650 shell_grab_end(&resize->base);
1651 free(grab);
1652}
1653
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001654static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001655 noop_grab_focus,
1656 resize_grab_motion,
1657 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001658 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001659 noop_grab_axis_source,
1660 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001661 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662};
1663
Giulio Camuffob8366642013-04-25 13:57:46 +03001664/*
1665 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001666 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001667static void
1668surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1669 int32_t *y, int32_t *w, int32_t *h) {
1670 pixman_region32_t region;
1671 pixman_box32_t *box;
1672 struct weston_subsurface *subsurface;
1673
1674 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 surface->width,
1676 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001677
1678 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1679 pixman_region32_union_rect(&region, &region,
1680 subsurface->position.x,
1681 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 subsurface->surface->width,
1683 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001684 }
1685
1686 box = pixman_region32_extents(&region);
1687 if (x)
1688 *x = box->x1;
1689 if (y)
1690 *y = box->y1;
1691 if (w)
1692 *w = box->x2 - box->x1;
1693 if (h)
1694 *h = box->y2 - box->y1;
1695
1696 pixman_region32_fini(&region);
1697}
1698
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699static int
1700surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001701 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001702{
1703 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001704 const unsigned resize_topbottom =
1705 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1706 const unsigned resize_leftright =
1707 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1708 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001709 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001711 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001712 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1713 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714 return 0;
1715
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001716 /* Check for invalid edge combinations. */
1717 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1718 (edges & resize_topbottom) == resize_topbottom ||
1719 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001720 return 0;
1721
1722 resize = malloc(sizeof *resize);
1723 if (!resize)
1724 return -1;
1725
1726 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001728 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1729 resize->width = geometry.width;
1730 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001731
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001732 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001734 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001735
1736 return 0;
1737}
1738
1739static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001740busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001741{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001742 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001743 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001744 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001745 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001746 wl_fixed_t sx, sy;
1747
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 view = weston_compositor_pick_view(pointer->seat->compositor,
1749 pointer->x, pointer->y,
1750 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001751 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001752
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001753 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001754 shell_grab_end(grab);
1755 free(grab);
1756 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001757}
1758
1759static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001760busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001761 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001762{
Jonas Ådahld2510102014-10-05 21:39:14 +02001763 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001764}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001765
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001766static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001767busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001768 uint32_t time, uint32_t button, uint32_t state)
1769{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001771 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001772 struct weston_pointer *pointer = grab->grab.pointer;
1773 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001774
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001775 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001776 activate(shsurf->shell, shsurf->view, seat,
1777 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001778 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001779 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001780 activate(shsurf->shell, shsurf->view, seat,
1781 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001782 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001783 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784}
1785
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001786static void
1787busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1788{
1789 struct shell_grab *grab = (struct shell_grab *) base;
1790
1791 shell_grab_end(grab);
1792 free(grab);
1793}
1794
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001795static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796 busy_cursor_grab_focus,
1797 busy_cursor_grab_motion,
1798 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001799 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001800 noop_grab_axis_source,
1801 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001802 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803};
1804
1805static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001806handle_pointer_focus(struct wl_listener *listener, void *data)
1807{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001808 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001810 struct shell_surface *shsurf;
1811 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814 return;
1815
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001816 shsurf = get_shell_surface(view->surface);
1817 if (!shsurf)
1818 return;
1819
1820 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1821
1822 if (shsurf->unresponsive)
1823 set_busy_cursor(shsurf, pointer);
1824 else
1825 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826}
1827
1828static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001829shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1830{
1831 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001832 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001833}
1834
1835static void
1836shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1837{
1838 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001839 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001840}
1841
1842static void
1843handle_keyboard_focus(struct wl_listener *listener, void *data)
1844{
1845 struct weston_keyboard *keyboard = data;
1846 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1847
1848 if (seat->focused_surface) {
1849 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1850 if (shsurf)
1851 shell_surface_lose_keyboard_focus(shsurf);
1852 }
1853
1854 seat->focused_surface = keyboard->focus;
1855
1856 if (seat->focused_surface) {
1857 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1858 if (shsurf)
1859 shell_surface_gain_keyboard_focus(shsurf);
1860 }
1861}
1862
Philip Withnall07926d92013-11-25 18:01:40 +00001863/* The surface will be inserted into the list immediately after the link
1864 * returned by this function (i.e. will be stacked immediately above the
1865 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001866static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001867shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1868{
1869 struct workspace *ws;
1870
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001871 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1872 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001873 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001874 }
1875
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001876 /* Move the surface to a normal workspace layer so that surfaces
1877 * which were previously fullscreen or transient are no longer
1878 * rendered on top. */
1879 ws = get_current_workspace(shsurf->shell);
1880 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001881}
1882
Philip Withnall648a4dd2013-11-25 18:01:44 +00001883static void
1884shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1885{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001886 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001887}
1888
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001889/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001890 * geometry to ensure the changes are redrawn.
1891 *
1892 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1893 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001894static void
1895shell_surface_update_layer(struct shell_surface *shsurf)
1896{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001897 struct weston_surface *surface =
1898 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001899 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001900
1901 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1902
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001903 if (new_layer_link == NULL)
1904 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001905 if (new_layer_link == &shsurf->view->layer_link)
1906 return;
1907
1908 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001909 weston_layer_entry_remove(&shsurf->view->layer_link);
1910 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001911 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001912 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001913
1914 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001915}
1916
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001917static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001918shell_surface_set_output(struct shell_surface *shsurf,
1919 struct weston_output *output)
1920{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001921 struct weston_surface *es =
1922 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001923
1924 /* get the default output, if the client set it as NULL
1925 check whether the ouput is available */
1926 if (output)
1927 shsurf->output = output;
1928 else if (es->output)
1929 shsurf->output = es->output;
1930 else
1931 shsurf->output = get_default_output(es->compositor);
1932}
1933
1934static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001935weston_view_set_initial_position(struct weston_view *view,
1936 struct desktop_shell *shell);
1937
1938static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001939unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001940{
Philip Withnallf85fe842013-11-25 18:01:37 +00001941 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001942 wl_list_remove(&shsurf->fullscreen.transform.link);
1943 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001944
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 if (shsurf->fullscreen.black_view)
1946 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1947 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001948
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001949 if (shsurf->saved_position_valid)
1950 weston_view_set_position(shsurf->view,
1951 shsurf->saved_x, shsurf->saved_y);
1952 else
1953 weston_view_set_initial_position(shsurf->view, shsurf->shell);
1954
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001955 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001957 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001958 shsurf->saved_rotation_valid = false;
1959 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001960
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001961 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08001962}
1963
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001964static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001965unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001966{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001967 struct weston_surface *surface =
1968 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1969
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001970 /* undo all maximized things here */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001971 shsurf->output = get_default_output(surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001972
1973 if (shsurf->saved_position_valid)
1974 weston_view_set_position(shsurf->view,
1975 shsurf->saved_x, shsurf->saved_y);
1976 else
1977 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001978
1979 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 wl_list_insert(&shsurf->view->geometry.transformation_list,
1981 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001982 shsurf->saved_rotation_valid = false;
1983 }
1984
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001985 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001986}
1987
Philip Withnallbecb77e2013-11-25 18:01:30 +00001988static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001989set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001990{
1991 struct shell_surface *shsurf;
1992 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001993 struct weston_view *view;
1994
1995 view = get_default_view(surface);
1996 if (!view)
1997 return;
1998
1999 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2000
2001 shsurf = get_shell_surface(surface);
2002 current_ws = get_current_workspace(shsurf->shell);
2003
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002004 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002005 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002006
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002007 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002008 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002009
2010 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002011 weston_view_damage_below(view);
2012}
2013
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002014
Tiago Vignattibe143262012-04-16 17:31:41 +03002015static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002016shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002017{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002018 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002019}
2020
Pekka Paalanen8274d902014-08-06 19:36:51 +03002021static int
2022black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2023{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002024 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002025 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002026 int n;
2027 int rem;
2028 int ret;
2029
2030 n = snprintf(buf, len, "black background surface for ");
2031 if (n < 0)
2032 return n;
2033
2034 rem = (int)len - n;
2035 if (rem < 0)
2036 rem = 0;
2037
2038 if (fs_surface->get_label)
2039 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2040 else
2041 ret = snprintf(buf + n, rem, "<unknown>");
2042
2043 if (ret < 0)
2044 return n;
2045
2046 return n + ret;
2047}
2048
Alex Wu21858432012-04-01 20:13:08 +08002049static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002050black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002051
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002053create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002054 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002055 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002056{
2057 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002059
2060 surface = weston_surface_create(ec);
2061 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002062 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002063 return NULL;
2064 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065 view = weston_view_create(surface);
2066 if (surface == NULL) {
2067 weston_log("no memory\n");
2068 weston_surface_destroy(surface);
2069 return NULL;
2070 }
Alex Wu4539b082012-03-01 12:57:46 +08002071
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002072 surface->committed = black_surface_committed;
2073 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002074 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002075 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002076 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002077 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002078 pixman_region32_fini(&surface->input);
2079 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002080
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002081 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002082 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083
2084 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002085}
2086
Philip Withnalled8f1a92013-11-25 18:01:43 +00002087static void
2088shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2089{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002090 struct weston_surface *surface =
2091 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002092 struct weston_output *output = shsurf->fullscreen_output;
2093
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002094 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002095
2096 if (!shsurf->fullscreen.black_view)
2097 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002098 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002099 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002100 output->x, output->y,
2101 output->width,
2102 output->height);
2103
2104 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002105 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2106 weston_layer_entry_insert(&shsurf->view->layer_link,
2107 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002108 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002109 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002110
Armin Krezović4663aca2016-06-30 06:04:29 +02002111 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002112 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002113}
2114
Alex Wu4539b082012-03-01 12:57:46 +08002115/* Create black surface and append it to the associated fullscreen surface.
2116 * Handle size dismatch and positioning according to the method. */
2117static void
2118shell_configure_fullscreen(struct shell_surface *shsurf)
2119{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002120 struct weston_surface *surface =
2121 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002122 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002123
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002124 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002125 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002126 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2127 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002128
Philip Withnalled8f1a92013-11-25 18:01:43 +00002129 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002130
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002131 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002132 &surf_width, &surf_height);
2133
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002134 if (surface->buffer_ref.buffer)
2135 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002136}
2137
Alex Wu4539b082012-03-01 12:57:46 +08002138static void
2139shell_map_fullscreen(struct shell_surface *shsurf)
2140{
Alex Wubd3354b2012-04-17 17:20:49 +08002141 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002142}
2143
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002144static struct weston_output *
2145get_focused_output(struct weston_compositor *compositor)
2146{
2147 struct weston_seat *seat;
2148 struct weston_output *output = NULL;
2149
2150 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002151 struct weston_touch *touch = weston_seat_get_touch(seat);
2152 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2153 struct weston_keyboard *keyboard =
2154 weston_seat_get_keyboard(seat);
2155
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002156 /* Priority has touch focus, then pointer and
2157 * then keyboard focus. We should probably have
2158 * three for loops and check frist for touch,
2159 * then for pointer, etc. but unless somebody has some
2160 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002161 if (touch && touch->focus)
2162 output = touch->focus->output;
2163 else if (pointer && pointer->focus)
2164 output = pointer->focus->output;
2165 else if (keyboard && keyboard->focus)
2166 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002167
2168 if (output)
2169 break;
2170 }
2171
2172 return output;
2173}
2174
2175static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002176destroy_shell_seat(struct wl_listener *listener, void *data)
2177{
2178 struct shell_seat *shseat =
2179 container_of(listener,
2180 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002181
2182 wl_list_remove(&shseat->seat_destroy_listener.link);
2183 free(shseat);
2184}
2185
Jason Ekstrand024177c2014-04-21 19:42:58 -05002186static void
2187shell_seat_caps_changed(struct wl_listener *listener, void *data)
2188{
Derek Foreman1281a362015-07-31 16:55:32 -05002189 struct weston_keyboard *keyboard;
2190 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002191 struct shell_seat *seat;
2192
2193 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002194 keyboard = weston_seat_get_keyboard(seat->seat);
2195 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002196
Derek Foreman1281a362015-07-31 16:55:32 -05002197 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002198 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002199 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002200 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002201 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002202 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002203 wl_list_init(&seat->keyboard_focus_listener.link);
2204 }
2205
Derek Foreman1281a362015-07-31 16:55:32 -05002206 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002207 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002208 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002209 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002210 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002211 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002212 wl_list_init(&seat->pointer_focus_listener.link);
2213 }
2214}
2215
Giulio Camuffo5085a752013-03-25 21:42:45 +01002216static struct shell_seat *
2217create_shell_seat(struct weston_seat *seat)
2218{
2219 struct shell_seat *shseat;
2220
2221 shseat = calloc(1, sizeof *shseat);
2222 if (!shseat) {
2223 weston_log("no memory to allocate shell seat\n");
2224 return NULL;
2225 }
2226
2227 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002228
2229 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2230 wl_signal_add(&seat->destroy_signal,
2231 &shseat->seat_destroy_listener);
2232
Jason Ekstrand024177c2014-04-21 19:42:58 -05002233 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2234 wl_list_init(&shseat->keyboard_focus_listener.link);
2235
2236 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2237 wl_list_init(&shseat->pointer_focus_listener.link);
2238
2239 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2240 wl_signal_add(&seat->updated_caps_signal,
2241 &shseat->caps_changed_listener);
2242 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2243
Giulio Camuffo5085a752013-03-25 21:42:45 +01002244 return shseat;
2245}
2246
2247static struct shell_seat *
2248get_shell_seat(struct weston_seat *seat)
2249{
2250 struct wl_listener *listener;
2251
2252 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002253 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002254
2255 return container_of(listener,
2256 struct shell_seat, seat_destroy_listener);
2257}
2258
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002259static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002260fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002261{
2262 struct shell_surface *shsurf = data;
2263
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002264 weston_surface_destroy(shsurf->view->surface);
2265 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002266}
2267
2268static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002269fade_out_done(struct weston_view_animation *animation, void *data)
2270{
2271 struct shell_surface *shsurf = data;
2272 struct wl_event_loop *loop;
2273
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002274 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002275
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002276 if (weston_view_is_mapped(shsurf->view)) {
2277 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002278 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002279 }
2280}
2281
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002282struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002283get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002284{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002285 if (weston_surface_is_desktop_surface(surface)) {
2286 struct weston_desktop_surface *desktop_surface =
2287 weston_surface_get_desktop_surface(surface);
2288 return weston_desktop_surface_get_user_data(desktop_surface);
2289 }
2290 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002291}
2292
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002293/*
2294 * libweston-desktop
2295 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002296
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002297static void
2298desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2299 void *shell)
2300{
2301 struct weston_desktop_client *client =
2302 weston_desktop_surface_get_client(desktop_surface);
2303 struct wl_client *wl_client =
2304 weston_desktop_client_get_client(client);
2305 struct weston_view *view;
2306 struct shell_surface *shsurf;
2307 struct weston_surface *surface =
2308 weston_desktop_surface_get_surface(desktop_surface);
2309
2310 view = weston_desktop_surface_create_view(desktop_surface);
2311 if (!view)
2312 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002313
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002314 shsurf = calloc(1, sizeof *shsurf);
2315 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002316 if (wl_client)
2317 wl_client_post_no_memory(wl_client);
2318 else
2319 weston_log("no memory to allocate shell surface\n");
2320 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002321 }
2322
Pekka Paalanen8274d902014-08-06 19:36:51 +03002323 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002324
Tiago Vignattibc052c92012-04-19 16:18:18 +03002325 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002326 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002327 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002328 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002329 shsurf->desktop_surface = desktop_surface;
2330 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002331 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002332 wl_list_init(&shsurf->fullscreen.transform.link);
2333
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002334 shsurf->output = get_default_output(shsurf->shell->compositor);
2335
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002336 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002337
Pekka Paalanen460099f2012-01-20 16:48:25 +02002338 /* empty when not in use */
2339 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002340 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002341
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002342 wl_list_init(&shsurf->workspace_transform.link);
2343
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002344 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2345 weston_desktop_surface_set_activated(desktop_surface,
2346 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002347}
2348
Tiago Vignattibc052c92012-04-19 16:18:18 +03002349static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002350desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2351 void *shell)
2352{
2353 struct shell_surface *shsurf =
2354 weston_desktop_surface_get_user_data(desktop_surface);
2355 struct weston_surface *surface =
2356 weston_desktop_surface_get_surface(desktop_surface);
2357
2358 if (!shsurf)
2359 return;
2360
2361 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2362
2363 if (shsurf->fullscreen.black_view)
2364 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2365
2366 weston_surface_set_label_func(surface, NULL);
2367 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2368 shsurf->desktop_surface = NULL;
2369
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002370 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002371 if (weston_surface_is_mapped(surface) &&
2372 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2373 pixman_region32_fini(&surface->pending.input);
2374 pixman_region32_init(&surface->pending.input);
2375 pixman_region32_fini(&surface->input);
2376 pixman_region32_init(&surface->input);
2377 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2378 fade_out_done, shsurf);
2379 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002380 weston_view_destroy(shsurf->view);
2381 free(shsurf);
2382 }
2383}
2384
2385static void
2386set_maximized_position(struct desktop_shell *shell,
2387 struct shell_surface *shsurf)
2388{
2389 pixman_rectangle32_t area;
2390 struct weston_geometry geometry;
2391
2392 get_output_work_area(shell, shsurf->output, &area);
2393 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2394
2395 weston_view_set_position(shsurf->view,
2396 area.x - geometry.x,
2397 area.y - geometry.y);
2398}
2399
2400static void
2401map(struct desktop_shell *shell, struct shell_surface *shsurf,
2402 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002403{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002404 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002405 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2406 struct weston_compositor *compositor = shell->compositor;
2407 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002408
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002409 /* initial positioning, see also configure() */
2410 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
2411 center_on_output(shsurf->view, shsurf->fullscreen_output);
2412 shell_map_fullscreen(shsurf);
2413 } else if (weston_desktop_surface_get_maximized(shsurf->desktop_surface)) {
2414 set_maximized_position(shell, shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002415 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002416 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002417 }
2418
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002419 /* Surface stacking order, see also activate(). */
2420 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002421
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002422 weston_view_update_transform(shsurf->view);
2423 shsurf->view->is_mapped = true;
2424 if (weston_desktop_surface_get_maximized(shsurf->desktop_surface)) {
2425 surface->output = shsurf->output;
2426 shsurf->view->output = shsurf->output;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002427 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002428
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429 if (!shell->locked) {
2430 wl_list_for_each(seat, &compositor->seat_list, link)
2431 activate(shell, shsurf->view, seat,
2432 WESTON_ACTIVATE_FLAG_CONFIGURE);
2433 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002434
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435 if (!weston_desktop_surface_get_maximized(shsurf->desktop_surface) &&
2436 !weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
2437 switch (shell->win_animation_type) {
2438 case ANIMATION_FADE:
2439 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2440 break;
2441 case ANIMATION_ZOOM:
2442 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2443 break;
2444 case ANIMATION_NONE:
2445 default:
2446 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002447 }
2448 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002449}
2450
2451static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002452desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2453 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002454{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002455 struct shell_surface *shsurf =
2456 weston_desktop_surface_get_user_data(desktop_surface);
2457 struct weston_surface *surface =
2458 weston_desktop_surface_get_surface(desktop_surface);
2459 struct weston_view *view = shsurf->view;
2460 struct desktop_shell *shell = data;
2461
2462 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002463 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002464
2465 if (!weston_surface_is_mapped(surface)) {
2466 map(shell, shsurf, sx, sy);
2467 surface->is_mapped = true;
2468 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2469 ++surface->ref_count;
2470 return;
2471 }
2472
2473 if (sx == 0 && sy == 0 &&
2474 shsurf->last_width == surface->width &&
2475 shsurf->last_height == surface->height)
2476 return;
2477
2478 if (weston_desktop_surface_get_fullscreen(desktop_surface))
2479 shell_configure_fullscreen(shsurf);
2480 else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2481 set_maximized_position(shell, shsurf);
2482 surface->output = shsurf->output;
2483 } else {
2484 float from_x, from_y;
2485 float to_x, to_y;
2486 float x, y;
2487
2488 if (shsurf->resize_edges) {
2489 sx = 0;
2490 sy = 0;
2491 }
2492
2493 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2494 sx = shsurf->last_width - surface->width;
2495 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2496 sy = shsurf->last_height - surface->height;
2497
2498 shsurf->last_width = surface->width;
2499 shsurf->last_height = surface->height;
2500
2501 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2502 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2503 x = shsurf->view->geometry.x + to_x - from_x;
2504 y = shsurf->view->geometry.y + to_y - from_y;
2505
2506 weston_view_set_position(shsurf->view, x, y);
2507 }
2508
2509 /* XXX: would a fullscreen surface need the same handling? */
2510 if (surface->output) {
2511 wl_list_for_each(view, &surface->views, surface_link)
2512 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002513 }
2514}
2515
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002516static void
2517set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2518 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002519{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002520 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2521 struct weston_surface *surface =
2522 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2523 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002524
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002525 if (fullscreen) {
2526 /* handle clients launching in fullscreen */
2527 if (output == NULL && !weston_surface_is_mapped(surface)) {
2528 /* Set the output to the one that has focus currently. */
2529 output = get_focused_output(surface->compositor);
2530 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002531
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002532 shell_surface_set_output(shsurf, output);
2533 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002534
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002535 width = shsurf->output->width;
2536 height = shsurf->output->height;
2537 } else {
2538 unset_fullscreen(shsurf);
2539 }
2540 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2541 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002542}
2543
2544static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002545desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2546 struct weston_seat *seat, uint32_t serial, void *shell)
2547{
2548 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2549 struct weston_touch *touch = weston_seat_get_touch(seat);
2550 struct shell_surface *shsurf =
2551 weston_desktop_surface_get_user_data(desktop_surface);
2552 struct weston_surface *surface =
2553 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2554 struct wl_resource *resource = surface->resource;
2555 struct weston_surface *focus;
2556
2557 if (pointer &&
2558 pointer->focus &&
2559 pointer->button_count > 0 &&
2560 pointer->grab_serial == serial) {
2561 focus = weston_surface_get_main_surface(pointer->focus->surface);
2562 if ((focus == surface) &&
2563 (surface_move(shsurf, pointer, true) < 0))
2564 wl_resource_post_no_memory(resource);
2565 } else if (touch &&
2566 touch->focus &&
2567 touch->grab_serial == serial) {
2568 focus = weston_surface_get_main_surface(touch->focus->surface);
2569 if ((focus == surface) &&
2570 (surface_touch_move(shsurf, touch) < 0))
2571 wl_resource_post_no_memory(resource);
2572 }
2573}
2574
2575static void
2576desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2577 struct weston_seat *seat, uint32_t serial,
2578 enum weston_desktop_surface_edge edges, void *shell)
2579{
2580 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2581 struct shell_surface *shsurf =
2582 weston_desktop_surface_get_user_data(desktop_surface);
2583 struct weston_surface *surface =
2584 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2585 struct wl_resource *resource = surface->resource;
2586 struct weston_surface *focus;
2587
2588 if (!pointer ||
2589 pointer->button_count == 0 ||
2590 pointer->grab_serial != serial ||
2591 pointer->focus == NULL)
2592 return;
2593
2594 focus = weston_surface_get_main_surface(pointer->focus->surface);
2595 if (focus != surface)
2596 return;
2597
2598 if (surface_resize(shsurf, pointer, edges) < 0)
2599 wl_resource_post_no_memory(resource);
2600}
2601
2602static void
2603desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2604 bool fullscreen,
2605 struct weston_output *output, void *shell)
2606{
2607 struct shell_surface *shsurf =
2608 weston_desktop_surface_get_user_data(desktop_surface);
2609
2610 set_fullscreen(shsurf, fullscreen, output);
2611}
2612
2613static void
2614set_maximized(struct shell_surface *shsurf, bool maximized)
2615{
2616 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2617 struct weston_surface *surface =
2618 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2619 int32_t width = 0, height = 0;
2620
2621 if (maximized) {
2622 struct weston_output *output;
2623 struct desktop_shell *shell;
2624 pixman_rectangle32_t area;
2625
2626 if (!weston_surface_is_mapped(surface))
2627 output = get_focused_output(surface->compositor);
2628 else
2629 output = surface->output;
2630
2631 shell_surface_set_output(shsurf, output);
2632
2633 shell = shell_surface_get_shell(shsurf);
2634 get_output_work_area(shell, shsurf->output, &area);
2635
2636 width = area.width;
2637 height = area.height;
2638 } else {
2639 unset_maximized(shsurf);
2640 }
2641 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2642 weston_desktop_surface_set_size(desktop_surface, width, height);
2643}
2644
2645static void
2646desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2647 bool maximized, void *shell)
2648{
2649 struct shell_surface *shsurf =
2650 weston_desktop_surface_get_user_data(desktop_surface);
2651
2652 set_maximized(shsurf, maximized);
2653}
2654
2655static void
2656desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2657 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002658{
2659 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002660 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002661
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002662 /* apply compositor's own minimization logic (hide) */
2663 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002664}
2665
Rafael Antognollie2a34552013-12-03 15:35:45 -02002666static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002667set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002668{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002669 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002670
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002671 if (pointer->grab->interface == &busy_cursor_grab_interface)
2672 return;
2673
2674 grab = malloc(sizeof *grab);
2675 if (!grab)
2676 return;
2677
2678 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2679 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2680 /* Mark the shsurf as ungrabbed so that button binding is able
2681 * to move it. */
2682 shsurf->grabbed = 0;
2683}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002684
2685static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002686end_busy_cursor(struct weston_compositor *compositor,
2687 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002688{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002689 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002690 struct shell_grab *grab;
2691 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002692
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002693 wl_list_for_each(seat, &compositor->seat_list, link) {
2694 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2695 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002696
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002697 if (!pointer)
2698 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002699
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002700 if (pointer->grab->interface != &busy_cursor_grab_interface)
2701 continue;
2702
2703 grab = (struct shell_grab *) pointer->grab;
2704 shsurf = grab->shsurf;
2705 if (!shsurf)
2706 continue;
2707
2708 grab_client =
2709 weston_desktop_surface_get_client(shsurf->desktop_surface);
2710 if (grab_client == desktop_client) {
2711 shell_grab_end(grab);
2712 free(grab);
2713 }
2714 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002715}
2716
2717static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002718desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2719 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002720{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002721 struct shell_surface *shsurf =
2722 weston_desktop_surface_get_user_data(desktop_surface);
2723 bool *unresponsive = user_data;
2724
2725 shsurf->unresponsive = *unresponsive;
2726}
2727
2728static void
2729desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2730 void *shell_)
2731{
2732 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002733 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002734 struct weston_seat *seat;
2735 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002736
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002737 weston_desktop_client_for_each_surface(desktop_client,
2738 desktop_surface_set_unresponsive,
2739 &unresponsive);
2740
2741
2742 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2743 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2744 struct weston_desktop_client *grab_client;
2745
2746 if (!pointer || !pointer->focus)
2747 continue;
2748
2749 shsurf = get_shell_surface(pointer->focus->surface);
2750 if (!shsurf)
2751 continue;
2752
2753 grab_client =
2754 weston_desktop_surface_get_client(shsurf->desktop_surface);
2755 if (grab_client == desktop_client)
2756 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002757 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002758}
2759
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002760static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002761desktop_surface_pong(struct weston_desktop_client *desktop_client,
2762 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002763{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002764 struct desktop_shell *shell = shell_;
2765 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002766
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002767 weston_desktop_client_for_each_surface(desktop_client,
2768 desktop_surface_set_unresponsive,
2769 &unresponsive);
2770 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002771}
2772
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002773static const struct weston_desktop_api shell_desktop_api = {
2774 .struct_size = sizeof(struct weston_desktop_api),
2775 .surface_added = desktop_surface_added,
2776 .surface_removed = desktop_surface_removed,
2777 .committed = desktop_surface_committed,
2778 .move = desktop_surface_move,
2779 .resize = desktop_surface_resize,
2780 .fullscreen_requested = desktop_surface_fullscreen_requested,
2781 .maximized_requested = desktop_surface_maximized_requested,
2782 .minimized_requested = desktop_surface_minimized_requested,
2783 .ping_timeout = desktop_surface_ping_timeout,
2784 .pong = desktop_surface_pong,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002785};
2786
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002787/* ************************ *
2788 * end of libweston-desktop *
2789 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002790static 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
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003773shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003774{
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003775 struct shell_output *shell_output = data;
3776 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003777
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003778 shell_output->fade.animation = NULL;
3779 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003780 case FADE_IN:
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003781 weston_surface_destroy(shell_output->fade.view->surface);
3782 shell_output->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 *
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003793shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
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
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003809 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3810 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
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;
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003825 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003826
3827 switch (type) {
3828 case FADE_IN:
3829 tint = 0.0;
3830 break;
3831 case FADE_OUT:
3832 tint = 1.0;
3833 break;
3834 default:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003835 return;
3836 }
3837
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003838 /* Create a separate fade surface for each output */
3839 wl_list_for_each(shell_output, &shell->output_list, link) {
3840 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003841
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003842 if (shell_output->fade.view == NULL) {
3843 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3844 if (!shell_output->fade.view)
3845 return;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003846
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003847 shell_output->fade.view->alpha = 1.0 - tint;
3848 weston_view_update_transform(shell_output->fade.view);
3849 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003850
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003851 if (shell_output->fade.view->output == NULL) {
3852 /* If the black view gets a NULL output, we lost the
3853 * last output and we'll just cancel the fade. This
3854 * happens when you close the last window under the
3855 * X11 or Wayland backends. */
3856 shell->locked = false;
3857 weston_surface_destroy(shell_output->fade.view->surface);
3858 shell_output->fade.view = NULL;
3859 } else if (shell_output->fade.animation) {
3860 weston_fade_update(shell_output->fade.animation, tint);
3861 } else {
3862 shell_output->fade.animation =
3863 weston_fade_run(shell_output->fade.view,
3864 1.0 - tint, tint, 300.0,
3865 shell_fade_done_for_output, shell_output);
3866 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08003867 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003868}
3869
3870static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003871do_shell_fade_startup(void *data)
3872{
3873 struct desktop_shell *shell = data;
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003874 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003875
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003876 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003877 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003878 } else {
3879 weston_log("desktop shell: "
3880 "unexpected fade-in animation type %d\n",
3881 shell->startup_animation_type);
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003882 wl_list_for_each(shell_output, &shell->output_list, link) {
3883 weston_surface_destroy(shell_output->fade.view->surface);
3884 shell_output->fade.view = NULL;
3885 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07003886 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003887}
3888
3889static void
3890shell_fade_startup(struct desktop_shell *shell)
3891{
3892 struct wl_event_loop *loop;
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003893 struct shell_output *shell_output;
3894 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003895
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003896 wl_list_for_each(shell_output, &shell->output_list, link) {
3897 if (!shell_output->fade.startup_timer)
3898 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003899
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003900 wl_event_source_remove(shell_output->fade.startup_timer);
3901 shell_output->fade.startup_timer = NULL;
3902 has_fade = true;
3903 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003904
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003905 if (has_fade) {
3906 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3907 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3908 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003909}
3910
3911static int
3912fade_startup_timeout(void *data)
3913{
3914 struct desktop_shell *shell = data;
3915
3916 shell_fade_startup(shell);
3917 return 0;
3918}
3919
3920static void
3921shell_fade_init(struct desktop_shell *shell)
3922{
3923 /* Make compositor output all black, and wait for the desktop-shell
3924 * client to signal it is ready, then fade in. The timer triggers a
3925 * fade-in, in case the desktop-shell client takes too long.
3926 */
3927
3928 struct wl_event_loop *loop;
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003929 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003930
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03003931 if (shell->startup_animation_type == ANIMATION_NONE)
3932 return;
3933
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003934 wl_list_for_each(shell_output, &shell->output_list, link) {
3935 if (shell_output->fade.view != NULL) {
3936 weston_log("%s: warning: fade surface already exists\n",
3937 __func__);
3938 continue;
3939 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003940
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003941 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
3942 if (!shell_output->fade.view)
3943 return;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003944
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003945 weston_view_update_transform(shell_output->fade.view);
3946 weston_surface_damage(shell_output->fade.view->surface);
3947
3948 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3949 shell_output->fade.startup_timer =
3950 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3951 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
3952 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003953}
3954
3955static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003956idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003957{
3958 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003959 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003960
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08003961 struct weston_seat *seat;
3962
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003963 wl_list_for_each(seat, &shell->compositor->seat_list, link)
3964 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003965
3966 shell_fade(shell, FADE_OUT);
Bryce Harringtonfde5adb2016-08-10 17:25:24 -07003967 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003968}
3969
3970static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003971wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003972{
3973 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003974 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003975
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003976 unlock(shell);
3977}
3978
3979static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003980transform_handler(struct wl_listener *listener, void *data)
3981{
3982 struct weston_surface *surface = data;
3983 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003984 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003985 int x, y;
3986
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003987 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003988 return;
3989
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003990 api = shsurf->shell->xwayland_surface_api;
3991 if (!api) {
3992 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
3993 shsurf->shell->xwayland_surface_api = api;
3994 }
3995
3996 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02003997 return;
3998
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003999 if (!weston_view_is_mapped(shsurf->view))
4000 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004001
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004002 x = shsurf->view->geometry.x;
4003 y = shsurf->view->geometry.y;
4004
4005 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004006}
4007
4008static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004009center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004010{
Giulio Camuffob8366642013-04-25 13:57:46 +03004011 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004012 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004013
Jason Ekstranda7af7042013-10-12 22:38:11 -05004014 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004015
4016 x = output->x + (output->width - width) / 2 - surf_x / 2;
4017 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004018
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004019 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004020}
4021
4022static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004023weston_view_set_initial_position(struct weston_view *view,
4024 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004025{
4026 struct weston_compositor *compositor = shell->compositor;
4027 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004028 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004029 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004030 struct weston_output *output, *target_output = NULL;
4031 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004032 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004033
4034 /* As a heuristic place the new window on the same output as the
4035 * pointer. Falling back to the output containing 0, 0.
4036 *
4037 * TODO: Do something clever for touch too?
4038 */
4039 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004040 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4041
4042 if (pointer) {
4043 ix = wl_fixed_to_int(pointer->x);
4044 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004045 break;
4046 }
4047 }
4048
4049 wl_list_for_each(output, &compositor->output_list, link) {
4050 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4051 target_output = output;
4052 break;
4053 }
4054 }
4055
4056 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 weston_view_set_position(view, 10 + random() % 400,
4058 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004059 return;
4060 }
4061
4062 /* Valid range within output where the surface will still be onscreen.
4063 * If this is negative it means that the surface is bigger than
4064 * output.
4065 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004066 get_output_work_area(shell, target_output, &area);
4067
Derek Foremanf814c5d2015-04-15 12:23:54 -05004068 x = area.x;
4069 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004070 range_x = area.width - view->surface->width;
4071 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004072
Rob Bradford4cb88c72012-08-13 15:18:44 +01004073 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004074 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004075
4076 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004077 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004078
Jason Ekstranda7af7042013-10-12 22:38:11 -05004079 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004080}
4081
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004082static bool
4083check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4084{
4085 struct timespec now;
4086
4087 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4088 return false;
4089
4090 /*
4091 * If the shell helper client dies before the session has been
4092 * up for roughly 30 seconds, better just make Weston shut down,
4093 * because the user likely has no way to interact with the desktop
4094 * anyway.
4095 */
4096 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4097 weston_log("Error: %s apparently cannot run at all.\n",
4098 shell->client);
4099 weston_log_continue(STAMP_SPACE "Quitting...");
4100 wl_display_terminate(shell->compositor->wl_display);
4101
4102 return true;
4103 }
4104
4105 return false;
4106}
4107
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004108static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004109
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004110static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004111respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004112{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004113 uint32_t time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004114
4115 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4116 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004117 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004118 shell->child.deathstamp = time;
4119 shell->child.deathcount = 0;
4120 }
4121
4122 shell->child.deathcount++;
4123 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004124 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004125 return;
4126 }
4127
Pekka Paalanen826dc142014-08-27 12:11:53 +03004128 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004129 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004130}
4131
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004132static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004133desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4134{
4135 struct desktop_shell *shell;
4136
4137 shell = container_of(listener, struct desktop_shell,
4138 child.client_destroy_listener);
4139
Pekka Paalanen826dc142014-08-27 12:11:53 +03004140 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004141 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004142 /*
4143 * unbind_desktop_shell() will reset shell->child.desktop_shell
4144 * before the respawned process has a chance to create a new
4145 * desktop_shell object, because we are being called from the
4146 * wl_client destructor which destroys all wl_resources before
4147 * returning.
4148 */
4149
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004150 if (!check_desktop_shell_crash_too_early(shell))
4151 respawn_desktop_shell_process(shell);
4152
Pekka Paalanen826dc142014-08-27 12:11:53 +03004153 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004154}
4155
4156static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004157launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004158{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004159 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004160
Pekka Paalanen826dc142014-08-27 12:11:53 +03004161 shell->child.client = weston_client_start(shell->compositor,
4162 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004163
Arnaud Vrac42631192014-08-25 20:56:46 +02004164 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004165 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004166 return;
4167 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004168
4169 shell->child.client_destroy_listener.notify =
4170 desktop_shell_client_destroy;
4171 wl_client_add_destroy_listener(shell->child.client,
4172 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004173}
4174
4175static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004176unbind_desktop_shell(struct wl_resource *resource)
4177{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004178 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004179
4180 if (shell->locked)
4181 resume_desktop(shell);
4182
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004183 shell->child.desktop_shell = NULL;
4184 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004185}
4186
4187static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004188bind_desktop_shell(struct wl_client *client,
4189 void *data, uint32_t version, uint32_t id)
4190{
Tiago Vignattibe143262012-04-16 17:31:41 +03004191 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004192 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004193
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004194 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4195 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004196
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004197 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004198 wl_resource_set_implementation(resource,
4199 &desktop_shell_implementation,
4200 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004201 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004202 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004203 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004204
4205 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4206 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004207}
4208
Kristian Høgsberg07045392012-02-19 18:52:44 -05004209struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004210 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004211 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004212 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004213 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004214 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004215};
4216
4217static void
4218switcher_next(struct switcher *switcher)
4219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004220 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004221 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004222 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004223 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004224
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004225 /* temporary re-display minimized surfaces */
4226 struct weston_view *tmp;
4227 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004228 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4229 weston_layer_entry_remove(&view->layer_link);
4230 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004231 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4232 *minimized = view;
4233 }
4234
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004235 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004236 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004237 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004238 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004239 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004240 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004241 next = view;
4242 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 view->alpha = 0.25;
4244 weston_view_geometry_dirty(view);
4245 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004246 }
Alex Wu1659daa2012-04-01 20:13:09 +08004247
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004248 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 view->alpha = 0.25;
4250 weston_view_geometry_dirty(view);
4251 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004252 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004253 }
4254
4255 if (next == NULL)
4256 next = first;
4257
Alex Wu07b26062012-03-12 16:06:01 +08004258 if (next == NULL)
4259 return;
4260
Kristian Høgsberg07045392012-02-19 18:52:44 -05004261 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004262 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004263
4264 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004265 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004266 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004267
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004268 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004269 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004271}
4272
4273static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004274switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004275{
4276 struct switcher *switcher =
4277 container_of(listener, struct switcher, listener);
4278
4279 switcher_next(switcher);
4280}
4281
4282static void
Daniel Stone351eb612012-05-31 15:27:47 -04004283switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004284{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004285 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004286 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004287 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004288
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004289 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004290 if (is_focus_view(view))
4291 continue;
4292
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 view->alpha = 1.0;
4294 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004295 }
4296
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004297 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004298 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004299 keyboard->seat,
4300 WESTON_ACTIVATE_FLAG_CONFIGURE);
4301 }
4302
Kristian Høgsberg07045392012-02-19 18:52:44 -05004303 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004304 weston_keyboard_end_grab(keyboard);
4305 if (keyboard->input_method_resource)
4306 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004307
4308 /* re-hide surfaces that were temporary shown during the switch */
4309 struct weston_view **minimized;
4310 wl_array_for_each(minimized, &switcher->minimized_array) {
4311 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004312 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004313 weston_layer_entry_remove(&(*minimized)->layer_link);
4314 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004315 weston_view_damage_below(*minimized);
4316 }
4317 }
4318 wl_array_release(&switcher->minimized_array);
4319
Kristian Høgsberg07045392012-02-19 18:52:44 -05004320 free(switcher);
4321}
4322
4323static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004324switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01004325 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004326{
4327 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004328 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004329
Daniel Stonec9785ea2012-05-30 16:31:52 +01004330 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004331 switcher_next(switcher);
4332}
4333
4334static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004335switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004336 uint32_t mods_depressed, uint32_t mods_latched,
4337 uint32_t mods_locked, uint32_t group)
4338{
4339 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004340 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004341
Daniel Stone351eb612012-05-31 15:27:47 -04004342 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4343 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004344}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004345
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004346static void
4347switcher_cancel(struct weston_keyboard_grab *grab)
4348{
4349 struct switcher *switcher = container_of(grab, struct switcher, grab);
4350
4351 switcher_destroy(switcher);
4352}
4353
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004354static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004355 switcher_key,
4356 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004357 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004358};
4359
4360static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004361switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
4362 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004363{
Tiago Vignattibe143262012-04-16 17:31:41 +03004364 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004365 struct switcher *switcher;
4366
4367 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004368 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004369 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004370 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004371 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004372 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004373
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004374 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004375 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004376 weston_keyboard_start_grab(keyboard, &switcher->grab);
4377 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004378 switcher_next(switcher);
4379}
4380
Pekka Paalanen3c647232011-12-22 13:43:43 +02004381static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004382backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
4383 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004384{
4385 struct weston_compositor *compositor = data;
4386 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004387 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004388
4389 /* TODO: we're limiting to simple use cases, where we assume just
4390 * control on the primary display. We'd have to extend later if we
4391 * ever get support for setting backlights on random desktop LCD
4392 * panels though */
4393 output = get_default_output(compositor);
4394 if (!output)
4395 return;
4396
4397 if (!output->set_backlight)
4398 return;
4399
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004400 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4401 backlight_new = output->backlight_current - 25;
4402 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4403 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004404
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004405 if (backlight_new < 5)
4406 backlight_new = 5;
4407 if (backlight_new > 255)
4408 backlight_new = 255;
4409
4410 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004411 output->set_backlight(output, output->backlight_current);
4412}
4413
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004414static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004415force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
4416 uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004417{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004418 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004419 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004420 struct desktop_shell *shell = data;
4421 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004422 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004423
Derek Foreman8ae2db52015-07-15 13:00:36 -05004424 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004425 if (!focus_surface)
4426 return;
4427
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004428 wl_signal_emit(&compositor->kill_signal, focus_surface);
4429
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004430 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004431 wl_client_get_credentials(client, &pid, NULL, NULL);
4432
4433 /* Skip clients that we launched ourselves (the credentials of
4434 * the socketpair is ours) */
4435 if (pid == getpid())
4436 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004437
Daniel Stone325fc2d2012-05-30 16:31:58 +01004438 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004439}
4440
4441static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004442workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004443 uint32_t key, void *data)
4444{
4445 struct desktop_shell *shell = data;
4446 unsigned int new_index = shell->workspaces.current;
4447
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004448 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004449 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004450 if (new_index != 0)
4451 new_index--;
4452
4453 change_workspace(shell, new_index);
4454}
4455
4456static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004457workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004458 uint32_t key, void *data)
4459{
4460 struct desktop_shell *shell = data;
4461 unsigned int new_index = shell->workspaces.current;
4462
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004463 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004464 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004465 if (new_index < shell->workspaces.num - 1)
4466 new_index++;
4467
4468 change_workspace(shell, new_index);
4469}
4470
4471static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004472workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004473 uint32_t key, void *data)
4474{
4475 struct desktop_shell *shell = data;
4476 unsigned int new_index;
4477
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004478 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004479 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004480 new_index = key - KEY_F1;
4481 if (new_index >= shell->workspaces.num)
4482 new_index = shell->workspaces.num - 1;
4483
4484 change_workspace(shell, new_index);
4485}
4486
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004487static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004488workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4489 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004490{
4491 struct desktop_shell *shell = data;
4492 unsigned int new_index = shell->workspaces.current;
4493
4494 if (shell->locked)
4495 return;
4496
4497 if (new_index != 0)
4498 new_index--;
4499
Derek Foreman8ae2db52015-07-15 13:00:36 -05004500 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004501}
4502
4503static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004504workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4505 uint32_t time, uint32_t key, void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004506{
4507 struct desktop_shell *shell = data;
4508 unsigned int new_index = shell->workspaces.current;
4509
4510 if (shell->locked)
4511 return;
4512
4513 if (new_index < shell->workspaces.num - 1)
4514 new_index++;
4515
Derek Foreman8ae2db52015-07-15 13:00:36 -05004516 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004517}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004518
4519static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004520shell_reposition_view_on_output_destroy(struct weston_view *view)
4521{
4522 struct weston_output *output, *first_output;
4523 struct weston_compositor *ec = view->surface->compositor;
4524 struct shell_surface *shsurf;
4525 float x, y;
4526 int visible;
4527
4528 x = view->geometry.x;
4529 y = view->geometry.y;
4530
4531 /* At this point the destroyed output is not in the list anymore.
4532 * If the view is still visible somewhere, we leave where it is,
4533 * otherwise, move it to the first output. */
4534 visible = 0;
4535 wl_list_for_each(output, &ec->output_list, link) {
4536 if (pixman_region32_contains_point(&output->region,
4537 x, y, NULL)) {
4538 visible = 1;
4539 break;
4540 }
4541 }
4542
4543 if (!visible) {
4544 first_output = container_of(ec->output_list.next,
4545 struct weston_output, link);
4546
4547 x = first_output->x + first_output->width / 4;
4548 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004549
4550 weston_view_set_position(view, x, y);
4551 } else {
4552 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004553 }
4554
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004555
4556 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004557 if (!shsurf)
4558 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004559
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004560 shsurf->saved_position_valid = false;
4561 set_maximized(shsurf, false);
4562 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004563}
4564
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004565void
4566shell_for_each_layer(struct desktop_shell *shell,
4567 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004568{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004569 struct workspace **ws;
4570
4571 func(shell, &shell->fullscreen_layer, data);
4572 func(shell, &shell->panel_layer, data);
4573 func(shell, &shell->background_layer, data);
4574 func(shell, &shell->lock_layer, data);
4575 func(shell, &shell->input_panel_layer, data);
4576
4577 wl_array_for_each(ws, &shell->workspaces.array)
4578 func(shell, &(*ws)->layer, data);
4579}
4580
4581static void
4582shell_output_destroy_move_layer(struct desktop_shell *shell,
4583 struct weston_layer *layer,
4584 void *data)
4585{
4586 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004587 struct weston_view *view;
4588
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004589 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004590 if (view->output != output)
4591 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004592
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004593 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004594 }
4595}
4596
4597static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004598handle_output_destroy(struct wl_listener *listener, void *data)
4599{
4600 struct shell_output *output_listener =
4601 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004602 struct weston_output *output = output_listener->output;
4603 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004604
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004605 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004606
Quentin Glidic561201c2016-08-20 18:19:51 +02004607 wl_list_remove(&output_listener->panel_surface_listener.link);
4608 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004609 wl_list_remove(&output_listener->destroy_listener.link);
4610 wl_list_remove(&output_listener->link);
4611 free(output_listener);
4612}
4613
4614static void
David Fort50d962f2016-06-09 17:55:52 +02004615shell_resize_surface_to_output(struct desktop_shell *shell,
4616 struct weston_surface *surface,
4617 const struct weston_output *output)
4618{
4619 if (!surface)
4620 return;
4621
4622 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4623 surface->resource,
4624 output->width,
4625 output->height);
4626}
4627
4628
4629static void
4630handle_output_resized(struct wl_listener *listener, void *data)
4631{
4632 struct desktop_shell *shell =
4633 container_of(listener, struct desktop_shell, resized_listener);
4634 struct weston_output *output = (struct weston_output *)data;
4635 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4636
4637 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4638 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4639}
4640
4641static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004642create_shell_output(struct desktop_shell *shell,
4643 struct weston_output *output)
4644{
4645 struct shell_output *shell_output;
4646
4647 shell_output = zalloc(sizeof *shell_output);
4648 if (shell_output == NULL)
4649 return;
4650
4651 shell_output->output = output;
4652 shell_output->shell = shell;
4653 shell_output->destroy_listener.notify = handle_output_destroy;
4654 wl_signal_add(&output->destroy_signal,
4655 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004656 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004657}
4658
4659static void
4660handle_output_create(struct wl_listener *listener, void *data)
4661{
4662 struct desktop_shell *shell =
4663 container_of(listener, struct desktop_shell, output_create_listener);
4664 struct weston_output *output = (struct weston_output *)data;
4665
4666 create_shell_output(shell, output);
4667}
4668
4669static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004670handle_output_move_layer(struct desktop_shell *shell,
4671 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004672{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004673 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004674 struct weston_view *view;
4675 float x, y;
4676
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004677 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004678 if (view->output != output)
4679 continue;
4680
4681 x = view->geometry.x + output->move_x;
4682 y = view->geometry.y + output->move_y;
4683 weston_view_set_position(view, x, y);
4684 }
4685}
4686
4687static void
4688handle_output_move(struct wl_listener *listener, void *data)
4689{
4690 struct desktop_shell *shell;
4691
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004692 shell = container_of(listener, struct desktop_shell,
4693 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004694
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004695 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004696}
4697
4698static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004699setup_output_destroy_handler(struct weston_compositor *ec,
4700 struct desktop_shell *shell)
4701{
4702 struct weston_output *output;
4703
4704 wl_list_init(&shell->output_list);
4705 wl_list_for_each(output, &ec->output_list, link)
4706 create_shell_output(shell, output);
4707
4708 shell->output_create_listener.notify = handle_output_create;
4709 wl_signal_add(&ec->output_created_signal,
4710 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004711
4712 shell->output_move_listener.notify = handle_output_move;
4713 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004714}
4715
4716static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004717shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004718{
Tiago Vignattibe143262012-04-16 17:31:41 +03004719 struct desktop_shell *shell =
4720 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004721 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004722 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004723
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004724 /* Force state to unlocked so we don't try to fade */
4725 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004726
4727 if (shell->child.client) {
4728 /* disable respawn */
4729 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004730 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004731 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004732
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004733 wl_list_remove(&shell->idle_listener.link);
4734 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004735 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004736
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004737 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004738 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004739
Xiong Zhang6b481422013-10-23 13:58:32 +08004740 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4741 wl_list_remove(&shell_output->destroy_listener.link);
4742 wl_list_remove(&shell_output->link);
4743 free(shell_output);
4744 }
4745
4746 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004747 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004748 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004749
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004750 wl_array_for_each(ws, &shell->workspaces.array)
4751 workspace_destroy(*ws);
4752 wl_array_release(&shell->workspaces.array);
4753
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004754 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004755 free(shell);
4756}
4757
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004758static void
4759shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4760{
4761 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004762 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004763
Bob Ham744e6532016-01-12 10:21:48 +00004764 if (shell->allow_zap)
4765 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4766 MODIFIER_CTRL | MODIFIER_ALT,
4767 terminate_binding, ec);
4768
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004769 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004770 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4771 click_to_activate_binding,
4772 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004773 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4774 click_to_activate_binding,
4775 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004776 weston_compositor_add_touch_binding(ec, 0,
4777 touch_to_activate_binding,
4778 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004779 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4780 MODIFIER_SUPER | MODIFIER_ALT,
4781 surface_opacity_binding, NULL);
4782 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4783 MODIFIER_SUPER, zoom_axis_binding,
4784 NULL);
Bob Ham553d1242016-01-12 10:21:49 +00004785 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4786 backlight_binding, ec);
4787 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4788 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004789
4790 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004791 if (shell->exposay_modifier)
4792 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4793 exposay_binding, shell);
4794
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004795 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004796 if (!mod)
4797 return;
4798
Daniel Stone325fc2d2012-05-30 16:31:58 +01004799 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4800 zoom_key_binding, NULL);
4801 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4802 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004803 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4804 maximize_binding, NULL);
4805 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4806 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004807 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4808 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01004809 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05004810 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004811 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08004812 weston_compositor_add_button_binding(ec, BTN_LEFT,
4813 mod | MODIFIER_SHIFT,
4814 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004815
4816 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05004817 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004818 rotate_binding, NULL);
4819
Daniel Stone325fc2d2012-05-30 16:31:58 +01004820 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4821 shell);
4822 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4823 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4825 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826 weston_compositor_add_key_binding(ec, KEY_K, mod,
4827 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004828 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4829 workspace_up_binding, shell);
4830 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4831 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004832 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4833 workspace_move_surface_up_binding,
4834 shell);
4835 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4836 workspace_move_surface_down_binding,
4837 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004838
4839 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4840 if (shell->workspaces.num > 1) {
4841 num_workspace_bindings = shell->workspaces.num;
4842 if (num_workspace_bindings > 6)
4843 num_workspace_bindings = 6;
4844 for (i = 0; i < num_workspace_bindings; i++)
4845 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4846 workspace_f_binding,
4847 shell);
4848 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004849
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02004850 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004851}
4852
Jason Ekstrand024177c2014-04-21 19:42:58 -05004853static void
4854handle_seat_created(struct wl_listener *listener, void *data)
4855{
4856 struct weston_seat *seat = data;
4857
4858 create_shell_seat(seat);
4859}
4860
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004861WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004862module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004863 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004864{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004865 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004866 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004867 struct workspace **pws;
4868 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004869 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004870
Peter Huttererf3d62272013-08-08 11:57:05 +10004871 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004872 if (shell == NULL)
4873 return -1;
4874
Kristian Høgsberg75840622011-09-06 13:48:16 -04004875 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004876
4877 shell->destroy_listener.notify = shell_destroy;
4878 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004879 shell->idle_listener.notify = idle_handler;
4880 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4881 shell->wake_listener.notify = wake_handler;
4882 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004883 shell->transform_listener.notify = transform_handler;
4884 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004885
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004886 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4887 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004888 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4889 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004890 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004891
4892 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004893 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004894
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004895 if (input_panel_setup(shell) < 0)
4896 return -1;
4897
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004898 shell->text_backend = text_backend_init(ec);
4899 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00004900 return -1;
4901
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004902 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004903
Daniel Stonedf8133b2013-11-19 11:37:14 +01004904 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
4905 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
4906
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004907 for (i = 0; i < shell->workspaces.num; i++) {
4908 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4909 if (pws == NULL)
4910 return -1;
4911
4912 *pws = workspace_create();
4913 if (*pws == NULL)
4914 return -1;
4915 }
4916 activate_workspace(shell, 0);
4917
Manuel Bachmann50c87db2014-02-26 15:52:13 +01004918 weston_layer_init(&shell->minimized_layer, NULL);
4919
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004920 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004921 wl_list_init(&shell->workspaces.animation.link);
4922 shell->workspaces.animation.frame = animate_workspace_change_frame;
4923
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004924 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
4925 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004926 return -1;
4927
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004928 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004929 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004930 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004931 return -1;
4932
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004933 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004934
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004935 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02004936
Xiong Zhang6b481422013-10-23 13:58:32 +08004937 setup_output_destroy_handler(ec, shell);
4938
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004939 loop = wl_display_get_event_loop(ec->wl_display);
4940 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004941
Jason Ekstrand024177c2014-04-21 19:42:58 -05004942 wl_list_for_each(seat, &ec->seat_list, link)
4943 handle_seat_created(NULL, seat);
4944 shell->seat_create_listener.notify = handle_seat_created;
4945 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004946
David Fort50d962f2016-06-09 17:55:52 +02004947 shell->resized_listener.notify = handle_output_resized;
4948 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
4949
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01004950 screenshooter_create(ec);
4951
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004952 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004953
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004954 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004955
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004956 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
4957
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004958 return 0;
4959}