client: fix dealloacated error
[platform/core/uifw/libtdm.git] / client / tdm_client.c
index 4a39714..3a2ab48 100644 (file)
@@ -1,38 +1,40 @@
 /**************************************************************************
-
-libtdm
-
-Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
-
-Contact: Eunchul Kim <chulspro.kim@samsung.com>,
-         JinYoung Jeon <jy0.jeon@samsung.com>,
-         Taeheon Kim <th908.kim@samsung.com>,
-         YoungJun Cho <yj44.cho@samsung.com>,
-         SooChan Lim <sc1.lim@samsung.com>,
        Boram Park <sc1.lim@samsung.com>
-
-Permission is hereby granted, free of charge, to any person obtaining a
-copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sub license, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice (including the
-next paragraph) shall be included in all copies or substantial portions
-of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
-IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
-ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
+ *
+ * libtdm
+ *
+ * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
*          JinYoung Jeon <jy0.jeon@samsung.com>,
*          Taeheon Kim <th908.kim@samsung.com>,
*          YoungJun Cho <yj44.cho@samsung.com>,
*          SooChan Lim <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
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
 **************************************************************************/
 
+#define WL_HIDE_DEPRECATED
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -41,41 +43,412 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <string.h>
 #include <stdlib.h>
 #include <time.h>
+#include <strings.h>
+#include <poll.h>
+
+#include <tdm-client-protocol.h>
 
 #include "tdm_client.h"
 #include "tdm_log.h"
 #include "tdm_macro.h"
 #include "tdm_list.h"
-#include "tdm-client-protocol.h"
+#include "tdm.h"
+#include "tdm_private.h"
+#include <tbm_surface_internal.h>
 
-int tdm_debug;
+#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_voutput tdm_private_client_voutput;
 
 typedef struct _tdm_private_client {
+       pthread_mutex_t lock;
+
        struct wl_display *display;
+       struct wl_event_queue *queue;
        struct wl_registry *registry;
        struct wl_tdm *tdm;
+       struct list_head output_list;
+       struct list_head voutput_list;
 
-       struct list_head vblank_list;
+       unsigned int enable_ttrace;
+       unsigned int stamp;
+
+       tdm_private_client_vblank *temp_vblank;
 } tdm_private_client;
 
