tbm_module: make tbm_module_bufmgr_bind_native_display
[platform/core/uifw/libtbm.git] / src / tbm_drm_helper_client.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
6
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>
9
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:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
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.
29
30 **************************************************************************/
31
32 #define WL_HIDE_DEPRECATED
33
34 #include "config.h"
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stddef.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42
43 #include "tbm_bufmgr_int.h"
44
45 #include "wayland-tbm-drm-auth-client-protocol.h"
46
47 struct wayland_tbm_drm_auth_client {
48         struct wl_display *display;
49         struct wl_tbm_drm_auth *wl_tbm_drm_auth;
50         int auth_fd;
51         char *device;
52         uint32_t capabilities;
53 };
54
55 static int tbm_drm_fd = -1;
56
57 /* LCOV_EXCL_START */
58 static void
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)
60 {
61         struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
62
63         /* client authentication infomation */
64         tbm_drm_client->auth_fd = auth_fd;
65         tbm_drm_client->capabilities = capabilities;
66         if (device_name)
67             tbm_drm_client->device = strdup(device_name);
68 }
69
70 static const struct wl_tbm_drm_auth_listener wl_tbm_drm_auth_client_listener = {
71         handle_tbm_drm_authentication_info
72 };
73
74 static void
75 _wayland_tbm_drm_auth_client_registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
76 {
77         struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
78
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);
82
83                 wl_tbm_drm_auth_add_listener(tbm_drm_client->wl_tbm_drm_auth, &wl_tbm_drm_auth_client_listener, tbm_drm_client);
84         }
85 }
86
87 static void
88 _wayland_tbm_drm_auth_client_registry_remove_global(void *data, struct wl_registry *registry, uint32_t name)
89 {
90 }
91
92 static const struct wl_registry_listener registry_listener = {
93         _wayland_tbm_drm_auth_client_registry_handle_global,
94         _wayland_tbm_drm_auth_client_registry_remove_global
95 };
96
97 int
98 tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities)
99 {
100         struct wl_display *display;
101         struct wl_registry *wl_registry;
102         struct wayland_tbm_drm_auth_client *tbm_drm_client;
103
104         tbm_drm_client = calloc(1, sizeof(struct wayland_tbm_drm_auth_client));
105         TBM_RETURN_VAL_IF_FAIL(tbm_drm_client != NULL, 0);
106
107         tbm_drm_client->auth_fd = -1;
108
109         display = wl_display_connect("tbm-drm-auth");
110         if (!display) {
111                 TBM_ERR("Failed to connect display\n");
112                 free(tbm_drm_client);
113
114                 return 0;
115         }
116
117         tbm_drm_client->display = display;
118
119         wl_registry = wl_display_get_registry(display);
120         if (!wl_registry) {
121                 TBM_ERR("Failed to get registry\n");
122                 wl_display_disconnect(display);
123                 free(tbm_drm_client);
124
125                 return 0;
126         }
127
128         wl_registry_add_listener(wl_registry, &registry_listener, tbm_drm_client);
129         if (wl_display_roundtrip(display) < 0) { //For Gloabl registry
130                 TBM_ERR("Failed to wl_display_roundtrip for global registry\n");
131                 wl_registry_destroy(wl_registry);
132                 wl_display_disconnect(display);
133                 free(tbm_drm_client);
134                 return 0;
135         }
136
137         if (!tbm_drm_client->wl_tbm_drm_auth) {
138                 TBM_ERR("Failed to get wl_tbm_drm_auth interface\n");
139                 wl_registry_destroy(wl_registry);
140                 wl_display_disconnect(display);
141                 free(tbm_drm_client);
142
143                 return 0;
144         }
145
146         wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
147         if (wl_display_roundtrip(display) < 0) {
148                 TBM_ERR("Failed to wl_display_roundtrip get auth info\n");
149                 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
150                 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
151                 wl_registry_destroy(wl_registry);
152                 wl_display_disconnect(display);
153                 free(tbm_drm_client);
154                 return 0;
155         }
156
157         if (tbm_drm_client->auth_fd < 0) {
158                 TBM_ERR("Failed to get auth info\n");
159                 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
160                 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
161                 wl_registry_destroy(wl_registry);
162                 wl_display_disconnect(display);
163                 free(tbm_drm_client);
164
165                 return 0;
166         }
167
168         if (auth_fd)
169                 *auth_fd = tbm_drm_client->auth_fd;
170         else
171                 close(tbm_drm_client->auth_fd);
172
173         if (capabilities)
174                 *capabilities = tbm_drm_client->capabilities;
175
176         if (device) {
177                 if (tbm_drm_client->device)
178                         *device = strdup(tbm_drm_client->device);
179                 else
180                         *device = NULL;
181         }
182
183         wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
184         wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
185
186         if (tbm_drm_client->device)
187                 free(tbm_drm_client->device);
188
189         free(tbm_drm_client);
190
191         wl_registry_destroy(wl_registry);
192         wl_display_disconnect(display);
193
194         return 1;
195 }
196
197
198 void
199 tbm_drm_helper_set_fd(int fd)
200 {
201         int fd_max = tbm_bufmgr_get_fd_limit();
202
203         if (tbm_drm_fd == fd)
204                 return;
205
206         if (fd < 0 || fd > fd_max) {
207                 TBM_ERR("%d out of fd range\n", fd);
208                 return;
209         }
210
211         if (tbm_drm_fd != -1)
212                 TBM_WRN("already has TBM_DRM_FD: %d\n", tbm_drm_fd);
213
214         tbm_drm_fd = fd;
215
216         TBM_INFO("TBM_DRM_FD: %d\n", tbm_drm_fd);
217 }
218
219 void
220 tbm_drm_helper_unset_fd(void)
221 {
222         tbm_drm_fd = -1;
223         TBM_INFO("TBM_DRM_FD: %d\n", tbm_drm_fd);
224 }
225
226 int
227 tbm_drm_helper_get_fd(void)
228 {
229         int new_fd, flags;
230
231         if (tbm_drm_fd == -1) {
232                 TBM_ERR("no drm fd");
233                 return -1;
234         }
235
236         TBM_INFO("TBM_DRM_FD: %d\n", tbm_drm_fd);
237
238         flags = fcntl(tbm_drm_fd, F_GETFD);
239         if (flags == -1) {
240                 TBM_ERR("fcntl failed: %m");
241                 return -1;
242         }
243
244         new_fd = dup(tbm_drm_fd);
245         if (new_fd < 0) {
246                 TBM_ERR("dup failed: %m");
247                 return -1;
248         }
249
250         if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
251                 TBM_ERR("failed to set fd\n");
252                 close(new_fd);
253                 return -1;
254         }
255
256         TBM_INFO("Return TBM_FD: %d\n", new_fd);
257
258         return new_fd;
259 }
260
261 /* LCOV_EXCL_STOP */