server: set permission and group to tdm-socket when it is added
[platform/core/uifw/libtdm.git] / src / tdm_server.c
index 1233deb..9363768 100644 (file)
  *     However, the internal function which does lock/unlock the mutex of
  *     private_display in itself can be called.
  * - DO NOT use the tdm_private_display structure here.
+ * - The callback function things can be called in main thread.
  */
 
 struct _tdm_private_server {
        tdm_private_loop *private_loop;
-       struct list_head client_list;
-       struct list_head vblank_list;
+       struct list_head output_list;
+       struct list_head wait_list;
 };
 
-typedef struct _tdm_server_client_info {
+typedef struct _tdm_server_output_info {
        struct list_head link;
        tdm_private_server *private_server;
        struct wl_resource *resource;
-
-       tdm_output *vblank_output;
-       double vblank_gap;
-       unsigned int last_tv_sec;
-       unsigned int last_tv_usec;
-} tdm_server_client_info;
+       tdm_output *output;
+       struct list_head vblank_list;
+       unsigned int watch_output_changes;
+} tdm_server_output_info;
 
 typedef struct _tdm_server_vblank_info {
        struct list_head link;
-       tdm_server_client_info *client_info;
+       tdm_server_output_info *output_info;
        struct wl_resource *resource;
 
-       unsigned int req_sec;
-       unsigned int req_usec;
-
-       tdm_event_loop_source *timer_source;
-       unsigned int timer_target_sec;
-       unsigned int timer_target_usec;
+       tdm_vblank *vblank;
 } tdm_server_vblank_info;
 
+typedef struct _tdm_server_wait_info {
+       struct list_head link;
+       tdm_server_vblank_info *vblank_info;
+
+       unsigned int req_id;
+} tdm_server_wait_info;
+
+typedef struct _tdm_server_client_info {
+       struct list_head link;
+       pid_t pid;
+       char name[TDM_NAME_LEN];
+} tdm_server_client_info;
+
 static tdm_private_server *keep_private_server;
-static int tdm_debug_server;
+static struct list_head client_list;
 
-static void
-_tdm_server_send_done(tdm_server_vblank_info *vblank_info, unsigned int sequence,
-                                         unsigned int tv_sec, unsigned int tv_usec);
+static void destroy_wait(tdm_server_wait_info *wait_info);
 
-static tdm_error
-_tdm_server_cb_timer(void *user_data)
+static void
+_tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
 {
-       tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data;
+       char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
+       FILE *h;
+       size_t len;
 
-       _tdm_server_send_done(vblank_info, 0,
-                                                 vblank_info->timer_target_sec,
-                                                 vblank_info->timer_target_usec);
+       snprintf(name, size, "Unknown");
 
-       return TDM_ERROR_NONE;
+       snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
+       h = fopen(proc, "r");
+       if (!h)
+               return;
+
+       len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
+       if (len == 0) {
+               char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
+               len = p - pname;
+       }
+       pname[len - 1] = '\0';
+
+       strncpy(name, pname, size - 1);
+       name[size - 1] = '\0';
+
+       fclose(h);
 }
 
