fbb875b203266a0e1461970bfe0162bc834a5822
[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 <boram1288.park@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 #define WL_HIDE_DEPRECATED
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <time.h>
46 #include <strings.h>
47 #include <poll.h>
48
49 #include <tdm-client-protocol.h>
50
51 #include "tdm_client.h"
52 #include "tdm_log.h"
53 #include "tdm_macro.h"
54 #include "tdm_list.h"
55 #include "tdm.h"
56 #include "tdm_private.h"
57 #include <tbm_surface_internal.h>
58
59 #define TDM_ARRAY_NTH_DATA(array, type, n) (((type*)((array)->data)) + n)
60
61 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
62 typedef struct _tdm_private_client_voutput tdm_private_client_voutput;
63
64 typedef struct _tdm_private_client {
65         pthread_mutex_t lock;
66
67         struct wl_display *display;
68         struct wl_event_queue *queue;
69         struct wl_registry *registry;
70         struct wl_tdm *tdm;
71         struct list_head output_list;
72         struct list_head voutput_list;
73
74         unsigned int enable_ttrace;
75         unsigned int stamp;
76
77         tdm_private_client_vblank *temp_vblank;
78 } tdm_private_client;
79
80 typedef struct _tdm_private_client_output {
81         struct list_head link;
82
83         tdm_private_client *private_client;
84
85         char name[TDM_NAME_LEN];
86         struct wl_tdm_output *output;
87         int width;
88         int height;
89         int refresh;
90         tdm_output_conn_status connection;
91         tdm_output_dpms dpms;
92         struct list_head vblank_list;
93         struct list_head change_handler_list;
94
95         unsigned int req_id;
96         unsigned int watch_output_changes;
97
98         tdm_private_client_voutput *voutput;
99 } tdm_private_client_output;
100
101 typedef struct _tdm_private_client_buffer {
102         struct list_head link;
103         struct wl_buffer *wl_buffer;
104 } tdm_private_client_buffer;
105
106 struct _tdm_private_client_voutput {
107         struct list_head link;
108         struct wl_tdm_voutput *wl_voutput;
109         struct list_head commit_handler_list;
110
111         struct {
112                 int count;
113                 tdm_client_output_mode *modes;
114         } available_modes;
115
116         unsigned int mmwidth;
117         unsigned int mmheight;
118
119         uint32_t msg;
120
121         struct list_head buffer_list;
122         tbm_bufmgr bufmgr;
123         tdm_private_client_buffer *attach_buffer;
124
125         tdm_private_client *private_client;
126         tdm_private_client_output *private_output;
127         char name[TDM_NAME_LEN];
128         int get_output;
129 };
130
131 struct _tdm_private_client_vblank {
132         tdm_private_client_output *private_output;
133
134         struct wl_tdm_vblank *vblank;
135         struct list_head wait_list;
136
137         char name[TDM_NAME_LEN];
138         unsigned int sync;
139         unsigned int fps;
140         int offset;
141         unsigned int enable_fake;
142         unsigned int enable_ttrace;
143
144         unsigned int started;
145         unsigned int stamp;
146
147         double req_time;
148         double last_time;
149
150         struct list_head link;
151 };
152
153 typedef struct _tdm_client_output_handler_info {
154         tdm_private_client_output *private_output;
155
156         tdm_client_output_change_handler func;
157         void *user_data;
158
159         struct list_head link;
160         struct list_head call_link;
161 } tdm_client_output_handler_info;
162
163 typedef struct _tdm_client_wait_info {
164         tdm_private_client_vblank *private_vblank;
165
166         tdm_client_vblank_handler func;
167         void *user_data;
168
169         unsigned int req_id;
170         double req_time;
171         int need_free;
172
173         struct list_head link;
174         struct list_head call_link;
175 } tdm_client_wait_info;
176
177 typedef struct _tdm_client_voutput_commit_handler_info {
178         tdm_private_client_voutput *private_voutput;
179
180         tdm_client_voutput_commit_handler func;
181         void *user_data;
182
183         struct list_head link;
184         struct list_head call_link;
185 } tdm_client_voutput_commit_handler_info;
186
187 static unsigned int
188 _tdm_client_check_wl_error(tdm_private_client *private_client, const char *func, int line)
189 {
190         uint32_t ec, id;
191         const struct wl_interface *intf;
192         int err;
193
194         err = wl_display_get_error(private_client->display);
195         if (!err)
196                 return false;
197
198         if (err == EINVAL || err == ENOMEM || err == EFAULT || err == EPROTO) {
199                 ec = wl_display_get_protocol_error(private_client->display, &intf, &id);
200                 TDM_ERR("[%s,%d] errno(%d) Got protocol error '%u' on interface '%s' (object '%u')",
201                                 func, line, err, ec, (intf) ? intf->name : "destroyed", id);
202         } else {
203                 TDM_ERR("[%s,%d] errno(%d)", func, line, err);
204         }
205
206         return true;
207 }
208
209 #define CHECK_WL_PROTOCOL_ERROR(pc)  _tdm_client_check_wl_error(pc, __FUNCTION__, __LINE__)
210
211 static void
212 _tdm_client_vblank_cb_stamp(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t stamp)
213 {
214         tdm_private_client_vblank *private_vblank = data;
215         tdm_private_client *private_client;
216
217         TDM_RETURN_IF_FAIL(private_vblank != NULL);
218
219         private_vblank->stamp = stamp;
220
221         TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
222         private_client = private_vblank->private_output->private_client;
223
224         private_client->stamp = stamp;
225 }
226
227 /* LCOV_EXCL_START */
228 static void
229 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
230                                                    uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
231                                                    uint32_t tv_usec, uint32_t error)
232 {
233         tdm_private_client_vblank *private_vblank = data;
234         tdm_private_client *private_client;
235         tdm_client_wait_info *w = NULL, *wait_info = NULL;
236
237         TDM_RETURN_IF_FAIL(private_vblank != NULL);
238
239         private_client = private_vblank->private_output->private_client;
240
241         private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
242
243         TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
244                         private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
245
246         LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
247                 if (w->req_id != req_id)
248                         continue;
249
250                 wait_info = w;
251                 break;
252         }
253
254         if (!wait_info) {
255                 TDM_ERR("no wait infomation for req_id(%d)", req_id);
256                 return;
257         }
258
259         if (private_vblank->enable_ttrace)
260                 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
261
262         if (wait_info->req_time >= private_vblank->last_time)
263                 TDM_WRN("'req(%.6f) < last(%.6f)' failed. error(%d)", wait_info->req_time, private_vblank->last_time, error);
264
265         if (wait_info->need_free)
266                 LIST_DEL(&wait_info->link);
267
268         if (wait_info->func) {
269                 pthread_mutex_unlock(&private_client->lock);
270                 wait_info->func(private_vblank, error, sequence, tv_sec, tv_usec, wait_info->user_data);
271                 pthread_mutex_lock(&private_client->lock);
272         }
273
274         if (wait_info->need_free)
275                 free(w);
276         else
277                 wait_info->need_free = 1;
278 }
279 /* LCOV_EXCL_STOP */
280
281 /* LCOV_EXCL_START */
282 static void
283 _tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
284 {
285         tdm_private_client_vblank *private_vblank = data;
286         tdm_private_client *private_client;
287
288         TDM_RETURN_IF_FAIL(private_vblank != NULL);
289
290         private_vblank->enable_ttrace = enable;
291
292         TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
293         private_client = private_vblank->private_output->private_client;
294
295         private_client->enable_ttrace = enable;
296 }
297 /* LCOV_EXCL_STOP */
298
299 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
300         _tdm_client_vblank_cb_stamp,
301         _tdm_client_vblank_cb_done,
302         _tdm_client_vblank_cb_ttrace,
303 };
304
305 static void
306 _tdm_client_output_destroy(tdm_private_client_output *private_output)
307 {
308         tdm_private_client_vblank *v = NULL, *vv = NULL;
309         tdm_client_output_handler_info *h = NULL, *hh = NULL;
310
311         LIST_DEL(&private_output->link);
312
313         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
314                 TDM_ERR("vblanks SHOULD be destroyed first!");
315                 LIST_DEL(&v->link);
316                 v->private_output = NULL;
317         }
318
319         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
320                 LIST_DEL(&h->link);
321                 free(h);
322         }
323
324         wl_tdm_output_destroy(private_output->output);
325
326         free(private_output);
327 }
328
329 static void
330 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
331                                                    uint32_t width, uint32_t height, uint32_t refresh, uint32_t error)
332 {
333         tdm_private_client_output *private_output = (tdm_private_client_output*)data;
334
335         TDM_RETURN_IF_FAIL(private_output != NULL);
336
337         private_output->width = width;
338         private_output->height = height;
339         private_output->refresh = refresh;
340
341         if (error != TDM_ERROR_NONE)
342                 TDM_INFO("mode event error: %d", error);
343
344         TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
345                         private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
346                         width, height, refresh);
347 }
348
349 static void
350 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
351 {
352         tdm_private_client_output *private_output = (tdm_private_client_output*)data;
353         tdm_private_client *private_client;
354         tdm_client_output_handler_info *h = NULL, *hh = NULL;
355         tdm_value v;
356         struct list_head call_list;
357
358         TDM_RETURN_IF_FAIL(private_output != NULL);
359
360         private_client = private_output->private_client;
361
362         if (private_output->connection == value)
363                 return;
364
365         private_output->connection = value;
366
367         if (error != TDM_ERROR_NONE)
368                 TDM_INFO("connection event error: %d", error);
369
370         TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
371                         private_output,
372                         wl_proxy_get_id((struct wl_proxy*)private_output->output),
373                         value);
374
375         LIST_INITHEAD(&call_list);
376
377         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
378                 LIST_ADDTAIL(&h->call_link, &call_list);
379         }
380
381         v.u32 = value;
382         pthread_mutex_unlock(&private_client->lock);
383         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
384                 if (h->func)
385                         h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
386         }
387         pthread_mutex_lock(&private_client->lock);
388 }
389
390 static void
391 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
392 {
393         tdm_private_client_output *private_output = (tdm_private_client_output*)data;
394         tdm_private_client *private_client;
395         tdm_client_output_handler_info *h = NULL, *hh = NULL;
396         tdm_value v;
397         struct list_head call_list;
398
399         TDM_RETURN_IF_FAIL(private_output != NULL);
400
401         private_client = private_output->private_client;
402
403         /* If value is extended value, we handle it as DPMS on in client side
404          * The extended DPMS value is valid only in server side.
405          * Or, need to export to client side also?
406          */
407         if (value > TDM_OUTPUT_DPMS_OFF)
408                 value = TDM_OUTPUT_DPMS_ON;
409
410         if (private_output->dpms == value)
411                 return;
412
413         private_output->dpms = value;
414
415         if (error != TDM_ERROR_NONE)
416                 TDM_INFO("dpms event error: %d", error);
417
418         TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
419                         private_output,
420                         wl_proxy_get_id((struct wl_proxy*)private_output->output),
421                         value);
422
423         LIST_INITHEAD(&call_list);
424
425         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
426                 LIST_ADDTAIL(&h->call_link, &call_list);
427         }
428
429         v.u32 = value;
430         pthread_mutex_unlock(&private_client->lock);
431         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
432                 if (h->func)
433                         h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
434         }
435         pthread_mutex_lock(&private_client->lock);
436 }
437
438 static const struct wl_tdm_output_listener tdm_client_output_listener = {
439         _tdm_client_output_cb_mode,
440         _tdm_client_output_cb_connection,
441         _tdm_client_output_cb_dpms,
442 };
443
444 static void
445 _tdm_client_cb_global(void *data, struct wl_registry *registry,
446                                           uint32_t name, const char *interface,
447                                           uint32_t version)
448 {
449         tdm_private_client *private_client = data;
450
451         if (strncmp(interface, "wl_tdm", 6) == 0) {
452                 private_client->tdm =
453                         wl_registry_bind(registry, name, &wl_tdm_interface, version);
454                 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
455
456                 wl_display_flush(private_client->display);
457         }
458 }
459
460 /* LCOV_EXCL_START */
461 static void
462 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
463 {
464 }
465 /* LCOV_EXCL_STOP */
466
467 static const struct wl_registry_listener tdm_client_registry_listener = {
468         _tdm_client_cb_global,
469         _tdm_client_cb_global_remove
470 };
471
472 tdm_client*
473 tdm_client_create(tdm_error *error)
474 {
475         tdm_private_client *private_client;
476
477         private_client = calloc(1, sizeof *private_client);
478         if (!private_client) {
479                 /* LCOV_EXCL_START */
480
481                 TDM_ERR("alloc failed");
482                 if (error)
483                         *error = TDM_ERROR_OUT_OF_MEMORY;
484                 return NULL;
485
486                 /* LCOV_EXCL_STOP */
487         }
488
489         if (pthread_mutex_init(&private_client->lock, NULL)) {
490                 TDM_ERR("mutex init failed: %m");
491                 free(private_client);
492                 if (error)
493                         *error = TDM_ERROR_OUT_OF_MEMORY;
494                 return NULL;
495         }
496
497         LIST_INITHEAD(&private_client->output_list);
498         LIST_INITHEAD(&private_client->voutput_list);
499
500         private_client->display = wl_display_connect("tdm-socket");
501         TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
502
503         private_client->queue = wl_display_create_queue(private_client->display);
504         TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
505
506         private_client->registry = wl_display_get_registry(private_client->display);
507         TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
508
509         wl_registry_add_listener(private_client->registry,
510                                                          &tdm_client_registry_listener, private_client);
511         wl_display_roundtrip(private_client->display);
512
513         if (CHECK_WL_PROTOCOL_ERROR(private_client))
514                 goto create_failed;
515
516         /* check global objects */
517         TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
518
519         if (error)
520                 *error = TDM_ERROR_NONE;
521
522         return (tdm_client*)private_client;
523 create_failed:
524         tdm_client_destroy((tdm_client*)private_client);
525         if (error)
526                 *error = TDM_ERROR_OPERATION_FAILED;
527         return NULL;
528 }
529
530 void
531 tdm_client_destroy(tdm_client *client)
532 {
533         tdm_private_client *private_client = (tdm_private_client*)client;
534         tdm_private_client_output *o = NULL, *oo = NULL;
535         tdm_private_client_voutput *vo = NULL, *voo = NULL;
536
537         if (!private_client)
538                 return;
539
540         pthread_mutex_lock(&private_client->lock);
541
542         if (private_client->temp_vblank) {
543                 pthread_mutex_unlock(&private_client->lock);
544                 tdm_client_vblank_destroy(private_client->temp_vblank);
545                 pthread_mutex_lock(&private_client->lock);
546         }
547
548         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
549                 _tdm_client_output_destroy(o);
550         }
551
552         LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_client->voutput_list, link) {
553                 tdm_client_voutput_destroy(vo);
554         }
555
556         if (private_client->tdm)
557                 wl_tdm_destroy(private_client->tdm);
558         if (private_client->registry)
559                 wl_registry_destroy(private_client->registry);
560         if (private_client->queue)
561                 wl_event_queue_destroy(private_client->queue);
562         if (private_client->display)
563                 wl_display_disconnect(private_client->display);
564
565         pthread_mutex_unlock(&private_client->lock);
566         pthread_mutex_destroy(&private_client->lock);
567
568         free(private_client);
569 }
570
571 tdm_error
572 tdm_client_get_fd(tdm_client *client, int *fd)
573 {
574         tdm_private_client *private_client;
575
576         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
577         TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
578
579         private_client = (tdm_private_client*)client;
580
581         pthread_mutex_lock(&private_client->lock);
582
583         *fd = wl_display_get_fd(private_client->display);
584
585         pthread_mutex_unlock(&private_client->lock);
586
587         if (*fd < 0)
588                 return TDM_ERROR_OPERATION_FAILED;
589
590         return TDM_ERROR_NONE;
591 }
592
593 tdm_error
594 tdm_client_handle_events(tdm_client *client)
595 {
596         tdm_private_client *private_client;
597
598         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
599
600         /* LCOV_EXCL_START */
601         private_client = (tdm_private_client*)client;
602
603         pthread_mutex_lock(&private_client->lock);
604
605         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
606                 pthread_mutex_unlock(&private_client->lock);
607                 return TDM_ERROR_PROTOCOL_ERROR;
608         }
609
610         if (private_client->enable_ttrace)
611                 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
612
613         wl_display_dispatch(private_client->display);
614
615         if (private_client->enable_ttrace)
616                 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
617
618         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
619                 pthread_mutex_unlock(&private_client->lock);
620                 return TDM_ERROR_PROTOCOL_ERROR;
621         }
622
623         pthread_mutex_unlock(&private_client->lock);
624
625         return TDM_ERROR_NONE;
626         /* LCOV_EXCL_STOP */
627 }
628
629 static int
630 _tdm_client_poll(struct wl_display *display, short int events, int timeout)
631 {
632         int ret;
633         struct pollfd pfd[1];
634
635         pfd[0].fd = wl_display_get_fd(display);
636         pfd[0].events = events;
637         do {
638                 ret = poll(pfd, 1, timeout);
639         } while (ret == -1 && errno == EINTR);
640
641         return ret;
642 }
643
644 static tdm_error
645 _tdm_client_dispatch_timeout(tdm_private_client *private_client, int timeout)
646 {
647         int ret;
648         struct wl_display *display = private_client->display;
649
650         if (wl_display_prepare_read(display) == -1) {
651                 if (wl_display_dispatch_pending(display) > 0)
652                         return TDM_ERROR_NONE;
653                 else
654                         return TDM_ERROR_OPERATION_FAILED;
655         }
656
657         while (true) {
658                 ret = wl_display_flush(display);
659
660                 if (ret != -1 || errno != EAGAIN)
661                         break;
662
663                 if (_tdm_client_poll(display, POLLOUT, -1) == -1) {
664                         wl_display_cancel_read(display);
665                         TDM_ERR("_tdm_client_poll failed");
666                         return TDM_ERROR_OPERATION_FAILED;
667                 }
668         }
669
670         /* Don't stop if flushing hits an EPIPE; continue so we can read any
671          * protocol error that may have triggered it. */
672         if (ret < 0 && errno != EPIPE) {
673                 TDM_ERR("ret(%d) errno(%d)", ret, errno);
674                 wl_display_cancel_read(display);
675                 return TDM_ERROR_OPERATION_FAILED;
676         }
677
678         ret = _tdm_client_poll(display, POLLIN, timeout);
679         if (ret <= 0) {
680                 wl_display_cancel_read(display);
681                 if (ret == 0) {
682                         TDM_ERR("_tdm_client_poll timeout.");
683                         return TDM_ERROR_TIMEOUT;
684                 } else {
685                         TDM_ERR("_tdm_client_poll failed. (ret:%d)", ret);
686                         return TDM_ERROR_OPERATION_FAILED;
687                 }
688         }
689
690         if (wl_display_read_events(display) == -1) {
691                 TDM_ERR("wl_display_read_events failed");
692                 return TDM_ERROR_OPERATION_FAILED;
693         }
694
695         ret = wl_display_dispatch_pending(display);
696
697         if (ret < 0) {
698                 TDM_ERR("_tdm_client_dispatch_timeout failed");
699                 return TDM_ERROR_OPERATION_FAILED;
700         }
701
702         return TDM_ERROR_NONE;
703 }
704
705 tdm_error
706 tdm_client_handle_events_timeout(tdm_client *client, int ms_timeout)
707 {
708         tdm_private_client *private_client;
709         tdm_error ret;
710         TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
711
712         private_client = (tdm_private_client*)client;
713
714         pthread_mutex_lock(&private_client->lock);
715
716         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
717                 pthread_mutex_unlock(&private_client->lock);
718                 return TDM_ERROR_PROTOCOL_ERROR;
719         }
720
721         if (private_client->enable_ttrace)
722                 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
723
724         ret = _tdm_client_dispatch_timeout(private_client, ms_timeout);
725
726         if (private_client->enable_ttrace)
727                 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
728
729         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
730                 pthread_mutex_unlock(&private_client->lock);
731                 return TDM_ERROR_PROTOCOL_ERROR;
732         }
733
734         pthread_mutex_unlock(&private_client->lock);
735
736         return ret;
737 }
738
739 typedef struct _tdm_client_vblank_temp {
740         tdm_client_vblank_handler2 func;
741         void *user_data;
742 } tdm_client_vblank_temp;
743
744 /* LCOV_EXCL_START */
745 static void
746 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
747                                                                 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
748 {
749         tdm_client_vblank_temp *vblank_temp = user_data;
750
751         TDM_RETURN_IF_FAIL(vblank_temp != NULL);
752         TDM_RETURN_IF_FAIL(vblank != NULL);
753
754         if (vblank_temp->func)
755                 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
756
757         free(vblank_temp);
758 }
759 /* LCOV_EXCL_STOP */
760
761 /* LCOV_EXCL_START */ /* deprecated */
762 tdm_error
763 tdm_client_wait_vblank(tdm_client *client, char *name,
764                                            int sw_timer, int interval, int sync,
765                                            tdm_client_vblank_handler2 func, void *user_data)
766 {
767         tdm_private_client *private_client = (tdm_private_client*)client;
768         tdm_client_output *output;
769         tdm_client_vblank_temp *vblank_temp;
770         tdm_error ret;
771
772         TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
773         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
774         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
775
776         if (CHECK_WL_PROTOCOL_ERROR(private_client))
777                 return TDM_ERROR_PROTOCOL_ERROR;
778
779         if (!private_client->temp_vblank) {
780                 output = tdm_client_get_output(client, name, &ret);
781                 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
782
783                 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
784                 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
785         }
786
787         tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
788         tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
789
790         vblank_temp = calloc(1, sizeof *vblank_temp);
791         TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
792
793         vblank_temp->func = func;
794         vblank_temp->user_data = user_data;
795
796         return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
797 }
798 /* LCOV_EXCL_STOP */
799
800 tdm_client_output*
801 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
802 {
803         tdm_private_client *private_client;
804         tdm_private_client_output *private_output = NULL;
805         struct wl_proxy *wrapper;
806
807         if (error)
808                 *error = TDM_ERROR_NONE;
809
810         if (!client) {
811                 TDM_ERR("'!client' failed");
812                 if (error)
813                         *error = TDM_ERROR_INVALID_PARAMETER;
814                 return NULL;
815         }
816
817         private_client = (tdm_private_client*)client;
818
819         pthread_mutex_lock(&private_client->lock);
820
821         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
822                 if (error)
823                         *error = TDM_ERROR_PROTOCOL_ERROR;
824                 pthread_mutex_unlock(&private_client->lock);
825                 return NULL;
826         }
827
828         if (!name) {
829                 name = "primary";
830         } else if (!strncmp(name, "primary", 7) || !strncmp(name, "default", 7)) {
831                 TDM_DBG("get primary or default output");
832         } else {
833                 tdm_private_client_voutput *private_voutput = NULL;
834                 int find = 0;
835
836                 LIST_FOR_EACH_ENTRY(private_voutput, &private_client->voutput_list, link) {
837                         if (!strncmp(private_voutput->name, name, TDM_NAME_LEN)) {
838                                 find = 1;
839                                 break;
840                         }
841                 }
842                 if (!find) {
843                         if (error)
844                                 *error = TDM_ERROR_INVALID_PARAMETER;
845                         pthread_mutex_unlock(&private_client->lock);
846                         return NULL;
847                 }
848         }
849
850         LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
851                 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
852                         pthread_mutex_unlock(&private_client->lock);
853                         return (tdm_client_output*)private_output;
854                 }
855         }
856
857         wrapper = wl_proxy_create_wrapper(private_client->tdm);
858         if (!wrapper) {
859                 TDM_ERR("create output_wrapper failed");
860                 if (error)
861                         *error = TDM_ERROR_OUT_OF_MEMORY;
862                 pthread_mutex_unlock(&private_client->lock);
863                 return NULL;
864         }
865
866         wl_proxy_set_queue(wrapper, private_client->queue);
867
868         private_output = calloc(1, sizeof *private_output);
869         if (!private_output) {
870                 /* LCOV_EXCL_START */
871                 wl_proxy_wrapper_destroy(wrapper);
872                 TDM_ERR("alloc failed");
873                 if (error)
874                         *error = TDM_ERROR_OUT_OF_MEMORY;
875                 pthread_mutex_unlock(&private_client->lock);
876                 return NULL;
877
878                 /* LCOV_EXCL_STOP */
879         }
880
881         private_output->private_client = private_client;
882
883         snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
884         private_output->output = wl_tdm_create_output((struct wl_tdm *)wrapper, private_output->name);
885         wl_proxy_wrapper_destroy(wrapper);
886         if (!private_output->output) {
887                 /* LCOV_EXCL_START */
888
889                 TDM_ERR("couldn't create output resource");
890                 free(private_output);
891                 if (error)
892                         *error = TDM_ERROR_OUT_OF_MEMORY;
893                 pthread_mutex_unlock(&private_client->lock);
894                 return NULL;
895
896                 /* LCOV_EXCL_STOP */
897         }
898
899         LIST_INITHEAD(&private_output->vblank_list);
900         LIST_INITHEAD(&private_output->change_handler_list);
901
902         wl_tdm_output_add_listener(private_output->output,
903                                                            &tdm_client_output_listener, private_output);
904         wl_display_roundtrip_queue(private_client->display, private_client->queue);
905
906         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
907
908         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
909                 wl_tdm_output_destroy(private_output->output);
910                 free(private_output);
911                 if (error)
912                         *error = TDM_ERROR_PROTOCOL_ERROR;
913                 pthread_mutex_unlock(&private_client->lock);
914                 return NULL;
915         }
916
917         LIST_ADDTAIL(&private_output->link, &private_client->output_list);
918
919         pthread_mutex_unlock(&private_client->lock);
920
921         return (tdm_client_output*)private_output;
922 }
923
924 tdm_error
925 tdm_client_output_add_change_handler(tdm_client_output *output,
926                                                                          tdm_client_output_change_handler func,
927                                                                          void *user_data)
928 {
929         tdm_private_client_output *private_output;
930         tdm_private_client *private_client;
931         tdm_client_output_handler_info *h = NULL;
932
933         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
934         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
935
936         private_output = (tdm_private_client_output*)output;
937         private_client = private_output->private_client;
938
939         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
940                 if (h->func == func && h->user_data == user_data) {
941                         TDM_ERR("can't add twice");
942                         return TDM_ERROR_BAD_REQUEST;
943                 }
944         }
945
946         h = calloc(1, sizeof *h);
947         TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
948
949         pthread_mutex_lock(&private_client->lock);
950
951         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
952                 free(h);
953                 pthread_mutex_unlock(&private_client->lock);
954                 return TDM_ERROR_PROTOCOL_ERROR;
955         }
956
957         if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
958                 wl_tdm_output_watch_output_changes(private_output->output, 1);
959                 wl_display_roundtrip_queue(private_client->display, private_client->queue);
960
961                 /* TODO: this is very tricky.
962                  * If a client adds the change_handler, we might be able to guess that
963                  * the client will watch the tdm client's fd and handle tdm events in
964                  * event loop. Otherwise, we CAN'T make sure if a client has event loop
965                  * which handles tdm events.
966                  */
967                 private_output->watch_output_changes = 1;
968         }
969
970         h->private_output = private_output;
971         h->func = func;
972         h->user_data = user_data;
973         LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
974         LIST_INITHEAD(&h->call_link);
975
976         pthread_mutex_unlock(&private_client->lock);
977
978         return TDM_ERROR_NONE;
979 }
980
981 void
982 tdm_client_output_remove_change_handler(tdm_client_output *output,
983                                                                                 tdm_client_output_change_handler func,
984                                                                                 void *user_data)
985 {
986         tdm_private_client_output *private_output;
987         tdm_private_client *private_client;
988         tdm_client_output_handler_info *h = NULL;
989
990         TDM_RETURN_IF_FAIL(output != NULL);
991         TDM_RETURN_IF_FAIL(func != NULL);
992
993         private_output = (tdm_private_client_output*)output;
994         private_client = private_output->private_client;
995
996         pthread_mutex_lock(&private_client->lock);
997
998         LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
999                 if (h->func != func || h->user_data != user_data)
1000                         continue;
1001
1002                 LIST_DEL(&h->link);
1003                 free(h);
1004
1005                 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
1006                         private_output->watch_output_changes = 0;
1007                         if (!CHECK_WL_PROTOCOL_ERROR(private_client)) {
1008                                 wl_tdm_output_watch_output_changes(private_output->output, 0);
1009                                 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1010                         }
1011                 }
1012
1013                 pthread_mutex_unlock(&private_client->lock);
1014
1015                 return;
1016         }
1017
1018         pthread_mutex_unlock(&private_client->lock);
1019 }
1020
1021 tdm_error
1022 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
1023 {
1024         tdm_private_client_output *private_output;
1025         tdm_private_client *private_client;
1026
1027         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1028         TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
1029
1030         private_output = (tdm_private_client_output*)output;
1031         private_client = private_output->private_client;
1032
1033         pthread_mutex_lock(&private_client->lock);
1034
1035         if (private_output->watch_output_changes) {
1036                 *refresh = private_output->refresh;
1037                 pthread_mutex_unlock(&private_client->lock);
1038                 return TDM_ERROR_NONE;
1039         }
1040
1041         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1042                 pthread_mutex_unlock(&private_client->lock);
1043                 return TDM_ERROR_PROTOCOL_ERROR;
1044         }
1045
1046         wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1047         wl_tdm_output_get_mode(private_output->output);
1048         wl_display_roundtrip_queue(private_client->display, private_client->queue);
1049         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1050
1051         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1052                 pthread_mutex_unlock(&private_client->lock);
1053                 return TDM_ERROR_PROTOCOL_ERROR;
1054         }
1055
1056         *refresh = private_output->refresh;
1057
1058         pthread_mutex_unlock(&private_client->lock);
1059
1060         return TDM_ERROR_NONE;
1061 }
1062
1063 tdm_error
1064 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
1065 {
1066         tdm_private_client_output *private_output;
1067         tdm_private_client *private_client;
1068
1069         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1070         TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
1071
1072         private_output = (tdm_private_client_output*)output;
1073         private_client = private_output->private_client;
1074
1075         pthread_mutex_lock(&private_client->lock);
1076
1077         if (private_output->watch_output_changes) {
1078                 *status = private_output->connection;
1079                 pthread_mutex_unlock(&private_client->lock);
1080                 return TDM_ERROR_NONE;
1081         }
1082
1083         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1084                 pthread_mutex_unlock(&private_client->lock);
1085                 return TDM_ERROR_PROTOCOL_ERROR;
1086         }
1087
1088         wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1089         wl_tdm_output_get_connection(private_output->output);
1090         wl_display_roundtrip_queue(private_client->display, private_client->queue);
1091         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1092
1093         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1094                 pthread_mutex_unlock(&private_client->lock);
1095                 return TDM_ERROR_PROTOCOL_ERROR;
1096         }
1097
1098         *status = private_output->connection;
1099
1100         pthread_mutex_unlock(&private_client->lock);
1101
1102         return TDM_ERROR_NONE;
1103 }
1104
1105 tdm_error
1106 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
1107 {
1108         tdm_private_client_output *private_output;
1109         tdm_private_client *private_client;
1110
1111         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1112         TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
1113
1114         private_output = (tdm_private_client_output*)output;
1115         private_client = private_output->private_client;
1116
1117         if (private_output->watch_output_changes) {
1118                 *dpms = private_output->dpms;
1119                 return TDM_ERROR_NONE;
1120         }
1121
1122         pthread_mutex_lock(&private_client->lock);
1123
1124         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1125                 pthread_mutex_unlock(&private_client->lock);
1126                 return TDM_ERROR_PROTOCOL_ERROR;
1127         }
1128
1129         wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1130         wl_tdm_output_get_dpms(private_output->output);
1131         wl_display_roundtrip_queue(private_client->display, private_client->queue);
1132         wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1133
1134         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1135                 pthread_mutex_unlock(&private_client->lock);
1136                 return TDM_ERROR_PROTOCOL_ERROR;
1137         }
1138
1139         *dpms = private_output->dpms;
1140
1141         pthread_mutex_unlock(&private_client->lock);
1142
1143         return TDM_ERROR_NONE;
1144 }
1145
1146 tdm_client_vblank*
1147 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
1148 {
1149         tdm_private_client *private_client;
1150         tdm_private_client_output *private_output;
1151         tdm_private_client_vblank *private_vblank;
1152         struct wl_proxy *wrapper;
1153
1154         if (error)
1155                 *error = TDM_ERROR_NONE;
1156
1157         if (!output) {
1158                 TDM_ERR("'!output' failed");
1159                 if (error)
1160                         *error = TDM_ERROR_INVALID_PARAMETER;
1161                 return NULL;
1162         }
1163
1164         private_output = (tdm_private_client_output*)output;
1165         private_client = private_output->private_client;
1166
1167         pthread_mutex_lock(&private_client->lock);
1168
1169         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1170                 if (error)
1171                         *error = TDM_ERROR_PROTOCOL_ERROR;
1172                 pthread_mutex_unlock(&private_client->lock);
1173                 return NULL;
1174         }
1175
1176         wrapper = wl_proxy_create_wrapper(private_output->output);
1177         if (!wrapper) {
1178                 TDM_ERR("create output_wrapper failed");
1179                 if (error)
1180                         *error = TDM_ERROR_OUT_OF_MEMORY;
1181                 pthread_mutex_unlock(&private_client->lock);
1182                 return NULL;
1183         }
1184
1185         wl_proxy_set_queue(wrapper, private_client->queue);
1186
1187         private_vblank = calloc(1, sizeof *private_vblank);
1188         if (!private_vblank) {
1189                 /* LCOV_EXCL_START */
1190
1191                 TDM_ERR("alloc failed");
1192                 wl_proxy_wrapper_destroy(wrapper);
1193                 if (error)
1194                         *error = TDM_ERROR_OUT_OF_MEMORY;
1195                 pthread_mutex_unlock(&private_client->lock);
1196                 return NULL;
1197
1198                 /* LCOV_EXCL_STOP */
1199         }
1200
1201         private_vblank->private_output = private_output;
1202
1203         private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
1204         wl_proxy_wrapper_destroy(wrapper);
1205         if (!private_vblank->vblank) {
1206                 /* LCOV_EXCL_START */
1207
1208                 TDM_ERR("couldn't create vblank resource");
1209                 free(private_vblank);
1210                 if (error)
1211                         *error = TDM_ERROR_OUT_OF_MEMORY;
1212                 pthread_mutex_unlock(&private_client->lock);
1213                 return NULL;
1214
1215                 /* LCOV_EXCL_STOP */
1216         }
1217
1218         /* initial value */
1219         private_vblank->fps = private_output->refresh;
1220         private_vblank->offset = 0;
1221         private_vblank->enable_fake = 0;
1222
1223         LIST_INITHEAD(&private_vblank->wait_list);
1224
1225         wl_tdm_vblank_add_listener(private_vblank->vblank,
1226                                                            &tdm_client_vblank_listener, private_vblank);
1227         wl_display_roundtrip_queue(private_client->display, private_client->queue);
1228
1229         wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
1230
1231         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1232                 wl_tdm_vblank_destroy(private_vblank->vblank);
1233                 free(private_vblank);
1234                 if (error)
1235                         *error = TDM_ERROR_PROTOCOL_ERROR;
1236                 pthread_mutex_unlock(&private_client->lock);
1237                 return NULL;
1238         }
1239
1240         LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
1241
1242         pthread_mutex_unlock(&private_client->lock);
1243
1244         return (tdm_client_vblank*)private_vblank;
1245 }
1246
1247 void
1248 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
1249 {
1250         tdm_private_client_vblank *private_vblank;
1251         tdm_private_client *private_client;
1252         tdm_client_wait_info *w = NULL, *ww = NULL;
1253
1254         TDM_RETURN_IF_FAIL(vblank != NULL);
1255
1256         private_vblank = vblank;
1257         private_client = private_vblank->private_output->private_client;
1258
1259         pthread_mutex_lock(&private_client->lock);
1260
1261         LIST_DEL(&private_vblank->link);
1262
1263         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
1264                 LIST_DEL(&w->link);
1265                 free(w);
1266         }
1267
1268         wl_tdm_vblank_destroy(private_vblank->vblank);
1269
1270         free(private_vblank);
1271
1272         pthread_mutex_unlock(&private_client->lock);
1273 }
1274
1275 tdm_error
1276 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
1277 {
1278         tdm_private_client_vblank *private_vblank;
1279         tdm_private_client *private_client;
1280
1281         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1282
1283         private_vblank = vblank;
1284         private_client = private_vblank->private_output->private_client;
1285
1286         pthread_mutex_lock(&private_client->lock);
1287
1288         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1289                 pthread_mutex_unlock(&private_client->lock);
1290                 return TDM_ERROR_PROTOCOL_ERROR;
1291         }
1292
1293         if (!name)
1294                 name = TDM_VBLANK_DEFAULT_NAME;
1295
1296         strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
1297         private_vblank->name[TDM_NAME_LEN - 1] = '\0';
1298
1299         wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
1300
1301         pthread_mutex_unlock(&private_client->lock);
1302
1303         return TDM_ERROR_NONE;
1304 }
1305
1306 tdm_error
1307 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
1308 {
1309         tdm_private_client_vblank *private_vblank;
1310         tdm_private_client *private_client;
1311
1312         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1313
1314         private_vblank = vblank;
1315         private_client = private_vblank->private_output->private_client;
1316
1317         pthread_mutex_lock(&private_client->lock);
1318         private_vblank->sync = sync;
1319         pthread_mutex_unlock(&private_client->lock);
1320
1321         return TDM_ERROR_NONE;
1322 }
1323
1324 tdm_error
1325 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
1326 {
1327         tdm_private_client_vblank *private_vblank;
1328         tdm_private_client *private_client;
1329
1330         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1331         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
1332
1333         private_vblank = vblank;
1334         private_client = private_vblank->private_output->private_client;
1335
1336         pthread_mutex_lock(&private_client->lock);
1337
1338         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1339                 pthread_mutex_unlock(&private_client->lock);
1340                 return TDM_ERROR_PROTOCOL_ERROR;
1341         }
1342
1343         if (private_vblank->fps == fps) {
1344                 pthread_mutex_unlock(&private_client->lock);
1345                 return TDM_ERROR_NONE;
1346         }
1347
1348         private_vblank->fps = fps;
1349
1350         wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
1351
1352         pthread_mutex_unlock(&private_client->lock);
1353
1354         return TDM_ERROR_NONE;
1355 }
1356
1357 tdm_error
1358 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
1359 {
1360         tdm_private_client_vblank *private_vblank;
1361         tdm_private_client *private_client;
1362
1363         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1364
1365         private_vblank = vblank;
1366         TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
1367
1368         private_client = private_vblank->private_output->private_client;
1369
1370         pthread_mutex_lock(&private_client->lock);
1371
1372         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1373                 pthread_mutex_unlock(&private_client->lock);
1374                 return TDM_ERROR_PROTOCOL_ERROR;
1375         }
1376
1377         if (private_vblank->offset == offset_ms) {
1378                 pthread_mutex_unlock(&private_client->lock);
1379                 return TDM_ERROR_NONE;
1380         }
1381
1382         private_vblank->offset = offset_ms;
1383
1384         wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
1385
1386         pthread_mutex_unlock(&private_client->lock);
1387
1388         return TDM_ERROR_NONE;
1389 }
1390
1391 tdm_error
1392 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
1393 {
1394         tdm_private_client_vblank *private_vblank;
1395         tdm_private_client *private_client;
1396
1397         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1398
1399         private_vblank = vblank;
1400         private_client = private_vblank->private_output->private_client;
1401
1402         pthread_mutex_lock(&private_client->lock);
1403
1404         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1405                 pthread_mutex_unlock(&private_client->lock);
1406                 return TDM_ERROR_PROTOCOL_ERROR;
1407         }
1408
1409         if (private_vblank->enable_fake == enable_fake) {
1410                 pthread_mutex_unlock(&private_client->lock);
1411                 return TDM_ERROR_NONE;
1412         }
1413
1414         private_vblank->enable_fake = enable_fake;
1415
1416         wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
1417
1418         pthread_mutex_unlock(&private_client->lock);
1419
1420         return TDM_ERROR_NONE;
1421 }
1422
1423 tdm_error
1424 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
1425 {
1426         tdm_private_client *private_client;
1427         tdm_private_client_output *private_output;
1428         tdm_private_client_vblank *private_vblank;
1429         tdm_client_wait_info *w;
1430         struct timespec tp;
1431         unsigned int req_sec, req_usec;
1432         int ret = 0;
1433
1434         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1435         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1436         /* can't support "interval 0" and "getting current_msc" things because
1437          * there is a socket communication between TDM client and server. It's impossible
1438          * to return the current msc or sequence immediately.
1439          */
1440         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1441
1442         private_vblank = vblank;
1443         private_output = private_vblank->private_output;
1444         private_client = private_output->private_client;
1445
1446         pthread_mutex_lock(&private_client->lock);
1447
1448         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1449                 pthread_mutex_unlock(&private_client->lock);
1450                 return TDM_ERROR_PROTOCOL_ERROR;
1451         }
1452
1453         if (!private_vblank->started)
1454                 private_vblank->started = 1;
1455
1456         if (!private_vblank->enable_fake) {
1457                 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1458                         TDM_ERR("output disconnected");
1459                         pthread_mutex_unlock(&private_client->lock);
1460                         return TDM_ERROR_OUTPUT_DISCONNECTED;
1461                 }
1462                 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1463                         TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1464                         pthread_mutex_unlock(&private_client->lock);
1465                         return TDM_ERROR_DPMS_OFF;
1466                 }
1467         }
1468
1469         w = calloc(1, sizeof *w);
1470         if (!w) {
1471                 /* LCOV_EXCL_START */
1472
1473                 TDM_ERR("alloc failed");
1474                 pthread_mutex_unlock(&private_client->lock);
1475                 return TDM_ERROR_OUT_OF_MEMORY;
1476
1477                 /* LCOV_EXCL_STOP */
1478         }
1479
1480         w->private_vblank = private_vblank;
1481         w->func = func;
1482         w->user_data = user_data;
1483
1484         LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1485         LIST_INITHEAD(&w->call_link);
1486
1487         clock_gettime(CLOCK_MONOTONIC, &tp);
1488         req_sec = (unsigned int)tp.tv_sec;
1489         req_usec = (unsigned int)(tp.tv_nsec / 1000);
1490
1491         w->req_id = ++private_output->req_id;
1492         w->req_time = TDM_TIME(req_sec, req_usec);
1493         w->need_free = (private_vblank->sync) ? 0 : 1;
1494
1495         wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
1496
1497         if (private_vblank->enable_ttrace)
1498                 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1499
1500         TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
1501                         vblank, interval, w->req_id, w->req_time);
1502
1503         private_vblank->req_time = w->req_time;
1504
1505         if (private_vblank->last_time >= w->req_time)
1506                 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1507
1508         if (!private_vblank->sync) {
1509                 wl_display_flush(private_client->display);
1510                 pthread_mutex_unlock(&private_client->lock);
1511                 return TDM_ERROR_NONE;
1512         }
1513
1514         /* LCOV_EXCL_START */
1515
1516         while (ret != -1 && !w->need_free)
1517                 ret = wl_display_dispatch(private_client->display);
1518
1519         clock_gettime(CLOCK_MONOTONIC, &tp);
1520         TDM_DBG("block during %d us",
1521                         ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1522                         - (req_sec * 1000000 + req_usec));
1523
1524         LIST_DEL(&w->link);
1525         free(w);
1526
1527         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1528                 pthread_mutex_unlock(&private_client->lock);
1529                 return TDM_ERROR_PROTOCOL_ERROR;
1530         }
1531
1532         pthread_mutex_unlock(&private_client->lock);
1533
1534         return TDM_ERROR_NONE;
1535
1536         /* LCOV_EXCL_STOP */
1537 }
1538
1539 tdm_error
1540 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
1541                                                    tdm_client_vblank_handler func, void *user_data)
1542 {
1543         tdm_private_client *private_client;
1544         tdm_private_client_output *private_output;
1545         tdm_private_client_vblank *private_vblank;
1546         tdm_client_wait_info *w;
1547         struct timespec tp;
1548         unsigned int req_sec, req_usec;
1549         int ret = 0;
1550
1551         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1552         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1553
1554         private_vblank = vblank;
1555         private_output = private_vblank->private_output;
1556         private_client = private_output->private_client;
1557
1558         pthread_mutex_lock(&private_client->lock);
1559
1560         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1561                 pthread_mutex_unlock(&private_client->lock);
1562                 return TDM_ERROR_PROTOCOL_ERROR;
1563         }
1564
1565         if (!private_vblank->started)
1566                 private_vblank->started = 1;
1567
1568         if (!private_vblank->enable_fake) {
1569                 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1570                         TDM_ERR("output disconnected");
1571                         pthread_mutex_unlock(&private_client->lock);
1572                         return TDM_ERROR_OUTPUT_DISCONNECTED;
1573                 }
1574                 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1575                         TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1576                         pthread_mutex_unlock(&private_client->lock);
1577                         return TDM_ERROR_DPMS_OFF;
1578                 }
1579         }
1580
1581         w = calloc(1, sizeof *w);
1582         if (!w) {
1583                 /* LCOV_EXCL_START */
1584
1585                 TDM_ERR("alloc failed");
1586                 pthread_mutex_unlock(&private_client->lock);
1587                 return TDM_ERROR_OUT_OF_MEMORY;
1588
1589                 /* LCOV_EXCL_STOP */
1590         }
1591
1592         w->private_vblank = private_vblank;
1593         w->func = func;
1594         w->user_data = user_data;
1595
1596         LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1597         LIST_INITHEAD(&w->call_link);
1598
1599         clock_gettime(CLOCK_MONOTONIC, &tp);
1600         req_sec = (unsigned int)tp.tv_sec;
1601         req_usec = (unsigned int)(tp.tv_nsec / 1000);
1602
1603         w->req_id = ++private_output->req_id;
1604         w->req_time = TDM_TIME(req_sec, req_usec);
1605         w->need_free = (private_vblank->sync) ? 0 : 1;
1606
1607         wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
1608
1609         if (private_vblank->enable_ttrace)
1610                 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1611
1612         TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
1613                         vblank, sequence, w->req_id, w->req_time);
1614
1615         private_vblank->req_time = w->req_time;
1616
1617         if (private_vblank->last_time >= w->req_time)
1618                 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1619
1620         if (!private_vblank->sync) {
1621                 wl_display_flush(private_client->display);
1622                 pthread_mutex_unlock(&private_client->lock);
1623                 return TDM_ERROR_NONE;
1624         }
1625
1626         /* LCOV_EXCL_START */
1627
1628         while (ret != -1 && !w->need_free)
1629                 ret = wl_display_dispatch(private_client->display);
1630
1631         clock_gettime(CLOCK_MONOTONIC, &tp);
1632         TDM_DBG("block during %d us",
1633                         ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1634                         - (req_sec * 1000000 + req_usec));
1635
1636         LIST_DEL(&w->link);
1637         free(w);
1638
1639         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1640                 pthread_mutex_unlock(&private_client->lock);
1641                 return TDM_ERROR_PROTOCOL_ERROR;
1642         }
1643
1644         pthread_mutex_unlock(&private_client->lock);
1645
1646         return TDM_ERROR_NONE;
1647
1648         /* LCOV_EXCL_STOP */
1649 }
1650
1651 unsigned int
1652 tdm_client_vblank_is_waiting(tdm_client_vblank *vblank)
1653 {
1654         tdm_private_client_vblank *private_vblank;
1655
1656         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, 0);
1657
1658         private_vblank = vblank;
1659
1660         return (LIST_LENGTH(&private_vblank->wait_list) > 0) ? 1 : 0;
1661 }
1662
1663 static tbm_surface_h
1664 _tdm_client_voutput_create_surface_from_param(tbm_bufmgr bufmgr,
1665                                                          int is_fd,
1666                                                          int32_t width,
1667                                                          int32_t height,
1668                                                          uint32_t format,
1669                                                          int32_t bpp,
1670                                                          int32_t size,
1671                                                          int32_t num_plane,
1672                                                          struct wl_array *plane_buf_idx,
1673                                                          struct wl_array *plane_offset,
1674                                                          struct wl_array *plane_stride,
1675                                                          struct wl_array *plane_size,
1676                                                          uint32_t flags,
1677                                                          int32_t num_buf,
1678                                                          uint32_t buf0,
1679                                                          uint32_t buf1,
1680                                                          uint32_t buf2)
1681 {
1682         int32_t names[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
1683         tbm_surface_info_s info = { 0, };
1684         tbm_bo bos[TBM_SURF_PLANE_MAX];
1685         int i, numPlane, numName;
1686         tbm_surface_h tbm_surface;
1687
1688         numPlane = tbm_surface_internal_get_num_planes(format);
1689         TDM_RETURN_VAL_IF_FAIL(numPlane == num_plane, NULL);
1690
1691         info.width = width;
1692         info.height = height;
1693         info.format = format;
1694         info.bpp = bpp;
1695         info.size = size;
1696         info.num_planes = numPlane;
1697
1698         /*Fill plane info*/
1699         for (i = 0; i < numPlane; i++) {
1700                 info.planes[i].offset = *TDM_ARRAY_NTH_DATA(plane_offset, int32_t, i);
1701                 info.planes[i].stride = *TDM_ARRAY_NTH_DATA(plane_stride, int32_t, i);
1702                 info.planes[i].size = *TDM_ARRAY_NTH_DATA(plane_size, int32_t, i);
1703         }
1704
1705         /*Fill buffer*/
1706         numName = num_buf;
1707         names[0] = buf0;
1708         names[1] = buf1;
1709         names[2] = buf2;
1710
1711         for (i = 0; i < numName; i++) {
1712                 if (is_fd)
1713                         bos[i] = tbm_bo_import_fd(bufmgr, names[i]);
1714                 else
1715                         bos[i] = tbm_bo_import(bufmgr, names[i]);
1716         }
1717
1718         tbm_surface = tbm_surface_internal_create_with_bos(&info, bos, numName);
1719         if (tbm_surface == NULL) {
1720                 if (is_fd) {
1721                         close(buf0);
1722                         close(buf1);
1723                         close(buf2);
1724                 }
1725                 return NULL;
1726         }
1727
1728         if (is_fd) {
1729                 close(buf0);
1730                 close(buf1);
1731                 close(buf2);
1732         }
1733
1734         for (i = 0; i < numName; i++)
1735                 tbm_bo_unref(bos[i]);
1736
1737         return tbm_surface;
1738 }
1739 static void
1740 tdm_client_voutput_cb_buffer_import_with_id(void *data,
1741                 struct wl_tdm_voutput *wl_voutput,
1742                 struct wl_buffer *wl_buffer,
1743                 int32_t width,
1744                 int32_t height,
1745                 uint32_t format,
1746                 int32_t bpp,
1747                 int32_t size,
1748                 int32_t num_plane,
1749                 struct wl_array *plane_buf_idx,
1750                 struct wl_array *plane_offset,
1751                 struct wl_array *plane_stride,
1752                 struct wl_array *plane_size,
1753                 uint32_t flags,
1754                 int32_t num_buf,
1755                 uint32_t buf0,
1756                 uint32_t buf1,
1757                 uint32_t buf2)
1758 {
1759         tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1760         tdm_private_client_buffer *buffer = NULL;
1761         tbm_surface_h tbm_surface;
1762
1763         TDM_RETURN_IF_FAIL(private_voutput != NULL);
1764
1765         buffer = calloc(1, sizeof *buffer);
1766         TDM_RETURN_IF_FAIL(buffer != NULL);
1767
1768         tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 0,
1769                               width, height, format, bpp, size,
1770                               num_plane,
1771                               plane_buf_idx, plane_offset, plane_stride, plane_size,
1772                               0,
1773                               num_buf,
1774                               buf0, buf1, buf2);
1775         TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
1776
1777         tbm_surface_internal_ref(tbm_surface);
1778         wl_buffer_set_user_data(wl_buffer, tbm_surface);
1779
1780         buffer->wl_buffer = wl_buffer;
1781
1782         LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
1783
1784         return;
1785
1786 fail:
1787         if (buffer)
1788                 free(buffer);
1789
1790         if (wl_buffer)
1791                 wl_buffer_destroy(wl_buffer);
1792 }
1793
1794 static void
1795 tdm_client_voutput_cb_buffer_import_with_fd(void *data,
1796                 struct wl_tdm_voutput *wl_voutput,
1797                 struct wl_buffer *wl_buffer,
1798                 int32_t width,
1799                 int32_t height,
1800                 uint32_t format,
1801                 int32_t bpp,
1802                 int32_t size,
1803                 int32_t num_plane,
1804                 struct wl_array *plane_buf_idx,
1805                 struct wl_array *plane_offset,
1806                 struct wl_array *plane_stride,
1807                 struct wl_array *plane_size,
1808                 uint32_t flags,
1809                 int32_t num_buf,
1810                 int32_t buf0,
1811                 int32_t buf1,
1812                 int32_t buf2)
1813 {
1814         tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1815         tdm_private_client_buffer *buffer = NULL;
1816         tbm_surface_h tbm_surface;
1817
1818         TDM_RETURN_IF_FAIL(private_voutput != NULL);
1819
1820         buffer = calloc(1, sizeof *buffer);
1821         TDM_RETURN_IF_FAIL(buffer != NULL);
1822
1823         tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 1,
1824                               width, height, format, bpp, size,
1825                               num_plane,
1826                               plane_buf_idx, plane_offset, plane_stride, plane_size,
1827                               0,
1828                               num_buf,
1829                               buf0, buf1, buf2);
1830         TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
1831
1832         tbm_surface_internal_ref(tbm_surface);
1833         wl_buffer_set_user_data(wl_buffer, tbm_surface);
1834
1835         buffer->wl_buffer = wl_buffer;
1836
1837         LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
1838
1839         return;
1840
1841 fail:
1842         if (buffer)
1843                 free(buffer);
1844
1845         if (wl_buffer)
1846                 wl_buffer_destroy(wl_buffer);
1847 }
1848
1849 static void
1850 tdm_client_voutput_cb_buffer_destroy(void *data,
1851                 struct wl_tdm_voutput *wl_voutput,
1852                 struct wl_buffer *wl_buffer)
1853 {
1854         tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1855         tdm_private_client_buffer *cb = NULL, *cbb = NULL;
1856         tbm_surface_h tbm_surface = NULL;
1857
1858         TDM_RETURN_IF_FAIL(private_voutput != NULL);
1859
1860         LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
1861                 if (wl_buffer == cb->wl_buffer) {
1862                         LIST_DEL(&cb->link);
1863
1864                         tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(wl_buffer);
1865                         if (tbm_surface)
1866                                 tbm_surface_internal_unref(tbm_surface);
1867
1868                         wl_buffer_set_user_data(wl_buffer, NULL);
1869                         wl_buffer_destroy(wl_buffer);
1870
1871                         free(cb);
1872
1873                         break;
1874                 }
1875         }
1876
1877         return;
1878 }
1879
1880 void
1881 tdm_client_voutput_cb_attach_buffer(void *data,
1882                 struct wl_tdm_voutput *wl_voutput,
1883                 struct wl_buffer *wl_buffer)
1884 {
1885         tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1886         tdm_private_client_buffer *cb = NULL, *cbb = NULL;
1887
1888         TDM_RETURN_IF_FAIL(private_voutput != NULL);
1889
1890         LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
1891                 if (wl_buffer == cb->wl_buffer) {
1892                         private_voutput->attach_buffer = cb;
1893                         break;
1894                 }
1895         }
1896
1897         return;
1898 }
1899
1900 void
1901 tdm_client_voutput_cb_commit(void *data, struct wl_tdm_voutput *wl_voutput)
1902 {
1903         tdm_private_client_voutput *private_voutput;
1904         tdm_private_client *private_client;
1905         tbm_surface_h buffer = NULL;
1906         tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
1907         struct list_head call_list;
1908
1909         private_voutput = (tdm_private_client_voutput *)data;
1910         TDM_RETURN_IF_FAIL(private_voutput != NULL);
1911         TDM_RETURN_IF_FAIL(private_voutput->attach_buffer != NULL);
1912
1913         buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
1914         TDM_RETURN_IF_FAIL(buffer != NULL);
1915
1916         tbm_surface_internal_ref(buffer);
1917
1918         private_client = private_voutput->private_client;
1919
1920         LIST_INITHEAD(&call_list);
1921
1922         LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
1923                 LIST_ADDTAIL(&h->call_link, &call_list);
1924         }
1925
1926         pthread_mutex_unlock(&private_client->lock);
1927         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
1928                 if (h->func)
1929                         h->func(private_voutput, buffer, h->user_data);
1930         }
1931
1932         /* if no added commit_handler call commit done immediately */
1933         if (LIST_IS_EMPTY(&private_voutput->commit_handler_list))
1934                 tdm_client_voutput_commit_done(private_voutput);
1935
1936         pthread_mutex_lock(&private_client->lock);
1937 }
1938
1939 void
1940 tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput, uint32_t msg)
1941 {
1942         tdm_private_client_voutput *private_voutput = data;
1943
1944         private_voutput->msg = msg;
1945 }
1946
1947 static const struct wl_tdm_voutput_listener tdm_client_voutput_lisntener = {
1948         tdm_client_voutput_cb_buffer_import_with_id,
1949         tdm_client_voutput_cb_buffer_import_with_fd,
1950         tdm_client_voutput_cb_buffer_destroy,
1951         tdm_client_voutput_cb_attach_buffer,
1952         tdm_client_voutput_cb_commit,
1953         tdm_client_voutput_cb_ack_message
1954 };
1955
1956 tdm_client_voutput *
1957 tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error)
1958 {
1959         tdm_private_client *private_client;
1960         tdm_private_client_output *private_output;
1961         tdm_private_client_voutput *private_voutput;
1962         struct wl_proxy *wrapper;
1963
1964         if (error)
1965                 *error = TDM_ERROR_NONE;
1966
1967         if (!client) {
1968                 TDM_ERR("'!client' failed");
1969                 if (error)
1970                         *error = TDM_ERROR_INVALID_PARAMETER;
1971                 return NULL;
1972         }
1973
1974         if (!name) {
1975                 TDM_ERR("'!name' failed");
1976                 if (error)
1977                         *error = TDM_ERROR_INVALID_PARAMETER;
1978                 return NULL;
1979         }
1980
1981         private_client = (tdm_private_client *)client;
1982
1983         pthread_mutex_lock(&private_client->lock);
1984
1985         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1986                 if (error)
1987                         *error = TDM_ERROR_PROTOCOL_ERROR;
1988                 pthread_mutex_unlock(&private_client->lock);
1989                 return NULL;
1990         }
1991
1992         LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
1993                 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
1994                         if (error)
1995                                 *error = TDM_ERROR_INVALID_PARAMETER; // FIXME define new error type.
1996                         pthread_mutex_unlock(&private_client->lock);
1997                         return NULL;
1998                 }
1999         }
2000
2001         wrapper = wl_proxy_create_wrapper(private_client->tdm);
2002         if (!wrapper) {
2003                 TDM_ERR("create virtual output wrapper failed");
2004                 if (error)
2005                         *error = TDM_ERROR_OUT_OF_MEMORY;
2006                 pthread_mutex_unlock(&private_client->lock);
2007                 return NULL;
2008         }
2009
2010         wl_proxy_set_queue(wrapper, private_client->queue);
2011
2012         private_voutput = calloc(1, sizeof *private_voutput);
2013         if (!private_voutput) {
2014                 /* LOCV_EXCL_START */
2015                 wl_proxy_wrapper_destroy(wrapper);
2016                 TDM_ERR("alloc failed");
2017                 if (error)
2018                         *error = TDM_ERROR_OUT_OF_MEMORY;
2019                 pthread_mutex_unlock(&private_client->lock);
2020                 return NULL;
2021                 /* LOCV_EXCL_STOP */
2022         }
2023
2024         private_voutput->bufmgr = tbm_bufmgr_init(-1);
2025         if (private_voutput->bufmgr == NULL) {
2026                 /* LCOV_EXCL_START */
2027                 TDM_ERR("fail tbm_bufmgr_init");
2028                 free(private_voutput);
2029                 if (error)
2030                         *error = TDM_ERROR_OUT_OF_MEMORY;
2031                 pthread_mutex_unlock(&private_client->lock);
2032                 return NULL;
2033                 /* LCOV_EXCL_STOP */
2034         }
2035
2036         LIST_INITHEAD(&private_voutput->commit_handler_list);
2037         LIST_INITHEAD(&private_voutput->buffer_list);
2038
2039         private_voutput->private_client = private_client;
2040         strncpy(private_voutput->name, name, TDM_NAME_LEN);
2041
2042         private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, name);
2043         wl_proxy_wrapper_destroy(wrapper);
2044         if (!private_voutput->wl_voutput) {
2045                 /* LCOV_EXCL_START */
2046                 TDM_ERR("couldn't create voutput resource");
2047                 free(private_voutput);
2048                 if (error)
2049                         *error = TDM_ERROR_OUT_OF_MEMORY;
2050                 pthread_mutex_unlock(&private_client->lock);
2051                 return NULL;
2052                 /* LCOV_EXCL_STOP */
2053         }
2054
2055         wl_tdm_voutput_add_listener(private_voutput->wl_voutput,
2056                                                                 &tdm_client_voutput_lisntener, private_voutput);
2057         wl_display_roundtrip_queue(private_client->display, private_client->queue);
2058
2059         wl_proxy_set_queue((struct wl_proxy *)private_voutput->wl_voutput, NULL);
2060
2061         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2062                 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2063                 free(private_voutput);
2064                 if (error)
2065                         *error = TDM_ERROR_PROTOCOL_ERROR;
2066                 pthread_mutex_unlock(&private_client->lock);
2067                 return NULL;
2068         }
2069
2070         if (private_voutput->msg != WL_TDM_VOUTPUT_MESSAGE_ADDED) {
2071                 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2072                 free(private_voutput);
2073                 if (error)
2074                         *error = TDM_ERROR_PROTOCOL_ERROR;      // FIXME add new error type.
2075                 pthread_mutex_unlock(&private_client->lock);
2076                 return NULL;
2077         }
2078
2079         LIST_ADDTAIL(&private_voutput->link, &private_client->voutput_list);
2080
2081         pthread_mutex_unlock(&private_client->lock);
2082
2083         return (tdm_client_voutput *)private_voutput;
2084 }
2085
2086 void
2087 tdm_client_voutput_destroy(tdm_client_voutput *voutput)
2088 {
2089         tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)voutput;
2090         tdm_private_client *private_client;
2091         tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
2092
2093         if (!private_voutput)
2094                 return;
2095         private_client = private_voutput->private_client;
2096
2097         pthread_mutex_lock(&private_client->lock);
2098
2099         if (!(LIST_IS_EMPTY(&private_voutput->buffer_list))) {
2100                 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
2101
2102                 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
2103                         tbm_surface_h tbm_surface = NULL;
2104
2105                         if (!cb) continue;
2106
2107                         LIST_DEL(&cb->link);
2108
2109                         tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(cb->wl_buffer);
2110                         if (tbm_surface)
2111                                 tbm_surface_internal_unref(tbm_surface);
2112
2113                         wl_buffer_set_user_data(cb->wl_buffer, NULL);
2114                         wl_buffer_destroy(cb->wl_buffer);
2115
2116                         free(cb);
2117                 }
2118         }
2119
2120         if (private_voutput->bufmgr)
2121                 tbm_bufmgr_deinit(private_voutput->bufmgr);
2122
2123         if (private_voutput->available_modes.modes)
2124                 free(private_voutput->available_modes.modes);
2125
2126         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_voutput->commit_handler_list, link) {
2127                 LIST_DEL(&h->link);
2128                 free(h);
2129         }
2130
2131         if (private_voutput->get_output)
2132                 _tdm_client_output_destroy(private_voutput->private_output);
2133
2134         wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2135
2136         LIST_DEL(&private_voutput->link);
2137
2138         free(private_voutput);
2139
2140         pthread_mutex_unlock(&private_client->lock);
2141 }
2142
2143 tdm_error
2144 tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_client_output_mode *modes, int count)
2145 {
2146         tdm_private_client_voutput *private_voutput;
2147         tdm_private_client *private_client;
2148         tdm_error ret = TDM_ERROR_NONE;
2149
2150         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2151
2152         if ((count > 0) && (modes == NULL))
2153                 return TDM_ERROR_INVALID_PARAMETER;
2154
2155         private_voutput = (tdm_private_client_voutput *)voutput;
2156         private_client = private_voutput->private_client;
2157
2158         if (!private_voutput->private_output) {
2159                 private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
2160                 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
2161         }
2162
2163         pthread_mutex_lock(&private_client->lock);
2164
2165         if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2166                 pthread_mutex_unlock(&private_client->lock);
2167                 return TDM_ERROR_BAD_REQUEST;
2168         }
2169
2170         if (private_voutput->available_modes.modes)
2171                 free(private_voutput->available_modes.modes);
2172
2173         private_voutput->available_modes.count = count;
2174
2175         if (count != 0) {
2176                 private_voutput->available_modes.modes = calloc(count, sizeof(tdm_client_output_mode));
2177                 memcpy(private_voutput->available_modes.modes, modes, sizeof(tdm_client_output_mode) * count);
2178         }
2179
2180         pthread_mutex_unlock(&private_client->lock);
2181
2182         return TDM_ERROR_NONE;
2183 }
2184
2185 tdm_error
2186 tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, unsigned int mmWidth, unsigned int mmHeight)
2187 {
2188         tdm_private_client_voutput *private_voutput;
2189         tdm_private_client *private_client;
2190         tdm_error ret = TDM_ERROR_NONE;
2191
2192         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2193         TDM_RETURN_VAL_IF_FAIL(mmWidth != 0, TDM_ERROR_INVALID_PARAMETER);
2194         TDM_RETURN_VAL_IF_FAIL(mmHeight != 0, TDM_ERROR_INVALID_PARAMETER);
2195
2196         private_voutput = (tdm_private_client_voutput *)voutput;
2197         private_client = private_voutput->private_client;
2198
2199         if (!private_voutput->private_output) {
2200                 private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
2201                 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
2202         }
2203
2204         pthread_mutex_lock(&private_client->lock);
2205
2206         if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2207                 pthread_mutex_unlock(&private_client->lock);
2208                 return TDM_ERROR_BAD_REQUEST;
2209         }
2210
2211         private_voutput->mmwidth = mmWidth;
2212         private_voutput->mmheight = mmHeight;
2213
2214         pthread_mutex_unlock(&private_client->lock);
2215
2216         return TDM_ERROR_NONE;
2217 }
2218
2219 tdm_error
2220 tdm_client_voutput_add_commit_handler(tdm_client_voutput *voutput,
2221                                                                           tdm_client_voutput_commit_handler func,
2222                                                                           void *user_data)
2223 {
2224         tdm_private_client_voutput *private_voutput;
2225         tdm_private_client *private_client;
2226         tdm_client_voutput_commit_handler_info *h = NULL;
2227
2228         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2229         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
2230
2231         private_voutput = (tdm_private_client_voutput *)voutput;
2232         private_client = private_voutput->private_client;
2233
2234         LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
2235                 if (h->func == func && h->user_data == user_data) {
2236                         TDM_ERR("can't add twice");
2237                         return TDM_ERROR_BAD_REQUEST;
2238                 }
2239         }
2240
2241         h = calloc(1, sizeof *h);
2242         TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
2243
2244         pthread_mutex_lock(&private_client->lock);
2245
2246         if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2247                 free(h);
2248                 pthread_mutex_unlock(&private_client->lock);
2249                 return TDM_ERROR_PROTOCOL_ERROR;
2250         }
2251
2252         h->private_voutput = private_voutput;
2253         h->func = func;
2254         h->user_data = user_data;
2255         LIST_ADDTAIL(&h->link, &private_voutput->commit_handler_list);
2256         LIST_INITHEAD(&h->call_link);
2257
2258         pthread_mutex_unlock(&private_client->lock);
2259
2260         return TDM_ERROR_NONE;
2261 }
2262
2263 void
2264 tdm_client_voutput_remove_commit_handler(tdm_client_voutput *voutput,
2265                                                                                  tdm_client_voutput_commit_handler func,
2266                                                                                  void *user_data)
2267 {
2268         tdm_private_client_voutput *private_voutput;
2269         tdm_private_client *private_client;
2270         tdm_client_voutput_commit_handler_info *h = NULL;
2271
2272         TDM_RETURN_IF_FAIL(voutput != NULL);
2273         TDM_RETURN_IF_FAIL(func != NULL);
2274
2275         private_voutput = (tdm_private_client_voutput *)voutput;
2276         private_client = private_voutput->private_client;
2277
2278         pthread_mutex_lock(&private_client->lock);
2279
2280         LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
2281                 if (h->func != func || h->user_data != user_data)
2282                         continue;
2283
2284                 LIST_DEL(&h->link);
2285                 free(h);
2286
2287                 pthread_mutex_unlock(&private_client->lock);
2288         }
2289
2290         pthread_mutex_unlock(&private_client->lock);
2291 }
2292
2293 tdm_error
2294 tdm_client_voutput_commit_done(tdm_client_voutput *voutput)
2295 {
2296         tdm_private_client_voutput *private_voutput;
2297         tdm_private_client *private_client;
2298         tbm_surface_h buffer = NULL;
2299
2300         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2301
2302         private_voutput = (tdm_private_client_voutput *)voutput;
2303         TDM_RETURN_VAL_IF_FAIL(private_voutput->attach_buffer != NULL, TDM_ERROR_NONE);
2304
2305         private_client = private_voutput->private_client;
2306
2307         pthread_mutex_lock(&private_client->lock);
2308
2309         buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
2310         tbm_surface_internal_unref(buffer);
2311         private_voutput->attach_buffer = NULL;
2312         wl_tdm_voutput_commit_done(private_voutput->wl_voutput);
2313
2314         pthread_mutex_unlock(&private_client->lock);
2315
2316         return TDM_ERROR_NONE;
2317 }
2318
2319 tdm_client_output *
2320 tdm_client_voutput_get_client_output(tdm_client_voutput *voutput, tdm_error *error)
2321 {
2322         tdm_private_client_voutput *private_voutput;
2323         tdm_private_client_output *private_output = NULL;
2324         tdm_private_client *private_client;
2325         tdm_error ret = TDM_ERROR_NONE;
2326
2327         if (error)
2328                 *error = TDM_ERROR_NONE;
2329
2330         if (!voutput) {
2331                 TDM_ERR("'!voutput' failed");
2332                 if (error)
2333                         *error = TDM_ERROR_INVALID_PARAMETER;
2334                 return NULL;
2335         }
2336
2337         private_voutput = (tdm_private_client_voutput *)voutput;
2338         private_client = private_voutput->private_client;
2339
2340         pthread_mutex_lock(&private_client->lock);
2341
2342         if (private_voutput->get_output) {
2343                 pthread_mutex_unlock(&private_client->lock);
2344                 return private_voutput->private_output;
2345         }
2346
2347         pthread_mutex_unlock(&private_client->lock);
2348         private_output = (tdm_private_client_output *)tdm_client_get_output(private_voutput->private_client, private_voutput->name, &ret);
2349         if (!private_output) {
2350                 TDM_ERR("tdm_client_voutput_get_client_output get private_output fail");
2351                 if (error)
2352                         *error = ret;
2353                 return NULL;
2354         }
2355         pthread_mutex_lock(&private_client->lock);
2356         private_output->voutput = private_voutput;
2357         private_voutput->private_output = private_output;
2358         private_voutput->get_output = 1;
2359
2360         pthread_mutex_unlock(&private_client->lock);
2361
2362         return private_output;
2363 }
2364
2365 void
2366 _tdm_client_voutput_send_available_modes(tdm_private_client_voutput *private_voutput)
2367 {
2368         tdm_client_output_mode *modes, *mode;
2369         struct wl_array array;
2370         int i, size;
2371
2372         modes = private_voutput->available_modes.modes;
2373         size = sizeof(tdm_client_output_mode);
2374
2375         wl_array_init(&array);
2376         for (i = 0; i < private_voutput->available_modes.count; i++) {
2377                 mode = wl_array_add(&array, size);
2378                 memcpy(mode, &modes[i], size);
2379         }
2380         wl_tdm_voutput_set_available_modes(private_voutput->wl_voutput, &array);
2381         wl_array_release(&array);
2382 }
2383
2384 tdm_error
2385 tdm_client_voutput_connect(tdm_client_voutput *voutput)
2386 {
2387         tdm_private_client_output *private_output = NULL;;
2388         tdm_private_client_voutput *private_voutput = NULL;
2389         tdm_private_client *private_client;
2390
2391         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2392
2393         private_voutput = (tdm_private_client_voutput *)voutput;
2394         private_client = private_voutput->private_client;
2395         private_output = private_voutput->private_output;
2396
2397         pthread_mutex_lock(&private_client->lock);
2398
2399         if (private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2400                 pthread_mutex_unlock(&private_client->lock);
2401                 return TDM_ERROR_NONE;
2402         }
2403
2404         if (!private_output->watch_output_changes)
2405                 private_output->connection = TDM_OUTPUT_CONN_STATUS_CONNECTED;
2406
2407         _tdm_client_voutput_send_available_modes(private_voutput);
2408
2409         wl_tdm_voutput_set_physical_size(private_voutput->wl_voutput, private_voutput->mmwidth, private_voutput->mmheight);
2410
2411         wl_tdm_voutput_connect(private_voutput->wl_voutput);
2412
2413         pthread_mutex_unlock(&private_client->lock);
2414
2415         return TDM_ERROR_NONE;
2416 }
2417
2418 tdm_error
2419 tdm_client_voutput_disconnect(tdm_client_voutput *voutput)
2420 {
2421         tdm_private_client_output *private_output = NULL;
2422         tdm_private_client_voutput *private_voutput = NULL;
2423         tdm_private_client *private_client;
2424
2425         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2426
2427         private_voutput = (tdm_private_client_voutput *)voutput;
2428         private_client = private_voutput->private_client;
2429         private_output = private_voutput->private_output;
2430
2431         pthread_mutex_lock(&private_client->lock);
2432
2433         if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2434                 pthread_mutex_unlock(&private_client->lock);
2435                 return TDM_ERROR_NONE;
2436         }
2437
2438         if (!private_output->watch_output_changes)
2439                 private_output->connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
2440
2441         wl_tdm_voutput_disconnect(private_voutput->wl_voutput);
2442
2443         pthread_mutex_unlock(&private_client->lock);
2444
2445         return TDM_ERROR_NONE;
2446 }
2447
2448
2449 tdm_error
2450 tdm_client_voutput_set_mode(tdm_client_voutput *voutput, int index)
2451 {
2452         tdm_private_client_output *private_output;
2453         tdm_private_client_voutput *private_voutput = NULL;
2454         tdm_private_client *private_client;
2455
2456         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2457         TDM_RETURN_VAL_IF_FAIL(index >= 0, TDM_ERROR_INVALID_PARAMETER);
2458
2459         private_voutput = (tdm_private_client_voutput *)voutput;
2460         private_client = private_voutput->private_client;
2461         private_output = private_voutput->private_output;
2462
2463         pthread_mutex_lock(&private_client->lock);
2464
2465         if (private_voutput->available_modes.count - 1 < index) {
2466                 pthread_mutex_unlock(&private_client->lock);
2467                 return TDM_ERROR_INVALID_PARAMETER;
2468         }
2469
2470         if ((private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) ||
2471                 (private_voutput->available_modes.count == 0)) {
2472                 pthread_mutex_unlock(&private_client->lock);
2473                 return TDM_ERROR_BAD_REQUEST;
2474         }
2475
2476         TDM_DBG("mode_set request : %d", index);
2477
2478         wl_tdm_voutput_set_mode(private_voutput->wl_voutput, index);
2479
2480         pthread_mutex_unlock(&private_client->lock);
2481
2482         return TDM_ERROR_NONE;
2483 }