1 /**************************************************************************
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
32 #define WL_HIDE_DEPRECATED
43 #include "tbm_bufmgr_int.h"
45 #include "wayland-tbm-drm-auth-client-protocol.h"
47 struct wayland_tbm_drm_auth_client {
48 struct wl_display *display;
49 struct wl_tbm_drm_auth *wl_tbm_drm_auth;
52 uint32_t capabilities;
55 static int tbm_drm_fd = -1;
59 handle_tbm_drm_authentication_info(void *data, struct wl_tbm_drm_auth *wl_tbm_drm_auth, const char *device_name, uint32_t capabilities, int32_t auth_fd)
61 struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
63 /* client authentication infomation */
64 tbm_drm_client->auth_fd = auth_fd;
65 tbm_drm_client->capabilities = capabilities;
67 tbm_drm_client->device = strdup(device_name);
70 static const struct wl_tbm_drm_auth_listener wl_tbm_drm_auth_client_listener = {
71 handle_tbm_drm_authentication_info
75 _wayland_tbm_drm_auth_client_registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
77 struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
79 if (!strcmp(interface, "wl_tbm_drm_auth")) {
80 tbm_drm_client->wl_tbm_drm_auth = wl_registry_bind(registry, name, &wl_tbm_drm_auth_interface, version);
81 TBM_RETURN_IF_FAIL(tbm_drm_client->wl_tbm_drm_auth != NULL);
83 wl_tbm_drm_auth_add_listener(tbm_drm_client->wl_tbm_drm_auth, &wl_tbm_drm_auth_client_listener, tbm_drm_client);
87 static const struct wl_registry_listener registry_listener = {
88 _wayland_tbm_drm_auth_client_registry_handle_global,
93 tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities)
95 struct wl_display *display;
96 struct wl_registry *wl_registry;
97 struct wayland_tbm_drm_auth_client *tbm_drm_client;
99 tbm_drm_client = calloc(1, sizeof(struct wayland_tbm_drm_auth_client));
100 TBM_RETURN_VAL_IF_FAIL(tbm_drm_client != NULL, 0);
102 tbm_drm_client->auth_fd = -1;
104 display = wl_display_connect("tbm-drm-auth");
106 TBM_LOG_E("Failed to connect display\n");
107 free(tbm_drm_client);
112 tbm_drm_client->display = display;
114 wl_registry = wl_display_get_registry(display);
116 TBM_LOG_E("Failed to get registry\n");
117 wl_display_disconnect(display);
118 free(tbm_drm_client);
123 wl_registry_add_listener(wl_registry, ®istry_listener, tbm_drm_client);
124 if (wl_display_roundtrip(display) < 0) { //For Gloabl registry
125 TBM_LOG_E("Failed to wl_display_roundtrip for global registry\n");
126 wl_registry_destroy(wl_registry);
127 wl_display_disconnect(display);
128 free(tbm_drm_client);
132 if (!tbm_drm_client->wl_tbm_drm_auth) {
133 TBM_LOG_E("Failed to get wl_tbm_drm_auth interface\n");
134 wl_registry_destroy(wl_registry);
135 wl_display_disconnect(display);
136 free(tbm_drm_client);
141 wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
142 if (wl_display_roundtrip(display) < 0) {
143 TBM_LOG_E("Failed to wl_display_roundtrip get auth info\n");
144 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
145 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
146 wl_registry_destroy(wl_registry);
147 wl_display_disconnect(display);
148 free(tbm_drm_client);
152 if (tbm_drm_client->auth_fd < 0) {
153 TBM_LOG_E("Failed to get auth info\n");
154 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
155 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
156 wl_registry_destroy(wl_registry);
157 wl_display_disconnect(display);
158 free(tbm_drm_client);
164 *auth_fd = tbm_drm_client->auth_fd;
166 close(tbm_drm_client->auth_fd);
169 *capabilities = tbm_drm_client->capabilities;
172 if (tbm_drm_client->device)
173 *device = strdup(tbm_drm_client->device);
178 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
179 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
181 if (tbm_drm_client->device)
182 free(tbm_drm_client->device);
184 free(tbm_drm_client);
186 wl_registry_destroy(wl_registry);
187 wl_display_disconnect(display);
194 tbm_drm_helper_set_fd(int fd)
196 int fd_max = tbm_bufmgr_get_fd_limit();
198 if (tbm_drm_fd == fd)
201 if (fd < 0 || fd > fd_max) {
202 TBM_LOG_E("%d out of fd range\n", fd);
206 if (tbm_drm_fd != -1)
207 TBM_LOG_W("already has TBM_DRM_FD: %d\n", tbm_drm_fd);
211 TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
215 tbm_drm_helper_unset_fd(void)
218 TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
222 tbm_drm_helper_get_fd(void)
226 if (tbm_drm_fd == -1) {
227 TBM_LOG_E("no drm fd");
231 TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
233 flags = fcntl(tbm_drm_fd, F_GETFD);
235 TBM_LOG_E("fcntl failed: %m");
239 new_fd = dup(tbm_drm_fd);
241 TBM_LOG_E("dup failed: %m");
245 if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
246 TBM_LOG_E("failed to set fd\n");
251 TBM_LOG_I("Return TBM_FD: %d\n", new_fd);