tbm_bufmgr_debug_show: increasing size of title and data to avoid memory overflow
[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 /* LCOV_EXCL_START */
56 static void
57 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)
58 {
59         struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
60
61         /* client authentication infomation */
62         tbm_drm_client->auth_fd = auth_fd;
63         tbm_drm_client->capabilities = capabilities;
64         if (device_name)
65             tbm_drm_client->device = strdup(device_name);
66 }
67
68 static const struct wl_tbm_drm_auth_listener wl_tbm_drm_auth_client_listener = {
69         handle_tbm_drm_authentication_info
70 };
71
72 static void
73 _wayland_tbm_drm_auth_client_registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
74 {
75         struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
76
77         if (!strcmp(interface, "wl_tbm_drm_auth")) {
78                 tbm_drm_client->wl_tbm_drm_auth = wl_registry_bind(registry, name, &wl_tbm_drm_auth_interface, version);
79                 TBM_RETURN_IF_FAIL(tbm_drm_client->wl_tbm_drm_auth != NULL);
80
81                 wl_tbm_drm_auth_add_listener(tbm_drm_client->wl_tbm_drm_auth, &wl_tbm_drm_auth_client_listener, tbm_drm_client);
82         }
83 }
84
85 static const struct wl_registry_listener registry_listener = {
86         _wayland_tbm_drm_auth_client_registry_handle_global,
87         NULL
88 };
89
90 int
91 tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities)
92 {
93         struct wl_display *display;
94         struct wl_registry *wl_registry;
95         struct wayland_tbm_drm_auth_client *tbm_drm_client;
96
97         tbm_drm_client = calloc(1, sizeof(struct wayland_tbm_drm_auth_client));
98         TBM_RETURN_VAL_IF_FAIL(tbm_drm_client != NULL, 0);
99
100         tbm_drm_client->auth_fd = -1;
101
102         display = wl_display_connect("tbm-drm-auth");
103         if (!display) {
104                 TBM_LOG_E("Failed to connect display\n");
105                 free(tbm_drm_client);
106
107                 return 0;
108         }
109
110         tbm_drm_client->display = display;
111
112         wl_registry = wl_display_get_registry(display);
113         if (!wl_registry) {
114                 TBM_LOG_E("Failed to get registry\n");
115                 wl_display_disconnect(display);
116                 free(tbm_drm_client);
117
118                 return 0;
119         }
120
121         wl_registry_add_listener(wl_registry, &registry_listener, tbm_drm_client);
122         if (wl_display_roundtrip(display) < 0) { //For Gloabl registry
123                 TBM_LOG_E("Failed to wl_display_roundtrip for global registry\n");
124                 wl_registry_destroy(wl_registry);
125                 wl_display_disconnect(display);
126                 free(tbm_drm_client);
127                 return 0;
128         }
129
130         if (!tbm_drm_client->wl_tbm_drm_auth) {
131                 TBM_LOG_E("Failed to get wl_tbm_drm_auth interface\n");
132                 wl_registry_destroy(wl_registry);
133                 wl_display_disconnect(display);
134                 free(tbm_drm_client);
135
136                 return 0;
137         }
138
139         wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
140         if (wl_display_roundtrip(display) < 0) {
141                 TBM_LOG_E("Failed to wl_display_roundtrip get auth info\n");
142                 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
143                 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
144                 wl_registry_destroy(wl_registry);
145                 wl_display_disconnect(display);
146                 free(tbm_drm_client);
147                 return 0;
148         }
149
150         if (tbm_drm_client->auth_fd < 0) {
151                 TBM_LOG_E("Failed to get auth info\n");
152                 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
153                 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
154                 wl_registry_destroy(wl_registry);
155                 wl_display_disconnect(display);
156                 free(tbm_drm_client);
157
158                 return 0;
159         }
160
161         if (auth_fd)
162                 *auth_fd = tbm_drm_client->auth_fd;
163         else
164                 close(tbm_drm_client->auth_fd);
165
166         if (capabilities)
167                 *capabilities = tbm_drm_client->capabilities;
168
169         if (device) {
170                 if (tbm_drm_client->device)
171                         *device = strdup(tbm_drm_client->device);
172                 else
173                         *device = NULL;
174         }
175
176         wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
177         wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
178
179         if (tbm_drm_client->device)
180                 free(tbm_drm_client->device);
181
182         free(tbm_drm_client);
183
184         wl_registry_destroy(wl_registry);
185         wl_display_disconnect(display);
186
187         return 1;
188 }
189
190
191 void
192 tbm_drm_helper_set_fd(int fd)
193 {
194         char buf[32];
195         int ret;
196
197         snprintf(buf, sizeof(buf), "%d", fd);
198
199         ret = setenv("TBM_DRM_FD", (const char*)buf, 1);
200         if (ret) {
201                 TBM_LOG_E("failed to set TBM_DRM_FD to %d\n", fd);
202                 return;
203         }
204
205         TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
206 }
207
208 void
209 tbm_drm_helper_unset_fd(void)
210 {
211         int ret;
212
213         ret = unsetenv("TBM_DRM_FD");
214         if (ret) {
215                 TBM_LOG_E("failed to unset TBM_DRM_FD\n");
216                 return;
217         }
218 }
219
220 int
221 tbm_drm_helper_get_fd(void)
222 {
223         const char *value;
224         int ret, flags, fd = -1;
225         int new_fd = -1;
226
227         value = (const char*)getenv("TBM_DRM_FD");
228         if (!value)
229                 return -1;
230
231         ret = sscanf(value, "%d", &fd);
232         if (ret <= 0)
233                 return -1;
234
235         TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
236
237         flags = fcntl(fd, F_GETFD);
238         if (flags == -1) {
239                 TBM_LOG_E("fcntl failed: %m\n");
240                 return -1;
241         }
242
243         new_fd = dup(fd);
244         if (new_fd < 0) {
245                 TBM_LOG_E("dup failed: %m\n");
246                 return -1;
247         }
248
249         fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC);
250
251         TBM_LOG_I("Return TBM_FD: %d\n", new_fd);
252
253         return new_fd;
254 }
255
256 /* LCOV_EXCL_STOP */