blob: 4faa8b4ea06b8e0c3c75fc74eea512ae08463567 [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_matrix_init(struct weston_matrix *matrix)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050032{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050033 static const struct weston_matrix identity = {
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050034 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
35 };
36
37 memcpy(matrix, &identity, sizeof identity);
38}
39
Pekka Paalanenc61eca62012-01-06 14:10:06 +020040WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050041weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050042{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050043 struct weston_matrix tmp;
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050044 const GLfloat *row, *column;
45 div_t d;
46 int i, j;
47
48 for (i = 0; i < 16; i++) {
49 tmp.d[i] = 0;
50 d = div(i, 4);
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];
55 }
56 memcpy(m, &tmp, sizeof tmp);
57}
58
59WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050060weston_matrix_translate(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050061{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050062 struct weston_matrix translate = {
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050063 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
64 };
65
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050066 weston_matrix_multiply(matrix, &translate);
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050067}
68
69WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050070weston_matrix_scale(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050071{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050072 struct weston_matrix scale = {
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050073 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
74 };
75
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050076 weston_matrix_multiply(matrix, &scale);
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050077}
78
79WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050080weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050081{
82 int i, j;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050083 struct weston_vector t;
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050084
85 for (i = 0; i < 4; i++) {
86 t.f[i] = 0;
87 for (j = 0; j < 4; j++)
88 t.f[i] += v->f[j] * matrix->d[i + j * 4];
89 }
90
91 *v = t;
92}
93
94WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050095weston_spring_init(struct weston_spring *spring,
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050096 double k, double current, double target)
97{
98 spring->k = k;
99 spring->friction = 400.0;
100 spring->current = current;
101 spring->previous = current;
102 spring->target = target;
103}
104
105WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106weston_spring_update(struct weston_spring *spring, uint32_t msec)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -0500107{
108 double force, v, current, step;
109
110 step = 0.01;
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;
116
117 spring->current =
118 current + (current - spring->previous) +
119 force * step * step;
120 spring->previous = current;
121
122#if 0
123 if (spring->current >= 1.0) {
124#ifdef TWEENER_BOUNCE
125 spring->current = 2.0 - spring->current;
126 spring->previous = 2.0 - spring->previous;
127#else
128 spring->current = 1.0;
129 spring->previous = 1.0;
130#endif
131 }
132
133 if (spring->current <= 0.0) {
134 spring->current = 0.0;
135 spring->previous = 0.0;
136 }
137#endif
138 spring->timestamp += 4;
139 }
140}
141
142WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500143weston_spring_done(struct weston_spring *spring)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -0500144{
145 return fabs(spring->previous - spring->target) < 0.0002 &&
146 fabs(spring->current - spring->target) < 0.0002;
147}
148
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500149struct weston_zoom {
150 struct weston_surface *surface;
151 struct weston_animation animation;
152 struct weston_spring spring;
153 struct weston_transform transform;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500154 struct wl_listener listener;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500155 GLfloat start, stop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 void (*done)(struct weston_zoom *zoom, void *data);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500157 void *data;
158};
159
160static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500161weston_zoom_destroy(struct weston_zoom *zoom)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500162{
163 wl_list_remove(&zoom->animation.link);
164 wl_list_remove(&zoom->listener.link);
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200165 wl_list_remove(&zoom->transform.link);
166 zoom->surface->transform.dirty = 1;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500167 if (zoom->done)
168 zoom->done(zoom, zoom->data);
169 free(zoom);
170}
171
172static void
173handle_zoom_surface_destroy(struct wl_listener *listener,
174 struct wl_resource *resource, uint32_t time)
175{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500176 struct weston_zoom *zoom =
177 container_of(listener, struct weston_zoom, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500178
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500179 weston_zoom_destroy(zoom);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500180}
181
182static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500183weston_zoom_frame(struct weston_animation *animation,
184 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_zoom *zoom =
187 container_of(animation, struct weston_zoom, animation);
188 struct weston_surface *es = zoom->surface;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500189 GLfloat scale;
190
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 weston_spring_update(&zoom->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500192
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500193 if (weston_spring_done(&zoom->spring)) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194 weston_zoom_destroy(zoom);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200195 return;
196 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500197
Kristian Høgsbergef458242011-12-15 11:24:25 -0500198 scale = zoom->start +
199 (zoom->stop - zoom->start) * zoom->spring.current;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500200 weston_matrix_init(&zoom->transform.matrix);
201 weston_matrix_translate(&zoom->transform.matrix,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500202 -(es->x + es->width / 2.0),
203 -(es->y + es->height / 2.0), 0);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500204 weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
205 weston_matrix_translate(&zoom->transform.matrix,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500206 es->x + es->width / 2.0,
207 es->y + es->height / 2.0, 0);
208
Kristian Høgsbergef458242011-12-15 11:24:25 -0500209 es->alpha = zoom->spring.current * 255;
210 if (es->alpha > 255)
211 es->alpha = 255;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500212 scale = 1.0 / zoom->spring.current;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500213 weston_matrix_init(&zoom->transform.inverse);
214 weston_matrix_scale(&zoom->transform.inverse, scale, scale, scale);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500215
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200216 zoom->surface->transform.dirty = 1;
217
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500218 weston_compositor_damage_all(es->compositor);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500219}
220
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221WL_EXPORT struct weston_zoom *
222weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
223 weston_zoom_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500224{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225 struct weston_zoom *zoom;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500226
227 zoom = malloc(sizeof *zoom);
228 if (!zoom)
229 return NULL;
230
231 zoom->surface = surface;
232 zoom->done = done;
233 zoom->data = data;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500234 zoom->start = start;
235 zoom->stop = stop;
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200236 wl_list_insert(&surface->transform.list, &zoom->transform.link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500237 weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0);
Kristian Høgsbergef458242011-12-15 11:24:25 -0500238 zoom->spring.friction = 700;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500239 zoom->spring.timestamp = weston_compositor_get_time();
240 zoom->animation.frame = weston_zoom_frame;
241 weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500242
243 zoom->listener.func = handle_zoom_surface_destroy;
244 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
245 &zoom->listener.link);
246
247 wl_list_insert(surface->compositor->animation_list.prev,
248 &zoom->animation.link);
249
250 return zoom;
251}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500252
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500253struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500254 uint32_t key;
255 uint32_t button;
256 uint32_t modifier;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500257 weston_binding_handler_t handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500258 void *data;
259 struct wl_list link;
260};
261
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500262WL_EXPORT struct weston_binding *
263weston_compositor_add_binding(struct weston_compositor *compositor,
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500264 uint32_t key, uint32_t button, uint32_t modifier,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500265 weston_binding_handler_t handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500268
269 binding = malloc(sizeof *binding);
270 if (binding == NULL)
271 return NULL;
272
273 binding->key = key;
274 binding->button = button;
275 binding->modifier = modifier;
276 binding->handler = handler;
277 binding->data = data;
278 wl_list_insert(compositor->binding_list.prev, &binding->link);
279
280 return binding;
281}
282
283WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500284weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500285{
286 wl_list_remove(&binding->link);
287 free(binding);
288}
289
290WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500291weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200292{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500293 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200294
295 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500296 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200297}
298
299WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300weston_compositor_run_binding(struct weston_compositor *compositor,
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500301 struct weston_input_device *device,
302 uint32_t time,
303 uint32_t key, uint32_t button, int32_t state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500304{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500305 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500306
307 wl_list_for_each(b, &compositor->binding_list, link) {
308 if (b->key == key && b->button == button &&
309 b->modifier == device->modifier_state && state) {
310 b->handler(&device->input_device,
311 time, key, button, state, b->data);
312 break;
313 }
314 }
315}