1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <boram1288.park@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
40 #include <tdm-server-protocol.h>
42 #include "tdm_private.h"
45 * - tdm server doesn't care about thread things.
46 * - DO NOT use the TDM internal functions here.
47 * However, the internal function which does lock/unlock the mutex of
48 * private_display in itself can be called.
49 * - DO NOT use the tdm_private_display structure here.
50 * - The callback function things can be called in main thread.
53 struct _tdm_private_server {
54 tdm_private_loop *private_loop;
55 struct list_head output_list;
56 struct list_head voutput_list;
57 struct list_head wait_list;
60 typedef struct _tdm_server_output_info {
61 struct list_head link;
62 tdm_private_server *private_server;
63 struct wl_resource *resource;
65 struct list_head vblank_list;
66 unsigned int watch_output_changes;
67 } tdm_server_output_info;
69 typedef struct _tdm_server_voutput_info {
70 struct list_head link;
71 tdm_private_server *private_server;
72 struct wl_resource *resource;
74 struct list_head output_list;
75 } tdm_server_voutput_info;
77 typedef struct _tdm_server_vblank_info {
78 struct list_head link;
79 tdm_server_output_info *output_info;
80 struct wl_resource *resource;
84 } tdm_server_vblank_info;
86 typedef struct _tdm_server_wait_info {
87 struct list_head link;
88 tdm_server_vblank_info *vblank_info;
92 } tdm_server_wait_info;
94 typedef struct _tdm_server_client_info {
95 struct list_head link;
97 char name[TDM_NAME_LEN];
98 struct wl_resource *resource;
99 } tdm_server_client_info;
101 static tdm_private_server *keep_private_server;
102 static struct list_head client_list;
104 static void destroy_wait(tdm_server_wait_info *wait_info);
107 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
109 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
113 snprintf(name, size, "Unknown");
115 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
116 h = fopen(proc, "r");
120 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
122 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
125 pname[len - 1] = '\0';
127 strncpy(name, pname, size - 1);
128 name[size - 1] = '\0';
133 /* LCOV_EXCL_START */
135 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
136 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
138 tdm_server_wait_info *found;
139 tdm_server_vblank_info *vblank_info;
141 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
143 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
144 tdm_server_wait_info, link, found);
146 TDM_ERR("wait_info(%p) is destroyed", wait_info);
150 if (tdm_debug_module & TDM_DEBUG_VBLANK)
151 TDM_DBG("req_id(%d) done", wait_info->req_id);
153 vblank_info = wait_info->vblank_info;
155 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
156 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
158 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
159 sequence, tv_sec, tv_usec, error);
161 destroy_wait(wait_info);
165 /* LCOV_EXCL_START */
167 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
168 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
170 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
174 /* LCOV_EXCL_START */
176 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
177 tdm_value value, void *user_data)
179 tdm_server_output_info *output_info = user_data;
180 struct wl_client *client;
183 TDM_RETURN_IF_FAIL(output_info != NULL);
185 client = wl_resource_get_client(output_info->resource);
186 TDM_RETURN_IF_FAIL(client != NULL);
188 wl_client_get_credentials(client, &pid, NULL, NULL);
190 if (!output_info->watch_output_changes) {
191 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
195 TDM_DBG("send the output changes: %d", (unsigned int)pid);
198 case TDM_OUTPUT_CHANGE_DPMS:
199 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
201 case TDM_OUTPUT_CHANGE_CONNECTION:
202 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
211 destroy_wait(tdm_server_wait_info *wait_info)
213 LIST_DEL(&wait_info->link);
218 destroy_vblank_callback(struct wl_resource *resource)
220 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
221 tdm_server_wait_info *w = NULL, *ww = NULL;
223 TDM_RETURN_IF_FAIL(vblank_info != NULL);
225 LIST_DEL(&vblank_info->link);
227 if (vblank_info->vblank)
228 tdm_vblank_destroy(vblank_info->vblank);
230 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
231 if (w->vblank_info == vblank_info)
238 /* LCOV_EXCL_START */
240 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
242 wl_resource_destroy(resource);
246 /* LCOV_EXCL_START */
248 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
250 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
253 ret = tdm_vblank_set_name(vblank_info->vblank, name);
254 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
258 /* LCOV_EXCL_START */
260 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
262 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
265 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
266 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
270 /* LCOV_EXCL_START */
272 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
274 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
277 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
278 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
283 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
285 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
288 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
289 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
293 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
294 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
296 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
297 tdm_server_output_info *output_info = vblank_info->output_info;
298 tdm_private_server *private_server = output_info->private_server;
299 tdm_server_wait_info *wait_info;
300 unsigned int enable_fake = 0;
303 wait_info = calloc(1, sizeof * wait_info);
305 /* LCOV_EXCL_START */
307 TDM_ERR("alloc failed");
308 ret = TDM_ERROR_OUT_OF_MEMORY;
314 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
315 wait_info->vblank_info = vblank_info;
316 wait_info->req_id = req_id;
317 wait_info->req_time = TDM_TIME(req_sec, req_usec);
319 if (tdm_debug_module & TDM_DEBUG_VBLANK)
320 TDM_DBG("req_id(%d) wait", req_id);
322 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
323 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
325 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
327 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
328 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
331 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
335 /* LCOV_EXCL_START */
337 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
339 destroy_wait(wait_info);
345 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
346 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
348 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
349 tdm_server_output_info *output_info = vblank_info->output_info;
350 tdm_private_server *private_server = output_info->private_server;
351 tdm_server_wait_info *wait_info;
352 unsigned int enable_fake = 0;
355 wait_info = calloc(1, sizeof * wait_info);
357 /* LCOV_EXCL_START */
359 TDM_ERR("alloc failed");
360 ret = TDM_ERROR_OUT_OF_MEMORY;
366 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
367 wait_info->vblank_info = vblank_info;
368 wait_info->req_id = req_id;
369 wait_info->req_time = TDM_TIME(req_sec, req_usec);
371 if (tdm_debug_module & TDM_DEBUG_VBLANK)
372 TDM_DBG("req_id(%d) wait", req_id);
374 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
375 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
377 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
379 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
380 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
383 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
387 /* LCOV_EXCL_START */
389 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
391 destroy_wait(wait_info);
396 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
397 _tdm_server_vblank_cb_destroy,
398 _tdm_server_vblank_cb_set_name,
399 _tdm_server_vblank_cb_set_fps,
400 _tdm_server_vblank_cb_set_offset,
401 _tdm_server_vblank_cb_set_enable_fake,
402 _tdm_server_vblank_cb_wait_vblank,
403 _tdm_server_vblank_cb_wait_vblank_seq,
406 /* LCOV_EXCL_START */
408 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
410 wl_resource_destroy(resource);
415 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
417 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
418 tdm_private_server *private_server = output_info->private_server;
419 tdm_private_loop *private_loop = private_server->private_loop;
420 struct wl_resource *vblank_resource;
422 tdm_server_vblank_info *vblank_info;
425 wl_resource_create(client, &wl_tdm_vblank_interface,
426 wl_resource_get_version(resource), id);
427 if (!vblank_resource) {
428 /* LCOV_EXCL_START */
430 wl_resource_post_no_memory(resource);
431 TDM_ERR("wl_resource_create failed");
437 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
439 /* LCOV_EXCL_START */
441 wl_resource_post_no_memory(resource);
442 wl_resource_destroy(vblank_resource);
443 TDM_ERR("tdm_vblank_create failed");
449 vblank_info = calloc(1, sizeof * vblank_info);
451 /* LCOV_EXCL_START */
453 wl_resource_post_no_memory(resource);
454 wl_resource_destroy(vblank_resource);
455 tdm_vblank_destroy(vblank);
456 TDM_ERR("alloc failed");
462 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
463 vblank_info->output_info = output_info;
464 vblank_info->resource = vblank_resource;
465 vblank_info->vblank = vblank;
466 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
468 tdm_vblank_set_resource(vblank, vblank_resource);
470 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
471 vblank_info, destroy_vblank_callback);
473 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
475 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
476 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
477 if (output == output_info->output)
478 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
484 /* LCOV_EXCL_START */
486 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
488 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
490 TDM_RETURN_IF_FAIL(output_info != NULL);
492 output_info->watch_output_changes = enable;
497 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
499 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
500 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
503 TDM_RETURN_IF_FAIL(output_info != NULL);
505 ret = tdm_output_get_conn_status(output_info->output, &status);
506 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
508 wl_tdm_output_send_connection(output_info->resource, status, ret);
513 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
517 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
519 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
520 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
523 TDM_RETURN_IF_FAIL(output_info != NULL);
525 ret = tdm_output_get_conn_status(output_info->output, &status);
526 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
528 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
529 const tdm_output_mode *mode = NULL;
530 unsigned int hdisplay, vdisplay, vrefresh;
532 ret = tdm_output_get_mode(output_info->output, &mode);
533 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
535 hdisplay = (mode) ? mode->hdisplay : 0;
536 vdisplay = (mode) ? mode->vdisplay : 0;
537 vrefresh = (mode) ? mode->vrefresh : 0;
539 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
541 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
546 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
550 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
552 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
553 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
556 TDM_RETURN_IF_FAIL(output_info != NULL);
558 ret = tdm_output_get_conn_status(output_info->output, &status);
559 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
561 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
562 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
564 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
565 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
567 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
569 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
574 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
577 static const struct wl_tdm_output_interface tdm_output_implementation = {
578 _tdm_server_output_cb_destroy,
579 _tdm_server_output_cb_create_vblank,
580 _tdm_server_output_cb_watch_output_changes,
581 _tdm_server_output_cb_get_connection,
582 _tdm_server_output_cb_get_mode,
583 _tdm_server_output_cb_get_dpms,
587 destroy_output_callback(struct wl_resource *resource)
589 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
590 tdm_server_vblank_info *v = NULL, *vv = NULL;
592 TDM_RETURN_IF_FAIL(output_info != NULL);
594 LIST_DEL(&output_info->link);
596 tdm_output_remove_change_handler(output_info->output,
597 _tdm_server_cb_output_change, output_info);
599 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
600 wl_resource_destroy(v->resource);
607 destroy_voutput_callback(struct wl_resource *resource)
609 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
611 TDM_RETURN_IF_FAIL(voutput_info != NULL);
613 LIST_DEL(&voutput_info->link);
619 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
620 const char *name, uint32_t id)
622 tdm_private_server *private_server = wl_resource_get_user_data(resource);
623 tdm_server_output_info *output_info;
624 struct wl_resource *output_resource = NULL;
626 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
629 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
631 /* LCOV_EXCL_START */
633 TDM_ERR("There is no '%s' output", name);
634 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
635 "There is no '%s' output", name);
642 wl_resource_create(client, &wl_tdm_output_interface,
643 wl_resource_get_version(resource), id);
644 if (!output_resource) {
645 /* LCOV_EXCL_START */
647 wl_resource_post_no_memory(resource);
648 TDM_ERR("wl_resource_create failed");
654 output_info = calloc(1, sizeof * output_info);
656 /* LCOV_EXCL_START */
658 wl_resource_post_no_memory(resource);
659 wl_resource_destroy(output_resource);
660 TDM_ERR("alloc failed");
666 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
667 if (ret != TDM_ERROR_NONE) {
668 wl_resource_post_no_memory(resource);
669 wl_resource_destroy(output_resource);
671 TDM_ERR("tdm_output_add_change_handler failed");
675 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
676 output_info->private_server = private_server;
677 output_info->resource = output_resource;
678 output_info->output = output;
679 LIST_INITHEAD(&output_info->vblank_list);
681 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
682 output_info, destroy_output_callback);
684 ret = tdm_output_get_conn_status(output, &status);
685 wl_tdm_output_send_connection(output_resource, status, ret);
687 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
688 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
689 const tdm_output_mode *mode = NULL;
690 unsigned int hdisplay, vdisplay, vrefresh;
692 ret = tdm_output_get_mode(output, &mode);
693 hdisplay = (mode) ? mode->hdisplay : 0;
694 vdisplay = (mode) ? mode->vdisplay : 0;
695 vrefresh = (mode) ? mode->vrefresh : 0;
696 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
698 ret = tdm_output_get_dpms(output, &dpms_value);
699 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
701 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
702 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
706 typedef struct _tdm_server_voutput_info {
707 tdm_private_server *private_server;
708 tdm_output_conn_status status;
712 tdm_output_mode *modes;
714 } tdm_server_voutput_info;
716 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
718 wl_resource_destroy(resource);
722 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
723 struct wl_resource *resource,
727 uint32_t hsync_start,
732 uint32_t vsync_start,
741 tdm_server_voutput_info *voutput_info;
742 tdm_output_mode *tmp_modes, *old_modes;
743 tdm_output_mode *new_mode;
746 voutput_info = wl_resource_get_user_data(resource);
748 count = voutput_info->available_modes.count;
749 old_modes = voutput_info->available_modes.modes;
754 tmp_modes = malloc(count * sizeof(*tmp_modes));
755 memcpy(tmp_modes, old_modes, count * sizeof(tdm_output_mode));
758 voutput_info->available_modes.count = index + 1;
759 voutput_info->available_modes.modes =
760 realloc(voutput_info->available_modes.modes,
761 sizeof(tdm_output_mode) * (index + 1));
765 memcpy(voutput_info->available_modes.modes, tmp_modes, count * sizeof(tdm_output_mode));
770 new_mode = &voutput_info->available_modes.modes[index];
771 new_mode->clock = clock;
772 new_mode->hdisplay = hdisplay;
773 new_mode->hsync_start = hsync_start;
774 new_mode->hsync_end = hsync_end;
775 new_mode->htotal = htotal;
776 new_mode->hskew= hskew;
777 new_mode->vdisplay= vdisplay;
778 new_mode->vsync_start= vsync_start;
779 new_mode->vsync_end = vsync_end;
780 new_mode->vtotal = vtotal;
781 new_mode->vscan = vscan;
782 new_mode->vrefresh = vrefresh;
783 new_mode->flags = flags;
784 new_mode->type = type;
787 strncpy(new_mode->name, name, len);
788 new_mode->name[len] = '\0';
792 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
794 tdm_server_voutput_info *voutput_info;
796 voutput_info = wl_resource_get_user_data(resource);
797 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
801 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
803 tdm_server_voutput_info *voutput_info;
805 voutput_info = wl_resource_get_user_data(resource);
806 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
808 /* Do free resources when it's being disconnected */
809 free(voutput_info->available_modes.modes);
810 voutput_info->available_modes.modes = NULL;
811 voutput_info->available_modes.count = 0;
814 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
815 _tdm_voutput_cb_destroy,
816 _tdm_voutput_cb_set_available_modes,
817 _tdm_voutput_cb_connect,
818 _tdm_voutput_cb_disconnect
822 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
824 tdm_server_voutput_info *voutput_info;
826 voutput_info = wl_resource_get_user_data(resource);
828 /* Do free your own resource */
833 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
835 struct wl_resource *voutput_resource = NULL;
836 tdm_private_server *private_server = wl_resource_get_user_data(resource);
837 tdm_server_voutput_info *voutput_info;
841 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
843 TDM_ERR("There is '%s' output, cannot create.", name);
844 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
845 "There is '%s' output", name);
849 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
851 TDM_ERR("output creation fail(%s)(%d).", name, ret);
852 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
853 "%s output creation fail", name);
858 wl_resource_create(client, &wl_tdm_voutput_interface,
859 wl_resource_get_version(resource), id);
860 if (!voutput_resource) {
861 /* LCOV_EXCL_START */
863 wl_resource_post_no_memory(resource);
864 TDM_ERR("wl_resource_create failed");
870 voutput_info = calloc(1, sizeof * voutput_info);
872 /* LCOV_EXCL_START */
874 wl_resource_post_no_memory(resource);
875 wl_resource_destroy(voutput_resource);
876 TDM_ERR("alloc failed");
882 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
883 voutput_info->private_server = private_server;
884 voutput_info->resource = voutput_resource;
885 voutput_info->output = output;
886 LIST_INITHEAD(&voutput_info->output_list);
888 wl_resource_set_implementation(voutput_resource,
889 &tdm_voutput_implementation,
891 tdm_voutput_cb_resource_destroy);
893 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
896 /* LCOV_EXCL_START */
898 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
900 tdm_private_server *private_server = wl_resource_get_user_data(resource);
901 tdm_private_loop *private_loop = private_server->private_loop;
902 char message[TDM_SERVER_REPLY_MSG_LEN];
904 int len = sizeof(message), size;
907 wl_client_get_credentials(client, NULL, &uid, NULL);
910 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
911 TDM_ERR("%s", message);
913 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
916 size = sizeof(message) - len;
919 wl_client_flush(client);
922 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
923 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
925 strncpy(buffer, m, copylen);
929 buffer[copylen] = '\0';
931 wl_tdm_send_debug_message(resource, buffer);
934 wl_tdm_send_debug_done(resource);
938 static const struct wl_tdm_interface tdm_implementation = {
939 _tdm_server_cb_debug,
940 _tdm_server_cb_create_output,
941 _tdm_server_cb_create_virtual_output
945 destroy_client(struct wl_resource *resource)
947 tdm_server_client_info *c = NULL, *cc = NULL;
948 struct wl_client *client;
950 client = wl_resource_get_client(resource);
951 TDM_RETURN_IF_FAIL(client != NULL);
953 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
954 if (c->resource == resource) {
963 _tdm_server_bind(struct wl_client *client, void *data,
964 uint32_t version, uint32_t id)
966 struct wl_resource *resource;
967 tdm_server_client_info *cinfo;
969 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
971 /* LCOV_EXCL_START */
973 wl_client_post_no_memory(client);
979 cinfo = calloc(1, sizeof(tdm_server_client_info));
981 /* LCOV_EXCL_START */
983 wl_client_post_no_memory(client);
984 wl_resource_destroy(resource);
990 cinfo->resource = resource;
992 LIST_ADDTAIL(&cinfo->link, &client_list);
993 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
994 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
996 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1000 tdm_server_init(tdm_private_loop *private_loop)
1002 tdm_private_server *private_server;
1004 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1005 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1007 if (private_loop->private_server)
1008 return TDM_ERROR_NONE;
1010 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1011 /* LCOV_EXCL_START */
1013 TDM_ERR("createing a tdm-socket failed");
1014 return TDM_ERROR_OPERATION_FAILED;
1016 /* LCOV_EXCL_STOP */
1019 private_server = calloc(1, sizeof * private_server);
1020 if (!private_server) {
1021 TDM_ERR("alloc failed");
1022 return TDM_ERROR_OUT_OF_MEMORY;
1025 LIST_INITHEAD(&private_server->output_list);
1026 LIST_INITHEAD(&private_server->voutput_list);
1027 LIST_INITHEAD(&private_server->wait_list);
1029 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1030 private_server, _tdm_server_bind)) {
1031 /* LCOV_EXCL_START */
1033 TDM_ERR("creating a global resource failed");
1034 free(private_server);
1035 return TDM_ERROR_OUT_OF_MEMORY;
1037 /* LCOV_EXCL_STOP */
1040 private_server->private_loop = private_loop;
1041 private_loop->private_server = private_server;
1042 keep_private_server = private_server;
1044 LIST_INITHEAD(&client_list);
1046 return TDM_ERROR_NONE;
1050 tdm_server_deinit(tdm_private_loop *private_loop)
1052 tdm_server_output_info *o = NULL, *oo = NULL;
1053 // tdm_server_voutput_info *vo = NULL, *voo = NULL;
1054 tdm_server_wait_info *w = NULL, *ww = NULL;
1055 tdm_server_client_info *c = NULL, *cc = NULL;
1056 tdm_private_server *private_server;
1058 if (!private_loop->private_server)
1061 private_server = private_loop->private_server;
1063 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1067 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1068 wl_resource_destroy(o->resource);
1071 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1072 wl_resource_destroy(vo->resource);
1075 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1076 wl_resource_destroy(c->resource);
1079 free(private_server);
1080 private_loop->private_server = NULL;
1081 keep_private_server = NULL;
1084 /* LCOV_EXCL_START */
1086 tdm_server_get_client_name(pid_t pid)
1088 tdm_server_client_info *c = NULL;
1090 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1092 return (const char*)c->name;
1097 /* LCOV_EXCL_STOP */
1099 /* LCOV_EXCL_START */
1101 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1103 tdm_private_server *private_server = keep_private_server;
1104 tdm_server_output_info *output_info = NULL;
1106 if (!keep_private_server)
1107 return TDM_ERROR_NONE;
1109 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1110 tdm_server_vblank_info *vblank_info = NULL;
1112 if (output && output_info->output != output)
1115 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1116 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1120 return TDM_ERROR_NONE;
1122 /* LCOV_EXCL_STOP */