-typedef struct _tdm_client_vblank_info {
+typedef struct _tdm_private_client_output {
+       struct list_head link;
+
+       tdm_private_client *private_client;
+
+       char name[TDM_NAME_LEN];
+       struct wl_tdm_output *output;
+       int width;
+       int height;
+       int refresh;
+       tdm_output_conn_status connection;
+       tdm_output_dpms dpms;
+       struct list_head vblank_list;
+       struct list_head change_handler_list;
+
+       unsigned int req_id;
+       unsigned int watch_output_changes;
+
+       tdm_private_client_voutput *voutput;
+} tdm_private_client_output;
+
+typedef struct _tdm_private_client_buffer {
        struct list_head link;
+       struct wl_buffer *wl_buffer;
+} tdm_private_client_buffer;
+
+struct _tdm_private_client_voutput {
+       struct list_head link;
+       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 *attach_buffer;
+
+       tdm_private_client *private_client;
+       tdm_private_client_output *private_output;
+       char name[TDM_NAME_LEN];
+       int get_output;
+};
+
+struct _tdm_private_client_vblank {
+       tdm_private_client_output *private_output;
+
        struct wl_tdm_vblank *vblank;
+       struct list_head wait_list;
+
+       char name[TDM_NAME_LEN];
+       unsigned int sync;
+       unsigned int fps;
+       int offset;
+       unsigned int enable_fake;
+       unsigned int enable_ttrace;
+
+       unsigned int started;
+       unsigned int stamp;
+
+       double req_time;
+       double last_time;
+
+       struct list_head link;
+};
+
+typedef struct _tdm_client_output_handler_info {
+       tdm_private_client_output *private_output;
+
+       tdm_client_output_change_handler func;
+       void *user_data;
+
+       struct list_head link;
+       struct list_head call_link;
+} tdm_client_output_handler_info;
+
+typedef struct _tdm_client_wait_info {
+       tdm_private_client_vblank *private_vblank;
+
        tdm_client_vblank_handler func;
-       unsigned int req_sec;
-       unsigned int req_usec;
        void *user_data;
+
+       unsigned int req_id;
+       double req_time;
        int need_free;
-} tdm_client_vblank_info;
+
+       struct list_head link;
+       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)
+{
+       uint32_t ec, id;
+       const struct wl_interface *intf;
+       int err;
+
+       err = wl_display_get_error(private_client->display);
+       if (!err)
+               return false;
+
+       if (err == EINVAL || err == ENOMEM || err == EFAULT || err == EPROTO) {
+               ec = wl_display_get_protocol_error(private_client->display, &intf, &id);
+               TDM_ERR("[%s,%d] errno(%d) Got protocol error '%u' on interface '%s' (object '%u')",
+                               func, line, err, ec, (intf) ? intf->name : "destroyed", id);
+       } else {
+               TDM_ERR("[%s,%d] errno(%d)", func, line, err);
+       }
+
+       return true;
+}
+
+#define CHECK_WL_PROTOCOL_ERROR(pc)  _tdm_client_check_wl_error(pc, __FUNCTION__, __LINE__)
+
+static void
+_tdm_client_vblank_cb_stamp(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t stamp)
+{
+       tdm_private_client_vblank *private_vblank = data;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_IF_FAIL(private_vblank != NULL);
+
+       private_vblank->stamp = stamp;
+
+       TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
+       private_client = private_vblank->private_output->private_client;
+
+       private_client->stamp = stamp;
+}
+
+/* LCOV_EXCL_START */
+static void
+_tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
+                                                  uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
+                                                  uint32_t tv_usec, uint32_t error)
+{
+       tdm_private_client_vblank *private_vblank = data;
+       tdm_private_client *private_client;
+       tdm_client_wait_info *w = NULL, *wait_info = NULL;
+
+       TDM_RETURN_IF_FAIL(private_vblank != NULL);
+
+       private_client = private_vblank->private_output->private_client;
+
+       private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
+
+       TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
+                       private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
+
+       LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
+               if (w->req_id != req_id)
+                       continue;
+
+               wait_info = w;
+               break;
+       }
+
+       if (!wait_info) {
+               TDM_ERR("no wait infomation for req_id(%d)", req_id);
+               return;
+       }
+
+       if (private_vblank->enable_ttrace)
+               TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
+
+       if (wait_info->req_time >= private_vblank->last_time)
+               TDM_WRN("'req(%.6f) < last(%.6f)' failed. error(%d)", wait_info->req_time, private_vblank->last_time, error);
+
+       if (wait_info->need_free)
+               LIST_DEL(&wait_info->link);
+
+       if (wait_info->func) {
+               pthread_mutex_unlock(&private_client->lock);
+               wait_info->func(private_vblank, error, sequence, tv_sec, tv_usec, wait_info->user_data);
+               pthread_mutex_lock(&private_client->lock);
+       }
+
+       if (wait_info->need_free)
+               free(w);
+       else
+               wait_info->need_free = 1;
+}
+/* LCOV_EXCL_STOP */
+
+/* LCOV_EXCL_START */
+static void
+_tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
+{
+       tdm_private_client_vblank *private_vblank = data;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_IF_FAIL(private_vblank != NULL);
+
+       private_vblank->enable_ttrace = enable;
+
+       TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
+       private_client = private_vblank->private_output->private_client;
+
+       private_client->enable_ttrace = enable;
+}
+/* LCOV_EXCL_STOP */
+
+static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
+       _tdm_client_vblank_cb_stamp,
+       _tdm_client_vblank_cb_done,
+       _tdm_client_vblank_cb_ttrace,
+};
+
+static void
+_tdm_client_output_destroy(tdm_private_client_output *private_output)
+{
+       tdm_private_client_vblank *v = NULL, *vv = NULL;
+       tdm_client_output_handler_info *h = NULL, *hh = NULL;
+
+       LIST_DEL(&private_output->link);
+
+       LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
+               TDM_ERR("vblanks SHOULD be destroyed first!");
+               LIST_DEL(&v->link);
+               v->private_output = NULL;
+       }
+
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
+               LIST_DEL(&h->link);
+               free(h);
+       }
+
+       wl_tdm_output_destroy(private_output->output);
+
+       free(private_output);
+}
+
+static void
+_tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
+                                                  uint32_t width, uint32_t height, uint32_t refresh, uint32_t error)
+{
+       tdm_private_client_output *private_output = (tdm_private_client_output*)data;
+
+       TDM_RETURN_IF_FAIL(private_output != NULL);
+
+       private_output->width = width;
+       private_output->height = height;
+       private_output->refresh = refresh;
+
+       if (error != TDM_ERROR_NONE)
+               TDM_INFO("mode event error: %d", error);
+
+       TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
+                       private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
+                       width, height, refresh);
+}
+
+static void
+_tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
+{
+       tdm_private_client_output *private_output = (tdm_private_client_output*)data;
+       tdm_private_client *private_client;
+       tdm_client_output_handler_info *h = NULL, *hh = NULL;
+       tdm_value v;
+       struct list_head call_list;
+
+       TDM_RETURN_IF_FAIL(private_output != NULL);
+
+       private_client = private_output->private_client;
+
+       if (private_output->connection == value && value != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
+               return;
+
+       private_output->connection = value;
+
+       if (error != TDM_ERROR_NONE)
+               TDM_INFO("connection event error: %d", error);
+
+       TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
+                       private_output,
+                       wl_proxy_get_id((struct wl_proxy*)private_output->output),
+                       value);
+
+       LIST_INITHEAD(&call_list);
+
+       LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
+               LIST_ADDTAIL(&h->call_link, &call_list);
+       }
+
+       v.u32 = value;
+       pthread_mutex_unlock(&private_client->lock);
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
+               if (h->func)
+                       h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
+       }
+       pthread_mutex_lock(&private_client->lock);
+}
+
+static void
+_tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
+{
+       tdm_private_client_output *private_output = (tdm_private_client_output*)data;
+       tdm_private_client *private_client;
+       tdm_client_output_handler_info *h = NULL, *hh = NULL;
+       tdm_value v;
+       struct list_head call_list;
+
+       TDM_RETURN_IF_FAIL(private_output != NULL);
+
+       private_client = private_output->private_client;
+
+       /* If value is extended value, we handle it as DPMS on in client side
+        * The extended DPMS value is valid only in server side.
+        * Or, need to export to client side also?
+        */
+       if (value > TDM_OUTPUT_DPMS_OFF)
+               value = TDM_OUTPUT_DPMS_ON;
+
+       if (private_output->dpms == value)
+               return;
+
+       private_output->dpms = value;
+
+       if (error != TDM_ERROR_NONE)
+               TDM_INFO("dpms event error: %d", error);
+
+       TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
+                       private_output,
+                       wl_proxy_get_id((struct wl_proxy*)private_output->output),
+                       value);
+
+       LIST_INITHEAD(&call_list);
+
+       LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
+               LIST_ADDTAIL(&h->call_link, &call_list);
+       }
+
+       v.u32 = value;
+       pthread_mutex_unlock(&private_client->lock);
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
+               if (h->func)
+                       h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
+       }
+       pthread_mutex_lock(&private_client->lock);
+}
+
+static const struct wl_tdm_output_listener tdm_client_output_listener = {
+       _tdm_client_output_cb_mode,
+       _tdm_client_output_cb_connection,
+       _tdm_client_output_cb_dpms,
+};
 
 static void
 _tdm_client_cb_global(void *data, struct wl_registry *registry,
-                      uint32_t name, const char *interface,
-                      uint32_t version)
+                                         uint32_t name, const char *interface,
+                                         uint32_t version)
 {
        tdm_private_client *private_client = data;
 
-       if (strcmp(interface, "wl_tdm") == 0) {
+       if (strncmp(interface, "wl_tdm", 6) == 0) {
                private_client->tdm =
                        wl_registry_bind(registry, name, &wl_tdm_interface, version);
                TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
@@ -84,58 +457,73 @@ _tdm_client_cb_global(void *data, struct wl_registry *registry,
        }
 }
 
+/* LCOV_EXCL_START */
 static void
 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
 {
 }
+/* LCOV_EXCL_STOP */
 
-static const struct wl_registry_listener tdm_client_registry_listener =
-{
-    _tdm_client_cb_global,
-    _tdm_client_cb_global_remove
+static const struct wl_registry_listener tdm_client_registry_listener = {
+       _tdm_client_cb_global,
+       _tdm_client_cb_global_remove
 };
 
 tdm_client*
-tdm_client_create(tdm_client_error *error)
+tdm_client_create(tdm_error *error)
 {
        tdm_private_client *private_client;
-       const char *debug;
-
-       debug = getenv("TDM_DEBUG");
-       if (debug && (strstr(debug, "1")))
-               tdm_debug = 1;
 
        private_client = calloc(1, sizeof *private_client);
        if (!private_client) {
+               /* LCOV_EXCL_START */
+
                TDM_ERR("alloc failed");
                if (error)
-                       *error = TDM_CLIENT_ERROR_OUT_OF_MEMORY;
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       if (pthread_mutex_init(&private_client->lock, NULL)) {
+               TDM_ERR("mutex init failed: %m");
+               free(private_client);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
                return NULL;
        }
 
+       LIST_INITHEAD(&private_client->output_list);
+       LIST_INITHEAD(&private_client->voutput_list);
+
        private_client->display = wl_display_connect("tdm-socket");
        TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
 
+       private_client->queue = wl_display_create_queue(private_client->display);
+       TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
+
        private_client->registry = wl_display_get_registry(private_client->display);
        TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
 
        wl_registry_add_listener(private_client->registry,
-                                &tdm_client_registry_listener, private_client);
+                                                        &tdm_client_registry_listener, private_client);
        wl_display_roundtrip(private_client->display);
 
+       if (CHECK_WL_PROTOCOL_ERROR(private_client))
+               goto create_failed;
+
        /* check global objects */
        TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
 
-       LIST_INITHEAD(&private_client->vblank_list);
-
        if (error)
-               *error = TDM_CLIENT_ERROR_NONE;
+               *error = TDM_ERROR_NONE;
 
        return (tdm_client*)private_client;
 create_failed:
        tdm_client_destroy((tdm_client*)private_client);
        if (error)
-               *error = TDM_CLIENT_ERROR_OPERATION_FAILED;
+               *error = TDM_ERROR_OPERATION_FAILED;
        return NULL;
 }
 
@@ -143,150 +531,1999 @@ void
 tdm_client_destroy(tdm_client *client)
 {
        tdm_private_client *private_client = (tdm_private_client*)client;
-       tdm_client_vblank_info *v = NULL, *vv = NULL;
+       tdm_private_client_output *o = NULL, *oo = NULL;
+       tdm_private_client_voutput *vo = NULL, *voo = NULL;
 
        if (!private_client)
                return;
 
-       LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_client->vblank_list, link) {
-               LIST_DEL(&v->link);
-               wl_tdm_vblank_destroy(v->vblank);
-               free(v);
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_client->temp_vblank) {
+               pthread_mutex_unlock(&private_client->lock);
+               tdm_client_vblank_destroy(private_client->temp_vblank);
+               pthread_mutex_lock(&private_client->lock);
+       }
+
+       LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
+               _tdm_client_output_destroy(o);
+       }
+
+       LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_client->voutput_list, link) {
+               tdm_client_voutput_destroy(vo);
        }
 
        if (private_client->tdm)
                wl_tdm_destroy(private_client->tdm);
        if (private_client->registry)
                wl_registry_destroy(private_client->registry);
+       if (private_client->queue)
+               wl_event_queue_destroy(private_client->queue);
        if (private_client->display)
                wl_display_disconnect(private_client->display);
 
+       pthread_mutex_unlock(&private_client->lock);
+       pthread_mutex_destroy(&private_client->lock);
+
        free(private_client);
 }
 