-static tdm_error
-_tdm_server_update_timer(tdm_server_vblank_info *vblank_info, int interval)
+static tdm_output*
+_tdm_server_find_output(tdm_private_server *private_server, const char *name)
 {
-       tdm_server_client_info *client_info = vblank_info->client_info;
-       tdm_private_server *private_server = client_info->private_server;
        tdm_private_loop *private_loop = private_server->private_loop;
-       unsigned long last, prev_req, req, curr, next;
-       unsigned int ms_delay;
-       tdm_error ret;
+       tdm_output *found = NULL;
 
-       ret = tdm_display_lock(private_loop->dpy);
-       TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
-
-       if (!vblank_info->timer_source) {
-               vblank_info->timer_source =
-                       tdm_event_loop_add_timer_handler(private_loop->dpy,
-                                                                                        _tdm_server_cb_timer,
-                                                                                        vblank_info,
-                                                                                        NULL);
-               if (!vblank_info->timer_source) {
-                       TDM_ERR("couldn't add timer");
-                       tdm_display_unlock(private_loop->dpy);
-                       return TDM_ERROR_OPERATION_FAILED;
-               }
-       }
+       if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
+               found = tdm_display_get_output(private_loop->dpy, 0, NULL);
 
-       last = (unsigned long)client_info->last_tv_sec * 1000000 + client_info->last_tv_usec;
-       req = (unsigned long)vblank_info->req_sec * 1000000 + vblank_info->req_usec;
-       curr = tdm_helper_get_time_in_micros();
+       if (!found) {
+               int count = 0, i;
 
-       prev_req = last + (unsigned int)((req - last) / client_info->vblank_gap) * client_info->vblank_gap;
-       next = prev_req + (unsigned long)(client_info->vblank_gap * interval);
+               tdm_display_get_output_count(private_loop->dpy, &count);
 
-       while (next < curr)
-               next += (unsigned long)client_info->vblank_gap;
+               for (i = 0; i < count; i++) {
+                       tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
+                       tdm_output_conn_status status;
+                       const char *model = NULL;
+                       tdm_error ret;
 
-       TDM_DBG("last(%.6lu) req(%.6lu) curr(%.6lu) prev_req(%.6lu) next(%.6lu)",
-                       last, req, curr, prev_req, next);
+                       ret = tdm_output_get_conn_status(output, &status);
+                       if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
+                               continue;
 
-       ms_delay = (unsigned int)ceil((double)(next - curr) / 1000);
-       if (ms_delay == 0)
-               ms_delay = 1;
+                       ret = tdm_output_get_model_info(output, NULL, &model, NULL);
+                       if (ret || !model)
+                               continue;
 
-       TDM_DBG("delay(%lu) ms_delay(%d)", next - curr, ms_delay);
+                       if (strncmp(model, name, TDM_NAME_LEN))
+                               continue;
 
-       ret = tdm_event_loop_source_timer_update(vblank_info->timer_source, ms_delay);
-       if (ret != TDM_ERROR_NONE) {
-               tdm_event_loop_source_remove(vblank_info->timer_source);
-               vblank_info->timer_source = NULL;
-               tdm_display_unlock(private_loop->dpy);
-               TDM_ERR("couldn't update timer");
-               return TDM_ERROR_OPERATION_FAILED;
+                       found = output;
+                       break;
+               }
        }
 
-       TDM_DBG("timer tick: %d us", (int)(next - last));
-
-       vblank_info->timer_target_sec = next / 1000000;
-       vblank_info->timer_target_usec = next % 1000000;
-
-       tdm_display_unlock(private_loop->dpy);
-
-       return TDM_ERROR_NONE;
+       return found;
 }
 
 static void
-_tdm_server_send_done(tdm_server_vblank_info *vblank_info, unsigned int sequence,
-                                         unsigned int tv_sec, unsigned int tv_usec)
+_tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
+                                         unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
 {
-       tdm_server_vblank_info *found;
-       tdm_server_client_info *client_info;
-       unsigned long vtime = (tv_sec * 1000000) + tv_usec;
-       unsigned long curr = tdm_helper_get_time_in_micros();
+       tdm_server_wait_info *found;
+       tdm_server_vblank_info *vblank_info;
 
        if (!keep_private_server)
                return;
 
-       LIST_FIND_ITEM(vblank_info, &keep_private_server->vblank_list,
-                                  tdm_server_vblank_info, link, found);
+       LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
+                                  tdm_server_wait_info, link, found);
        if (!found) {
-               TDM_DBG("vblank_info(%p) is destroyed", vblank_info);
+               TDM_DBG("wait_info(%p) is destroyed", wait_info);
                return;
        }
 
-       client_info = vblank_info->client_info;
-       client_info->last_tv_sec = tv_sec;
-       client_info->last_tv_usec = tv_usec;
+       if (tdm_debug_module & TDM_DEBUG_VBLANK)
+               TDM_INFO("req_id(%d) done", wait_info->req_id);
 
-       TDM_DBG("wl_tdm_vblank@%d done. tv(%lu) curr(%lu)",
-                       wl_resource_get_id(vblank_info->resource), vtime, curr);
+       TDM_TRACE_COUNT(ServerDoneVBlank, wait_info->req_id);
 
-       if (tdm_debug_server) {
-               if (curr - vtime > 1000) /* 1ms */
-                       TDM_WRN("delay: %d us", (int)(curr - vtime));
-       }
+       vblank_info = wait_info->vblank_info;
+       wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
+                                                       sequence, tv_sec, tv_usec, error);
+       destroy_wait(wait_info);
+}
 
