blob: 99438202e789b2b8e49a0d912b58038dc524eb3f [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
Benjamin Franzkebfeda132012-01-30 14:04:04 +010028#include <unistd.h>
29#include <fcntl.h>
30
Kristian Høgsberg698c0582011-12-04 15:20:19 -050031#include "compositor.h"
32
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050033WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050034weston_spring_init(struct weston_spring *spring,
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050035 double k, double current, double target)
36{
37 spring->k = k;
38 spring->friction = 400.0;
39 spring->current = current;
40 spring->previous = current;
41 spring->target = target;
42}
43
44WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050045weston_spring_update(struct weston_spring *spring, uint32_t msec)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050046{
47 double force, v, current, step;
48
49 step = 0.01;
50 while (4 < msec - spring->timestamp) {
51 current = spring->current;
52 v = current - spring->previous;
53 force = spring->k * (spring->target - current) / 10.0 +
54 (spring->previous - current) - v * spring->friction;
55
56 spring->current =
57 current + (current - spring->previous) +
58 force * step * step;
59 spring->previous = current;
60
61#if 0
62 if (spring->current >= 1.0) {
63#ifdef TWEENER_BOUNCE
64 spring->current = 2.0 - spring->current;
65 spring->previous = 2.0 - spring->previous;
66#else
67 spring->current = 1.0;
68 spring->previous = 1.0;
69#endif
70 }
71
72 if (spring->current <= 0.0) {
73 spring->current = 0.0;
74 spring->previous = 0.0;
75 }
76#endif
77 spring->timestamp += 4;
78 }
79}
80
81WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082weston_spring_done(struct weston_spring *spring)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050083{
84 return fabs(spring->previous - spring->target) < 0.0002 &&
85 fabs(spring->current - spring->target) < 0.0002;
86}
87
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050088struct weston_zoom {
89 struct weston_surface *surface;
90 struct weston_animation animation;
91 struct weston_spring spring;
92 struct weston_transform transform;
Kristian Høgsberg698c0582011-12-04 15:20:19 -050093 struct wl_listener listener;
Kristian Høgsbergef458242011-12-15 11:24:25 -050094 GLfloat start, stop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050095 void (*done)(struct weston_zoom *zoom, void *data);
Kristian Høgsberg698c0582011-12-04 15:20:19 -050096 void *data;
97};
98
Juan Zhao2abd07b2012-04-25 19:09:51 +080099struct weston_fade {
100 struct weston_surface *surface;
101 struct weston_animation animation;
102 struct weston_spring spring;
103 struct wl_listener listener;
104 void (*done)(struct weston_fade *fade, void *data);
105 void *data;
106};
107
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500108static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109weston_zoom_destroy(struct weston_zoom *zoom)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500110{
111 wl_list_remove(&zoom->animation.link);
112 wl_list_remove(&zoom->listener.link);
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200113 wl_list_remove(&zoom->transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200114 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500115 if (zoom->done)
116 zoom->done(zoom, zoom->data);
117 free(zoom);
118}
119
120static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400121handle_zoom_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500122{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500123 struct weston_zoom *zoom =
124 container_of(listener, struct weston_zoom, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500125
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500126 weston_zoom_destroy(zoom);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500127}
128
129static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130weston_zoom_frame(struct weston_animation *animation,
131 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500132{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133 struct weston_zoom *zoom =
134 container_of(animation, struct weston_zoom, animation);
135 struct weston_surface *es = zoom->surface;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500136 GLfloat scale;
137
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138 weston_spring_update(&zoom->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500139
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500140 if (weston_spring_done(&zoom->spring)) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 weston_zoom_destroy(zoom);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200142 return;
143 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500144
Kristian Høgsbergef458242011-12-15 11:24:25 -0500145 scale = zoom->start +
146 (zoom->stop - zoom->start) * zoom->spring.current;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500147 weston_matrix_init(&zoom->transform.matrix);
148 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200149 -0.5f * es->geometry.width,
150 -0.5f * es->geometry.height, 0);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500151 weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
152 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200153 0.5f * es->geometry.width,
154 0.5f * es->geometry.height, 0);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500155
Kristian Høgsbergef458242011-12-15 11:24:25 -0500156 es->alpha = zoom->spring.current * 255;
157 if (es->alpha > 255)
158 es->alpha = 255;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500159
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200160 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg2ea09442012-02-28 23:12:52 -0500161 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500162}
163
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164WL_EXPORT struct weston_zoom *
165weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
166 weston_zoom_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500167{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500168 struct weston_zoom *zoom;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500169
170 zoom = malloc(sizeof *zoom);
171 if (!zoom)
172 return NULL;
173
174 zoom->surface = surface;
175 zoom->done = done;
176 zoom->data = data;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500177 zoom->start = start;
178 zoom->stop = stop;
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200179 wl_list_insert(&surface->geometry.transformation_list,
180 &zoom->transform.link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500181 weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0);
Kristian Høgsbergef458242011-12-15 11:24:25 -0500182 zoom->spring.friction = 700;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500183 zoom->spring.timestamp = weston_compositor_get_time();
184 zoom->animation.frame = weston_zoom_frame;
185 weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500186
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400187 zoom->listener.notify = handle_zoom_surface_destroy;
188 wl_signal_add(&surface->surface.resource.destroy_signal,
189 &zoom->listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500190
Pekka Paalanen73772042012-01-25 14:45:18 +0200191 wl_list_insert(&surface->compositor->animation_list,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500192 &zoom->animation.link);
193
194 return zoom;
195}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500196
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500198 uint32_t key;
199 uint32_t button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600200 uint32_t axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500201 uint32_t modifier;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500202 weston_binding_handler_t handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500203 void *data;
204 struct wl_list link;
205};
206
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500207WL_EXPORT struct weston_binding *
208weston_compositor_add_binding(struct weston_compositor *compositor,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600209 uint32_t key, uint32_t button, uint32_t axis, uint32_t modifier,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500210 weston_binding_handler_t handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500211{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500212 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500213
214 binding = malloc(sizeof *binding);
215 if (binding == NULL)
216 return NULL;
217
218 binding->key = key;
219 binding->button = button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600220 binding->axis = axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500221 binding->modifier = modifier;
222 binding->handler = handler;
223 binding->data = data;
224 wl_list_insert(compositor->binding_list.prev, &binding->link);
225
226 return binding;
227}
228
229WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500230weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500231{
232 wl_list_remove(&binding->link);
233 free(binding);
234}
235
236WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500237weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200238{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500239 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200240
241 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500242 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200243}
244
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500245struct binding_keyboard_grab {
246 uint32_t key;
247 struct wl_keyboard_grab grab;
248};
249
250static void
251binding_key(struct wl_keyboard_grab *grab,
252 uint32_t time, uint32_t key, int32_t state)
253{
254 struct binding_keyboard_grab *b =
255 container_of(grab, struct binding_keyboard_grab, grab);
256 struct wl_resource *resource;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400257 struct wl_display *display;
258 uint32_t serial;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500259
260 resource = grab->input_device->keyboard_focus_resource;
261 if (key == b->key) {
262 if (!state) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400263 wl_input_device_end_keyboard_grab(grab->input_device);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500264 free(b);
265 }
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400266 } else if (resource) {
267 display = wl_client_get_display(resource->client);
268 serial = wl_display_next_serial(display);
269 wl_input_device_send_key(resource, serial, time, key, state);
270 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500271}
272
273static const struct wl_keyboard_grab_interface binding_grab = {
274 binding_key
275};
276
277static void
278install_binding_grab(struct wl_input_device *device,
279 uint32_t time, uint32_t key)
280{
281 struct binding_keyboard_grab *grab;
282
283 grab = malloc(sizeof *grab);
284 grab->key = key;
285 grab->grab.interface = &binding_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400286 wl_input_device_start_keyboard_grab(device, &grab->grab);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500287}
288
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200289WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500290weston_compositor_run_binding(struct weston_compositor *compositor,
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500291 struct weston_input_device *device,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600292 uint32_t time, uint32_t key,
293 uint32_t button, uint32_t axis, int32_t state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500294{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500295 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500296
297 wl_list_for_each(b, &compositor->binding_list, link) {
Scott Moreau6a3633d2012-03-20 08:47:59 -0600298 if (b->key == key && b->button == button && b->axis == axis &&
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500299 b->modifier == device->modifier_state && state) {
300 b->handler(&device->input_device,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600301 time, key, button, axis, state, b->data);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500302
303 /* If this was a key binding and it didn't
304 * install a keyboard grab, install one now to
305 * swallow the key release. */
306 if (b->key &&
307 device->input_device.keyboard_grab ==
308 &device->input_device.default_keyboard_grab)
309 install_binding_grab(&device->input_device,
310 time, key);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500311 }
312 }
313}
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100314
315WL_EXPORT int
316weston_environment_get_fd(const char *env)
317{
318 char *e, *end;
319 int fd, flags;
320
321 e = getenv(env);
322 if (!e)
323 return -1;
324 fd = strtol(e, &end, 0);
325 if (*end != '\0')
326 return -1;
327
328 flags = fcntl(fd, F_GETFD);
329 if (flags == -1)
330 return -1;
331
332 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
333 unsetenv(env);
334
335 return fd;
336}
Juan Zhao2abd07b2012-04-25 19:09:51 +0800337/*fade in and fade out animation*/
338static void
339weston_fade_destroy(struct weston_fade *fade)
340{
341 wl_list_remove(&fade->animation.link);
342 wl_list_remove(&fade->listener.link);
343 fade->surface->geometry.dirty = 1;
344 if (fade->done)
345 fade->done(fade, fade->data);
346 free(fade);
347}
348
349static void
350handle_fade_surface_destroy(struct wl_listener *listener, void *data)
351{
352 struct weston_fade *fade =
353 container_of(listener, struct weston_fade, listener);
354
355 weston_fade_destroy(fade);
356}
357
358static void
359weston_fade_frame(struct weston_animation *animation,
360 struct weston_output *output, uint32_t msecs)
361{
362 struct weston_fade *fade =
363 container_of(animation, struct weston_fade, animation);
364 struct weston_surface *es = fade->surface;
365 float fade_factor;
366
367 weston_spring_update(&fade->spring, msecs);
368
369 if (weston_spring_done(&fade->spring)) {
370 weston_fade_destroy(fade);
371 return;
372 }
373 if (fade->spring.current > 1)
374 fade_factor = 1;
375 else if (fade->spring.current < 0 )
376 fade_factor = 0;
377 else
378 fade_factor = fade->spring.current;
379 es->alpha = fade_factor * 255;
380
381 fade->surface->geometry.dirty = 1;
382 weston_compositor_schedule_repaint(es->compositor);
383}
384
385WL_EXPORT struct weston_fade *
386weston_fade_run(struct weston_surface *surface,
387 weston_fade_done_func_t done, void *data)
388{
389 struct weston_fade *fade;
390
391 fade = malloc(sizeof *fade);
392 if (!fade)
393 return NULL;
394
395 fade->surface = surface;
396 fade->done = done;
397 fade->data = data;
398 weston_spring_init(&fade->spring, 200.0, 0, 1.0);
399 fade->spring.friction = 700;
400 fade->spring.timestamp = weston_compositor_get_time();
401 fade->animation.frame = weston_fade_frame;
402 weston_fade_frame(&fade->animation, NULL, fade->spring.timestamp);
403
404 fade->listener.notify = handle_fade_surface_destroy;
405 wl_signal_add(&surface->surface.resource.destroy_signal,
406 &fade->listener);
407
408 wl_list_insert(&surface->compositor->animation_list,
409 &fade->animation.link);
410
411 return fade;
412}