-tdm_client_error
+tdm_error
 tdm_client_get_fd(tdm_client *client, int *fd)
 {
        tdm_private_client *private_client;
 
-       TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
 
        private_client = (tdm_private_client*)client;
 
+       pthread_mutex_lock(&private_client->lock);
+
        *fd = wl_display_get_fd(private_client->display);
+
+       pthread_mutex_unlock(&private_client->lock);
+
        if (*fd < 0)
-               return TDM_CLIENT_ERROR_OPERATION_FAILED;
+               return TDM_ERROR_OPERATION_FAILED;
 
-       return TDM_CLIENT_ERROR_NONE;
+       return TDM_ERROR_NONE;
 }
 
-tdm_client_error
+tdm_error
 tdm_client_handle_events(tdm_client *client)
 {
        tdm_private_client *private_client;
 
-       TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
 
+       /* LCOV_EXCL_START */
        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:%u", (unsigned int)private_client->stamp);
+
        wl_display_dispatch(private_client->display);
 
-       return TDM_CLIENT_ERROR_NONE;
+       if (private_client->enable_ttrace)
+               TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%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 TDM_ERROR_NONE;
+       /* LCOV_EXCL_STOP */
 }
 
-static void
-_tdm_client_cb_vblank_done(void *data, struct wl_tdm_vblank *vblank,
-                           uint32_t sequence, uint32_t tv_sec, uint32_t tv_usec)
+static int
+_tdm_client_poll(struct wl_display *display, short int events, int timeout)
 {
-       tdm_client_vblank_info *vblank_info = (tdm_client_vblank_info*)data;
+       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;
+}
 