-       wl_tdm_vblank_send_done(vblank_info->resource, sequence, tv_sec, tv_usec);
-       wl_resource_destroy(vblank_info->resource);
+static void
+_tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
+                                         unsigned int tv_sec, unsigned int tv_usec, void *user_data)
+{
+       _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
 }
 
 static void
-_tdm_server_cb_output_vblank(tdm_output *output, unsigned int sequence,
-                                                        unsigned int tv_sec, unsigned int tv_usec,
-                                                        void *user_data)
+_tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
+                                                        tdm_value value, void *user_data)
 {
-       tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data;
+       tdm_server_output_info *output_info = user_data;
+       struct wl_client *client;
+       pid_t pid = 0;
+
+       TDM_RETURN_IF_FAIL(output_info != NULL);
+
+       client = wl_resource_get_client(output_info->resource);
+       TDM_RETURN_IF_FAIL(client != NULL);
+
+       wl_client_get_credentials(client, &pid, NULL, NULL);
 
-       _tdm_server_send_done(vblank_info, sequence, tv_sec, tv_usec);
+       if (!output_info->watch_output_changes) {
+               TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
+               return;
+       }
+
+       TDM_DBG("send the output changes: %d", (unsigned int)pid);
+
+       switch (type) {
+       case TDM_OUTPUT_CHANGE_DPMS:
+               wl_tdm_output_send_dpms(output_info->resource, value.u32);
+               break;
+       case TDM_OUTPUT_CHANGE_CONNECTION:
+               wl_tdm_output_send_connection(output_info->resource, value.u32);
+               break;
+       default:
+               break;
+       }
+}
+
+static void
+destroy_wait(tdm_server_wait_info *wait_info)
+{
+       LIST_DEL(&wait_info->link);
+       free(wait_info);
 }
 
 static void
 destroy_vblank_callback(struct wl_resource *resource)
 {
        tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+       tdm_server_wait_info *w = NULL, *ww = NULL;
 
-       if (vblank_info->timer_source) {
-               tdm_private_server *private_server = vblank_info->client_info->private_server;
+       TDM_RETURN_IF_FAIL(vblank_info != NULL);
 
-               tdm_display_lock(private_server->private_loop->dpy);
-               tdm_event_loop_source_remove(vblank_info->timer_source);
-               tdm_display_unlock(private_server->private_loop->dpy);
+       if (vblank_info->vblank)
+               tdm_vblank_destroy(vblank_info->vblank);
+
+       LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
+               if (w->vblank_info == vblank_info)
+                       destroy_wait(w);
        }
 
        LIST_DEL(&vblank_info->link);
@@ -226,114 +255,152 @@ destroy_vblank_callback(struct wl_resource *resource)
 }
 
 static void
-_tdm_server_client_cb_destroy(struct wl_client *client, struct wl_resource *resource)
+_tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
 {
        wl_resource_destroy(resource);
 }
 
 static void
