blob: 81da00ad70deaf67cadde093d00b17ea815b7562 [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;
190 struct exposay_surface *esurface;
191 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)
287 exposay_highlight_surface(shell, esurface);
288
289 exposay_animate_in(esurface);
290
291 i++;
292 }
293
294 weston_compositor_schedule_repaint(shell->compositor);
295
296 return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW;
297}
298
299static void
300exposay_focus(struct weston_pointer_grab *grab)
301{
302}
303
304static void
305exposay_motion(struct weston_pointer_grab *grab, uint32_t time,
306 wl_fixed_t x, wl_fixed_t y)
307{
308 struct desktop_shell *shell =
309 container_of(grab, struct desktop_shell, exposay.grab_ptr);
310
311 weston_pointer_move(grab->pointer, x, y);
312
313 exposay_pick(shell,
314 wl_fixed_to_int(grab->pointer->x),
315 wl_fixed_to_int(grab->pointer->y));
316}
317
318static void
319exposay_button(struct weston_pointer_grab *grab, uint32_t time, uint32_t button,
320 uint32_t state_w)
321{
322 struct desktop_shell *shell =
323 container_of(grab, struct desktop_shell, exposay.grab_ptr);
324 struct weston_seat *seat = grab->pointer->seat;
325 enum wl_pointer_button_state state = state_w;
326
327 if (button != BTN_LEFT)
328 return;
329
330 /* Store the surface we clicked on, and don't do anything if we end up
331 * releasing on a different surface. */
332 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
333 shell->exposay.clicked = shell->exposay.focus_current;
334 return;
335 }
336
337 if (shell->exposay.focus_current == shell->exposay.clicked)
338 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
339 else
340 shell->exposay.clicked = NULL;
341}
342
343static void
344exposay_pointer_grab_cancel(struct weston_pointer_grab *grab)
345{
346 struct desktop_shell *shell =
347 container_of(grab, struct desktop_shell, exposay.grab_ptr);
348
349 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
350}
351
352static const struct weston_pointer_grab_interface exposay_ptr_grab = {
353 exposay_focus,
354 exposay_motion,
355 exposay_button,
356 exposay_pointer_grab_cancel,
357};
358
359static int
360exposay_maybe_move(struct desktop_shell *shell, int row, int column)
361{
362 struct exposay_surface *esurface;
363
364 wl_list_for_each(esurface, &shell->exposay.surface_list, link) {
365 if (esurface->row != row || esurface->column != column)
366 continue;
367
368 exposay_highlight_surface(shell, esurface);
369 return 1;
370 }
371
372 return 0;
373}
374
375static void
376exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key,
377 uint32_t state_w)
378{
379 struct weston_seat *seat = grab->keyboard->seat;
380 struct desktop_shell *shell =
381 container_of(grab, struct desktop_shell, exposay.grab_kbd);
382 enum wl_keyboard_key_state state = state_w;
383
384 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
385 return;
386
387 switch (key) {
388 case KEY_ESC:
389 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
390 break;
391 case KEY_ENTER:
392 exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat);
393 break;
394 case KEY_UP:
395 exposay_maybe_move(shell, shell->exposay.row_current - 1,
396 shell->exposay.column_current);
397 break;
398 case KEY_DOWN:
399 /* Special case for trying to move to the bottom row when it
400 * has fewer items than all the others. */
401 if (!exposay_maybe_move(shell, shell->exposay.row_current + 1,
402 shell->exposay.column_current) &&
403 shell->exposay.row_current < (shell->exposay.grid_size - 1)) {
404 exposay_maybe_move(shell, shell->exposay.row_current + 1,
405 (shell->exposay.num_surfaces %
406 shell->exposay.grid_size) - 1);
407 }
408 break;
409 case KEY_LEFT:
410 exposay_maybe_move(shell, shell->exposay.row_current,
411 shell->exposay.column_current - 1);
412 break;
413 case KEY_RIGHT:
414 exposay_maybe_move(shell, shell->exposay.row_current,
415 shell->exposay.column_current + 1);
416 break;
417 case KEY_TAB:
418 /* Try to move right, then down (and to the leftmost column),
419 * then if all else fails, to the top left. */
420 if (!exposay_maybe_move(shell, shell->exposay.row_current,
421 shell->exposay.column_current + 1) &&
422 !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0))
423 exposay_maybe_move(shell, 0, 0);
424 break;
425 default:
426 break;
427 }
428}
429
430static void
431exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
432 uint32_t mods_depressed, uint32_t mods_latched,
433 uint32_t mods_locked, uint32_t group)
434{
435 struct desktop_shell *shell =
436 container_of(grab, struct desktop_shell, exposay.grab_kbd);
437 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
438
439 /* We want to know when mod has been pressed and released.
440 * FIXME: There is a problem here: if mod is pressed, then a key
441 * is pressed and released, then mod is released, we will treat that
442 * as if only mod had been pressed and released. */
443 if (seat->modifier_state) {
444 if (seat->modifier_state == shell->binding_modifier) {
445 shell->exposay.mod_pressed = true;
446 } else {
447 shell->exposay.mod_invalid = true;
448 }
449 } else {
450 if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid)
451 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat);
452
453 shell->exposay.mod_invalid = false;
454 shell->exposay.mod_pressed = false;
455 }
456
457 return;
458}
459
460static void
461exposay_cancel(struct weston_keyboard_grab *grab)
462{
463 struct desktop_shell *shell =
464 container_of(grab, struct desktop_shell, exposay.grab_kbd);
465
466 exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat);
467}
468
469static const struct weston_keyboard_grab_interface exposay_kbd_grab = {
470 exposay_key,
471 exposay_modifier,
472 exposay_cancel,
473};
474
475/**
476 * Called when the transition from overview -> inactive has completed.
477 */
478static enum exposay_layout_state
479exposay_set_inactive(struct desktop_shell *shell)
480{
481 struct weston_seat *seat = shell->exposay.seat;
482
483 weston_keyboard_end_grab(seat->keyboard);
484 weston_pointer_end_grab(seat->pointer);
485 if (seat->keyboard->input_method_resource)
486 seat->keyboard->grab = &seat->keyboard->input_method_grab;
487
488 return EXPOSAY_LAYOUT_INACTIVE;
489}
490
491/**
492 * Begins the transition from overview to inactive. */
493static enum exposay_layout_state
494exposay_transition_inactive(struct desktop_shell *shell, int switch_focus)
495{
496 struct exposay_surface *esurface;
497
498 /* Call activate() before we start the animations to avoid
499 * animating back the old state and then immediately transitioning
500 * to the new. */
501 if (switch_focus && shell->exposay.focus_current)
502 activate(shell, shell->exposay.focus_current->surface,
503 shell->exposay.seat);
504 else if (shell->exposay.focus_prev)
505 activate(shell, shell->exposay.focus_prev->surface,
506 shell->exposay.seat);
507
508 wl_list_for_each(esurface, &shell->exposay.surface_list, link)
509 exposay_animate_out(esurface);
510 weston_compositor_schedule_repaint(shell->compositor);
511
512 return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE;
513}
514
515static enum exposay_layout_state
516exposay_transition_active(struct desktop_shell *shell)
517{
518 struct weston_seat *seat = shell->exposay.seat;
519
520 shell->exposay.workspace = get_current_workspace(shell);
521 shell->exposay.focus_prev = get_default_view (seat->keyboard->focus);
522 shell->exposay.focus_current = get_default_view (seat->keyboard->focus);
523 shell->exposay.clicked = NULL;
524 wl_list_init(&shell->exposay.surface_list);
525
526 lower_fullscreen_layer(shell);
527 shell->exposay.grab_kbd.interface = &exposay_kbd_grab;
528 weston_keyboard_start_grab(seat->keyboard,
529 &shell->exposay.grab_kbd);
530 weston_keyboard_set_focus(seat->keyboard, NULL);
531
532 shell->exposay.grab_ptr.interface = &exposay_ptr_grab;
533 weston_pointer_start_grab(seat->pointer,
534 &shell->exposay.grab_ptr);
535 weston_pointer_set_focus(seat->pointer, NULL,
536 seat->pointer->x, seat->pointer->y);
537
538 return exposay_layout(shell);
539}
540
541static void
542exposay_check_state(struct desktop_shell *shell)
543{
544 enum exposay_layout_state state_new = shell->exposay.state_cur;
545 int do_switch = 0;
546
547 /* Don't do anything whilst animations are running, just store up
548 * target state changes and only act on them when the animations have
549 * completed. */
550 if (exposay_is_animating(shell))
551 return;
552
553 switch (shell->exposay.state_target) {
554 case EXPOSAY_TARGET_OVERVIEW:
555 switch (shell->exposay.state_cur) {
556 case EXPOSAY_LAYOUT_OVERVIEW:
557 goto out;
558 case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW:
559 state_new = EXPOSAY_LAYOUT_OVERVIEW;
560 break;
561 default:
562 state_new = exposay_transition_active(shell);
563 break;
564 }
565 break;
566
567 case EXPOSAY_TARGET_SWITCH:
568 do_switch = 1; /* fallthrough */
569 case EXPOSAY_TARGET_CANCEL:
570 switch (shell->exposay.state_cur) {
571 case EXPOSAY_LAYOUT_INACTIVE:
572 goto out;
573 case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE:
574 state_new = exposay_set_inactive(shell);
575 break;
576 default:
577 state_new = exposay_transition_inactive(shell, do_switch);
578 break;
579 }
580
581 break;
582 }
583
584out:
585 shell->exposay.state_cur = state_new;
586}
587
588static void
589exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state,
590 struct weston_seat *seat)
591{
592 shell->exposay.state_target = state;
593 shell->exposay.seat = seat;
594 exposay_check_state(shell);
595}
596
597void
598exposay_binding(struct weston_seat *seat, enum weston_keyboard_modifier modifier,
599 void *data)
600{
601 struct desktop_shell *shell = data;
602
603 exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, seat);
604}