blob: d254d12d02885d9de2c9a1dd3ca3283074a1f5e8 [file] [log] [blame]
Kristian Høgsberg698c0582011-12-04 15:20:19 -05001/*
2 * Copyright © 2011 Intel Corporation
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>
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050024#include <string.h>
Kristian Høgsberg698c0582011-12-04 15:20:19 -050025#include <stdio.h>
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050026#include <math.h>
Kristian Høgsberg698c0582011-12-04 15:20:19 -050027
28#include "compositor.h"
29
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050030WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050031weston_spring_init(struct weston_spring *spring,
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050032 double k, double current, double target)
33{
34 spring->k = k;
35 spring->friction = 400.0;
36 spring->current = current;
37 spring->previous = current;
38 spring->target = target;
39}
40
41WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042weston_spring_update(struct weston_spring *spring, uint32_t msec)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050043{
44 double force, v, current, step;
45
46 step = 0.01;
47 while (4 < msec - spring->timestamp) {
48 current = spring->current;
49 v = current - spring->previous;
50 force = spring->k * (spring->target - current) / 10.0 +
51 (spring->previous - current) - v * spring->friction;
52
53 spring->current =
54 current + (current - spring->previous) +
55 force * step * step;
56 spring->previous = current;
57
58#if 0
59 if (spring->current >= 1.0) {
60#ifdef TWEENER_BOUNCE
61 spring->current = 2.0 - spring->current;
62 spring->previous = 2.0 - spring->previous;
63#else
64 spring->current = 1.0;
65 spring->previous = 1.0;
66#endif
67 }
68
69 if (spring->current <= 0.0) {
70 spring->current = 0.0;
71 spring->previous = 0.0;
72 }
73#endif
74 spring->timestamp += 4;
75 }
76}
77
78WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050079weston_spring_done(struct weston_spring *spring)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050080{
81 return fabs(spring->previous - spring->target) < 0.0002 &&
82 fabs(spring->current - spring->target) < 0.0002;
83}
84
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050085struct weston_zoom {
86 struct weston_surface *surface;
87 struct weston_animation animation;
88 struct weston_spring spring;
89 struct weston_transform transform;
Kristian Høgsberg698c0582011-12-04 15:20:19 -050090 struct wl_listener listener;
Kristian Høgsbergef458242011-12-15 11:24:25 -050091 GLfloat start, stop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092 void (*done)(struct weston_zoom *zoom, void *data);
Kristian Høgsberg698c0582011-12-04 15:20:19 -050093 void *data;
94};
95
96static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097weston_zoom_destroy(struct weston_zoom *zoom)
Kristian Høgsberg698c0582011-12-04 15:20:19 -050098{
99 wl_list_remove(&zoom->animation.link);
100 wl_list_remove(&zoom->listener.link);
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200101 wl_list_remove(&zoom->transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200102 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500103 if (zoom->done)
104 zoom->done(zoom, zoom->data);
105 free(zoom);
106}
107
108static void
109handle_zoom_surface_destroy(struct wl_listener *listener,
110 struct wl_resource *resource, uint32_t time)
111{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112 struct weston_zoom *zoom =
113 container_of(listener, struct weston_zoom, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500114
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115 weston_zoom_destroy(zoom);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500116}
117
118static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500119weston_zoom_frame(struct weston_animation *animation,
120 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500121{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_zoom *zoom =
123 container_of(animation, struct weston_zoom, animation);
124 struct weston_surface *es = zoom->surface;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500125 GLfloat scale;
126
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 weston_spring_update(&zoom->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500128
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500129 if (weston_spring_done(&zoom->spring)) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 weston_zoom_destroy(zoom);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200131 return;
132 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500133
Kristian Høgsbergef458242011-12-15 11:24:25 -0500134 scale = zoom->start +
135 (zoom->stop - zoom->start) * zoom->spring.current;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500136 weston_matrix_init(&zoom->transform.matrix);
137 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200138 -0.5f * es->geometry.width,
139 -0.5f * es->geometry.height, 0);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500140 weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
141 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200142 0.5f * es->geometry.width,
143 0.5f * es->geometry.height, 0);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500144
Kristian Høgsbergef458242011-12-15 11:24:25 -0500145 es->alpha = zoom->spring.current * 255;
146 if (es->alpha > 255)
147 es->alpha = 255;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500148
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200149 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg2ea09442012-02-28 23:12:52 -0500150 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500151}
152
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500153WL_EXPORT struct weston_zoom *
154weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
155 weston_zoom_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500156{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500157 struct weston_zoom *zoom;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500158
159 zoom = malloc(sizeof *zoom);
160 if (!zoom)
161 return NULL;
162
163 zoom->surface = surface;
164 zoom->done = done;
165 zoom->data = data;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500166 zoom->start = start;
167 zoom->stop = stop;
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200168 wl_list_insert(&surface->geometry.transformation_list,
169 &zoom->transform.link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500170 weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0);
Kristian Høgsbergef458242011-12-15 11:24:25 -0500171 zoom->spring.friction = 700;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500172 zoom->spring.timestamp = weston_compositor_get_time();
173 zoom->animation.frame = weston_zoom_frame;
174 weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500175
176 zoom->listener.func = handle_zoom_surface_destroy;
177 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
178 &zoom->listener.link);
179
Pekka Paalanen73772042012-01-25 14:45:18 +0200180 wl_list_insert(&surface->compositor->animation_list,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500181 &zoom->animation.link);
182
183 return zoom;
184}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500185
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500187 uint32_t key;
188 uint32_t button;
189 uint32_t modifier;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 weston_binding_handler_t handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500191 void *data;
192 struct wl_list link;
193};
194
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195WL_EXPORT struct weston_binding *
196weston_compositor_add_binding(struct weston_compositor *compositor,
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500197 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500198 weston_binding_handler_t handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500199{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500200 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500201
202 binding = malloc(sizeof *binding);
203 if (binding == NULL)
204 return NULL;
205
206 binding->key = key;
207 binding->button = button;
208 binding->modifier = modifier;
209 binding->handler = handler;
210 binding->data = data;
211 wl_list_insert(compositor->binding_list.prev, &binding->link);
212
213 return binding;
214}
215
216WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500217weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500218{
219 wl_list_remove(&binding->link);
220 free(binding);
221}
222
223WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500224weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200225{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500226 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200227
228 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500229 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200230}
231
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500232struct binding_keyboard_grab {
233 uint32_t key;
234 struct wl_keyboard_grab grab;
235};
236
237static void
238binding_key(struct wl_keyboard_grab *grab,
239 uint32_t time, uint32_t key, int32_t state)
240{
241 struct binding_keyboard_grab *b =
242 container_of(grab, struct binding_keyboard_grab, grab);
243 struct wl_resource *resource;
244
245 resource = grab->input_device->keyboard_focus_resource;
246 if (key == b->key) {
247 if (!state) {
248 wl_input_device_end_keyboard_grab(grab->input_device,
249 time);
250 free(b);
251 }
252 } else if (resource)
253 wl_input_device_send_key(resource, time, key, state);
254}
255
256static const struct wl_keyboard_grab_interface binding_grab = {
257 binding_key
258};
259
260static void
261install_binding_grab(struct wl_input_device *device,
262 uint32_t time, uint32_t key)
263{
264 struct binding_keyboard_grab *grab;
265
266 grab = malloc(sizeof *grab);
267 grab->key = key;
268 grab->grab.interface = &binding_grab;
269 wl_input_device_start_keyboard_grab(device, &grab->grab, time);
270}
271
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200272WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273weston_compositor_run_binding(struct weston_compositor *compositor,
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500274 struct weston_input_device *device,
275 uint32_t time,
276 uint32_t key, uint32_t button, int32_t state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500277{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500278 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500279
280 wl_list_for_each(b, &compositor->binding_list, link) {
281 if (b->key == key && b->button == button &&
282 b->modifier == device->modifier_state && state) {
283 b->handler(&device->input_device,
284 time, key, button, state, b->data);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500285
286 /* If this was a key binding and it didn't
287 * install a keyboard grab, install one now to
288 * swallow the key release. */
289 if (b->key &&
290 device->input_device.keyboard_grab ==
291 &device->input_device.default_keyboard_grab)
292 install_binding_grab(&device->input_device,
293 time, key);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500294 }
295 }
296}