-       TDM_RETURN_IF_FAIL(vblank_info != NULL);
+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;
+       }
 
-       if (vblank_info->vblank != vblank)
-               TDM_NEVER_GET_HERE();
+       while (true) {
+               ret = wl_display_flush(display);
 
-       TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info,
-               wl_proxy_get_id((struct wl_proxy *)vblank));
+               if (ret != -1 || errno != EAGAIN)
+                       break;
 
-       if (vblank_info->func) {
-               vblank_info->func(sequence, tv_sec, tv_usec, vblank_info->user_data);
+               if (_tdm_client_poll(display, POLLOUT, -1) == -1) {
+                       wl_display_cancel_read(display);
+                       TDM_ERR("_tdm_client_poll failed");
+                       return TDM_ERROR_OPERATION_FAILED;
+               }
        }
 
-       if (vblank_info->need_free) {
-               LIST_DEL(&vblank_info->link);
-               free(vblank_info);
-       } else {
-               vblank_info->need_free = 1;
+       /* 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;
 }
 
-static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
-       _tdm_client_cb_vblank_done,
-};
+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;
+} tdm_client_vblank_temp;
+
+/* LCOV_EXCL_START */
+static void
+_tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
+                                                               unsigned int tv_sec, unsigned int tv_usec, void *user_data)
+{
+       tdm_client_vblank_temp *vblank_temp = user_data;
+
+       TDM_RETURN_IF_FAIL(vblank_temp != NULL);
+       TDM_RETURN_IF_FAIL(vblank != NULL);
+
+       if (vblank_temp->func)
+               vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
+
+       free(vblank_temp);
+}
+/* LCOV_EXCL_STOP */
 
