Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010-2012 Intel Corporation |
| 3 | * Copyright © 2011-2012 Collabora, Ltd. |
| 4 | * Copyright © 2013 Raspberry Pi Foundation |
| 5 | * |
Bryce Harrington | af637c2 | 2015-06-11 12:55:55 -0700 | [diff] [blame] | 6 | * 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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 12 | * |
Bryce Harrington | af637c2 | 2015-06-11 12:55:55 -0700 | [diff] [blame] | 13 | * 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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <linux/input.h> |
| 29 | |
| 30 | #include "shell.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 31 | #include "shared/helpers.h" |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 32 | |
| 33 | struct exposay_surface { |
| 34 | struct desktop_shell *shell; |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 35 | struct exposay_output *eoutput; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 36 | struct weston_surface *surface; |
| 37 | struct weston_view *view; |
Emilio Pozuelo Monfort | f942b73 | 2014-02-10 14:23:04 +0100 | [diff] [blame] | 38 | struct wl_listener view_destroy_listener; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 39 | 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 | |
| 57 | static void exposay_set_state(struct desktop_shell *shell, |
| 58 | enum exposay_target_state state, |
| 59 | struct weston_seat *seat); |
| 60 | static void exposay_check_state(struct desktop_shell *shell); |
| 61 | |
| 62 | static void |
Emilio Pozuelo Monfort | f942b73 | 2014-02-10 14:23:04 +0100 | [diff] [blame] | 63 | exposay_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 | |
| 76 | static void |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 77 | exposay_in_flight_inc(struct desktop_shell *shell) |
| 78 | { |
| 79 | shell->exposay.in_flight++; |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | exposay_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 | |
| 91 | static void |
| 92 | exposay_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 | |
| 112 | static void |
| 113 | exposay_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 | |
| 124 | static void |
| 125 | exposay_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 Monfort | f942b73 | 2014-02-10 14:23:04 +0100 | [diff] [blame] | 130 | exposay_surface_destroy(esurface); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 131 | |
| 132 | exposay_in_flight_dec(shell); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | exposay_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 | |
| 152 | static void |
| 153 | exposay_highlight_surface(struct desktop_shell *shell, |
| 154 | struct exposay_surface *esurface) |
| 155 | { |
U. Artie Eoff | 6d6d190 | 2014-01-15 13:42:18 -0800 | [diff] [blame] | 156 | struct weston_view *view = esurface->view; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 157 | |
Emilio Pozuelo Monfort | a759201 | 2014-02-10 16:52:33 +0100 | [diff] [blame] | 158 | if (shell->exposay.focus_current == view) |
| 159 | return; |
| 160 | |
U. Artie Eoff | 6d6d190 | 2014-01-15 13:42:18 -0800 | [diff] [blame] | 161 | shell->exposay.row_current = esurface->row; |
| 162 | shell->exposay.column_current = esurface->column; |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 163 | shell->exposay.cur_output = esurface->eoutput; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 164 | |
Emilio Pozuelo Monfort | 9e7c759 | 2014-01-30 14:01:10 +0100 | [diff] [blame] | 165 | activate(shell, view->surface, shell->exposay.seat, false); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 166 | shell->exposay.focus_current = view; |
| 167 | } |
| 168 | |
| 169 | static int |
| 170 | exposay_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 | |
| 179 | static void |
| 180 | exposay_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 Monfort | f942b73 | 2014-02-10 14:23:04 +0100 | [diff] [blame] | 198 | static void |
| 199 | handle_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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 208 | /* 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. */ |
| 211 | static enum exposay_layout_state |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 212 | exposay_layout(struct desktop_shell *shell, struct shell_output *shell_output) |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 213 | { |
| 214 | struct workspace *workspace = shell->exposay.workspace; |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 215 | struct weston_output *output = shell_output->output; |
| 216 | struct exposay_output *eoutput = &shell_output->eoutput; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 217 | struct weston_view *view; |
Emilio Pozuelo Monfort | e6bbe5a | 2014-01-07 16:41:39 +0100 | [diff] [blame] | 218 | struct exposay_surface *esurface, *highlight = NULL; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 219 | int w, h; |
| 220 | int i; |
| 221 | int last_row_removed = 0; |
| 222 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 223 | eoutput->num_surfaces = 0; |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 224 | wl_list_for_each(view, &workspace->layer.view_list.link, layer_link.link) { |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 225 | if (!get_shell_surface(view->surface)) |
| 226 | continue; |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 227 | if (view->output != output) |
| 228 | continue; |
| 229 | eoutput->num_surfaces++; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 232 | 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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 238 | 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 Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 253 | 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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 257 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 258 | eoutput->hpadding_outer = (output->width / 10); |
| 259 | eoutput->vpadding_outer = (output->height / 10); |
| 260 | eoutput->padding_inner = 80; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 261 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 262 | w = output->width - (eoutput->hpadding_outer * 2); |
| 263 | w -= eoutput->padding_inner * (eoutput->grid_size - 1); |
| 264 | w /= eoutput->grid_size; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 265 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 266 | h = output->height - (eoutput->vpadding_outer * 2); |
| 267 | h -= eoutput->padding_inner * (eoutput->grid_size - 1); |
| 268 | h /= eoutput->grid_size; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 269 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 270 | 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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 275 | |
| 276 | i = 0; |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 277 | wl_list_for_each(view, &workspace->layer.view_list.link, layer_link.link) { |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 278 | int pad; |
| 279 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 280 | pad = eoutput->surface_size + eoutput->padding_inner; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 281 | |
| 282 | if (!get_shell_surface(view->surface)) |
| 283 | continue; |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 284 | if (view->output != output) |
| 285 | continue; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 286 | |
| 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 Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 296 | esurface->eoutput = eoutput; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 297 | esurface->view = view; |
| 298 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 299 | esurface->row = i / eoutput->grid_size; |
| 300 | esurface->column = i % eoutput->grid_size; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 301 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 302 | esurface->x = output->x + eoutput->hpadding_outer; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 303 | esurface->x += pad * esurface->column; |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 304 | esurface->y = output->y + eoutput->vpadding_outer; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 305 | esurface->y += pad * esurface->row; |
| 306 | |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 307 | if (esurface->row == eoutput->grid_size - 1) |
| 308 | esurface->x += (eoutput->surface_size + eoutput->padding_inner) * last_row_removed / 2; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 309 | |
| 310 | if (view->surface->width > view->surface->height) |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 311 | esurface->scale = eoutput->surface_size / (float) view->surface->width; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 312 | else |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 313 | esurface->scale = eoutput->surface_size / (float) view->surface->height; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 314 | 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 Monfort | e6bbe5a | 2014-01-07 16:41:39 +0100 | [diff] [blame] | 318 | highlight = esurface; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 319 | |
| 320 | exposay_animate_in(esurface); |
| 321 | |
Derek Foreman | d8156e2 | 2015-05-26 16:21:05 -0500 | [diff] [blame] | 322 | /* 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øgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 331 | i++; |
| 332 | } |
| 333 | |
Emilio Pozuelo Monfort | 5905ebc | 2014-05-24 02:43:04 +0200 | [diff] [blame] | 334 | if (highlight) { |
| 335 | shell->exposay.focus_current = NULL; |
Emilio Pozuelo Monfort | e6bbe5a | 2014-01-07 16:41:39 +0100 | [diff] [blame] | 336 | exposay_highlight_surface(shell, highlight); |
Emilio Pozuelo Monfort | 5905ebc | 2014-05-24 02:43:04 +0200 | [diff] [blame] | 337 | } |
Emilio Pozuelo Monfort | e6bbe5a | 2014-01-07 16:41:39 +0100 | [diff] [blame] | 338 | |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 339 | weston_compositor_schedule_repaint(shell->compositor); |
| 340 | |
| 341 | return EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW; |
| 342 | } |
| 343 | |
| 344 | static void |
| 345 | exposay_focus(struct weston_pointer_grab *grab) |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | exposay_motion(struct weston_pointer_grab *grab, uint32_t time, |
| 351 | wl_fixed_t x, wl_fixed_t y) |
| 352 | { |
| 353 | struct desktop_shell *shell = |
| 354 | container_of(grab, struct desktop_shell, exposay.grab_ptr); |
| 355 | |
| 356 | weston_pointer_move(grab->pointer, x, y); |
| 357 | |
| 358 | exposay_pick(shell, |
| 359 | wl_fixed_to_int(grab->pointer->x), |
| 360 | wl_fixed_to_int(grab->pointer->y)); |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | exposay_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 | |
| 388 | static void |
| 389 | exposay_pointer_grab_cancel(struct weston_pointer_grab *grab) |
| 390 | { |
| 391 | struct desktop_shell *shell = |
| 392 | container_of(grab, struct desktop_shell, exposay.grab_ptr); |
| 393 | |
| 394 | exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat); |
| 395 | } |
| 396 | |
| 397 | static const struct weston_pointer_grab_interface exposay_ptr_grab = { |
| 398 | exposay_focus, |
| 399 | exposay_motion, |
| 400 | exposay_button, |
| 401 | exposay_pointer_grab_cancel, |
| 402 | }; |
| 403 | |
| 404 | static int |
| 405 | exposay_maybe_move(struct desktop_shell *shell, int row, int column) |
| 406 | { |
| 407 | struct exposay_surface *esurface; |
| 408 | |
| 409 | wl_list_for_each(esurface, &shell->exposay.surface_list, link) { |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 410 | if (esurface->eoutput != shell->exposay.cur_output || |
| 411 | esurface->row != row || esurface->column != column) |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 412 | continue; |
| 413 | |
| 414 | exposay_highlight_surface(shell, esurface); |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | exposay_key(struct weston_keyboard_grab *grab, uint32_t time, uint32_t key, |
| 423 | uint32_t state_w) |
| 424 | { |
| 425 | struct weston_seat *seat = grab->keyboard->seat; |
| 426 | struct desktop_shell *shell = |
| 427 | container_of(grab, struct desktop_shell, exposay.grab_kbd); |
| 428 | enum wl_keyboard_key_state state = state_w; |
| 429 | |
| 430 | if (state != WL_KEYBOARD_KEY_STATE_RELEASED) |
| 431 | return; |
| 432 | |
| 433 | switch (key) { |
| 434 | case KEY_ESC: |
| 435 | exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat); |
| 436 | break; |
| 437 | case KEY_ENTER: |
| 438 | exposay_set_state(shell, EXPOSAY_TARGET_SWITCH, seat); |
| 439 | break; |
| 440 | case KEY_UP: |
| 441 | exposay_maybe_move(shell, shell->exposay.row_current - 1, |
| 442 | shell->exposay.column_current); |
| 443 | break; |
| 444 | case KEY_DOWN: |
| 445 | /* Special case for trying to move to the bottom row when it |
| 446 | * has fewer items than all the others. */ |
| 447 | if (!exposay_maybe_move(shell, shell->exposay.row_current + 1, |
| 448 | shell->exposay.column_current) && |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 449 | shell->exposay.row_current < (shell->exposay.cur_output->grid_size - 1)) { |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 450 | exposay_maybe_move(shell, shell->exposay.row_current + 1, |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 451 | (shell->exposay.cur_output->num_surfaces % |
| 452 | shell->exposay.cur_output->grid_size) - 1); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 453 | } |
| 454 | break; |
| 455 | case KEY_LEFT: |
| 456 | exposay_maybe_move(shell, shell->exposay.row_current, |
| 457 | shell->exposay.column_current - 1); |
| 458 | break; |
| 459 | case KEY_RIGHT: |
| 460 | exposay_maybe_move(shell, shell->exposay.row_current, |
| 461 | shell->exposay.column_current + 1); |
| 462 | break; |
| 463 | case KEY_TAB: |
| 464 | /* Try to move right, then down (and to the leftmost column), |
| 465 | * then if all else fails, to the top left. */ |
| 466 | if (!exposay_maybe_move(shell, shell->exposay.row_current, |
| 467 | shell->exposay.column_current + 1) && |
| 468 | !exposay_maybe_move(shell, shell->exposay.row_current + 1, 0)) |
| 469 | exposay_maybe_move(shell, 0, 0); |
| 470 | break; |
| 471 | default: |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | exposay_modifier(struct weston_keyboard_grab *grab, uint32_t serial, |
| 478 | uint32_t mods_depressed, uint32_t mods_latched, |
| 479 | uint32_t mods_locked, uint32_t group) |
| 480 | { |
| 481 | struct desktop_shell *shell = |
| 482 | container_of(grab, struct desktop_shell, exposay.grab_kbd); |
| 483 | struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat; |
| 484 | |
| 485 | /* We want to know when mod has been pressed and released. |
| 486 | * FIXME: There is a problem here: if mod is pressed, then a key |
| 487 | * is pressed and released, then mod is released, we will treat that |
| 488 | * as if only mod had been pressed and released. */ |
| 489 | if (seat->modifier_state) { |
| 490 | if (seat->modifier_state == shell->binding_modifier) { |
| 491 | shell->exposay.mod_pressed = true; |
| 492 | } else { |
| 493 | shell->exposay.mod_invalid = true; |
| 494 | } |
| 495 | } else { |
| 496 | if (shell->exposay.mod_pressed && !shell->exposay.mod_invalid) |
| 497 | exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, seat); |
| 498 | |
| 499 | shell->exposay.mod_invalid = false; |
| 500 | shell->exposay.mod_pressed = false; |
| 501 | } |
| 502 | |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | static void |
| 507 | exposay_cancel(struct weston_keyboard_grab *grab) |
| 508 | { |
| 509 | struct desktop_shell *shell = |
| 510 | container_of(grab, struct desktop_shell, exposay.grab_kbd); |
| 511 | |
| 512 | exposay_set_state(shell, EXPOSAY_TARGET_CANCEL, shell->exposay.seat); |
| 513 | } |
| 514 | |
| 515 | static const struct weston_keyboard_grab_interface exposay_kbd_grab = { |
| 516 | exposay_key, |
| 517 | exposay_modifier, |
| 518 | exposay_cancel, |
| 519 | }; |
| 520 | |
| 521 | /** |
| 522 | * Called when the transition from overview -> inactive has completed. |
| 523 | */ |
| 524 | static enum exposay_layout_state |
| 525 | exposay_set_inactive(struct desktop_shell *shell) |
| 526 | { |
| 527 | struct weston_seat *seat = shell->exposay.seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 528 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
| 529 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 530 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 531 | if (pointer) |
| 532 | weston_pointer_end_grab(pointer); |
Derek Foreman | cee82d6 | 2015-07-15 13:00:33 -0500 | [diff] [blame] | 533 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 534 | if (keyboard) { |
| 535 | weston_keyboard_end_grab(keyboard); |
| 536 | if (keyboard->input_method_resource) |
| 537 | keyboard->grab = &keyboard->input_method_grab; |
Derek Foreman | cee82d6 | 2015-07-15 13:00:33 -0500 | [diff] [blame] | 538 | } |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 539 | |
| 540 | return EXPOSAY_LAYOUT_INACTIVE; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Begins the transition from overview to inactive. */ |
| 545 | static enum exposay_layout_state |
| 546 | exposay_transition_inactive(struct desktop_shell *shell, int switch_focus) |
| 547 | { |
| 548 | struct exposay_surface *esurface; |
| 549 | |
| 550 | /* Call activate() before we start the animations to avoid |
| 551 | * animating back the old state and then immediately transitioning |
| 552 | * to the new. */ |
| 553 | if (switch_focus && shell->exposay.focus_current) |
| 554 | activate(shell, shell->exposay.focus_current->surface, |
Emilio Pozuelo Monfort | 9e7c759 | 2014-01-30 14:01:10 +0100 | [diff] [blame] | 555 | shell->exposay.seat, true); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 556 | else if (shell->exposay.focus_prev) |
| 557 | activate(shell, shell->exposay.focus_prev->surface, |
Emilio Pozuelo Monfort | 9e7c759 | 2014-01-30 14:01:10 +0100 | [diff] [blame] | 558 | shell->exposay.seat, true); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 559 | |
| 560 | wl_list_for_each(esurface, &shell->exposay.surface_list, link) |
| 561 | exposay_animate_out(esurface); |
| 562 | weston_compositor_schedule_repaint(shell->compositor); |
| 563 | |
| 564 | return EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE; |
| 565 | } |
| 566 | |
| 567 | static enum exposay_layout_state |
| 568 | exposay_transition_active(struct desktop_shell *shell) |
| 569 | { |
| 570 | struct weston_seat *seat = shell->exposay.seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 571 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
| 572 | struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat); |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 573 | struct shell_output *shell_output; |
| 574 | bool animate = false; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 575 | |
| 576 | shell->exposay.workspace = get_current_workspace(shell); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 577 | shell->exposay.focus_prev = get_default_view(keyboard->focus); |
| 578 | shell->exposay.focus_current = get_default_view(keyboard->focus); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 579 | shell->exposay.clicked = NULL; |
| 580 | wl_list_init(&shell->exposay.surface_list); |
| 581 | |
Mario Kleiner | 9f4d655 | 2015-06-21 21:25:08 +0200 | [diff] [blame] | 582 | lower_fullscreen_layer(shell, NULL); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 583 | shell->exposay.grab_kbd.interface = &exposay_kbd_grab; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 584 | weston_keyboard_start_grab(keyboard, |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 585 | &shell->exposay.grab_kbd); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 586 | weston_keyboard_set_focus(keyboard, NULL); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 587 | |
| 588 | shell->exposay.grab_ptr.interface = &exposay_ptr_grab; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 589 | if (pointer) { |
| 590 | weston_pointer_start_grab(pointer, |
Derek Foreman | 3dd570e | 2015-05-22 11:47:20 -0500 | [diff] [blame] | 591 | &shell->exposay.grab_ptr); |
Derek Foreman | f9318d1 | 2015-05-11 15:40:11 -0500 | [diff] [blame^] | 592 | weston_pointer_clear_focus(pointer); |
Derek Foreman | 3dd570e | 2015-05-22 11:47:20 -0500 | [diff] [blame] | 593 | } |
Emilio Pozuelo Monfort | 3b6e68e | 2014-02-10 13:22:32 +0100 | [diff] [blame] | 594 | wl_list_for_each(shell_output, &shell->output_list, link) { |
| 595 | enum exposay_layout_state state; |
| 596 | |
| 597 | state = exposay_layout(shell, shell_output); |
| 598 | |
| 599 | if (state == EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW) |
| 600 | animate = true; |
| 601 | } |
| 602 | |
| 603 | return animate ? EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW |
| 604 | : EXPOSAY_LAYOUT_OVERVIEW; |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void |
| 608 | exposay_check_state(struct desktop_shell *shell) |
| 609 | { |
| 610 | enum exposay_layout_state state_new = shell->exposay.state_cur; |
| 611 | int do_switch = 0; |
| 612 | |
| 613 | /* Don't do anything whilst animations are running, just store up |
| 614 | * target state changes and only act on them when the animations have |
| 615 | * completed. */ |
| 616 | if (exposay_is_animating(shell)) |
| 617 | return; |
| 618 | |
| 619 | switch (shell->exposay.state_target) { |
| 620 | case EXPOSAY_TARGET_OVERVIEW: |
| 621 | switch (shell->exposay.state_cur) { |
| 622 | case EXPOSAY_LAYOUT_OVERVIEW: |
| 623 | goto out; |
| 624 | case EXPOSAY_LAYOUT_ANIMATE_TO_OVERVIEW: |
| 625 | state_new = EXPOSAY_LAYOUT_OVERVIEW; |
| 626 | break; |
| 627 | default: |
| 628 | state_new = exposay_transition_active(shell); |
| 629 | break; |
| 630 | } |
| 631 | break; |
| 632 | |
| 633 | case EXPOSAY_TARGET_SWITCH: |
| 634 | do_switch = 1; /* fallthrough */ |
| 635 | case EXPOSAY_TARGET_CANCEL: |
| 636 | switch (shell->exposay.state_cur) { |
| 637 | case EXPOSAY_LAYOUT_INACTIVE: |
| 638 | goto out; |
| 639 | case EXPOSAY_LAYOUT_ANIMATE_TO_INACTIVE: |
| 640 | state_new = exposay_set_inactive(shell); |
| 641 | break; |
| 642 | default: |
| 643 | state_new = exposay_transition_inactive(shell, do_switch); |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | out: |
| 651 | shell->exposay.state_cur = state_new; |
| 652 | } |
| 653 | |
| 654 | static void |
| 655 | exposay_set_state(struct desktop_shell *shell, enum exposay_target_state state, |
| 656 | struct weston_seat *seat) |
| 657 | { |
| 658 | shell->exposay.state_target = state; |
| 659 | shell->exposay.seat = seat; |
| 660 | exposay_check_state(shell); |
| 661 | } |
| 662 | |
| 663 | void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 664 | exposay_binding(struct weston_keyboard *keyboard, enum weston_keyboard_modifier modifier, |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 665 | void *data) |
| 666 | { |
| 667 | struct desktop_shell *shell = data; |
| 668 | |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 669 | exposay_set_state(shell, EXPOSAY_TARGET_OVERVIEW, keyboard->seat); |
Kristian Høgsberg | 1ef2313 | 2013-12-04 00:20:01 -0800 | [diff] [blame] | 670 | } |