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.
31 #include "compositor.h"
34 weston_spring_init(struct weston_spring *spring,
35 double k, double current, double target)
38 spring->friction = 400.0;
39 spring->current = current;
40 spring->previous = current;
41 spring->target = target;
45 weston_spring_update(struct weston_spring *spring, uint32_t msec)
47 double force, v, current, step;
49 /* Limit the number of executions of the loop below by ensuring that
50 * the timestamp for last update of the spring is no more than 1s ago.
51 * This handles the case where time moves backwards or forwards in
54 if (msec - spring->timestamp > 1000) {
55 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
56 spring->timestamp, msec);
57 spring->timestamp = msec - 1000;
61 while (4 < msec - spring->timestamp) {
62 current = spring->current;
63 v = current - spring->previous;
64 force = spring->k * (spring->target - current) / 10.0 +
65 (spring->previous - current) - v * spring->friction;
68 current + (current - spring->previous) +
70 spring->previous = current;
73 if (spring->current >= 1.0) {
75 spring->current = 2.0 - spring->current;
76 spring->previous = 2.0 - spring->previous;
78 spring->current = 1.0;
79 spring->previous = 1.0;
83 if (spring->current <= 0.0) {
84 spring->current = 0.0;
85 spring->previous = 0.0;
88 spring->timestamp += 4;
93 weston_spring_done(struct weston_spring *spring)
95 return fabs(spring->previous - spring->target) < 0.0002 &&
96 fabs(spring->current - spring->target) < 0.0002;
99 typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
101 struct weston_surface_animation {
102 struct weston_surface *surface;
103 struct weston_animation animation;
104 struct weston_spring spring;
105 struct weston_transform transform;
106 struct wl_listener listener;
108 weston_surface_animation_frame_func_t frame;
109 weston_surface_animation_done_func_t done;
114 weston_surface_animation_destroy(struct weston_surface_animation *animation)
116 wl_list_remove(&animation->animation.link);
117 wl_list_remove(&animation->listener.link);
118 wl_list_remove(&animation->transform.link);
119 animation->surface->geometry.dirty = 1;
121 animation->done(animation, animation->data);
126 handle_animation_surface_destroy(struct wl_listener *listener, void *data)
128 struct weston_surface_animation *animation =
129 container_of(listener,
130 struct weston_surface_animation, listener);
132 weston_surface_animation_destroy(animation);
136 weston_surface_animation_frame(struct weston_animation *base,
137 struct weston_output *output, uint32_t msecs)
139 struct weston_surface_animation *animation =
141 struct weston_surface_animation, animation);
143 if (base->frame_counter <= 1)
144 animation->spring.timestamp = msecs;
146 weston_spring_update(&animation->spring, msecs);
148 if (weston_spring_done(&animation->spring)) {
149 weston_surface_animation_destroy(animation);
153 if (animation->frame)
154 animation->frame(animation);
156 animation->surface->geometry.dirty = 1;
157 weston_compositor_schedule_repaint(animation->surface->compositor);
160 static struct weston_surface_animation *
161 weston_surface_animation_run(struct weston_surface *surface,
162 float start, float stop,
163 weston_surface_animation_frame_func_t frame,
164 weston_surface_animation_done_func_t done,
167 struct weston_surface_animation *animation;
169 animation = malloc(sizeof *animation);
173 animation->surface = surface;
174 animation->frame = frame;
175 animation->done = done;
176 animation->data = data;
177 animation->start = start;
178 animation->stop = stop;
179 weston_matrix_init(&animation->transform.matrix);
180 wl_list_insert(&surface->geometry.transformation_list,
181 &animation->transform.link);
182 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
183 animation->spring.friction = 700;
184 animation->animation.frame_counter = 0;
185 animation->animation.frame = weston_surface_animation_frame;
186 weston_surface_animation_frame(&animation->animation, NULL, 0);
188 animation->listener.notify = handle_animation_surface_destroy;
189 wl_signal_add(&surface->surface.resource.destroy_signal,
190 &animation->listener);
192 wl_list_insert(&surface->output->animation_list,
193 &animation->animation.link);
199 zoom_frame(struct weston_surface_animation *animation)
201 struct weston_surface *es = animation->surface;
204 scale = animation->start +
205 (animation->stop - animation->start) *
206 animation->spring.current;
207 weston_matrix_init(&animation->transform.matrix);
208 weston_matrix_translate(&animation->transform.matrix,
209 -0.5f * es->geometry.width,
210 -0.5f * es->geometry.height, 0);
211 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
212 weston_matrix_translate(&animation->transform.matrix,
213 0.5f * es->geometry.width,
214 0.5f * es->geometry.height, 0);
216 es->alpha = animation->spring.current;
221 WL_EXPORT struct weston_surface_animation *
222 weston_zoom_run(struct weston_surface *surface, float start, float stop,
223 weston_surface_animation_done_func_t done, void *data)
225 return weston_surface_animation_run(surface, start, stop,
226 zoom_frame, done, data);
230 fade_frame(struct weston_surface_animation *animation)
232 if (animation->spring.current > 1)
233 animation->surface->alpha = 1;
234 else if (animation->spring.current < 0 )
235 animation->surface->alpha = 0;
237 animation->surface->alpha = animation->spring.current;
240 WL_EXPORT struct weston_surface_animation *
241 weston_fade_run(struct weston_surface *surface,
242 weston_surface_animation_done_func_t done, void *data)
244 return weston_surface_animation_run(surface, 0, 0,
245 fade_frame, done, data);
249 slide_frame(struct weston_surface_animation *animation)
253 scale = animation->start +
254 (animation->stop - animation->start) *
255 animation->spring.current;
256 weston_matrix_init(&animation->transform.matrix);
257 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
260 WL_EXPORT struct weston_surface_animation *
261 weston_slide_run(struct weston_surface *surface, float start, float stop,
262 weston_surface_animation_done_func_t done, void *data)
264 struct weston_surface_animation *animation;
266 animation = weston_surface_animation_run(surface, start, stop,
267 slide_frame, done, data);
268 animation->spring.friction = 900;
269 animation->spring.k = 300;
274 struct weston_binding {
284 static struct weston_binding *
285 weston_compositor_add_binding(struct weston_compositor *compositor,
286 uint32_t key, uint32_t button, uint32_t axis,
287 uint32_t modifier, void *handler, void *data)
289 struct weston_binding *binding;
291 binding = malloc(sizeof *binding);
296 binding->button = button;
297 binding->axis = axis;
298 binding->modifier = modifier;
299 binding->handler = handler;
300 binding->data = data;
305 WL_EXPORT struct weston_binding *
306 weston_compositor_add_key_binding(struct weston_compositor *compositor,
307 uint32_t key, uint32_t modifier,
308 weston_key_binding_handler_t handler,
311 struct weston_binding *binding;
313 binding = weston_compositor_add_binding(compositor, key, 0, 0,
314 modifier, handler, data);
318 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
323 WL_EXPORT struct weston_binding *
324 weston_compositor_add_button_binding(struct weston_compositor *compositor,
325 uint32_t button, uint32_t modifier,
326 weston_button_binding_handler_t handler,
329 struct weston_binding *binding;
331 binding = weston_compositor_add_binding(compositor, 0, button, 0,
332 modifier, handler, data);
336 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
341 WL_EXPORT struct weston_binding *
342 weston_compositor_add_axis_binding(struct weston_compositor *compositor,
343 uint32_t axis, uint32_t modifier,
344 weston_axis_binding_handler_t handler,
347 struct weston_binding *binding;
349 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
350 modifier, handler, data);
354 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
360 weston_binding_destroy(struct weston_binding *binding)
362 wl_list_remove(&binding->link);
367 weston_binding_list_destroy_all(struct wl_list *list)
369 struct weston_binding *binding, *tmp;
371 wl_list_for_each_safe(binding, tmp, list, link)
372 weston_binding_destroy(binding);
375 struct binding_keyboard_grab {
377 struct wl_keyboard_grab grab;
381 binding_key(struct wl_keyboard_grab *grab,
382 uint32_t time, uint32_t key, uint32_t state_w)
384 struct binding_keyboard_grab *b =
385 container_of(grab, struct binding_keyboard_grab, grab);
386 struct wl_resource *resource;
387 struct wl_display *display;
388 enum wl_keyboard_key_state state = state_w;
391 resource = grab->keyboard->focus_resource;
393 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
394 wl_keyboard_end_grab(grab->keyboard);
397 } else if (resource) {
398 display = wl_client_get_display(resource->client);
399 serial = wl_display_next_serial(display);
400 wl_keyboard_send_key(resource, serial, time, key, state);
405 binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
406 uint32_t mods_depressed, uint32_t mods_latched,
407 uint32_t mods_locked, uint32_t group)
409 struct wl_resource *resource;
411 resource = grab->keyboard->focus_resource;
415 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
416 mods_latched, mods_locked, group);
419 static const struct wl_keyboard_grab_interface binding_grab = {
425 install_binding_grab(struct wl_seat *seat,
426 uint32_t time, uint32_t key)
428 struct binding_keyboard_grab *grab;
430 grab = malloc(sizeof *grab);
432 grab->grab.interface = &binding_grab;
433 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
437 weston_compositor_run_key_binding(struct weston_compositor *compositor,
438 struct weston_seat *seat,
439 uint32_t time, uint32_t key,
440 enum wl_keyboard_key_state state)
442 struct weston_binding *b;
444 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
447 wl_list_for_each(b, &compositor->key_binding_list, link) {
448 if (b->key == key && b->modifier == seat->modifier_state) {
449 weston_key_binding_handler_t handler = b->handler;
450 handler(&seat->seat, time, key, b->data);
452 /* If this was a key binding and it didn't
453 * install a keyboard grab, install one now to
454 * swallow the key release. */
455 if (seat->seat.keyboard->grab ==
456 &seat->seat.keyboard->default_grab)
457 install_binding_grab(&seat->seat, time, key);
463 weston_compositor_run_button_binding(struct weston_compositor *compositor,
464 struct weston_seat *seat,
465 uint32_t time, uint32_t button,
466 enum wl_pointer_button_state state)
468 struct weston_binding *b;
470 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
473 wl_list_for_each(b, &compositor->button_binding_list, link) {
474 if (b->button == button && b->modifier == seat->modifier_state) {
475 weston_button_binding_handler_t handler = b->handler;
476 handler(&seat->seat, time, button, b->data);
482 weston_compositor_run_axis_binding(struct weston_compositor *compositor,
483 struct weston_seat *seat,
484 uint32_t time, uint32_t axis,
487 struct weston_binding *b;
489 wl_list_for_each(b, &compositor->axis_binding_list, link) {
490 if (b->axis == axis && b->modifier == seat->modifier_state) {
491 weston_axis_binding_handler_t handler = b->handler;
492 handler(&seat->seat, time, axis, value, b->data);
498 weston_environment_get_fd(const char *env)
506 fd = strtol(e, &end, 0);
510 flags = fcntl(fd, F_GETFD);
514 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);