enhance log
[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 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         const char *debug;
99
100         debug = getenv("TDM_DEBUG");
101         if (debug && (strstr(debug, "1")))
102                 tdm_debug = 1;
103
104         private_client = calloc(1, sizeof *private_client);
105         if (!private_client) {
106                 TDM_ERR("alloc failed");
107                 if (error)
108                         *error = TDM_CLIENT_ERROR_OUT_OF_MEMORY;
109                 return NULL;
110         }
111
112         private_client->display = wl_display_connect("tdm-socket");
113         TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
114
115         private_client->registry = wl_display_get_registry(private_client->display);
116         TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
117
118         wl_registry_add_listener(private_client->registry,
119                                  &tdm_client_registry_listener, private_client);
120         wl_display_roundtrip(private_client->display);
121
122         /* check global objects */
123         TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
124
125         LIST_INITHEAD(&private_client->vblank_list);
126
127         if (error)
128                 *error = TDM_CLIENT_ERROR_NONE;
129
130         return (tdm_client*)private_client;
131 create_failed:
132         tdm_client_destroy((tdm_client*)private_client);
133         if (error)
134                 *error = TDM_CLIENT_ERROR_OPERATION_FAILED;
135         return NULL;
136 }
137
138 void
139 tdm_client_destroy(tdm_client *client)
140 {
141         tdm_private_client *private_client = (tdm_private_client*)client;
142         tdm_client_vblank_info *v = NULL, *vv = NULL;
143
144         if (!private_client)
145                 return;
146
147         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_client->vblank_list, link) {
148                 LIST_DEL(&v->link);
149                 wl_tdm_vblank_destroy(v->vblank);
150                 free(v);
151         }
152
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);
159
160         free(private_client);
161 }
162
163 tdm_client_error
164 tdm_client_get_fd(tdm_client *client, int *fd)
165 {
166         tdm_private_client *private_client;
167
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);
170
171         private_client = (tdm_private_client*)client;
172
173         *fd = wl_display_get_fd(private_client->display);
174         if (*fd < 0)
175                 return TDM_CLIENT_ERROR_OPERATION_FAILED;
176
177         return TDM_CLIENT_ERROR_NONE;
178 }
179
180 tdm_client_error
181 tdm_client_handle_events(tdm_client *client)
182 {
183         tdm_private_client *private_client;
184
185         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
186
187         private_client = (tdm_private_client*)client;
188
189         wl_display_dispatch(private_client->display);
190
191         return TDM_CLIENT_ERROR_NONE;
192 }
193
194 static void
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)
197 {
198         tdm_client_vblank_info *vblank_info = (tdm_client_vblank_info*)data;
199
200         TDM_RETURN_IF_FAIL(vblank_info != NULL);
201
202         if (vblank_info->vblank != vblank)
203                 TDM_NEVER_GET_HERE();
204
205         TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info, wl_proxy_get_id((struct wl_proxy *)vblank));
206
207         if (vblank_info->func) {
208                 vblank_info->func(sequence, tv_sec, tv_usec, vblank_info->user_data);
209         }
210
211         LIST_DEL(&vblank_info->link);
212         free(vblank_info);
213 }
214
215 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
216         _tdm_client_cb_vblank_done,
217 };
218
219 tdm_client_error
220 tdm_client_wait_vblank(tdm_client *client, char *name, int interval, int sync,
221                        tdm_client_vblank_handler func, void *user_data)
222 {
223         tdm_private_client *private_client = (tdm_private_client*)client;
224         tdm_client_vblank_info *vblank_info;
225
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);
231
232         vblank_info = calloc(1, sizeof *vblank_info);
233         if (!vblank_info) {
234                 TDM_ERR("alloc failed");
235                 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
236         }
237
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");
241                 free(vblank_info);
242                 return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
243         }
244
245         TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info, wl_proxy_get_id((struct wl_proxy *)vblank_info->vblank));
246
247         wl_tdm_vblank_add_listener(vblank_info->vblank,
248                                    &tdm_client_vblank_listener, vblank_info);
249
250         vblank_info->func = func;
251         vblank_info->user_data = user_data;
252         LIST_ADDTAIL(&vblank_info->link, &private_client->vblank_list);
253
254         if (sync)
255                 wl_display_roundtrip(private_client->display);
256         else
257                 wl_display_flush(private_client->display);
258
259         return TDM_CLIENT_ERROR_NONE;
260 }