blob: 56a9eadb11d65095d53278d0942cec1bcf358f61 [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
Kristian Høgsberg583a2362012-06-25 17:13:58 -040023#include <stdlib.h>
24
Scott Moreau429490d2012-06-17 18:10:59 -060025#include "compositor.h"
Kristian Høgsberg583a2362012-06-25 17:13:58 -040026#include "text-cursor-position-server-protocol.h"
27
28struct 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
35static void
36text_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
44struct text_cursor_position_interface text_cursor_position_implementation = {
45 text_cursor_position_notify
46};
47
48static void
49bind_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
56static void
57text_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
66void
67text_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 Moreau429490d2012-06-17 18:10:59 -060087
88WL_EXPORT void
89weston_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
111static void
112weston_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)) {
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400126 if (output->zoom.level <= 0.0) {
Scott Moreau429490d2012-06-17 18:10:59 -0600127 output->zoom.active = 0;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -0400128 output->disable_planes--;
129 }
Scott Moreau429490d2012-06-17 18:10:59 -0600130 output->zoom.spring_z.current = output->zoom.level;
131 wl_list_remove(&animation->link);
132 wl_list_init(&animation->link);
133 }
134
135 output->dirty = 1;
136 weston_output_damage(output);
137}
138
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400139static struct weston_seat *
140weston_zoom_pick_seat(struct weston_compositor *compositor)
141{
142 return container_of(compositor->seat_list.next,
143 struct weston_seat, link);
144}
145
146
Scott Moreau429490d2012-06-17 18:10:59 -0600147static void
148weston_zoom_frame_xy(struct weston_animation *animation,
149 struct weston_output *output, uint32_t msecs)
150{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400151 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
Scott Moreau429490d2012-06-17 18:10:59 -0600152 wl_fixed_t x, y;
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400153
Scott Moreau429490d2012-06-17 18:10:59 -0600154 if (animation->frame_counter <= 1)
155 output->zoom.spring_xy.timestamp = msecs;
156
157 weston_spring_update(&output->zoom.spring_xy, msecs);
158
159 x = output->zoom.from.x - ((output->zoom.from.x - output->zoom.to.x) *
160 output->zoom.spring_xy.current);
161 y = output->zoom.from.y - ((output->zoom.from.y - output->zoom.to.y) *
162 output->zoom.spring_xy.current);
163
164 output->zoom.current.x = x;
165 output->zoom.current.y = y;
166
167 if (weston_spring_done(&output->zoom.spring_xy)) {
168 output->zoom.spring_xy.current = output->zoom.spring_xy.target;
169 output->zoom.current.x =
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400170 output->zoom.type == ZOOM_FOCUS_POINTER ?
171 seat->pointer.x : output->zoom.text_cursor.x;
Scott Moreau429490d2012-06-17 18:10:59 -0600172 output->zoom.current.y =
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400173 output->zoom.type == ZOOM_FOCUS_POINTER ?
174 seat->pointer.y : output->zoom.text_cursor.y;
Scott Moreau429490d2012-06-17 18:10:59 -0600175 wl_list_remove(&animation->link);
176 wl_list_init(&animation->link);
177 }
178
179 output->dirty = 1;
180 weston_output_damage(output);
181}
182
183static void
184zoom_area_center_from_pointer(struct weston_output *output,
185 wl_fixed_t *x, wl_fixed_t *y)
186{
187 float level = output->zoom.spring_z.current;
188 wl_fixed_t offset_x = wl_fixed_from_int(output->x);
189 wl_fixed_t offset_y = wl_fixed_from_int(output->y);
190 wl_fixed_t w = wl_fixed_from_int(output->current->width);
191 wl_fixed_t h = wl_fixed_from_int(output->current->height);
192
193 *x -= ((((*x - offset_x) / (float) w) - 0.5) * (w * (1.0 - level)));
194 *y -= ((((*y - offset_y) / (float) h) - 0.5) * (h * (1.0 - level)));
195}
196
197static void
198weston_output_update_zoom_transform(struct weston_output *output)
199{
200 uint32_t type = output->zoom.type;
201 float global_x, global_y;
202 wl_fixed_t x = output->zoom.current.x;
203 wl_fixed_t y = output->zoom.current.y;
204 float trans_min, trans_max;
205 float ratio, level;
206
207 level = output->zoom.spring_z.current;
208 ratio = 1 / level;
209
210 if (!output->zoom.active || level > output->zoom.max_level)
211 return;
212
213 if (type == ZOOM_FOCUS_POINTER &&
214 wl_list_empty(&output->zoom.animation_xy.link))
215 zoom_area_center_from_pointer(output, &x, &y);
216
217 global_x = wl_fixed_to_double(x);
218 global_y = wl_fixed_to_double(y);
219
220 output->zoom.trans_x =
221 ((((global_x - output->x) / output->current->width) *
222 (level * 2)) - level) * ratio;
223 output->zoom.trans_y =
224 ((((global_y - output->y) / output->current->height) *
225 (level * 2)) - level) * ratio;
226
227 trans_max = level * 2 - level;
228 trans_min = -trans_max;
229
230 /* Clip zoom area to output */
231 if (output->zoom.trans_x > trans_max)
232 output->zoom.trans_x = trans_max;
233 else if (output->zoom.trans_x < trans_min)
234 output->zoom.trans_x = trans_min;
235 if (output->zoom.trans_y > trans_max)
236 output->zoom.trans_y = trans_max;
237 else if (output->zoom.trans_y < trans_min)
238 output->zoom.trans_y = trans_min;
239}
240
241static void
242weston_zoom_transition(struct weston_output *output, uint32_t type,
243 wl_fixed_t x, wl_fixed_t y)
244{
245 if (output->zoom.type != type) {
246 /* Set from/to points and start animation */
247 output->zoom.spring_xy.current = 0.0;
248 output->zoom.spring_xy.previous = 0.0;
249 output->zoom.spring_xy.target = 1.0;
250
251 if (wl_list_empty(&output->zoom.animation_xy.link)) {
252 output->zoom.animation_xy.frame_counter = 0;
253 wl_list_insert(output->animation_list.prev,
254 &output->zoom.animation_xy.link);
255
256 output->zoom.from.x = (type == ZOOM_FOCUS_TEXT) ?
257 x : output->zoom.text_cursor.x;
258 output->zoom.from.y = (type == ZOOM_FOCUS_TEXT) ?
259 y : output->zoom.text_cursor.y;
260 } else {
261 output->zoom.from.x = output->zoom.current.x;
262 output->zoom.from.y = output->zoom.current.y;
263 }
264
265 output->zoom.to.x = (type == ZOOM_FOCUS_POINTER) ?
266 x : output->zoom.text_cursor.x;
267 output->zoom.to.y = (type == ZOOM_FOCUS_POINTER) ?
268 y : output->zoom.text_cursor.y;
269 output->zoom.current.x = output->zoom.from.x;
270 output->zoom.current.y = output->zoom.from.y;
271
272 output->zoom.type = type;
273 }
274
275 if (output->zoom.level != output->zoom.spring_z.current) {
276 output->zoom.spring_z.target = output->zoom.level;
277 if (wl_list_empty(&output->zoom.animation_z.link)) {
278 output->zoom.animation_z.frame_counter = 0;
279 wl_list_insert(output->animation_list.prev,
280 &output->zoom.animation_z.link);
281 }
282 }
283
284 output->dirty = 1;
285 weston_output_damage(output);
286}
287
288WL_EXPORT void
289weston_output_update_zoom(struct weston_output *output, uint32_t type)
290{
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400291 struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
292 wl_fixed_t x = seat->pointer.x;
293 wl_fixed_t y = seat->pointer.y;
Scott Moreau429490d2012-06-17 18:10:59 -0600294
295 zoom_area_center_from_pointer(output, &x, &y);
296
297 if (type == ZOOM_FOCUS_POINTER) {
298 if (wl_list_empty(&output->zoom.animation_xy.link)) {
Kristian Høgsbergef6f1362012-08-10 10:07:55 -0400299 output->zoom.current.x = seat->pointer.x;
300 output->zoom.current.y = seat->pointer.y;
Scott Moreau429490d2012-06-17 18:10:59 -0600301 } else {
302 output->zoom.to.x = x;
303 output->zoom.to.y = y;
304 }
305 }
306
307 if (type == ZOOM_FOCUS_TEXT) {
308 if (wl_list_empty(&output->zoom.animation_xy.link)) {
309 output->zoom.current.x = output->zoom.text_cursor.x;
310 output->zoom.current.y = output->zoom.text_cursor.y;
311 } else {
312 output->zoom.to.x = output->zoom.text_cursor.x;
313 output->zoom.to.y = output->zoom.text_cursor.y;
314 }
315 }
316
317 weston_zoom_transition(output, type, x, y);
318 weston_output_update_zoom_transform(output);
319}
320
321WL_EXPORT void
322weston_output_init_zoom(struct weston_output *output)
323{
324 output->zoom.active = 0;
325 output->zoom.increment = 0.07;
326 output->zoom.max_level = 0.95;
327 output->zoom.level = 0.0;
328 output->zoom.trans_x = 0.0;
329 output->zoom.trans_y = 0.0;
330 output->zoom.type = ZOOM_FOCUS_POINTER;
331 weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
332 output->zoom.spring_z.friction = 1000;
333 output->zoom.animation_z.frame = weston_zoom_frame_z;
334 wl_list_init(&output->zoom.animation_z.link);
335 weston_spring_init(&output->zoom.spring_xy, 250.0, 0.0, 0.0);
336 output->zoom.spring_xy.friction = 1000;
337 output->zoom.animation_xy.frame = weston_zoom_frame_xy;
338 wl_list_init(&output->zoom.animation_xy.link);
339}