blob: cb03ddee6404d31f3d331c2a72820ad68108e23b [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øgsberg414bd422012-06-21 22:07:30 -040088typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
89
90struct weston_surface_animation {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_surface *surface;
92 struct weston_animation animation;
93 struct weston_spring spring;
94 struct weston_transform transform;
Kristian Høgsberg698c0582011-12-04 15:20:19 -050095 struct wl_listener listener;
Kristian Høgsbergef458242011-12-15 11:24:25 -050096 GLfloat start, stop;
Kristian Høgsberg414bd422012-06-21 22:07:30 -040097 weston_surface_animation_frame_func_t frame;
98 weston_surface_animation_done_func_t done;
Juan Zhao2abd07b2012-04-25 19:09:51 +080099 void *data;
100};
101
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500102static void
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400103weston_surface_animation_destroy(struct weston_surface_animation *animation)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500104{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400105 wl_list_remove(&animation->animation.link);
106 wl_list_remove(&animation->listener.link);
107 wl_list_remove(&animation->transform.link);
108 animation->surface->geometry.dirty = 1;
109 if (animation->done)
110 animation->done(animation, animation->data);
111 free(animation);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500112}
113
114static void
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400115handle_animation_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500116{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400117 struct weston_surface_animation *animation =
118 container_of(listener,
119 struct weston_surface_animation, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500120
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400121 weston_surface_animation_destroy(animation);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500122}
123
124static void
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400125weston_surface_animation_frame(struct weston_animation *base,
126 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500127{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400128 struct weston_surface_animation *animation =
129 container_of(base,
130 struct weston_surface_animation, animation);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500131
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400132 if (base->frame_counter <= 1)
133 animation->spring.timestamp = msecs;
Scott Moreaue2949db2012-06-11 13:09:23 -0600134
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400135 weston_spring_update(&animation->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500136
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400137 if (weston_spring_done(&animation->spring)) {
138 weston_surface_animation_destroy(animation);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200139 return;
140 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500141
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400142 if (animation->frame)
143 animation->frame(animation);
144
145 animation->surface->geometry.dirty = 1;
146 weston_compositor_schedule_repaint(animation->surface->compositor);
147}
148
149static struct weston_surface_animation *
150weston_surface_animation_run(struct weston_surface *surface,
151 GLfloat start, GLfloat stop,
152 weston_surface_animation_frame_func_t frame,
153 weston_surface_animation_done_func_t done,
154 void *data)
155{
156 struct weston_surface_animation *animation;
157
158 animation = malloc(sizeof *animation);
159 if (!animation)
160 return NULL;
161
162 animation->surface = surface;
163 animation->frame = frame;
164 animation->done = done;
165 animation->data = data;
166 animation->start = start;
167 animation->stop = stop;
168 weston_matrix_init(&animation->transform.matrix);
169 wl_list_insert(&surface->geometry.transformation_list,
170 &animation->transform.link);
171 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
172 animation->spring.friction = 700;
173 animation->animation.frame_counter = 0;
174 animation->animation.frame = weston_surface_animation_frame;
175 weston_surface_animation_frame(&animation->animation, NULL, 0);
176
177 animation->listener.notify = handle_animation_surface_destroy;
178 wl_signal_add(&surface->surface.resource.destroy_signal,
179 &animation->listener);
180
181 wl_list_insert(&surface->output->animation_list,
182 &animation->animation.link);
183
184 return animation;
185}
186
187static void
188zoom_frame(struct weston_surface_animation *animation)
189{
190 struct weston_surface *es = animation->surface;
191 GLfloat scale;
192
193 scale = animation->start +
194 (animation->stop - animation->start) *
195 animation->spring.current;
196 weston_matrix_init(&animation->transform.matrix);
197 weston_matrix_translate(&animation->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200198 -0.5f * es->geometry.width,
199 -0.5f * es->geometry.height, 0);
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400200 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
201 weston_matrix_translate(&animation->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200202 0.5f * es->geometry.width,
203 0.5f * es->geometry.height, 0);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500204
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400205 es->alpha = animation->spring.current;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400206 if (es->alpha > 1.0)
207 es->alpha = 1.0;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500208}
209
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400210WL_EXPORT struct weston_surface_animation *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500211weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400212 weston_surface_animation_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500213{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400214 return weston_surface_animation_run(surface, start, stop,
215 zoom_frame, done, data);
216}
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500217
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400218static void
219fade_frame(struct weston_surface_animation *animation)
220{
221 if (animation->spring.current > 1)
222 animation->surface->alpha = 1;
223 else if (animation->spring.current < 0 )
224 animation->surface->alpha = 0;
225 else
226 animation->surface->alpha = animation->spring.current;
227}
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500228
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400229WL_EXPORT struct weston_surface_animation *
230weston_fade_run(struct weston_surface *surface,
231 weston_surface_animation_done_func_t done, void *data)
232{
233 return weston_surface_animation_run(surface, 0, 0,
234 fade_frame, done, data);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500235}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500236
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -0400237static void
238slide_frame(struct weston_surface_animation *animation)
239{
240 GLfloat scale;
241
242 scale = animation->start +
243 (animation->stop - animation->start) *
244 animation->spring.current;
245 weston_matrix_init(&animation->transform.matrix);
246 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
247}
248
249WL_EXPORT struct weston_surface_animation *
250weston_slide_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
251 weston_surface_animation_done_func_t done, void *data)
252{
253 struct weston_surface_animation *animation;
254
255 animation = weston_surface_animation_run(surface, start, stop,
256 slide_frame, done, data);
257 animation->spring.friction = 900;
258 animation->spring.k = 300;
259
260 return animation;
261}
262
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500263struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500264 uint32_t key;
265 uint32_t button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600266 uint32_t axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500267 uint32_t modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +0100268 void *handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500269 void *data;
270 struct wl_list link;
271};
272
Daniel Stone325fc2d2012-05-30 16:31:58 +0100273static struct weston_binding *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500274weston_compositor_add_binding(struct weston_compositor *compositor,
Daniel Stone325fc2d2012-05-30 16:31:58 +0100275 uint32_t key, uint32_t button, uint32_t axis,
276 uint32_t modifier, void *handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500277{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500278 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500279
280 binding = malloc(sizeof *binding);
281 if (binding == NULL)
282 return NULL;
283
284 binding->key = key;
285 binding->button = button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600286 binding->axis = axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500287 binding->modifier = modifier;
288 binding->handler = handler;
289 binding->data = data;
Daniel Stone325fc2d2012-05-30 16:31:58 +0100290
291 return binding;
292}
293
294WL_EXPORT struct weston_binding *
295weston_compositor_add_key_binding(struct weston_compositor *compositor,
296 uint32_t key, uint32_t modifier,
297 weston_key_binding_handler_t handler,
298 void *data)
299{
300 struct weston_binding *binding;
301
302 binding = weston_compositor_add_binding(compositor, key, 0, 0,
303 modifier, handler, data);
304 if (binding == NULL)
305 return NULL;
306
307 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
308
309 return binding;
310}
311
312WL_EXPORT struct weston_binding *
313weston_compositor_add_button_binding(struct weston_compositor *compositor,
314 uint32_t button, uint32_t modifier,
315 weston_button_binding_handler_t handler,
316 void *data)
317{
318 struct weston_binding *binding;
319
320 binding = weston_compositor_add_binding(compositor, 0, button, 0,
321 modifier, handler, data);
322 if (binding == NULL)
323 return NULL;
324
325 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
326
327 return binding;
328}
329
330WL_EXPORT struct weston_binding *
331weston_compositor_add_axis_binding(struct weston_compositor *compositor,
332 uint32_t axis, uint32_t modifier,
333 weston_axis_binding_handler_t handler,
334 void *data)
335{
336 struct weston_binding *binding;
337
338 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
339 modifier, handler, data);
340 if (binding == NULL)
341 return NULL;
342
343 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500344
345 return binding;
346}
347
348WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500349weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500350{
351 wl_list_remove(&binding->link);
352 free(binding);
353}
354
355WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500356weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200357{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500358 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200359
360 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500361 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200362}
363
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500364struct binding_keyboard_grab {
365 uint32_t key;
366 struct wl_keyboard_grab grab;
367};
368
369static void
370binding_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100371 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500372{
373 struct binding_keyboard_grab *b =
374 container_of(grab, struct binding_keyboard_grab, grab);
375 struct wl_resource *resource;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400376 struct wl_display *display;
Daniel Stonec9785ea2012-05-30 16:31:52 +0100377 enum wl_keyboard_key_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400378 uint32_t serial;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500379
Daniel Stone37816df2012-05-16 18:45:18 +0100380 resource = grab->keyboard->focus_resource;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500381 if (key == b->key) {
Daniel Stonec9785ea2012-05-30 16:31:52 +0100382 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Daniel Stone37816df2012-05-16 18:45:18 +0100383 wl_keyboard_end_grab(grab->keyboard);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500384 free(b);
385 }
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400386 } else if (resource) {
387 display = wl_client_get_display(resource->client);
388 serial = wl_display_next_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +0100389 wl_keyboard_send_key(resource, serial, time, key, state);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400390 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500391}
392
Daniel Stone351eb612012-05-31 15:27:47 -0400393static void
394binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
395 uint32_t mods_depressed, uint32_t mods_latched,
396 uint32_t mods_locked, uint32_t group)
397{
398 struct wl_resource *resource;
399
400 resource = grab->keyboard->focus_resource;
401 if (!resource)
402 return;
403
404 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
405 mods_latched, mods_locked, group);
406}
407
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500408static const struct wl_keyboard_grab_interface binding_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -0400409 binding_key,
410 binding_modifiers,
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500411};
412
413static void
Daniel Stone37816df2012-05-16 18:45:18 +0100414install_binding_grab(struct wl_seat *seat,
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500415 uint32_t time, uint32_t key)
416{
417 struct binding_keyboard_grab *grab;
418
419 grab = malloc(sizeof *grab);
420 grab->key = key;
421 grab->grab.interface = &binding_grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100422 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500423}
424
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200425WL_EXPORT void
Daniel Stone325fc2d2012-05-30 16:31:58 +0100426weston_compositor_run_key_binding(struct weston_compositor *compositor,
427 struct weston_seat *seat,
428 uint32_t time, uint32_t key,
429 enum wl_keyboard_key_state state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500431 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500432
Daniel Stone325fc2d2012-05-30 16:31:58 +0100433 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
434 return;
435
436 wl_list_for_each(b, &compositor->key_binding_list, link) {
437 if (b->key == key && b->modifier == seat->modifier_state) {
438 weston_key_binding_handler_t handler = b->handler;
439 handler(&seat->seat, time, key, b->data);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500440
441 /* If this was a key binding and it didn't
442 * install a keyboard grab, install one now to
443 * swallow the key release. */
Daniel Stone325fc2d2012-05-30 16:31:58 +0100444 if (seat->seat.keyboard->grab ==
Daniel Stone37816df2012-05-16 18:45:18 +0100445 &seat->seat.keyboard->default_grab)
446 install_binding_grab(&seat->seat, time, key);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500447 }
448 }
449}
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100450
Daniel Stone325fc2d2012-05-30 16:31:58 +0100451WL_EXPORT void
452weston_compositor_run_button_binding(struct weston_compositor *compositor,
453 struct weston_seat *seat,
454 uint32_t time, uint32_t button,
455 enum wl_pointer_button_state state)
456{
457 struct weston_binding *b;
458
459 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
460 return;
461
462 wl_list_for_each(b, &compositor->button_binding_list, link) {
463 if (b->button == button && b->modifier == seat->modifier_state) {
464 weston_button_binding_handler_t handler = b->handler;
465 handler(&seat->seat, time, button, b->data);
466 }
467 }
468}
469
470WL_EXPORT void
471weston_compositor_run_axis_binding(struct weston_compositor *compositor,
472 struct weston_seat *seat,
473 uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +0100474 wl_fixed_t value)
Daniel Stone325fc2d2012-05-30 16:31:58 +0100475{
476 struct weston_binding *b;
477
478 wl_list_for_each(b, &compositor->axis_binding_list, link) {
479 if (b->axis == axis && b->modifier == seat->modifier_state) {
480 weston_axis_binding_handler_t handler = b->handler;
481 handler(&seat->seat, time, axis, value, b->data);
482 }
483 }
484}
485
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100486WL_EXPORT int
487weston_environment_get_fd(const char *env)
488{
489 char *e, *end;
490 int fd, flags;
491
492 e = getenv(env);
493 if (!e)
494 return -1;
495 fd = strtol(e, &end, 0);
496 if (*end != '\0')
497 return -1;
498
499 flags = fcntl(fd, F_GETFD);
500 if (flags == -1)
501 return -1;
502
503 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
504 unsetenv(env);
505
506 return fd;
507}