-tdm_client_error
+/* LCOV_EXCL_START */ /* deprecated */
+tdm_error
 tdm_client_wait_vblank(tdm_client *client, char *name,
-                       int sw_timer, int interval, int sync,
-                       tdm_client_vblank_handler func, void *user_data)
+                                          int sw_timer, int interval, int sync,
+                                          tdm_client_vblank_handler2 func, void *user_data)
 {
        tdm_private_client *private_client = (tdm_private_client*)client;
-       tdm_client_vblank_info *vblank_info;
-       struct timespec tp;
-       int ret = 0;
+       tdm_client_output *output;
+       tdm_client_vblank_temp *vblank_temp;
+       tdm_error ret;
 
-       TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_CLIENT_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(private_client->tdm != NULL, TDM_CLIENT_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
 
-       vblank_info = calloc(1, sizeof *vblank_info);
-       if (!vblank_info) {
-               TDM_ERR("alloc failed");
-               return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
+       if (CHECK_WL_PROTOCOL_ERROR(private_client))
+               return TDM_ERROR_PROTOCOL_ERROR;
+
+       if (!private_client->temp_vblank) {
+               output = tdm_client_get_output(client, name, &ret);
+               TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
+
+               private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
+               TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
        }
 
-       clock_gettime(CLOCK_MONOTONIC, &tp);
+       tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
+       tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
 
-       vblank_info->req_sec = (unsigned int)tp.tv_sec;
-       vblank_info->req_usec = (unsigned int)(tp.tv_nsec/1000L);
-       vblank_info->need_free = (sync) ? 0 : 1;
+       vblank_temp = calloc(1, sizeof *vblank_temp);
+       TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
 
-       vblank_info->vblank =
-               wl_tdm_wait_vblank(private_client->tdm, name, sw_timer, interval,
-                                  vblank_info->req_sec, vblank_info->req_usec);
-       if (!vblank_info->vblank) {
-               TDM_ERR("couldn't create vblank resource");
-               free(vblank_info);
-               return TDM_CLIENT_ERROR_OUT_OF_MEMORY;
+       vblank_temp->func = func;
+       vblank_temp->user_data = user_data;
+
+       return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
+}
+/* LCOV_EXCL_STOP */
+
+tdm_client_output*
+tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
+{
+       tdm_private_client *private_client;
+       tdm_private_client_output *private_output = NULL;
+       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;
        }
 
-       TDM_DBG("vblank_info(%p) wl_tbm_vblank@%d", vblank_info,
-               wl_proxy_get_id((struct wl_proxy *)vblank_info->vblank));
+       private_client = (tdm_private_client*)client;
 
-       wl_tdm_vblank_add_listener(vblank_info->vblank,
-                                  &tdm_client_vblank_listener, vblank_info);
+       pthread_mutex_lock(&private_client->lock);
 
-       vblank_info->func = func;
-       vblank_info->user_data = user_data;
-       LIST_ADDTAIL(&vblank_info->link, &private_client->vblank_list);
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               if (error)
+                       *error = TDM_ERROR_PROTOCOL_ERROR;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
 
-       if (!sync) {
-               wl_display_flush(private_client->display);
-               return TDM_CLIENT_ERROR_NONE;
+       if (!name) {
+               name = "primary";
+       } else if (!strncmp(name, "primary", 7) || !strncmp(name, "default", 7)) {
+               TDM_DBG("get primary or default output");
+       } else {
+               tdm_private_client_voutput *private_voutput = NULL;
+               int find = 0;
+
+               LIST_FOR_EACH_ENTRY(private_voutput, &private_client->voutput_list, link) {
+                       if (!strncmp(private_voutput->name, name, TDM_NAME_LEN)) {
+                               find = 1;
+                               break;
+                       }
+               }
+               if (!find) {
+                       if (error)
+                               *error = TDM_ERROR_INVALID_PARAMETER;
+                       pthread_mutex_unlock(&private_client->lock);
+                       return NULL;
+               }
        }
 
-       while (ret != -1 && !vblank_info->need_free)
-               ret = wl_display_dispatch(private_client->display);
+       LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
+               if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
+                       pthread_mutex_unlock(&private_client->lock);
+                       return (tdm_client_output*)private_output;
+               }
+       }
 
-       clock_gettime(CLOCK_MONOTONIC, &tp);
-       TDM_DBG("block during %d us",
-               ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec/1000L))
-               - (vblank_info->req_sec * 1000000 + vblank_info->req_usec));
+       wrapper = wl_proxy_create_wrapper(private_client->tdm);
+       if (!wrapper) {
+               TDM_ERR("create 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_output = calloc(1, sizeof *private_output);
+       if (!private_output) {
+               /* LCOV_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;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       private_output->private_client = private_client;
+
+       snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
+       private_output->output = wl_tdm_create_output((struct wl_tdm *)wrapper, private_output->name);
+       wl_proxy_wrapper_destroy(wrapper);
+       if (!private_output->output) {
+               /* LCOV_EXCL_START */
+
+               TDM_ERR("couldn't create output resource");
+               free(private_output);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       LIST_INITHEAD(&private_output->vblank_list);
+       LIST_INITHEAD(&private_output->change_handler_list);
+
+       wl_tdm_output_add_listener(private_output->output,
+                                                          &tdm_client_output_listener, private_output);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               wl_tdm_output_destroy(private_output->output);
+               free(private_output);
+               if (error)
+                       *error = TDM_ERROR_PROTOCOL_ERROR;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
+
+       LIST_ADDTAIL(&private_output->link, &private_client->output_list);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return (tdm_client_output*)private_output;
+}
+
+tdm_error
+tdm_client_output_add_change_handler(tdm_client_output *output,
+                                                                        tdm_client_output_change_handler func,
+                                                                        void *user_data)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client *private_client;
+       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);
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_client;
+
+       LIST_FOR_EACH_ENTRY(h, &private_output->change_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;
+       }
+
+       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
+                * the client will watch the tdm client's fd and handle tdm events in
+                * event loop. Otherwise, we CAN'T make sure if a client has event loop
+                * which handles tdm events.
+                */
+               private_output->watch_output_changes = 1;
+       }
+
+       h->private_output = private_output;
+       h->func = func;
+       h->user_data = user_data;
+       LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
+       LIST_INITHEAD(&h->call_link);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+void
+tdm_client_output_remove_change_handler(tdm_client_output *output,
+                                                                               tdm_client_output_change_handler func,
+                                                                               void *user_data)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client *private_client;
+       tdm_client_output_handler_info *h = NULL;
+
+       TDM_RETURN_IF_FAIL(output != NULL);
+       TDM_RETURN_IF_FAIL(func != NULL);
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
+               if (h->func != func || h->user_data != user_data)
+                       continue;
+
+               LIST_DEL(&h->link);
+               free(h);
+
+               if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
+                       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);
+
+               return;
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+}
+
+tdm_error
+tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_output->watch_output_changes) {
+               *refresh = private_output->refresh;
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
+       wl_tdm_output_get_mode(private_output->output);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       *refresh = private_output->refresh;
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_output_get_mode(tdm_client_output *output, unsigned int *width, unsigned int *height)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(width != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(height != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_output->watch_output_changes) {
+               *width = private_output->width;
+               *height = private_output->height;
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
+       wl_tdm_output_get_mode(private_output->output);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       *width = private_output->width;
+       *height = private_output->height;
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_output->watch_output_changes) {
+               *status = private_output->connection;
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
+       wl_tdm_output_get_connection(private_output->output);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       *status = private_output->connection;
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_client;
+
+       if (private_output->watch_output_changes) {
+               *dpms = private_output->dpms;
+               return TDM_ERROR_NONE;
+       }
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
+       wl_tdm_output_get_dpms(private_output->output);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+       wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       *dpms = private_output->dpms;
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_client_vblank*
+tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
+{
+       tdm_private_client *private_client;
+       tdm_private_client_output *private_output;
+       tdm_private_client_vblank *private_vblank;
+       struct wl_proxy *wrapper;
+
+       if (error)
+               *error = TDM_ERROR_NONE;
+
+       if (!output) {
+               TDM_ERR("'!output' failed");
+               if (error)
+                       *error = TDM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       private_output = (tdm_private_client_output*)output;
+       private_client = private_output->private_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;
+       }
+
+       wrapper = wl_proxy_create_wrapper(private_output->output);
+       if (!wrapper) {
+               TDM_ERR("create 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_vblank = calloc(1, sizeof *private_vblank);
+       if (!private_vblank) {
+               /* LCOV_EXCL_START */
+
+               TDM_ERR("alloc failed");
+               wl_proxy_wrapper_destroy(wrapper);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       private_vblank->private_output = private_output;
+
+       private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
+       wl_proxy_wrapper_destroy(wrapper);
+       if (!private_vblank->vblank) {
+               /* LCOV_EXCL_START */
+
+               TDM_ERR("couldn't create vblank resource");
+               free(private_vblank);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       /* initial value */
+       private_vblank->fps = private_output->refresh;
+       private_vblank->offset = 0;
+       private_vblank->enable_fake = 0;
+
+       LIST_INITHEAD(&private_vblank->wait_list);
+
+       wl_tdm_vblank_add_listener(private_vblank->vblank,
+                                                          &tdm_client_vblank_listener, private_vblank);
+       wl_display_roundtrip_queue(private_client->display, private_client->queue);
+
+       wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               wl_tdm_vblank_destroy(private_vblank->vblank);
+               free(private_vblank);
+               if (error)
+                       *error = TDM_ERROR_PROTOCOL_ERROR;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+       }
+
+       LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return (tdm_client_vblank*)private_vblank;
+}
+
+void
+tdm_client_vblank_destroy(tdm_client_vblank *vblank)
+{
+       tdm_private_client_vblank *private_vblank;
+       tdm_private_client *private_client;
+       tdm_client_wait_info *w = NULL, *ww = NULL;
+
+       TDM_RETURN_IF_FAIL(vblank != NULL);
+
+       private_vblank = vblank;
+       private_client = private_vblank->private_output->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       LIST_DEL(&private_vblank->link);
+
+       LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
+               LIST_DEL(&w->link);
+               free(w);
+       }
+
+       wl_tdm_vblank_destroy(private_vblank->vblank);
+
+       free(private_vblank);
+
+       pthread_mutex_unlock(&private_client->lock);
+}
+
+tdm_error
+tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
+{
+       tdm_private_client_vblank *private_vblank;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       private_client = private_vblank->private_output->private_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 (!name)
+               name = TDM_VBLANK_DEFAULT_NAME;
+
+       strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
+       private_vblank->name[TDM_NAME_LEN - 1] = '\0';
+
+       wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
+{
+       tdm_private_client_vblank *private_vblank;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       private_client = private_vblank->private_output->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+       private_vblank->sync = sync;
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
+{
+       tdm_private_client_vblank *private_vblank;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       private_client = private_vblank->private_output->private_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_vblank->fps == fps) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       private_vblank->fps = fps;
+
+       wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
+{
+       tdm_private_client_vblank *private_vblank;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
+
+       private_client = private_vblank->private_output->private_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_vblank->offset == offset_ms) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       private_vblank->offset = offset_ms;
+
+       wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
+{
+       tdm_private_client_vblank *private_vblank;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       private_client = private_vblank->private_output->private_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_vblank->enable_fake == enable_fake) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       private_vblank->enable_fake = enable_fake;
+
+       wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
+{
+       tdm_private_client *private_client;
+       tdm_private_client_output *private_output;
+       tdm_private_client_vblank *private_vblank;
+       tdm_client_wait_info *w;
+       struct timespec tp;
+       unsigned int req_sec, req_usec;
+       int ret = 0;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+       /* can't support "interval 0" and "getting current_msc" things because
+        * there is a socket communication between TDM client and server. It's impossible
+        * to return the current msc or sequence immediately.
+        */
+       TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       private_output = private_vblank->private_output;
+       private_client = private_output->private_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_vblank->started)
+               private_vblank->started = 1;
+
+       if (!private_vblank->enable_fake) {
+               if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+                       TDM_ERR("output disconnected");
+                       pthread_mutex_unlock(&private_client->lock);
+                       return TDM_ERROR_OUTPUT_DISCONNECTED;
+               }
+               if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
+                       TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
+                       pthread_mutex_unlock(&private_client->lock);
+                       return TDM_ERROR_DPMS_OFF;
+               }
+       }
+
+       w = calloc(1, sizeof *w);
+       if (!w) {
+               /* LCOV_EXCL_START */
+
+               TDM_ERR("alloc failed");
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_OUT_OF_MEMORY;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       w->private_vblank = private_vblank;
+       w->func = func;
+       w->user_data = user_data;
+
+       LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
+       LIST_INITHEAD(&w->call_link);
+
+       clock_gettime(CLOCK_MONOTONIC, &tp);
+       req_sec = (unsigned int)tp.tv_sec;
+       req_usec = (unsigned int)(tp.tv_nsec / 1000);
+
+       w->req_id = ++private_output->req_id;
+       w->req_time = TDM_TIME(req_sec, req_usec);
+       w->need_free = (private_vblank->sync) ? 0 : 1;
+
+       wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
+
+       if (private_vblank->enable_ttrace)
+               TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
+
+       TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
+                       vblank, interval, w->req_id, w->req_time);
+
+       private_vblank->req_time = w->req_time;
+
+       if (private_vblank->last_time >= w->req_time)
+               TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
+
+       if (!private_vblank->sync) {
+               wl_display_flush(private_client->display);
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       /* LCOV_EXCL_START */
+
+       while (ret != -1 && !w->need_free)
+               ret = wl_display_dispatch(private_client->display);
+
+       clock_gettime(CLOCK_MONOTONIC, &tp);
+       TDM_DBG("block during %d us",
+                       ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
+                       - (req_sec * 1000000 + req_usec));
+
+       LIST_DEL(&w->link);
+       free(w);
+
+       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 TDM_ERROR_NONE;
+
+       /* LCOV_EXCL_STOP */
+}
+
+tdm_error
+tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
+                                                  tdm_client_vblank_handler func, void *user_data)
+{
+       tdm_private_client *private_client;
+       tdm_private_client_output *private_output;
+       tdm_private_client_vblank *private_vblank;
+       tdm_client_wait_info *w;
+       struct timespec tp;
+       unsigned int req_sec, req_usec;
+       int ret = 0;
+
+       TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_vblank = vblank;
+       private_output = private_vblank->private_output;
+       private_client = private_output->private_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_vblank->started)
+               private_vblank->started = 1;
+
+       if (!private_vblank->enable_fake) {
+               if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+                       TDM_ERR("output disconnected");
+                       pthread_mutex_unlock(&private_client->lock);
+                       return TDM_ERROR_OUTPUT_DISCONNECTED;
+               }
+               if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
+                       TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
+                       pthread_mutex_unlock(&private_client->lock);
+                       return TDM_ERROR_DPMS_OFF;
+               }
+       }
+
+       w = calloc(1, sizeof *w);
+       if (!w) {
+               /* LCOV_EXCL_START */
+
+               TDM_ERR("alloc failed");
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_OUT_OF_MEMORY;
+
+               /* LCOV_EXCL_STOP */
+       }
+
+       w->private_vblank = private_vblank;
+       w->func = func;
+       w->user_data = user_data;
+
+       LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
+       LIST_INITHEAD(&w->call_link);
+
+       clock_gettime(CLOCK_MONOTONIC, &tp);
+       req_sec = (unsigned int)tp.tv_sec;
+       req_usec = (unsigned int)(tp.tv_nsec / 1000);
+
+       w->req_id = ++private_output->req_id;
+       w->req_time = TDM_TIME(req_sec, req_usec);
+       w->need_free = (private_vblank->sync) ? 0 : 1;
+
+       wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
+
+       if (private_vblank->enable_ttrace)
+               TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
+
+       TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
+                       vblank, sequence, w->req_id, w->req_time);
+
+       private_vblank->req_time = w->req_time;
+
+       if (private_vblank->last_time >= w->req_time)
+               TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
+
+       if (!private_vblank->sync) {
+               wl_display_flush(private_client->display);
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       /* LCOV_EXCL_START */
+
+       while (ret != -1 && !w->need_free)
+               ret = wl_display_dispatch(private_client->display);
+
+       clock_gettime(CLOCK_MONOTONIC, &tp);
+       TDM_DBG("block during %d us",
+                       ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
+                       - (req_sec * 1000000 + req_usec));
+
+       LIST_DEL(&w->link);
+       free(w);
+
+       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 TDM_ERROR_NONE;
+
+       /* 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_attach_buffer(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;
+
+       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) {
+                       private_voutput->attach_buffer = 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;
+       tbm_surface_h buffer = NULL;
+       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);
+       TDM_RETURN_IF_FAIL(private_voutput->attach_buffer != NULL);
+
+       buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
+       TDM_RETURN_IF_FAIL(buffer != NULL);
+
+       tbm_surface_internal_ref(buffer);
+
+       private_client = private_voutput->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, buffer, h->user_data);
+       }
+
+       /* if no added commit_handler call commit done immediately */
+       if (LIST_IS_EMPTY(&private_voutput->commit_handler_list))
+               tdm_client_voutput_commit_done(private_voutput);
+
+       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_attach_buffer,
+       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 = NULL;
+       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->private_client = private_client;
+       strncpy(private_voutput->name, name, TDM_NAME_LEN - 1);
+       private_voutput->name[TDM_NAME_LEN - 1] = '\0';
+
+       private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, 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;
+       }
+
+       LIST_ADDTAIL(&private_voutput->link, &private_client->voutput_list);
+
+       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_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
+
+       if (!private_voutput)
+               return;
+       private_client = private_voutput->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       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;
+
+                       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);
+       }
+
+       if (private_voutput->get_output)
+               _tdm_client_output_destroy(private_voutput->private_output);
+
+       wl_tdm_voutput_destroy(private_voutput->wl_voutput);
+
+       LIST_DEL(&private_voutput->link);
+
+       free(private_voutput);
+
+       pthread_mutex_unlock(&private_client->lock);
+}
+
+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_private_client *private_client;
+       tdm_error ret = TDM_ERROR_NONE;
+
+       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;
+       private_client = private_voutput->private_client;
+
+       if (!private_voutput->private_output) {
+               private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
+               TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
+       }
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+               pthread_mutex_unlock(&private_client->lock);
+               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);
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       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_private_client *private_client;
+       tdm_error ret = TDM_ERROR_NONE;
+
+       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;
+       private_client = private_voutput->private_client;
+
+       if (!private_voutput->private_output) {
+               private_voutput->private_output = tdm_client_voutput_get_client_output(private_voutput, &ret);
+               TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
+       }
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_voutput->private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_BAD_REQUEST;
+       }
+
+       private_voutput->mmwidth = mmWidth;
+       private_voutput->mmheight = mmHeight;
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       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->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->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);
+
+               return;
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+}
+
+tdm_error
+tdm_client_voutput_commit_done(tdm_client_voutput *voutput)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tbm_surface_h buffer = NULL;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       TDM_RETURN_VAL_IF_FAIL(private_voutput->attach_buffer != NULL, TDM_ERROR_NONE);
+
+       private_client = private_voutput->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       buffer = (tbm_surface_h)wl_buffer_get_user_data(private_voutput->attach_buffer->wl_buffer);
+       tbm_surface_internal_unref(buffer);
+       private_voutput->attach_buffer = NULL;
+       wl_tdm_voutput_commit_done(private_voutput->wl_voutput);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       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;
+       tdm_private_client_output *private_output = NULL;
+       tdm_private_client *private_client;
+       tdm_error ret = TDM_ERROR_NONE;
+
+       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;
+       private_client = private_voutput->private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_voutput->get_output) {
+               pthread_mutex_unlock(&private_client->lock);
+               return private_voutput->private_output;
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+       private_output = (tdm_private_client_output *)tdm_client_get_output(private_voutput->private_client, private_voutput->name, &ret);
+       if (!private_output) {
+               TDM_ERR("tdm_client_voutput_get_client_output get private_output fail");
+               if (error)
+                       *error = ret;
+               return NULL;
+       }
+       pthread_mutex_lock(&private_client->lock);
+       private_output->voutput = private_voutput;
+       private_voutput->private_output = private_output;
+       private_voutput->get_output = 1;
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return private_output;
+}
+
+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_voutput_connect(tdm_client_voutput *voutput)
+{
+       tdm_private_client_output *private_output = NULL;;
+       tdm_private_client_voutput *private_voutput = NULL;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->private_client;
+       private_output = private_voutput->private_output;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_output->connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       if (!private_output->watch_output_changes)
+               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);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_voutput_disconnect(tdm_client_voutput *voutput)
+{
+       tdm_private_client_output *private_output = NULL;
+       tdm_private_client_voutput *private_voutput = NULL;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->private_client;
+       private_output = private_voutput->private_output;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_NONE;
+       }
+
+       if (!private_output->watch_output_changes)
+               private_output->connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
+
+       wl_tdm_voutput_disconnect(private_voutput->wl_voutput);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+
+tdm_error
+tdm_client_voutput_set_mode(tdm_client_voutput *voutput, int index)
+{
+       tdm_private_client_output *private_output;
+       tdm_private_client_voutput *private_voutput = NULL;
+       tdm_private_client *private_client;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(index >= 0, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->private_client;
+       private_output = private_voutput->private_output;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (private_voutput->available_modes.count - 1 < index) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_INVALID_PARAMETER;
+       }
+
+       if ((private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) ||
+               (private_voutput->available_modes.count == 0)) {
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_BAD_REQUEST;
+       }
+
+       TDM_DBG("mode_set request : %d", index);
+
+       wl_tdm_voutput_set_mode(private_voutput->wl_voutput, index);
 
-       LIST_DEL(&vblank_info->link);
-       free(vblank_info);
+       pthread_mutex_unlock(&private_client->lock);
 
-       return TDM_CLIENT_ERROR_NONE;
+       return TDM_ERROR_NONE;
 }