apply gcov
[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 #include <strings.h>
45
46 #include <tdm-client-protocol.h>
47
48 #include "tdm_client.h"
49 #include "tdm_log.h"
50 #include "tdm_macro.h"
51 #include "tdm_list.h"
52 #include "tdm.h"
53 #include "tdm_private.h"
54
55 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
56
57 typedef struct _tdm_private_client {
58         pthread_mutex_t lock;
59
60         struct wl_display *display;
61         struct wl_event_queue *queue;
62         struct wl_registry *registry;
63         struct wl_tdm *tdm;
64         struct list_head output_list;
65
66         unsigned int enable_ttrace;
67         unsigned int stamp;
68
69         tdm_private_client_vblank *temp_vblank;
70 } tdm_private_client;
71
72 typedef struct _tdm_private_client_output {
73         tdm_private_client *private_client;
74
75         char name[TDM_NAME_LEN];
76         struct wl_tdm_output *output;
77         int width;
78         int height;
79         int refresh;
80         tdm_output_conn_status connection;
81         tdm_output_dpms dpms;
82         struct list_head vblank_list;
83         struct list_head change_handler_list;
84
85         unsigned int req_id;
86         unsigned int watch_output_changes;
87
88         struct list_head link;
89 } tdm_private_client_output;
90
91 struct _tdm_private_client_vblank {
92         tdm_private_client_output *private_output;
93
94         struct wl_tdm_vblank *vblank;
95         struct list_head wait_list;
96
97         char name[TDM_NAME_LEN];
98         unsigned int sync;
99         unsigned int fps;
100         int offset;
101         unsigned int enable_fake;
102         unsigned int enable_ttrace;
103
104         unsigned int started;
105         unsigned int stamp;
106
107         double req_time;
108         double last_time;
109
110         struct list_head link;
111 };
112
113 typedef struct _tdm_client_output_handler_info {
114         tdm_private_client_output *private_output;
115
116         tdm_client_output_change_handler func;
117         void *user_data;
118
119         struct list_head link;
120         struct list_head call_link;
121 } tdm_client_output_handler_info;
122
123 typedef struct _tdm_client_wait_info {
124         tdm_private_client_vblank *private_vblank;
125
126         tdm_client_vblank_handler func;
127         void *user_data;
128
129         unsigned int req_id;
130         double req_time;
131         int need_free;
132
133         struct list_head link;
134         struct list_head call_link;
135 } tdm_client_wait_info;
136
137 static void
138 _tdm_client_vblank_cb_stamp(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t stamp)
139 {
140         tdm_private_client_vblank *private_vblank = data;
141         tdm_private_client *private_client;
142
143         TDM_RETURN_IF_FAIL(private_vblank != NULL);
144
145         private_vblank->stamp = stamp;
146
147         TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
148         private_client = private_vblank->private_output->private_client;
149
150         private_client->stamp = stamp;
151 }
152
153 /* LCOV_EXCL_START */
154 static void
155 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
156                                                    uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
157                                                    uint32_t tv_usec, uint32_t error)
158 {
159         tdm_private_client_vblank *private_vblank = data;
160         tdm_private_client *private_client;
161         tdm_client_wait_info *w = NULL, *wait_info = NULL;
162
163         TDM_RETURN_IF_FAIL(private_vblank != NULL);
164
165         private_client = private_vblank->private_output->private_client;
166
167         private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
168
169         TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
170                         private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
171
172         LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
173                 if (w->req_id != req_id)
174                         continue;
175
176                 wait_info = w;
177                 break;
178         }
179
180         if (!wait_info) {
181                 TDM_ERR("no wait infomation for req_id(%d)", req_id);
182                 return;
183         }
184
185         if (private_vblank->enable_ttrace)
186                 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
187
188         if (wait_info->req_time >= private_vblank->last_time)
189                 TDM_WRN("'req(%.6f) < last(%.6f)' failed. error(%d)", wait_info->req_time, private_vblank->last_time, error);
190
191         if (wait_info->need_free)
192                 LIST_DEL(&wait_info->link);
193
194         if (wait_info->func) {
195                 pthread_mutex_unlock(&private_client->lock);
196                 wait_info->func(private_vblank, error, sequence, tv_sec, tv_usec, wait_info->user_data);
197                 pthread_mutex_lock(&private_client->lock);
198         }
199
200         if (wait_info->need_free)
201                 free(w);
202         else
203                 wait_info->need_free = 1;
204 }
205 /* LCOV_EXCL_STOP */
206
207 /* LCOV_EXCL_START */
208 static void
209 _tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
210 {
211         tdm_private_client_vblank *private_vblank = data;
212         tdm_private_client *private_client;
213
214         TDM_RETURN_IF_FAIL(private_vblank != NULL);
215
216         private_vblank->enable_ttrace = enable;
217
218         TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
219         private_client = private_vblank->private_output->private_client;
220
221         private_client->enable_ttrace = enable;
222 }
223 /* LCOV_EXCL_STOP */
224
225 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
226         _tdm_client_vblank_cb_stamp,
227         _tdm_client_vblank_cb_done,
228         _tdm_client_vblank_cb_ttrace,
229 };
230
231 static void
232 _tdm_client_output_destroy(tdm_private_client_output *private_output)
233 {
234         tdm_private_client_vblank *v = NULL, *vv = NULL;
235         tdm_client_output_handler_info *h = NULL, *hh = NULL;
236
237         LIST_DEL(&private_output->link);
238
239         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
240                 TDM_ERR("vblanks SHOULD be destroyed first!");
241                 LIST_DEL(&v->link);
242                 v->private_output = NULL;
243         }
244
245         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
246                 LIST_DEL(&h->link);
247                 free(h);
248         }
249
250         wl_tdm_output_destroy(private_output->output);
251
252         free(private_output);
253 }
254
255 static void
256 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
257                                                    uint32_t width, uint32_t height, uint32_t refresh, uint32_t error)
258 {
259         tdm_private_client_output *private_output = (tdm_private_client_output*)data;
260
261         TDM_RETURN_IF_FAIL(private_output != NULL);
262
263         private_output->width = width;
264         private_output->height = height;
265         private_output->refresh = refresh;
266
267         if (error != TDM_ERROR_NONE)
268                 TDM_INFO("mode event error: %d", error);
269
270         TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
271                         private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
272                         width, height, refresh);
273 }
274
275 static void
276 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
277 {
278         tdm_private_client_output *private_output = (tdm_private_client_output*)data;
279         tdm_private_client *private_client;
280         tdm_client_output_handler_info *h = NULL, *hh = NULL;
281         tdm_value v;
282         struct list_head call_list;
283
284         TDM_RETURN_IF_FAIL(private_output != NULL);
285
286         private_client = private_output->private_client;
287
288         if (private_output->connection == value)
289                 return;
290
291         private_output->connection = value;
292
293         if (error != TDM_ERROR_NONE)
294                 TDM_INFO("connection event error: %d", error);
295
296         TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
297                         private_output,
298                         wl_proxy_get_id((struct wl_proxy*)private_output->output),
299                         value);
300
301         LIST_INITHEAD(&call_list);
302
303         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
304                 LIST_ADDTAIL(&h->call_link, &call_list);
305         }
306
307         v.u32 = value;
308         pthread_mutex_unlock(&private_client->lock);
309         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
310                 if (h->func)
311                         h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
312         }
313         pthread_mutex_lock(&private_client->lock);
314 }
315
316 static void
317 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
318 {
319         tdm_private_client_output *private_output = (tdm_private_client_output*)data;
320         tdm_private_client *private_client;
321         tdm_client_output_handler_info *h = NULL, *hh = NULL;
322         tdm_value v;
323         struct list_head call_list;
324
325         TDM_RETURN_IF_FAIL(private_output != NULL);
326
327         private_client = private_output->private_client;
328
329         /* If value is extended value, we handle it as DPMS on in client side
330          * The extended DPMS value is valid only in server side.
331          * Or, need to export to client side also?
332          */
333         if (value > TDM_OUTPUT_DPMS_OFF)
334                 value = TDM_OUTPUT_DPMS_ON;
335
336         if (private_output->dpms == value)
337                 return;
338
339         private_output->dpms = value;
340
341         if (error != TDM_ERROR_NONE)
342                 TDM_INFO("dpms event error: %d", error);
343
344         TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
345                         private_output,
346                         wl_proxy_get_id((struct wl_proxy*)private_output->output),
347                         value);
348
349         LIST_INITHEAD(&call_list);
350
351         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
352                 LIST_ADDTAIL(&h->call_link, &call_list);
353         }
354
355         v.u32 = value;
356         pthread_mutex_unlock(&private_client->lock);
357         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
358                 if (h->func)
359                         h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
360         }
361         pthread_mutex_lock(&private_client->lock);
362 }
363
364 static const struct wl_tdm_output_listener tdm_client_output_listener = {
365         _tdm_client_output_cb_mode,
366         _tdm_client_output_cb_connection,
367         _tdm_client_output_cb_dpms,
368 };
369
370 static void
371 _tdm_client_cb_global(void *data, struct wl_registry *registry,
372                                           uint32_t name, const char *interface,
373                                           uint32_t version)
374 {
375         tdm_private_client *private_client = data;
376
377         if (strncmp(interface, "wl_tdm", 6) == 0) {
378                 private_client->tdm =
379                         wl_registry_bind(registry, name, &wl_tdm_interface, version);
380                 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
381
382                 wl_display_flush(private_client->display);
383         }
384 }
385
386 /* LCOV_EXCL_START */
387 static void
388 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
389 {
390 }
391 /* LCOV_EXCL_STOP */
392
393 static const struct wl_registry_listener tdm_client_registry_listener = {
394         _tdm_client_cb_global,
395         _tdm_client_cb_global_remove
396 };
397
398 tdm_client*
399 tdm_client_create(tdm_error *error)
400 {
401         tdm_private_client *private_client;
402
403         private_client = calloc(1, sizeof *private_client);
404         if (!private_client) {
405                 /* LCOV_EXCL_START */
406
407                 TDM_ERR("alloc failed");
408                 if (error)
409                         *error = TDM_ERROR_OUT_OF_MEMORY;
410                 return NULL;
411
412                 /* LCOV_EXCL_STOP */
413         }
414
415         if (pthread_mutex_init(&private_client->lock, NULL)) {
416                 TDM_ERR("mutex init failed: %m");
417                 free(private_client);
418                 if (error)
419                         *error = TDM_ERROR_OUT_OF_MEMORY;
420                 return NULL;
421         }
422
423         LIST_INITHEAD(&private_client->output_list);
424
425         private_client->display = wl_display_connect("tdm-socket");
426         TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
427
428         private_client->queue = wl_display_create_queue(private_client->display);
429         TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
430
431         private_client->registry = wl_display_get_registry(private_client->display);
432         TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
433
434         wl_registry_add_listener(private_client->registry,
435                                                          &tdm_client_registry_listener, private_client);
436         wl_display_roundtrip(private_client->display);
437
438         /* check global objects */
439         TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
440
441         if (error)
442                 *error = TDM_ERROR_NONE;
443
444         return (tdm_client*)private_client;
445 create_failed:
446         tdm_client_destroy((tdm_client*)private_client);
447         if (error)
448                 *error = TDM_ERROR_OPERATION_FAILED;
449         return NULL;
450 }
451
452 void
453 tdm_client_destroy(tdm_client *client)
454 {
455         tdm_private_client *private_client = (tdm_private_client*)client;
456         tdm_private_client_output *o = NULL, *oo = NULL;
457
458         if (!private_client)
459                 return;
460
461         pthread_mutex_lock(&private_client->lock);
462
463         if (private_client->temp_vblank)
464                 tdm_client_vblank_destroy(private_client->temp_vblank);
465
466         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
467                 _tdm_client_output_destroy(o);
468         }
469
470         if (private_client->tdm)
471                 wl_tdm_destroy(private_client->tdm);
472         if (private_client->registry)
473                 wl_registry_destroy(private_client->registry);
474         if (private_client->queue)
475                 wl_event_queue_destroy(private_client->queue);
476         if (private_client->display)
477                 wl_display_disconnect(private_client->display);
478
479         pthread_mutex_unlock(&private_client->lock);
480         pthread_mutex_destroy(&private_client->lock);
481
482         free(private_client);
483 }
484
485 tdm_error
486 tdm_client_get_fd(tdm_client *client, int *fd)
487 {
488         tdm_private_client *private_client;
489
490         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
491         TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
492
493         private_client = (tdm_private_client*)client;
494
495         pthread_mutex_lock(&private_client->lock);
496
497         *fd = wl_display_get_fd(private_client->display);
498
499         pthread_mutex_unlock(&private_client->lock);
500
501         if (*fd < 0)
502                 return TDM_ERROR_OPERATION_FAILED;
503
504         return TDM_ERROR_NONE;
505 }
506
507 tdm_error
508 tdm_client_handle_events(tdm_client *client)
509 {
510         tdm_private_client *private_client;
511
512         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
513
514         /* LCOV_EXCL_START */
515         private_client = (tdm_private_client*)client;
516
517         pthread_mutex_lock(&private_client->lock);
518
519         if (private_client->enable_ttrace)
520                 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
521
522         wl_display_dispatch(private_client->display);
523
524         if (private_client->enable_ttrace)
525                 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
526
527         pthread_mutex_unlock(&private_client->lock);
528
529         return TDM_ERROR_NONE;
530         /* LCOV_EXCL_STOP */
531 }
532
533 typedef struct _tdm_client_vblank_temp {
534         tdm_client_vblank_handler2 func;
535         void *user_data;
536 } tdm_client_vblank_temp;
537
538 /* LCOV_EXCL_START */
539 static void
540 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
541                                                                 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
542 {
543         tdm_client_vblank_temp *vblank_temp = user_data;
544         tdm_private_client_vblank *private_vblank;
545         tdm_private_client *private_client;
546
547         TDM_RETURN_IF_FAIL(vblank_temp != NULL);
548         TDM_RETURN_IF_FAIL(vblank != NULL);
549
550         private_vblank = vblank;
551         private_client = private_vblank->private_output->private_client;
552
553         if (vblank_temp->func) {
554                 pthread_mutex_unlock(&private_client->lock);
555                 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
556                 pthread_mutex_lock(&private_client->lock);
557         }
558
559         free(vblank_temp);
560 }
561 /* LCOV_EXCL_STOP */
562
563 /* LCOV_EXCL_START */ /* deprecated */
564 tdm_error
565 tdm_client_wait_vblank(tdm_client *client, char *name,
566                                            int sw_timer, int interval, int sync,
567                                            tdm_client_vblank_handler2 func, void *user_data)
568 {
569         tdm_private_client *private_client = (tdm_private_client*)client;
570         tdm_client_output *output;
571         tdm_client_vblank_temp *vblank_temp;
572         tdm_error ret;
573
574         TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
575         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
576         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
577
578         if (!private_client->temp_vblank) {
579                 output = tdm_client_get_output(client, name, &ret);
580                 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
581
582                 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
583                 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
584         }
585
586         tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
587         tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
588
589         vblank_temp = calloc(1, sizeof *vblank_temp);
590         TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
591
592         vblank_temp->func = func;
593         vblank_temp->user_data = user_data;
594
595         return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
596 }
597 /* LCOV_EXCL_STOP */
598
599 tdm_client_output*
600 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
601 {
602         tdm_private_client *private_client;
603         tdm_private_client_output *private_output = NULL;
604         struct wl_proxy *wrapper;
605
606         if (error)
607                 *error = TDM_ERROR_NONE;
608
609         if (!client) {
610                 TDM_ERR("'!client' failed");
611                 if (error)
612                         *error = TDM_ERROR_INVALID_PARAMETER;
613                 return NULL;
614         }
615
616         private_client = (tdm_private_client*)client;
617
618         pthread_mutex_lock(&private_client->lock);
619
620         if (!name)
621                 name = "primary";
622
623         LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
624                 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
625                         pthread_mutex_unlock(&private_client->lock);
626                         return (tdm_client_output*)private_output;
627                 }
628         }
629
630         wrapper = wl_proxy_create_wrapper(private_client->tdm);
631         if (!wrapper) {
632                 TDM_ERR("create output_wrapper failed");
633                 if (error)
634                         *error = TDM_ERROR_OUT_OF_MEMORY;
635                 pthread_mutex_unlock(&private_client->lock);
636                 return NULL;
637         }
638
639         wl_proxy_set_queue(wrapper, private_client->queue);
640
641         private_output = calloc(1, sizeof *private_output);
642         if (!private_output) {
643                 /* LCOV_EXCL_START */
644                 wl_proxy_wrapper_destroy(wrapper);
645                 TDM_ERR("alloc failed");
646                 if (error)
647                         *error = TDM_ERROR_OUT_OF_MEMORY;
648                 pthread_mutex_unlock(&private_client->lock);
649                 return NULL;
650
651                 /* LCOV_EXCL_STOP */
652         }
653
654         private_output->private_client = private_client;
655
656         snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
657         private_output->output = wl_tdm_create_output((struct wl_tdm *)wrapper, private_output->name);
658         wl_proxy_wrapper_destroy(wrapper);
659         if (!private_output->output) {
660                 /* LCOV_EXCL_START */
661
662                 TDM_ERR("couldn't create output resource");
663                 free(private_output);
664                 if (error)
665                         *error = TDM_ERROR_OUT_OF_MEMORY;
666                 pthread_mutex_unlock(&private_client->lock);
667                 return NULL;
668
669                 /* LCOV_EXCL_STOP */
670         }
671
672         LIST_INITHEAD(&private_output->vblank_list);
673         LIST_INITHEAD(&private_output->change_handler_list);
674         LIST_ADDTAIL(&private_output->link, &private_client->output_list);
675
676         wl_tdm_output_add_listener(private_output->output,
677                                                            &tdm_client_output_listener, private_output);
678         wl_display_roundtrip_queue(private_client->display, private_client->queue);
679
680         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
681
682         pthread_mutex_unlock(&private_client->lock);
683
684         return (tdm_client_output*)private_output;
685 }
686
687 tdm_error
688 tdm_client_output_add_change_handler(tdm_client_output *output,
689                                                                          tdm_client_output_change_handler func,
690                                                                          void *user_data)
691 {
692         tdm_private_client_output *private_output;
693         tdm_private_client *private_client;
694         tdm_client_output_handler_info *h;
695
696         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
697         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
698
699         private_output = (tdm_private_client_output*)output;
700         private_client = private_output->private_client;
701
702         h = calloc(1, sizeof *h);
703         TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
704
705         pthread_mutex_lock(&private_client->lock);
706
707         if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
708                 wl_tdm_output_watch_output_changes(private_output->output, 1);
709
710                 /* TODO: this is very tricky.
711                  * If a client adds the change_handler, we might be able to guess that
712                  * the client will watch the tdm client's fd and handle tdm events in
713                  * event loop. Otherwise, we CAN'T make sure if a client has event loop
714                  * which handles tdm events.
715                  */
716                 private_output->watch_output_changes = 1;
717         }
718
719         h->private_output = private_output;
720         h->func = func;
721         h->user_data = user_data;
722         LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
723         LIST_INITHEAD(&h->call_link);
724
725         pthread_mutex_unlock(&private_client->lock);
726
727         return TDM_ERROR_NONE;
728 }
729
730 void
731 tdm_client_output_remove_change_handler(tdm_client_output *output,
732                                                                                 tdm_client_output_change_handler func,
733                                                                                 void *user_data)
734 {
735         tdm_private_client_output *private_output;
736         tdm_private_client *private_client;
737         tdm_client_output_handler_info *h = NULL;
738
739         TDM_RETURN_IF_FAIL(output != NULL);
740         TDM_RETURN_IF_FAIL(func != NULL);
741
742         private_output = (tdm_private_client_output*)output;
743         private_client = private_output->private_client;
744
745         pthread_mutex_lock(&private_client->lock);
746
747         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
748                 if (h->func != func || h->user_data != user_data)
749                         continue;
750
751                 LIST_DEL(&h->link);
752                 free(h);
753
754                 if (LIST_IS_EMPTY(&private_output->change_handler_list))
755                         wl_tdm_output_watch_output_changes(private_output->output, 0);
756
757                 pthread_mutex_unlock(&private_client->lock);
758
759                 return;
760         }
761
762         pthread_mutex_unlock(&private_client->lock);
763 }
764
765 tdm_error
766 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
767 {
768         tdm_private_client_output *private_output;
769         tdm_private_client *private_client;
770
771         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
772         TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
773
774         private_output = (tdm_private_client_output*)output;
775         private_client = private_output->private_client;
776
777         pthread_mutex_lock(&private_client->lock);
778
779         if (private_output->watch_output_changes) {
780                 *refresh = private_output->refresh;
781                 pthread_mutex_unlock(&private_client->lock);
782                 return TDM_ERROR_NONE;
783         }
784
785         wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
786         wl_tdm_output_get_mode(private_output->output);
787         wl_display_roundtrip_queue(private_client->display, private_client->queue);
788         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
789
790         *refresh = private_output->refresh;
791
792         pthread_mutex_unlock(&private_client->lock);
793
794         return TDM_ERROR_NONE;
795 }
796
797 tdm_error
798 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
799 {
800         tdm_private_client_output *private_output;
801         tdm_private_client *private_client;
802
803         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
804         TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
805
806         private_output = (tdm_private_client_output*)output;
807         private_client = private_output->private_client;
808
809         pthread_mutex_lock(&private_client->lock);
810
811         if (private_output->watch_output_changes) {
812                 *status = private_output->connection;
813                 pthread_mutex_unlock(&private_client->lock);
814                 return TDM_ERROR_NONE;
815         }
816
817         wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
818         wl_tdm_output_get_connection(private_output->output);
819         wl_display_roundtrip_queue(private_client->display, private_client->queue);
820         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
821
822         *status = private_output->connection;
823
824         pthread_mutex_unlock(&private_client->lock);
825
826         return TDM_ERROR_NONE;
827 }
828
829 tdm_error
830 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
831 {
832         tdm_private_client_output *private_output;
833         tdm_private_client *private_client;
834
835         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
836         TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
837
838         private_output = (tdm_private_client_output*)output;
839         private_client = private_output->private_client;
840
841         if (private_output->watch_output_changes) {
842                 *dpms = private_output->dpms;
843                 return TDM_ERROR_NONE;
844         }
845
846         pthread_mutex_lock(&private_client->lock);
847
848         wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
849         wl_tdm_output_get_dpms(private_output->output);
850         wl_display_roundtrip_queue(private_client->display, private_client->queue);
851         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
852
853         *dpms = private_output->dpms;
854
855         pthread_mutex_unlock(&private_client->lock);
856
857         return TDM_ERROR_NONE;
858 }
859
860 tdm_client_vblank*
861 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
862 {
863         tdm_private_client *private_client;
864         tdm_private_client_output *private_output;
865         tdm_private_client_vblank *private_vblank;
866         struct wl_proxy *wrapper;
867
868         if (error)
869                 *error = TDM_ERROR_NONE;
870
871         if (!output) {
872                 TDM_ERR("'!output' failed");
873                 if (error)
874                         *error = TDM_ERROR_INVALID_PARAMETER;
875                 return NULL;
876         }
877
878         private_output = (tdm_private_client_output*)output;
879         private_client = private_output->private_client;
880
881         pthread_mutex_lock(&private_client->lock);
882
883         wrapper = wl_proxy_create_wrapper(private_output->output);
884         if (!wrapper) {
885                 TDM_ERR("create output_wrapper failed");
886                 if (error)
887                         *error = TDM_ERROR_OUT_OF_MEMORY;
888                 pthread_mutex_unlock(&private_client->lock);
889                 return NULL;
890         }
891
892         wl_proxy_set_queue(wrapper, private_client->queue);
893
894         private_vblank = calloc(1, sizeof *private_vblank);
895         if (!private_vblank) {
896                 /* LCOV_EXCL_START */
897
898                 TDM_ERR("alloc failed");
899                 wl_proxy_wrapper_destroy(wrapper);
900                 if (error)
901                         *error = TDM_ERROR_OUT_OF_MEMORY;
902                 pthread_mutex_unlock(&private_client->lock);
903                 return NULL;
904
905                 /* LCOV_EXCL_STOP */
906         }
907
908         private_vblank->private_output = private_output;
909
910         private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
911         wl_proxy_wrapper_destroy(wrapper);
912         if (!private_vblank->vblank) {
913                 /* LCOV_EXCL_START */
914
915                 TDM_ERR("couldn't create vblank resource");
916                 free(private_vblank);
917                 if (error)
918                         *error = TDM_ERROR_OUT_OF_MEMORY;
919                 pthread_mutex_unlock(&private_client->lock);
920                 return NULL;
921
922                 /* LCOV_EXCL_STOP */
923         }
924
925         /* initial value */
926         private_vblank->fps = private_output->refresh;
927         private_vblank->offset = 0;
928         private_vblank->enable_fake = 0;
929
930         LIST_INITHEAD(&private_vblank->wait_list);
931         LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
932
933         wl_tdm_vblank_add_listener(private_vblank->vblank,
934                                                            &tdm_client_vblank_listener, private_vblank);
935         wl_display_roundtrip_queue(private_client->display, private_client->queue);
936
937         wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
938
939         pthread_mutex_unlock(&private_client->lock);
940
941         return (tdm_client_vblank*)private_vblank;
942 }
943
944 void
945 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
946 {
947         tdm_private_client_vblank *private_vblank;
948         tdm_private_client *private_client;
949         tdm_client_wait_info *w = NULL, *ww = NULL;
950
951         TDM_RETURN_IF_FAIL(vblank != NULL);
952
953         private_vblank = vblank;
954         private_client = private_vblank->private_output->private_client;
955
956         pthread_mutex_lock(&private_client->lock);
957
958         LIST_DEL(&private_vblank->link);
959
960         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
961                 LIST_DEL(&w->link);
962                 free(w);
963         }
964
965         wl_tdm_vblank_destroy(private_vblank->vblank);
966
967         free(private_vblank);
968
969         pthread_mutex_unlock(&private_client->lock);
970 }
971
972 tdm_error
973 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
974 {
975         tdm_private_client_vblank *private_vblank;
976         tdm_private_client *private_client;
977
978         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
979
980         private_vblank = vblank;
981         private_client = private_vblank->private_output->private_client;
982
983         pthread_mutex_lock(&private_client->lock);
984
985         if (!name)
986                 name = TDM_VBLANK_DEFAULT_NAME;
987
988         strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
989         private_vblank->name[TDM_NAME_LEN - 1] = '\0';
990
991         wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
992
993         pthread_mutex_unlock(&private_client->lock);
994
995         return TDM_ERROR_NONE;
996 }
997
998 tdm_error
999 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
1000 {
1001         tdm_private_client_vblank *private_vblank;
1002         tdm_private_client *private_client;
1003
1004         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1005
1006         private_vblank = vblank;
1007         private_client = private_vblank->private_output->private_client;
1008
1009         pthread_mutex_lock(&private_client->lock);
1010         private_vblank->sync = sync;
1011         pthread_mutex_unlock(&private_client->lock);
1012
1013         return TDM_ERROR_NONE;
1014 }
1015
1016 tdm_error
1017 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
1018 {
1019         tdm_private_client_vblank *private_vblank;
1020         tdm_private_client *private_client;
1021
1022         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1023         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
1024
1025         private_vblank = vblank;
1026         private_client = private_vblank->private_output->private_client;
1027
1028         pthread_mutex_lock(&private_client->lock);
1029
1030         if (private_vblank->fps == fps) {
1031                 pthread_mutex_unlock(&private_client->lock);
1032                 return TDM_ERROR_NONE;
1033         }
1034
1035         private_vblank->fps = fps;
1036
1037         wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
1038
1039         pthread_mutex_unlock(&private_client->lock);
1040
1041         return TDM_ERROR_NONE;
1042 }
1043
1044 tdm_error
1045 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
1046 {
1047         tdm_private_client_vblank *private_vblank;
1048         tdm_private_client *private_client;
1049
1050         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1051
1052         private_vblank = vblank;
1053         TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
1054
1055         private_client = private_vblank->private_output->private_client;
1056
1057         pthread_mutex_lock(&private_client->lock);
1058
1059         if (private_vblank->offset == offset_ms) {
1060                 pthread_mutex_unlock(&private_client->lock);
1061                 return TDM_ERROR_NONE;
1062         }
1063
1064         private_vblank->offset = offset_ms;
1065
1066         wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
1067
1068         pthread_mutex_unlock(&private_client->lock);
1069
1070         return TDM_ERROR_NONE;
1071 }
1072
1073 tdm_error
1074 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
1075 {
1076         tdm_private_client_vblank *private_vblank;
1077         tdm_private_client *private_client;
1078
1079         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1080
1081         private_vblank = vblank;
1082         private_client = private_vblank->private_output->private_client;
1083
1084         pthread_mutex_lock(&private_client->lock);
1085
1086         if (private_vblank->enable_fake == enable_fake) {
1087                 pthread_mutex_unlock(&private_client->lock);
1088                 return TDM_ERROR_NONE;
1089         }
1090
1091         private_vblank->enable_fake = enable_fake;
1092
1093         wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
1094
1095         pthread_mutex_unlock(&private_client->lock);
1096
1097         return TDM_ERROR_NONE;
1098 }
1099
1100 tdm_error
1101 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
1102 {
1103         tdm_private_client *private_client;
1104         tdm_private_client_output *private_output;
1105         tdm_private_client_vblank *private_vblank;
1106         tdm_client_wait_info *w;
1107         struct timespec tp;
1108         unsigned int req_sec, req_usec;
1109         int ret = 0;
1110
1111         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1112         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1113         /* can't support "interval 0" and "getting current_msc" things because
1114          * there is a socket communication between TDM client and server. It's impossible
1115          * to return the current msc or sequence immediately.
1116          */
1117         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1118
1119         private_vblank = vblank;
1120         private_output = private_vblank->private_output;
1121         private_client = private_output->private_client;
1122
1123         pthread_mutex_lock(&private_client->lock);
1124
1125         if (!private_vblank->started)
1126                 private_vblank->started = 1;
1127
1128         if (private_output->watch_output_changes && !private_vblank->enable_fake) {
1129                 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1130                         TDM_ERR("output disconnected");
1131                         pthread_mutex_unlock(&private_client->lock);
1132                         return TDM_ERROR_OUTPUT_DISCONNECTED;
1133                 }
1134                 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1135                         TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1136                         pthread_mutex_unlock(&private_client->lock);
1137                         return TDM_ERROR_DPMS_OFF;
1138                 }
1139         }
1140
1141         w = calloc(1, sizeof *w);
1142         if (!w) {
1143                 /* LCOV_EXCL_START */
1144
1145                 TDM_ERR("alloc failed");
1146                 pthread_mutex_unlock(&private_client->lock);
1147                 return TDM_ERROR_OUT_OF_MEMORY;
1148
1149                 /* LCOV_EXCL_STOP */
1150         }
1151
1152         w->private_vblank = private_vblank;
1153         w->func = func;
1154         w->user_data = user_data;
1155
1156         LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1157         LIST_INITHEAD(&w->call_link);
1158
1159         clock_gettime(CLOCK_MONOTONIC, &tp);
1160         req_sec = (unsigned int)tp.tv_sec;
1161         req_usec = (unsigned int)(tp.tv_nsec / 1000);
1162
1163         w->req_id = ++private_output->req_id;
1164         w->req_time = TDM_TIME(req_sec, req_usec);
1165         w->need_free = (private_vblank->sync) ? 0 : 1;
1166
1167         wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
1168
1169         if (private_vblank->enable_ttrace)
1170                 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1171
1172         TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
1173                         vblank, interval, w->req_id, w->req_time);
1174
1175         private_vblank->req_time = w->req_time;
1176
1177         if (private_vblank->last_time >= w->req_time)
1178                 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1179
1180         if (!private_vblank->sync) {
1181                 wl_display_flush(private_client->display);
1182                 pthread_mutex_unlock(&private_client->lock);
1183                 return TDM_ERROR_NONE;
1184         }
1185
1186         /* LCOV_EXCL_START */
1187
1188         while (ret != -1 && !w->need_free)
1189                 ret = wl_display_dispatch(private_client->display);
1190
1191         clock_gettime(CLOCK_MONOTONIC, &tp);
1192         TDM_DBG("block during %d us",
1193                         ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1194                         - (req_sec * 1000000 + req_usec));
1195
1196         LIST_DEL(&w->link);
1197         free(w);
1198
1199         pthread_mutex_unlock(&private_client->lock);
1200
1201         return TDM_ERROR_NONE;
1202
1203         /* LCOV_EXCL_STOP */
1204 }
1205
1206 tdm_error
1207 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
1208                                                    tdm_client_vblank_handler func, void *user_data)
1209 {
1210         tdm_private_client *private_client;
1211         tdm_private_client_output *private_output;
1212         tdm_private_client_vblank *private_vblank;
1213         tdm_client_wait_info *w;
1214         struct timespec tp;
1215         unsigned int req_sec, req_usec;
1216         int ret = 0;
1217
1218         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1219         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1220
1221         private_vblank = vblank;
1222         private_output = private_vblank->private_output;
1223         private_client = private_output->private_client;
1224
1225         pthread_mutex_lock(&private_client->lock);
1226
1227         if (!private_vblank->started)
1228                 private_vblank->started = 1;
1229
1230         if (private_output->watch_output_changes && !private_vblank->enable_fake) {
1231                 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1232                         TDM_ERR("output disconnected");
1233                         pthread_mutex_unlock(&private_client->lock);
1234                         return TDM_ERROR_OUTPUT_DISCONNECTED;
1235                 }
1236                 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1237                         TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1238                         pthread_mutex_unlock(&private_client->lock);
1239                         return TDM_ERROR_DPMS_OFF;
1240                 }
1241         }
1242
1243         w = calloc(1, sizeof *w);
1244         if (!w) {
1245                 /* LCOV_EXCL_START */
1246
1247                 TDM_ERR("alloc failed");
1248                 pthread_mutex_unlock(&private_client->lock);
1249                 return TDM_ERROR_OUT_OF_MEMORY;
1250
1251                 /* LCOV_EXCL_STOP */
1252         }
1253
1254         w->private_vblank = private_vblank;
1255         w->func = func;
1256         w->user_data = user_data;
1257
1258         LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1259         LIST_INITHEAD(&w->call_link);
1260
1261         clock_gettime(CLOCK_MONOTONIC, &tp);
1262         req_sec = (unsigned int)tp.tv_sec;
1263         req_usec = (unsigned int)(tp.tv_nsec / 1000);
1264
1265         w->req_id = ++private_output->req_id;
1266         w->req_time = TDM_TIME(req_sec, req_usec);
1267         w->need_free = (private_vblank->sync) ? 0 : 1;
1268
1269         wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
1270
1271         if (private_vblank->enable_ttrace)
1272                 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1273
1274         TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
1275                         vblank, sequence, w->req_id, w->req_time);
1276
1277         private_vblank->req_time = w->req_time;
1278
1279         if (private_vblank->last_time >= w->req_time)
1280                 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1281
1282         if (!private_vblank->sync) {
1283                 wl_display_flush(private_client->display);
1284                 pthread_mutex_unlock(&private_client->lock);
1285                 return TDM_ERROR_NONE;
1286         }
1287
1288         /* LCOV_EXCL_START */
1289
1290         while (ret != -1 && !w->need_free)
1291                 ret = wl_display_dispatch(private_client->display);
1292
1293         clock_gettime(CLOCK_MONOTONIC, &tp);
1294         TDM_DBG("block during %d us",
1295                         ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1296                         - (req_sec * 1000000 + req_usec));
1297
1298         LIST_DEL(&w->link);
1299         free(w);
1300
1301         pthread_mutex_unlock(&private_client->lock);
1302
1303         return TDM_ERROR_NONE;
1304
1305         /* LCOV_EXCL_STOP */
1306 }