-_tdm_server_client_cb_wait_vblank(struct wl_client *client,
-                                                                 struct wl_resource *resource,
-                                                                 uint32_t id, const char *name,
-                                                                 int32_t sw_timer, int32_t interval,
-                                                                 uint32_t req_sec, uint32_t req_usec)
+_tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
 {
-       tdm_server_client_info *client_info = wl_resource_get_user_data(resource);
-       tdm_private_server *private_server = client_info->private_server;
-       tdm_private_loop *private_loop = private_server->private_loop;
-       tdm_server_vblank_info *vblank_info;
-       struct wl_resource *vblank_resource;
-       tdm_output *found = NULL;
-       tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_ON;
+       tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+
+       tdm_vblank_set_name(vblank_info->vblank, name);
+}
+
+static void
+_tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
+{
+       tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+
+       tdm_vblank_set_fps(vblank_info->vblank, fps);
+}
+
+static void
+_tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
+{
+       tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+
+       tdm_vblank_set_offset(vblank_info->vblank, offset);
+}
+
+static void
+_tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
+{
+       tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+
+       tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
+}
+
+static void
+_tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
+                                                                 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
+{
+       tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+       tdm_server_output_info *output_info = vblank_info->output_info;
+       tdm_private_server *private_server = output_info->private_server;
+       tdm_server_wait_info *wait_info;
+       unsigned int enable_fake = 0;
        tdm_error ret;
-       const char *model;
 
-       TDM_DBG("The tdm client requests vblank");
+       TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
 
-       if (tdm_debug_server) {
-               unsigned long curr = tdm_helper_get_time_in_micros();
-               unsigned long reqtime = (req_sec * 1000000) + req_usec;
-               if (curr - reqtime > 1000) /* 1ms */
-                       TDM_WRN("delay(req): %d us", (int)(curr - reqtime));
+       wait_info = calloc(1, sizeof * wait_info);
+       if (!wait_info) {
+               TDM_ERR("alloc failed");
+               ret = TDM_ERROR_OUT_OF_MEMORY;
+               goto wait_failed;
        }
 
-       if (client_info->vblank_output) {
-               model = NULL;
-               ret = tdm_output_get_model_info(client_info->vblank_output, NULL, &model, NULL);
-               if (model && !strncmp(model, name, TDM_NAME_LEN))
-                       found = client_info->vblank_output;
-       }
+       LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
+       wait_info->vblank_info = vblank_info;
+       wait_info->req_id = req_id;
 
-       if (!strncmp(name, "primary", TDM_NAME_LEN))
-               found = tdm_display_get_output(private_loop->dpy, 0, NULL);
+       if (tdm_debug_module & TDM_DEBUG_VBLANK)
+               TDM_INFO("req_id(%d) wait", req_id);
 
-       if (!found) {
-               int count = 0, i;
+       ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
 
-               tdm_display_get_output_count(private_loop->dpy, &count);
+       tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
+       if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
+               goto wait_failed;
 
-               for (i = 0; i < count; i++) {
-                       tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
-                       tdm_output_conn_status status;
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
 
-                       ret = tdm_output_get_conn_status(output, &status);
-                       if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
-                               continue;
+       return;
+wait_failed:
+       wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
+       if (wait_info)
+               destroy_wait(wait_info);
+}
 
-                       model = NULL;
-                       ret = tdm_output_get_model_info(output, NULL, &model, NULL);
-                       if (ret || !model)
-                               continue;
+static void
+_tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
+                                                                         uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
+{
+       tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+       tdm_server_output_info *output_info = vblank_info->output_info;
+       tdm_private_server *private_server = output_info->private_server;
+       tdm_server_wait_info *wait_info;
+       unsigned int enable_fake = 0;
+       tdm_error ret;
 
-                       if (strncmp(model, name, TDM_NAME_LEN))
-                               continue;
+       TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
 
-                       found = output;
-                       break;
-               }
+       wait_info = calloc(1, sizeof * wait_info);
+       if (!wait_info) {
+               TDM_ERR("alloc failed");
+               ret = TDM_ERROR_OUT_OF_MEMORY;
+               goto wait_failed;
        }
 
-       if (!found) {
-               wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_INVALID_NAME,
-                                                          "There is no '%s' output", name);
-               TDM_ERR("There is no '%s' output", name);
-               return;
-       }
+       LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
+       wait_info->vblank_info = vblank_info;
+       wait_info->req_id = req_id;
 
-       if (client_info->vblank_output != found) {
-               const tdm_output_mode *mode = NULL;
+       if (tdm_debug_module & TDM_DEBUG_VBLANK)
+               TDM_INFO("req_id(%d) wait", req_id);
 
-               client_info->vblank_output = found;
+       ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
 
-               tdm_output_get_mode(client_info->vblank_output, &mode);
-               if (!mode || mode->vrefresh <= 0) {
-                       wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
-                                                                  "couldn't get mode of %s", name);
-                       TDM_ERR("couldn't get mode of %s", name);
-                       return;
-               }
+       tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
+       if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
+               goto wait_failed;
 
-               client_info->vblank_gap = (double)1000000 / mode->vrefresh;
-               TDM_INFO("vblank_gap(%.6lf)", client_info->vblank_gap);
-       }
+       TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
 
-       tdm_output_get_dpms(found, &dpms_value);
+       return;
+wait_failed:
+       wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
+       if (wait_info)
+               destroy_wait(wait_info);
+}
 
