blob: d21c275fd8a81289b29be632786c1842ae4810de [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
99static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500100weston_zoom_destroy(struct weston_zoom *zoom)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500101{
102 wl_list_remove(&zoom->animation.link);
103 wl_list_remove(&zoom->listener.link);
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200104 wl_list_remove(&zoom->transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200105 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500106 if (zoom->done)
107 zoom->done(zoom, zoom->data);
108 free(zoom);
109}
110
111static void
112handle_zoom_surface_destroy(struct wl_listener *listener,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400113 struct wl_resource *resource)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500114{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115 struct weston_zoom *zoom =
116 container_of(listener, struct weston_zoom, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500117
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500118 weston_zoom_destroy(zoom);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500119}
120
121static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122weston_zoom_frame(struct weston_animation *animation,
123 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500124{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500125 struct weston_zoom *zoom =
126 container_of(animation, struct weston_zoom, animation);
127 struct weston_surface *es = zoom->surface;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500128 GLfloat scale;
129
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 weston_spring_update(&zoom->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500131
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500132 if (weston_spring_done(&zoom->spring)) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133 weston_zoom_destroy(zoom);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200134 return;
135 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500136
Kristian Høgsbergef458242011-12-15 11:24:25 -0500137 scale = zoom->start +
138 (zoom->stop - zoom->start) * zoom->spring.current;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500139 weston_matrix_init(&zoom->transform.matrix);
140 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200141 -0.5f * es->geometry.width,
142 -0.5f * es->geometry.height, 0);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500143 weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
144 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200145 0.5f * es->geometry.width,
146 0.5f * es->geometry.height, 0);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500147
Kristian Høgsbergef458242011-12-15 11:24:25 -0500148 es->alpha = zoom->spring.current * 255;
149 if (es->alpha > 255)
150 es->alpha = 255;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500151
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200152 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg2ea09442012-02-28 23:12:52 -0500153 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500154}
155
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156WL_EXPORT struct weston_zoom *
157weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
158 weston_zoom_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500159{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160 struct weston_zoom *zoom;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500161
162 zoom = malloc(sizeof *zoom);
163 if (!zoom)
164 return NULL;
165
166 zoom->surface = surface;
167 zoom->done = done;
168 zoom->data = data;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500169 zoom->start = start;
170 zoom->stop = stop;
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200171 wl_list_insert(&surface->geometry.transformation_list,
172 &zoom->transform.link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500173 weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0);
Kristian Høgsbergef458242011-12-15 11:24:25 -0500174 zoom->spring.friction = 700;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500175 zoom->spring.timestamp = weston_compositor_get_time();
176 zoom->animation.frame = weston_zoom_frame;
177 weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500178
179 zoom->listener.func = handle_zoom_surface_destroy;
180 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
181 &zoom->listener.link);
182
Pekka Paalanen73772042012-01-25 14:45:18 +0200183 wl_list_insert(&surface->compositor->animation_list,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500184 &zoom->animation.link);
185
186 return zoom;
187}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500188
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500189struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500190 uint32_t key;
191 uint32_t button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600192 uint32_t axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500193 uint32_t modifier;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194 weston_binding_handler_t handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500195 void *data;
196 struct wl_list link;
197};
198
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199WL_EXPORT struct weston_binding *
200weston_compositor_add_binding(struct weston_compositor *compositor,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600201 uint32_t key, uint32_t button, uint32_t axis, uint32_t modifier,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500202 weston_binding_handler_t handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500203{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500204 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500205
206 binding = malloc(sizeof *binding);
207 if (binding == NULL)
208 return NULL;
209
210 binding->key = key;
211 binding->button = button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600212 binding->axis = axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500213 binding->modifier = modifier;
214 binding->handler = handler;
215 binding->data = data;
216 wl_list_insert(compositor->binding_list.prev, &binding->link);
217
218 return binding;
219}
220
221WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500222weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500223{
224 wl_list_remove(&binding->link);
225 free(binding);
226}
227
228WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500229weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200230{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500231 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200232
233 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500234 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200235}
236
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500237struct binding_keyboard_grab {
238 uint32_t key;
239 struct wl_keyboard_grab grab;
240};
241
242static void
243binding_key(struct wl_keyboard_grab *grab,
244 uint32_t time, uint32_t key, int32_t state)
245{
246 struct binding_keyboard_grab *b =
247 container_of(grab, struct binding_keyboard_grab, grab);
248 struct wl_resource *resource;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400249 struct wl_display *display;
250 uint32_t serial;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500251
252 resource = grab->input_device->keyboard_focus_resource;
253 if (key == b->key) {
254 if (!state) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400255 wl_input_device_end_keyboard_grab(grab->input_device);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500256 free(b);
257 }
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400258 } else if (resource) {
259 display = wl_client_get_display(resource->client);
260 serial = wl_display_next_serial(display);
261 wl_input_device_send_key(resource, serial, time, key, state);
262 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500263}
264
265static const struct wl_keyboard_grab_interface binding_grab = {
266 binding_key
267};
268
269static void
270install_binding_grab(struct wl_input_device *device,
271 uint32_t time, uint32_t key)
272{
273 struct binding_keyboard_grab *grab;
274
275 grab = malloc(sizeof *grab);
276 grab->key = key;
277 grab->grab.interface = &binding_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400278 wl_input_device_start_keyboard_grab(device, &grab->grab);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500279}
280
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200281WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500282weston_compositor_run_binding(struct weston_compositor *compositor,
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500283 struct weston_input_device *device,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600284 uint32_t time, uint32_t key,
285 uint32_t button, uint32_t axis, int32_t state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500287 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500288
289 wl_list_for_each(b, &compositor->binding_list, link) {
Scott Moreau6a3633d2012-03-20 08:47:59 -0600290 if (b->key == key && b->button == button && b->axis == axis &&
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500291 b->modifier == device->modifier_state && state) {
292 b->handler(&device->input_device,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600293 time, key, button, axis, state, b->data);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500294
295 /* If this was a key binding and it didn't
296 * install a keyboard grab, install one now to
297 * swallow the key release. */
298 if (b->key &&
299 device->input_device.keyboard_grab ==
300 &device->input_device.default_keyboard_grab)
301 install_binding_grab(&device->input_device,
302 time, key);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500303 }
304 }
305}
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100306
307WL_EXPORT int
308weston_environment_get_fd(const char *env)
309{
310 char *e, *end;
311 int fd, flags;
312
313 e = getenv(env);
314 if (!e)
315 return -1;
316 fd = strtol(e, &end, 0);
317 if (*end != '\0')
318 return -1;
319
320 flags = fcntl(fd, F_GETFD);
321 if (flags == -1)
322 return -1;
323
324 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
325 unsetenv(env);
326
327 return fd;
328}