adapt tizen coding rule
[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 <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stddef.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40
41 #include "tbm_bufmgr_int.h"
42
43 #include "wayland-tbm-drm-auth-client-protocol.h"
44
45 struct wayland_tbm_drm_auth_client {
46         struct wl_display *display;
47         struct wl_tbm_drm_auth *wl_tbm_drm_auth;
48         int auth_fd;
49         char *device;
50         uint32_t capabilities;
51 };
52
53 /* LCOV_EXCL_START */
54 static void
55 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)
56 {
57         struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
58
59         /* client authentication infomation */
60         tbm_drm_client->auth_fd = auth_fd;
61         tbm_drm_client->capabilities = capabilities;
62         if (device_name)
63             tbm_drm_client->device = strdup(device_name);
64 }
65
66 static const struct wl_tbm_drm_auth_listener wl_tbm_drm_auth_client_listener = {
67         handle_tbm_drm_authentication_info
68 };
69
70 static void
71 _wayland_tbm_drm_auth_client_registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
72 {
73         struct wayland_tbm_drm_auth_client *tbm_drm_client = (struct wayland_tbm_drm_auth_client *)data;
74
75         if (!strcmp(interface, "wl_tbm_drm_auth")) {
76                 tbm_drm_client->wl_tbm_drm_auth = wl_registry_bind(registry, name, &wl_tbm_drm_auth_interface, version);
77                 TBM_RETURN_IF_FAIL(tbm_drm_client->wl_tbm_drm_auth != NULL);
78
79                 wl_tbm_drm_auth_add_listener(tbm_drm_client->wl_tbm_drm_auth, &wl_tbm_drm_auth_client_listener, tbm_drm_client);
80         }
81 }
82
83 static const struct wl_registry_listener registry_listener = {
84         _wayland_tbm_drm_auth_client_registry_handle_global,
85         NULL
86 };
87
88 int
89 tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities)
90 {
91         struct wl_display *display;
92         struct wl_registry *wl_registry;
93         struct wayland_tbm_drm_auth_client *tbm_drm_client;
94
95         tbm_drm_client = calloc(1, sizeof(struct wayland_tbm_drm_auth_client));
96         TBM_RETURN_VAL_IF_FAIL(tbm_drm_client != NULL, 0);
97
98         tbm_drm_client->auth_fd = -1;
99
100         display = wl_display_connect("tbm-drm-auth");
101         if (!display) {
102                 TBM_LOG_E("Failed to connect display\n");
103                 free(tbm_drm_client);
104
105                 return 0;
106         }
107
108         tbm_drm_client->display = display;
109
110         wl_registry = wl_display_get_registry(display);
111         if (!wl_registry) {
112                 TBM_LOG_E("Failed to get registry\n");
113                 wl_display_disconnect(display);
114                 free(tbm_drm_client);
115
116                 return 0;
117         }
118
119         wl_registry_add_listener(wl_registry, &registry_listener, tbm_drm_client);
120         wl_display_roundtrip(display); //For Gloabl registry
121
122         if (!tbm_drm_client->wl_tbm_drm_auth) {
123                 TBM_LOG_E("Failed to get wl_tbm_drm_auth interface\n");
124                 wl_registry_destroy(wl_registry);
125                 wl_display_disconnect(display);
126                 free(tbm_drm_client);
127
128                 return 0;
129         }
130
131         wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
132         wl_display_roundtrip(display);
133
134         if (tbm_drm_client->auth_fd < 0) {
135                 TBM_LOG_E("Failed to get auth info\n");
136                 wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
137                 wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
138                 wl_registry_destroy(wl_registry);
139                 wl_display_disconnect(display);
140                 free(tbm_drm_client);
141
142                 return 0;
143         }
144
145         if (auth_fd)
146                 *auth_fd = tbm_drm_client->auth_fd;
147         else
148                 close(tbm_drm_client->auth_fd);
149
150         if (capabilities)
151                 *capabilities = tbm_drm_client->capabilities;
152
153         if (device) {
154                 if (tbm_drm_client->device)
155                         *device = strdup(tbm_drm_client->device);
156                 else
157                         *device = NULL;
158         }
159
160         wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
161         wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
162
163         if (tbm_drm_client->device)
164                 free(tbm_drm_client->device);
165
166         free(tbm_drm_client);
167
168         wl_registry_destroy(wl_registry);
169         wl_display_disconnect(display);
170
171         return 1;
172 }
173 /* LCOV_EXCL_STOP */
174