-       if (dpms_value != TDM_OUTPUT_DPMS_ON && !sw_timer) {
-               wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_DPMS_OFF,
-                                                          "dpms '%s'", tdm_get_dpms_str(dpms_value));
-               TDM_ERR("dpms '%s'", tdm_get_dpms_str(dpms_value));
-               return;
-       }
+static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
+       _tdm_server_vblank_cb_destroy,
+       _tdm_server_vblank_cb_set_name,
+       _tdm_server_vblank_cb_set_fps,
+       _tdm_server_vblank_cb_set_offset,
+       _tdm_server_vblank_cb_set_enable_fake,
+       _tdm_server_vblank_cb_wait_vblank,
+       _tdm_server_vblank_cb_wait_vblank_seq,
+};
 
-       vblank_info = calloc(1, sizeof * vblank_info);
-       if (!vblank_info) {
-               wl_resource_post_no_memory(resource);
-               TDM_ERR("alloc failed");
-               return;
-       }
+static void
+_tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
+{
+       wl_resource_destroy(resource);
+}
 
-       TDM_DBG("wl_tdm_vblank@%d output(%s) interval(%d)", id, name, interval);
+static void
+_tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
+{
+       tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
+       tdm_private_server *private_server = output_info->private_server;
+       tdm_private_loop *private_loop = private_server->private_loop;
+       struct wl_resource *vblank_resource;
+       tdm_vblank *vblank;
+       tdm_server_vblank_info *vblank_info;
 
        vblank_resource =
                wl_resource_create(client, &wl_tdm_vblank_interface,
@@ -341,105 +408,202 @@ _tdm_server_client_cb_wait_vblank(struct wl_client *client,
        if (!vblank_resource) {
                wl_resource_post_no_memory(resource);
                TDM_ERR("wl_resource_create failed");
-               goto free_info;
+               return;
        }
 
-       vblank_info->resource = vblank_resource;
-       vblank_info->client_info = client_info;
-       vblank_info->req_sec = req_sec;
-       vblank_info->req_usec = req_usec;
-
-       if (dpms_value == TDM_OUTPUT_DPMS_ON) {
-               ret = tdm_output_wait_vblank(found, interval, 0,
-                                                                        _tdm_server_cb_output_vblank, vblank_info);
-               if (ret != TDM_ERROR_NONE) {
-                       wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
-                                                                  "couldn't wait vblank for %s", name);
-                       TDM_ERR("couldn't wait vblank for %s", name);
-                       goto destroy_resource;
-               }
-       } else if (sw_timer) {
-               ret = _tdm_server_update_timer(vblank_info, interval);
-               if (ret != TDM_ERROR_NONE) {
-                       wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
-                                                                  "couldn't update timer for %s", name);
-                       TDM_ERR("couldn't update timer for %s", name);
-                       goto destroy_resource;
-               }
-       } else {
-               wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
-                                                          "bad implementation");
-               TDM_NEVER_GET_HERE();
-               goto destroy_resource;
+       vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
+       if (!vblank) {
+               wl_resource_post_no_memory(resource);
+               wl_resource_destroy(vblank_resource);
+               TDM_ERR("tdm_vblank_create failed");
+               return;
        }
 
-       wl_resource_set_implementation(vblank_resource, NULL, vblank_info,
-                                                                  destroy_vblank_callback);
+       vblank_info = calloc(1, sizeof * vblank_info);
+       if (!vblank_info) {
+               wl_resource_post_no_memory(resource);
+               wl_resource_destroy(vblank_resource);
+               tdm_vblank_destroy(vblank);
+               TDM_ERR("alloc failed");
+               return;
+       }
+
+       LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
+       vblank_info->output_info = output_info;
+       vblank_info->resource = vblank_resource;
+       vblank_info->vblank = vblank;
+
+       tdm_vblank_set_resource(vblank, vblank_resource);
+
+       wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
+                                                                  vblank_info, destroy_vblank_callback);
 
