support client API for wait_vblank
[platform/core/uifw/libtdm.git] / client / tdm_client.c
1 /**************************************************************************
2
3 libtdm
4
5 Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6
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>
13
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:
21
22 The above copyright notice and this permission notice (including the
23 next paragraph) shall be included in all copies or substantial portions
24 of the Software.
25
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.
33
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #include "tdm_client.h"
45 #include "tdm_log.h"
46 #include "tdm_macro.h"
47 #include "tdm_list.h"
48 #include "tdm-client-protocol.h"
49
50 extern int tdm_debug;
51
52 typedef struct _tdm_private_client {
53         struct wl_display *display;
54         struct wl_registry *registry;
55         struct wl_tdm *tdm;
56
57         struct list_head vblank_list;
58 } tdm_private_client;
59
60 typedef struct _tdm_client_vblank_info {
61         struct list_head link;
62         struct wl_tdm_vblank *vblank;
63         tdm_client_vblank_handler func;
64         void *user_data;
65 } tdm_client_vblank_info;
66
67 static void
68 _tdm_client_cb_global(void *data, struct wl_registry *registry,
69                       uint32_t name, const char *interface,
70                       uint32_t version)
71 {
72         tdm_private_client *private_client = data;
73
74         if (strcmp(interface, "wl_tdm") == 0) {
75                 private_client->tdm =
76                         wl_registry_bind(registry, name, &wl_tdm_interface, version);
77                 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
78
79                 wl_display_flush(private_client->display);
80         }
81 }
82
83 static void
84 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
85 {
86 }
87
88 static const struct wl_registry_listener tdm_client_registry_listener =
89 {
90     _tdm_client_cb_global,
91     _tdm_client_cb_global_remove
92 };
93
94 tdm_client*
95 tdm_client_create(tdm_client_error *error)
96 {
97         tdm_private_client *private_client;
98
99         private_client = calloc(1, sizeof *private_client);
100         if (!private_client) {
101                 TDM_ERR("alloc failed");
102                 if (error)
103                         *error = TDM_CLIENT_ERROR_OUT_OF_MEMORY;
104                 return NULL;
105         }
106
107         private_client->display = wl_display_connect("tdm-socket");
108         TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
109
110         private_client->registry = wl_display_get_registry(private_client->display);
111         TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
112
113         wl_registry_add_listener(private_client->registry,
114                                  &tdm_client_registry_listener, private_client);
115         wl_display_roundtrip(private_client->display);
116
117         /* check global objects */
118         TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
119
120         LIST_INITHEAD(&private_client->vblank_list);
121
122         if (error)
123                 *error = TDM_CLIENT_ERROR_NONE;
124
125         return (tdm_client*)private_client;
126 create_failed:
127         tdm_client_destroy((tdm_client*)private_client);
128         if (error)
129                 *error = TDM_CLIENT_ERROR_OPERATION_FAILED;
130         return NULL;
131 }
132
133 void
134 tdm_client_destroy(tdm_client *client)
135 {
136         tdm_private_client *private_client = (tdm_private_client*)client;
137         tdm_client_vblank_info *v = NULL, *vv = NULL;
138
139         if (!private_client)
140                 return;
141
142         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_client->vblank_list, link) {
143                 LIST_DEL(&v->link);
144                 wl_tdm_vblank_destroy(v->vblank);
145                 free(v);
146         }
147
148         if (private_client->tdm)
149                 wl_tdm_destroy(private_client->tdm);
150         if (private_client->registry)
151                 wl_registry_destroy(private_client->registry);
152         if (private_client->display)
153                 wl_display_disconnect(private_client->display);
154
155         free(private_client);
156 }
157
158 tdm_client_error
159 tdm_client_get_fd(tdm_client *client, int *fd)
160 {
161         tdm_private_client *private_client;
162
163         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
164         TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
165
166         private_client = (tdm_private_client*)client;
167
168         *fd = wl_display_get_fd(private_client->display);
169         if (*fd < 0)
170                 return TDM_CLIENT_ERROR_OPERATION_FAILED;
171
172         return TDM_CLIENT_ERROR_NONE;
173 }
174
175 tdm_client_error
176 tdm_client_handle_events(tdm_client *client)
177 {
178         tdm_private_client *private_client;
179
180         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
181
182         private_client = (tdm_private_client*)client;
183
184         wl_display_dispatch(private_client->display);
185
186         return TDM_CLIENT_ERROR_NONE;
187 }
188
189 static void
190 _tdm_client_cb_vblank_done(void *data, struct wl_tdm_vblank *vblank,
191                            uint32_t sequence, uint32_t tv_sec, uint32_t tv_usec)
192 {
193         tdm_client_vblank_info *vblank_info = (tdm_client_vblank_info*)data;
194
195         TDM_RETURN_IF_FAIL(vblank_info != NULL);
196
197         if (vblank_info->func) {
198                 vblank_info->func(sequence, tv_sec, tv_usec, vblank_info->user_data);
199         }
200
201         LIST_DEL(&vblank_info->link);
202         free(vblank_info);
203 }
204
205 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
206         _tdm_client_cb_vblank_done,
207 };
208
209 tdm_client_error
210 tdm_client_wait_vblank(tdm_client *client, char *name, int interval, int sync,
211                        tdm_client_vblank_handler func, void *user_data)
212 {
213         tdm_private_client *private_client = (tdm_private_client*)client;
214         tdm_client_vblank_info *vblank_info;
215
216         TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
217         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_CLIENT_ERROR_INVALID_PARAMETER);
218         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
219         TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
220         TDM_RETURN_VAL_IF_FAIL(private_client->tdm != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
221
222         vblank_info = calloc(1, sizeof *vblank_info);
223         if (!vblank_info) {
224                 TDM_ERR("alloc failed");
225                 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
226         }
227
228         vblank_info->vblank = wl_tdm_wait_vblank(private_client->tdm, name, interval);
229         if (!vblank_info->vblank) {
230                 TDM_ERR("couldn't create vblank resource");
231                 free(vblank_info);
232                 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
233         }
234
235         wl_tdm_vblank_add_listener(vblank_info->vblank,
236                                    &tdm_client_vblank_listener, vblank_info);
237
238         vblank_info->func = func;
239         vblank_info->user_data = user_data;
240         LIST_ADDTAIL(&vblank_info->link, &private_client->vblank_list);
241
242         if (sync)
243                 wl_display_roundtrip(private_client->display);
244         else
245                 wl_display_flush(private_client->display);
246
247         return TDM_CLIENT_ERROR_NONE;
248 }