blob: b8995a7b29695e352477ed8e7ce8d366f21ecf66 [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
23#include "compositor.h"
24
25WL_EXPORT void
26weston_text_cursor_position_notify(struct weston_surface *surface,
27 wl_fixed_t cur_pos_x,
28 wl_fixed_t cur_pos_y)
29{
30 struct weston_output *output;
31 wl_fixed_t global_x, global_y;
32
33 weston_surface_to_global_fixed(surface, cur_pos_x, cur_pos_y,
34 &global_x, &global_y);
35
36 wl_list_for_each(output, &surface->compositor->output_list, link)
37 if (output->zoom.active &&
38 pixman_region32_contains_point(&output->region,
39 wl_fixed_to_int(global_x),
40 wl_fixed_to_int(global_y),
41 NULL)) {
42 output->zoom.text_cursor.x = global_x;
43 output->zoom.text_cursor.y = global_y;
44 weston_output_update_zoom(output, ZOOM_FOCUS_TEXT);
45 }
46}
47
48static void
49weston_zoom_frame_z(struct weston_animation *animation,
50 struct weston_output *output, uint32_t msecs)
51{
52 if (animation->frame_counter <= 1)
53 output->zoom.spring_z.timestamp = msecs;
54
55 weston_spring_update(&output->zoom.spring_z, msecs);
56
57 if (output->zoom.spring_z.current > output->zoom.max_level)
58 output->zoom.spring_z.current = output->zoom.max_level;
59 else if (output->zoom.spring_z.current < 0.0)
60 output->zoom.spring_z.current = 0.0;
61
62 if (weston_spring_done(&output->zoom.spring_z)) {
63 if (output->zoom.level <= 0.0)
64 output->zoom.active = 0;
65 output->zoom.spring_z.current = output->zoom.level;
66 wl_list_remove(&animation->link);
67 wl_list_init(&animation->link);
68 }
69
70 output->dirty = 1;
71 weston_output_damage(output);
72}
73
74static void
75weston_zoom_frame_xy(struct weston_animation *animation,
76 struct weston_output *output, uint32_t msecs)
77{
78 wl_fixed_t x, y;
79 if (animation->frame_counter <= 1)
80 output->zoom.spring_xy.timestamp = msecs;
81
82 weston_spring_update(&output->zoom.spring_xy, msecs);
83
84 x = output->zoom.from.x - ((output->zoom.from.x - output->zoom.to.x) *
85 output->zoom.spring_xy.current);
86 y = output->zoom.from.y - ((output->zoom.from.y - output->zoom.to.y) *
87 output->zoom.spring_xy.current);
88
89 output->zoom.current.x = x;
90 output->zoom.current.y = y;
91
92 if (weston_spring_done(&output->zoom.spring_xy)) {
93 output->zoom.spring_xy.current = output->zoom.spring_xy.target;
94 output->zoom.current.x =
95 (output->zoom.type == ZOOM_FOCUS_POINTER) ?
96 output->compositor->seat->pointer.x :
97 output->zoom.text_cursor.x;
98 output->zoom.current.y =
99 (output->zoom.type == ZOOM_FOCUS_POINTER) ?
100 output->compositor->seat->pointer.y :
101 output->zoom.text_cursor.y;
102 wl_list_remove(&animation->link);
103 wl_list_init(&animation->link);
104 }
105
106 output->dirty = 1;
107 weston_output_damage(output);
108}
109
110static void
111zoom_area_center_from_pointer(struct weston_output *output,
112 wl_fixed_t *x, wl_fixed_t *y)
113{
114 float level = output->zoom.spring_z.current;
115 wl_fixed_t offset_x = wl_fixed_from_int(output->x);
116 wl_fixed_t offset_y = wl_fixed_from_int(output->y);
117 wl_fixed_t w = wl_fixed_from_int(output->current->width);
118 wl_fixed_t h = wl_fixed_from_int(output->current->height);
119
120 *x -= ((((*x - offset_x) / (float) w) - 0.5) * (w * (1.0 - level)));
121 *y -= ((((*y - offset_y) / (float) h) - 0.5) * (h * (1.0 - level)));
122}
123
124static void
125weston_output_update_zoom_transform(struct weston_output *output)
126{
127 uint32_t type = output->zoom.type;
128 float global_x, global_y;
129 wl_fixed_t x = output->zoom.current.x;
130 wl_fixed_t y = output->zoom.current.y;
131 float trans_min, trans_max;
132 float ratio, level;
133
134 level = output->zoom.spring_z.current;
135 ratio = 1 / level;
136
137 if (!output->zoom.active || level > output->zoom.max_level)
138 return;
139
140 if (type == ZOOM_FOCUS_POINTER &&
141 wl_list_empty(&output->zoom.animation_xy.link))
142 zoom_area_center_from_pointer(output, &x, &y);
143
144 global_x = wl_fixed_to_double(x);
145 global_y = wl_fixed_to_double(y);
146
147 output->zoom.trans_x =
148 ((((global_x - output->x) / output->current->width) *
149 (level * 2)) - level) * ratio;
150 output->zoom.trans_y =
151 ((((global_y - output->y) / output->current->height) *
152 (level * 2)) - level) * ratio;
153
154 trans_max = level * 2 - level;
155 trans_min = -trans_max;
156
157 /* Clip zoom area to output */
158 if (output->zoom.trans_x > trans_max)
159 output->zoom.trans_x = trans_max;
160 else if (output->zoom.trans_x < trans_min)
161 output->zoom.trans_x = trans_min;
162 if (output->zoom.trans_y > trans_max)
163 output->zoom.trans_y = trans_max;
164 else if (output->zoom.trans_y < trans_min)
165 output->zoom.trans_y = trans_min;
166}
167
168static void
169weston_zoom_transition(struct weston_output *output, uint32_t type,
170 wl_fixed_t x, wl_fixed_t y)
171{
172 if (output->zoom.type != type) {
173 /* Set from/to points and start animation */
174 output->zoom.spring_xy.current = 0.0;
175 output->zoom.spring_xy.previous = 0.0;
176 output->zoom.spring_xy.target = 1.0;
177
178 if (wl_list_empty(&output->zoom.animation_xy.link)) {
179 output->zoom.animation_xy.frame_counter = 0;
180 wl_list_insert(output->animation_list.prev,
181 &output->zoom.animation_xy.link);
182
183 output->zoom.from.x = (type == ZOOM_FOCUS_TEXT) ?
184 x : output->zoom.text_cursor.x;
185 output->zoom.from.y = (type == ZOOM_FOCUS_TEXT) ?
186 y : output->zoom.text_cursor.y;
187 } else {
188 output->zoom.from.x = output->zoom.current.x;
189 output->zoom.from.y = output->zoom.current.y;
190 }
191
192 output->zoom.to.x = (type == ZOOM_FOCUS_POINTER) ?
193 x : output->zoom.text_cursor.x;
194 output->zoom.to.y = (type == ZOOM_FOCUS_POINTER) ?
195 y : output->zoom.text_cursor.y;
196 output->zoom.current.x = output->zoom.from.x;
197 output->zoom.current.y = output->zoom.from.y;
198
199 output->zoom.type = type;
200 }
201
202 if (output->zoom.level != output->zoom.spring_z.current) {
203 output->zoom.spring_z.target = output->zoom.level;
204 if (wl_list_empty(&output->zoom.animation_z.link)) {
205 output->zoom.animation_z.frame_counter = 0;
206 wl_list_insert(output->animation_list.prev,
207 &output->zoom.animation_z.link);
208 }
209 }
210
211 output->dirty = 1;
212 weston_output_damage(output);
213}
214
215WL_EXPORT void
216weston_output_update_zoom(struct weston_output *output, uint32_t type)
217{
218 wl_fixed_t x = output->compositor->seat->pointer.x;
219 wl_fixed_t y = output->compositor->seat->pointer.y;
220
221 zoom_area_center_from_pointer(output, &x, &y);
222
223 if (type == ZOOM_FOCUS_POINTER) {
224 if (wl_list_empty(&output->zoom.animation_xy.link)) {
225 output->zoom.current.x =
226 output->compositor->seat->pointer.x;
227 output->zoom.current.y =
228 output->compositor->seat->pointer.y;
229 } else {
230 output->zoom.to.x = x;
231 output->zoom.to.y = y;
232 }
233 }
234
235 if (type == ZOOM_FOCUS_TEXT) {
236 if (wl_list_empty(&output->zoom.animation_xy.link)) {
237 output->zoom.current.x = output->zoom.text_cursor.x;
238 output->zoom.current.y = output->zoom.text_cursor.y;
239 } else {
240 output->zoom.to.x = output->zoom.text_cursor.x;
241 output->zoom.to.y = output->zoom.text_cursor.y;
242 }
243 }
244
245 weston_zoom_transition(output, type, x, y);
246 weston_output_update_zoom_transform(output);
247}
248
249WL_EXPORT void
250weston_output_init_zoom(struct weston_output *output)
251{
252 output->zoom.active = 0;
253 output->zoom.increment = 0.07;
254 output->zoom.max_level = 0.95;
255 output->zoom.level = 0.0;
256 output->zoom.trans_x = 0.0;
257 output->zoom.trans_y = 0.0;
258 output->zoom.type = ZOOM_FOCUS_POINTER;
259 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
260 output->zoom.spring_z.friction = 1000;
261 output->zoom.animation_z.frame = weston_zoom_frame_z;
262 wl_list_init(&output->zoom.animation_z.link);
263 weston_spring_init(&output->zoom.spring_xy, 250.0, 0.0, 0.0);
264 output->zoom.spring_xy.friction = 1000;
265 output->zoom.animation_xy.frame = weston_zoom_frame_xy;
266 wl_list_init(&output->zoom.animation_xy.link);
267}