-       LIST_ADDTAIL(&vblank_info->link, &private_server->vblank_list);
        return;
-destroy_resource:
-       wl_resource_destroy(vblank_resource);
-free_info:
-       free(vblank_info);
 }
 
-static const struct wl_tdm_client_interface tdm_client_implementation = {
-       _tdm_server_client_cb_destroy,
-       _tdm_server_client_cb_wait_vblank,
+static void
+_tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
+{
+       tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
+
+       TDM_RETURN_IF_FAIL(output_info != NULL);
+
+       output_info->watch_output_changes = enable;
+}
+
+static const struct wl_tdm_output_interface tdm_output_implementation = {
+       _tdm_server_output_cb_destroy,
+       _tdm_server_output_cb_create_vblank,
+       _tdm_server_output_cb_watch_output_changes,
 };
 
 static void
-destroy_client_callback(struct wl_resource *resource)
+destroy_output_callback(struct wl_resource *resource)
 {
-       tdm_server_client_info *client_info = wl_resource_get_user_data(resource);
-       LIST_DEL(&client_info->link);
-       free(client_info);
+       tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
+       tdm_server_vblank_info *v = NULL, *vv = NULL;
+
+       TDM_RETURN_IF_FAIL(output_info != NULL);
+
+       tdm_output_remove_change_handler(output_info->output,
+                                                                        _tdm_server_cb_output_change, output_info);
+
+       LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
+               wl_resource_destroy(v->resource);
+       }
+
+       LIST_DEL(&output_info->link);
+       free(output_info);
 }
 
 static void
-_tdm_server_cb_create_client(struct wl_client *client,
-                                                        struct wl_resource *resource, uint32_t id)
+_tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
+                                                        const char *name, uint32_t id)
 {
        tdm_private_server *private_server = wl_resource_get_user_data(resource);
-       tdm_server_client_info *client_info;
-       struct wl_resource *client_resource;
+       tdm_server_output_info *output_info;
+       struct wl_resource *output_resource = NULL;
+       tdm_output *output;
+       const tdm_output_mode *mode = NULL;
+       tdm_output_dpms dpms_value;
+       tdm_output_conn_status status;
+
+       output = _tdm_server_find_output(private_server, name);
+       if (!output) {
+               TDM_ERR("There is no '%s' output", name);
+               wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                                          "There is no '%s' output", name);
+               return;
+       }
 
-       client_info = calloc(1, sizeof * client_info);
-       if (!client_info) {
-               wl_resource_post_no_memory(resource);
-               TDM_ERR("alloc failed");
+       tdm_output_get_mode(output, &mode);
+       if (!mode) {
+               TDM_ERR("no mode for '%s' output", name);
+               wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                                          "no mode for '%s' output", name);
                return;
        }
 
