| 1 | /* |
| 2 | * Copyright © 2012 Collabora, Ltd. |
| 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 <stdint.h> |
| 24 | #include <cstring> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "server_wlegl_handle.h" |
| 28 | #include "wayland-android-server-protocol.h" |
| 29 | |
| 30 | static void |
| 31 | add_fd(struct wl_client *client, struct wl_resource *resource, int32_t fd) |
| 32 | { |
| 33 | server_wlegl_handle *handle = server_wlegl_handle_from(resource); |
| 34 | |
| 35 | if (handle->fds.size >= handle->num_fds * sizeof(int)) { |
| 36 | close(fd); |
| 37 | wl_resource_post_error(&handle->resource, |
| 38 | ANDROID_WLEGL_HANDLE_ERROR_TOO_MANY_FDS, |
| 39 | "too many file descriptors"); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | int *ptr = (int *)wl_array_add(&handle->fds, sizeof(int)); |
| 44 | *ptr = fd; |
| 45 | } |
| 46 | |
| 47 | static void |
| 48 | destroy(struct wl_client *client, struct wl_resource *resource) |
| 49 | { |
| 50 | wl_resource_destroy(resource); |
| 51 | } |
| 52 | |
| 53 | static const struct android_wlegl_handle_interface server_handle_impl = { |
| 54 | add_fd, |
| 55 | destroy |
| 56 | }; |
| 57 | |
| 58 | static void |
| 59 | server_wlegl_handle_dtor(struct wl_resource *resource) |
| 60 | { |
| 61 | server_wlegl_handle *handle = server_wlegl_handle_from(resource); |
| 62 | |
| 63 | int *fd = (int *)handle->fds.data; |
| 64 | int *end = (int *)((char *)handle->fds.data + handle->fds.size); |
| 65 | |
| 66 | for (; fd < end; ++fd) |
| 67 | close(*fd); |
| 68 | |
| 69 | wl_array_release(&handle->fds); |
| 70 | wl_array_release(&handle->ints); |
| 71 | |
| 72 | delete handle; |
| 73 | } |
| 74 | |
| 75 | server_wlegl_handle * |
| 76 | server_wlegl_handle_create(uint32_t id) |
| 77 | { |
| 78 | server_wlegl_handle *handle = new server_wlegl_handle; |
| 79 | |
| 80 | memset(handle, 0, sizeof(*handle)); |
| 81 | |
| 82 | handle->resource.object.id = id; |
| 83 | handle->resource.object.interface = &android_wlegl_handle_interface; |
| 84 | handle->resource.object.implementation = |
| 85 | (void (**)(void))&server_handle_impl; |
| 86 | |
| 87 | handle->resource.destroy = server_wlegl_handle_dtor; |
| 88 | handle->resource.data = handle; |
| 89 | |
| 90 | wl_array_init(&handle->ints); |
| 91 | wl_array_init(&handle->fds); |
| 92 | |
| 93 | return handle; |
| 94 | } |
| 95 | |
| 96 | buffer_handle_t |
| 97 | server_wlegl_handle_to_native(server_wlegl_handle *handle) |
| 98 | { |
| 99 | native_handle_t *native; |
| 100 | int numFds = handle->fds.size / sizeof(int); |
| 101 | int numInts = handle->ints.size / sizeof(int32_t); |
| 102 | |
| 103 | if (numFds != handle->num_fds) |
| 104 | return NULL; |
| 105 | |
| 106 | native = native_handle_create(numFds, numInts); |
| 107 | |
| 108 | memcpy(&native->data[0], handle->fds.data, handle->fds.size); |
| 109 | memcpy(&native->data[numFds], handle->ints.data, handle->ints.size); |
| 110 | /* ownership of fds passed to native_handle_t */ |
| 111 | handle->fds.size = 0; |
| 112 | |
| 113 | return (buffer_handle_t) native; |
| 114 | } |