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 | |
Kristian Høgsberg | 583a236 | 2012-06-25 17:13:58 -0400 | [diff] [blame^] | 23 | #include <stdlib.h> |
| 24 | |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 25 | #include "compositor.h" |
Kristian Høgsberg | 583a236 | 2012-06-25 17:13:58 -0400 | [diff] [blame^] | 26 | #include "text-cursor-position-server-protocol.h" |
| 27 | |
| 28 | struct text_cursor_position { |
| 29 | struct wl_object base; |
| 30 | struct weston_compositor *ec; |
| 31 | struct wl_global *global; |
| 32 | struct wl_listener destroy_listener; |
| 33 | }; |
| 34 | |
| 35 | static void |
| 36 | text_cursor_position_notify(struct wl_client *client, |
| 37 | struct wl_resource *resource, |
| 38 | struct wl_resource *surface_resource, |
| 39 | wl_fixed_t x, wl_fixed_t y) |
| 40 | { |
| 41 | weston_text_cursor_position_notify((struct weston_surface *) surface_resource, x, y); |
| 42 | } |
| 43 | |
| 44 | struct text_cursor_position_interface text_cursor_position_implementation = { |
| 45 | text_cursor_position_notify |
| 46 | }; |
| 47 | |
| 48 | static void |
| 49 | bind_text_cursor_position(struct wl_client *client, |
| 50 | void *data, uint32_t version, uint32_t id) |
| 51 | { |
| 52 | wl_client_add_object(client, &text_cursor_position_interface, |
| 53 | &text_cursor_position_implementation, id, data); |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | text_cursor_position_notifier_destroy(struct wl_listener *listener, void *data) |
| 58 | { |
| 59 | struct text_cursor_position *text_cursor_position = |
| 60 | container_of(listener, struct text_cursor_position, destroy_listener); |
| 61 | |
| 62 | wl_display_remove_global(text_cursor_position->ec->wl_display, text_cursor_position->global); |
| 63 | free(text_cursor_position); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | text_cursor_position_notifier_create(struct weston_compositor *ec) |
| 68 | { |
| 69 | struct text_cursor_position *text_cursor_position; |
| 70 | |
| 71 | text_cursor_position = malloc(sizeof *text_cursor_position); |
| 72 | if (text_cursor_position == NULL) |
| 73 | return; |
| 74 | |
| 75 | text_cursor_position->base.interface = &text_cursor_position_interface; |
| 76 | text_cursor_position->base.implementation = |
| 77 | (void(**)(void)) &text_cursor_position_implementation; |
| 78 | text_cursor_position->ec = ec; |
| 79 | |
| 80 | text_cursor_position->global = wl_display_add_global(ec->wl_display, |
| 81 | &text_cursor_position_interface, |
| 82 | text_cursor_position, bind_text_cursor_position); |
| 83 | |
| 84 | text_cursor_position->destroy_listener.notify = text_cursor_position_notifier_destroy; |
| 85 | wl_signal_add(&ec->destroy_signal, &text_cursor_position->destroy_listener); |
| 86 | } |
Scott Moreau | 429490d | 2012-06-17 18:10:59 -0600 | [diff] [blame] | 87 | |
| 88 | WL_EXPORT void |
| 89 | weston_text_cursor_position_notify(struct weston_surface *surface, |
| 90 | wl_fixed_t cur_pos_x, |
| 91 | wl_fixed_t cur_pos_y) |
| 92 | { |
| 93 | struct weston_output *output; |
| 94 | wl_fixed_t global_x, global_y; |
| 95 | |
| 96 | weston_surface_to_global_fixed(surface, cur_pos_x, cur_pos_y, |
| 97 | &global_x, &global_y); |
| 98 | |
| 99 | wl_list_for_each(output, &surface->compositor->output_list, link) |
| 100 | if (output->zoom.active && |
| 101 | pixman_region32_contains_point(&output->region, |
| 102 | wl_fixed_to_int(global_x), |
| 103 | wl_fixed_to_int(global_y), |
| 104 | NULL)) { |
| 105 | output->zoom.text_cursor.x = global_x; |
| 106 | output->zoom.text_cursor.y = global_y; |
| 107 | weston_output_update_zoom(output, ZOOM_FOCUS_TEXT); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | weston_zoom_frame_z(struct weston_animation *animation, |
| 113 | struct weston_output *output, uint32_t msecs) |
| 114 | { |
| 115 | if (animation->frame_counter <= 1) |
| 116 | output->zoom.spring_z.timestamp = msecs; |
| 117 | |
| 118 | weston_spring_update(&output->zoom.spring_z, msecs); |
| 119 | |
| 120 | if (output->zoom.spring_z.current > output->zoom.max_level) |
| 121 | output->zoom.spring_z.current = output->zoom.max_level; |
| 122 | else if (output->zoom.spring_z.current < 0.0) |
| 123 | output->zoom.spring_z.current = 0.0; |
| 124 | |
| 125 | if (weston_spring_done(&output->zoom.spring_z)) { |
| 126 | if (output->zoom.level <= 0.0) |
| 127 | output->zoom.active = 0; |
| 128 | output->zoom.spring_z.current = output->zoom.level; |
| 129 | wl_list_remove(&animation->link); |
| 130 | wl_list_init(&animation->link); |
| 131 | } |
| 132 | |
| 133 | output->dirty = 1; |
| 134 | weston_output_damage(output); |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | weston_zoom_frame_xy(struct weston_animation *animation, |
| 139 | struct weston_output *output, uint32_t msecs) |
| 140 | { |
| 141 | wl_fixed_t x, y; |
| 142 | if (animation->frame_counter <= 1) |
| 143 | output->zoom.spring_xy.timestamp = msecs; |
| 144 | |
| 145 | weston_spring_update(&output->zoom.spring_xy, msecs); |
| 146 | |
| 147 | x = output->zoom.from.x - ((output->zoom.from.x - output->zoom.to.x) * |
| 148 | output->zoom.spring_xy.current); |
| 149 | y = output->zoom.from.y - ((output->zoom.from.y - output->zoom.to.y) * |
| 150 | output->zoom.spring_xy.current); |
| 151 | |
| 152 | output->zoom.current.x = x; |
| 153 | output->zoom.current.y = y; |
| 154 | |
| 155 | if (weston_spring_done(&output->zoom.spring_xy)) { |
| 156 | output->zoom.spring_xy.current = output->zoom.spring_xy.target; |
| 157 | output->zoom.current.x = |
| 158 | (output->zoom.type == ZOOM_FOCUS_POINTER) ? |
| 159 | output->compositor->seat->pointer.x : |
| 160 | output->zoom.text_cursor.x; |
| 161 | output->zoom.current.y = |
| 162 | (output->zoom.type == ZOOM_FOCUS_POINTER) ? |
| 163 | output->compositor->seat->pointer.y : |
| 164 | output->zoom.text_cursor.y; |
| 165 | wl_list_remove(&animation->link); |
| 166 | wl_list_init(&animation->link); |
| 167 | } |
| 168 | |
| 169 | output->dirty = 1; |
| 170 | weston_output_damage(output); |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | zoom_area_center_from_pointer(struct weston_output *output, |
| 175 | wl_fixed_t *x, wl_fixed_t *y) |
| 176 | { |
| 177 | float level = output->zoom.spring_z.current; |
| 178 | wl_fixed_t offset_x = wl_fixed_from_int(output->x); |
| 179 | wl_fixed_t offset_y = wl_fixed_from_int(output->y); |
| 180 | wl_fixed_t w = wl_fixed_from_int(output->current->width); |
| 181 | wl_fixed_t h = wl_fixed_from_int(output->current->height); |
| 182 | |
| 183 | *x -= ((((*x - offset_x) / (float) w) - 0.5) * (w * (1.0 - level))); |
| 184 | *y -= ((((*y - offset_y) / (float) h) - 0.5) * (h * (1.0 - level))); |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | weston_output_update_zoom_transform(struct weston_output *output) |
| 189 | { |
| 190 | uint32_t type = output->zoom.type; |
| 191 | float global_x, global_y; |
| 192 | wl_fixed_t x = output->zoom.current.x; |
| 193 | wl_fixed_t y = output->zoom.current.y; |
| 194 | float trans_min, trans_max; |
| 195 | float ratio, level; |
| 196 | |
| 197 | level = output->zoom.spring_z.current; |
| 198 | ratio = 1 / level; |
| 199 | |
| 200 | if (!output->zoom.active || level > output->zoom.max_level) |
| 201 | return; |
| 202 | |
| 203 | if (type == ZOOM_FOCUS_POINTER && |
| 204 | wl_list_empty(&output->zoom.animation_xy.link)) |
| 205 | zoom_area_center_from_pointer(output, &x, &y); |
| 206 | |
| 207 | global_x = wl_fixed_to_double(x); |
| 208 | global_y = wl_fixed_to_double(y); |
| 209 | |
| 210 | output->zoom.trans_x = |
| 211 | ((((global_x - output->x) / output->current->width) * |
| 212 | (level * 2)) - level) * ratio; |
| 213 | output->zoom.trans_y = |
| 214 | ((((global_y - output->y) / output->current->height) * |
| 215 | (level * 2)) - level) * ratio; |
| 216 | |
| 217 | trans_max = level * 2 - level; |
| 218 | trans_min = -trans_max; |
| 219 | |
| 220 | /* Clip zoom area to output */ |
| 221 | if (output->zoom.trans_x > trans_max) |
| 222 | output->zoom.trans_x = trans_max; |
| 223 | else if (output->zoom.trans_x < trans_min) |
| 224 | output->zoom.trans_x = trans_min; |
| 225 | if (output->zoom.trans_y > trans_max) |
| 226 | output->zoom.trans_y = trans_max; |
| 227 | else if (output->zoom.trans_y < trans_min) |
| 228 | output->zoom.trans_y = trans_min; |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | weston_zoom_transition(struct weston_output *output, uint32_t type, |
| 233 | wl_fixed_t x, wl_fixed_t y) |
| 234 | { |
| 235 | if (output->zoom.type != type) { |
| 236 | /* Set from/to points and start animation */ |
| 237 | output->zoom.spring_xy.current = 0.0; |
| 238 | output->zoom.spring_xy.previous = 0.0; |
| 239 | output->zoom.spring_xy.target = 1.0; |
| 240 | |
| 241 | if (wl_list_empty(&output->zoom.animation_xy.link)) { |
| 242 | output->zoom.animation_xy.frame_counter = 0; |
| 243 | wl_list_insert(output->animation_list.prev, |
| 244 | &output->zoom.animation_xy.link); |
| 245 | |
| 246 | output->zoom.from.x = (type == ZOOM_FOCUS_TEXT) ? |
| 247 | x : output->zoom.text_cursor.x; |
| 248 | output->zoom.from.y = (type == ZOOM_FOCUS_TEXT) ? |
| 249 | y : output->zoom.text_cursor.y; |
| 250 | } else { |
| 251 | output->zoom.from.x = output->zoom.current.x; |
| 252 | output->zoom.from.y = output->zoom.current.y; |
| 253 | } |
| 254 | |
| 255 | output->zoom.to.x = (type == ZOOM_FOCUS_POINTER) ? |
| 256 | x : output->zoom.text_cursor.x; |
| 257 | output->zoom.to.y = (type == ZOOM_FOCUS_POINTER) ? |
| 258 | y : output->zoom.text_cursor.y; |
| 259 | output->zoom.current.x = output->zoom.from.x; |
| 260 | output->zoom.current.y = output->zoom.from.y; |
| 261 | |
| 262 | output->zoom.type = type; |
| 263 | } |
| 264 | |
| 265 | if (output->zoom.level != output->zoom.spring_z.current) { |
| 266 | output->zoom.spring_z.target = output->zoom.level; |
| 267 | if (wl_list_empty(&output->zoom.animation_z.link)) { |
| 268 | output->zoom.animation_z.frame_counter = 0; |
| 269 | wl_list_insert(output->animation_list.prev, |
| 270 | &output->zoom.animation_z.link); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | output->dirty = 1; |
| 275 | weston_output_damage(output); |
| 276 | } |
| 277 | |
| 278 | WL_EXPORT void |
| 279 | weston_output_update_zoom(struct weston_output *output, uint32_t type) |
| 280 | { |
| 281 | wl_fixed_t x = output->compositor->seat->pointer.x; |
| 282 | wl_fixed_t y = output->compositor->seat->pointer.y; |
| 283 | |
| 284 | zoom_area_center_from_pointer(output, &x, &y); |
| 285 | |
| 286 | if (type == ZOOM_FOCUS_POINTER) { |
| 287 | if (wl_list_empty(&output->zoom.animation_xy.link)) { |
| 288 | output->zoom.current.x = |
| 289 | output->compositor->seat->pointer.x; |
| 290 | output->zoom.current.y = |
| 291 | output->compositor->seat->pointer.y; |
| 292 | } else { |
| 293 | output->zoom.to.x = x; |
| 294 | output->zoom.to.y = y; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (type == ZOOM_FOCUS_TEXT) { |
| 299 | if (wl_list_empty(&output->zoom.animation_xy.link)) { |
| 300 | output->zoom.current.x = output->zoom.text_cursor.x; |
| 301 | output->zoom.current.y = output->zoom.text_cursor.y; |
| 302 | } else { |
| 303 | output->zoom.to.x = output->zoom.text_cursor.x; |
| 304 | output->zoom.to.y = output->zoom.text_cursor.y; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | weston_zoom_transition(output, type, x, y); |
| 309 | weston_output_update_zoom_transform(output); |
| 310 | } |
| 311 | |
| 312 | WL_EXPORT void |
| 313 | weston_output_init_zoom(struct weston_output *output) |
| 314 | { |
| 315 | output->zoom.active = 0; |
| 316 | output->zoom.increment = 0.07; |
| 317 | output->zoom.max_level = 0.95; |
| 318 | output->zoom.level = 0.0; |
| 319 | output->zoom.trans_x = 0.0; |
| 320 | output->zoom.trans_y = 0.0; |
| 321 | output->zoom.type = ZOOM_FOCUS_POINTER; |
| 322 | weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0); |
| 323 | output->zoom.spring_z.friction = 1000; |
| 324 | output->zoom.animation_z.frame = weston_zoom_frame_z; |
| 325 | wl_list_init(&output->zoom.animation_z.link); |
| 326 | weston_spring_init(&output->zoom.spring_xy, 250.0, 0.0, 0.0); |
| 327 | output->zoom.spring_xy.friction = 1000; |
| 328 | output->zoom.animation_xy.frame = weston_zoom_frame_xy; |
| 329 | wl_list_init(&output->zoom.animation_xy.link); |
| 330 | } |