blob: c7511cff73cc20dfd382f04a1ea5acdb06bf54ea [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
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400112handle_zoom_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500113{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_zoom *zoom =
115 container_of(listener, struct weston_zoom, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500116
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500117 weston_zoom_destroy(zoom);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500118}
119
120static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500121weston_zoom_frame(struct weston_animation *animation,
122 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500123{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124 struct weston_zoom *zoom =
125 container_of(animation, struct weston_zoom, animation);
126 struct weston_surface *es = zoom->surface;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500127 GLfloat scale;
128
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 weston_spring_update(&zoom->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500130
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500131 if (weston_spring_done(&zoom->spring)) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500132 weston_zoom_destroy(zoom);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200133 return;
134 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500135
Kristian Høgsbergef458242011-12-15 11:24:25 -0500136 scale = zoom->start +
137 (zoom->stop - zoom->start) * zoom->spring.current;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138 weston_matrix_init(&zoom->transform.matrix);
139 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200140 -0.5f * es->geometry.width,
141 -0.5f * es->geometry.height, 0);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500142 weston_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
143 weston_matrix_translate(&zoom->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200144 0.5f * es->geometry.width,
145 0.5f * es->geometry.height, 0);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500146
Kristian Høgsbergef458242011-12-15 11:24:25 -0500147 es->alpha = zoom->spring.current * 255;
148 if (es->alpha > 255)
149 es->alpha = 255;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500150
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200151 zoom->surface->geometry.dirty = 1;
Kristian Høgsberg2ea09442012-02-28 23:12:52 -0500152 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500153}
154
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500155WL_EXPORT struct weston_zoom *
156weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
157 weston_zoom_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500158{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_zoom *zoom;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500160
161 zoom = malloc(sizeof *zoom);
162 if (!zoom)
163 return NULL;
164
165 zoom->surface = surface;
166 zoom->done = done;
167 zoom->data = data;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500168 zoom->start = start;
169 zoom->stop = stop;
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200170 wl_list_insert(&surface->geometry.transformation_list,
171 &zoom->transform.link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500172 weston_spring_init(&zoom->spring, 200.0, 0.0, 1.0);
Kristian Høgsbergef458242011-12-15 11:24:25 -0500173 zoom->spring.friction = 700;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500174 zoom->spring.timestamp = weston_compositor_get_time();
175 zoom->animation.frame = weston_zoom_frame;
176 weston_zoom_frame(&zoom->animation, NULL, zoom->spring.timestamp);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500177
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400178 zoom->listener.notify = handle_zoom_surface_destroy;
179 wl_signal_add(&surface->surface.resource.destroy_signal,
180 &zoom->listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500181
Pekka Paalanen73772042012-01-25 14:45:18 +0200182 wl_list_insert(&surface->compositor->animation_list,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500183 &zoom->animation.link);
184
185 return zoom;
186}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500187
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500189 uint32_t key;
190 uint32_t button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600191 uint32_t axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500192 uint32_t modifier;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193 weston_binding_handler_t handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500194 void *data;
195 struct wl_list link;
196};
197
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500198WL_EXPORT struct weston_binding *
199weston_compositor_add_binding(struct weston_compositor *compositor,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600200 uint32_t key, uint32_t button, uint32_t axis, uint32_t modifier,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500201 weston_binding_handler_t handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500202{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500203 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500204
205 binding = malloc(sizeof *binding);
206 if (binding == NULL)
207 return NULL;
208
209 binding->key = key;
210 binding->button = button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600211 binding->axis = axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500212 binding->modifier = modifier;
213 binding->handler = handler;
214 binding->data = data;
215 wl_list_insert(compositor->binding_list.prev, &binding->link);
216
217 return binding;
218}
219
220WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500222{
223 wl_list_remove(&binding->link);
224 free(binding);
225}
226
227WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500228weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200229{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500230 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200231
232 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500233 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200234}
235
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500236struct binding_keyboard_grab {
237 uint32_t key;
238 struct wl_keyboard_grab grab;
239};
240
241static void
242binding_key(struct wl_keyboard_grab *grab,
243 uint32_t time, uint32_t key, int32_t state)
244{
245 struct binding_keyboard_grab *b =
246 container_of(grab, struct binding_keyboard_grab, grab);
247 struct wl_resource *resource;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400248 struct wl_display *display;
249 uint32_t serial;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500250
251 resource = grab->input_device->keyboard_focus_resource;
252 if (key == b->key) {
253 if (!state) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400254 wl_input_device_end_keyboard_grab(grab->input_device);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500255 free(b);
256 }
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400257 } else if (resource) {
258 display = wl_client_get_display(resource->client);
259 serial = wl_display_next_serial(display);
260 wl_input_device_send_key(resource, serial, time, key, state);
261 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500262}
263
264static const struct wl_keyboard_grab_interface binding_grab = {
265 binding_key
266};
267
268static void
269install_binding_grab(struct wl_input_device *device,
270 uint32_t time, uint32_t key)
271{
272 struct binding_keyboard_grab *grab;
273
274 grab = malloc(sizeof *grab);
275 grab->key = key;
276 grab->grab.interface = &binding_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400277 wl_input_device_start_keyboard_grab(device, &grab->grab);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500278}
279
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200280WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500281weston_compositor_run_binding(struct weston_compositor *compositor,
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500282 struct weston_input_device *device,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600283 uint32_t time, uint32_t key,
284 uint32_t button, uint32_t axis, int32_t state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500285{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500286 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500287
288 wl_list_for_each(b, &compositor->binding_list, link) {
Scott Moreau6a3633d2012-03-20 08:47:59 -0600289 if (b->key == key && b->button == button && b->axis == axis &&
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500290 b->modifier == device->modifier_state && state) {
291 b->handler(&device->input_device,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600292 time, key, button, axis, state, b->data);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500293
294 /* If this was a key binding and it didn't
295 * install a keyboard grab, install one now to
296 * swallow the key release. */
297 if (b->key &&
298 device->input_device.keyboard_grab ==
299 &device->input_device.default_keyboard_grab)
300 install_binding_grab(&device->input_device,
301 time, key);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500302 }
303 }
304}
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100305
306WL_EXPORT int
307weston_environment_get_fd(const char *env)
308{
309 char *e, *end;
310 int fd, flags;
311
312 e = getenv(env);
313 if (!e)
314 return -1;
315 fd = strtol(e, &end, 0);
316 if (*end != '\0')
317 return -1;
318
319 flags = fcntl(fd, F_GETFD);
320 if (flags == -1)
321 return -1;
322
323 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
324 unsetenv(env);
325
326 return fd;
327}