1 /**************************************************************************
5 Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 JinYoung Jeon <jy0.jeon@samsung.com>,
9 Taeheon Kim <th908.kim@samsung.com>,
10 YoungJun Cho <yj44.cho@samsung.com>,
11 SooChan Lim <sc1.lim@samsung.com>,
12 Boram Park <sc1.lim@samsung.com>
14 Permission is hereby granted, free of charge, to any person obtaining a
15 copy of this software and associated documentation files (the
16 "Software"), to deal in the Software without restriction, including
17 without limitation the rights to use, copy, modify, merge, publish,
18 distribute, sub license, and/or sell copies of the Software, and to
19 permit persons to whom the Software is furnished to do so, subject to
20 the following conditions:
22 The above copyright notice and this permission notice (including the
23 next paragraph) shall be included in all copies or substantial portions
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
44 #include "tdm_client.h"
46 #include "tdm_macro.h"
48 #include "tdm-client-protocol.h"
52 typedef struct _tdm_private_client {
53 struct wl_display *display;
54 struct wl_registry *registry;
57 struct list_head vblank_list;
60 typedef struct _tdm_client_vblank_info {
61 struct list_head link;
62 struct wl_tdm_vblank *vblank;
63 tdm_client_vblank_handler func;
65 } tdm_client_vblank_info;
68 _tdm_client_cb_global(void *data, struct wl_registry *registry,
69 uint32_t name, const char *interface,
72 tdm_private_client *private_client = data;
74 if (strcmp(interface, "wl_tdm") == 0) {
76 wl_registry_bind(registry, name, &wl_tdm_interface, version);
77 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
79 wl_display_flush(private_client->display);
84 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
88 static const struct wl_registry_listener tdm_client_registry_listener =
90 _tdm_client_cb_global,
91 _tdm_client_cb_global_remove
95 tdm_client_create(tdm_client_error *error)
97 tdm_private_client *private_client;
100 debug = getenv("TDM_DEBUG");
101 if (debug && (strstr(debug, "1")))
104 private_client = calloc(1, sizeof *private_client);
105 if (!private_client) {
106 TDM_ERR("alloc failed");
108 *error = TDM_CLIENT_ERROR_OUT_OF_MEMORY;
112 private_client->display = wl_display_connect("tdm-socket");
113 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
115 private_client->registry = wl_display_get_registry(private_client->display);
116 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
118 wl_registry_add_listener(private_client->registry,
119 &tdm_client_registry_listener, private_client);
120 wl_display_roundtrip(private_client->display);
122 /* check global objects */
123 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
125 LIST_INITHEAD(&private_client->vblank_list);
128 *error = TDM_CLIENT_ERROR_NONE;
130 return (tdm_client*)private_client;
132 tdm_client_destroy((tdm_client*)private_client);
134 *error = TDM_CLIENT_ERROR_OPERATION_FAILED;
139 tdm_client_destroy(tdm_client *client)
141 tdm_private_client *private_client = (tdm_private_client*)client;
142 tdm_client_vblank_info *v = NULL, *vv = NULL;
147 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_client->vblank_list, link) {
149 wl_tdm_vblank_destroy(v->vblank);
153 if (private_client->tdm)
154 wl_tdm_destroy(private_client->tdm);
155 if (private_client->registry)
156 wl_registry_destroy(private_client->registry);
157 if (private_client->display)
158 wl_display_disconnect(private_client->display);
160 free(private_client);
164 tdm_client_get_fd(tdm_client *client, int *fd)
166 tdm_private_client *private_client;
168 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
169 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
171 private_client = (tdm_private_client*)client;
173 *fd = wl_display_get_fd(private_client->display);
175 return TDM_CLIENT_ERROR_OPERATION_FAILED;
177 return TDM_CLIENT_ERROR_NONE;
181 tdm_client_handle_events(tdm_client *client)
183 tdm_private_client *private_client;
185 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
187 private_client = (tdm_private_client*)client;
189 wl_display_dispatch(private_client->display);
191 return TDM_CLIENT_ERROR_NONE;
195 _tdm_client_cb_vblank_done(void *data, struct wl_tdm_vblank *vblank,
196 uint32_t sequence, uint32_t tv_sec, uint32_t tv_usec)
198 tdm_client_vblank_info *vblank_info = (tdm_client_vblank_info*)data;
200 TDM_RETURN_IF_FAIL(vblank_info != NULL);
202 if (vblank_info->vblank != vblank)
203 TDM_NEVER_GET_HERE();
205 TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info, wl_proxy_get_id((struct wl_proxy *)vblank));
207 if (vblank_info->func) {
208 vblank_info->func(sequence, tv_sec, tv_usec, vblank_info->user_data);
211 LIST_DEL(&vblank_info->link);
215 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
216 _tdm_client_cb_vblank_done,
220 tdm_client_wait_vblank(tdm_client *client, char *name, int interval, int sync,
221 tdm_client_vblank_handler func, void *user_data)
223 tdm_private_client *private_client = (tdm_private_client*)client;
224 tdm_client_vblank_info *vblank_info;
226 TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
227 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_CLIENT_ERROR_INVALID_PARAMETER);
228 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
229 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
230 TDM_RETURN_VAL_IF_FAIL(private_client->tdm != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
232 vblank_info = calloc(1, sizeof *vblank_info);
234 TDM_ERR("alloc failed");
235 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
238 vblank_info->vblank = wl_tdm_wait_vblank(private_client->tdm, name, interval);
239 if (!vblank_info->vblank) {
240 TDM_ERR("couldn't create vblank resource");
242 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
245 TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info, wl_proxy_get_id((struct wl_proxy *)vblank_info->vblank));
247 wl_tdm_vblank_add_listener(vblank_info->vblank,
248 &tdm_client_vblank_listener, vblank_info);
250 vblank_info->func = func;
251 vblank_info->user_data = user_data;
252 LIST_ADDTAIL(&vblank_info->link, &private_client->vblank_list);
255 wl_display_roundtrip(private_client->display);
257 wl_display_flush(private_client->display);
259 return TDM_CLIENT_ERROR_NONE;