blob: bff424f9428167289305b08b14cd4e4c7cd7dca5 [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
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200165 activate(shell, view, 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 Monfort3b6e68e2014-02-10 13:22:32 +0100299 esurface->row = i / eoutput->grid_size;
300 esurface->column = i % eoutput->grid_size;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800301
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100302 esurface->x = output->x + eoutput->hpadding_outer;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800303 esurface->x += pad * esurface->column;
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100304 esurface->y = output->y + eoutput->vpadding_outer;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800305 esurface->y += pad * esurface->row;
306
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100307 if (esurface->row == eoutput->grid_size - 1)
308 esurface->x += (eoutput->surface_size + eoutput->padding_inner) * last_row_removed / 2;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800309
310 if (view->surface->width > view->surface->height)
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100311 esurface->scale = eoutput->surface_size / (float) view->surface->width;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800312 else
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100313 esurface->scale = eoutput->surface_size / (float) view->surface->height;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800314 esurface->width = view->surface->width * esurface->scale;
315 esurface->height = view->surface->height * esurface->scale;
316
317 if (shell->exposay.focus_current == esurface->view)
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100318 highlight = esurface;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800319
320 exposay_animate_in(esurface);
321
Derek Foremand8156e22015-05-26 16:21:05 -0500322 /* We want our destroy handler to be after the animation
323 * destroy handler in the list, this way when the view is
324 * destroyed, the animation can safely call the animation
325 * completion callback before we free the esurface in our
326 * destroy handler.
327 */
328 esurface->view_destroy_listener.notify = handle_view_destroy;
329 wl_signal_add(&view->destroy_signal, &esurface->view_destroy_listener);
330
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800331 i++;
332 }
333
Emilio Pozuelo Monfort5905ebc2014-05-24 02:43:04 +0200334 if (highlight) {
335 shell->exposay.focus_current = NULL;
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100336 exposay_highlight_surface(shell, highlight);
Emilio Pozuelo Monfort5905ebc2014-05-24 02:43:04 +0200337 }
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100338
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800339 weston_compositor_schedule_repaint(shell->compositor);
340
341 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
342}
343
344static void
345exposay_focus(struct weston_pointer_grab *grab)
346{
347}
348
349static void
350exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200351 struct weston_pointer_motion_event *event)
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800352{
353 struct desktop_shell *shell =
354 container_of(grab, struct desktop_shell, exposay.grab_ptr);
355
Jonas Ådahld2510102014-10-05 21:39:14 +0200356 weston_pointer_move(grab->pointer, event);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800357
358 exposay_pick(shell,
359 wl_fixed_to_int(grab->pointer->x),
360 wl_fixed_to_int(grab->pointer->y));
361}
362
363static void
364exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
365 uint32_t state_w)
366{
367 struct desktop_shell *shell =
368 container_of(grab, struct desktop_shell, exposay.grab_ptr);
369 struct weston_seat *seat = grab->pointer->seat;
370 enum wl_pointer_button_state state = state_w;
371
372 if (button != BTN_LEFT)
373 return;
374
375 /* Store the surface we clicked on, and don't do anything if we end up
376 * releasing on a different surface. */
377 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
378 shell->exposay.clicked = shell->exposay.focus_current;
379 return;
380 }
381
382 if (shell->exposay.focus_current == shell->exposay.clicked)
383 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
384 else
385 shell->exposay.clicked = NULL;
386}
387
388static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200389exposay_axis(struct weston_pointer_grab *grab,
Peter Hutterer89b6a492016-01-18 15:58:17 +1000390 uint32_t time, struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200391{
392}
393
394static void
Peter Hutterer87743e92016-01-18 16:38:22 +1000395exposay_axis_source(struct weston_pointer_grab *grab, uint32_t source)
396{
397}
398
399static void
400exposay_frame(struct weston_pointer_grab *grab)
401{
402}
403
404static void
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800405exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
406{
407 struct desktop_shell *shell =
408 container_of(grab, struct desktop_shell, exposay.grab_ptr);
409
410 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
411}
412
413static const struct weston_pointer_grab_interface exposay_ptr_grab = {
414 exposay_focus,
415 exposay_motion,
416 exposay_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +0200417 exposay_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +1000418 exposay_axis_source,
419 exposay_frame,
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800420 exposay_pointer_grab_cancel,
421};
422
423static int
424exposay_maybe_move(struct desktop_shell *shell, int row, int column)
425{
426 struct exposay_surface *esurface;
427
428 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100429 if (esurface->eoutput != shell->exposay.cur_output ||
430 esurface->row != row || esurface->column != column)
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800431 continue;
432
433 exposay_highlight_surface(shell, esurface);
434 return 1;
435 }
436
437 return 0;
438}
439
440static void
441exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
442 uint32_t state_w)
443{
444 struct weston_seat *seat = grab->keyboard->seat;
445 struct desktop_shell *shell =
446 container_of(grab, struct desktop_shell, exposay.grab_kbd);
447 enum wl_keyboard_key_state state = state_w;
448
449 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
450 return;
451
452 switch (key) {
453 case KEY_ESC:
454 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
455 break;
456 case KEY_ENTER:
457 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
458 break;
459 case KEY_UP:
460 exposay_maybe_move(shell, shell->exposay.row_current - 1,
461 shell->exposay.column_current);
462 break;
463 case KEY_DOWN:
464 /* Special case for trying to move to the bottom row when it
465 * has fewer items than all the others. */
466 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
467 shell->exposay.column_current) &&
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100468 shell->exposay.row_current < (shell->exposay.cur_output->grid_size - 1)) {
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800469 exposay_maybe_move(shell, shell->exposay.row_current + 1,
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100470 (shell->exposay.cur_output->num_surfaces %
471 shell->exposay.cur_output->grid_size) - 1);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800472 }
473 break;
474 case KEY_LEFT:
475 exposay_maybe_move(shell, shell->exposay.row_current,
476 shell->exposay.column_current - 1);
477 break;
478 case KEY_RIGHT:
479 exposay_maybe_move(shell, shell->exposay.row_current,
480 shell->exposay.column_current + 1);
481 break;
482 case KEY_TAB:
483 /* Try to move right, then down (and to the leftmost column),
484 * then if all else fails, to the top left. */
485 if (!exposay_maybe_move(shell, shell->exposay.row_current,
486 shell->exposay.column_current + 1) &&
487 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
488 exposay_maybe_move(shell, 0, 0);
489 break;
490 default:
491 break;
492 }
493}
494
495static void
496exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
497 uint32_t mods_depressed, uint32_t mods_latched,
498 uint32_t mods_locked, uint32_t group)
499{
500 struct desktop_shell *shell =
501 container_of(grab, struct desktop_shell, exposay.grab_kbd);
502 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
503
504 /* We want to know when mod has been pressed and released.
505 * FIXME: There is a problem here: if mod is pressed, then a key
506 * is pressed and released, then mod is released, we will treat that
507 * as if only mod had been pressed and released. */
508 if (seat->modifier_state) {
509 if (seat->modifier_state == shell->binding_modifier) {
510 shell->exposay.mod_pressed = true;
511 } else {
512 shell->exposay.mod_invalid = true;
513 }
514 } else {
515 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
516 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
517
518 shell->exposay.mod_invalid = false;
519 shell->exposay.mod_pressed = false;
520 }
521
522 return;
523}
524
525static void
526exposay_cancel(struct weston_keyboard_grab *grab)
527{
528 struct desktop_shell *shell =
529 container_of(grab, struct desktop_shell, exposay.grab_kbd);
530
531 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
532}
533
534static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
535 exposay_key,
536 exposay_modifier,
537 exposay_cancel,
538};
539
540/**
541 * Called when the transition from overview -> inactive has completed.
542 */
543static enum exposay_layout_state
544exposay_set_inactive(struct desktop_shell *shell)
545{
546 struct weston_seat *seat = shell->exposay.seat;
Derek Foreman1281a362015-07-31 16:55:32 -0500547 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
548 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800549
Derek Foreman1281a362015-07-31 16:55:32 -0500550 if (pointer)
551 weston_pointer_end_grab(pointer);
Derek Foremancee82d62015-07-15 13:00:33 -0500552
Derek Foreman1281a362015-07-31 16:55:32 -0500553 if (keyboard) {
554 weston_keyboard_end_grab(keyboard);
555 if (keyboard->input_method_resource)
556 keyboard->grab = &keyboard->input_method_grab;
Derek Foremancee82d62015-07-15 13:00:33 -0500557 }
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800558
559 return EXPOSAY_LAYOUT_INACTIVE;
560}
561
562/**
563 * Begins the transition from overview to inactive. */
564static enum exposay_layout_state
565exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
566{
567 struct exposay_surface *esurface;
568
569 /* Call activate() before we start the animations to avoid
570 * animating back the old state and then immediately transitioning
571 * to the new. */
572 if (switch_focus && shell->exposay.focus_current)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200573 activate(shell, shell->exposay.focus_current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100574 shell->exposay.seat, true);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800575 else if (shell->exposay.focus_prev)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200576 activate(shell, shell->exposay.focus_prev,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100577 shell->exposay.seat, true);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800578
579 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
580 exposay_animate_out(esurface);
581 weston_compositor_schedule_repaint(shell->compositor);
582
583 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
584}
585
586static enum exposay_layout_state
587exposay_transition_active(struct desktop_shell *shell)
588{
589 struct weston_seat *seat = shell->exposay.seat;
Derek Foreman1281a362015-07-31 16:55:32 -0500590 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
591 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100592 struct shell_output *shell_output;
593 bool animate = false;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800594
595 shell->exposay.workspace = get_current_workspace(shell);
Derek Foreman1281a362015-07-31 16:55:32 -0500596 shell->exposay.focus_prev = get_default_view(keyboard->focus);
597 shell->exposay.focus_current = get_default_view(keyboard->focus);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800598 shell->exposay.clicked = NULL;
599 wl_list_init(&shell->exposay.surface_list);
600
Mario Kleiner9f4d6552015-06-21 21:25:08 +0200601 lower_fullscreen_layer(shell, NULL);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800602 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
Derek Foreman1281a362015-07-31 16:55:32 -0500603 weston_keyboard_start_grab(keyboard,
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800604 &shell->exposay.grab_kbd);
Derek Foreman1281a362015-07-31 16:55:32 -0500605 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800606
607 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
Derek Foreman1281a362015-07-31 16:55:32 -0500608 if (pointer) {
609 weston_pointer_start_grab(pointer,
Derek Foreman3dd570e2015-05-22 11:47:20 -0500610 &shell->exposay.grab_ptr);
Derek Foremanf9318d12015-05-11 15:40:11 -0500611 weston_pointer_clear_focus(pointer);
Derek Foreman3dd570e2015-05-22 11:47:20 -0500612 }
Emilio Pozuelo Monfort3b6e68e2014-02-10 13:22:32 +0100613 wl_list_for_each(shell_output, &shell->output_list, link) {
614 enum exposay_layout_state state;
615
616 state = exposay_layout(shell, shell_output);
617
618 if (state == EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW)
619 animate = true;
620 }
621
622 return animate ? EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW
623 : EXPOSAY_LAYOUT_OVERVIEW;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800624}
625
626static void
627exposay_check_state(struct desktop_shell *shell)
628{
629 enum exposay_layout_state state_new = shell->exposay.state_cur;
630 int do_switch = 0;
631
632 /* Don't do anything whilst animations are running, just store up
633 * target state changes and only act on them when the animations have
634 * completed. */
635 if (exposay_is_animating(shell))
636 return;
637
638 switch (shell->exposay.state_target) {
639 case EXPOSAY_TARGET_OVERVIEW:
640 switch (shell->exposay.state_cur) {
641 case EXPOSAY_LAYOUT_OVERVIEW:
642 goto out;
643 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
644 state_new = EXPOSAY_LAYOUT_OVERVIEW;
645 break;
646 default:
647 state_new = exposay_transition_active(shell);
648 break;
649 }
650 break;
651
652 case EXPOSAY_TARGET_SWITCH:
653 do_switch = 1; /* fallthrough */
654 case EXPOSAY_TARGET_CANCEL:
655 switch (shell->exposay.state_cur) {
656 case EXPOSAY_LAYOUT_INACTIVE:
657 goto out;
658 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
659 state_new = exposay_set_inactive(shell);
660 break;
661 default:
662 state_new = exposay_transition_inactive(shell, do_switch);
663 break;
664 }
665
666 break;
667 }
668
669out:
670 shell->exposay.state_cur = state_new;
671}
672
673static void
674exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
675 struct weston_seat *seat)
676{
677 shell->exposay.state_target = state;
678 shell->exposay.seat = seat;
679 exposay_check_state(shell);
680}
681
682void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500683exposay_binding(struct weston_keyboard *keyboard, enum weston_keyboard_modifier modifier,
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800684 void *data)
685{
686 struct desktop_shell *shell = data;
687
Derek Foreman8ae2db52015-07-15 13:00:36 -0500688 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, keyboard->seat);
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800689}