apply wayland coding style
[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 #include <time.h>
44
45 #include "tdm_client.h"
46 #include "tdm_log.h"
47 #include "tdm_macro.h"
48 #include "tdm_list.h"
49 #include "tdm-client-protocol.h"
50
51 int tdm_debug;
52
53 typedef struct _tdm_private_client {
54         struct wl_display *display;
55         struct wl_registry *registry;
56         struct wl_tdm *tdm;
57
58         struct list_head vblank_list;
59 } tdm_private_client;
60
61 typedef struct _tdm_client_vblank_info {
62         struct list_head link;
63         struct wl_tdm_vblank *vblank;
64         tdm_client_vblank_handler func;
65         unsigned int req_sec;
66         unsigned int req_usec;
67         void *user_data;
68 } tdm_client_vblank_info;
69
70 static void
71 _tdm_client_cb_global(void *data, struct wl_registry *registry,
72                       uint32_t name, const char *interface,
73                       uint32_t version)
74 {
75         tdm_private_client *private_client = data;
76
77         if (strcmp(interface, "wl_tdm") == 0) {
78                 private_client->tdm =
79                         wl_registry_bind(registry, name, &wl_tdm_interface, version);
80                 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
81
82                 wl_display_flush(private_client->display);
83         }
84 }
85
86 static void
87 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
88 {
89 }
90
91 static const struct wl_registry_listener tdm_client_registry_listener =
92 {
93     _tdm_client_cb_global,
94     _tdm_client_cb_global_remove
95 };
96
97 tdm_client*
98 tdm_client_create(tdm_client_error *error)
99 {
100         tdm_private_client *private_client;
101         const char *debug;
102
103         debug = getenv("TDM_DEBUG");
104         if (debug && (strstr(debug, "1")))
105                 tdm_debug = 1;
106
107         private_client = calloc(1, sizeof *private_client);
108         if (!private_client) {
109                 TDM_ERR("alloc failed");
110                 if (error)
111                         *error = TDM_CLIENT_ERROR_OUT_OF_MEMORY;
112                 return NULL;
113         }
114
115         private_client->display = wl_display_connect("tdm-socket");
116         TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
117
118         private_client->registry = wl_display_get_registry(private_client->display);
119         TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
120
121         wl_registry_add_listener(private_client->registry,
122                                  &tdm_client_registry_listener, private_client);
123         wl_display_roundtrip(private_client->display);
124
125         /* check global objects */
126         TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
127
128         LIST_INITHEAD(&private_client->vblank_list);
129
130         if (error)
131                 *error = TDM_CLIENT_ERROR_NONE;
132
133         return (tdm_client*)private_client;
134 create_failed:
135         tdm_client_destroy((tdm_client*)private_client);
136         if (error)
137                 *error = TDM_CLIENT_ERROR_OPERATION_FAILED;
138         return NULL;
139 }
140
141 void
142 tdm_client_destroy(tdm_client *client)
143 {
144         tdm_private_client *private_client = (tdm_private_client*)client;
145         tdm_client_vblank_info *v = NULL, *vv = NULL;
146
147         if (!private_client)
148                 return;
149
150         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_client->vblank_list, link) {
151                 LIST_DEL(&v->link);
152                 wl_tdm_vblank_destroy(v->vblank);
153                 free(v);
154         }
155
156         if (private_client->tdm)
157                 wl_tdm_destroy(private_client->tdm);
158         if (private_client->registry)
159                 wl_registry_destroy(private_client->registry);
160         if (private_client->display)
161                 wl_display_disconnect(private_client->display);
162
163         free(private_client);
164 }
165
166 tdm_client_error
167 tdm_client_get_fd(tdm_client *client, int *fd)
168 {
169         tdm_private_client *private_client;
170
171         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
172         TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
173
174         private_client = (tdm_private_client*)client;
175
176         *fd = wl_display_get_fd(private_client->display);
177         if (*fd < 0)
178                 return TDM_CLIENT_ERROR_OPERATION_FAILED;
179
180         return TDM_CLIENT_ERROR_NONE;
181 }
182
183 tdm_client_error
184 tdm_client_handle_events(tdm_client *client)
185 {
186         tdm_private_client *private_client;
187
188         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
189
190         private_client = (tdm_private_client*)client;
191
192         wl_display_dispatch(private_client->display);
193
194         return TDM_CLIENT_ERROR_NONE;
195 }
196
197 static void
198 _tdm_client_cb_vblank_done(void *data, struct wl_tdm_vblank *vblank,
199                            uint32_t sequence, uint32_t tv_sec, uint32_t tv_usec)
200 {
201         tdm_client_vblank_info *vblank_info = (tdm_client_vblank_info*)data;
202
203         TDM_RETURN_IF_FAIL(vblank_info != NULL);
204
205         if (vblank_info->vblank != vblank)
206                 TDM_NEVER_GET_HERE();
207
208         TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info,
209                 wl_proxy_get_id((struct wl_proxy *)vblank));
210
211         if (vblank_info->func) {
212                 vblank_info->func(sequence, tv_sec, tv_usec, vblank_info->user_data);
213         }
214
215         LIST_DEL(&vblank_info->link);
216         free(vblank_info);
217 }
218
219 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
220         _tdm_client_cb_vblank_done,
221 };
222
223 tdm_client_error
224 tdm_client_wait_vblank(tdm_client *client, char *name,
225                        int sw_timer, int interval, int sync,
226                        tdm_client_vblank_handler func, void *user_data)
227 {
228         tdm_private_client *private_client = (tdm_private_client*)client;
229         tdm_client_vblank_info *vblank_info;
230         struct timespec tp;
231
232         TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
233         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_CLIENT_ERROR_INVALID_PARAMETER);
234         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
235         TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
236         TDM_RETURN_VAL_IF_FAIL(private_client->tdm != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
237
238         vblank_info = calloc(1, sizeof *vblank_info);
239         if (!vblank_info) {
240                 TDM_ERR("alloc failed");
241                 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
242         }
243
244         clock_gettime(CLOCK_MONOTONIC, &tp);
245
246         vblank_info->req_sec = (unsigned int)tp.tv_sec;
247         vblank_info->req_usec = (unsigned int)(tp.tv_nsec/1000L);
248
249         vblank_info->vblank =
250                 wl_tdm_wait_vblank(private_client->tdm, name, sw_timer, interval,
251                                    vblank_info->req_sec, vblank_info->req_usec);
252         if (!vblank_info->vblank) {
253                 TDM_ERR("couldn't create vblank resource");
254                 free(vblank_info);
255                 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
256         }
257
258         TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info,
259                 wl_proxy_get_id((struct wl_proxy *)vblank_info->vblank));
260
261         wl_tdm_vblank_add_listener(vblank_info->vblank,
262                                    &tdm_client_vblank_listener, vblank_info);
263
264         vblank_info->func = func;
265         vblank_info->user_data = user_data;
266         LIST_ADDTAIL(&vblank_info->link, &private_client->vblank_list);
267
268         if (sync)
269                 wl_display_roundtrip(private_client->display);
270         else
271                 wl_display_flush(private_client->display);
272
273         return TDM_CLIENT_ERROR_NONE;
274 }