blob: fa360c24fb433ed83dfb2e1178ce1588ed3a1236 [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
Rob Bradfordc4f33382012-08-03 17:02:04 +010049 /* Avoid entering into an infinite loop */
50 if (msec - spring->timestamp > UINT32_MAX / 2) {
51 weston_log("timestamps going backwards (from %u to %u)\n",
52 spring->timestamp, msec);
53 spring->current = spring->previous = spring->target;
54 return;
55 }
56
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050057 step = 0.01;
58 while (4 < msec - spring->timestamp) {
59 current = spring->current;
60 v = current - spring->previous;
61 force = spring->k * (spring->target - current) / 10.0 +
62 (spring->previous - current) - v * spring->friction;
63
64 spring->current =
65 current + (current - spring->previous) +
66 force * step * step;
67 spring->previous = current;
68
69#if 0
70 if (spring->current >= 1.0) {
71#ifdef TWEENER_BOUNCE
72 spring->current = 2.0 - spring->current;
73 spring->previous = 2.0 - spring->previous;
74#else
75 spring->current = 1.0;
76 spring->previous = 1.0;
77#endif
78 }
79
80 if (spring->current <= 0.0) {
81 spring->current = 0.0;
82 spring->previous = 0.0;
83 }
84#endif
85 spring->timestamp += 4;
86 }
87}
88
89WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090weston_spring_done(struct weston_spring *spring)
Kristian Høgsberg4bfb82a2011-12-04 15:47:16 -050091{
92 return fabs(spring->previous - spring->target) < 0.0002 &&
93 fabs(spring->current - spring->target) < 0.0002;
94}
95
Kristian Høgsberg414bd422012-06-21 22:07:30 -040096typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation);
97
98struct weston_surface_animation {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050099 struct weston_surface *surface;
100 struct weston_animation animation;
101 struct weston_spring spring;
102 struct weston_transform transform;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500103 struct wl_listener listener;
Kristian Høgsbergef458242011-12-15 11:24:25 -0500104 GLfloat start, stop;
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400105 weston_surface_animation_frame_func_t frame;
106 weston_surface_animation_done_func_t done;
Juan Zhao2abd07b2012-04-25 19:09:51 +0800107 void *data;
108};
109
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500110static void
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400111weston_surface_animation_destroy(struct weston_surface_animation *animation)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500112{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400113 wl_list_remove(&animation->animation.link);
114 wl_list_remove(&animation->listener.link);
115 wl_list_remove(&animation->transform.link);
116 animation->surface->geometry.dirty = 1;
117 if (animation->done)
118 animation->done(animation, animation->data);
119 free(animation);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500120}
121
122static void
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400123handle_animation_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500124{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400125 struct weston_surface_animation *animation =
126 container_of(listener,
127 struct weston_surface_animation, listener);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500128
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400129 weston_surface_animation_destroy(animation);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500130}
131
132static void
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400133weston_surface_animation_frame(struct weston_animation *base,
134 struct weston_output *output, uint32_t msecs)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500135{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400136 struct weston_surface_animation *animation =
137 container_of(base,
138 struct weston_surface_animation, animation);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500139
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400140 if (base->frame_counter <= 1)
141 animation->spring.timestamp = msecs;
Scott Moreaue2949db2012-06-11 13:09:23 -0600142
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400143 weston_spring_update(&animation->spring, msecs);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500144
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400145 if (weston_spring_done(&animation->spring)) {
146 weston_surface_animation_destroy(animation);
Pekka Paalanen2da6d5f2012-01-03 13:27:41 +0200147 return;
148 }
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500149
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400150 if (animation->frame)
151 animation->frame(animation);
152
153 animation->surface->geometry.dirty = 1;
154 weston_compositor_schedule_repaint(animation->surface->compositor);
155}
156
157static struct weston_surface_animation *
158weston_surface_animation_run(struct weston_surface *surface,
159 GLfloat start, GLfloat stop,
160 weston_surface_animation_frame_func_t frame,
161 weston_surface_animation_done_func_t done,
162 void *data)
163{
164 struct weston_surface_animation *animation;
165
166 animation = malloc(sizeof *animation);
167 if (!animation)
168 return NULL;
169
170 animation->surface = surface;
171 animation->frame = frame;
172 animation->done = done;
173 animation->data = data;
174 animation->start = start;
175 animation->stop = stop;
176 weston_matrix_init(&animation->transform.matrix);
177 wl_list_insert(&surface->geometry.transformation_list,
178 &animation->transform.link);
179 weston_spring_init(&animation->spring, 200.0, 0.0, 1.0);
180 animation->spring.friction = 700;
181 animation->animation.frame_counter = 0;
182 animation->animation.frame = weston_surface_animation_frame;
183 weston_surface_animation_frame(&animation->animation, NULL, 0);
184
185 animation->listener.notify = handle_animation_surface_destroy;
186 wl_signal_add(&surface->surface.resource.destroy_signal,
187 &animation->listener);
188
189 wl_list_insert(&surface->output->animation_list,
190 &animation->animation.link);
191
192 return animation;
193}
194
195static void
196zoom_frame(struct weston_surface_animation *animation)
197{
198 struct weston_surface *es = animation->surface;
199 GLfloat scale;
200
201 scale = animation->start +
202 (animation->stop - animation->start) *
203 animation->spring.current;
204 weston_matrix_init(&animation->transform.matrix);
205 weston_matrix_translate(&animation->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200206 -0.5f * es->geometry.width,
207 -0.5f * es->geometry.height, 0);
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400208 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
209 weston_matrix_translate(&animation->transform.matrix,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200210 0.5f * es->geometry.width,
211 0.5f * es->geometry.height, 0);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500212
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400213 es->alpha = animation->spring.current;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400214 if (es->alpha > 1.0)
215 es->alpha = 1.0;
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500216}
217
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400218WL_EXPORT struct weston_surface_animation *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500219weston_zoom_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400220 weston_surface_animation_done_func_t done, void *data)
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500221{
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400222 return weston_surface_animation_run(surface, start, stop,
223 zoom_frame, done, data);
224}
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500225
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400226static void
227fade_frame(struct weston_surface_animation *animation)
228{
229 if (animation->spring.current > 1)
230 animation->surface->alpha = 1;
231 else if (animation->spring.current < 0 )
232 animation->surface->alpha = 0;
233 else
234 animation->surface->alpha = animation->spring.current;
235}
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500236
Kristian Høgsberg414bd422012-06-21 22:07:30 -0400237WL_EXPORT struct weston_surface_animation *
238weston_fade_run(struct weston_surface *surface,
239 weston_surface_animation_done_func_t done, void *data)
240{
241 return weston_surface_animation_run(surface, 0, 0,
242 fade_frame, done, data);
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500243}
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500244
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -0400245static void
246slide_frame(struct weston_surface_animation *animation)
247{
248 GLfloat scale;
249
250 scale = animation->start +
251 (animation->stop - animation->start) *
252 animation->spring.current;
253 weston_matrix_init(&animation->transform.matrix);
254 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
255}
256
257WL_EXPORT struct weston_surface_animation *
258weston_slide_run(struct weston_surface *surface, GLfloat start, GLfloat stop,
259 weston_surface_animation_done_func_t done, void *data)
260{
261 struct weston_surface_animation *animation;
262
263 animation = weston_surface_animation_run(surface, start, stop,
264 slide_frame, done, data);
265 animation->spring.friction = 900;
266 animation->spring.k = 300;
267
268 return animation;
269}
270
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500271struct weston_binding {
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500272 uint32_t key;
273 uint32_t button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600274 uint32_t axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500275 uint32_t modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +0100276 void *handler;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500277 void *data;
278 struct wl_list link;
279};
280
Daniel Stone325fc2d2012-05-30 16:31:58 +0100281static struct weston_binding *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500282weston_compositor_add_binding(struct weston_compositor *compositor,
Daniel Stone325fc2d2012-05-30 16:31:58 +0100283 uint32_t key, uint32_t button, uint32_t axis,
284 uint32_t modifier, void *handler, void *data)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500285{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500286 struct weston_binding *binding;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500287
288 binding = malloc(sizeof *binding);
289 if (binding == NULL)
290 return NULL;
291
292 binding->key = key;
293 binding->button = button;
Scott Moreau6a3633d2012-03-20 08:47:59 -0600294 binding->axis = axis;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500295 binding->modifier = modifier;
296 binding->handler = handler;
297 binding->data = data;
Daniel Stone325fc2d2012-05-30 16:31:58 +0100298
299 return binding;
300}
301
302WL_EXPORT struct weston_binding *
303weston_compositor_add_key_binding(struct weston_compositor *compositor,
304 uint32_t key, uint32_t modifier,
305 weston_key_binding_handler_t handler,
306 void *data)
307{
308 struct weston_binding *binding;
309
310 binding = weston_compositor_add_binding(compositor, key, 0, 0,
311 modifier, handler, data);
312 if (binding == NULL)
313 return NULL;
314
315 wl_list_insert(compositor->key_binding_list.prev, &binding->link);
316
317 return binding;
318}
319
320WL_EXPORT struct weston_binding *
321weston_compositor_add_button_binding(struct weston_compositor *compositor,
322 uint32_t button, uint32_t modifier,
323 weston_button_binding_handler_t handler,
324 void *data)
325{
326 struct weston_binding *binding;
327
328 binding = weston_compositor_add_binding(compositor, 0, button, 0,
329 modifier, handler, data);
330 if (binding == NULL)
331 return NULL;
332
333 wl_list_insert(compositor->button_binding_list.prev, &binding->link);
334
335 return binding;
336}
337
338WL_EXPORT struct weston_binding *
339weston_compositor_add_axis_binding(struct weston_compositor *compositor,
340 uint32_t axis, uint32_t modifier,
341 weston_axis_binding_handler_t handler,
342 void *data)
343{
344 struct weston_binding *binding;
345
346 binding = weston_compositor_add_binding(compositor, 0, 0, axis,
347 modifier, handler, data);
348 if (binding == NULL)
349 return NULL;
350
351 wl_list_insert(compositor->axis_binding_list.prev, &binding->link);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500352
353 return binding;
354}
355
356WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500357weston_binding_destroy(struct weston_binding *binding)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500358{
359 wl_list_remove(&binding->link);
360 free(binding);
361}
362
363WL_EXPORT void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500364weston_binding_list_destroy_all(struct wl_list *list)
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200365{
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500366 struct weston_binding *binding, *tmp;
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200367
368 wl_list_for_each_safe(binding, tmp, list, link)
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500369 weston_binding_destroy(binding);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200370}
371
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500372struct binding_keyboard_grab {
373 uint32_t key;
374 struct wl_keyboard_grab grab;
375};
376
377static void
378binding_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100379 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500380{
381 struct binding_keyboard_grab *b =
382 container_of(grab, struct binding_keyboard_grab, grab);
383 struct wl_resource *resource;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400384 struct wl_display *display;
Daniel Stonec9785ea2012-05-30 16:31:52 +0100385 enum wl_keyboard_key_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400386 uint32_t serial;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500387
Daniel Stone37816df2012-05-16 18:45:18 +0100388 resource = grab->keyboard->focus_resource;
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500389 if (key == b->key) {
Daniel Stonec9785ea2012-05-30 16:31:52 +0100390 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
Daniel Stone37816df2012-05-16 18:45:18 +0100391 wl_keyboard_end_grab(grab->keyboard);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500392 free(b);
393 }
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400394 } else if (resource) {
395 display = wl_client_get_display(resource->client);
396 serial = wl_display_next_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +0100397 wl_keyboard_send_key(resource, serial, time, key, state);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400398 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500399}
400
Daniel Stone351eb612012-05-31 15:27:47 -0400401static void
402binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
403 uint32_t mods_depressed, uint32_t mods_latched,
404 uint32_t mods_locked, uint32_t group)
405{
406 struct wl_resource *resource;
407
408 resource = grab->keyboard->focus_resource;
409 if (!resource)
410 return;
411
412 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
413 mods_latched, mods_locked, group);
414}
415
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500416static const struct wl_keyboard_grab_interface binding_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -0400417 binding_key,
418 binding_modifiers,
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500419};
420
421static void
Daniel Stone37816df2012-05-16 18:45:18 +0100422install_binding_grab(struct wl_seat *seat,
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500423 uint32_t time, uint32_t key)
424{
425 struct binding_keyboard_grab *grab;
426
427 grab = malloc(sizeof *grab);
428 grab->key = key;
429 grab->grab.interface = &binding_grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100430 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500431}
432
Pekka Paalanen4738f3b2012-01-02 15:47:07 +0200433WL_EXPORT void
Daniel Stone325fc2d2012-05-30 16:31:58 +0100434weston_compositor_run_key_binding(struct weston_compositor *compositor,
435 struct weston_seat *seat,
436 uint32_t time, uint32_t key,
437 enum wl_keyboard_key_state state)
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500438{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500439 struct weston_binding *b;
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500440
Daniel Stone325fc2d2012-05-30 16:31:58 +0100441 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
442 return;
443
444 wl_list_for_each(b, &compositor->key_binding_list, link) {
445 if (b->key == key && b->modifier == seat->modifier_state) {
446 weston_key_binding_handler_t handler = b->handler;
447 handler(&seat->seat, time, key, b->data);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -0500448
449 /* If this was a key binding and it didn't
450 * install a keyboard grab, install one now to
451 * swallow the key release. */
Daniel Stone325fc2d2012-05-30 16:31:58 +0100452 if (seat->seat.keyboard->grab ==
Daniel Stone37816df2012-05-16 18:45:18 +0100453 &seat->seat.keyboard->default_grab)
454 install_binding_grab(&seat->seat, time, key);
Kristian Høgsbergf47d8fe2011-12-19 15:16:06 -0500455 }
456 }
457}
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100458
Daniel Stone325fc2d2012-05-30 16:31:58 +0100459WL_EXPORT void
460weston_compositor_run_button_binding(struct weston_compositor *compositor,
461 struct weston_seat *seat,
462 uint32_t time, uint32_t button,
463 enum wl_pointer_button_state state)
464{
465 struct weston_binding *b;
466
467 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
468 return;
469
470 wl_list_for_each(b, &compositor->button_binding_list, link) {
471 if (b->button == button && b->modifier == seat->modifier_state) {
472 weston_button_binding_handler_t handler = b->handler;
473 handler(&seat->seat, time, button, b->data);
474 }
475 }
476}
477
478WL_EXPORT void
479weston_compositor_run_axis_binding(struct weston_compositor *compositor,
480 struct weston_seat *seat,
481 uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +0100482 wl_fixed_t value)
Daniel Stone325fc2d2012-05-30 16:31:58 +0100483{
484 struct weston_binding *b;
485
486 wl_list_for_each(b, &compositor->axis_binding_list, link) {
487 if (b->axis == axis && b->modifier == seat->modifier_state) {
488 weston_axis_binding_handler_t handler = b->handler;
489 handler(&seat->seat, time, axis, value, b->data);
490 }
491 }
492}
493
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100494WL_EXPORT int
495weston_environment_get_fd(const char *env)
496{
497 char *e, *end;
498 int fd, flags;
499
500 e = getenv(env);
501 if (!e)
502 return -1;
503 fd = strtol(e, &end, 0);
504 if (*end != '\0')
505 return -1;
506
507 flags = fcntl(fd, F_GETFD);
508 if (flags == -1)
509 return -1;
510
511 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
512 unsetenv(env);
513
514 return fd;
515}