text: Add language and text-direction to protocol
[profile/ivi/weston-ivi-shell.git] / src / zoom.c
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
23 #include <stdlib.h>
24
25 #include "compositor.h"
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 }
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.active && output->zoom.level <= 0.0) {
127                         output->zoom.active = 0;
128                         output->disable_planes--;
129                 }
130                 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
139 static struct weston_seat *
140 weston_zoom_pick_seat(struct weston_compositor *compositor)
141 {
142         return container_of(compositor->seat_list.next,
143                             struct weston_seat, link);
144 }
145
146
147 static void
148 weston_zoom_frame_xy(struct weston_animation *animation,
149                 struct weston_output *output, uint32_t msecs)
150 {
151         struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
152         wl_fixed_t x, y;
153
154         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 =
170                         output->zoom.type == ZOOM_FOCUS_POINTER ?
171                                 seat->pointer.x : output->zoom.text_cursor.x;
172                 output->zoom.current.y =
173                         output->zoom.type == ZOOM_FOCUS_POINTER ?
174                                 seat->pointer.y : output->zoom.text_cursor.y;
175                 wl_list_remove(&animation->link);
176                 wl_list_init(&animation->link);
177         }
178
179         output->dirty = 1;
180         weston_output_damage(output);
181 }
182
183 static void
184 zoom_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->width);
191         wl_fixed_t h = wl_fixed_from_int(output->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
197 static void
198 weston_zoom_apply_output_transform(struct weston_output *output,
199                                                 float *x, float *y)
200 {
201         float tx, ty;
202
203         switch(output->transform) {
204         case WL_OUTPUT_TRANSFORM_NORMAL:
205         default:
206                 return;
207         case WL_OUTPUT_TRANSFORM_90:
208                 tx = -*y;
209                 ty = *x;
210                 break;
211         case WL_OUTPUT_TRANSFORM_180:
212                 tx = -*x;
213                 ty = -*y;
214                 break;
215         case WL_OUTPUT_TRANSFORM_270:
216                 tx = *y;
217                 ty = -*x;
218                 break;
219         case WL_OUTPUT_TRANSFORM_FLIPPED:
220                 tx = -*x;
221                 ty = *y;
222                 break;
223         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
224                 tx = -*y;
225                 ty = -*x;
226                 break;
227         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
228                 tx = *x;
229                 ty = -*y;
230                 break;
231         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
232                 tx = *y;
233                 ty = *x;
234                 break;
235         }
236
237         *x = tx;
238         *y = ty;
239 }
240
241 static void
242 weston_output_update_zoom_transform(struct weston_output *output)
243 {
244         uint32_t type = output->zoom.type;
245         float global_x, global_y;
246         wl_fixed_t x = output->zoom.current.x;
247         wl_fixed_t y = output->zoom.current.y;
248         float trans_min, trans_max;
249         float ratio, level;
250
251         level = output->zoom.spring_z.current;
252         ratio = 1 / level;
253
254         if (!output->zoom.active || level > output->zoom.max_level ||
255             level == 0.0f)
256                 return;
257
258         if (type == ZOOM_FOCUS_POINTER &&
259                         wl_list_empty(&output->zoom.animation_xy.link))
260                 zoom_area_center_from_pointer(output, &x, &y);
261
262         global_x = wl_fixed_to_double(x);
263         global_y = wl_fixed_to_double(y);
264
265         output->zoom.trans_x =
266                 ((((global_x - output->x) / output->width) *
267                 (level * 2)) - level) * ratio;
268         output->zoom.trans_y =
269                 ((((global_y - output->y) / output->height) *
270                 (level * 2)) - level) * ratio;
271
272         weston_zoom_apply_output_transform(output, &output->zoom.trans_x,
273                                                    &output->zoom.trans_y);
274
275         trans_max = level * 2 - level;
276         trans_min = -trans_max;
277
278         /* Clip zoom area to output */
279         if (output->zoom.trans_x > trans_max)
280                 output->zoom.trans_x = trans_max;
281         else if (output->zoom.trans_x < trans_min)
282                 output->zoom.trans_x = trans_min;
283         if (output->zoom.trans_y > trans_max)
284                 output->zoom.trans_y = trans_max;
285         else if (output->zoom.trans_y < trans_min)
286                 output->zoom.trans_y = trans_min;
287 }
288
289 static void
290 weston_zoom_transition(struct weston_output *output, uint32_t type,
291                                                 wl_fixed_t x, wl_fixed_t y)
292 {
293         if (output->zoom.type != type) {
294                 /* Set from/to points and start animation */
295                 output->zoom.spring_xy.current = 0.0;
296                 output->zoom.spring_xy.previous = 0.0;
297                 output->zoom.spring_xy.target = 1.0;
298
299                 if (wl_list_empty(&output->zoom.animation_xy.link)) {
300                         output->zoom.animation_xy.frame_counter = 0;
301                         wl_list_insert(output->animation_list.prev,
302                                 &output->zoom.animation_xy.link);
303
304                         output->zoom.from.x = (type == ZOOM_FOCUS_TEXT) ?
305                                                 x : output->zoom.text_cursor.x;
306                         output->zoom.from.y = (type == ZOOM_FOCUS_TEXT) ?
307                                                 y : output->zoom.text_cursor.y;
308                 } else {
309                         output->zoom.from.x = output->zoom.current.x;
310                         output->zoom.from.y = output->zoom.current.y;
311                 }
312
313                 output->zoom.to.x = (type == ZOOM_FOCUS_POINTER) ?
314                                                 x : output->zoom.text_cursor.x;
315                 output->zoom.to.y = (type == ZOOM_FOCUS_POINTER) ?
316                                                 y : output->zoom.text_cursor.y;
317                 output->zoom.current.x = output->zoom.from.x;
318                 output->zoom.current.y = output->zoom.from.y;
319
320                 output->zoom.type = type;
321         }
322
323         if (output->zoom.level != output->zoom.spring_z.current) {
324                 output->zoom.spring_z.target = output->zoom.level;
325                 if (wl_list_empty(&output->zoom.animation_z.link)) {
326                         output->zoom.animation_z.frame_counter = 0;
327                         wl_list_insert(output->animation_list.prev,
328                                 &output->zoom.animation_z.link);
329                 }
330         }
331
332         output->dirty = 1;
333         weston_output_damage(output);
334 }
335
336 WL_EXPORT void
337 weston_output_update_zoom(struct weston_output *output, uint32_t type)
338 {
339         struct weston_seat *seat = weston_zoom_pick_seat(output->compositor);
340         wl_fixed_t x = seat->pointer.x;
341         wl_fixed_t y = seat->pointer.y;
342
343         zoom_area_center_from_pointer(output, &x, &y);
344
345         if (type == ZOOM_FOCUS_POINTER) {
346                 if (wl_list_empty(&output->zoom.animation_xy.link)) {
347                         output->zoom.current.x = seat->pointer.x;
348                         output->zoom.current.y = seat->pointer.y;
349                 } else {
350                         output->zoom.to.x = x;
351                         output->zoom.to.y = y;
352                 }
353         }
354
355         if (type == ZOOM_FOCUS_TEXT) {
356                 if (wl_list_empty(&output->zoom.animation_xy.link)) {
357                         output->zoom.current.x = output->zoom.text_cursor.x;
358                         output->zoom.current.y = output->zoom.text_cursor.y;
359                 } else {
360                         output->zoom.to.x = output->zoom.text_cursor.x;
361                         output->zoom.to.y = output->zoom.text_cursor.y;
362                 }
363         }
364
365         weston_zoom_transition(output, type, x, y);
366         weston_output_update_zoom_transform(output);
367 }
368
369 WL_EXPORT void
370 weston_output_init_zoom(struct weston_output *output)
371 {
372         output->zoom.active = 0;
373         output->zoom.increment = 0.07;
374         output->zoom.max_level = 0.95;
375         output->zoom.level = 0.0;
376         output->zoom.trans_x = 0.0;
377         output->zoom.trans_y = 0.0;
378         output->zoom.type = ZOOM_FOCUS_POINTER;
379         weston_spring_init(&output->zoom.spring_z, 250.0, 0.0, 0.0);
380         output->zoom.spring_z.friction = 1000;
381         output->zoom.animation_z.frame = weston_zoom_frame_z;
382         wl_list_init(&output->zoom.animation_z.link);
383         weston_spring_init(&output->zoom.spring_xy, 250.0, 0.0, 0.0);
384         output->zoom.spring_xy.friction = 1000;
385         output->zoom.animation_xy.frame = weston_zoom_frame_xy;
386         wl_list_init(&output->zoom.animation_xy.link);
387 }