blob: f409aa3a198c6a7254f00d197ad4b4bed7126961 [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 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#include "config.h"
26
27#include <linux/input.h>
28
29#include "shell.h"
30
31struct exposay_surface {
32 struct desktop_shell *shell;
33 struct weston_surface *surface;
34 struct weston_view *view;
35 struct wl_list link;
36
37 int x;
38 int y;
39 int width;
40 int height;
41 double scale;
42
43 int row;
44 int column;
45
46 /* The animations only apply a transformation for their own lifetime,
47 * and don't have an option to indefinitely maintain the
48 * transformation in a steady state - so, we apply our own once the
49 * animation has finished. */
50 struct weston_transform transform;
51};
52
53static void exposay_set_state(struct desktop_shell *shell,
54 enum exposay_target_state state,
55 struct weston_seat *seat);
56static void exposay_check_state(struct desktop_shell *shell);
57
58static void
59exposay_in_flight_inc(struct desktop_shell *shell)
60{
61 shell->exposay.in_flight++;
62}
63
64static void
65exposay_in_flight_dec(struct desktop_shell *shell)
66{
67 if (--shell->exposay.in_flight > 0)
68 return;
69
70 exposay_check_state(shell);
71}
72
73static void
74exposay_animate_in_done(struct weston_view_animation *animation, void *data)
75{
76 struct exposay_surface *esurface = data;
77
78 wl_list_insert(&esurface->view->geometry.transformation_list,
79 &esurface->transform.link);
80 weston_matrix_init(&esurface->transform.matrix);
81 weston_matrix_scale(&esurface->transform.matrix,
82 esurface->scale, esurface->scale, 1.0f);
83 weston_matrix_translate(&esurface->transform.matrix,
84 esurface->x - esurface->view->geometry.x,
85 esurface->y - esurface->view->geometry.y,
86 0);
87
88 weston_view_geometry_dirty(esurface->view);
89 weston_compositor_schedule_repaint(esurface->view->surface->compositor);
90
91 exposay_in_flight_dec(esurface->shell);
92}
93
94static void
95exposay_animate_in(struct exposay_surface *esurface)
96{
97 exposay_in_flight_inc(esurface->shell);
98
99 weston_move_scale_run(esurface->view,
100 esurface->x - esurface->view->geometry.x,
101 esurface->y - esurface->view->geometry.y,
102 1.0, esurface->scale, 0,
103 exposay_animate_in_done, esurface);
104}
105
106static void
107exposay_animate_out_done(struct weston_view_animation *animation, void *data)
108{
109 struct exposay_surface *esurface = data;
110 struct desktop_shell *shell = esurface->shell;
111
112 wl_list_remove(&esurface->link);
113 free(esurface);
114
115 exposay_in_flight_dec(shell);
116}
117
118static void
119exposay_animate_out(struct exposay_surface *esurface)
120{
121 exposay_in_flight_inc(esurface->shell);
122
123 /* Remove the static transformation set up by
124 * exposay_transform_in_done(). */
125 wl_list_remove(&esurface->transform.link);
126 weston_view_geometry_dirty(esurface->view);
127
128 weston_move_scale_run(esurface->view,
129 esurface->x - esurface->view->geometry.x,
130 esurface->y - esurface->view->geometry.y,
131 1.0, esurface->scale, 1,
132 exposay_animate_out_done, esurface);
133}
134
135static void
136exposay_highlight_surface(struct desktop_shell *shell,
137 struct exposay_surface *esurface)
138{
139 struct weston_view *view = NULL;
140
141 if (esurface) {
142 shell->exposay.row_current = esurface->row;
143 shell->exposay.column_current = esurface->column;
144 view = esurface->view;
145 }
146
147 activate(shell, view->surface, shell->exposay.seat);
148 shell->exposay.focus_current = view;
149}
150
151static int
152exposay_is_animating(struct desktop_shell *shell)
153{
154 if (shell->exposay.state_cur == EXPOSAY_LAYOUT_INACTIVE ||
155 shell->exposay.state_cur == EXPOSAY_LAYOUT_OVERVIEW)
156 return 0;
157
158 return (shell->exposay.in_flight > 0);
159}
160
161static void
162exposay_pick(struct desktop_shell *shell, int x, int y)
163{
164 struct exposay_surface *esurface;
165
166 if (exposay_is_animating(shell))
167 return;
168
169 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
170 if (x < esurface->x || x > esurface->x + esurface->width)
171 continue;
172 if (y < esurface->y || y > esurface->y + esurface->height)
173 continue;
174
175 exposay_highlight_surface(shell, esurface);
176 return;
177 }
178}
179
180/* Pretty lame layout for now; just tries to make a square. Should take
181 * aspect ratio into account really. Also needs to be notified of surface
182 * addition and removal and adjust layout/animate accordingly. */
183static enum exposay_layout_state
184exposay_layout(struct desktop_shell *shell)
185{
186 struct workspace *workspace = shell->exposay.workspace;
187 struct weston_compositor *compositor = shell->compositor;
188 struct weston_output *output = get_default_output(compositor);
189 struct weston_view *view;
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100190 struct exposay_surface *esurface, *highlight = NULL;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800191 int w, h;
192 int i;
193 int last_row_removed = 0;
194
195 wl_list_init(&shell->exposay.surface_list);
196
197 shell->exposay.num_surfaces = 0;
198 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
199 if (!get_shell_surface(view->surface))
200 continue;
201 shell->exposay.num_surfaces++;
202 }
203
204 if (shell->exposay.num_surfaces == 0) {
205 shell->exposay.grid_size = 0;
206 shell->exposay.hpadding_outer = 0;
207 shell->exposay.vpadding_outer = 0;
208 shell->exposay.padding_inner = 0;
209 shell->exposay.surface_size = 0;
210 return EXPOSAY_LAYOUT_OVERVIEW;
211 }
212
213 /* Lay the grid out as square as possible, losing surfaces from the
214 * bottom row if required. Start with fixed padding of a 10% margin
215 * around the outside and 80px internal padding between surfaces, and
216 * maximise the area made available to surfaces after this, but only
217 * to a maximum of 1/3rd the total output size.
218 *
219 * If we can't make a square grid, add one extra row at the bottom
220 * which will have a smaller number of columns.
221 *
222 * XXX: Surely there has to be a better way to express this maths,
223 * right?!
224 */
225 shell->exposay.grid_size = floor(sqrtf(shell->exposay.num_surfaces));
226 if (pow(shell->exposay.grid_size, 2) != shell->exposay.num_surfaces)
227 shell->exposay.grid_size++;
228 last_row_removed = pow(shell->exposay.grid_size, 2) - shell->exposay.num_surfaces;
229
230 shell->exposay.hpadding_outer = (output->width / 10);
231 shell->exposay.vpadding_outer = (output->height / 10);
232 shell->exposay.padding_inner = 80;
233
234 w = output->width - (shell->exposay.hpadding_outer * 2);
235 w -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
236 w /= shell->exposay.grid_size;
237
238 h = output->height - (shell->exposay.vpadding_outer * 2);
239 h -= shell->exposay.padding_inner * (shell->exposay.grid_size - 1);
240 h /= shell->exposay.grid_size;
241
242 shell->exposay.surface_size = (w < h) ? w : h;
243 if (shell->exposay.surface_size > (output->width / 2))
244 shell->exposay.surface_size = output->width / 2;
245 if (shell->exposay.surface_size > (output->height / 2))
246 shell->exposay.surface_size = output->height / 2;
247
248 i = 0;
249 wl_list_for_each(view, &workspace->layer.view_list, layer_link) {
250 int pad;
251
252 pad = shell->exposay.surface_size + shell->exposay.padding_inner;
253
254 if (!get_shell_surface(view->surface))
255 continue;
256
257 esurface = malloc(sizeof(*esurface));
258 if (!esurface) {
259 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL,
260 shell->exposay.seat);
261 break;
262 }
263
264 wl_list_insert(&shell->exposay.surface_list, &esurface->link);
265 esurface->shell = shell;
266 esurface->view = view;
267
268 esurface->row = i / shell->exposay.grid_size;
269 esurface->column = i % shell->exposay.grid_size;
270
271 esurface->x = shell->exposay.hpadding_outer;
272 esurface->x += pad * esurface->column;
273 esurface->y = shell->exposay.vpadding_outer;
274 esurface->y += pad * esurface->row;
275
276 if (esurface->row == shell->exposay.grid_size - 1)
277 esurface->x += (shell->exposay.surface_size + shell->exposay.padding_inner) * last_row_removed / 2;
278
279 if (view->surface->width > view->surface->height)
280 esurface->scale = shell->exposay.surface_size / (float) view->surface->width;
281 else
282 esurface->scale = shell->exposay.surface_size / (float) view->surface->height;
283 esurface->width = view->surface->width * esurface->scale;
284 esurface->height = view->surface->height * esurface->scale;
285
286 if (shell->exposay.focus_current == esurface->view)
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100287 highlight = esurface;
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800288
289 exposay_animate_in(esurface);
290
291 i++;
292 }
293
Emilio Pozuelo Monforte6bbe5a2014-01-07 16:41:39 +0100294 if (highlight)
295 exposay_highlight_surface(shell, highlight);
296
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800297 weston_compositor_schedule_repaint(shell->compositor);
298
299 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
300}
301
302static void
303exposay_focus(struct weston_pointer_grab *grab)
304{
305}
306
307static void
308exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
309 wl_fixed_t x, wl_fixed_t y)
310{
311 struct desktop_shell *shell =
312 container_of(grab, struct desktop_shell, exposay.grab_ptr);
313
314 weston_pointer_move(grab->pointer, x, y);
315
316 exposay_pick(shell,
317 wl_fixed_to_int(grab->pointer->x),
318 wl_fixed_to_int(grab->pointer->y));
319}
320
321static void
322exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
323 uint32_t state_w)
324{
325 struct desktop_shell *shell =
326 container_of(grab, struct desktop_shell, exposay.grab_ptr);
327 struct weston_seat *seat = grab->pointer->seat;
328 enum wl_pointer_button_state state = state_w;
329
330 if (button != BTN_LEFT)
331 return;
332
333 /* Store the surface we clicked on, and don't do anything if we end up
334 * releasing on a different surface. */
335 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
336 shell->exposay.clicked = shell->exposay.focus_current;
337 return;
338 }
339
340 if (shell->exposay.focus_current == shell->exposay.clicked)
341 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
342 else
343 shell->exposay.clicked = NULL;
344}
345
346static void
347exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
348{
349 struct desktop_shell *shell =
350 container_of(grab, struct desktop_shell, exposay.grab_ptr);
351
352 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
353}
354
355static const struct weston_pointer_grab_interface exposay_ptr_grab = {
356 exposay_focus,
357 exposay_motion,
358 exposay_button,
359 exposay_pointer_grab_cancel,
360};
361
362static int
363exposay_maybe_move(struct desktop_shell *shell, int row, int column)
364{
365 struct exposay_surface *esurface;
366
367 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
368 if (esurface->row != row || esurface->column != column)
369 continue;
370
371 exposay_highlight_surface(shell, esurface);
372 return 1;
373 }
374
375 return 0;
376}
377
378static void
379exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
380 uint32_t state_w)
381{
382 struct weston_seat *seat = grab->keyboard->seat;
383 struct desktop_shell *shell =
384 container_of(grab, struct desktop_shell, exposay.grab_kbd);
385 enum wl_keyboard_key_state state = state_w;
386
387 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
388 return;
389
390 switch (key) {
391 case KEY_ESC:
392 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
393 break;
394 case KEY_ENTER:
395 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
396 break;
397 case KEY_UP:
398 exposay_maybe_move(shell, shell->exposay.row_current - 1,
399 shell->exposay.column_current);
400 break;
401 case KEY_DOWN:
402 /* Special case for trying to move to the bottom row when it
403 * has fewer items than all the others. */
404 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
405 shell->exposay.column_current) &&
406 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
407 exposay_maybe_move(shell, shell->exposay.row_current + 1,
408 (shell->exposay.num_surfaces %
409 shell->exposay.grid_size) - 1);
410 }
411 break;
412 case KEY_LEFT:
413 exposay_maybe_move(shell, shell->exposay.row_current,
414 shell->exposay.column_current - 1);
415 break;
416 case KEY_RIGHT:
417 exposay_maybe_move(shell, shell->exposay.row_current,
418 shell->exposay.column_current + 1);
419 break;
420 case KEY_TAB:
421 /* Try to move right, then down (and to the leftmost column),
422 * then if all else fails, to the top left. */
423 if (!exposay_maybe_move(shell, shell->exposay.row_current,
424 shell->exposay.column_current + 1) &&
425 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
426 exposay_maybe_move(shell, 0, 0);
427 break;
428 default:
429 break;
430 }
431}
432
433static void
434exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
435 uint32_t mods_depressed, uint32_t mods_latched,
436 uint32_t mods_locked, uint32_t group)
437{
438 struct desktop_shell *shell =
439 container_of(grab, struct desktop_shell, exposay.grab_kbd);
440 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
441
442 /* We want to know when mod has been pressed and released.
443 * FIXME: There is a problem here: if mod is pressed, then a key
444 * is pressed and released, then mod is released, we will treat that
445 * as if only mod had been pressed and released. */
446 if (seat->modifier_state) {
447 if (seat->modifier_state == shell->binding_modifier) {
448 shell->exposay.mod_pressed = true;
449 } else {
450 shell->exposay.mod_invalid = true;
451 }
452 } else {
453 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
454 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
455
456 shell->exposay.mod_invalid = false;
457 shell->exposay.mod_pressed = false;
458 }
459
460 return;
461}
462
463static void
464exposay_cancel(struct weston_keyboard_grab *grab)
465{
466 struct desktop_shell *shell =
467 container_of(grab, struct desktop_shell, exposay.grab_kbd);
468
469 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
470}
471
472static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
473 exposay_key,
474 exposay_modifier,
475 exposay_cancel,
476};
477
478/**
479 * Called when the transition from overview -> inactive has completed.
480 */
481static enum exposay_layout_state
482exposay_set_inactive(struct desktop_shell *shell)
483{
484 struct weston_seat *seat = shell->exposay.seat;
485
486 weston_keyboard_end_grab(seat->keyboard);
487 weston_pointer_end_grab(seat->pointer);
488 if (seat->keyboard->input_method_resource)
489 seat->keyboard->grab = &seat->keyboard->input_method_grab;
490
491 return EXPOSAY_LAYOUT_INACTIVE;
492}
493
494/**
495 * Begins the transition from overview to inactive. */
496static enum exposay_layout_state
497exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
498{
499 struct exposay_surface *esurface;
500
501 /* Call activate() before we start the animations to avoid
502 * animating back the old state and then immediately transitioning
503 * to the new. */
504 if (switch_focus && shell->exposay.focus_current)
505 activate(shell, shell->exposay.focus_current->surface,
506 shell->exposay.seat);
507 else if (shell->exposay.focus_prev)
508 activate(shell, shell->exposay.focus_prev->surface,
509 shell->exposay.seat);
510
511 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
512 exposay_animate_out(esurface);
513 weston_compositor_schedule_repaint(shell->compositor);
514
515 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
516}
517
518static enum exposay_layout_state
519exposay_transition_active(struct desktop_shell *shell)
520{
521 struct weston_seat *seat = shell->exposay.seat;
522
523 shell->exposay.workspace = get_current_workspace(shell);
524 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
525 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
526 shell->exposay.clicked = NULL;
527 wl_list_init(&shell->exposay.surface_list);
528
529 lower_fullscreen_layer(shell);
530 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
531 weston_keyboard_start_grab(seat->keyboard,
532 &shell->exposay.grab_kbd);
533 weston_keyboard_set_focus(seat->keyboard, NULL);
534
535 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
536 weston_pointer_start_grab(seat->pointer,
537 &shell->exposay.grab_ptr);
538 weston_pointer_set_focus(seat->pointer, NULL,
539 seat->pointer->x, seat->pointer->y);
540
541 return exposay_layout(shell);
542}
543
544static void
545exposay_check_state(struct desktop_shell *shell)
546{
547 enum exposay_layout_state state_new = shell->exposay.state_cur;
548 int do_switch = 0;
549
550 /* Don't do anything whilst animations are running, just store up
551 * target state changes and only act on them when the animations have
552 * completed. */
553 if (exposay_is_animating(shell))
554 return;
555
556 switch (shell->exposay.state_target) {
557 case EXPOSAY_TARGET_OVERVIEW:
558 switch (shell->exposay.state_cur) {
559 case EXPOSAY_LAYOUT_OVERVIEW:
560 goto out;
561 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
562 state_new = EXPOSAY_LAYOUT_OVERVIEW;
563 break;
564 default:
565 state_new = exposay_transition_active(shell);
566 break;
567 }
568 break;
569
570 case EXPOSAY_TARGET_SWITCH:
571 do_switch = 1; /* fallthrough */
572 case EXPOSAY_TARGET_CANCEL:
573 switch (shell->exposay.state_cur) {
574 case EXPOSAY_LAYOUT_INACTIVE:
575 goto out;
576 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
577 state_new = exposay_set_inactive(shell);
578 break;
579 default:
580 state_new = exposay_transition_inactive(shell, do_switch);
581 break;
582 }
583
584 break;
585 }
586
587out:
588 shell->exposay.state_cur = state_new;
589}
590
591static void
592exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
593 struct weston_seat *seat)
594{
595 shell->exposay.state_target = state;
596 shell->exposay.seat = seat;
597 exposay_check_state(shell);
598}
599
600void
601exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
602 void *data)
603{
604 struct desktop_shell *shell = data;
605
606 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
607}