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_wait_list_validation_check(tdm_private_client_vblank *private_vblank)
231 if (private_vblank->wait_list.next == NULL || private_vblank->wait_list.prev == NULL) {
232 TDM_ERR("vblank(%p) wait_list broken. prev(%p), next(%p) pid(%d)",
233 private_vblank, private_vblank->wait_list.prev, private_vblank->wait_list.next, getpid());
240 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
241 uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
242 uint32_t tv_usec, uint32_t error)
244 tdm_private_client_vblank *private_vblank = data;
245 tdm_private_client *private_client;
246 tdm_client_wait_info *w = NULL, *wait_info = NULL;
248 TDM_RETURN_IF_FAIL(private_vblank != NULL);
250 private_client = private_vblank->private_output->private_client;
252 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
254 TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
255 private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
257 _tdm_client_vblank_wait_list_validation_check(private_vblank);
259 LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
260 if (w->req_id != req_id)
268 TDM_ERR("no wait infomation for req_id(%d)", req_id);
272 if (private_vblank->enable_ttrace)
273 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
275 if (wait_info->req_time >= private_vblank->last_time)
276 TDM_WRN("'req(%.6f) < last(%.6f)' failed. error(%d)", wait_info->req_time, private_vblank->last_time, error);
278 if (wait_info->need_free)
279 LIST_DEL(&wait_info->link);
281 if (wait_info->func) {
282 pthread_mutex_unlock(&private_client->lock);
283 wait_info->func(private_vblank, error, sequence, tv_sec, tv_usec, wait_info->user_data);
284 pthread_mutex_lock(&private_client->lock);
287 if (wait_info->need_free)
290 wait_info->need_free = 1;
294 /* LCOV_EXCL_START */
296 _tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
298 tdm_private_client_vblank *private_vblank = data;
299 tdm_private_client *private_client;
301 TDM_RETURN_IF_FAIL(private_vblank != NULL);
303 private_vblank->enable_ttrace = enable;
305 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
306 private_client = private_vblank->private_output->private_client;
308 private_client->enable_ttrace = enable;
312 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
313 _tdm_client_vblank_cb_stamp,
314 _tdm_client_vblank_cb_done,
315 _tdm_client_vblank_cb_ttrace,
319 _tdm_client_output_destroy(tdm_private_client_output *private_output)
321 tdm_private_client_vblank *v = NULL, *vv = NULL;
322 tdm_client_output_handler_info *h = NULL, *hh = NULL;
324 LIST_DEL(&private_output->link);
326 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
327 TDM_ERR("vblanks SHOULD be destroyed first!");
329 v->private_output = NULL;
332 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
337 wl_tdm_output_destroy(private_output->output);
339 free(private_output);
343 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
344 uint32_t width, uint32_t height, uint32_t refresh, uint32_t error)
346 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
348 TDM_RETURN_IF_FAIL(private_output != NULL);
350 private_output->width = width;
351 private_output->height = height;
352 private_output->refresh = refresh;
354 if (error != TDM_ERROR_NONE)
355 TDM_INFO("mode event error: %d", error);
357 TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
358 private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
359 width, height, refresh);
363 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
365 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
366 tdm_private_client *private_client;
367 tdm_client_output_handler_info *h = NULL, *hh = NULL;
369 struct list_head call_list;
371 TDM_RETURN_IF_FAIL(private_output != NULL);
373 private_client = private_output->private_client;
375 if (private_output->connection == value && value != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
378 private_output->connection = value;
380 if (error != TDM_ERROR_NONE)
381 TDM_INFO("connection event error: %d", error);
383 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
385 wl_proxy_get_id((struct wl_proxy*)private_output->output),
388 LIST_INITHEAD(&call_list);
390 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
391 LIST_ADDTAIL(&h->call_link, &call_list);
395 pthread_mutex_unlock(&private_client->lock);
396 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
398 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
400 pthread_mutex_lock(&private_client->lock);
404 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
406 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
407 tdm_private_client *private_client;
408 tdm_client_output_handler_info *h = NULL, *hh = NULL;
410 struct list_head call_list;
412 TDM_RETURN_IF_FAIL(private_output != NULL);
414 private_client = private_output->private_client;
416 /* If value is extended value, we handle it as DPMS on in client side
417 * The extended DPMS value is valid only in server side.
418 * Or, need to export to client side also?
420 if (value > TDM_OUTPUT_DPMS_OFF)
421 value = TDM_OUTPUT_DPMS_ON;
423 if (private_output->dpms == value)
426 private_output->dpms = value;
428 if (error != TDM_ERROR_NONE)
429 TDM_INFO("dpms event error: %d", error);
431 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
433 wl_proxy_get_id((struct wl_proxy*)private_output->output),
436 LIST_INITHEAD(&call_list);
438 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
439 LIST_ADDTAIL(&h->call_link, &call_list);
443 pthread_mutex_unlock(&private_client->lock);
444 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
446 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
448 pthread_mutex_lock(&private_client->lock);
451 static const struct wl_tdm_output_listener tdm_client_output_listener = {
452 _tdm_client_output_cb_mode,
453 _tdm_client_output_cb_connection,
454 _tdm_client_output_cb_dpms,
458 _tdm_client_cb_global(void *data, struct wl_registry *registry,
459 uint32_t name, const char *interface,
462 tdm_private_client *private_client = data;
464 if (strncmp(interface, "wl_tdm", 6) == 0) {
465 private_client->tdm =
466 wl_registry_bind(registry, name, &wl_tdm_interface, version);
467 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
469 wl_display_flush(private_client->display);
473 /* LCOV_EXCL_START */
475 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
480 static const struct wl_registry_listener tdm_client_registry_listener = {
481 _tdm_client_cb_global,
482 _tdm_client_cb_global_remove
486 tdm_client_create(tdm_error *error)
488 tdm_private_client *private_client;
490 private_client = calloc(1, sizeof *private_client);
491 if (!private_client) {
492 /* LCOV_EXCL_START */
494 TDM_ERR("alloc failed");
496 *error = TDM_ERROR_OUT_OF_MEMORY;
502 if (pthread_mutex_init(&private_client->lock, NULL)) {
503 TDM_ERR("mutex init failed: %m");
504 free(private_client);
506 *error = TDM_ERROR_OUT_OF_MEMORY;
510 LIST_INITHEAD(&private_client->output_list);
511 LIST_INITHEAD(&private_client->voutput_list);
513 private_client->display = wl_display_connect("tdm-socket");
514 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
516 private_client->queue = wl_display_create_queue(private_client->display);
517 TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
519 private_client->registry = wl_display_get_registry(private_client->display);
520 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
522 wl_registry_add_listener(private_client->registry,
523 &tdm_client_registry_listener, private_client);
524 wl_display_roundtrip(private_client->display);
526 if (CHECK_WL_PROTOCOL_ERROR(private_client))
529 /* check global objects */
530 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
533 *error = TDM_ERROR_NONE;
535 return (tdm_client*)private_client;
537 tdm_client_destroy((tdm_client*)private_client);
539 *error = TDM_ERROR_OPERATION_FAILED;
544 tdm_client_destroy(tdm_client *client)
546 tdm_private_client *private_client = (tdm_private_client*)client;
547 tdm_private_client_output *o = NULL, *oo = NULL;
548 tdm_private_client_voutput *vo = NULL, *voo = NULL;
553 pthread_mutex_lock(&private_client->lock);
555 if (private_client->temp_vblank) {
556 pthread_mutex_unlock(&private_client->lock);
557 tdm_client_vblank_destroy(private_client->temp_vblank);
558 pthread_mutex_lock(&private_client->lock);
561 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
562 _tdm_client_output_destroy(o);
565 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_client->voutput_list, link) {
566 tdm_client_voutput_destroy(vo);
569 if (private_client->tdm)
570 wl_tdm_destroy(private_client->tdm);
571 if (private_client->registry)
572 wl_registry_destroy(private_client->registry);
573 if (private_client->queue)
574 wl_event_queue_destroy(private_client->queue);
575 if (private_client->display)
576 wl_display_disconnect(private_client->display);
578 pthread_mutex_unlock(&private_client->lock);
579 pthread_mutex_destroy(&private_client->lock);
581 free(private_client);
585 tdm_client_get_fd(tdm_client *client, int *fd)
587 tdm_private_client *private_client;
589 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
590 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
592 private_client = (tdm_private_client*)client;
594 pthread_mutex_lock(&private_client->lock);
596 *fd = wl_display_get_fd(private_client->display);
598 pthread_mutex_unlock(&private_client->lock);
601 return TDM_ERROR_OPERATION_FAILED;
603 return TDM_ERROR_NONE;
607 tdm_client_handle_events(tdm_client *client)
609 tdm_private_client *private_client;
611 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
613 /* LCOV_EXCL_START */
614 private_client = (tdm_private_client*)client;
616 pthread_mutex_lock(&private_client->lock);
618 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
619 pthread_mutex_unlock(&private_client->lock);
620 return TDM_ERROR_PROTOCOL_ERROR;
623 if (private_client->enable_ttrace)
624 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
626 wl_display_dispatch(private_client->display);
628 if (private_client->enable_ttrace)
629 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
631 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
632 pthread_mutex_unlock(&private_client->lock);
633 return TDM_ERROR_PROTOCOL_ERROR;
636 pthread_mutex_unlock(&private_client->lock);
638 return TDM_ERROR_NONE;
643 _tdm_client_poll(struct wl_display *display, short int events, int timeout)
646 struct pollfd pfd[1];
648 pfd[0].fd = wl_display_get_fd(display);
649 pfd[0].events = events;
651 ret = poll(pfd, 1, timeout);
652 } while (ret == -1 && errno == EINTR);
658 _tdm_client_dispatch_timeout(tdm_private_client *private_client, int timeout)
661 struct wl_display *display = private_client->display;
663 if (wl_display_prepare_read(display) == -1) {
664 if (wl_display_dispatch_pending(display) > 0)
665 return TDM_ERROR_NONE;
667 return TDM_ERROR_OPERATION_FAILED;
671 ret = wl_display_flush(display);
673 if (ret != -1 || errno != EAGAIN)
676 if (_tdm_client_poll(display, POLLOUT, -1) == -1) {
677 wl_display_cancel_read(display);
678 TDM_ERR("_tdm_client_poll failed");
679 return TDM_ERROR_OPERATION_FAILED;
683 /* Don't stop if flushing hits an EPIPE; continue so we can read any
684 * protocol error that may have triggered it. */
685 if (ret < 0 && errno != EPIPE) {
686 TDM_ERR("ret(%d) errno(%d)", ret, errno);
687 wl_display_cancel_read(display);
688 return TDM_ERROR_OPERATION_FAILED;
691 ret = _tdm_client_poll(display, POLLIN, timeout);
693 wl_display_cancel_read(display);
695 TDM_ERR("_tdm_client_poll timeout.");
696 return TDM_ERROR_TIMEOUT;
698 TDM_ERR("_tdm_client_poll failed. (ret:%d)", ret);
699 return TDM_ERROR_OPERATION_FAILED;
703 if (wl_display_read_events(display) == -1) {
704 TDM_ERR("wl_display_read_events failed");
705 return TDM_ERROR_OPERATION_FAILED;
708 ret = wl_display_dispatch_pending(display);
711 TDM_ERR("_tdm_client_dispatch_timeout failed");
712 return TDM_ERROR_OPERATION_FAILED;
715 return TDM_ERROR_NONE;
719 tdm_client_handle_events_timeout(tdm_client *client, int ms_timeout)
721 tdm_private_client *private_client;
723 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
725 private_client = (tdm_private_client*)client;
727 pthread_mutex_lock(&private_client->lock);
729 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
730 pthread_mutex_unlock(&private_client->lock);
731 return TDM_ERROR_PROTOCOL_ERROR;
734 if (private_client->enable_ttrace)
735 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
737 ret = _tdm_client_dispatch_timeout(private_client, ms_timeout);
739 if (private_client->enable_ttrace)
740 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
742 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
743 pthread_mutex_unlock(&private_client->lock);
744 return TDM_ERROR_PROTOCOL_ERROR;
747 pthread_mutex_unlock(&private_client->lock);
752 typedef struct _tdm_client_vblank_temp {
753 tdm_client_vblank_handler2 func;
755 } tdm_client_vblank_temp;
757 /* LCOV_EXCL_START */
759 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
760 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
762 tdm_client_vblank_temp *vblank_temp = user_data;
764 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
765 TDM_RETURN_IF_FAIL(vblank != NULL);
767 if (vblank_temp->func)
768 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
774 /* LCOV_EXCL_START */ /* deprecated */
776 tdm_client_wait_vblank(tdm_client *client, char *name,
777 int sw_timer, int interval, int sync,
778 tdm_client_vblank_handler2 func, void *user_data)
780 tdm_private_client *private_client = (tdm_private_client*)client;
781 tdm_client_output *output;
782 tdm_client_vblank_temp *vblank_temp;
785 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
786 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
787 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
789 if (CHECK_WL_PROTOCOL_ERROR(private_client))
790 return TDM_ERROR_PROTOCOL_ERROR;
792 if (!private_client->temp_vblank) {
793 output = tdm_client_get_output(client, name, &ret);
794 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
796 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
797 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
800 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
801 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
803 vblank_temp = calloc(1, sizeof *vblank_temp);
804 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
806 vblank_temp->func = func;
807 vblank_temp->user_data = user_data;
809 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
814 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
816 tdm_private_client *private_client;
817 tdm_private_client_output *private_output = NULL;
818 struct wl_proxy *wrapper;
821 *error = TDM_ERROR_NONE;
824 TDM_ERR("'!client' failed");
826 *error = TDM_ERROR_INVALID_PARAMETER;
830 private_client = (tdm_private_client*)client;
832 pthread_mutex_lock(&private_client->lock);
834 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
836 *error = TDM_ERROR_PROTOCOL_ERROR;
837 pthread_mutex_unlock(&private_client->lock);
843 } else if (!strncmp(name, "primary", 7) || !strncmp(name, "default", 7)) {
844 TDM_DBG("get primary or default output");
846 tdm_private_client_voutput *private_voutput = NULL;
849 LIST_FOR_EACH_ENTRY(private_voutput, &private_client->voutput_list, link) {
850 if (!strncmp(private_voutput->name, name, TDM_NAME_LEN)) {
857 *error = TDM_ERROR_INVALID_PARAMETER;
858 pthread_mutex_unlock(&private_client->lock);
863 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
864 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
865 pthread_mutex_unlock(&private_client->lock);
866 return (tdm_client_output*)private_output;
870 wrapper = wl_proxy_create_wrapper(private_client->tdm);
872 TDM_ERR("create output_wrapper failed");
874 *error = TDM_ERROR_OUT_OF_MEMORY;
875 pthread_mutex_unlock(&private_client->lock);
879 wl_proxy_set_queue(wrapper, private_client->queue);
881 private_output = calloc(1, sizeof *private_output);
882 if (!private_output) {
883 /* LCOV_EXCL_START */
884 wl_proxy_wrapper_destroy(wrapper);
885 TDM_ERR("alloc failed");
887 *error = TDM_ERROR_OUT_OF_MEMORY;
888 pthread_mutex_unlock(&private_client->lock);
894 private_output->private_client = private_client;
896 snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
897 private_output->output = wl_tdm_create_output((struct wl_tdm *)wrapper, private_output->name);
898 wl_proxy_wrapper_destroy(wrapper);
899 if (!private_output->output) {
900 /* LCOV_EXCL_START */
902 TDM_ERR("couldn't create output resource");
903 free(private_output);
905 *error = TDM_ERROR_OUT_OF_MEMORY;
906 pthread_mutex_unlock(&private_client->lock);
912 LIST_INITHEAD(&private_output->vblank_list);
913 LIST_INITHEAD(&private_output->change_handler_list);
915 wl_tdm_output_add_listener(private_output->output,
916 &tdm_client_output_listener, private_output);
917 wl_display_roundtrip_queue(private_client->display, private_client->queue);
919 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
921 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
922 wl_tdm_output_destroy(private_output->output);
923 free(private_output);
925 *error = TDM_ERROR_PROTOCOL_ERROR;
926 pthread_mutex_unlock(&private_client->lock);
930 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
932 pthread_mutex_unlock(&private_client->lock);
934 return (tdm_client_output*)private_output;
938 tdm_client_output_add_change_handler(tdm_client_output *output,
939 tdm_client_output_change_handler func,
942 tdm_private_client_output *private_output;
943 tdm_private_client *private_client;
944 tdm_client_output_handler_info *h = NULL;
946 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
947 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
949 private_output = (tdm_private_client_output*)output;
950 private_client = private_output->private_client;
952 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
953 if (h->func == func && h->user_data == user_data) {
954 TDM_ERR("can't add twice");
955 return TDM_ERROR_BAD_REQUEST;
959 h = calloc(1, sizeof *h);
960 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
962 pthread_mutex_lock(&private_client->lock);
964 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
966 pthread_mutex_unlock(&private_client->lock);
967 return TDM_ERROR_PROTOCOL_ERROR;
970 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
971 wl_tdm_output_watch_output_changes(private_output->output, 1);
972 wl_display_roundtrip_queue(private_client->display, private_client->queue);
974 /* TODO: this is very tricky.
975 * If a client adds the change_handler, we might be able to guess that
976 * the client will watch the tdm client's fd and handle tdm events in
977 * event loop. Otherwise, we CAN'T make sure if a client has event loop
978 * which handles tdm events.
980 private_output->watch_output_changes = 1;
983 h->private_output = private_output;
985 h->user_data = user_data;
986 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
987 LIST_INITHEAD(&h->call_link);
989 pthread_mutex_unlock(&private_client->lock);
991 return TDM_ERROR_NONE;
995 tdm_client_output_remove_change_handler(tdm_client_output *output,
996 tdm_client_output_change_handler func,
999 tdm_private_client_output *private_output;
1000 tdm_private_client *private_client;
1001 tdm_client_output_handler_info *h = NULL;
1003 TDM_RETURN_IF_FAIL(output != NULL);
1004 TDM_RETURN_IF_FAIL(func != NULL);
1006 private_output = (tdm_private_client_output*)output;
1007 private_client = private_output->private_client;
1009 pthread_mutex_lock(&private_client->lock);
1011 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
1012 if (h->func != func || h->user_data != user_data)
1018 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
1019 private_output->watch_output_changes = 0;
1020 if (!CHECK_WL_PROTOCOL_ERROR(private_client)) {
1021 wl_tdm_output_watch_output_changes(private_output->output, 0);
1022 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1026 pthread_mutex_unlock(&private_client->lock);
1031 pthread_mutex_unlock(&private_client->lock);
1035 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
1037 tdm_private_client_output *private_output;
1038 tdm_private_client *private_client;
1040 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1041 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
1043 private_output = (tdm_private_client_output*)output;
1044 private_client = private_output->private_client;
1046 pthread_mutex_lock(&private_client->lock);
1048 if (private_output->watch_output_changes) {
1049 *refresh = private_output->refresh;
1050 pthread_mutex_unlock(&private_client->lock);
1051 return TDM_ERROR_NONE;
1054 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1055 pthread_mutex_unlock(&private_client->lock);
1056 return TDM_ERROR_PROTOCOL_ERROR;
1059 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1060 wl_tdm_output_get_mode(private_output->output);
1061 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1062 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1064 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1065 pthread_mutex_unlock(&private_client->lock);
1066 return TDM_ERROR_PROTOCOL_ERROR;
1069 *refresh = private_output->refresh;
1071 pthread_mutex_unlock(&private_client->lock);
1073 return TDM_ERROR_NONE;
1077 tdm_client_output_get_mode(tdm_client_output *output, unsigned int *width, unsigned int *height)
1079 tdm_private_client_output *private_output;
1080 tdm_private_client *private_client;
1082 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1083 TDM_RETURN_VAL_IF_FAIL(width != NULL, TDM_ERROR_INVALID_PARAMETER);
1084 TDM_RETURN_VAL_IF_FAIL(height != NULL, TDM_ERROR_INVALID_PARAMETER);
1086 private_output = (tdm_private_client_output*)output;
1087 private_client = private_output->private_client;
1089 pthread_mutex_lock(&private_client->lock);
1091 if (private_output->watch_output_changes) {
1092 *width = private_output->width;
1093 *height = private_output->height;
1094 pthread_mutex_unlock(&private_client->lock);
1095 return TDM_ERROR_NONE;
1098 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1099 pthread_mutex_unlock(&private_client->lock);
1100 return TDM_ERROR_PROTOCOL_ERROR;
1103 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1104 wl_tdm_output_get_mode(private_output->output);
1105 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1106 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1108 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1109 pthread_mutex_unlock(&private_client->lock);
1110 return TDM_ERROR_PROTOCOL_ERROR;
1113 *width = private_output->width;
1114 *height = private_output->height;
1116 pthread_mutex_unlock(&private_client->lock);
1118 return TDM_ERROR_NONE;
1122 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
1124 tdm_private_client_output *private_output;
1125 tdm_private_client *private_client;
1127 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1128 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
1130 private_output = (tdm_private_client_output*)output;
1131 private_client = private_output->private_client;
1133 pthread_mutex_lock(&private_client->lock);
1135 if (private_output->watch_output_changes) {
1136 *status = private_output->connection;
1137 pthread_mutex_unlock(&private_client->lock);
1138 return TDM_ERROR_NONE;
1141 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1142 pthread_mutex_unlock(&private_client->lock);
1143 return TDM_ERROR_PROTOCOL_ERROR;
1146 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1147 wl_tdm_output_get_connection(private_output->output);
1148 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1149 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1151 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1152 pthread_mutex_unlock(&private_client->lock);
1153 return TDM_ERROR_PROTOCOL_ERROR;
1156 *status = private_output->connection;
1158 pthread_mutex_unlock(&private_client->lock);
1160 return TDM_ERROR_NONE;
1164 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
1166 tdm_private_client_output *private_output;
1167 tdm_private_client *private_client;
1169 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1170 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
1172 private_output = (tdm_private_client_output*)output;
1173 private_client = private_output->private_client;
1175 if (private_output->watch_output_changes) {
1176 *dpms = private_output->dpms;
1177 return TDM_ERROR_NONE;
1180 pthread_mutex_lock(&private_client->lock);
1182 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1183 pthread_mutex_unlock(&private_client->lock);
1184 return TDM_ERROR_PROTOCOL_ERROR;
1187 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
1188 wl_tdm_output_get_dpms(private_output->output);
1189 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1190 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
1192 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1193 pthread_mutex_unlock(&private_client->lock);
1194 return TDM_ERROR_PROTOCOL_ERROR;
1197 *dpms = private_output->dpms;
1199 pthread_mutex_unlock(&private_client->lock);
1201 return TDM_ERROR_NONE;
1205 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
1207 tdm_private_client *private_client;
1208 tdm_private_client_output *private_output;
1209 tdm_private_client_vblank *private_vblank;
1210 struct wl_proxy *wrapper;
1213 *error = TDM_ERROR_NONE;
1216 TDM_ERR("'!output' failed");
1218 *error = TDM_ERROR_INVALID_PARAMETER;
1222 private_output = (tdm_private_client_output*)output;
1223 private_client = private_output->private_client;
1225 pthread_mutex_lock(&private_client->lock);
1227 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1229 *error = TDM_ERROR_PROTOCOL_ERROR;
1230 pthread_mutex_unlock(&private_client->lock);
1234 wrapper = wl_proxy_create_wrapper(private_output->output);
1236 TDM_ERR("create output_wrapper failed");
1238 *error = TDM_ERROR_OUT_OF_MEMORY;
1239 pthread_mutex_unlock(&private_client->lock);
1243 wl_proxy_set_queue(wrapper, private_client->queue);
1245 private_vblank = calloc(1, sizeof *private_vblank);
1246 if (!private_vblank) {
1247 /* LCOV_EXCL_START */
1249 TDM_ERR("alloc failed");
1250 wl_proxy_wrapper_destroy(wrapper);
1252 *error = TDM_ERROR_OUT_OF_MEMORY;
1253 pthread_mutex_unlock(&private_client->lock);
1256 /* LCOV_EXCL_STOP */
1259 private_vblank->private_output = private_output;
1261 private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
1262 wl_proxy_wrapper_destroy(wrapper);
1263 if (!private_vblank->vblank) {
1264 /* LCOV_EXCL_START */
1266 TDM_ERR("couldn't create vblank resource");
1267 free(private_vblank);
1269 *error = TDM_ERROR_OUT_OF_MEMORY;
1270 pthread_mutex_unlock(&private_client->lock);
1273 /* LCOV_EXCL_STOP */
1277 private_vblank->fps = private_output->refresh;
1278 private_vblank->offset = 0;
1279 private_vblank->enable_fake = 0;
1281 LIST_INITHEAD(&private_vblank->wait_list);
1283 wl_tdm_vblank_add_listener(private_vblank->vblank,
1284 &tdm_client_vblank_listener, private_vblank);
1285 wl_display_roundtrip_queue(private_client->display, private_client->queue);
1287 wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
1289 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1290 wl_tdm_vblank_destroy(private_vblank->vblank);
1291 free(private_vblank);
1293 *error = TDM_ERROR_PROTOCOL_ERROR;
1294 pthread_mutex_unlock(&private_client->lock);
1298 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
1300 pthread_mutex_unlock(&private_client->lock);
1302 return (tdm_client_vblank*)private_vblank;
1306 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
1308 tdm_private_client_vblank *private_vblank;
1309 tdm_private_client *private_client;
1310 tdm_client_wait_info *w = NULL, *ww = NULL;
1312 TDM_RETURN_IF_FAIL(vblank != NULL);
1314 private_vblank = vblank;
1315 private_client = private_vblank->private_output->private_client;
1317 pthread_mutex_lock(&private_client->lock);
1319 LIST_DEL(&private_vblank->link);
1321 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
1326 wl_tdm_vblank_destroy(private_vblank->vblank);
1328 free(private_vblank);
1330 pthread_mutex_unlock(&private_client->lock);
1334 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
1336 tdm_private_client_vblank *private_vblank;
1337 tdm_private_client *private_client;
1339 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1341 private_vblank = vblank;
1342 private_client = private_vblank->private_output->private_client;
1344 pthread_mutex_lock(&private_client->lock);
1346 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1347 pthread_mutex_unlock(&private_client->lock);
1348 return TDM_ERROR_PROTOCOL_ERROR;
1352 name = TDM_VBLANK_DEFAULT_NAME;
1354 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
1355 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
1357 wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
1359 pthread_mutex_unlock(&private_client->lock);
1361 return TDM_ERROR_NONE;
1365 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
1367 tdm_private_client_vblank *private_vblank;
1368 tdm_private_client *private_client;
1370 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1372 private_vblank = vblank;
1373 private_client = private_vblank->private_output->private_client;
1375 pthread_mutex_lock(&private_client->lock);
1376 private_vblank->sync = sync;
1377 pthread_mutex_unlock(&private_client->lock);
1379 return TDM_ERROR_NONE;
1383 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
1385 tdm_private_client_vblank *private_vblank;
1386 tdm_private_client *private_client;
1388 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1389 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
1391 private_vblank = vblank;
1392 private_client = private_vblank->private_output->private_client;
1394 pthread_mutex_lock(&private_client->lock);
1396 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1397 pthread_mutex_unlock(&private_client->lock);
1398 return TDM_ERROR_PROTOCOL_ERROR;
1401 if (private_vblank->fps == fps) {
1402 pthread_mutex_unlock(&private_client->lock);
1403 return TDM_ERROR_NONE;
1406 private_vblank->fps = fps;
1408 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
1410 pthread_mutex_unlock(&private_client->lock);
1412 return TDM_ERROR_NONE;
1416 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
1418 tdm_private_client_vblank *private_vblank;
1419 tdm_private_client *private_client;
1421 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1423 private_vblank = vblank;
1424 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
1426 private_client = private_vblank->private_output->private_client;
1428 pthread_mutex_lock(&private_client->lock);
1430 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1431 pthread_mutex_unlock(&private_client->lock);
1432 return TDM_ERROR_PROTOCOL_ERROR;
1435 if (private_vblank->offset == offset_ms) {
1436 pthread_mutex_unlock(&private_client->lock);
1437 return TDM_ERROR_NONE;
1440 private_vblank->offset = offset_ms;
1442 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
1444 pthread_mutex_unlock(&private_client->lock);
1446 return TDM_ERROR_NONE;
1450 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
1452 tdm_private_client_vblank *private_vblank;
1453 tdm_private_client *private_client;
1455 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1457 private_vblank = vblank;
1458 private_client = private_vblank->private_output->private_client;
1460 pthread_mutex_lock(&private_client->lock);
1462 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1463 pthread_mutex_unlock(&private_client->lock);
1464 return TDM_ERROR_PROTOCOL_ERROR;
1467 if (private_vblank->enable_fake == enable_fake) {
1468 pthread_mutex_unlock(&private_client->lock);
1469 return TDM_ERROR_NONE;
1472 private_vblank->enable_fake = enable_fake;
1474 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
1476 pthread_mutex_unlock(&private_client->lock);
1478 return TDM_ERROR_NONE;
1482 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
1484 tdm_private_client *private_client;
1485 tdm_private_client_output *private_output;
1486 tdm_private_client_vblank *private_vblank;
1487 tdm_client_wait_info *w;
1489 unsigned int req_sec, req_usec;
1492 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1493 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1494 /* can't support "interval 0" and "getting current_msc" things because
1495 * there is a socket communication between TDM client and server. It's impossible
1496 * to return the current msc or sequence immediately.
1498 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1500 private_vblank = vblank;
1501 private_output = private_vblank->private_output;
1502 private_client = private_output->private_client;
1504 pthread_mutex_lock(&private_client->lock);
1506 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1507 pthread_mutex_unlock(&private_client->lock);
1508 return TDM_ERROR_PROTOCOL_ERROR;
1511 if (!private_vblank->started)
1512 private_vblank->started = 1;
1514 if (!private_vblank->enable_fake) {
1515 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1516 TDM_ERR("output disconnected");
1517 pthread_mutex_unlock(&private_client->lock);
1518 return TDM_ERROR_OUTPUT_DISCONNECTED;
1520 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1521 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1522 pthread_mutex_unlock(&private_client->lock);
1523 return TDM_ERROR_DPMS_OFF;
1527 w = calloc(1, sizeof *w);
1529 /* LCOV_EXCL_START */
1531 TDM_ERR("alloc failed");
1532 pthread_mutex_unlock(&private_client->lock);
1533 return TDM_ERROR_OUT_OF_MEMORY;
1535 /* LCOV_EXCL_STOP */
1538 w->private_vblank = private_vblank;
1540 w->user_data = user_data;
1542 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1543 LIST_INITHEAD(&w->call_link);
1545 clock_gettime(CLOCK_MONOTONIC, &tp);
1546 req_sec = (unsigned int)tp.tv_sec;
1547 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1549 w->req_id = ++private_output->req_id;
1550 w->req_time = TDM_TIME(req_sec, req_usec);
1551 w->need_free = (private_vblank->sync) ? 0 : 1;
1553 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
1555 if (private_vblank->enable_ttrace)
1556 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1558 TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
1559 vblank, interval, w->req_id, w->req_time);
1561 private_vblank->req_time = w->req_time;
1563 if (private_vblank->last_time >= w->req_time)
1564 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1566 if (!private_vblank->sync) {
1567 wl_display_flush(private_client->display);
1568 pthread_mutex_unlock(&private_client->lock);
1569 return TDM_ERROR_NONE;
1572 /* LCOV_EXCL_START */
1574 while (ret != -1 && !w->need_free)
1575 ret = wl_display_dispatch(private_client->display);
1577 clock_gettime(CLOCK_MONOTONIC, &tp);
1578 TDM_DBG("block during %d us",
1579 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1580 - (req_sec * 1000000 + req_usec));
1585 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1586 pthread_mutex_unlock(&private_client->lock);
1587 return TDM_ERROR_PROTOCOL_ERROR;
1590 pthread_mutex_unlock(&private_client->lock);
1592 return TDM_ERROR_NONE;
1594 /* LCOV_EXCL_STOP */
1598 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
1599 tdm_client_vblank_handler func, void *user_data)
1601 tdm_private_client *private_client;
1602 tdm_private_client_output *private_output;
1603 tdm_private_client_vblank *private_vblank;
1604 tdm_client_wait_info *w;
1606 unsigned int req_sec, req_usec;
1609 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1610 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1612 private_vblank = vblank;
1613 private_output = private_vblank->private_output;
1614 private_client = private_output->private_client;
1616 pthread_mutex_lock(&private_client->lock);
1618 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1619 pthread_mutex_unlock(&private_client->lock);
1620 return TDM_ERROR_PROTOCOL_ERROR;
1623 if (!private_vblank->started)
1624 private_vblank->started = 1;
1626 if (!private_vblank->enable_fake) {
1627 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1628 TDM_ERR("output disconnected");
1629 pthread_mutex_unlock(&private_client->lock);
1630 return TDM_ERROR_OUTPUT_DISCONNECTED;
1632 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1633 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1634 pthread_mutex_unlock(&private_client->lock);
1635 return TDM_ERROR_DPMS_OFF;
1639 w = calloc(1, sizeof *w);
1641 /* LCOV_EXCL_START */
1643 TDM_ERR("alloc failed");
1644 pthread_mutex_unlock(&private_client->lock);
1645 return TDM_ERROR_OUT_OF_MEMORY;
1647 /* LCOV_EXCL_STOP */
1650 w->private_vblank = private_vblank;
1652 w->user_data = user_data;
1654 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1655 LIST_INITHEAD(&w->call_link);
1657 clock_gettime(CLOCK_MONOTONIC, &tp);
1658 req_sec = (unsigned int)tp.tv_sec;
1659 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1661 w->req_id = ++private_output->req_id;
1662 w->req_time = TDM_TIME(req_sec, req_usec);
1663 w->need_free = (private_vblank->sync) ? 0 : 1;
1665 wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
1667 if (private_vblank->enable_ttrace)
1668 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1670 TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
1671 vblank, sequence, w->req_id, w->req_time);
1673 private_vblank->req_time = w->req_time;
1675 if (private_vblank->last_time >= w->req_time)
1676 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1678 if (!private_vblank->sync) {
1679 wl_display_flush(private_client->display);
1680 pthread_mutex_unlock(&private_client->lock);
1681 return TDM_ERROR_NONE;
1684 /* LCOV_EXCL_START */
1686 while (ret != -1 && !w->need_free)
1687 ret = wl_display_dispatch(private_client->display);
1689 clock_gettime(CLOCK_MONOTONIC, &tp);
1690 TDM_DBG("block during %d us",
1691 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1692 - (req_sec * 1000000 + req_usec));
1697 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
1698 pthread_mutex_unlock(&private_client->lock);
1699 return TDM_ERROR_PROTOCOL_ERROR;
1702 pthread_mutex_unlock(&private_client->lock);
1704 return TDM_ERROR_NONE;
1706 /* LCOV_EXCL_STOP */
1710 tdm_client_vblank_is_waiting(tdm_client_vblank *vblank)
1712 tdm_private_client_vblank *private_vblank;
1714 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, 0);
1716 private_vblank = vblank;
1718 return (LIST_LENGTH(&private_vblank->wait_list) > 0) ? 1 : 0;
1721 static tbm_surface_h
1722 _tdm_client_voutput_create_surface_from_param(tbm_bufmgr bufmgr,
1730 struct wl_array *plane_buf_idx,
1731 struct wl_array *plane_offset,
1732 struct wl_array *plane_stride,
1733 struct wl_array *plane_size,
1740 int32_t names[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
1741 tbm_surface_info_s info = { 0, };
1742 tbm_bo bos[TBM_SURF_PLANE_MAX];
1743 int i, numPlane, numName;
1744 tbm_surface_h tbm_surface;
1746 numPlane = tbm_surface_internal_get_num_planes(format);
1747 TDM_RETURN_VAL_IF_FAIL(numPlane == num_plane, NULL);
1750 info.height = height;
1751 info.format = format;
1754 info.num_planes = numPlane;
1757 for (i = 0; i < numPlane; i++) {
1758 info.planes[i].offset = *TDM_ARRAY_NTH_DATA(plane_offset, int32_t, i);
1759 info.planes[i].stride = *TDM_ARRAY_NTH_DATA(plane_stride, int32_t, i);
1760 info.planes[i].size = *TDM_ARRAY_NTH_DATA(plane_size, int32_t, i);
1769 for (i = 0; i < numName; i++) {
1771 bos[i] = tbm_bo_import_fd(bufmgr, names[i]);
1773 bos[i] = tbm_bo_import(bufmgr, names[i]);
1776 tbm_surface = tbm_surface_internal_create_with_bos(&info, bos, numName);
1777 if (tbm_surface == NULL) {
1792 for (i = 0; i < numName; i++)
1793 tbm_bo_unref(bos[i]);
1798 tdm_client_voutput_cb_buffer_import_with_id(void *data,
1799 struct wl_tdm_voutput *wl_voutput,
1800 struct wl_buffer *wl_buffer,
1807 struct wl_array *plane_buf_idx,
1808 struct wl_array *plane_offset,
1809 struct wl_array *plane_stride,
1810 struct wl_array *plane_size,
1817 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1818 tdm_private_client_buffer *buffer = NULL;
1819 tbm_surface_h tbm_surface;
1821 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1823 buffer = calloc(1, sizeof *buffer);
1824 TDM_RETURN_IF_FAIL(buffer != NULL);
1826 tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 0,
1827 width, height, format, bpp, size,
1829 plane_buf_idx, plane_offset, plane_stride, plane_size,
1833 TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
1835 tbm_surface_internal_ref(tbm_surface);
1836 wl_buffer_set_user_data(wl_buffer, tbm_surface);
1838 buffer->wl_buffer = wl_buffer;
1840 LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
1849 wl_buffer_destroy(wl_buffer);
1853 tdm_client_voutput_cb_buffer_import_with_fd(void *data,
1854 struct wl_tdm_voutput *wl_voutput,
1855 struct wl_buffer *wl_buffer,
1862 struct wl_array *plane_buf_idx,
1863 struct wl_array *plane_offset,
1864 struct wl_array *plane_stride,
1865 struct wl_array *plane_size,
1872 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1873 tdm_private_client_buffer *buffer = NULL;
1874 tbm_surface_h tbm_surface;
1876 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1878 buffer = calloc(1, sizeof *buffer);
1879 TDM_RETURN_IF_FAIL(buffer != NULL);
1881 tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 1,
1882 width, height, format, bpp, size,
1884 plane_buf_idx, plane_offset, plane_stride, plane_size,
1888 TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
1890 tbm_surface_internal_ref(tbm_surface);
1891 wl_buffer_set_user_data(wl_buffer, tbm_surface);
1893 buffer->wl_buffer = wl_buffer;
1895 LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
1904 wl_buffer_destroy(wl_buffer);
1908 tdm_client_voutput_cb_buffer_destroy(void *data,
1909 struct wl_tdm_voutput *wl_voutput,
1910 struct wl_buffer *wl_buffer)
1912 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1913 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
1914 tbm_surface_h tbm_surface = NULL;
1916 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1918 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
1919 if (wl_buffer == cb->wl_buffer) {
1920 LIST_DEL(&cb->link);
1922 tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(wl_buffer);
1924 tbm_surface_internal_unref(tbm_surface);
1926 wl_buffer_set_user_data(wl_buffer, NULL);
1927 wl_buffer_destroy(wl_buffer);
1939 tdm_client_voutput_cb_attach_buffer(void *data,
1940 struct wl_tdm_voutput *wl_voutput,
1941 struct wl_buffer *wl_buffer)
1943 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
1944 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
1946 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1948 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
1949 if (wl_buffer == cb->wl_buffer) {
1950 private_voutput->attach_buffer = cb;
1959 tdm_client_voutput_cb_commit(void *data, struct wl_tdm_voutput *wl_voutput)
1961 tdm_private_client_voutput *private_voutput;
1962 tdm_private_client *private_client;
1963 tbm_surface_h buffer = NULL;
1964 tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
1965 struct list_head call_list;
1967 private_voutput = (tdm_private_client_voutput *)data;
1968 TDM_RETURN_IF_FAIL(private_voutput != NULL);
1969 TDM_RETURN_IF_FAIL(private_voutput->attach_buffer != NULL);
1971 buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
1972 TDM_RETURN_IF_FAIL(buffer != NULL);
1974 tbm_surface_internal_ref(buffer);
1976 private_client = private_voutput->private_client;
1978 LIST_INITHEAD(&call_list);
1980 LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
1981 LIST_ADDTAIL(&h->call_link, &call_list);
1984 pthread_mutex_unlock(&private_client->lock);
1985 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
1987 h->func(private_voutput, buffer, h->user_data);
1990 /* if no added commit_handler call commit done immediately */
1991 if (LIST_IS_EMPTY(&private_voutput->commit_handler_list))
1992 tdm_client_voutput_commit_done(private_voutput);
1994 pthread_mutex_lock(&private_client->lock);
1998 tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput, uint32_t msg)
2000 tdm_private_client_voutput *private_voutput = data;
2002 private_voutput->msg = msg;
2005 static const struct wl_tdm_voutput_listener tdm_client_voutput_lisntener = {
2006 tdm_client_voutput_cb_buffer_import_with_id,
2007 tdm_client_voutput_cb_buffer_import_with_fd,
2008 tdm_client_voutput_cb_buffer_destroy,
2009 tdm_client_voutput_cb_attach_buffer,
2010 tdm_client_voutput_cb_commit,
2011 tdm_client_voutput_cb_ack_message
2014 tdm_client_voutput *
2015 tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error)
2017 tdm_private_client *private_client;
2018 tdm_private_client_output *private_output = NULL;
2019 tdm_private_client_voutput *private_voutput;
2020 struct wl_proxy *wrapper;
2023 *error = TDM_ERROR_NONE;
2026 TDM_ERR("'!client' failed");
2028 *error = TDM_ERROR_INVALID_PARAMETER;
2033 TDM_ERR("'!name' failed");
2035 *error = TDM_ERROR_INVALID_PARAMETER;
2039 private_client = (tdm_private_client *)client;
2041 pthread_mutex_lock(&private_client->lock);
2043 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2045 *error = TDM_ERROR_PROTOCOL_ERROR;
2046 pthread_mutex_unlock(&private_client->lock);
2050 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
2051 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
2053 *error = TDM_ERROR_INVALID_PARAMETER; // FIXME define new error type.
2054 pthread_mutex_unlock(&private_client->lock);
2059 wrapper = wl_proxy_create_wrapper(private_client->tdm);
2061 TDM_ERR("create virtual output wrapper failed");
2063 *error = TDM_ERROR_OUT_OF_MEMORY;
2064 pthread_mutex_unlock(&private_client->lock);
2068 wl_proxy_set_queue(wrapper, private_client->queue);
2070 private_voutput = calloc(1, sizeof *private_voutput);
2071 if (!private_voutput) {
2072 /* LOCV_EXCL_START */
2073 wl_proxy_wrapper_destroy(wrapper);
2074 TDM_ERR("alloc failed");
2076 *error = TDM_ERROR_OUT_OF_MEMORY;
2077 pthread_mutex_unlock(&private_client->lock);
2079 /* LOCV_EXCL_STOP */
2082 private_voutput->bufmgr = tbm_bufmgr_init(-1);
2083 if (private_voutput->bufmgr == NULL) {
2084 /* LCOV_EXCL_START */
2085 TDM_ERR("fail tbm_bufmgr_init");
2086 free(private_voutput);
2088 *error = TDM_ERROR_OUT_OF_MEMORY;
2089 pthread_mutex_unlock(&private_client->lock);
2091 /* LCOV_EXCL_STOP */
2094 LIST_INITHEAD(&private_voutput->commit_handler_list);
2095 LIST_INITHEAD(&private_voutput->buffer_list);
2097 private_voutput->private_client = private_client;
2098 strncpy(private_voutput->name, name, TDM_NAME_LEN - 1);
2099 private_voutput->name[TDM_NAME_LEN - 1] = '\0';
2101 private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, name);
2102 wl_proxy_wrapper_destroy(wrapper);
2103 if (!private_voutput->wl_voutput) {
2104 /* LCOV_EXCL_START */
2105 TDM_ERR("couldn't create voutput resource");
2106 free(private_voutput);
2108 *error = TDM_ERROR_OUT_OF_MEMORY;
2109 pthread_mutex_unlock(&private_client->lock);
2111 /* LCOV_EXCL_STOP */
2114 wl_tdm_voutput_add_listener(private_voutput->wl_voutput,
2115 &tdm_client_voutput_lisntener, private_voutput);
2116 wl_display_roundtrip_queue(private_client->display, private_client->queue);
2118 wl_proxy_set_queue((struct wl_proxy *)private_voutput->wl_voutput, NULL);
2120 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2121 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2122 free(private_voutput);
2124 *error = TDM_ERROR_PROTOCOL_ERROR;
2125 pthread_mutex_unlock(&private_client->lock);
2129 if (private_voutput->msg != WL_TDM_VOUTPUT_MESSAGE_ADDED) {
2130 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2131 free(private_voutput);
2133 *error = TDM_ERROR_PROTOCOL_ERROR; // FIXME add new error type.
2134 pthread_mutex_unlock(&private_client->lock);
2138 LIST_ADDTAIL(&private_voutput->link, &private_client->voutput_list);
2140 pthread_mutex_unlock(&private_client->lock);
2142 return (tdm_client_voutput *)private_voutput;
2146 tdm_client_voutput_destroy(tdm_client_voutput *voutput)
2148 tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)voutput;
2149 tdm_private_client *private_client;
2150 tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
2152 if (!private_voutput)
2154 private_client = private_voutput->private_client;
2156 pthread_mutex_lock(&private_client->lock);
2158 if (!(LIST_IS_EMPTY(&private_voutput->buffer_list))) {
2159 tdm_private_client_buffer *cb = NULL, *cbb = NULL;
2161 LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
2162 tbm_surface_h tbm_surface = NULL;
2164 LIST_DEL(&cb->link);
2166 tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(cb->wl_buffer);
2168 tbm_surface_internal_unref(tbm_surface);
2170 wl_buffer_set_user_data(cb->wl_buffer, NULL);
2171 wl_buffer_destroy(cb->wl_buffer);
2177 if (private_voutput->bufmgr)
2178 tbm_bufmgr_deinit(private_voutput->bufmgr);
2180 if (private_voutput->available_modes.modes)
2181 free(private_voutput->available_modes.modes);
2183 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_voutput->commit_handler_list, link) {
2188 if (private_voutput->get_output)
2189 _tdm_client_output_destroy(private_voutput->private_output);
2191 wl_tdm_voutput_destroy(private_voutput->wl_voutput);
2193 LIST_DEL(&private_voutput->link);
2195 free(private_voutput);
2197 pthread_mutex_unlock(&private_client->lock);
2201 tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_client_output_mode *modes, int count)
2203 tdm_private_client_voutput *private_voutput;
2204 tdm_private_client *private_client;
2205 tdm_error ret = TDM_ERROR_NONE;
2207 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2209 if ((count > 0) && (modes == NULL))
2210 return TDM_ERROR_INVALID_PARAMETER;
2212 private_voutput = (tdm_private_client_voutput *)voutput;
2213 private_client = private_voutput->private_client;
2215 if (!private_voutput->private_output) {
2216 private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
2217 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
2220 pthread_mutex_lock(&private_client->lock);
2222 if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2223 pthread_mutex_unlock(&private_client->lock);
2224 return TDM_ERROR_BAD_REQUEST;
2227 if (private_voutput->available_modes.modes)
2228 free(private_voutput->available_modes.modes);
2230 private_voutput->available_modes.count = count;
2233 private_voutput->available_modes.modes = calloc(count, sizeof(tdm_client_output_mode));
2234 if (private_voutput->available_modes.modes == NULL) {
2235 private_voutput->available_modes.count = 0;
2236 pthread_mutex_unlock(&private_client->lock);
2237 return TDM_ERROR_OUT_OF_MEMORY;
2239 memcpy(private_voutput->available_modes.modes, modes, sizeof(tdm_client_output_mode) * count);
2242 pthread_mutex_unlock(&private_client->lock);
2244 return TDM_ERROR_NONE;
2248 tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, unsigned int mmWidth, unsigned int mmHeight)
2250 tdm_private_client_voutput *private_voutput;
2251 tdm_private_client *private_client;
2252 tdm_error ret = TDM_ERROR_NONE;
2254 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2255 TDM_RETURN_VAL_IF_FAIL(mmWidth != 0, TDM_ERROR_INVALID_PARAMETER);
2256 TDM_RETURN_VAL_IF_FAIL(mmHeight != 0, TDM_ERROR_INVALID_PARAMETER);
2258 private_voutput = (tdm_private_client_voutput *)voutput;
2259 private_client = private_voutput->private_client;
2261 if (!private_voutput->private_output) {
2262 private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
2263 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
2266 pthread_mutex_lock(&private_client->lock);
2268 if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2269 pthread_mutex_unlock(&private_client->lock);
2270 return TDM_ERROR_BAD_REQUEST;
2273 private_voutput->mmwidth = mmWidth;
2274 private_voutput->mmheight = mmHeight;
2276 pthread_mutex_unlock(&private_client->lock);
2278 return TDM_ERROR_NONE;
2282 tdm_client_voutput_add_commit_handler(tdm_client_voutput *voutput,
2283 tdm_client_voutput_commit_handler func,
2286 tdm_private_client_voutput *private_voutput;
2287 tdm_private_client *private_client;
2288 tdm_client_voutput_commit_handler_info *h = NULL;
2290 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2291 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
2293 private_voutput = (tdm_private_client_voutput *)voutput;
2294 private_client = private_voutput->private_client;
2296 LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
2297 if (h->func == func && h->user_data == user_data) {
2298 TDM_ERR("can't add twice");
2299 return TDM_ERROR_BAD_REQUEST;
2303 h = calloc(1, sizeof *h);
2304 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
2306 pthread_mutex_lock(&private_client->lock);
2308 if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
2310 pthread_mutex_unlock(&private_client->lock);
2311 return TDM_ERROR_PROTOCOL_ERROR;
2314 h->private_voutput = private_voutput;
2316 h->user_data = user_data;
2317 LIST_ADDTAIL(&h->link, &private_voutput->commit_handler_list);
2318 LIST_INITHEAD(&h->call_link);
2320 pthread_mutex_unlock(&private_client->lock);
2322 return TDM_ERROR_NONE;
2326 tdm_client_voutput_remove_commit_handler(tdm_client_voutput *voutput,
2327 tdm_client_voutput_commit_handler func,
2330 tdm_private_client_voutput *private_voutput;
2331 tdm_private_client *private_client;
2332 tdm_client_voutput_commit_handler_info *h = NULL;
2334 TDM_RETURN_IF_FAIL(voutput != NULL);
2335 TDM_RETURN_IF_FAIL(func != NULL);
2337 private_voutput = (tdm_private_client_voutput *)voutput;
2338 private_client = private_voutput->private_client;
2340 pthread_mutex_lock(&private_client->lock);
2342 LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
2343 if (h->func != func || h->user_data != user_data)
2349 pthread_mutex_unlock(&private_client->lock);
2354 pthread_mutex_unlock(&private_client->lock);
2358 tdm_client_voutput_commit_done(tdm_client_voutput *voutput)
2360 tdm_private_client_voutput *private_voutput;
2361 tdm_private_client *private_client;
2362 tbm_surface_h buffer = NULL;
2364 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2366 private_voutput = (tdm_private_client_voutput *)voutput;
2367 TDM_RETURN_VAL_IF_FAIL(private_voutput->attach_buffer != NULL, TDM_ERROR_NONE);
2369 private_client = private_voutput->private_client;
2371 pthread_mutex_lock(&private_client->lock);
2373 buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
2374 tbm_surface_internal_unref(buffer);
2375 private_voutput->attach_buffer = NULL;
2376 wl_tdm_voutput_commit_done(private_voutput->wl_voutput);
2378 pthread_mutex_unlock(&private_client->lock);
2380 return TDM_ERROR_NONE;
2384 tdm_client_voutput_get_client_output(tdm_client_voutput *voutput, tdm_error *error)
2386 tdm_private_client_voutput *private_voutput;
2387 tdm_private_client_output *private_output = NULL;
2388 tdm_private_client *private_client;
2389 tdm_error ret = TDM_ERROR_NONE;
2392 *error = TDM_ERROR_NONE;
2395 TDM_ERR("'!voutput' failed");
2397 *error = TDM_ERROR_INVALID_PARAMETER;
2401 private_voutput = (tdm_private_client_voutput *)voutput;
2402 private_client = private_voutput->private_client;
2404 pthread_mutex_lock(&private_client->lock);
2406 if (private_voutput->get_output) {
2407 pthread_mutex_unlock(&private_client->lock);
2408 return private_voutput->private_output;
2411 pthread_mutex_unlock(&private_client->lock);
2412 private_output = (tdm_private_client_output *)tdm_client_get_output(private_voutput->private_client, private_voutput->name, &ret);
2413 if (!private_output) {
2414 TDM_ERR("tdm_client_voutput_get_client_output get private_output fail");
2419 pthread_mutex_lock(&private_client->lock);
2420 private_output->voutput = private_voutput;
2421 private_voutput->private_output = private_output;
2422 private_voutput->get_output = 1;
2424 pthread_mutex_unlock(&private_client->lock);
2426 return private_output;
2430 _tdm_client_voutput_send_available_modes(tdm_private_client_voutput *private_voutput)
2432 tdm_client_output_mode *modes, *mode;
2433 struct wl_array array;
2436 modes = private_voutput->available_modes.modes;
2437 size = sizeof(tdm_client_output_mode);
2439 wl_array_init(&array);
2440 for (i = 0; i < private_voutput->available_modes.count; i++) {
2441 mode = wl_array_add(&array, size);
2442 memcpy(mode, &modes[i], size);
2444 wl_tdm_voutput_set_available_modes(private_voutput->wl_voutput, &array);
2445 wl_array_release(&array);
2449 tdm_client_voutput_connect(tdm_client_voutput *voutput)
2451 tdm_private_client_output *private_output = NULL;;
2452 tdm_private_client_voutput *private_voutput = NULL;
2453 tdm_private_client *private_client;
2455 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2457 private_voutput = (tdm_private_client_voutput *)voutput;
2458 private_client = private_voutput->private_client;
2459 private_output = private_voutput->private_output;
2461 pthread_mutex_lock(&private_client->lock);
2463 if (private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2464 pthread_mutex_unlock(&private_client->lock);
2465 return TDM_ERROR_NONE;
2468 if (!private_output->watch_output_changes)
2469 private_output->connection = TDM_OUTPUT_CONN_STATUS_CONNECTED;
2471 _tdm_client_voutput_send_available_modes(private_voutput);
2473 wl_tdm_voutput_set_physical_size(private_voutput->wl_voutput, private_voutput->mmwidth, private_voutput->mmheight);
2475 wl_tdm_voutput_connect(private_voutput->wl_voutput);
2477 pthread_mutex_unlock(&private_client->lock);
2479 return TDM_ERROR_NONE;
2483 tdm_client_voutput_disconnect(tdm_client_voutput *voutput)
2485 tdm_private_client_output *private_output = NULL;
2486 tdm_private_client_voutput *private_voutput = NULL;
2487 tdm_private_client *private_client;
2489 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2491 private_voutput = (tdm_private_client_voutput *)voutput;
2492 private_client = private_voutput->private_client;
2493 private_output = private_voutput->private_output;
2495 pthread_mutex_lock(&private_client->lock);
2497 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
2498 pthread_mutex_unlock(&private_client->lock);
2499 return TDM_ERROR_NONE;
2502 if (!private_output->watch_output_changes)
2503 private_output->connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
2505 wl_tdm_voutput_disconnect(private_voutput->wl_voutput);
2507 pthread_mutex_unlock(&private_client->lock);
2509 return TDM_ERROR_NONE;
2514 tdm_client_voutput_set_mode(tdm_client_voutput *voutput, int index)
2516 tdm_private_client_output *private_output;
2517 tdm_private_client_voutput *private_voutput = NULL;
2518 tdm_private_client *private_client;
2520 TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2521 TDM_RETURN_VAL_IF_FAIL(index >= 0, TDM_ERROR_INVALID_PARAMETER);
2523 private_voutput = (tdm_private_client_voutput *)voutput;
2524 private_client = private_voutput->private_client;
2525 private_output = private_voutput->private_output;
2527 pthread_mutex_lock(&private_client->lock);
2529 if (private_voutput->available_modes.count - 1 < index) {
2530 pthread_mutex_unlock(&private_client->lock);
2531 return TDM_ERROR_INVALID_PARAMETER;
2534 if ((private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) ||
2535 (private_voutput->available_modes.count == 0)) {
2536 pthread_mutex_unlock(&private_client->lock);
2537 return TDM_ERROR_BAD_REQUEST;
2540 TDM_DBG("mode_set request : %d", index);
2542 wl_tdm_voutput_set_mode(private_voutput->wl_voutput, index);
2544 pthread_mutex_unlock(&private_client->lock);
2546 return TDM_ERROR_NONE;