1a0a541d3f4e606e64cc50746449af4840f44eff
[platform/core/uifw/libtbm.git] / src / tbm_wayland.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 #include "config.h"
33
34 #include "tbm_bufmgr_int.h"
35 #include <xf86drm.h>
36 #include <wayland-client.h>
37 #include <wayland-drm-client-protocol.h>
38
39 struct display {
40         struct wl_display *display;
41         struct wl_registry *registry;
42
43         struct wl_drm   *wl_drm;
44         uint32_t                drm_fd;
45         char*                   drm_device_name;
46 };
47
48 /* initialize drm device */
49 static void
50 _drm_handle_device( void *data, struct wl_drm *drm, const char *device )
51 {
52         struct display *d = data;
53
54         d->drm_device_name = strdup(device);
55         return;
56 }
57
58 /* handle format of drm device */
59 static void
60 _drm_handle_format( void *data, struct wl_drm *drm, uint32_t format )
61 {
62         return;
63 }
64
65 /* handle authentication of drm device */
66 static void
67 _drm_handle_authenticated( void *data, struct wl_drm *drm )
68 {
69         struct display *d = data;
70
71         printf("DRM authenticated: name:%s fd:%d\n", d->drm_device_name, d->drm_fd);
72         return;
73 }
74
75 static void
76 _drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
77 {
78         return;
79 }
80
81 static const struct wl_drm_listener drm_listener = {
82         _drm_handle_device,
83         _drm_handle_format,
84         _drm_handle_authenticated,
85         _drm_handle_capabilities
86 };
87
88
89 static void
90 registry_handle_global(void *data, struct wl_registry *registry,
91                        uint32_t id, const char *interface, uint32_t version)
92 {
93         struct display *d = data;
94
95         if (strcmp(interface, "wl_drm") == 0) {
96                 d->wl_drm =
97                         wl_registry_bind(registry, id, &wl_drm_interface, version);
98                 wl_drm_add_listener(d->wl_drm, &drm_listener, d);
99         }
100 }
101
102 static void
103 registry_handle_global_remove(void *data, struct wl_registry *registry,
104                               uint32_t name)
105 {
106 }
107
108 static const struct wl_registry_listener registry_listener = {
109         registry_handle_global,
110         registry_handle_global_remove
111 };
112
113 static struct display *
114 create_display(void)
115 {
116         struct display *display;
117     drm_magic_t magic;
118
119         display = calloc(sizeof(struct display), 1);
120         if (display == NULL) {
121         TBM_LOG ("[libtbm:%d] out of memory\n", getpid());
122         return NULL;
123         }
124         display->display = wl_display_connect(NULL);
125     if (display->display == NULL) {
126         TBM_LOG ("[libtbm:%d] fail to wl display\n", getpid());
127         return NULL;
128     }
129
130         display->registry = wl_display_get_registry(display->display);
131         wl_registry_add_listener(display->registry,
132                                  &registry_listener, display);
133         wl_display_roundtrip(display->display);
134
135     if (display->wl_drm == NULL) {
136         TBM_LOG ("[libtbm:%d] No wl_drm global\n", getpid());
137         return NULL;
138     }
139     wl_display_roundtrip(display->display);
140
141     if (!display->drm_device_name) {
142         TBM_LOG ("[libtbm:%d] No drm device name\n", getpid());
143         return NULL;
144     }
145
146     display->drm_fd = open( display->drm_device_name, O_RDWR | O_CLOEXEC );
147     if (display->drm_fd < 0) {
148         TBM_LOG ("[libtbm:%d] Cannot open drm device\n", getpid());
149         return NULL;
150     }
151
152     drmGetMagic(display->drm_fd, &magic);
153     wl_drm_authenticate(display->wl_drm, magic);
154     wl_display_roundtrip(display->display);
155
156         return display;
157 }
158
159 static void
160 destroy_display(struct display *display)
161 {
162         if (display->wl_drm)
163                 wl_drm_destroy(display->wl_drm);
164         wl_registry_destroy(display->registry);
165         wl_display_flush(display->display);
166         wl_display_disconnect(display->display);
167         if (display->drm_device_name)
168                 free (display->drm_device_name);
169         free(display);
170 }
171
172 int
173 tbm_bufmgr_get_drm_fd_wayland()
174 {
175         struct display *display = NULL;
176         int drm_fd;
177
178         display = create_display();
179     if (display == NULL) {
180         TBM_LOG ("[libtbm:%d] fail to create display\n", getpid());
181         return -1;
182     }
183
184         drm_fd = display->drm_fd;
185         destroy_display(display);
186
187     return drm_fd;
188 }
189