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 **************************************************************************/
36 #define WL_HIDE_DEPRECATED
49 #include <tdm-client-protocol.h>
51 #include "tdm_client.h"
53 #include "tdm_macro.h"
56 #include "tdm_private.h"
57 #include <tbm_surface_internal.h>
59 #define TDM_ARRAY_NTH_DATA(array, type, n) (((type*)((array)->data)) + n)
61 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
62 typedef struct _tdm_private_client_voutput tdm_private_client_voutput;
64 typedef struct _tdm_private_client {
67 struct wl_display *display;
68 struct wl_event_queue *queue;
69 struct wl_registry *registry;
71 struct list_head output_list;
72 struct list_head voutput_list;
74 unsigned int enable_ttrace;
77 tdm_private_client_vblank *temp_vblank;
80 typedef struct _tdm_private_client_output {
81 struct list_head link;
83 tdm_private_client *private_client;
85 char name[TDM_NAME_LEN];
86 struct wl_tdm_output *output;
90 tdm_output_conn_status connection;
92 struct list_head vblank_list;
93 struct list_head change_handler_list;
96 unsigned int watch_output_changes;
98 tdm_private_client_voutput *voutput;
99 } tdm_private_client_output;
101 typedef struct _tdm_private_client_buffer {
102 struct list_head link;
103 struct wl_buffer *wl_buffer;
104 } tdm_private_client_buffer;
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;
113 tdm_client_output_mode *modes;
116 unsigned int mmwidth;
117 unsigned int mmheight;
121 struct list_head buffer_list;
123 tdm_private_client_buffer *attach_buffer;
125 tdm_private_client *private_client;
126 tdm_private_client_output *private_output;
127 char name[TDM_NAME_LEN];
131 struct _tdm_private_client_vblank {
132 tdm_private_client_output *private_output;
134 struct wl_tdm_vblank *vblank;
135 struct list_head wait_list;
137 char name[TDM_NAME_LEN];
141 unsigned int enable_fake;
142 unsigned int enable_ttrace;
144 unsigned int started;
150 struct list_head link;
153 typedef struct _tdm_client_output_handler_info {
154 tdm_private_client_output *private_output;
156 tdm_client_output_change_handler func;
159 struct list_head link;
160 struct list_head call_link;
161 } tdm_client_output_handler_info;
163 typedef struct _tdm_client_wait_info {
164 tdm_private_client_vblank *private_vblank;
166 tdm_client_vblank_handler func;
173 struct list_head link;
174 struct list_head call_link;
175 } tdm_client_wait_info;
177 typedef struct _tdm_client_voutput_commit_handler_info {
178 tdm_private_client_voutput *private_voutput;
180 tdm_client_voutput_commit_handler func;
183 struct list_head link;
184 struct list_head call_link;
185 } tdm_client_voutput_commit_handler_info;
188 _tdm_client_check_wl_error(tdm_private_client *private_client, const char *func, int line)
191 const struct wl_interface *intf;
194 err = wl_display_get_error(private_client->display);
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);
203 TDM_ERR("[%s,%d] errno(%d)", func, line, err);
209 #define CHECK_WL_PROTOCOL_ERROR(pc) _tdm_client_check_wl_error(pc, __FUNCTION__, __LINE__)
212 _tdm_client_vblank_cb_stamp(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t stamp)
214 tdm_private_client_vblank *private_vblank = data;
215 tdm_private_client *private_client;
217 TDM_RETURN_IF_FAIL(private_vblank != NULL);
219 private_vblank->stamp = stamp;
221 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
222 private_client = private_vblank->private_output->private_client;
224 private_client->stamp = stamp;
227 /* LCOV_EXCL_START */
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)
233 tdm_private_client_vblank *private_vblank = data;
234 tdm_private_client *private_client;
235 tdm_client_wait_info *w = NULL, *wait_info = NULL;
237 TDM_RETURN_IF_FAIL(private_vblank != NULL);
239 private_client = private_vblank->private_output->private_client;
241 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
243 TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
244 private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
246 LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
247 if (w->req_id != req_id)
255 TDM_ERR("no wait infomation for req_id(%d)", req_id);
259 if (private_vblank->enable_ttrace)
260 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
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);
265 if (wait_info->need_free)
266 LIST_DEL(&wait_info->link);
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);
274 if (wait_info->need_free)
277 wait_info->need_free = 1;
281 /* LCOV_EXCL_START */
283 _tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
285 tdm_private_client_vblank *private_vblank = data;
286 tdm_private_client *private_client;
288 TDM_RETURN_IF_FAIL(private_vblank != NULL);
290 private_vblank->enable_ttrace = enable;
292 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
293 private_client = private_vblank->private_output->private_client;
295 private_client->enable_ttrace = enable;
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,
306 _tdm_client_output_destroy(tdm_private_client_output *private_output)
308 tdm_private_client_vblank *v = NULL, *vv = NULL;
309 tdm_client_output_handler_info *h = NULL, *hh = NULL;
311 LIST_DEL(&private_output->link);
313 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
314 TDM_ERR("vblanks SHOULD be destroyed first!");
316 v->private_output = NULL;
319 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
324 wl_tdm_output_destroy(private_output->output);
326 free(private_output);
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)
333 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
335 TDM_RETURN_IF_FAIL(private_output != NULL);
337 private_output->width = width;
338 private_output->height = height;
339 private_output->refresh = refresh;
341 if (error != TDM_ERROR_NONE)
342 TDM_INFO("mode event error: %d", error);
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);
350 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
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;
356 struct list_head call_list;
358 TDM_RETURN_IF_FAIL(private_output != NULL);
360 private_client = private_output->private_client;
362 if (private_output->connection == value && value != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
365 private_output->connection = value;
367 if (error != TDM_ERROR_NONE)
368 TDM_INFO("connection event error: %d", error);
370 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
372 wl_proxy_get_id((struct wl_proxy*)private_output->output),
375 LIST_INITHEAD(&call_list);
377 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
378 LIST_ADDTAIL(&h->call_link, &call_list);
382 pthread_mutex_unlock(&private_client->lock);
383 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
385 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
387 pthread_mutex_lock(&private_client->lock);
391 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
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;
397 struct list_head call_list;
399 TDM_RETURN_IF_FAIL(private_output != NULL);
401 private_client = private_output->private_client;
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?
407 if (value > TDM_OUTPUT_DPMS_OFF)
408 value = TDM_OUTPUT_DPMS_ON;
410 if (private_output->dpms == value)
413 private_output->dpms = value;
415 if (error != TDM_ERROR_NONE)
416 TDM_INFO("dpms event error: %d", error);
418 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
420 wl_proxy_get_id((struct wl_proxy*)private_output->output),
423 LIST_INITHEAD(&call_list);
425 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
426 LIST_ADDTAIL(&h->call_link, &call_list);
430 pthread_mutex_unlock(&private_client->lock);
431 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
433 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
435 pthread_mutex_lock(&private_client->lock);
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,
445 _tdm_client_cb_global(void *data, struct wl_registry *registry,
446 uint32_t name, const char *interface,
449 tdm_private_client *private_client = data;
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);
456 wl_display_flush(private_client->display);
460 /* LCOV_EXCL_START */
462 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
467 static const struct wl_registry_listener tdm_client_registry_listener = {
468 _tdm_client_cb_global,
469 _tdm_client_cb_global_remove
473 tdm_client_create(tdm_error *error)
475 tdm_private_client *private_client;
477 private_client = calloc(1, sizeof *private_client);
478 if (!private_client) {
479 /* LCOV_EXCL_START */
481 TDM_ERR("alloc failed");
483 *error = TDM_ERROR_OUT_OF_MEMORY;
489 if (pthread_mutex_init(&private_client->lock, NULL)) {
490 TDM_ERR("mutex init failed: %m");
491 free(private_client);
493 *error = TDM_ERROR_OUT_OF_MEMORY;
497 LIST_INITHEAD(&private_client->output_list);
498 LIST_INITHEAD(&private_client->voutput_list);
500 private_client->display = wl_display_connect("tdm-socket");
501 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
503 private_client->queue = wl_display_create_queue(private_client->display);
504 TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
506 private_client->registry = wl_display_get_registry(private_client->display);
507 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
509 wl_registry_add_listener(private_client->registry,
510 &tdm_client_registry_listener, private_client);
511 wl_display_roundtrip(private_client->display);
513 if (CHECK_WL_PROTOCOL_ERROR(private_client))
516 /* check global objects */
517 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
520 *error = TDM_ERROR_NONE;
522 return (tdm_client*)private_client;
524 tdm_client_destroy((tdm_client*)private_client);
526 *error = TDM_ERROR_OPERATION_FAILED;
531 tdm_client_destroy(tdm_client *client)
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;
540 pthread_mutex_lock(&private_client->lock);
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);
548 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
549 _tdm_client_output_destroy(o);
552 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_client->voutput_list, link) {
553 tdm_client_voutput_destroy(vo);
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);
565 pthread_mutex_unlock(&private_client->lock);
566 pthread_mutex_destroy(&private_client->lock);
568 free(private_client);
572 tdm_client_get_fd(tdm_client *client, int *fd)
574 tdm_private_client *private_client;
576 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
577 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
579 private_client = (tdm_private_client*)client;
581 pthread_mutex_lock(&private_client->lock);
583 *fd = wl_display_get_fd(private_client->display);
585 pthread_mutex_unlock(&private_client->lock);
588 return TDM_ERROR_OPERATION_FAILED;
590 return TDM_ERROR_NONE;
594 tdm_client_handle_events(tdm_client *client)
596 tdm_private_client *private_client;
598 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
600 /* LCOV_EXCL_START */
601 private_client = (tdm_private_client*)client;
603 pthread_mutex_lock(&private_client->lock);
605 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
606 pthread_mutex_unlock(&private_client->lock);
607 return TDM_ERROR_PROTOCOL_ERROR;
610 if (private_client->enable_ttrace)
611 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
613 wl_display_dispatch(private_client->display);
615 if (private_client->enable_ttrace)
616 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
618 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
619 pthread_mutex_unlock(&private_client->lock);
620 return TDM_ERROR_PROTOCOL_ERROR;
623 pthread_mutex_unlock(&private_client->lock);
625 return TDM_ERROR_NONE;
630 _tdm_client_poll(struct wl_display *display, short int events, int timeout)
633 struct pollfd pfd[1];
635 pfd[0].fd = wl_display_get_fd(display);
636 pfd[0].events = events;
638 ret = poll(pfd, 1, timeout);
639 } while (ret == -1 && errno == EINTR);
645 _tdm_client_dispatch_timeout(tdm_private_client *private_client, int timeout)
648 struct wl_display *display = private_client->display;
650 if (wl_display_prepare_read(display) == -1) {
651 if (wl_display_dispatch_pending(display) > 0)
652 return TDM_ERROR_NONE;
654 return TDM_ERROR_OPERATION_FAILED;
658 ret = wl_display_flush(display);
660 if (ret != -1 || errno != EAGAIN)
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;
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;
678 ret = _tdm_client_poll(display, POLLIN, timeout);
680 wl_display_cancel_read(display);
682 TDM_ERR("_tdm_client_poll timeout.");
683 return TDM_ERROR_TIMEOUT;
685 TDM_ERR("_tdm_client_poll failed. (ret:%d)", ret);
686 return TDM_ERROR_OPERATION_FAILED;
690 if (wl_display_read_events(display) == -1) {
691 TDM_ERR("wl_display_read_events failed");
692 return TDM_ERROR_OPERATION_FAILED;
695 ret = wl_display_dispatch_pending(display);
698 TDM_ERR("_tdm_client_dispatch_timeout failed");
699 return TDM_ERROR_OPERATION_FAILED;
702 return TDM_ERROR_NONE;
706 tdm_client_handle_events_timeout(tdm_client *client, int ms_timeout)
708 tdm_private_client *private_client;
710 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
712 private_client = (tdm_private_client*)client;
714 pthread_mutex_lock(&private_client->lock);
716 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
717 pthread_mutex_unlock(&private_client->lock);
718 return TDM_ERROR_PROTOCOL_ERROR;
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);
724 ret = _tdm_client_dispatch_timeout(private_client, ms_timeout);
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);
729 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
730 pthread_mutex_unlock(&private_client->lock);
731 return TDM_ERROR_PROTOCOL_ERROR;
734 pthread_mutex_unlock(&private_client->lock);
739 typedef struct _tdm_client_vblank_temp {
740 tdm_client_vblank_handler2 func;
742 } tdm_client_vblank_temp;
744 /* LCOV_EXCL_START */
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)
749 tdm_client_vblank_temp *vblank_temp = user_data;
751 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
752 TDM_RETURN_IF_FAIL(vblank != NULL);
754 if (vblank_temp->func)
755 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
761 /* LCOV_EXCL_START */ /* deprecated */
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)
767 tdm_private_client *private_client = (tdm_private_client*)client;
768 tdm_client_output *output;
769 tdm_client_vblank_temp *vblank_temp;
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);
776 if (CHECK_WL_PROTOCOL_ERROR(private_client))
777 return TDM_ERROR_PROTOCOL_ERROR;
779 if (!private_client->temp_vblank) {
780 output = tdm_client_get_output(client, name, &ret);
781 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
783 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
784 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
787 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
788 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
790 vblank_temp = calloc(1, sizeof *vblank_temp);
791 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
793 vblank_temp->func = func;
794 vblank_temp->user_data = user_data;
796 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
801 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
803 tdm_private_client *private_client;
804 tdm_private_client_output *private_output = NULL;
805 struct wl_proxy *wrapper;
808 *error = TDM_ERROR_NONE;
811 TDM_ERR("'!client' failed");
813 *error = TDM_ERROR_INVALID_PARAMETER;
817 private_client = (tdm_private_client*)client;
819 pthread_mutex_lock(&private_client->lock);
821 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
823 *error = TDM_ERROR_PROTOCOL_ERROR;
824 pthread_mutex_unlock(&private_client->lock);
830 } else if (!strncmp(name, "primary", 7) || !strncmp(name, "default", 7)) {
831 TDM_DBG("get primary or default output");
833 tdm_private_client_voutput *private_voutput = NULL;
836 LIST_FOR_EACH_ENTRY(private_voutput, &private_client->voutput_list, link) {
837 if (!strncmp(private_voutput->name, name, TDM_NAME_LEN)) {
844 *error = TDM_ERROR_INVALID_PARAMETER;
845 pthread_mutex_unlock(&private_client->lock);
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;
857 wrapper = wl_proxy_create_wrapper(private_client->tdm);
859 TDM_ERR("create output_wrapper failed");
861 *error = TDM_ERROR_OUT_OF_MEMORY;
862 pthread_mutex_unlock(&private_client->lock);
866 wl_proxy_set_queue(wrapper, private_client->queue);
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");
874 *error = TDM_ERROR_OUT_OF_MEMORY;
875 pthread_mutex_unlock(&private_client->lock);
881 private_output->private_client = private_client;
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 */
889 TDM_ERR("couldn't create output resource");
890 free(private_output);
892 *error = TDM_ERROR_OUT_OF_MEMORY;
893 pthread_mutex_unlock(&private_client->lock);
899 LIST_INITHEAD(&private_output->vblank_list);
900 LIST_INITHEAD(&private_output->change_handler_list);
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);
906 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
908 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
909 wl_tdm_output_destroy(private_output->output);
910 free(private_output);
912 *error = TDM_ERROR_PROTOCOL_ERROR;
913 pthread_mutex_unlock(&private_client->lock);
917 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
919 pthread_mutex_unlock(&private_client->lock);
921 return (tdm_client_output*)private_output;
925 tdm_client_output_add_change_handler(tdm_client_output *output,
926 tdm_client_output_change_handler func,
929 tdm_private_client_output *private_output;
930 tdm_private_client *private_client;
931 tdm_client_output_handler_info *h = NULL;
933 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
934 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
936 private_output = (tdm_private_client_output*)output;
937 private_client = private_output->private_client;
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;
946 h = calloc(1, sizeof *h);
947 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
949 pthread_mutex_lock(&private_client->lock);
951 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
953 pthread_mutex_unlock(&private_client->lock);
954 return TDM_ERROR_PROTOCOL_ERROR;
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);
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.
967 private_output->watch_output_changes = 1;
970 h->private_output = private_output;
972 h->user_data = user_data;
973 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
974 LIST_INITHEAD(&h->call_link);
976 pthread_mutex_unlock(&private_client->lock);
978 return TDM_ERROR_NONE;
982 tdm_client_output_remove_change_handler(tdm_client_output *output,
983 tdm_client_output_change_handler func,
986 tdm_private_client_output *private_output;
987 tdm_private_client *private_client;
988 tdm_client_output_handler_info *h = NULL;
990 TDM_RETURN_IF_FAIL(output != NULL);
991 TDM_RETURN_IF_FAIL(func != NULL);
993 private_output = (tdm_private_client_output*)output;
994 private_client = private_output->private_client;
996 pthread_mutex_lock(&private_client->lock);
998 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
999 if (h->func != func || h->user_data != user_data)
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);
1013 pthread_mutex_unlock(&private_client->lock);
1018 pthread_mutex_unlock(&private_client->lock);
1022 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
1024 tdm_private_client_output *private_output;
1025 tdm_private_client *private_client;
1027 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1028 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
1030 private_output = (tdm_private_client_output*)output;
1031 private_client = private_output->private_client;
1033 pthread_mutex_lock(&private_client->lock);
1035 if (private_output->watch_output_changes) {
1036 *refresh = private_output->refresh;
1037 pthread_mutex_unlock(&private_client->lock);
1038 return TDM_ERROR_NONE;
1041 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1042 pthread_mutex_unlock(&private_client->lock);
1043 return TDM_ERROR_PROTOCOL_ERROR;
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);
1051 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1052 pthread_mutex_unlock(&private_client->lock);
1053 return TDM_ERROR_PROTOCOL_ERROR;
1056 *refresh = private_output->refresh;
1058 pthread_mutex_unlock(&private_client->lock);
1060 return TDM_ERROR_NONE;
1064 tdm_client_output_get_mode(tdm_client_output *output, unsigned int *width, unsigned int *height)
1066 tdm_private_client_output *private_output;
1067 tdm_private_client *private_client;
1069 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1070 TDM_RETURN_VAL_IF_FAIL(width != NULL, TDM_ERROR_INVALID_PARAMETER);
1071 TDM_RETURN_VAL_IF_FAIL(height != NULL, TDM_ERROR_INVALID_PARAMETER);
1073 private_output = (tdm_private_client_output*)output;
1074 private_client = private_output->private_client;
1076 pthread_mutex_lock(&private_client->lock);
1078 if (private_output->watch_output_changes) {
1079 *width = private_output->width;
1080 *height = private_output->height;
1081 pthread_mutex_unlock(&private_client->lock);
1082 return TDM_ERROR_NONE;
1085 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1086 pthread_mutex_unlock(&private_client->lock);
1087 return TDM_ERROR_PROTOCOL_ERROR;
1090 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1091 wl_tdm_output_get_mode(private_output->output);
1092 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1093 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1095 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1096 pthread_mutex_unlock(&private_client->lock);
1097 return TDM_ERROR_PROTOCOL_ERROR;
1100 *width = private_output->width;
1101 *height = private_output->height;
1103 pthread_mutex_unlock(&private_client->lock);
1105 return TDM_ERROR_NONE;
1109 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
1111 tdm_private_client_output *private_output;
1112 tdm_private_client *private_client;
1114 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1115 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
1117 private_output = (tdm_private_client_output*)output;
1118 private_client = private_output->private_client;
1120 pthread_mutex_lock(&private_client->lock);
1122 if (private_output->watch_output_changes) {
1123 *status = private_output->connection;
1124 pthread_mutex_unlock(&private_client->lock);
1125 return TDM_ERROR_NONE;
1128 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1129 pthread_mutex_unlock(&private_client->lock);
1130 return TDM_ERROR_PROTOCOL_ERROR;
1133 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1134 wl_tdm_output_get_connection(private_output->output);
1135 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1136 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1138 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1139 pthread_mutex_unlock(&private_client->lock);
1140 return TDM_ERROR_PROTOCOL_ERROR;
1143 *status = private_output->connection;
1145 pthread_mutex_unlock(&private_client->lock);
1147 return TDM_ERROR_NONE;
1151 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
1153 tdm_private_client_output *private_output;
1154 tdm_private_client *private_client;
1156 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1157 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
1159 private_output = (tdm_private_client_output*)output;
1160 private_client = private_output->private_client;
1162 if (private_output->watch_output_changes) {
1163 *dpms = private_output->dpms;
1164 return TDM_ERROR_NONE;
1167 pthread_mutex_lock(&private_client->lock);
1169 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1170 pthread_mutex_unlock(&private_client->lock);
1171 return TDM_ERROR_PROTOCOL_ERROR;
1174 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1175 wl_tdm_output_get_dpms(private_output->output);
1176 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1177 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1179 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1180 pthread_mutex_unlock(&private_client->lock);
1181 return TDM_ERROR_PROTOCOL_ERROR;
1184 *dpms = private_output->dpms;
1186 pthread_mutex_unlock(&private_client->lock);
1188 return TDM_ERROR_NONE;
1192 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
1194 tdm_private_client *private_client;
1195 tdm_private_client_output *private_output;
1196 tdm_private_client_vblank *private_vblank;
1197 struct wl_proxy *wrapper;
1200 *error = TDM_ERROR_NONE;
1203 TDM_ERR("'!output' failed");
1205 *error = TDM_ERROR_INVALID_PARAMETER;
1209 private_output = (tdm_private_client_output*)output;
1210 private_client = private_output->private_client;
1212 pthread_mutex_lock(&private_client->lock);
1214 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1216 *error = TDM_ERROR_PROTOCOL_ERROR;
1217 pthread_mutex_unlock(&private_client->lock);
1221 wrapper = wl_proxy_create_wrapper(private_output->output);
1223 TDM_ERR("create output_wrapper failed");
1225 *error = TDM_ERROR_OUT_OF_MEMORY;
1226 pthread_mutex_unlock(&private_client->lock);
1230 wl_proxy_set_queue(wrapper, private_client->queue);
1232 private_vblank = calloc(1, sizeof *private_vblank);
1233 if (!private_vblank) {
1234 /* LCOV_EXCL_START */
1236 TDM_ERR("alloc failed");
1237 wl_proxy_wrapper_destroy(wrapper);
1239 *error = TDM_ERROR_OUT_OF_MEMORY;
1240 pthread_mutex_unlock(&private_client->lock);
1243 /* LCOV_EXCL_STOP */
1246 private_vblank->private_output = private_output;
1248 private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
1249 wl_proxy_wrapper_destroy(wrapper);
1250 if (!private_vblank->vblank) {
1251 /* LCOV_EXCL_START */
1253 TDM_ERR("couldn't create vblank resource");
1254 free(private_vblank);
1256 *error = TDM_ERROR_OUT_OF_MEMORY;
1257 pthread_mutex_unlock(&private_client->lock);
1260 /* LCOV_EXCL_STOP */
1264 private_vblank->fps = private_output->refresh;
1265 private_vblank->offset = 0;
1266 private_vblank->enable_fake = 0;
1268 LIST_INITHEAD(&private_vblank->wait_list);
1270 wl_tdm_vblank_add_listener(private_vblank->vblank,
1271 &tdm_client_vblank_listener, private_vblank);
1272 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1274 wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
1276 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1277 wl_tdm_vblank_destroy(private_vblank->vblank);
1278 free(private_vblank);
1280 *error = TDM_ERROR_PROTOCOL_ERROR;
1281 pthread_mutex_unlock(&private_client->lock);
1285 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
1287 pthread_mutex_unlock(&private_client->lock);
1289 return (tdm_client_vblank*)private_vblank;
1293 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
1295 tdm_private_client_vblank *private_vblank;
1296 tdm_private_client *private_client;
1297 tdm_client_wait_info *w = NULL, *ww = NULL;
1299 TDM_RETURN_IF_FAIL(vblank != NULL);
1301 private_vblank = vblank;
1302 private_client = private_vblank->private_output->private_client;
1304 pthread_mutex_lock(&private_client->lock);
1306 LIST_DEL(&private_vblank->link);
1308 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
1313 wl_tdm_vblank_destroy(private_vblank->vblank);
1315 free(private_vblank);
1317 pthread_mutex_unlock(&private_client->lock);
1321 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
1323 tdm_private_client_vblank *private_vblank;
1324 tdm_private_client *private_client;
1326 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1328 private_vblank = vblank;
1329 private_client = private_vblank->private_output->private_client;
1331 pthread_mutex_lock(&private_client->lock);
1333 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1334 pthread_mutex_unlock(&private_client->lock);
1335 return TDM_ERROR_PROTOCOL_ERROR;
1339 name = TDM_VBLANK_DEFAULT_NAME;
1341 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
1342 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
1344 wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
1346 pthread_mutex_unlock(&private_client->lock);
1348 return TDM_ERROR_NONE;
1352 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
1354 tdm_private_client_vblank *private_vblank;
1355 tdm_private_client *private_client;
1357 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1359 private_vblank = vblank;
1360 private_client = private_vblank->private_output->private_client;
1362 pthread_mutex_lock(&private_client->lock);
1363 private_vblank->sync = sync;
1364 pthread_mutex_unlock(&private_client->lock);
1366 return TDM_ERROR_NONE;
1370 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
1372 tdm_private_client_vblank *private_vblank;
1373 tdm_private_client *private_client;
1375 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1376 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
1378 private_vblank = vblank;
1379 private_client = private_vblank->private_output->private_client;
1381 pthread_mutex_lock(&private_client->lock);
1383 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1384 pthread_mutex_unlock(&private_client->lock);
1385 return TDM_ERROR_PROTOCOL_ERROR;
1388 if (private_vblank->fps == fps) {
1389 pthread_mutex_unlock(&private_client->lock);
1390 return TDM_ERROR_NONE;
1393 private_vblank->fps = fps;
1395 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
1397 pthread_mutex_unlock(&private_client->lock);
1399 return TDM_ERROR_NONE;
1403 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
1405 tdm_private_client_vblank *private_vblank;
1406 tdm_private_client *private_client;
1408 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1410 private_vblank = vblank;
1411 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
1413 private_client = private_vblank->private_output->private_client;
1415 pthread_mutex_lock(&private_client->lock);
1417 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1418 pthread_mutex_unlock(&private_client->lock);
1419 return TDM_ERROR_PROTOCOL_ERROR;
1422 if (private_vblank->offset == offset_ms) {
1423 pthread_mutex_unlock(&private_client->lock);
1424 return TDM_ERROR_NONE;
1427 private_vblank->offset = offset_ms;
1429 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
1431 pthread_mutex_unlock(&private_client->lock);
1433 return TDM_ERROR_NONE;
1437 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
1439 tdm_private_client_vblank *private_vblank;
1440 tdm_private_client *private_client;
1442 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1444 private_vblank = vblank;
1445 private_client = private_vblank->private_output->private_client;
1447 pthread_mutex_lock(&private_client->lock);
1449 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1450 pthread_mutex_unlock(&private_client->lock);
1451 return TDM_ERROR_PROTOCOL_ERROR;
1454 if (private_vblank->enable_fake == enable_fake) {
1455 pthread_mutex_unlock(&private_client->lock);
1456 return TDM_ERROR_NONE;
1459 private_vblank->enable_fake = enable_fake;
1461 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
1463 pthread_mutex_unlock(&private_client->lock);
1465 return TDM_ERROR_NONE;
1469 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
1471 tdm_private_client *private_client;
1472 tdm_private_client_output *private_output;
1473 tdm_private_client_vblank *private_vblank;
1474 tdm_client_wait_info *w;
1476 unsigned int req_sec, req_usec;
1479 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1480 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1481 /* can't support "interval 0" and "getting current_msc" things because
1482 * there is a socket communication between TDM client and server. It's impossible
1483 * to return the current msc or sequence immediately.
1485 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1487 private_vblank = vblank;
1488 private_output = private_vblank->private_output;
1489 private_client = private_output->private_client;
1491 pthread_mutex_lock(&private_client->lock);
1493 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1494 pthread_mutex_unlock(&private_client->lock);
1495 return TDM_ERROR_PROTOCOL_ERROR;
1498 if (!private_vblank->started)
1499 private_vblank->started = 1;
1501 if (!private_vblank->enable_fake) {
1502 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1503 TDM_ERR("output disconnected");
1504 pthread_mutex_unlock(&private_client->lock);
1505 return TDM_ERROR_OUTPUT_DISCONNECTED;
1507 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1508 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1509 pthread_mutex_unlock(&private_client->lock);
1510 return TDM_ERROR_DPMS_OFF;
1514 w = calloc(1, sizeof *w);
1516 /* LCOV_EXCL_START */
1518 TDM_ERR("alloc failed");
1519 pthread_mutex_unlock(&private_client->lock);
1520 return TDM_ERROR_OUT_OF_MEMORY;
1522 /* LCOV_EXCL_STOP */
1525 w->private_vblank = private_vblank;
1527 w->user_data = user_data;
1529 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1530 LIST_INITHEAD(&w->call_link);
1532 clock_gettime(CLOCK_MONOTONIC, &tp);
1533 req_sec = (unsigned int)tp.tv_sec;
1534 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1536 w->req_id = ++private_output->req_id;
1537 w->req_time = TDM_TIME(req_sec, req_usec);
1538 w->need_free = (private_vblank->sync) ? 0 : 1;
1540 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
1542 if (private_vblank->enable_ttrace)
1543 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1545 TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
1546 vblank, interval, w->req_id, w->req_time);
1548 private_vblank->req_time = w->req_time;
1550 if (private_vblank->last_time >= w->req_time)
1551 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1553 if (!private_vblank->sync) {
1554 wl_display_flush(private_client->display);
1555 pthread_mutex_unlock(&private_client->lock);
1556 return TDM_ERROR_NONE;
1559 /* LCOV_EXCL_START */
1561 while (ret != -1 && !w->need_free)
1562 ret = wl_display_dispatch(private_client->display);
1564 clock_gettime(CLOCK_MONOTONIC, &tp);
1565 TDM_DBG("block during %d us",
1566 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1567 - (req_sec * 1000000 + req_usec));
1572 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1573 pthread_mutex_unlock(&private_client->lock);
1574 return TDM_ERROR_PROTOCOL_ERROR;
1577 pthread_mutex_unlock(&private_client->lock);
1579 return TDM_ERROR_NONE;
1581 /* LCOV_EXCL_STOP */
1585 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
1586 tdm_client_vblank_handler func, void *user_data)
1588 tdm_private_client *private_client;
1589 tdm_private_client_output *private_output;
1590 tdm_private_client_vblank *private_vblank;
1591 tdm_client_wait_info *w;
1593 unsigned int req_sec, req_usec;
1596 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1597 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1599 private_vblank = vblank;
1600 private_output = private_vblank->private_output;
1601 private_client = private_output->private_client;
1603 pthread_mutex_lock(&private_client->lock);
1605 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1606 pthread_mutex_unlock(&private_client->lock);
1607 return TDM_ERROR_PROTOCOL_ERROR;
1610 if (!private_vblank->started)
1611 private_vblank->started = 1;
1613 if (!private_vblank->enable_fake) {
1614 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1615 TDM_ERR("output disconnected");
1616 pthread_mutex_unlock(&private_client->lock);
1617 return TDM_ERROR_OUTPUT_DISCONNECTED;
1619 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1620 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1621 pthread_mutex_unlock(&private_client->lock);
1622 return TDM_ERROR_DPMS_OFF;
1626 w = calloc(1, sizeof *w);
1628 /* LCOV_EXCL_START */
1630 TDM_ERR("alloc failed");
1631 pthread_mutex_unlock(&private_client->lock);
1632 return TDM_ERROR_OUT_OF_MEMORY;
1634 /* LCOV_EXCL_STOP */
1637 w->private_vblank = private_vblank;
1639 w->user_data = user_data;
1641 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1642 LIST_INITHEAD(&w->call_link);
1644 clock_gettime(CLOCK_MONOTONIC, &tp);
1645 req_sec = (unsigned int)tp.tv_sec;
1646 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1648 w->req_id = ++private_output->req_id;
1649 w->req_time = TDM_TIME(req_sec, req_usec);
1650 w->need_free = (private_vblank->sync) ? 0 : 1;
1652 wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
1654 if (private_vblank->enable_ttrace)
1655 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1657 TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
1658 vblank, sequence, w->req_id, w->req_time);
1660 private_vblank->req_time = w->req_time;
1662 if (private_vblank->last_time >= w->req_time)
1663 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1665 if (!private_vblank->sync) {
1666 wl_display_flush(private_client->display);
1667 pthread_mutex_unlock(&private_client->lock);
1668 return TDM_ERROR_NONE;
1671 /* LCOV_EXCL_START */
1673 while (ret != -1 && !w->need_free)
1674 ret = wl_display_dispatch(private_client->display);
1676 clock_gettime(CLOCK_MONOTONIC, &tp);
1677 TDM_DBG("block during %d us",
1678 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1679 - (req_sec * 1000000 + req_usec));
1684 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1685 pthread_mutex_unlock(&private_client->lock);
1686 return TDM_ERROR_PROTOCOL_ERROR;
1689 pthread_mutex_unlock(&private_client->lock);
1691 return TDM_ERROR_NONE;
1693 /* LCOV_EXCL_STOP */
1697 tdm_client_vblank_is_waiting(tdm_client_vblank *vblank)
1699 tdm_private_client_vblank *private_vblank;
1701 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, 0);
1703 private_vblank = vblank;
1705 return (LIST_LENGTH(&private_vblank->wait_list) > 0) ? 1 : 0;
1708 static tbm_surface_h
1709 _tdm_client_voutput_create_surface_from_param(tbm_bufmgr bufmgr,
1717 struct wl_array *plane_buf_idx,
1718 struct wl_array *plane_offset,
1719 struct wl_array *plane_stride,
1720 struct wl_array *plane_size,
1727 int32_t names[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
1728 tbm_surface_info_s info = { 0, };
1729 tbm_bo bos[TBM_SURF_PLANE_MAX];
1730 int i, numPlane, numName;
1731 tbm_surface_h tbm_surface;
1733 numPlane = tbm_surface_internal_get_num_planes(format);
1734 TDM_RETURN_VAL_IF_FAIL(numPlane == num_plane, NULL);
1737 info.height = height;
1738 info.format = format;
1741 info.num_planes = numPlane;
1744 for (i = 0; i < numPlane; i++) {
1745 info.planes[i].offset = *TDM_ARRAY_NTH_DATA(plane_offset, int32_t, i);
1746 info.planes[i].stride = *TDM_ARRAY_NTH_DATA(plane_stride, int32_t, i);
1747 info.planes[i].size = *TDM_ARRAY_NTH_DATA(plane_size, int32_t, i);
1756 for (i = 0; i < numName; i++) {
1758 bos[i] = tbm_bo_import_fd(bufmgr, names[i]);
1760 bos[i] = tbm_bo_import(bufmgr, names[i]);
1763 tbm_surface = tbm_surface_internal_create_with_bos(&info, bos, numName);
1764 if (tbm_surface == NULL) {
1779 for (i = 0; i < numName; i++)
1780 tbm_bo_unref(bos[i]);
1785 tdm_client_voutput_cb_buffer_import_with_id(void *data,
1786 struct wl_tdm_voutput *wl_voutput,
1787 struct wl_buffer *wl_buffer,
1794 struct wl_array *plane_buf_idx,
1795 struct wl_array *plane_offset,
1796 struct wl_array *plane_stride,
1797 struct wl_array *plane_size,
1804 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1805 tdm_private_client_buffer *buffer = NULL;
1806 tbm_surface_h tbm_surface;
1808 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1810 buffer = calloc(1, sizeof *buffer);
1811 TDM_RETURN_IF_FAIL(buffer != NULL);
1813 tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 0,
1814 width, height, format, bpp, size,
1816 plane_buf_idx, plane_offset, plane_stride, plane_size,
1820 TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
1822 tbm_surface_internal_ref(tbm_surface);
1823 wl_buffer_set_user_data(wl_buffer, tbm_surface);
1825 buffer->wl_buffer = wl_buffer;
1827 LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
1836 wl_buffer_destroy(wl_buffer);
1840 tdm_client_voutput_cb_buffer_import_with_fd(void *data,
1841 struct wl_tdm_voutput *wl_voutput,
1842 struct wl_buffer *wl_buffer,
1849 struct wl_array *plane_buf_idx,
1850 struct wl_array *plane_offset,
1851 struct wl_array *plane_stride,
1852 struct wl_array *plane_size,
1859 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1860 tdm_private_client_buffer *buffer = NULL;
1861 tbm_surface_h tbm_surface;
1863 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1865 buffer = calloc(1, sizeof *buffer);
1866 TDM_RETURN_IF_FAIL(buffer != NULL);
1868 tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 1,
1869 width, height, format, bpp, size,
1871 plane_buf_idx, plane_offset, plane_stride, plane_size,
1875 TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
1877 tbm_surface_internal_ref(tbm_surface);
1878 wl_buffer_set_user_data(wl_buffer, tbm_surface);
1880 buffer->wl_buffer = wl_buffer;
1882 LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
1891 wl_buffer_destroy(wl_buffer);
1895 tdm_client_voutput_cb_buffer_destroy(void *data,
1896 struct wl_tdm_voutput *wl_voutput,
1897 struct wl_buffer *wl_buffer)
1899 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1900 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
1901 tbm_surface_h tbm_surface = NULL;
1903 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1905 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
1906 if (wl_buffer == cb->wl_buffer) {
1907 LIST_DEL(&cb->link);
1909 tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(wl_buffer);
1911 tbm_surface_internal_unref(tbm_surface);
1913 wl_buffer_set_user_data(wl_buffer, NULL);
1914 wl_buffer_destroy(wl_buffer);
1926 tdm_client_voutput_cb_attach_buffer(void *data,
1927 struct wl_tdm_voutput *wl_voutput,
1928 struct wl_buffer *wl_buffer)
1930 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1931 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
1933 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1935 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
1936 if (wl_buffer == cb->wl_buffer) {
1937 private_voutput->attach_buffer = cb;
1946 tdm_client_voutput_cb_commit(void *data, struct wl_tdm_voutput *wl_voutput)
1948 tdm_private_client_voutput *private_voutput;
1949 tdm_private_client *private_client;
1950 tbm_surface_h buffer = NULL;
1951 tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
1952 struct list_head call_list;
1954 private_voutput = (tdm_private_client_voutput *)data;
1955 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1956 TDM_RETURN_IF_FAIL(private_voutput->attach_buffer != NULL);
1958 buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
1959 TDM_RETURN_IF_FAIL(buffer != NULL);
1961 tbm_surface_internal_ref(buffer);
1963 private_client = private_voutput->private_client;
1965 LIST_INITHEAD(&call_list);
1967 LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
1968 LIST_ADDTAIL(&h->call_link, &call_list);
1971 pthread_mutex_unlock(&private_client->lock);
1972 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
1974 h->func(private_voutput, buffer, h->user_data);
1977 /* if no added commit_handler call commit done immediately */
1978 if (LIST_IS_EMPTY(&private_voutput->commit_handler_list))
1979 tdm_client_voutput_commit_done(private_voutput);
1981 pthread_mutex_lock(&private_client->lock);
1985 tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput, uint32_t msg)
1987 tdm_private_client_voutput *private_voutput = data;
1989 private_voutput->msg = msg;
1992 static const struct wl_tdm_voutput_listener tdm_client_voutput_lisntener = {
1993 tdm_client_voutput_cb_buffer_import_with_id,
1994 tdm_client_voutput_cb_buffer_import_with_fd,
1995 tdm_client_voutput_cb_buffer_destroy,
1996 tdm_client_voutput_cb_attach_buffer,
1997 tdm_client_voutput_cb_commit,
1998 tdm_client_voutput_cb_ack_message
2001 tdm_client_voutput *
2002 tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error)
2004 tdm_private_client *private_client;
2005 tdm_private_client_output *private_output = NULL;
2006 tdm_private_client_voutput *private_voutput;
2007 struct wl_proxy *wrapper;
2010 *error = TDM_ERROR_NONE;
2013 TDM_ERR("'!client' failed");
2015 *error = TDM_ERROR_INVALID_PARAMETER;
2020 TDM_ERR("'!name' failed");
2022 *error = TDM_ERROR_INVALID_PARAMETER;
2026 private_client = (tdm_private_client *)client;
2028 pthread_mutex_lock(&private_client->lock);
2030 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2032 *error = TDM_ERROR_PROTOCOL_ERROR;
2033 pthread_mutex_unlock(&private_client->lock);
2037 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
2038 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
2040 *error = TDM_ERROR_INVALID_PARAMETER; // FIXME define new error type.
2041 pthread_mutex_unlock(&private_client->lock);
2046 wrapper = wl_proxy_create_wrapper(private_client->tdm);
2048 TDM_ERR("create virtual output wrapper failed");
2050 *error = TDM_ERROR_OUT_OF_MEMORY;
2051 pthread_mutex_unlock(&private_client->lock);
2055 wl_proxy_set_queue(wrapper, private_client->queue);
2057 private_voutput = calloc(1, sizeof *private_voutput);
2058 if (!private_voutput) {
2059 /* LOCV_EXCL_START */
2060 wl_proxy_wrapper_destroy(wrapper);
2061 TDM_ERR("alloc failed");
2063 *error = TDM_ERROR_OUT_OF_MEMORY;
2064 pthread_mutex_unlock(&private_client->lock);
2066 /* LOCV_EXCL_STOP */
2069 private_voutput->bufmgr = tbm_bufmgr_init(-1);
2070 if (private_voutput->bufmgr == NULL) {
2071 /* LCOV_EXCL_START */
2072 TDM_ERR("fail tbm_bufmgr_init");
2073 free(private_voutput);
2075 *error = TDM_ERROR_OUT_OF_MEMORY;
2076 pthread_mutex_unlock(&private_client->lock);
2078 /* LCOV_EXCL_STOP */
2081 LIST_INITHEAD(&private_voutput->commit_handler_list);
2082 LIST_INITHEAD(&private_voutput->buffer_list);
2084 private_voutput->private_client = private_client;
2085 strncpy(private_voutput->name, name, TDM_NAME_LEN - 1);
2086 private_voutput->name[TDM_NAME_LEN - 1] = '\0';
2088 private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, name);
2089 wl_proxy_wrapper_destroy(wrapper);
2090 if (!private_voutput->wl_voutput) {
2091 /* LCOV_EXCL_START */
2092 TDM_ERR("couldn't create voutput resource");
2093 free(private_voutput);
2095 *error = TDM_ERROR_OUT_OF_MEMORY;
2096 pthread_mutex_unlock(&private_client->lock);
2098 /* LCOV_EXCL_STOP */
2101 wl_tdm_voutput_add_listener(private_voutput->wl_voutput,
2102 &tdm_client_voutput_lisntener, private_voutput);
2103 wl_display_roundtrip_queue(private_client->display, private_client->queue);
2105 wl_proxy_set_queue((struct wl_proxy *)private_voutput->wl_voutput, NULL);
2107 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2108 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2109 free(private_voutput);
2111 *error = TDM_ERROR_PROTOCOL_ERROR;
2112 pthread_mutex_unlock(&private_client->lock);
2116 if (private_voutput->msg != WL_TDM_VOUTPUT_MESSAGE_ADDED) {
2117 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2118 free(private_voutput);
2120 *error = TDM_ERROR_PROTOCOL_ERROR; // FIXME add new error type.
2121 pthread_mutex_unlock(&private_client->lock);
2125 LIST_ADDTAIL(&private_voutput->link, &private_client->voutput_list);
2127 pthread_mutex_unlock(&private_client->lock);
2129 return (tdm_client_voutput *)private_voutput;
2133 tdm_client_voutput_destroy(tdm_client_voutput *voutput)
2135 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)voutput;
2136 tdm_private_client *private_client;
2137 tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
2139 if (!private_voutput)
2141 private_client = private_voutput->private_client;
2143 pthread_mutex_lock(&private_client->lock);
2145 if (!(LIST_IS_EMPTY(&private_voutput->buffer_list))) {
2146 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
2148 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
2149 tbm_surface_h tbm_surface = NULL;
2151 LIST_DEL(&cb->link);
2153 tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(cb->wl_buffer);
2155 tbm_surface_internal_unref(tbm_surface);
2157 wl_buffer_set_user_data(cb->wl_buffer, NULL);
2158 wl_buffer_destroy(cb->wl_buffer);
2164 if (private_voutput->bufmgr)
2165 tbm_bufmgr_deinit(private_voutput->bufmgr);
2167 if (private_voutput->available_modes.modes)
2168 free(private_voutput->available_modes.modes);
2170 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_voutput->commit_handler_list, link) {
2175 if (private_voutput->get_output)
2176 _tdm_client_output_destroy(private_voutput->private_output);
2178 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2180 LIST_DEL(&private_voutput->link);
2182 free(private_voutput);
2184 pthread_mutex_unlock(&private_client->lock);
2188 tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_client_output_mode *modes, int count)
2190 tdm_private_client_voutput *private_voutput;
2191 tdm_private_client *private_client;
2192 tdm_error ret = TDM_ERROR_NONE;
2194 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2196 if ((count > 0) && (modes == NULL))
2197 return TDM_ERROR_INVALID_PARAMETER;
2199 private_voutput = (tdm_private_client_voutput *)voutput;
2200 private_client = private_voutput->private_client;
2202 if (!private_voutput->private_output) {
2203 private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
2204 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
2207 pthread_mutex_lock(&private_client->lock);
2209 if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2210 pthread_mutex_unlock(&private_client->lock);
2211 return TDM_ERROR_BAD_REQUEST;
2214 if (private_voutput->available_modes.modes)
2215 free(private_voutput->available_modes.modes);
2217 private_voutput->available_modes.count = count;
2220 private_voutput->available_modes.modes = calloc(count, sizeof(tdm_client_output_mode));
2221 memcpy(private_voutput->available_modes.modes, modes, sizeof(tdm_client_output_mode) * count);
2224 pthread_mutex_unlock(&private_client->lock);
2226 return TDM_ERROR_NONE;
2230 tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, unsigned int mmWidth, unsigned int mmHeight)
2232 tdm_private_client_voutput *private_voutput;
2233 tdm_private_client *private_client;
2234 tdm_error ret = TDM_ERROR_NONE;
2236 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2237 TDM_RETURN_VAL_IF_FAIL(mmWidth != 0, TDM_ERROR_INVALID_PARAMETER);
2238 TDM_RETURN_VAL_IF_FAIL(mmHeight != 0, TDM_ERROR_INVALID_PARAMETER);
2240 private_voutput = (tdm_private_client_voutput *)voutput;
2241 private_client = private_voutput->private_client;
2243 if (!private_voutput->private_output) {
2244 private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
2245 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
2248 pthread_mutex_lock(&private_client->lock);
2250 if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2251 pthread_mutex_unlock(&private_client->lock);
2252 return TDM_ERROR_BAD_REQUEST;
2255 private_voutput->mmwidth = mmWidth;
2256 private_voutput->mmheight = mmHeight;
2258 pthread_mutex_unlock(&private_client->lock);
2260 return TDM_ERROR_NONE;
2264 tdm_client_voutput_add_commit_handler(tdm_client_voutput *voutput,
2265 tdm_client_voutput_commit_handler func,
2268 tdm_private_client_voutput *private_voutput;
2269 tdm_private_client *private_client;
2270 tdm_client_voutput_commit_handler_info *h = NULL;
2272 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2273 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
2275 private_voutput = (tdm_private_client_voutput *)voutput;
2276 private_client = private_voutput->private_client;
2278 LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
2279 if (h->func == func && h->user_data == user_data) {
2280 TDM_ERR("can't add twice");
2281 return TDM_ERROR_BAD_REQUEST;
2285 h = calloc(1, sizeof *h);
2286 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
2288 pthread_mutex_lock(&private_client->lock);
2290 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2292 pthread_mutex_unlock(&private_client->lock);
2293 return TDM_ERROR_PROTOCOL_ERROR;
2296 h->private_voutput = private_voutput;
2298 h->user_data = user_data;
2299 LIST_ADDTAIL(&h->link, &private_voutput->commit_handler_list);
2300 LIST_INITHEAD(&h->call_link);
2302 pthread_mutex_unlock(&private_client->lock);
2304 return TDM_ERROR_NONE;
2308 tdm_client_voutput_remove_commit_handler(tdm_client_voutput *voutput,
2309 tdm_client_voutput_commit_handler func,
2312 tdm_private_client_voutput *private_voutput;
2313 tdm_private_client *private_client;
2314 tdm_client_voutput_commit_handler_info *h = NULL;
2316 TDM_RETURN_IF_FAIL(voutput != NULL);
2317 TDM_RETURN_IF_FAIL(func != NULL);
2319 private_voutput = (tdm_private_client_voutput *)voutput;
2320 private_client = private_voutput->private_client;
2322 pthread_mutex_lock(&private_client->lock);
2324 LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
2325 if (h->func != func || h->user_data != user_data)
2331 pthread_mutex_unlock(&private_client->lock);
2336 pthread_mutex_unlock(&private_client->lock);
2340 tdm_client_voutput_commit_done(tdm_client_voutput *voutput)
2342 tdm_private_client_voutput *private_voutput;
2343 tdm_private_client *private_client;
2344 tbm_surface_h buffer = NULL;
2346 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2348 private_voutput = (tdm_private_client_voutput *)voutput;
2349 TDM_RETURN_VAL_IF_FAIL(private_voutput->attach_buffer != NULL, TDM_ERROR_NONE);
2351 private_client = private_voutput->private_client;
2353 pthread_mutex_lock(&private_client->lock);
2355 buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
2356 tbm_surface_internal_unref(buffer);
2357 private_voutput->attach_buffer = NULL;
2358 wl_tdm_voutput_commit_done(private_voutput->wl_voutput);
2360 pthread_mutex_unlock(&private_client->lock);
2362 return TDM_ERROR_NONE;
2366 tdm_client_voutput_get_client_output(tdm_client_voutput *voutput, tdm_error *error)
2368 tdm_private_client_voutput *private_voutput;
2369 tdm_private_client_output *private_output = NULL;
2370 tdm_private_client *private_client;
2371 tdm_error ret = TDM_ERROR_NONE;
2374 *error = TDM_ERROR_NONE;
2377 TDM_ERR("'!voutput' failed");
2379 *error = TDM_ERROR_INVALID_PARAMETER;
2383 private_voutput = (tdm_private_client_voutput *)voutput;
2384 private_client = private_voutput->private_client;
2386 pthread_mutex_lock(&private_client->lock);
2388 if (private_voutput->get_output) {
2389 pthread_mutex_unlock(&private_client->lock);
2390 return private_voutput->private_output;
2393 pthread_mutex_unlock(&private_client->lock);
2394 private_output = (tdm_private_client_output *)tdm_client_get_output(private_voutput->private_client, private_voutput->name, &ret);
2395 if (!private_output) {
2396 TDM_ERR("tdm_client_voutput_get_client_output get private_output fail");
2401 pthread_mutex_lock(&private_client->lock);
2402 private_output->voutput = private_voutput;
2403 private_voutput->private_output = private_output;
2404 private_voutput->get_output = 1;
2406 pthread_mutex_unlock(&private_client->lock);
2408 return private_output;
2412 _tdm_client_voutput_send_available_modes(tdm_private_client_voutput *private_voutput)
2414 tdm_client_output_mode *modes, *mode;
2415 struct wl_array array;
2418 modes = private_voutput->available_modes.modes;
2419 size = sizeof(tdm_client_output_mode);
2421 wl_array_init(&array);
2422 for (i = 0; i < private_voutput->available_modes.count; i++) {
2423 mode = wl_array_add(&array, size);
2424 memcpy(mode, &modes[i], size);
2426 wl_tdm_voutput_set_available_modes(private_voutput->wl_voutput, &array);
2427 wl_array_release(&array);
2431 tdm_client_voutput_connect(tdm_client_voutput *voutput)
2433 tdm_private_client_output *private_output = NULL;;
2434 tdm_private_client_voutput *private_voutput = NULL;
2435 tdm_private_client *private_client;
2437 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2439 private_voutput = (tdm_private_client_voutput *)voutput;
2440 private_client = private_voutput->private_client;
2441 private_output = private_voutput->private_output;
2443 pthread_mutex_lock(&private_client->lock);
2445 if (private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2446 pthread_mutex_unlock(&private_client->lock);
2447 return TDM_ERROR_NONE;
2450 if (!private_output->watch_output_changes)
2451 private_output->connection = TDM_OUTPUT_CONN_STATUS_CONNECTED;
2453 _tdm_client_voutput_send_available_modes(private_voutput);
2455 wl_tdm_voutput_set_physical_size(private_voutput->wl_voutput, private_voutput->mmwidth, private_voutput->mmheight);
2457 wl_tdm_voutput_connect(private_voutput->wl_voutput);
2459 pthread_mutex_unlock(&private_client->lock);
2461 return TDM_ERROR_NONE;
2465 tdm_client_voutput_disconnect(tdm_client_voutput *voutput)
2467 tdm_private_client_output *private_output = NULL;
2468 tdm_private_client_voutput *private_voutput = NULL;
2469 tdm_private_client *private_client;
2471 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2473 private_voutput = (tdm_private_client_voutput *)voutput;
2474 private_client = private_voutput->private_client;
2475 private_output = private_voutput->private_output;
2477 pthread_mutex_lock(&private_client->lock);
2479 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2480 pthread_mutex_unlock(&private_client->lock);
2481 return TDM_ERROR_NONE;
2484 if (!private_output->watch_output_changes)
2485 private_output->connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
2487 wl_tdm_voutput_disconnect(private_voutput->wl_voutput);
2489 pthread_mutex_unlock(&private_client->lock);
2491 return TDM_ERROR_NONE;
2496 tdm_client_voutput_set_mode(tdm_client_voutput *voutput, int index)
2498 tdm_private_client_output *private_output;
2499 tdm_private_client_voutput *private_voutput = NULL;
2500 tdm_private_client *private_client;
2502 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2503 TDM_RETURN_VAL_IF_FAIL(index >= 0, TDM_ERROR_INVALID_PARAMETER);
2505 private_voutput = (tdm_private_client_voutput *)voutput;
2506 private_client = private_voutput->private_client;
2507 private_output = private_voutput->private_output;
2509 pthread_mutex_lock(&private_client->lock);
2511 if (private_voutput->available_modes.count - 1 < index) {
2512 pthread_mutex_unlock(&private_client->lock);
2513 return TDM_ERROR_INVALID_PARAMETER;
2516 if ((private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) ||
2517 (private_voutput->available_modes.count == 0)) {
2518 pthread_mutex_unlock(&private_client->lock);
2519 return TDM_ERROR_BAD_REQUEST;
2522 TDM_DBG("mode_set request : %d", index);
2524 wl_tdm_voutput_set_mode(private_voutput->wl_voutput, index);
2526 pthread_mutex_unlock(&private_client->lock);
2528 return TDM_ERROR_NONE;