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 **************************************************************************/
34 #include "tbm_bufmgr_int.h"
39 #include "wayland-client.h"
46 #ifndef WL_DRM_ERROR_ENUM
47 #define WL_DRM_ERROR_ENUM
49 WL_DRM_ERROR_AUTHENTICATE_FAIL = 0,
50 WL_DRM_ERROR_INVALID_FORMAT = 1,
51 WL_DRM_ERROR_INVALID_NAME = 2,
53 #endif /* WL_DRM_ERROR_ENUM */
56 #ifndef WL_DRM_CAPABILITY_ENUM
57 #define WL_DRM_CAPABILITY_ENUM
59 * wl_drm_capability - wl_drm capability bitmask
60 * @WL_DRM_CAPABILITY_PRIME: wl_drm prime available
62 * Bitmask of capabilities.
64 enum wl_drm_capability {
65 WL_DRM_CAPABILITY_PRIME = 1,
67 #endif /* WL_DRM_CAPABILITY_ENUM */
69 struct wl_drm_listener {
74 void (*device)(void *data,
75 struct wl_drm *wl_drm,
81 void (*format)(void *data,
82 struct wl_drm *wl_drm,
85 * authenticated - (none)
87 void (*authenticated)(void *data,
88 struct wl_drm *wl_drm);
90 * capabilities - (none)
93 void (*capabilities)(void *data,
94 struct wl_drm *wl_drm,
99 wl_drm_add_listener(struct wl_drm *wl_drm,
100 const struct wl_drm_listener *listener, void *data)
102 return wl_proxy_add_listener((struct wl_proxy *) wl_drm,
103 (void (**)(void)) listener, data);
106 #define WL_DRM_AUTHENTICATE 0
109 wl_drm_destroy(struct wl_drm *wl_drm)
111 wl_proxy_destroy((struct wl_proxy *) wl_drm);
115 wl_drm_authenticate(struct wl_drm *wl_drm, uint32_t id)
117 wl_proxy_marshal((struct wl_proxy *) wl_drm,
118 WL_DRM_AUTHENTICATE, id);
121 static const struct wl_interface *types[] = {
123 &wl_buffer_interface,
129 &wl_buffer_interface,
140 &wl_buffer_interface,
153 static const struct wl_message wl_drm_requests[] = {
154 { "authenticate", "u", types + 0 },
155 { "create_buffer", "nuiiuu", types + 1 },
156 { "create_planar_buffer", "nuiiuiiiiii", types + 7 },
157 { "create_prime_buffer", "2nhiiuiiiiii", types + 18 },
160 static const struct wl_message wl_drm_events[] = {
161 { "device", "s", types + 0 },
162 { "format", "u", types + 0 },
163 { "authenticated", "", types + 0 },
164 { "capabilities", "u", types + 0 },
167 static const struct wl_interface wl_drm_interface = {
177 struct wl_event_queue *wl_queue;
179 struct wl_drm* wl_drm;
184 static void wl_client_drm_handle_device(void *data, struct wl_drm *drm, const char *device)
186 struct wl_drm_info *drm_info = (struct wl_drm_info *)data;
189 printf("device[%s]\n", device);
190 drm_info->fd = open(device, O_RDWR | O_CLOEXEC);
191 if (drm_info->fd < 0) {
192 printf("Failed to open a device: %d (%s)\n", errno, device);
196 drmGetMagic(drm_info->fd, &magic);
197 printf("magic[%x]\n", magic);
198 wl_drm_authenticate(drm_info->wl_drm, magic);
201 static void wl_client_drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
206 static void wl_client_drm_handle_authenticated(void *data, struct wl_drm *drm)
208 struct wl_drm_info *drm_info = (struct wl_drm_info *)data;
209 drm_info->authenticated = 1;
212 static void wl_client_drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
219 static void wl_client_registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
221 struct wl_drm_info *info = (struct wl_drm_info *)data;
222 static const struct wl_drm_listener wl_drm_client_listener = {
223 wl_client_drm_handle_device,
224 wl_client_drm_handle_format,
225 wl_client_drm_handle_authenticated,
226 wl_client_drm_handle_capabilities
229 printf("interface[%s]\n", interface);
230 if (!strcmp(interface, "wl_drm")) {
231 info->wl_drm = wl_registry_bind(registry, name, &wl_drm_interface, (version > 2) ? 2 : version);
233 wl_proxy_set_queue((struct wl_proxy *)info->wl_drm, info->wl_queue);
235 wl_drm_add_listener(info->wl_drm, &wl_drm_client_listener, data);
239 static int tbm_util_get_drm_fd(void *dpy, int *fd)
241 struct wl_display *disp = NULL;
242 struct wl_registry *wl_registry;
244 struct wl_drm_info info = {
252 static const struct wl_registry_listener registry_listener = {
253 wl_client_registry_handle_global,
262 disp = wl_display_connect(NULL);
264 printf("Failed to create a new display connection\n");
271 info.wl_queue = wl_display_create_queue(dpy);
272 if (!info.wl_queue) {
273 printf("Failed to create a WL Queue\n");
275 wl_display_disconnect(disp);
280 wl_registry = wl_display_get_registry(dpy);
282 printf("Failed to get registry\n");
284 wl_event_queue_destroy(info.wl_queue);
287 wl_display_disconnect(disp);
292 wl_proxy_set_queue((struct wl_proxy *)wl_registry, info.wl_queue);
294 wl_registry_add_listener(wl_registry, ®istry_listener, &info);
295 wl_display_roundtrip(dpy);
297 printf("Consuming Dispatch Queue begin\n");
298 while (ret != -1 && !info.authenticated) {
300 ret = wl_display_dispatch_queue(dpy, info.wl_queue);
302 ret = wl_display_dispatch(dpy);
304 printf("Dispatch Queue consumed: %d\n", ret);
306 printf("Consuming Dispatch Queue end\n");
309 wl_event_queue_destroy(info.wl_queue);
311 wl_registry_destroy(wl_registry);
312 wl_drm_destroy(info.wl_drm);
316 wl_display_disconnect(disp);
318 return *fd >= 0 ? 0 : -1;
322 tbm_bufmgr_get_drm_fd_wayland()
326 if(tbm_util_get_drm_fd(NULL, &fd))
328 printf("Failed to get drm_fd\n");