virtual: add protocol to share wl_buffer(tbm_surface) with tdm_client
[platform/core/uifw/libtdm.git] / client / tdm_client.c
index a257939..53434f1 100644 (file)
@@ -9,7 +9,7 @@
  *          Taeheon Kim <th908.kim@samsung.com>,
  *          YoungJun Cho <yj44.cho@samsung.com>,
  *          SooChan Lim <sc1.lim@samsung.com>,
- *          Boram Park <sc1.lim@samsung.com>
+ *          Boram Park <boram1288.park@samsung.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
@@ -33,6 +33,8 @@
  *
 **************************************************************************/
 
+#define WL_HIDE_DEPRECATED
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -42,6 +44,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <strings.h>
+#include <poll.h>
 
 #include <tdm-client-protocol.h>
 
@@ -52,6 +55,8 @@
 #include "tdm.h"
 #include "tdm_private.h"
 
+#define TDM_ARRAY_NTH_DATA(array, type, n) (((type*)((array)->data)) + n)
+
 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
 
 typedef struct _tdm_private_client {
@@ -88,6 +93,32 @@ typedef struct _tdm_private_client_output {
        struct list_head link;
 } tdm_private_client_output;
 
+typedef struct _tdm_private_client_buffer {
+       struct list_head link;
+       struct wl_buffer *wl_buffer;
+} tdm_private_client_buffer;
+
+typedef struct _tdm_private_client_voutput {
+    tdm_private_client_output base;
+       struct wl_tdm_voutput *wl_voutput;
+       struct list_head commit_handler_list;
+
+       struct
+       {
+               int count;
+               tdm_client_output_mode *modes;
+       } available_modes;
+
+       unsigned int mmwidth;
+       unsigned int mmheight;
+
+       uint32_t msg;
+
+       struct list_head buffer_list;
+       tbm_bufmgr bufmgr;
+       tdm_private_client_buffer *commit_buffer;
+} tdm_private_client_voutput;
+
 struct _tdm_private_client_vblank {
        tdm_private_client_output *private_output;
 
@@ -134,6 +165,17 @@ typedef struct _tdm_client_wait_info {
        struct list_head call_link;
 } tdm_client_wait_info;
 
+typedef struct _tdm_client_voutput_commit_handler_info
+{
+       tdm_private_client_voutput *private_voutput;
+
+       tdm_client_voutput_commit_handler func;
+       void *user_data;
+
+       struct list_head link;
+       struct list_head call_link;
+} tdm_client_voutput_commit_handler_info;
+
 static unsigned int
 _tdm_client_check_wl_error(tdm_private_client *private_client, const char *func, int line)
 {
@@ -570,6 +612,116 @@ tdm_client_handle_events(tdm_client *client)
        /* LCOV_EXCL_STOP */
 }
 
+static int
+_tdm_client_poll(struct wl_display *display, short int events, int timeout)
+{
+       int ret;
+       struct pollfd pfd[1];
+
+       pfd[0].fd = wl_display_get_fd(display);
+       pfd[0].events = events;
+       do {
+               ret = poll(pfd, 1, timeout);
+       } while (ret == -1 && errno == EINTR);
+
+       return ret;
+}
+
+static tdm_error
+_tdm_client_dispatch_timeout(tdm_private_client *private_client, int timeout)
+{
+       int ret;
+       struct wl_display *display = private_client->display;
+
+       if (wl_display_prepare_read(display) == -1) {
+               if (wl_display_dispatch_pending(display) > 0)
+                       return TDM_ERROR_NONE;
+               else
+                       return TDM_ERROR_OPERATION_FAILED;
+       }
+
+       while (true) {
+               ret = wl_display_flush(display);
+
+               if (ret != -1 || errno != EAGAIN)
+                       break;
+
+               if (_tdm_client_poll(display, POLLOUT, -1) == -1) {
+                       wl_display_cancel_read(display);
+                       TDM_ERR("_tdm_client_poll failed");
+                       return TDM_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       /* Don't stop if flushing hits an EPIPE; continue so we can read any
+        * protocol error that may have triggered it. */
+       if (ret < 0 && errno != EPIPE) {
+               TDM_ERR("ret(%d) errno(%d)", ret, errno);
+               wl_display_cancel_read(display);
+               return TDM_ERROR_OPERATION_FAILED;
+       }
+
+       ret = _tdm_client_poll(display, POLLIN, timeout);
+       if (ret <= 0) {
+               wl_display_cancel_read(display);
+               if (ret == 0) {
+                       TDM_ERR("_tdm_client_poll timeout.");
+                       return TDM_ERROR_TIMEOUT;
+               } else {
+                       TDM_ERR("_tdm_client_poll failed. (ret:%d)", ret);
+                       return TDM_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       if (wl_display_read_events(display) == -1) {
+               TDM_ERR("wl_display_read_events failed");
+               return TDM_ERROR_OPERATION_FAILED;
+       }
+
+       ret = wl_display_dispatch_pending(display);
+
+       if (ret < 0) {
+               TDM_ERR("_tdm_client_dispatch_timeout failed");
+               return TDM_ERROR_OPERATION_FAILED;
+       }
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_handle_events_timeout(tdm_client *client, int ms_timeout)
+{
+       tdm_private_client *private_client;
+       tdm_error ret;
+       TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_client = (tdm_private_client*)client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       if (private_client->enable_ttrace)
+               TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
+
+       ret = _tdm_client_dispatch_timeout(private_client, ms_timeout);
+
+       if (private_client->enable_ttrace)
+               TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events_Timeout:%u", (unsigned int)private_client->stamp);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return ret;
+}
+
 typedef struct _tdm_client_vblank_temp {
        tdm_client_vblank_handler2 func;
        void *user_data;
@@ -749,7 +901,7 @@ tdm_client_output_add_change_handler(tdm_client_output *output,
 {
        tdm_private_client_output *private_output;
        tdm_private_client *private_client;
-       tdm_client_output_handler_info *h;
+       tdm_client_output_handler_info *h = NULL;
 
        TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
        TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
@@ -777,6 +929,7 @@ tdm_client_output_add_change_handler(tdm_client_output *output,
 
        if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
                wl_tdm_output_watch_output_changes(private_output->output, 1);
+               wl_display_roundtrip_queue(private_client->display, private_client->queue);
 
                /* TODO: this is very tricky.
                 * If a client adds the change_handler, we might be able to guess that
@@ -823,8 +976,11 @@ tdm_client_output_remove_change_handler(tdm_client_output *output,
                free(h);
 
                if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
-                       if (!CHECK_WL_PROTOCOL_ERROR(private_client))
+                       private_output->watch_output_changes = 0;
+                       if (!CHECK_WL_PROTOCOL_ERROR(private_client)) {
                                wl_tdm_output_watch_output_changes(private_output->output, 0);
+                               wl_display_roundtrip_queue(private_client->display, private_client->queue);
+                       }
                }
 
                pthread_mutex_unlock(&private_client->lock);
@@ -1464,3 +1620,690 @@ tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
 
        /* LCOV_EXCL_STOP */
 }
+
+unsigned int
+tdm_client_vblank_is_waiting(tdm_client_vblank *vblank)
+{
+       tdm_private_client_vblank *private_vblank;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, 0);
+
+       private_vblank = vblank;
+
+       return (LIST_LENGTH(&private_vblank->wait_list) > 0) ? 1 : 0;
+}
+
+static tbm_surface_h
+_tdm_client_voutput_create_surface_from_param(tbm_bufmgr bufmgr,
+                                                        int is_fd,
+                                                        int32_t width,
+                                                        int32_t height,
+                                                        uint32_t format,
+                                                        int32_t bpp,
+                                                        int32_t size,
+                                                        int32_t num_plane,
+                                                        struct wl_array *plane_buf_idx,
+                                                        struct wl_array *plane_offset,
+                                                        struct wl_array *plane_stride,
+                                                        struct wl_array *plane_size,
+                                                        uint32_t flags,
+                                                        int32_t num_buf,
+                                                        uint32_t buf0,
+                                                        uint32_t buf1,
+                                                        uint32_t buf2)
+{
+       int32_t names[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
+       tbm_surface_info_s info = { 0, };
+       tbm_bo bos[TBM_SURF_PLANE_MAX];
+       int i, numPlane, numName;
+       tbm_surface_h tbm_surface;
+
+       numPlane = tbm_surface_internal_get_num_planes(format);
+       TDM_RETURN_VAL_IF_FAIL(numPlane == num_plane, NULL);
+
+       info.width = width;
+       info.height = height;
+       info.format = format;
+       info.bpp = bpp;
+       info.size = size;
+       info.num_planes = numPlane;
+
+       /*Fill plane info*/
+       for (i = 0; i < numPlane; i++) {
+               info.planes[i].offset = *TDM_ARRAY_NTH_DATA(plane_offset, int32_t, i);
+               info.planes[i].stride = *TDM_ARRAY_NTH_DATA(plane_stride, int32_t, i);
+               info.planes[i].size = *TDM_ARRAY_NTH_DATA(plane_size, int32_t, i);
+       }
+
+       /*Fill buffer*/
+       numName = num_buf;
+       names[0] = buf0;
+       names[1] = buf1;
+       names[2] = buf2;
+
+       for (i = 0; i < numName; i++) {
+               if (is_fd)
+                       bos[i] = tbm_bo_import_fd(bufmgr, names[i]);
+               else
+                       bos[i] = tbm_bo_import(bufmgr, names[i]);
+       }
+
+       tbm_surface = tbm_surface_internal_create_with_bos(&info, bos, numName);
+       if (tbm_surface == NULL) {
+               if (is_fd) {
+                       close(buf0);
+                       close(buf1);
+                       close(buf2);
+               }
+               return NULL;
+       }
+
+       if (is_fd) {
+               close(buf0);
+               close(buf1);
+               close(buf2);
+       }
+
+       for (i = 0; i < numName; i++)
+               tbm_bo_unref(bos[i]);
+
+       return tbm_surface;
+}
+static void
+tdm_client_voutput_cb_buffer_import_with_id(void *data,
+               struct wl_tdm_voutput *wl_voutput,
+               struct wl_buffer *wl_buffer,
+               int32_t width,
+               int32_t height,
+               uint32_t format,
+               int32_t bpp,
+               int32_t size,
+               int32_t num_plane,
+               struct wl_array *plane_buf_idx,
+               struct wl_array *plane_offset,
+               struct wl_array *plane_stride,
+               struct wl_array *plane_size,
+               uint32_t flags,
+               int32_t num_buf,
+               uint32_t buf0,
+               uint32_t buf1,
+               uint32_t buf2)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
+       tdm_private_client_buffer *buffer = NULL;
+       tbm_surface_h tbm_surface;
+
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       buffer = calloc(1, sizeof *buffer);
+       TDM_RETURN_IF_FAIL(buffer != NULL);
+
+       tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 0,
+                             width, height, format, bpp, size,
+                             num_plane,
+                             plane_buf_idx, plane_offset, plane_stride, plane_size,
+                             0,
+                             num_buf,
+                             buf0, buf1, buf2);
+       TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
+
+       tbm_surface_internal_ref(tbm_surface);
+       wl_buffer_set_user_data(wl_buffer, tbm_surface);
+
+       buffer->wl_buffer = wl_buffer;
+
+       LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
+
+       return;
+
+fail:
+       if (buffer)
+               free(buffer);
+
+       if (wl_buffer)
+               wl_buffer_destroy(wl_buffer);
+}
+
+static void
+tdm_client_voutput_cb_buffer_import_with_fd(void *data,
+               struct wl_tdm_voutput *wl_voutput,
+               struct wl_buffer *wl_buffer,
+               int32_t width,
+               int32_t height,
+               uint32_t format,
+               int32_t bpp,
+               int32_t size,
+               int32_t num_plane,
+               struct wl_array *plane_buf_idx,
+               struct wl_array *plane_offset,
+               struct wl_array *plane_stride,
+               struct wl_array *plane_size,
+               uint32_t flags,
+               int32_t num_buf,
+               int32_t buf0,
+               int32_t buf1,
+               int32_t buf2)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
+       tdm_private_client_buffer *buffer = NULL;
+       tbm_surface_h tbm_surface;
+
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       buffer = calloc(1, sizeof *buffer);
+       TDM_RETURN_IF_FAIL(buffer != NULL);
+
+       tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 1,
+                             width, height, format, bpp, size,
+                             num_plane,
+                             plane_buf_idx, plane_offset, plane_stride, plane_size,
+                             0,
+                             num_buf,
+                             buf0, buf1, buf2);
+       TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
+
+       tbm_surface_internal_ref(tbm_surface);
+       wl_buffer_set_user_data(wl_buffer, tbm_surface);
+
+       buffer->wl_buffer = wl_buffer;
+
+       LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
+
+       return;
+
+fail:
+       if (buffer)
+               free(buffer);
+
+       if (wl_buffer)
+               wl_buffer_destroy(wl_buffer);
+}
+
+static void
+tdm_client_voutput_cb_buffer_destroy(void *data,
+               struct wl_tdm_voutput *wl_voutput,
+               struct wl_buffer *wl_buffer)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
+       tdm_private_client_buffer *cb = NULL, *cbb = NULL;
+       tbm_surface_h tbm_surface = NULL;
+
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
+               if (wl_buffer == cb->wl_buffer) {
+                       LIST_DEL(&cb->link);
+
+                       tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(wl_buffer);
+                       if (tbm_surface)
+                               tbm_surface_internal_unref(tbm_surface);
+
+                       wl_buffer_set_user_data(wl_buffer, NULL);
+                       wl_buffer_destroy(wl_buffer);
+
+                       free(cb);
+
+                       break;
+               }
+       }
+
+       return;
+}
+
+void
+tdm_client_voutput_cb_commit(void *data, struct wl_tdm_voutput *wl_voutput)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
+       struct list_head call_list;
+
+       private_voutput = (tdm_private_client_voutput *)data;
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       private_client = private_voutput->base.private_client;
+
+       LIST_INITHEAD(&call_list);
+
+       LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
+               LIST_ADDTAIL(&h->call_link, &call_list);
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
+               if (h->func)
+                       h->func(private_voutput, h->user_data);
+       }
+       pthread_mutex_lock(&private_client->lock);
+}
+
+void
+tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput, uint32_t msg)
+{
+       tdm_private_client_voutput *private_voutput = data;
+
+       private_voutput->msg = msg;
+}
+
+static const struct wl_tdm_voutput_listener tdm_client_voutput_lisntener = {
+       tdm_client_voutput_cb_buffer_import_with_id,
+       tdm_client_voutput_cb_buffer_import_with_fd,
+       tdm_client_voutput_cb_buffer_destroy,
+       tdm_client_voutput_cb_commit,
+       tdm_client_voutput_cb_ack_message
+};
+
+tdm_client_voutput *
+tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error)
+{
+       tdm_private_client *private_client;
+       tdm_private_client_output *private_output;
+       tdm_private_client_voutput *private_voutput;
+       struct wl_proxy *wrapper;
+
+       if (error)
+               *error = TDM_ERROR_NONE;
+
+       if (!client) {
+               TDM_ERR("'!client' failed");
+               if (error)
+                       *error = TDM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       if (!name) {
+               TDM_ERR("'!name' failed");
+               if (error)
+                       *error = TDM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       private_client = (tdm_private_client *)client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               if (error)
+                       *error = TDM_ERROR_PROTOCOL_ERROR;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
+
+       LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
+               if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
+                       if (error)
+                               *error = TDM_ERROR_INVALID_PARAMETER; // FIXME define new error type.
+                       pthread_mutex_unlock(&private_client->lock);
+                       return NULL;
+               }
+       }
+
+       wrapper = wl_proxy_create_wrapper(private_client->tdm);
+       if (!wrapper) {
+               TDM_ERR("create virtual output wrapper failed");
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
+
+       wl_proxy_set_queue(wrapper, private_client->queue);
+
+       private_voutput = calloc(1, sizeof *private_voutput);
+       if (!private_voutput) {
+               /* LOCV_EXCL_START */
+               wl_proxy_wrapper_destroy(wrapper);
+               TDM_ERR("alloc failed");
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+               /* LOCV_EXCL_STOP */
+       }
+
+       private_voutput->bufmgr = tbm_bufmgr_init(-1);
+       if (private_voutput->bufmgr == NULL) {
+               /* LCOV_EXCL_START */
+               TDM_ERR("fail tbm_bufmgr_init");
+               free(private_voutput);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
+
+       LIST_INITHEAD(&private_voutput->commit_handler_list);
+       LIST_INITHEAD(&private_voutput->buffer_list);
+
+       private_voutput->base.private_client = private_client;
+
+       private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, private_voutput->base.name);
+       wl_proxy_wrapper_destroy(wrapper);
+       if (!private_voutput->wl_voutput) {
+               /* LCOV_EXCL_START */
+               TDM_ERR("couldn't create voutput resource");
+               free(private_voutput);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
+
+       wl_tdm_voutput_add_listener(private_voutput->wl_voutput,
+                                                               &tdm_client_voutput_lisntener, private_voutput);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+
+       wl_proxy_set_queue((struct wl_proxy *)private_voutput->wl_voutput, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               wl_tdm_voutput_destroy(private_voutput->wl_voutput);
+               free(private_voutput);
+               if (error)
+                       *error = TDM_ERROR_PROTOCOL_ERROR;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
+
+       if (private_voutput->msg != WL_TDM_VOUTPUT_MESSAGE_ADDED)
+       {
+               wl_tdm_voutput_destroy(private_voutput->wl_voutput);
+               free(private_voutput);
+               if (error)
+                       *error = TDM_ERROR_PROTOCOL_ERROR;      // FIXME add new error type.
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return (tdm_client_voutput *)private_voutput;
+}
+
+void
+tdm_client_voutput_destroy(tdm_client_voutput *voutput)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)voutput;
+       tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
+
+       if (!private_voutput)
+               return;
+
+       if (!(LIST_IS_EMPTY(&private_voutput->buffer_list))) {
+               tdm_private_client_buffer *cb = NULL, *cbb = NULL;
+
+               LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
+                       tbm_surface_h tbm_surface = NULL;
+
+                       if (!cb) continue;
+
+                       LIST_DEL(&cb->link);
+
+                       tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(cb->wl_buffer);
+                       if (tbm_surface)
+                               tbm_surface_internal_unref(tbm_surface);
+
+                       wl_buffer_set_user_data(cb->wl_buffer, NULL);
+                       wl_buffer_destroy(cb->wl_buffer);
+
+                       free(cb);
+               }
+       }
+
+       if (private_voutput->bufmgr)
+               tbm_bufmgr_deinit(private_voutput->bufmgr);
+
+       if (private_voutput->available_modes.modes)
+               free(private_voutput->available_modes.modes);
+
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_voutput->commit_handler_list, link) {
+               LIST_DEL(&h->link);
+               free(h);
+       }
+
+       wl_tdm_voutput_destroy(private_voutput->wl_voutput);
+
+       free(private_voutput);
+}
+
+tdm_error
+tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_client_output_mode *modes, int count)
+{
+       tdm_private_client_voutput *private_voutput;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       if ((count > 0) && (modes == NULL))
+               return TDM_ERROR_INVALID_PARAMETER;
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+
+       if (private_voutput->base.connection == TDM_OUTPUT_CONN_STATUS_CONNECTED)
+               return TDM_ERROR_BAD_REQUEST;
+
+       if (private_voutput->available_modes.modes)
+               free(private_voutput->available_modes.modes);
+
+       private_voutput->available_modes.count = count;
+
+       if (count != 0)
+       {
+               private_voutput->available_modes.modes = calloc(count, sizeof(tdm_client_output_mode));
+               memcpy(private_voutput->available_modes.modes, modes, sizeof(tdm_client_output_mode) * count);
+       }
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, unsigned int mmWidth, unsigned int mmHeight)
+{
+       tdm_private_client_voutput *private_voutput;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(mmWidth != 0, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(mmHeight != 0, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+
+       if (private_voutput->base.connection == TDM_OUTPUT_CONN_STATUS_CONNECTED)
+               return TDM_ERROR_BAD_REQUEST;
+
+       private_voutput->mmwidth = mmWidth;
+       private_voutput->mmheight = mmHeight;
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_voutput_add_commit_handler(tdm_client_voutput *voutput,
+                                                                         tdm_client_voutput_commit_handler func,
+                                                                         void *user_data)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->base.private_client;
+
+       LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
+               if (h->func == func && h->user_data == user_data) {
+                       TDM_ERR("can't add twice");
+                       return TDM_ERROR_BAD_REQUEST;
+               }
+       }
+
+       h = calloc(1, sizeof *h);
+       TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               free(h);
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       h->private_voutput = private_voutput;
+       h->func = func;
+       h->user_data = user_data;
+       LIST_ADDTAIL(&h->link, &private_voutput->commit_handler_list);
+       LIST_INITHEAD(&h->call_link);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+void
+tdm_client_voutput_remove_commit_handler(tdm_client_voutput *voutput,
+                                                                                tdm_client_voutput_commit_handler func,
+                                                                                void *user_data)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL;
+
+       TDM_RETURN_IF_FAIL(voutput != NULL);
+       TDM_RETURN_IF_FAIL(func != NULL);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->base.private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
+               if (h->func != func || h->user_data != user_data)
+                       continue;
+
+               LIST_DEL(&h->link);
+               free(h);
+
+               pthread_mutex_unlock(&private_client->lock);
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+}
+
+tdm_error
+tdm_client_voutput_commit_done(tdm_client_voutput *voutput)
+{
+       tdm_private_client_voutput *private_voutput;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       wl_tdm_voutput_commit_done(private_voutput->wl_voutput);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_client_output *
+tdm_client_voutput_get_client_output(tdm_client_voutput *voutput, tdm_error *error)
+{
+       tdm_private_client_voutput *private_voutput;
+
+       if (error)
+               *error = TDM_ERROR_NONE;
+
+       if (!voutput)
+       {
+               TDM_ERR("'!voutput' failed");
+               if (error)
+                       *error = TDM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+
+       return &private_voutput->base;
+}
+
+tdm_error
+tdm_client_output_get_available_modes(tdm_client_output *output, tdm_client_output_mode **modes, int *count)
+{
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_output_set_mode(tdm_client_output *output, const tdm_client_output_mode *mode)
+{
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       return TDM_ERROR_NONE;
+}
+
+void
+_tdm_client_voutput_send_available_modes(tdm_private_client_voutput *private_voutput)
+{
+       tdm_client_output_mode *modes, *mode;
+       struct wl_array array;
+       int i, size;
+
+       modes = private_voutput->available_modes.modes;
+       size = sizeof(tdm_client_output_mode);
+
+       wl_array_init(&array);
+       for (i = 0; i < private_voutput->available_modes.count; i++) {
+               mode = wl_array_add(&array, size);
+               memcpy(mode, &modes[i], size);
+       }
+       wl_tdm_voutput_set_available_modes(private_voutput->wl_voutput, &array);
+       wl_array_release(&array);
+}
+
+tdm_error
+tdm_client_output_connect(tdm_client_output *output)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client_voutput *private_voutput;
+
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_output = (tdm_private_client_output *)output;
+       private_voutput = (tdm_private_client_voutput *)output;
+
+       TDM_RETURN_VAL_IF_FAIL(private_output->connection != TDM_OUTPUT_CONN_STATUS_CONNECTED,
+                                                  TDM_ERROR_BAD_REQUEST);
+
+       private_output->connection = TDM_OUTPUT_CONN_STATUS_CONNECTED;
+
+       _tdm_client_voutput_send_available_modes(private_voutput);
+
+       wl_tdm_voutput_set_physical_size(private_voutput->wl_voutput, private_voutput->mmwidth, private_voutput->mmheight);
+
+       wl_tdm_voutput_connect(private_voutput->wl_voutput);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_output_disconnect(tdm_client_output *output)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client_output *private_output;
+
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_output = (tdm_private_client_output *)output;
+       private_voutput = (tdm_private_client_voutput *)output;
+
+       TDM_RETURN_VAL_IF_FAIL(private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED,
+                                                  TDM_ERROR_BAD_REQUEST);
+
+       private_output->connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
+
+       wl_tdm_voutput_disconnect(private_voutput->wl_voutput);
+
+       return TDM_ERROR_NONE;
+}