2 * Copyright © 2011 Intel Corporation
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.
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.
28 #include "compositor.h"
31 weston_matrix_init(struct weston_matrix *matrix)
33 static const struct weston_matrix identity = {
34 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
37 memcpy(matrix, &identity, sizeof identity);
41 weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
43 struct weston_matrix tmp;
44 const GLfloat *row, *column;
48 for (i = 0; i < 16; i++) {
51 row = m->d + d.quot * 4;
52 column = n->d + d.rem;
53 for (j = 0; j < 4; j++)
54 tmp.d[i] += row[j] * column[j * 4];
56 memcpy(m, &tmp, sizeof tmp);
60 weston_matrix_translate(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
62 struct weston_matrix translate = {
63 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
66 weston_matrix_multiply(matrix, &translate);
70 weston_matrix_scale(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
72 struct weston_matrix scale = {
73 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
76 weston_matrix_multiply(matrix, &scale);
80 weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
83 struct weston_vector t;
85 for (i = 0; i < 4; i++) {
87 for (j = 0; j < 4; j++)
88 t.f[i] += v->f[j] * matrix->d[i + j * 4];
95 weston_spring_init(struct weston_spring *spring,
96 double k, double current, double target)
99 spring->friction = 400.0;
100 spring->current = current;
101 spring->previous = current;
102 spring->target = target;
106 weston_spring_update(struct weston_spring *spring, uint32_t msec)
108 double force, v, current, step;
111 while (4 < msec - spring->timestamp) {
112 current = spring->current;
113 v = current - spring->previous;
114 force = spring->k * (spring->target - current) / 10.0 +
115 (spring->previous - current) - v * spring->friction;
118 current + (current - spring->previous) +
120 spring->previous = current;
123 if (spring->current >= 1.0) {
124 #ifdef TWEENER_BOUNCE
125 spring->current = 2.0 - spring->current;
126 spring->previous = 2.0 - spring->previous;
128 spring->current = 1.0;
129 spring->previous = 1.0;
133 if (spring->current <= 0.0) {
134 spring->current = 0.0;
135 spring->previous = 0.0;
138 spring->timestamp += 4;
143 weston_spring_done(struct weston_spring *spring)
145 return fabs(spring->previous - spring->target) < 0.0002 &&
146 fabs(spring->current - spring->target) < 0.0002;
150 struct weston_surface *surface;
151 struct weston_animation animation;
152 struct weston_spring spring;
153 struct weston_transform transform;
154 struct wl_listener listener;
156 void (*done)(struct weston_zoom *zoom, void *data);
161 weston_zoom_destroy(struct weston_zoom *zoom)
163 wl_list_remove(&zoom->animation.link);
164 wl_list_remove(&zoom->listener.link);
165 zoom->surface->transform = NULL;
167 zoom->done(zoom, zoom->data);
172 handle_zoom_surface_destroy(struct wl_listener *listener,
173 struct wl_resource *resource, uint32_t time)
175 struct weston_zoom *zoom =
176 container_of(listener, struct weston_zoom, listener);
178 weston_zoom_destroy(zoom);
182 weston_zoom_frame(struct weston_animation *animation,
183 struct weston_output *output, uint32_t msecs)
185 struct weston_zoom *zoom =
186 container_of(animation, struct weston_zoom, animation);
187 struct weston_surface *es = zoom->surface;
190 weston_spring_update(&zoom->spring, msecs);
192 if (weston_spring_done(&zoom->spring))
193 weston_zoom_destroy(zoom);
195 scale = zoom->start +
196 (zoom->stop - zoom->start) * zoom->spring.current;
197 weston_matrix_init(&zoom->transform.matrix);
198 weston_matrix_translate(&zoom->transform.matrix,
199 -(es->x + es->width / 2.0),
200 -(es->y + es->height / 2.0), 0);
201 weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
202 weston_matrix_translate(&zoom->transform.matrix,
203 es->x + es->width / 2.0,
204 es->y + es->height / 2.0, 0);
206 es->alpha = zoom->spring.current * 255;
209 scale = 1.0 / zoom->spring.current;
210 weston_matrix_init(&zoom->transform.inverse);
211 weston_matrix_scale(&zoom->transform.inverse, scale, scale, scale);
213 weston_compositor_damage_all(es->compositor);
216 WL_EXPORT struct weston_zoom *
217 weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
218 weston_zoom_done_func_t done, void *data)
220 struct weston_zoom *zoom;
222 zoom = malloc(sizeof *zoom);
226 zoom->surface = surface;
231 surface->transform = &zoom->transform;
232 weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0);
233 zoom->spring.friction = 700;
234 zoom->spring.timestamp = weston_compositor_get_time();
235 zoom->animation.frame = weston_zoom_frame;
236 weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp);
238 zoom->listener.func = handle_zoom_surface_destroy;
239 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
240 &zoom->listener.link);
242 wl_list_insert(surface->compositor->animation_list.prev,
243 &zoom->animation.link);
248 struct weston_binding {
252 weston_binding_handler_t handler;
257 WL_EXPORT struct weston_binding *
258 weston_compositor_add_binding(struct weston_compositor *compositor,
259 uint32_t key, uint32_t button, uint32_t modifier,
260 weston_binding_handler_t handler, void *data)
262 struct weston_binding *binding;
264 binding = malloc(sizeof *binding);
269 binding->button = button;
270 binding->modifier = modifier;
271 binding->handler = handler;
272 binding->data = data;
273 wl_list_insert(compositor->binding_list.prev, &binding->link);
279 weston_binding_destroy(struct weston_binding *binding)
281 wl_list_remove(&binding->link);
286 weston_compositor_run_binding(struct weston_compositor *compositor,
287 struct weston_input_device *device,
289 uint32_t key, uint32_t button, int32_t state)
291 struct weston_binding *b;
293 wl_list_for_each(b, &compositor->binding_list, link) {
294 if (b->key == key && b->button == button &&
295 b->modifier == device->modifier_state && state) {
296 b->handler(&device->input_device,
297 time, key, button, state, b->data);