Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Scott Moreau |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | 583a236 | 2012-06-25 17:13:58 -0400 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 27 | #include "compositor.h" |
Kristian Høgsberg | 583a236 | 2012-06-25 17:13:58 -0400 | [diff] [blame] | 28 | #include "text-cursor-position-server-protocol.h" |
| 29 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 30 | static void |
| 31 | weston_zoom_frame_z(struct weston_animation *animation, |
| 32 | struct weston_output *output, uint32_t msecs) |
| 33 | { |
| 34 | if (animation->frame_counter <= 1) |
| 35 | output->zoom.spring_z.timestamp = msecs; |
| 36 | |
| 37 | weston_spring_update(&output->zoom.spring_z, msecs); |
| 38 | |
| 39 | if (output->zoom.spring_z.current > output->zoom.max_level) |
| 40 | output->zoom.spring_z.current = output->zoom.max_level; |
| 41 | else if (output->zoom.spring_z.current < 0.0) |
| 42 | output->zoom.spring_z.current = 0.0; |
| 43 | |
| 44 | if (weston_spring_done(&output->zoom.spring_z)) { |
Ville Syrjälä | aa628d0 | 2012-11-16 11:48:47 +0200 | [diff] [blame] | 45 | if (output->zoom.active && output->zoom.level <= 0.0) { |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 46 | output->zoom.active = 0; |
Kristian Høgsberg | 79af73e | 2012-08-03 15:45:23 -0400 | [diff] [blame] | 47 | output->disable_planes--; |
Giulio Camuffo | 412b024 | 2013-11-14 23:42:51 +0100 | [diff] [blame^] | 48 | wl_list_remove(&output->zoom.motion_listener.link); |
Kristian Høgsberg | 79af73e | 2012-08-03 15:45:23 -0400 | [diff] [blame] | 49 | } |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 50 | output->zoom.spring_z.current = output->zoom.level; |
| 51 | wl_list_remove(&animation->link); |
| 52 | wl_list_init(&animation->link); |
| 53 | } |
| 54 | |
| 55 | output->dirty = 1; |
| 56 | weston_output_damage(output); |
| 57 | } |
| 58 | |
Kristian Høgsberg | ef6f136 | 2012-08-10 10:07:55 -0400 | [diff] [blame] | 59 | static struct weston_seat * |
| 60 | weston_zoom_pick_seat(struct weston_compositor *compositor) |
| 61 | { |
| 62 | return container_of(compositor->seat_list.next, |
| 63 | struct weston_seat, link); |
| 64 | } |
| 65 | |
| 66 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 67 | static void |
| 68 | weston_zoom_frame_xy(struct weston_animation *animation, |
| 69 | struct weston_output *output, uint32_t msecs) |
| 70 | { |
Kristian Høgsberg | ef6f136 | 2012-08-10 10:07:55 -0400 | [diff] [blame] | 71 | struct weston_seat *seat = weston_zoom_pick_seat(output->compositor); |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 72 | wl_fixed_t x, y; |
Kristian Høgsberg | ef6f136 | 2012-08-10 10:07:55 -0400 | [diff] [blame] | 73 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 74 | if (animation->frame_counter <= 1) |
| 75 | output->zoom.spring_xy.timestamp = msecs; |
| 76 | |
| 77 | weston_spring_update(&output->zoom.spring_xy, msecs); |
| 78 | |
| 79 | x = output->zoom.from.x - ((output->zoom.from.x - output->zoom.to.x) * |
| 80 | output->zoom.spring_xy.current); |
| 81 | y = output->zoom.from.y - ((output->zoom.from.y - output->zoom.to.y) * |
| 82 | output->zoom.spring_xy.current); |
| 83 | |
| 84 | output->zoom.current.x = x; |
| 85 | output->zoom.current.y = y; |
| 86 | |
| 87 | if (weston_spring_done(&output->zoom.spring_xy)) { |
| 88 | output->zoom.spring_xy.current = output->zoom.spring_xy.target; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 89 | output->zoom.current.x = seat->pointer->x; |
| 90 | output->zoom.current.y = seat->pointer->y; |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 91 | wl_list_remove(&animation->link); |
| 92 | wl_list_init(&animation->link); |
| 93 | } |
| 94 | |
| 95 | output->dirty = 1; |
| 96 | weston_output_damage(output); |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | zoom_area_center_from_pointer(struct weston_output *output, |
| 101 | wl_fixed_t *x, wl_fixed_t *y) |
| 102 | { |
| 103 | float level = output->zoom.spring_z.current; |
| 104 | wl_fixed_t offset_x = wl_fixed_from_int(output->x); |
| 105 | wl_fixed_t offset_y = wl_fixed_from_int(output->y); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 106 | wl_fixed_t w = wl_fixed_from_int(output->width); |
| 107 | wl_fixed_t h = wl_fixed_from_int(output->height); |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 108 | |
| 109 | *x -= ((((*x - offset_x) / (float) w) - 0.5) * (w * (1.0 - level))); |
| 110 | *y -= ((((*y - offset_y) / (float) h) - 0.5) * (h * (1.0 - level))); |
| 111 | } |
| 112 | |
| 113 | static void |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 114 | weston_zoom_apply_output_transform(struct weston_output *output, |
| 115 | float *x, float *y) |
| 116 | { |
| 117 | float tx, ty; |
| 118 | |
| 119 | switch(output->transform) { |
| 120 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 121 | default: |
| 122 | return; |
| 123 | case WL_OUTPUT_TRANSFORM_90: |
| 124 | tx = -*y; |
| 125 | ty = *x; |
| 126 | break; |
| 127 | case WL_OUTPUT_TRANSFORM_180: |
| 128 | tx = -*x; |
| 129 | ty = -*y; |
| 130 | break; |
| 131 | case WL_OUTPUT_TRANSFORM_270: |
| 132 | tx = *y; |
| 133 | ty = -*x; |
| 134 | break; |
| 135 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 136 | tx = -*x; |
| 137 | ty = *y; |
| 138 | break; |
| 139 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 140 | tx = -*y; |
| 141 | ty = -*x; |
| 142 | break; |
| 143 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 144 | tx = *x; |
| 145 | ty = -*y; |
| 146 | break; |
| 147 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 148 | tx = *y; |
| 149 | ty = *x; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | *x = tx; |
| 154 | *y = ty; |
| 155 | } |
| 156 | |
| 157 | static void |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 158 | weston_output_update_zoom_transform(struct weston_output *output) |
| 159 | { |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 160 | float global_x, global_y; |
| 161 | wl_fixed_t x = output->zoom.current.x; |
| 162 | wl_fixed_t y = output->zoom.current.y; |
| 163 | float trans_min, trans_max; |
| 164 | float ratio, level; |
| 165 | |
| 166 | level = output->zoom.spring_z.current; |
| 167 | ratio = 1 / level; |
| 168 | |
Ander Conselvan de Oliveira | 434e8f3 | 2012-11-21 15:11:36 +0200 | [diff] [blame] | 169 | if (!output->zoom.active || level > output->zoom.max_level || |
| 170 | level == 0.0f) |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 171 | return; |
| 172 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 173 | if (wl_list_empty(&output->zoom.animation_xy.link)) |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 174 | zoom_area_center_from_pointer(output, &x, &y); |
| 175 | |
| 176 | global_x = wl_fixed_to_double(x); |
| 177 | global_y = wl_fixed_to_double(y); |
| 178 | |
| 179 | output->zoom.trans_x = |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 180 | ((((global_x - output->x) / output->width) * |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 181 | (level * 2)) - level) * ratio; |
| 182 | output->zoom.trans_y = |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 183 | ((((global_y - output->y) / output->height) * |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 184 | (level * 2)) - level) * ratio; |
| 185 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 186 | weston_zoom_apply_output_transform(output, &output->zoom.trans_x, |
| 187 | &output->zoom.trans_y); |
| 188 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 189 | trans_max = level * 2 - level; |
| 190 | trans_min = -trans_max; |
| 191 | |
| 192 | /* Clip zoom area to output */ |
| 193 | if (output->zoom.trans_x > trans_max) |
| 194 | output->zoom.trans_x = trans_max; |
| 195 | else if (output->zoom.trans_x < trans_min) |
| 196 | output->zoom.trans_x = trans_min; |
| 197 | if (output->zoom.trans_y > trans_max) |
| 198 | output->zoom.trans_y = trans_max; |
| 199 | else if (output->zoom.trans_y < trans_min) |
| 200 | output->zoom.trans_y = trans_min; |
| 201 | } |
| 202 | |
| 203 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 204 | weston_zoom_transition(struct weston_output *output, wl_fixed_t x, wl_fixed_t y) |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 205 | { |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 206 | if (output->zoom.level != output->zoom.spring_z.current) { |
| 207 | output->zoom.spring_z.target = output->zoom.level; |
| 208 | if (wl_list_empty(&output->zoom.animation_z.link)) { |
| 209 | output->zoom.animation_z.frame_counter = 0; |
| 210 | wl_list_insert(output->animation_list.prev, |
| 211 | &output->zoom.animation_z.link); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | output->dirty = 1; |
| 216 | weston_output_damage(output); |
| 217 | } |
| 218 | |
| 219 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 220 | weston_output_update_zoom(struct weston_output *output) |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 221 | { |
Kristian Høgsberg | ef6f136 | 2012-08-10 10:07:55 -0400 | [diff] [blame] | 222 | struct weston_seat *seat = weston_zoom_pick_seat(output->compositor); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 223 | wl_fixed_t x = seat->pointer->x; |
| 224 | wl_fixed_t y = seat->pointer->y; |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 225 | |
| 226 | zoom_area_center_from_pointer(output, &x, &y); |
| 227 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 228 | if (wl_list_empty(&output->zoom.animation_xy.link)) { |
| 229 | output->zoom.current.x = seat->pointer->x; |
| 230 | output->zoom.current.y = seat->pointer->y; |
| 231 | } else { |
| 232 | output->zoom.to.x = x; |
| 233 | output->zoom.to.y = y; |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 234 | } |
| 235 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 236 | weston_zoom_transition(output, x, y); |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 237 | weston_output_update_zoom_transform(output); |
| 238 | } |
| 239 | |
Giulio Camuffo | 412b024 | 2013-11-14 23:42:51 +0100 | [diff] [blame^] | 240 | static void |
| 241 | motion(struct wl_listener *listener, void *data) |
| 242 | { |
| 243 | struct weston_output_zoom *zoom = |
| 244 | container_of(listener, struct weston_output_zoom, motion_listener); |
| 245 | struct weston_output *output = |
| 246 | container_of(zoom, struct weston_output, zoom); |
| 247 | |
| 248 | weston_output_update_zoom(output); |
| 249 | } |
| 250 | |
| 251 | WL_EXPORT void |
| 252 | weston_output_activate_zoom(struct weston_output *output) |
| 253 | { |
| 254 | struct weston_seat *seat = weston_zoom_pick_seat(output->compositor); |
| 255 | |
| 256 | if (output->zoom.active) |
| 257 | return; |
| 258 | |
| 259 | output->zoom.active = 1; |
| 260 | output->disable_planes++; |
| 261 | wl_signal_add(&seat->pointer->motion_signal, |
| 262 | &output->zoom.motion_listener); |
| 263 | } |
| 264 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 265 | WL_EXPORT void |
| 266 | weston_output_init_zoom(struct weston_output *output) |
| 267 | { |
| 268 | output->zoom.active = 0; |
| 269 | output->zoom.increment = 0.07; |
| 270 | output->zoom.max_level = 0.95; |
| 271 | output->zoom.level = 0.0; |
| 272 | output->zoom.trans_x = 0.0; |
| 273 | output->zoom.trans_y = 0.0; |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 274 | weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0); |
| 275 | output->zoom.spring_z.friction = 1000; |
| 276 | output->zoom.animation_z.frame = weston_zoom_frame_z; |
| 277 | wl_list_init(&output->zoom.animation_z.link); |
| 278 | weston_spring_init(&output->zoom.spring_xy, 250.0, 0.0, 0.0); |
| 279 | output->zoom.spring_xy.friction = 1000; |
| 280 | output->zoom.animation_xy.frame = weston_zoom_frame_xy; |
| 281 | wl_list_init(&output->zoom.animation_xy.link); |
Giulio Camuffo | 412b024 | 2013-11-14 23:42:51 +0100 | [diff] [blame^] | 282 | output->zoom.motion_listener.notify = motion; |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 283 | } |