blob: ea2a37512b8353a638c2b28ca5f889c0a853e2ba [file] [log] [blame]
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08001/*
2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
5 *
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øgsberg1ef23132013-12-04 00:20:01 -080012 *
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øgsberg1ef23132013-12-04 00:20:01 -080024 */
25
26#include "config.h"
27
28#include <linux/input.h>
29
30#include "shell.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070031#include "shared/helpers.h"
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080032
33struct exposay_surface {
34 struct desktop_shell *shell;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +010035 struct exposay_output *eoutput;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080036 struct weston_surface *surface;
37 struct weston_view *view;
Emilio Pozuelo Monfortf942b732014-02-10 14:23:04 +010038 struct wl_listener view_destroy_listener;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039 struct wl_list link;
40
41 int x;
42 int y;
43 int width;
44 int height;
45 double scale;
46
47 int row;
48 int column;
49
50 /* The animations only apply a transformation for their own lifetime,
51 * and don't have an option to indefinitely maintain the
52 * transformation in a steady state - so, we apply our own once the
53 * animation has finished. */
54 struct weston_transform transform;
55};
56
57static void exposay_set_state(struct desktop_shell *shell,
58 enum exposay_target_state state,
59 struct weston_seat *seat);
60static void exposay_check_state(struct desktop_shell *shell);
61
62static void
Emilio Pozuelo Monfortf942b732014-02-10 14:23:04 +010063exposay_surface_destroy(struct exposay_surface *esurface)
64{
65 wl_list_remove(&esurface->link);
66 wl_list_remove(&esurface->view_destroy_listener.link);
67
68 if (esurface->shell->exposay.focus_current == esurface->view)
69 esurface->shell->exposay.focus_current = NULL;
70 if (esurface->shell->exposay.focus_prev == esurface->view)
71 esurface->shell->exposay.focus_prev = NULL;
72
73 free(esurface);
74}
75
76static void
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080077exposay_in_flight_inc(struct desktop_shell *shell)
78{
79 shell->exposay.in_flight++;
80}
81
82static void
83exposay_in_flight_dec(struct desktop_shell *shell)
84{
85 if (--shell->exposay.in_flight > 0)
86 return;
87
88 exposay_check_state(shell);
89}
90
91static void
92exposay_animate_in_done(struct weston_view_animation *animation, void *data)
93{
94 struct exposay_surface *esurface = data;
95
96 wl_list_insert(&esurface->view->geometry.transformation_list,
97 &esurface->transform.link);
98 weston_matrix_init(&esurface->transform.matrix);
99 weston_matrix_scale(&esurface->transform.matrix,
100 esurface->scale, esurface->scale, 1.0f);
101 weston_matrix_translate(&esurface->transform.matrix,
102 esurface->x - esurface->view->geometry.x,
103 esurface->y - esurface->view->geometry.y,
104 0);
105
106 weston_view_geometry_dirty(esurface->view);
107 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
108
109 exposay_in_flight_dec(esurface->shell);
110}
111
112static void
113exposay_animate_in(struct exposay_surface *esurface)
114{
115 exposay_in_flight_inc(esurface->shell);
116
117 weston_move_scale_run(esurface->view,
118 esurface->x - esurface->view->geometry.x,
119 esurface->y - esurface->view->geometry.y,
120 1.0, esurface->scale, 0,
121 exposay_animate_in_done, esurface);
122}
123
124static void
125exposay_animate_out_done(struct weston_view_animation *animation, void *data)
126{
127 struct exposay_surface *esurface = data;
128 struct desktop_shell *shell = esurface->shell;
129
Emilio Pozuelo Monfortf942b732014-02-10 14:23:04 +0100130 exposay_surface_destroy(esurface);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800131
132 exposay_in_flight_dec(shell);
133}
134
135static void
136exposay_animate_out(struct exposay_surface *esurface)
137{
138 exposay_in_flight_inc(esurface->shell);
139
140 /* Remove the static transformation set up by
141 * exposay_transform_in_done(). */
142 wl_list_remove(&esurface->transform.link);
143 weston_view_geometry_dirty(esurface->view);
144
145 weston_move_scale_run(esurface->view,
146 esurface->x - esurface->view->geometry.x,
147 esurface->y - esurface->view->geometry.y,
148 1.0, esurface->scale, 1,
149 exposay_animate_out_done, esurface);
150}
151
152static void
153exposay_highlight_surface(struct desktop_shell *shell,
154 struct exposay_surface *esurface)
155{
U. Artie Eoff6d6d1902014-01-15 13:42:18 -0800156 struct weston_view *view = esurface->view;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800157
Emilio Pozuelo Monforta7592012014-02-10 16:52:33 +0100158 if (shell->exposay.focus_current == view)
159 return;
160
U. Artie Eoff6d6d1902014-01-15 13:42:18 -0800161 shell->exposay.row_current = esurface->row;
162 shell->exposay.column_current = esurface->column;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100163 shell->exposay.cur_output = esurface->eoutput;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800164
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100165 activate(shell, view->surface, shell->exposay.seat, false);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800166 shell->exposay.focus_current = view;
167}
168
169static int
170exposay_is_animating(struct desktop_shell *shell)
171{
172 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
173 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
174 return 0;
175
176 return (shell->exposay.in_flight > 0);
177}
178
179static void
180exposay_pick(struct desktop_shell *shell, int x, int y)
181{
182 struct exposay_surface *esurface;
183
184 if (exposay_is_animating(shell))
185 return;
186
187 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
188 if (x < esurface->x || x > esurface->x + esurface->width)
189 continue;
190 if (y < esurface->y || y > esurface->y + esurface->height)
191 continue;
192
193 exposay_highlight_surface(shell, esurface);
194 return;
195 }
196}
197
Emilio Pozuelo Monfortf942b732014-02-10 14:23:04 +0100198static void
199handle_view_destroy(struct wl_listener *listener, void *data)
200{
201 struct exposay_surface *esurface = container_of(listener,
202 struct exposay_surface,
203 view_destroy_listener);
204
205 exposay_surface_destroy(esurface);
206}
207
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800208/* Pretty lame layout for now; just tries to make a square. Should take
209 * aspect ratio into account really. Also needs to be notified of surface
210 * addition and removal and adjust layout/animate accordingly. */
211static enum exposay_layout_state
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100212exposay_layout(struct desktop_shell *shell, struct shell_output *shell_output)
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800213{
214 struct workspace *workspace = shell->exposay.workspace;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100215 struct weston_output *output = shell_output->output;
216 struct exposay_output *eoutput = &shell_output->eoutput;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800217 struct weston_view *view;
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100218 struct exposay_surface *esurface, *highlight = NULL;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800219 int w, h;
220 int i;
221 int last_row_removed = 0;
222
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100223 eoutput->num_surfaces = 0;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300224 wl_list_for_each(view, &workspace->layer.view_list.link, layer_link.link) {
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800225 if (!get_shell_surface(view->surface))
226 continue;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100227 if (view->output != output)
228 continue;
229 eoutput->num_surfaces++;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800230 }
231
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100232 if (eoutput->num_surfaces == 0) {
233 eoutput->grid_size = 0;
234 eoutput->hpadding_outer = 0;
235 eoutput->vpadding_outer = 0;
236 eoutput->padding_inner = 0;
237 eoutput->surface_size = 0;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800238 return EXPOSAY_LAYOUT_OVERVIEW;
239 }
240
241 /* Lay the grid out as square as possible, losing surfaces from the
242 * bottom row if required. Start with fixed padding of a 10% margin
243 * around the outside and 80px internal padding between surfaces, and
244 * maximise the area made available to surfaces after this, but only
245 * to a maximum of 1/3rd the total output size.
246 *
247 * If we can't make a square grid, add one extra row at the bottom
248 * which will have a smaller number of columns.
249 *
250 * XXX: Surely there has to be a better way to express this maths,
251 * right?!
252 */
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100253 eoutput->grid_size = floor(sqrtf(eoutput->num_surfaces));
254 if (pow(eoutput->grid_size, 2) != eoutput->num_surfaces)
255 eoutput->grid_size++;
256 last_row_removed = pow(eoutput->grid_size, 2) - eoutput->num_surfaces;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800257
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100258 eoutput->hpadding_outer = (output->width / 10);
259 eoutput->vpadding_outer = (output->height / 10);
260 eoutput->padding_inner = 80;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800261
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100262 w = output->width - (eoutput->hpadding_outer * 2);
263 w -= eoutput->padding_inner * (eoutput->grid_size - 1);
264 w /= eoutput->grid_size;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800265
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100266 h = output->height - (eoutput->vpadding_outer * 2);
267 h -= eoutput->padding_inner * (eoutput->grid_size - 1);
268 h /= eoutput->grid_size;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800269
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100270 eoutput->surface_size = (w < h) ? w : h;
271 if (eoutput->surface_size > (output->width / 2))
272 eoutput->surface_size = output->width / 2;
273 if (eoutput->surface_size > (output->height / 2))
274 eoutput->surface_size = output->height / 2;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800275
276 i = 0;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300277 wl_list_for_each(view, &workspace->layer.view_list.link, layer_link.link) {
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800278 int pad;
279
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100280 pad = eoutput->surface_size + eoutput->padding_inner;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800281
282 if (!get_shell_surface(view->surface))
283 continue;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100284 if (view->output != output)
285 continue;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800286
287 esurface = malloc(sizeof(*esurface));
288 if (!esurface) {
289 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
290 shell->exposay.seat);
291 break;
292 }
293
294 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
295 esurface->shell = shell;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100296 esurface->eoutput = eoutput;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800297 esurface->view = view;
298
Emilio Pozuelo Monfortf942b732014-02-10 14:23:04 +0100299 esurface->view_destroy_listener.notify = handle_view_destroy;
300 wl_signal_add(&view->destroy_signal, &esurface->view_destroy_listener);
301
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100302 esurface->row = i / eoutput->grid_size;
303 esurface->column = i % eoutput->grid_size;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800304
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100305 esurface->x = output->x + eoutput->hpadding_outer;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800306 esurface->x += pad * esurface->column;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100307 esurface->y = output->y + eoutput->vpadding_outer;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800308 esurface->y += pad * esurface->row;
309
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100310 if (esurface->row == eoutput->grid_size - 1)
311 esurface->x += (eoutput->surface_size + eoutput->padding_inner) * last_row_removed / 2;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800312
313 if (view->surface->width > view->surface->height)
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100314 esurface->scale = eoutput->surface_size / (float) view->surface->width;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800315 else
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100316 esurface->scale = eoutput->surface_size / (float) view->surface->height;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800317 esurface->width = view->surface->width * esurface->scale;
318 esurface->height = view->surface->height * esurface->scale;
319
320 if (shell->exposay.focus_current == esurface->view)
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100321 highlight = esurface;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800322
323 exposay_animate_in(esurface);
324
325 i++;
326 }
327
Emilio Pozuelo Monfort5905ebc2014-05-24 02:43:04 +0200328 if (highlight) {
329 shell->exposay.focus_current = NULL;
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100330 exposay_highlight_surface(shell, highlight);
Emilio Pozuelo Monfort5905ebc2014-05-24 02:43:04 +0200331 }
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100332
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800333 weston_compositor_schedule_repaint(shell->compositor);
334
335 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
336}
337
338static void
339exposay_focus(struct weston_pointer_grab *grab)
340{
341}
342
343static void
344exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
345 wl_fixed_t x, wl_fixed_t y)
346{
347 struct desktop_shell *shell =
348 container_of(grab, struct desktop_shell, exposay.grab_ptr);
349
350 weston_pointer_move(grab->pointer, x, y);
351
352 exposay_pick(shell,
353 wl_fixed_to_int(grab->pointer->x),
354 wl_fixed_to_int(grab->pointer->y));
355}
356
357static void
358exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
359 uint32_t state_w)
360{
361 struct desktop_shell *shell =
362 container_of(grab, struct desktop_shell, exposay.grab_ptr);
363 struct weston_seat *seat = grab->pointer->seat;
364 enum wl_pointer_button_state state = state_w;
365
366 if (button != BTN_LEFT)
367 return;
368
369 /* Store the surface we clicked on, and don't do anything if we end up
370 * releasing on a different surface. */
371 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
372 shell->exposay.clicked = shell->exposay.focus_current;
373 return;
374 }
375
376 if (shell->exposay.focus_current == shell->exposay.clicked)
377 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
378 else
379 shell->exposay.clicked = NULL;
380}
381
382static void
383exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
384{
385 struct desktop_shell *shell =
386 container_of(grab, struct desktop_shell, exposay.grab_ptr);
387
388 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
389}
390
391static const struct weston_pointer_grab_interface exposay_ptr_grab = {
392 exposay_focus,
393 exposay_motion,
394 exposay_button,
395 exposay_pointer_grab_cancel,
396};
397
398static int
399exposay_maybe_move(struct desktop_shell *shell, int row, int column)
400{
401 struct exposay_surface *esurface;
402
403 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100404 if (esurface->eoutput != shell->exposay.cur_output ||
405 esurface->row != row || esurface->column != column)
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800406 continue;
407
408 exposay_highlight_surface(shell, esurface);
409 return 1;
410 }
411
412 return 0;
413}
414
415static void
416exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
417 uint32_t state_w)
418{
419 struct weston_seat *seat = grab->keyboard->seat;
420 struct desktop_shell *shell =
421 container_of(grab, struct desktop_shell, exposay.grab_kbd);
422 enum wl_keyboard_key_state state = state_w;
423
424 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
425 return;
426
427 switch (key) {
428 case KEY_ESC:
429 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
430 break;
431 case KEY_ENTER:
432 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
433 break;
434 case KEY_UP:
435 exposay_maybe_move(shell, shell->exposay.row_current - 1,
436 shell->exposay.column_current);
437 break;
438 case KEY_DOWN:
439 /* Special case for trying to move to the bottom row when it
440 * has fewer items than all the others. */
441 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
442 shell->exposay.column_current) &&
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100443 shell->exposay.row_current < (shell->exposay.cur_output->grid_size - 1)) {
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800444 exposay_maybe_move(shell, shell->exposay.row_current + 1,
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100445 (shell->exposay.cur_output->num_surfaces %
446 shell->exposay.cur_output->grid_size) - 1);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800447 }
448 break;
449 case KEY_LEFT:
450 exposay_maybe_move(shell, shell->exposay.row_current,
451 shell->exposay.column_current - 1);
452 break;
453 case KEY_RIGHT:
454 exposay_maybe_move(shell, shell->exposay.row_current,
455 shell->exposay.column_current + 1);
456 break;
457 case KEY_TAB:
458 /* Try to move right, then down (and to the leftmost column),
459 * then if all else fails, to the top left. */
460 if (!exposay_maybe_move(shell, shell->exposay.row_current,
461 shell->exposay.column_current + 1) &&
462 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
463 exposay_maybe_move(shell, 0, 0);
464 break;
465 default:
466 break;
467 }
468}
469
470static void
471exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
472 uint32_t mods_depressed, uint32_t mods_latched,
473 uint32_t mods_locked, uint32_t group)
474{
475 struct desktop_shell *shell =
476 container_of(grab, struct desktop_shell, exposay.grab_kbd);
477 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
478
479 /* We want to know when mod has been pressed and released.
480 * FIXME: There is a problem here: if mod is pressed, then a key
481 * is pressed and released, then mod is released, we will treat that
482 * as if only mod had been pressed and released. */
483 if (seat->modifier_state) {
484 if (seat->modifier_state == shell->binding_modifier) {
485 shell->exposay.mod_pressed = true;
486 } else {
487 shell->exposay.mod_invalid = true;
488 }
489 } else {
490 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
491 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
492
493 shell->exposay.mod_invalid = false;
494 shell->exposay.mod_pressed = false;
495 }
496
497 return;
498}
499
500static void
501exposay_cancel(struct weston_keyboard_grab *grab)
502{
503 struct desktop_shell *shell =
504 container_of(grab, struct desktop_shell, exposay.grab_kbd);
505
506 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
507}
508
509static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
510 exposay_key,
511 exposay_modifier,
512 exposay_cancel,
513};
514
515/**
516 * Called when the transition from overview -> inactive has completed.
517 */
518static enum exposay_layout_state
519exposay_set_inactive(struct desktop_shell *shell)
520{
521 struct weston_seat *seat = shell->exposay.seat;
522
523 weston_keyboard_end_grab(seat->keyboard);
Derek Foreman3dd570e2015-05-22 11:47:20 -0500524 if (seat->pointer_device_count)
525 weston_pointer_end_grab(seat->pointer);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800526 if (seat->keyboard->input_method_resource)
527 seat->keyboard->grab = &seat->keyboard->input_method_grab;
528
529 return EXPOSAY_LAYOUT_INACTIVE;
530}
531
532/**
533 * Begins the transition from overview to inactive. */
534static enum exposay_layout_state
535exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
536{
537 struct exposay_surface *esurface;
538
539 /* Call activate() before we start the animations to avoid
540 * animating back the old state and then immediately transitioning
541 * to the new. */
542 if (switch_focus && shell->exposay.focus_current)
543 activate(shell, shell->exposay.focus_current->surface,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100544 shell->exposay.seat, true);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800545 else if (shell->exposay.focus_prev)
546 activate(shell, shell->exposay.focus_prev->surface,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100547 shell->exposay.seat, true);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800548
549 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
550 exposay_animate_out(esurface);
551 weston_compositor_schedule_repaint(shell->compositor);
552
553 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
554}
555
556static enum exposay_layout_state
557exposay_transition_active(struct desktop_shell *shell)
558{
559 struct weston_seat *seat = shell->exposay.seat;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100560 struct shell_output *shell_output;
561 bool animate = false;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800562
563 shell->exposay.workspace = get_current_workspace(shell);
564 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
565 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
566 shell->exposay.clicked = NULL;
567 wl_list_init(&shell->exposay.surface_list);
568
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200569 lower_fullscreen_layer(shell, NULL);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800570 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
571 weston_keyboard_start_grab(seat->keyboard,
572 &shell->exposay.grab_kbd);
573 weston_keyboard_set_focus(seat->keyboard, NULL);
574
575 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
Derek Foreman3dd570e2015-05-22 11:47:20 -0500576 if (seat->pointer_device_count) {
577 weston_pointer_start_grab(seat->pointer,
578 &shell->exposay.grab_ptr);
579 weston_pointer_set_focus(seat->pointer, NULL,
580 seat->pointer->x,
581 seat->pointer->y);
582 }
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100583 wl_list_for_each(shell_output, &shell->output_list, link) {
584 enum exposay_layout_state state;
585
586 state = exposay_layout(shell, shell_output);
587
588 if (state == EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW)
589 animate = true;
590 }
591
592 return animate ? EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW
593 : EXPOSAY_LAYOUT_OVERVIEW;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800594}
595
596static void
597exposay_check_state(struct desktop_shell *shell)
598{
599 enum exposay_layout_state state_new = shell->exposay.state_cur;
600 int do_switch = 0;
601
602 /* Don't do anything whilst animations are running, just store up
603 * target state changes and only act on them when the animations have
604 * completed. */
605 if (exposay_is_animating(shell))
606 return;
607
608 switch (shell->exposay.state_target) {
609 case EXPOSAY_TARGET_OVERVIEW:
610 switch (shell->exposay.state_cur) {
611 case EXPOSAY_LAYOUT_OVERVIEW:
612 goto out;
613 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
614 state_new = EXPOSAY_LAYOUT_OVERVIEW;
615 break;
616 default:
617 state_new = exposay_transition_active(shell);
618 break;
619 }
620 break;
621
622 case EXPOSAY_TARGET_SWITCH:
623 do_switch = 1; /* fallthrough */
624 case EXPOSAY_TARGET_CANCEL:
625 switch (shell->exposay.state_cur) {
626 case EXPOSAY_LAYOUT_INACTIVE:
627 goto out;
628 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
629 state_new = exposay_set_inactive(shell);
630 break;
631 default:
632 state_new = exposay_transition_inactive(shell, do_switch);
633 break;
634 }
635
636 break;
637 }
638
639out:
640 shell->exposay.state_cur = state_new;
641}
642
643static void
644exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
645 struct weston_seat *seat)
646{
647 shell->exposay.state_target = state;
648 shell->exposay.seat = seat;
649 exposay_check_state(shell);
650}
651
652void
653exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
654 void *data)
655{
656 struct desktop_shell *shell = data;
657
658 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
659}