-       client_resource =
-               wl_resource_create(client, &wl_tdm_client_interface,
+       tdm_output_get_dpms(output, &dpms_value);
+       tdm_output_get_conn_status(output, &status);
+
+       output_resource =
+               wl_resource_create(client, &wl_tdm_output_interface,
                                                   wl_resource_get_version(resource), id);
-       if (!client_resource) {
+       if (!output_resource) {
                wl_resource_post_no_memory(resource);
-               free(client_info);
                TDM_ERR("wl_resource_create failed");
                return;
        }
 
-       client_info->private_server = private_server;
-       client_info->resource = client_resource;
+       output_info = calloc(1, sizeof * output_info);
+       if (!output_info) {
+               wl_resource_post_no_memory(resource);
+               wl_resource_destroy(output_resource);
+               TDM_ERR("alloc failed");
+               return;
+       }
+
+       LIST_ADDTAIL(&output_info->link, &private_server->output_list);
+       output_info->private_server = private_server;
+       output_info->resource = output_resource;
+       output_info->output = output;
+       LIST_INITHEAD(&output_info->vblank_list);
+
+       tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
+
+       wl_resource_set_implementation(output_resource, &tdm_output_implementation,
+                                                                  output_info, destroy_output_callback);
+
+       wl_tdm_output_send_mode(output_resource, mode->hdisplay, mode->vdisplay, mode->vrefresh);
+       wl_tdm_output_send_dpms(output_resource, dpms_value);
+       wl_tdm_output_send_connection(output_resource, status);
+}
+
+static void
+_tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
+{
+       tdm_private_server *private_server = wl_resource_get_user_data(resource);
+       tdm_private_loop *private_loop = private_server->private_loop;
+       char message[TDM_SERVER_REPLY_MSG_LEN];
+       int size = sizeof(message);
+       uid_t uid;
+
+       wl_client_get_credentials(client, NULL, &uid, NULL);
+
+       if (uid != 0) {
+               snprintf(message, size, "tdm-monitor: SHOULD be a superuser.\n");
+               TDM_ERR("%s", message);
+       } else {
+               tdm_monitor_server_command(private_loop->dpy, options, message, &size);
+       }
+
+       wl_tdm_send_debug_done(resource, message);
+}
 
-       wl_resource_set_implementation(client_resource, &tdm_client_implementation,
-                                                                  client_info, destroy_client_callback);
+static void
+_tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
+                                                                        unsigned int pid, const char *name, unsigned int fps)
+{
+       tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
+       TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
 
-       LIST_ADDTAIL(&client_info->link, &private_server->client_list);
+       TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
 }
 
 static const struct wl_tdm_interface tdm_implementation = {
-       _tdm_server_cb_create_client,
+       _tdm_server_cb_debug,
+       _tdm_server_cb_create_output,
+       _tdm_server_cb_set_client_vblank_fps,
 };
 
 static void
+destroy_client(struct wl_resource *resource)
+{
+       tdm_server_client_info *c = NULL, *cc = NULL;
+       struct wl_client *client;
+       pid_t pid = -1;
+
+       client = wl_resource_get_client(resource);
+       TDM_RETURN_IF_FAIL(client != NULL);
+
+       wl_client_get_credentials(client, &pid, NULL, NULL);
+
+       LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
+               if (c->pid == pid) {
+                       LIST_DEL(&c->link);
+                       free(c);
+                       return;
+               }
+       }
+}
+
+static void
 _tdm_server_bind(struct wl_client *client, void *data,
                                 uint32_t version, uint32_t id)
 {
        struct wl_resource *resource;
+       tdm_server_client_info *cinfo;
 
        resource = wl_resource_create(client, &wl_tdm_interface, version, id);
        if (!resource) {
@@ -447,29 +611,136 @@ _tdm_server_bind(struct wl_client *client, void *data,
                return;
        }
 
-       TDM_DBG("tdm server binding");
+       cinfo = calloc(1, sizeof(tdm_server_client_info));
+       if (!cinfo) {
+               wl_client_post_no_memory(client);
+               wl_resource_destroy(resource);
+               return;
+       }
+
+       LIST_ADDTAIL(&cinfo->link, &client_list);
+       wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
+       _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
 
-       wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
+       wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
+}
+
+static int
+_tdm_getgrnam_r(const char *name)
+{
+       struct group *grp = NULL;
+       struct group *grp_res = NULL;
+       char* buf = NULL;
+       size_t buf_len;
+       int ret;
+       int id;
+
+       buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (buf_len == -1)
+               buf_len = 2048;
+
+       buf = calloc(1, buf_len * sizeof(char));
+       if (!buf) {
+               TDM_ERR("creating buffer failed");
+               goto failed;
+       }
+
+       grp = calloc(1, sizeof(struct group));
+       if (!grp) {
+               TDM_ERR("creating group failed");
+               goto failed;
+       }
+
+       ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
+       if (ret < 0) {
+               TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
+               goto failed;
+       }
+
+       if (grp_res == NULL) {
+               TDM_ERR("finding name:%s group failed", name);
+               goto failed;
+       }
+
+       id = grp->gr_gid;
+       free(buf);
+       free(grp);
+
+       return id;
+
+failed:
+       if (buf)
+               free(buf);
+       if (grp)
+               free(grp);
+
+       return -1;
+}
+
+static int
+_tdm_socket_init(tdm_private_loop *private_loop)
+{
+       const char *dir = NULL;
+       char socket_path[128];
+       int ret = -1;
+       uid_t uid;
+       gid_t gid;
+
+       if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
+               TDM_ERR("createing a tdm-socket failed");
+               return 0;
+       }
+
+       dir = getenv("XDG_RUNTIME_DIR");
+       if (!dir) {
+               TDM_ERR("getting XDG_RUNTIME_DIR failed");
+               return 0;
+       }
+
+       snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tdm-socket");
+
+       ret = chmod(socket_path, 509);
+       if (ret < 0) {
+               TDM_ERR("changing modes of socket file failed:%s (%m)", socket_path);
+               return 0;
+       }
+
+       ret = _tdm_getgrnam_r("root");
+       if (ret < 0) {
+               TDM_ERR("getting uid failed");
+               return 0;
+       }
+       uid = ret;
+
+       ret = _tdm_getgrnam_r("display");
+       if (ret < 0) {
+               TDM_ERR("getting gid failed");
+               return 0;
+       }
+       gid = ret;
+
+       ret = chown(socket_path, uid, gid);
+       if (ret < 0) {
+               TDM_ERR("changing owner of socket file failed:%s (%m)", socket_path);
+               return 0;
+       }
+
+       return 1;
 }
 
 INTERN tdm_error
 tdm_server_init(tdm_private_loop *private_loop)
 {
        tdm_private_server *private_server;
-       const char *debug;
 
-       debug = getenv("TDM_DEBUG_SERVER");
-       if (debug && (strstr(debug, "1")))
-               tdm_debug_server = 1;
+       TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
+       TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
 
        if (private_loop->private_server)
                return TDM_ERROR_NONE;
 
-       TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
-       TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
-
-       if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
-               TDM_ERR("createing a tdm-socket failed");
+       if (!_tdm_socket_init(private_loop)) {
+               TDM_ERR("initializing tdm-socket failed");
                return TDM_ERROR_OPERATION_FAILED;
        }
 
@@ -479,8 +750,8 @@ tdm_server_init(tdm_private_loop *private_loop)
                return TDM_ERROR_OUT_OF_MEMORY;
        }
 
