blob: f71d08cca7121f6f5c65c76eb3b3fdd4b1e8b8c4 [file] [log] [blame]
Scott Moreau429490d2012-06-17 18:10:59 -06001/*
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 Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Kristian Høgsberg583a2362012-06-25 17:13:58 -040025#include <stdlib.h>
26
Scott Moreau429490d2012-06-17 18:10:59 -060027#include "compositor.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040028#include "text-cursor-position-server-protocol.h"
29
Scott Moreau429490d2012-06-17 18:10:59 -060030static void
31weston_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äaa628d02012-11-16 11:48:47 +020045 if (output->zoom.active && output->zoom.level <= 0.0) {
Scott Moreau429490d2012-06-17 18:10:59 -060046 output->zoom.active = 0;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040047 output->disable_planes--;
Giulio Camuffo412b0242013-11-14 23:42:51 +010048 wl_list_remove(&output->zoom.motion_listener.link);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -040049 }
Scott Moreau429490d2012-06-17 18:10:59 -060050 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øgsbergef6f1362012-08-10 10:07:55 -040059static struct weston_seat *
60weston_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 Moreau429490d2012-06-17 18:10:59 -060067static void
68weston_zoom_frame_xy(struct weston_animation *animation,
69 struct weston_output *output, uint32_t msecs)
70{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -040071 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -060072 wl_fixed_t x, y;
Kristian Høgsbergef6f1362012-08-10 10:07:55 -040073
Scott Moreau429490d2012-06-17 18:10:59 -060074 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 Ekstranda7af7042013-10-12 22:38:11 -050089 output->zoom.current.x = seat->pointer->x;
90 output->zoom.current.y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -060091 wl_list_remove(&animation->link);
92 wl_list_init(&animation->link);
93 }
94
95 output->dirty = 1;
96 weston_output_damage(output);
97}
98
99static void
100zoom_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 Moreau1bad5db2012-08-18 01:04:05 -0600106 wl_fixed_t w = wl_fixed_from_int(output->width);
107 wl_fixed_t h = wl_fixed_from_int(output->height);
Scott Moreau429490d2012-06-17 18:10:59 -0600108
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
113static void
114weston_output_update_zoom_transform(struct weston_output *output)
115{
Scott Moreau429490d2012-06-17 18:10:59 -0600116 float global_x, global_y;
117 wl_fixed_t x = output->zoom.current.x;
118 wl_fixed_t y = output->zoom.current.y;
119 float trans_min, trans_max;
120 float ratio, level;
121
122 level = output->zoom.spring_z.current;
Scott Moreau429490d2012-06-17 18:10:59 -0600123
Ander Conselvan de Oliveira434e8f32012-11-21 15:11:36 +0200124 if (!output->zoom.active || level > output->zoom.max_level ||
125 level == 0.0f)
Scott Moreau429490d2012-06-17 18:10:59 -0600126 return;
127
Carlos Olmedo Escobar1a873aa2015-01-17 20:31:53 +0100128 ratio = 1 / level;
129
Jason Ekstranda7af7042013-10-12 22:38:11 -0500130 if (wl_list_empty(&output->zoom.animation_xy.link))
Scott Moreau429490d2012-06-17 18:10:59 -0600131 zoom_area_center_from_pointer(output, &x, &y);
132
133 global_x = wl_fixed_to_double(x);
134 global_y = wl_fixed_to_double(y);
135
136 output->zoom.trans_x =
Scott Moreau1bad5db2012-08-18 01:04:05 -0600137 ((((global_x - output->x) / output->width) *
Scott Moreau429490d2012-06-17 18:10:59 -0600138 (level * 2)) - level) * ratio;
139 output->zoom.trans_y =
Scott Moreau1bad5db2012-08-18 01:04:05 -0600140 ((((global_y - output->y) / output->height) *
Scott Moreau429490d2012-06-17 18:10:59 -0600141 (level * 2)) - level) * ratio;
142
143 trans_max = level * 2 - level;
144 trans_min = -trans_max;
145
146 /* Clip zoom area to output */
147 if (output->zoom.trans_x > trans_max)
148 output->zoom.trans_x = trans_max;
149 else if (output->zoom.trans_x < trans_min)
150 output->zoom.trans_x = trans_min;
151 if (output->zoom.trans_y > trans_max)
152 output->zoom.trans_y = trans_max;
153 else if (output->zoom.trans_y < trans_min)
154 output->zoom.trans_y = trans_min;
155}
156
157static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500158weston_zoom_transition(struct weston_output *output, wl_fixed_t x, wl_fixed_t y)
Scott Moreau429490d2012-06-17 18:10:59 -0600159{
Scott Moreau429490d2012-06-17 18:10:59 -0600160 if (output->zoom.level != output->zoom.spring_z.current) {
161 output->zoom.spring_z.target = output->zoom.level;
162 if (wl_list_empty(&output->zoom.animation_z.link)) {
163 output->zoom.animation_z.frame_counter = 0;
164 wl_list_insert(output->animation_list.prev,
165 &output->zoom.animation_z.link);
166 }
167 }
168
169 output->dirty = 1;
170 weston_output_damage(output);
171}
172
173WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500174weston_output_update_zoom(struct weston_output *output)
Scott Moreau429490d2012-06-17 18:10:59 -0600175{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400176 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400177 wl_fixed_t x = seat->pointer->x;
178 wl_fixed_t y = seat->pointer->y;
Scott Moreau429490d2012-06-17 18:10:59 -0600179
180 zoom_area_center_from_pointer(output, &x, &y);
181
Jason Ekstranda7af7042013-10-12 22:38:11 -0500182 if (wl_list_empty(&output->zoom.animation_xy.link)) {
183 output->zoom.current.x = seat->pointer->x;
184 output->zoom.current.y = seat->pointer->y;
185 } else {
186 output->zoom.to.x = x;
187 output->zoom.to.y = y;
Scott Moreau429490d2012-06-17 18:10:59 -0600188 }
189
Jason Ekstranda7af7042013-10-12 22:38:11 -0500190 weston_zoom_transition(output, x, y);
Scott Moreau429490d2012-06-17 18:10:59 -0600191 weston_output_update_zoom_transform(output);
192}
193
Giulio Camuffo412b0242013-11-14 23:42:51 +0100194static void
195motion(struct wl_listener *listener, void *data)
196{
197 struct weston_output_zoom *zoom =
198 container_of(listener, struct weston_output_zoom, motion_listener);
199 struct weston_output *output =
200 container_of(zoom, struct weston_output, zoom);
201
202 weston_output_update_zoom(output);
203}
204
205WL_EXPORT void
206weston_output_activate_zoom(struct weston_output *output)
207{
208 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
209
210 if (output->zoom.active)
211 return;
212
213 output->zoom.active = 1;
214 output->disable_planes++;
215 wl_signal_add(&seat->pointer->motion_signal,
216 &output->zoom.motion_listener);
217}
218
Scott Moreau429490d2012-06-17 18:10:59 -0600219WL_EXPORT void
220weston_output_init_zoom(struct weston_output *output)
221{
222 output->zoom.active = 0;
223 output->zoom.increment = 0.07;
224 output->zoom.max_level = 0.95;
225 output->zoom.level = 0.0;
226 output->zoom.trans_x = 0.0;
227 output->zoom.trans_y = 0.0;
Scott Moreau429490d2012-06-17 18:10:59 -0600228 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
229 output->zoom.spring_z.friction = 1000;
230 output->zoom.animation_z.frame = weston_zoom_frame_z;
231 wl_list_init(&output->zoom.animation_z.link);
232 weston_spring_init(&output->zoom.spring_xy, 250.0, 0.0, 0.0);
233 output->zoom.spring_xy.friction = 1000;
234 output->zoom.animation_xy.frame = weston_zoom_frame_xy;
235 wl_list_init(&output->zoom.animation_xy.link);
Giulio Camuffo412b0242013-11-14 23:42:51 +0100236 output->zoom.motion_listener.notify = motion;
Scott Moreau429490d2012-06-17 18:10:59 -0600237}