1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
47 #include <tdm-client-protocol.h>
49 #include "tdm_client.h"
51 #include "tdm_macro.h"
54 #include "tdm_private.h"
56 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
58 typedef struct _tdm_private_client {
61 struct wl_display *display;
62 struct wl_event_queue *queue;
63 struct wl_registry *registry;
65 struct list_head output_list;
67 unsigned int enable_ttrace;
70 tdm_private_client_vblank *temp_vblank;
73 typedef struct _tdm_private_client_output {
74 tdm_private_client *private_client;
76 char name[TDM_NAME_LEN];
77 struct wl_tdm_output *output;
81 tdm_output_conn_status connection;
83 struct list_head vblank_list;
84 struct list_head change_handler_list;
87 unsigned int watch_output_changes;
89 struct list_head link;
90 } tdm_private_client_output;
92 typedef struct _tdm_private_client_voutput {
93 tdm_private_client_output base;
94 struct wl_tdm_voutput *wl_voutput;
96 } tdm_private_client_voutput;
98 struct _tdm_private_client_vblank {
99 tdm_private_client_output *private_output;
101 struct wl_tdm_vblank *vblank;
102 struct list_head wait_list;
104 char name[TDM_NAME_LEN];
108 unsigned int enable_fake;
109 unsigned int enable_ttrace;
111 unsigned int started;
117 struct list_head link;
120 typedef struct _tdm_client_output_handler_info {
121 tdm_private_client_output *private_output;
123 tdm_client_output_change_handler func;
126 struct list_head link;
127 struct list_head call_link;
128 } tdm_client_output_handler_info;
130 typedef struct _tdm_client_wait_info {
131 tdm_private_client_vblank *private_vblank;
133 tdm_client_vblank_handler func;
140 struct list_head link;
141 struct list_head call_link;
142 } tdm_client_wait_info;
145 _tdm_client_check_wl_error(tdm_private_client *private_client, const char *func, int line)
148 const struct wl_interface *intf;
151 err = wl_display_get_error(private_client->display);
155 if (err == EINVAL || err == ENOMEM || err == EFAULT || err == EPROTO) {
156 ec = wl_display_get_protocol_error(private_client->display, &intf, &id);
157 TDM_ERR("[%s,%d] errno(%d) Got protocol error '%u' on interface '%s' (object '%u')",
158 func, line, err, ec, (intf) ? intf->name : "destroyed", id);
160 TDM_ERR("[%s,%d] errno(%d)", func, line, err);
166 #define CHECK_WL_PROTOCOL_ERROR(pc) _tdm_client_check_wl_error(pc, __FUNCTION__, __LINE__)
169 _tdm_client_vblank_cb_stamp(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t stamp)
171 tdm_private_client_vblank *private_vblank = data;
172 tdm_private_client *private_client;
174 TDM_RETURN_IF_FAIL(private_vblank != NULL);
176 private_vblank->stamp = stamp;
178 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
179 private_client = private_vblank->private_output->private_client;
181 private_client->stamp = stamp;
184 /* LCOV_EXCL_START */
186 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
187 uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
188 uint32_t tv_usec, uint32_t error)
190 tdm_private_client_vblank *private_vblank = data;
191 tdm_private_client *private_client;
192 tdm_client_wait_info *w = NULL, *wait_info = NULL;
194 TDM_RETURN_IF_FAIL(private_vblank != NULL);
196 private_client = private_vblank->private_output->private_client;
198 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
200 TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
201 private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
203 LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
204 if (w->req_id != req_id)
212 TDM_ERR("no wait infomation for req_id(%d)", req_id);
216 if (private_vblank->enable_ttrace)
217 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
219 if (wait_info->req_time >= private_vblank->last_time)
220 TDM_WRN("'req(%.6f) < last(%.6f)' failed. error(%d)", wait_info->req_time, private_vblank->last_time, error);
222 if (wait_info->need_free)
223 LIST_DEL(&wait_info->link);
225 if (wait_info->func) {
226 pthread_mutex_unlock(&private_client->lock);
227 wait_info->func(private_vblank, error, sequence, tv_sec, tv_usec, wait_info->user_data);
228 pthread_mutex_lock(&private_client->lock);
231 if (wait_info->need_free)
234 wait_info->need_free = 1;
238 /* LCOV_EXCL_START */
240 _tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
242 tdm_private_client_vblank *private_vblank = data;
243 tdm_private_client *private_client;
245 TDM_RETURN_IF_FAIL(private_vblank != NULL);
247 private_vblank->enable_ttrace = enable;
249 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
250 private_client = private_vblank->private_output->private_client;
252 private_client->enable_ttrace = enable;
256 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
257 _tdm_client_vblank_cb_stamp,
258 _tdm_client_vblank_cb_done,
259 _tdm_client_vblank_cb_ttrace,
263 _tdm_client_output_destroy(tdm_private_client_output *private_output)
265 tdm_private_client_vblank *v = NULL, *vv = NULL;
266 tdm_client_output_handler_info *h = NULL, *hh = NULL;
268 LIST_DEL(&private_output->link);
270 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
271 TDM_ERR("vblanks SHOULD be destroyed first!");
273 v->private_output = NULL;
276 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
281 wl_tdm_output_destroy(private_output->output);
283 free(private_output);
287 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
288 uint32_t width, uint32_t height, uint32_t refresh, uint32_t error)
290 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
292 TDM_RETURN_IF_FAIL(private_output != NULL);
294 private_output->width = width;
295 private_output->height = height;
296 private_output->refresh = refresh;
298 if (error != TDM_ERROR_NONE)
299 TDM_INFO("mode event error: %d", error);
301 TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
302 private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
303 width, height, refresh);
307 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
309 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
310 tdm_private_client *private_client;
311 tdm_client_output_handler_info *h = NULL, *hh = NULL;
313 struct list_head call_list;
315 TDM_RETURN_IF_FAIL(private_output != NULL);
317 private_client = private_output->private_client;
319 if (private_output->connection == value)
322 private_output->connection = value;
324 if (error != TDM_ERROR_NONE)
325 TDM_INFO("connection event error: %d", error);
327 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
329 wl_proxy_get_id((struct wl_proxy*)private_output->output),
332 LIST_INITHEAD(&call_list);
334 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
335 LIST_ADDTAIL(&h->call_link, &call_list);
339 pthread_mutex_unlock(&private_client->lock);
340 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
342 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
344 pthread_mutex_lock(&private_client->lock);
348 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
350 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
351 tdm_private_client *private_client;
352 tdm_client_output_handler_info *h = NULL, *hh = NULL;
354 struct list_head call_list;
356 TDM_RETURN_IF_FAIL(private_output != NULL);
358 private_client = private_output->private_client;
360 /* If value is extended value, we handle it as DPMS on in client side
361 * The extended DPMS value is valid only in server side.
362 * Or, need to export to client side also?
364 if (value > TDM_OUTPUT_DPMS_OFF)
365 value = TDM_OUTPUT_DPMS_ON;
367 if (private_output->dpms == value)
370 private_output->dpms = value;
372 if (error != TDM_ERROR_NONE)
373 TDM_INFO("dpms event error: %d", error);
375 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
377 wl_proxy_get_id((struct wl_proxy*)private_output->output),
380 LIST_INITHEAD(&call_list);
382 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
383 LIST_ADDTAIL(&h->call_link, &call_list);
387 pthread_mutex_unlock(&private_client->lock);
388 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
390 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
392 pthread_mutex_lock(&private_client->lock);
395 static const struct wl_tdm_output_listener tdm_client_output_listener = {
396 _tdm_client_output_cb_mode,
397 _tdm_client_output_cb_connection,
398 _tdm_client_output_cb_dpms,
402 _tdm_client_cb_global(void *data, struct wl_registry *registry,
403 uint32_t name, const char *interface,
406 tdm_private_client *private_client = data;
408 if (strncmp(interface, "wl_tdm", 6) == 0) {
409 private_client->tdm =
410 wl_registry_bind(registry, name, &wl_tdm_interface, version);
411 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
413 wl_display_flush(private_client->display);
417 /* LCOV_EXCL_START */
419 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
424 static const struct wl_registry_listener tdm_client_registry_listener = {
425 _tdm_client_cb_global,
426 _tdm_client_cb_global_remove
430 tdm_client_create(tdm_error *error)
432 tdm_private_client *private_client;
434 private_client = calloc(1, sizeof *private_client);
435 if (!private_client) {
436 /* LCOV_EXCL_START */
438 TDM_ERR("alloc failed");
440 *error = TDM_ERROR_OUT_OF_MEMORY;
446 if (pthread_mutex_init(&private_client->lock, NULL)) {
447 TDM_ERR("mutex init failed: %m");
448 free(private_client);
450 *error = TDM_ERROR_OUT_OF_MEMORY;
454 LIST_INITHEAD(&private_client->output_list);
456 private_client->display = wl_display_connect("tdm-socket");
457 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
459 private_client->queue = wl_display_create_queue(private_client->display);
460 TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
462 private_client->registry = wl_display_get_registry(private_client->display);
463 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
465 wl_registry_add_listener(private_client->registry,
466 &tdm_client_registry_listener, private_client);
467 wl_display_roundtrip(private_client->display);
469 if (CHECK_WL_PROTOCOL_ERROR(private_client))
472 /* check global objects */
473 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
476 *error = TDM_ERROR_NONE;
478 return (tdm_client*)private_client;
480 tdm_client_destroy((tdm_client*)private_client);
482 *error = TDM_ERROR_OPERATION_FAILED;
487 tdm_client_destroy(tdm_client *client)
489 tdm_private_client *private_client = (tdm_private_client*)client;
490 tdm_private_client_output *o = NULL, *oo = NULL;
495 pthread_mutex_lock(&private_client->lock);
497 if (private_client->temp_vblank) {
498 pthread_mutex_unlock(&private_client->lock);
499 tdm_client_vblank_destroy(private_client->temp_vblank);
500 pthread_mutex_lock(&private_client->lock);
503 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
504 _tdm_client_output_destroy(o);
507 if (private_client->tdm)
508 wl_tdm_destroy(private_client->tdm);
509 if (private_client->registry)
510 wl_registry_destroy(private_client->registry);
511 if (private_client->queue)
512 wl_event_queue_destroy(private_client->queue);
513 if (private_client->display)
514 wl_display_disconnect(private_client->display);
516 pthread_mutex_unlock(&private_client->lock);
517 pthread_mutex_destroy(&private_client->lock);
519 free(private_client);
523 tdm_client_get_fd(tdm_client *client, int *fd)
525 tdm_private_client *private_client;
527 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
528 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
530 private_client = (tdm_private_client*)client;
532 pthread_mutex_lock(&private_client->lock);
534 *fd = wl_display_get_fd(private_client->display);
536 pthread_mutex_unlock(&private_client->lock);
539 return TDM_ERROR_OPERATION_FAILED;
541 return TDM_ERROR_NONE;
545 tdm_client_handle_events(tdm_client *client)
547 tdm_private_client *private_client;
549 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
551 /* LCOV_EXCL_START */
552 private_client = (tdm_private_client*)client;
554 pthread_mutex_lock(&private_client->lock);
556 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
557 pthread_mutex_unlock(&private_client->lock);
558 return TDM_ERROR_PROTOCOL_ERROR;
561 if (private_client->enable_ttrace)
562 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
564 wl_display_dispatch(private_client->display);
566 if (private_client->enable_ttrace)
567 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
569 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
570 pthread_mutex_unlock(&private_client->lock);
571 return TDM_ERROR_PROTOCOL_ERROR;
574 pthread_mutex_unlock(&private_client->lock);
576 return TDM_ERROR_NONE;
581 _tdm_client_poll(struct wl_display *display, short int events, int timeout)
584 struct pollfd pfd[1];
586 pfd[0].fd = wl_display_get_fd(display);
587 pfd[0].events = events;
589 ret = poll(pfd, 1, timeout);
590 } while (ret == -1 && errno == EINTR);
596 _tdm_client_dispatch_timeout(tdm_private_client *private_client, int timeout)
599 struct wl_display *display = private_client->display;
601 if (wl_display_prepare_read(display) == -1) {
602 if (wl_display_dispatch_pending(display) > 0)
603 return TDM_ERROR_NONE;
605 return TDM_ERROR_OPERATION_FAILED;
609 ret = wl_display_flush(display);
611 if (ret != -1 || errno != EAGAIN)
614 if (_tdm_client_poll(display, POLLOUT, -1) == -1) {
615 wl_display_cancel_read(display);
616 TDM_ERR("_tdm_client_poll failed");
617 return TDM_ERROR_OPERATION_FAILED;
621 /* Don't stop if flushing hits an EPIPE; continue so we can read any
622 * protocol error that may have triggered it. */
623 if (ret < 0 && errno != EPIPE) {
624 TDM_ERR("ret(%d) errno(%d)", ret, errno);
625 wl_display_cancel_read(display);
626 return TDM_ERROR_OPERATION_FAILED;
629 ret = _tdm_client_poll(display, POLLIN, timeout);
631 wl_display_cancel_read(display);
633 TDM_ERR("_tdm_client_poll timeout.");
634 return TDM_ERROR_TIMEOUT;
636 TDM_ERR("_tdm_client_poll failed. (ret:%d)", ret);
637 return TDM_ERROR_OPERATION_FAILED;
641 if (wl_display_read_events(display) == -1) {
642 TDM_ERR("wl_display_read_events failed");
643 return TDM_ERROR_OPERATION_FAILED;
646 ret = wl_display_dispatch_pending(display);
649 TDM_ERR("_tdm_client_dispatch_timeout failed");
650 return TDM_ERROR_OPERATION_FAILED;
653 return TDM_ERROR_NONE;
657 tdm_client_handle_events_timeout(tdm_client *client, int ms_timeout)
659 tdm_private_client *private_client;
661 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
663 private_client = (tdm_private_client*)client;
665 pthread_mutex_lock(&private_client->lock);
667 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
668 pthread_mutex_unlock(&private_client->lock);
669 return TDM_ERROR_PROTOCOL_ERROR;
672 if (private_client->enable_ttrace)
673 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
675 ret = _tdm_client_dispatch_timeout(private_client, ms_timeout);
677 if (private_client->enable_ttrace)
678 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
680 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
681 pthread_mutex_unlock(&private_client->lock);
682 return TDM_ERROR_PROTOCOL_ERROR;
685 pthread_mutex_unlock(&private_client->lock);
690 typedef struct _tdm_client_vblank_temp {
691 tdm_client_vblank_handler2 func;
693 } tdm_client_vblank_temp;
695 /* LCOV_EXCL_START */
697 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
698 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
700 tdm_client_vblank_temp *vblank_temp = user_data;
702 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
703 TDM_RETURN_IF_FAIL(vblank != NULL);
705 if (vblank_temp->func)
706 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
712 /* LCOV_EXCL_START */ /* deprecated */
714 tdm_client_wait_vblank(tdm_client *client, char *name,
715 int sw_timer, int interval, int sync,
716 tdm_client_vblank_handler2 func, void *user_data)
718 tdm_private_client *private_client = (tdm_private_client*)client;
719 tdm_client_output *output;
720 tdm_client_vblank_temp *vblank_temp;
723 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
724 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
725 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
727 if (CHECK_WL_PROTOCOL_ERROR(private_client))
728 return TDM_ERROR_PROTOCOL_ERROR;
730 if (!private_client->temp_vblank) {
731 output = tdm_client_get_output(client, name, &ret);
732 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
734 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
735 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
738 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
739 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
741 vblank_temp = calloc(1, sizeof *vblank_temp);
742 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
744 vblank_temp->func = func;
745 vblank_temp->user_data = user_data;
747 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
752 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
754 tdm_private_client *private_client;
755 tdm_private_client_output *private_output = NULL;
756 struct wl_proxy *wrapper;
759 *error = TDM_ERROR_NONE;
762 TDM_ERR("'!client' failed");
764 *error = TDM_ERROR_INVALID_PARAMETER;
768 private_client = (tdm_private_client*)client;
770 pthread_mutex_lock(&private_client->lock);
772 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
774 *error = TDM_ERROR_PROTOCOL_ERROR;
775 pthread_mutex_unlock(&private_client->lock);
781 } else if (strncmp(name, "primary", 7) && strncmp(name, "default", 7)) {
783 *error = TDM_ERROR_INVALID_PARAMETER;
784 pthread_mutex_unlock(&private_client->lock);
788 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
789 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
790 pthread_mutex_unlock(&private_client->lock);
791 return (tdm_client_output*)private_output;
795 wrapper = wl_proxy_create_wrapper(private_client->tdm);
797 TDM_ERR("create output_wrapper failed");
799 *error = TDM_ERROR_OUT_OF_MEMORY;
800 pthread_mutex_unlock(&private_client->lock);
804 wl_proxy_set_queue(wrapper, private_client->queue);
806 private_output = calloc(1, sizeof *private_output);
807 if (!private_output) {
808 /* LCOV_EXCL_START */
809 wl_proxy_wrapper_destroy(wrapper);
810 TDM_ERR("alloc failed");
812 *error = TDM_ERROR_OUT_OF_MEMORY;
813 pthread_mutex_unlock(&private_client->lock);
819 private_output->private_client = private_client;
821 snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
822 private_output->output = wl_tdm_create_output((struct wl_tdm *)wrapper, private_output->name);
823 wl_proxy_wrapper_destroy(wrapper);
824 if (!private_output->output) {
825 /* LCOV_EXCL_START */
827 TDM_ERR("couldn't create output resource");
828 free(private_output);
830 *error = TDM_ERROR_OUT_OF_MEMORY;
831 pthread_mutex_unlock(&private_client->lock);
837 LIST_INITHEAD(&private_output->vblank_list);
838 LIST_INITHEAD(&private_output->change_handler_list);
840 wl_tdm_output_add_listener(private_output->output,
841 &tdm_client_output_listener, private_output);
842 wl_display_roundtrip_queue(private_client->display, private_client->queue);
844 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
846 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
847 wl_tdm_output_destroy(private_output->output);
848 free(private_output);
850 *error = TDM_ERROR_PROTOCOL_ERROR;
851 pthread_mutex_unlock(&private_client->lock);
855 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
857 pthread_mutex_unlock(&private_client->lock);
859 return (tdm_client_output*)private_output;
863 tdm_client_output_add_change_handler(tdm_client_output *output,
864 tdm_client_output_change_handler func,
867 tdm_private_client_output *private_output;
868 tdm_private_client *private_client;
869 tdm_client_output_handler_info *h = NULL;
871 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
872 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
874 private_output = (tdm_private_client_output*)output;
875 private_client = private_output->private_client;
877 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
878 if (h->func == func && h->user_data == user_data) {
879 TDM_ERR("can't add twice");
880 return TDM_ERROR_BAD_REQUEST;
884 h = calloc(1, sizeof *h);
885 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
887 pthread_mutex_lock(&private_client->lock);
889 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
891 pthread_mutex_unlock(&private_client->lock);
892 return TDM_ERROR_PROTOCOL_ERROR;
895 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
896 wl_tdm_output_watch_output_changes(private_output->output, 1);
897 wl_display_roundtrip_queue(private_client->display, private_client->queue);
899 /* TODO: this is very tricky.
900 * If a client adds the change_handler, we might be able to guess that
901 * the client will watch the tdm client's fd and handle tdm events in
902 * event loop. Otherwise, we CAN'T make sure if a client has event loop
903 * which handles tdm events.
905 private_output->watch_output_changes = 1;
908 h->private_output = private_output;
910 h->user_data = user_data;
911 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
912 LIST_INITHEAD(&h->call_link);
914 pthread_mutex_unlock(&private_client->lock);
916 return TDM_ERROR_NONE;
920 tdm_client_output_remove_change_handler(tdm_client_output *output,
921 tdm_client_output_change_handler func,
924 tdm_private_client_output *private_output;
925 tdm_private_client *private_client;
926 tdm_client_output_handler_info *h = NULL;
928 TDM_RETURN_IF_FAIL(output != NULL);
929 TDM_RETURN_IF_FAIL(func != NULL);
931 private_output = (tdm_private_client_output*)output;
932 private_client = private_output->private_client;
934 pthread_mutex_lock(&private_client->lock);
936 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
937 if (h->func != func || h->user_data != user_data)
943 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
944 private_output->watch_output_changes = 0;
945 if (!CHECK_WL_PROTOCOL_ERROR(private_client)) {
946 wl_tdm_output_watch_output_changes(private_output->output, 0);
947 wl_display_roundtrip_queue(private_client->display, private_client->queue);
951 pthread_mutex_unlock(&private_client->lock);
956 pthread_mutex_unlock(&private_client->lock);
960 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
962 tdm_private_client_output *private_output;
963 tdm_private_client *private_client;
965 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
966 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
968 private_output = (tdm_private_client_output*)output;
969 private_client = private_output->private_client;
971 pthread_mutex_lock(&private_client->lock);
973 if (private_output->watch_output_changes) {
974 *refresh = private_output->refresh;
975 pthread_mutex_unlock(&private_client->lock);
976 return TDM_ERROR_NONE;
979 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
980 pthread_mutex_unlock(&private_client->lock);
981 return TDM_ERROR_PROTOCOL_ERROR;
984 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
985 wl_tdm_output_get_mode(private_output->output);
986 wl_display_roundtrip_queue(private_client->display, private_client->queue);
987 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
989 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
990 pthread_mutex_unlock(&private_client->lock);
991 return TDM_ERROR_PROTOCOL_ERROR;
994 *refresh = private_output->refresh;
996 pthread_mutex_unlock(&private_client->lock);
998 return TDM_ERROR_NONE;
1002 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
1004 tdm_private_client_output *private_output;
1005 tdm_private_client *private_client;
1007 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1008 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
1010 private_output = (tdm_private_client_output*)output;
1011 private_client = private_output->private_client;
1013 pthread_mutex_lock(&private_client->lock);
1015 if (private_output->watch_output_changes) {
1016 *status = private_output->connection;
1017 pthread_mutex_unlock(&private_client->lock);
1018 return TDM_ERROR_NONE;
1021 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1022 pthread_mutex_unlock(&private_client->lock);
1023 return TDM_ERROR_PROTOCOL_ERROR;
1026 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1027 wl_tdm_output_get_connection(private_output->output);
1028 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1029 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1031 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1032 pthread_mutex_unlock(&private_client->lock);
1033 return TDM_ERROR_PROTOCOL_ERROR;
1036 *status = private_output->connection;
1038 pthread_mutex_unlock(&private_client->lock);
1040 return TDM_ERROR_NONE;
1044 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
1046 tdm_private_client_output *private_output;
1047 tdm_private_client *private_client;
1049 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1050 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
1052 private_output = (tdm_private_client_output*)output;
1053 private_client = private_output->private_client;
1055 if (private_output->watch_output_changes) {
1056 *dpms = private_output->dpms;
1057 return TDM_ERROR_NONE;
1060 pthread_mutex_lock(&private_client->lock);
1062 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1063 pthread_mutex_unlock(&private_client->lock);
1064 return TDM_ERROR_PROTOCOL_ERROR;
1067 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1068 wl_tdm_output_get_dpms(private_output->output);
1069 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1070 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1072 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1073 pthread_mutex_unlock(&private_client->lock);
1074 return TDM_ERROR_PROTOCOL_ERROR;
1077 *dpms = private_output->dpms;
1079 pthread_mutex_unlock(&private_client->lock);
1081 return TDM_ERROR_NONE;
1085 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
1087 tdm_private_client *private_client;
1088 tdm_private_client_output *private_output;
1089 tdm_private_client_vblank *private_vblank;
1090 struct wl_proxy *wrapper;
1093 *error = TDM_ERROR_NONE;
1096 TDM_ERR("'!output' failed");
1098 *error = TDM_ERROR_INVALID_PARAMETER;
1102 private_output = (tdm_private_client_output*)output;
1103 private_client = private_output->private_client;
1105 pthread_mutex_lock(&private_client->lock);
1107 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1109 *error = TDM_ERROR_PROTOCOL_ERROR;
1110 pthread_mutex_unlock(&private_client->lock);
1114 wrapper = wl_proxy_create_wrapper(private_output->output);
1116 TDM_ERR("create output_wrapper failed");
1118 *error = TDM_ERROR_OUT_OF_MEMORY;
1119 pthread_mutex_unlock(&private_client->lock);
1123 wl_proxy_set_queue(wrapper, private_client->queue);
1125 private_vblank = calloc(1, sizeof *private_vblank);
1126 if (!private_vblank) {
1127 /* LCOV_EXCL_START */
1129 TDM_ERR("alloc failed");
1130 wl_proxy_wrapper_destroy(wrapper);
1132 *error = TDM_ERROR_OUT_OF_MEMORY;
1133 pthread_mutex_unlock(&private_client->lock);
1136 /* LCOV_EXCL_STOP */
1139 private_vblank->private_output = private_output;
1141 private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
1142 wl_proxy_wrapper_destroy(wrapper);
1143 if (!private_vblank->vblank) {
1144 /* LCOV_EXCL_START */
1146 TDM_ERR("couldn't create vblank resource");
1147 free(private_vblank);
1149 *error = TDM_ERROR_OUT_OF_MEMORY;
1150 pthread_mutex_unlock(&private_client->lock);
1153 /* LCOV_EXCL_STOP */
1157 private_vblank->fps = private_output->refresh;
1158 private_vblank->offset = 0;
1159 private_vblank->enable_fake = 0;
1161 LIST_INITHEAD(&private_vblank->wait_list);
1163 wl_tdm_vblank_add_listener(private_vblank->vblank,
1164 &tdm_client_vblank_listener, private_vblank);
1165 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1167 wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
1169 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1170 wl_tdm_vblank_destroy(private_vblank->vblank);
1171 free(private_vblank);
1173 *error = TDM_ERROR_PROTOCOL_ERROR;
1174 pthread_mutex_unlock(&private_client->lock);
1178 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
1180 pthread_mutex_unlock(&private_client->lock);
1182 return (tdm_client_vblank*)private_vblank;
1186 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
1188 tdm_private_client_vblank *private_vblank;
1189 tdm_private_client *private_client;
1190 tdm_client_wait_info *w = NULL, *ww = NULL;
1192 TDM_RETURN_IF_FAIL(vblank != NULL);
1194 private_vblank = vblank;
1195 private_client = private_vblank->private_output->private_client;
1197 pthread_mutex_lock(&private_client->lock);
1199 LIST_DEL(&private_vblank->link);
1201 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
1206 wl_tdm_vblank_destroy(private_vblank->vblank);
1208 free(private_vblank);
1210 pthread_mutex_unlock(&private_client->lock);
1214 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
1216 tdm_private_client_vblank *private_vblank;
1217 tdm_private_client *private_client;
1219 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1221 private_vblank = vblank;
1222 private_client = private_vblank->private_output->private_client;
1224 pthread_mutex_lock(&private_client->lock);
1226 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1227 pthread_mutex_unlock(&private_client->lock);
1228 return TDM_ERROR_PROTOCOL_ERROR;
1232 name = TDM_VBLANK_DEFAULT_NAME;
1234 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
1235 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
1237 wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
1239 pthread_mutex_unlock(&private_client->lock);
1241 return TDM_ERROR_NONE;
1245 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
1247 tdm_private_client_vblank *private_vblank;
1248 tdm_private_client *private_client;
1250 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1252 private_vblank = vblank;
1253 private_client = private_vblank->private_output->private_client;
1255 pthread_mutex_lock(&private_client->lock);
1256 private_vblank->sync = sync;
1257 pthread_mutex_unlock(&private_client->lock);
1259 return TDM_ERROR_NONE;
1263 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
1265 tdm_private_client_vblank *private_vblank;
1266 tdm_private_client *private_client;
1268 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1269 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
1271 private_vblank = vblank;
1272 private_client = private_vblank->private_output->private_client;
1274 pthread_mutex_lock(&private_client->lock);
1276 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1277 pthread_mutex_unlock(&private_client->lock);
1278 return TDM_ERROR_PROTOCOL_ERROR;
1281 if (private_vblank->fps == fps) {
1282 pthread_mutex_unlock(&private_client->lock);
1283 return TDM_ERROR_NONE;
1286 private_vblank->fps = fps;
1288 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
1290 pthread_mutex_unlock(&private_client->lock);
1292 return TDM_ERROR_NONE;
1296 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
1298 tdm_private_client_vblank *private_vblank;
1299 tdm_private_client *private_client;
1301 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1303 private_vblank = vblank;
1304 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
1306 private_client = private_vblank->private_output->private_client;
1308 pthread_mutex_lock(&private_client->lock);
1310 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1311 pthread_mutex_unlock(&private_client->lock);
1312 return TDM_ERROR_PROTOCOL_ERROR;
1315 if (private_vblank->offset == offset_ms) {
1316 pthread_mutex_unlock(&private_client->lock);
1317 return TDM_ERROR_NONE;
1320 private_vblank->offset = offset_ms;
1322 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
1324 pthread_mutex_unlock(&private_client->lock);
1326 return TDM_ERROR_NONE;
1330 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
1332 tdm_private_client_vblank *private_vblank;
1333 tdm_private_client *private_client;
1335 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1337 private_vblank = vblank;
1338 private_client = private_vblank->private_output->private_client;
1340 pthread_mutex_lock(&private_client->lock);
1342 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1343 pthread_mutex_unlock(&private_client->lock);
1344 return TDM_ERROR_PROTOCOL_ERROR;
1347 if (private_vblank->enable_fake == enable_fake) {
1348 pthread_mutex_unlock(&private_client->lock);
1349 return TDM_ERROR_NONE;
1352 private_vblank->enable_fake = enable_fake;
1354 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
1356 pthread_mutex_unlock(&private_client->lock);
1358 return TDM_ERROR_NONE;
1362 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
1364 tdm_private_client *private_client;
1365 tdm_private_client_output *private_output;
1366 tdm_private_client_vblank *private_vblank;
1367 tdm_client_wait_info *w;
1369 unsigned int req_sec, req_usec;
1372 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1373 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1374 /* can't support "interval 0" and "getting current_msc" things because
1375 * there is a socket communication between TDM client and server. It's impossible
1376 * to return the current msc or sequence immediately.
1378 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1380 private_vblank = vblank;
1381 private_output = private_vblank->private_output;
1382 private_client = private_output->private_client;
1384 pthread_mutex_lock(&private_client->lock);
1386 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1387 pthread_mutex_unlock(&private_client->lock);
1388 return TDM_ERROR_PROTOCOL_ERROR;
1391 if (!private_vblank->started)
1392 private_vblank->started = 1;
1394 if (!private_vblank->enable_fake) {
1395 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1396 TDM_ERR("output disconnected");
1397 pthread_mutex_unlock(&private_client->lock);
1398 return TDM_ERROR_OUTPUT_DISCONNECTED;
1400 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1401 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1402 pthread_mutex_unlock(&private_client->lock);
1403 return TDM_ERROR_DPMS_OFF;
1407 w = calloc(1, sizeof *w);
1409 /* LCOV_EXCL_START */
1411 TDM_ERR("alloc failed");
1412 pthread_mutex_unlock(&private_client->lock);
1413 return TDM_ERROR_OUT_OF_MEMORY;
1415 /* LCOV_EXCL_STOP */
1418 w->private_vblank = private_vblank;
1420 w->user_data = user_data;
1422 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1423 LIST_INITHEAD(&w->call_link);
1425 clock_gettime(CLOCK_MONOTONIC, &tp);
1426 req_sec = (unsigned int)tp.tv_sec;
1427 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1429 w->req_id = ++private_output->req_id;
1430 w->req_time = TDM_TIME(req_sec, req_usec);
1431 w->need_free = (private_vblank->sync) ? 0 : 1;
1433 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
1435 if (private_vblank->enable_ttrace)
1436 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1438 TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
1439 vblank, interval, w->req_id, w->req_time);
1441 private_vblank->req_time = w->req_time;
1443 if (private_vblank->last_time >= w->req_time)
1444 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1446 if (!private_vblank->sync) {
1447 wl_display_flush(private_client->display);
1448 pthread_mutex_unlock(&private_client->lock);
1449 return TDM_ERROR_NONE;
1452 /* LCOV_EXCL_START */
1454 while (ret != -1 && !w->need_free)
1455 ret = wl_display_dispatch(private_client->display);
1457 clock_gettime(CLOCK_MONOTONIC, &tp);
1458 TDM_DBG("block during %d us",
1459 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1460 - (req_sec * 1000000 + req_usec));
1465 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1466 pthread_mutex_unlock(&private_client->lock);
1467 return TDM_ERROR_PROTOCOL_ERROR;
1470 pthread_mutex_unlock(&private_client->lock);
1472 return TDM_ERROR_NONE;
1474 /* LCOV_EXCL_STOP */
1478 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
1479 tdm_client_vblank_handler func, void *user_data)
1481 tdm_private_client *private_client;
1482 tdm_private_client_output *private_output;
1483 tdm_private_client_vblank *private_vblank;
1484 tdm_client_wait_info *w;
1486 unsigned int req_sec, req_usec;
1489 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1490 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1492 private_vblank = vblank;
1493 private_output = private_vblank->private_output;
1494 private_client = private_output->private_client;
1496 pthread_mutex_lock(&private_client->lock);
1498 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1499 pthread_mutex_unlock(&private_client->lock);
1500 return TDM_ERROR_PROTOCOL_ERROR;
1503 if (!private_vblank->started)
1504 private_vblank->started = 1;
1506 if (!private_vblank->enable_fake) {
1507 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1508 TDM_ERR("output disconnected");
1509 pthread_mutex_unlock(&private_client->lock);
1510 return TDM_ERROR_OUTPUT_DISCONNECTED;
1512 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1513 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1514 pthread_mutex_unlock(&private_client->lock);
1515 return TDM_ERROR_DPMS_OFF;
1519 w = calloc(1, sizeof *w);
1521 /* LCOV_EXCL_START */
1523 TDM_ERR("alloc failed");
1524 pthread_mutex_unlock(&private_client->lock);
1525 return TDM_ERROR_OUT_OF_MEMORY;
1527 /* LCOV_EXCL_STOP */
1530 w->private_vblank = private_vblank;
1532 w->user_data = user_data;
1534 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1535 LIST_INITHEAD(&w->call_link);
1537 clock_gettime(CLOCK_MONOTONIC, &tp);
1538 req_sec = (unsigned int)tp.tv_sec;
1539 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1541 w->req_id = ++private_output->req_id;
1542 w->req_time = TDM_TIME(req_sec, req_usec);
1543 w->need_free = (private_vblank->sync) ? 0 : 1;
1545 wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
1547 if (private_vblank->enable_ttrace)
1548 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1550 TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
1551 vblank, sequence, w->req_id, w->req_time);
1553 private_vblank->req_time = w->req_time;
1555 if (private_vblank->last_time >= w->req_time)
1556 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1558 if (!private_vblank->sync) {
1559 wl_display_flush(private_client->display);
1560 pthread_mutex_unlock(&private_client->lock);
1561 return TDM_ERROR_NONE;
1564 /* LCOV_EXCL_START */
1566 while (ret != -1 && !w->need_free)
1567 ret = wl_display_dispatch(private_client->display);
1569 clock_gettime(CLOCK_MONOTONIC, &tp);
1570 TDM_DBG("block during %d us",
1571 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1572 - (req_sec * 1000000 + req_usec));
1577 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1578 pthread_mutex_unlock(&private_client->lock);
1579 return TDM_ERROR_PROTOCOL_ERROR;
1582 pthread_mutex_unlock(&private_client->lock);
1584 return TDM_ERROR_NONE;
1586 /* LCOV_EXCL_STOP */
1590 tdm_client_vblank_is_waiting(tdm_client_vblank *vblank)
1592 tdm_private_client_vblank *private_vblank;
1594 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, 0);
1596 private_vblank = vblank;
1598 return (LIST_LENGTH(&private_vblank->wait_list) > 0) ? 1 : 0;
1602 tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput, uint32_t msg)
1604 tdm_private_client_voutput *private_voutput = data;
1606 private_voutput->msg = msg;
1609 static const struct wl_tdm_voutput_listener tdm_client_voutput_lisntener = {
1610 tdm_client_voutput_cb_ack_message
1613 tdm_client_voutput *
1614 tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error)
1616 tdm_private_client *private_client;
1617 tdm_private_client_output *private_output;
1618 tdm_private_client_voutput *private_voutput;
1619 struct wl_proxy *wrapper;
1622 *error = TDM_ERROR_NONE;
1625 TDM_ERR("'!client' failed");
1627 *error = TDM_ERROR_INVALID_PARAMETER;
1632 TDM_ERR("'!name' failed");
1634 *error = TDM_ERROR_INVALID_PARAMETER;
1638 private_client = (tdm_private_client *)client;
1640 pthread_mutex_lock(&private_client->lock);
1642 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1644 *error = TDM_ERROR_PROTOCOL_ERROR;
1645 pthread_mutex_unlock(&private_client->lock);
1649 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
1650 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
1652 *error = TDM_ERROR_INVALID_PARAMETER; // FIXME define new error type.
1653 pthread_mutex_unlock(&private_client->lock);
1658 wrapper = wl_proxy_create_wrapper(private_client->tdm);
1660 TDM_ERR("create virtual output wrapper failed");
1662 *error = TDM_ERROR_OUT_OF_MEMORY;
1663 pthread_mutex_unlock(&private_client->lock);
1667 wl_proxy_set_queue(wrapper, private_client->queue);
1669 private_voutput = calloc(1, sizeof *private_voutput);
1670 if (!private_voutput) {
1671 /* LOCV_EXCL_START */
1672 wl_proxy_wrapper_destroy(wrapper);
1673 TDM_ERR("alloc failed");
1675 *error = TDM_ERROR_OUT_OF_MEMORY;
1677 /* LOCV_EXCL_STOP */
1680 private_voutput->base.private_client = private_client;
1682 snprintf(private_voutput->base.name, TDM_NAME_LEN, "%s", name);
1683 private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, private_voutput->base.name);
1684 wl_proxy_wrapper_destroy(wrapper);
1685 if (!private_voutput->wl_voutput) {
1686 /* LCOV_EXCL_START */
1687 TDM_ERR("couldn't create voutput resource");
1688 free(private_voutput);
1690 *error = TDM_ERROR_OUT_OF_MEMORY;
1691 pthread_mutex_unlock(&private_client->lock);
1693 /* LCOV_EXCL_STOP */
1696 wl_tdm_voutput_add_listener(private_voutput->wl_voutput,
1697 &tdm_client_voutput_lisntener, private_voutput);
1698 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1700 wl_proxy_set_queue((struct wl_proxy *)private_voutput->wl_voutput, NULL);
1702 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1703 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
1704 free(private_voutput);
1706 *error = TDM_ERROR_PROTOCOL_ERROR;
1707 pthread_mutex_unlock(&private_client->lock);
1711 if (private_voutput->msg != WL_TDM_VOUTPUT_MESSAGE_ADDED)
1713 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
1714 free(private_voutput);
1716 *error = TDM_ERROR_PROTOCOL_ERROR; // FIXME add new error type.
1717 pthread_mutex_unlock(&private_client->lock);
1721 pthread_mutex_unlock(&private_client->lock);
1723 return (tdm_client_voutput *)private_voutput;
1727 tdm_client_voutput_destroy(tdm_client_voutput *voutput)
1729 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)voutput;
1731 if (!private_voutput)
1734 free(private_voutput);
1738 tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_client_output_mode *modes, int count)
1740 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
1741 TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
1742 TDM_RETURN_VAL_IF_FAIL(count > 0, TDM_ERROR_INVALID_PARAMETER);
1744 return TDM_ERROR_NONE;
1748 tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, int mmWidth, int mmHeight)
1750 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
1751 TDM_RETURN_VAL_IF_FAIL(mmWidth > 0, TDM_ERROR_INVALID_PARAMETER);
1752 TDM_RETURN_VAL_IF_FAIL(mmHeight > 0, TDM_ERROR_INVALID_PARAMETER);
1754 return TDM_ERROR_NONE;
1758 tdm_client_voutput_get_client_output(tdm_client_voutput *voutput, tdm_error *error)
1760 tdm_private_client_voutput *private_voutput;
1763 *error = TDM_ERROR_NONE;
1767 TDM_ERR("'!voutput' failed");
1769 *error = TDM_ERROR_INVALID_PARAMETER;
1773 private_voutput = (tdm_private_client_voutput *)voutput;
1775 return &private_voutput->base;
1779 tdm_client_output_set_buffer_queue(tdm_client_output *output, void *queue, void *func)
1781 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1782 TDM_RETURN_VAL_IF_FAIL(queue != NULL, TDM_ERROR_INVALID_PARAMETER);
1783 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1785 return TDM_ERROR_NONE;
1789 tdm_client_output_get_available_modes(tdm_client_output *output, tdm_client_output_mode **modes, int *count)
1791 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1792 TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
1793 TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
1795 return TDM_ERROR_NONE;
1799 tdm_client_output_set_mode(tdm_client_output *output, const tdm_client_output_mode *mode)
1801 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1802 TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
1804 return TDM_ERROR_NONE;
1808 tdm_client_output_connect(tdm_client_output *output)
1810 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1812 return TDM_ERROR_NONE;
1816 tdm_client_output_disconnect(tdm_client_output *output)
1818 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1819 return TDM_ERROR_NONE;