-       LIST_INITHEAD(&private_server->client_list);
-       LIST_INITHEAD(&private_server->vblank_list);
+       LIST_INITHEAD(&private_server->output_list);
+       LIST_INITHEAD(&private_server->wait_list);
 
        if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
                                                  private_server, _tdm_server_bind)) {
@@ -493,13 +764,16 @@ tdm_server_init(tdm_private_loop *private_loop)
        private_loop->private_server = private_server;
        keep_private_server = private_server;
 
+       LIST_INITHEAD(&client_list);
+
        return TDM_ERROR_NONE;
 }
 
 INTERN void
 tdm_server_deinit(tdm_private_loop *private_loop)
 {
-       tdm_server_vblank_info *v = NULL, *vv = NULL;
+       tdm_server_output_info *o = NULL, *oo = NULL;
+       tdm_server_wait_info *w = NULL, *ww = NULL;
        tdm_server_client_info *c = NULL, *cc = NULL;
        tdm_private_server *private_server;
 
@@ -508,15 +782,33 @@ tdm_server_deinit(tdm_private_loop *private_loop)
 
        private_server = private_loop->private_server;
 
-       LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_server->vblank_list, link) {
-               wl_resource_destroy(v->resource);
+       LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
+               destroy_wait(w);
        }
 
-       LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_server->client_list, link) {
-               wl_resource_destroy(c->resource);
+       LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
+               wl_resource_destroy(o->resource);
        }
 
        free(private_server);
        private_loop->private_server = NULL;
        keep_private_server = NULL;
+
+       LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
+               LIST_DEL(&c->link);
+               free(c);
+       }
+}
+
+INTERN const char*
+tdm_server_get_client_name(pid_t pid)
+{
+       tdm_server_client_info *c = NULL;
+
+       LIST_FOR_EACH_ENTRY(c, &client_list, link) {
+               if (c->pid == pid)
+                       return (const char*)c->name;
+       }
+
+       return NULL;
 }