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 #define WL_HIDE_DEPRECATED
42 #include <tdm-server-protocol.h>
44 #include "tdm_private.h"
47 * - tdm server doesn't care about thread things.
48 * - DO NOT use the TDM internal functions here.
49 * However, the internal function which does lock/unlock the mutex of
50 * private_display in itself can be called.
51 * - DO NOT use the tdm_private_display structure here.
52 * - The callback function things can be called in main thread.
55 struct _tdm_private_server {
56 tdm_private_loop *private_loop;
57 struct list_head output_list;
58 struct list_head voutput_list;
59 struct list_head wait_list;
62 typedef struct _tdm_server_output_info {
63 struct list_head link;
64 tdm_private_server *private_server;
65 struct wl_resource *resource;
67 struct list_head vblank_list;
68 unsigned int watch_output_changes;
69 } tdm_server_output_info;
71 typedef struct _tdm_server_voutput_buffer {
72 struct list_head link;
73 struct wl_resource *wl_buffer;
75 } tdm_server_voutput_buffer;
77 typedef struct _tdm_server_voutput_info {
78 struct list_head link;
79 tdm_private_server *private_server;
80 struct wl_resource *resource;
82 struct list_head output_list;
84 tdm_output_conn_status status;
88 tdm_output_mode *modes;
92 unsigned int mmheight;
94 struct list_head buffer_list;
95 tdm_server_voutput_buffer *attach_buffer;
97 } tdm_server_voutput_info;
99 typedef struct _tdm_server_vblank_info {
100 struct list_head link;
101 tdm_server_output_info *output_info;
102 struct wl_resource *resource;
106 } tdm_server_vblank_info;
108 typedef struct _tdm_server_wait_info {
109 struct list_head link;
110 tdm_server_vblank_info *vblank_info;
114 } tdm_server_wait_info;
116 typedef struct _tdm_server_client_info {
117 struct list_head link;
119 char name[TDM_NAME_LEN];
120 struct wl_resource *resource;
121 } tdm_server_client_info;
123 static tdm_private_server *keep_private_server;
124 static struct list_head client_list;
126 static void destroy_wait(tdm_server_wait_info *wait_info);
129 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
131 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
135 snprintf(name, size, "Unknown");
137 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
138 h = fopen(proc, "r");
142 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
144 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
147 pname[len - 1] = '\0';
149 strncpy(name, pname, size - 1);
150 name[size - 1] = '\0';
155 /* LCOV_EXCL_START */
157 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
158 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
160 tdm_server_wait_info *found;
161 tdm_server_vblank_info *vblank_info;
163 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
165 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
166 tdm_server_wait_info, link, found);
168 TDM_ERR("wait_info(%p) is destroyed", wait_info);
172 if (tdm_debug_module & TDM_DEBUG_VBLANK)
173 TDM_DBG("req_id(%d) done", wait_info->req_id);
175 vblank_info = wait_info->vblank_info;
177 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
178 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
180 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
181 sequence, tv_sec, tv_usec, error);
183 destroy_wait(wait_info);
187 /* LCOV_EXCL_START */
189 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
190 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
192 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
196 /* LCOV_EXCL_START */
198 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
199 tdm_value value, void *user_data)
201 tdm_server_output_info *output_info = user_data;
202 struct wl_client *client;
205 TDM_RETURN_IF_FAIL(output_info != NULL);
207 client = wl_resource_get_client(output_info->resource);
208 TDM_RETURN_IF_FAIL(client != NULL);
210 wl_client_get_credentials(client, &pid, NULL, NULL);
212 if (!output_info->watch_output_changes) {
213 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
217 TDM_DBG("send the output changes: %d", (unsigned int)pid);
220 case TDM_OUTPUT_CHANGE_DPMS:
221 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
223 case TDM_OUTPUT_CHANGE_CONNECTION:
224 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
233 destroy_wait(tdm_server_wait_info *wait_info)
235 LIST_DEL(&wait_info->link);
240 destroy_vblank_callback(struct wl_resource *resource)
242 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
243 tdm_server_wait_info *w = NULL, *ww = NULL;
245 TDM_RETURN_IF_FAIL(vblank_info != NULL);
247 LIST_DEL(&vblank_info->link);
249 if (vblank_info->vblank)
250 tdm_vblank_destroy(vblank_info->vblank);
252 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
253 if (w->vblank_info == vblank_info)
260 /* LCOV_EXCL_START */
262 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
264 wl_resource_destroy(resource);
268 /* LCOV_EXCL_START */
270 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
272 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
275 ret = tdm_vblank_set_name(vblank_info->vblank, name);
276 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
280 /* LCOV_EXCL_START */
282 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
284 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
287 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
288 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
292 /* LCOV_EXCL_START */
294 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
296 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
299 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
300 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
305 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
307 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
310 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
311 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
315 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
316 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
318 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
319 tdm_server_output_info *output_info = vblank_info->output_info;
320 tdm_private_server *private_server = output_info->private_server;
321 tdm_server_wait_info *wait_info;
322 unsigned int enable_fake = 0;
325 wait_info = calloc(1, sizeof * wait_info);
327 /* LCOV_EXCL_START */
329 TDM_ERR("alloc failed");
330 ret = TDM_ERROR_OUT_OF_MEMORY;
336 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
337 wait_info->vblank_info = vblank_info;
338 wait_info->req_id = req_id;
339 wait_info->req_time = TDM_TIME(req_sec, req_usec);
341 if (tdm_debug_module & TDM_DEBUG_VBLANK)
342 TDM_DBG("req_id(%d) wait", req_id);
344 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
345 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
347 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
349 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
350 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
353 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
357 /* LCOV_EXCL_START */
359 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
361 destroy_wait(wait_info);
367 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
368 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
370 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
371 tdm_server_output_info *output_info = vblank_info->output_info;
372 tdm_private_server *private_server = output_info->private_server;
373 tdm_server_wait_info *wait_info;
374 unsigned int enable_fake = 0;
377 wait_info = calloc(1, sizeof * wait_info);
379 /* LCOV_EXCL_START */
381 TDM_ERR("alloc failed");
382 ret = TDM_ERROR_OUT_OF_MEMORY;
388 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
389 wait_info->vblank_info = vblank_info;
390 wait_info->req_id = req_id;
391 wait_info->req_time = TDM_TIME(req_sec, req_usec);
393 if (tdm_debug_module & TDM_DEBUG_VBLANK)
394 TDM_DBG("req_id(%d) wait", req_id);
396 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
397 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
399 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
401 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
402 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
405 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
409 /* LCOV_EXCL_START */
411 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
413 destroy_wait(wait_info);
418 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
419 _tdm_server_vblank_cb_destroy,
420 _tdm_server_vblank_cb_set_name,
421 _tdm_server_vblank_cb_set_fps,
422 _tdm_server_vblank_cb_set_offset,
423 _tdm_server_vblank_cb_set_enable_fake,
424 _tdm_server_vblank_cb_wait_vblank,
425 _tdm_server_vblank_cb_wait_vblank_seq,
428 /* LCOV_EXCL_START */
430 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
432 wl_resource_destroy(resource);
437 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
439 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
440 tdm_private_server *private_server = output_info->private_server;
441 tdm_private_loop *private_loop = private_server->private_loop;
442 struct wl_resource *vblank_resource;
444 tdm_server_vblank_info *vblank_info;
447 wl_resource_create(client, &wl_tdm_vblank_interface,
448 wl_resource_get_version(resource), id);
449 if (!vblank_resource) {
450 /* LCOV_EXCL_START */
452 wl_resource_post_no_memory(resource);
453 TDM_ERR("wl_resource_create failed");
459 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
461 /* LCOV_EXCL_START */
463 wl_resource_post_no_memory(resource);
464 wl_resource_destroy(vblank_resource);
465 TDM_ERR("tdm_vblank_create failed");
471 vblank_info = calloc(1, sizeof * vblank_info);
473 /* LCOV_EXCL_START */
475 wl_resource_post_no_memory(resource);
476 wl_resource_destroy(vblank_resource);
477 tdm_vblank_destroy(vblank);
478 TDM_ERR("alloc failed");
484 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
485 vblank_info->output_info = output_info;
486 vblank_info->resource = vblank_resource;
487 vblank_info->vblank = vblank;
488 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
490 tdm_vblank_set_resource(vblank, vblank_resource);
492 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
493 vblank_info, destroy_vblank_callback);
495 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
497 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
498 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
499 if (output == output_info->output)
500 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
506 /* LCOV_EXCL_START */
508 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
510 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
512 TDM_RETURN_IF_FAIL(output_info != NULL);
514 output_info->watch_output_changes = enable;
519 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
521 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
522 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
525 TDM_RETURN_IF_FAIL(output_info != NULL);
527 ret = tdm_output_get_conn_status(output_info->output, &status);
528 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
530 wl_tdm_output_send_connection(output_info->resource, status, ret);
535 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
539 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
541 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
542 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
545 TDM_RETURN_IF_FAIL(output_info != NULL);
547 ret = tdm_output_get_conn_status(output_info->output, &status);
548 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
550 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
551 const tdm_output_mode *mode = NULL;
552 unsigned int hdisplay, vdisplay, vrefresh;
554 ret = tdm_output_get_mode(output_info->output, &mode);
555 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
557 hdisplay = (mode) ? mode->hdisplay : 0;
558 vdisplay = (mode) ? mode->vdisplay : 0;
559 vrefresh = (mode) ? mode->vrefresh : 0;
561 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
563 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
568 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
572 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
574 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
575 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
578 TDM_RETURN_IF_FAIL(output_info != NULL);
580 ret = tdm_output_get_conn_status(output_info->output, &status);
581 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
583 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
584 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
586 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
587 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
589 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
591 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
596 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
599 static const struct wl_tdm_output_interface tdm_output_implementation = {
600 _tdm_server_output_cb_destroy,
601 _tdm_server_output_cb_create_vblank,
602 _tdm_server_output_cb_watch_output_changes,
603 _tdm_server_output_cb_get_connection,
604 _tdm_server_output_cb_get_mode,
605 _tdm_server_output_cb_get_dpms,
609 destroy_output_callback(struct wl_resource *resource)
611 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
612 tdm_server_vblank_info *v = NULL, *vv = NULL;
614 TDM_RETURN_IF_FAIL(output_info != NULL);
616 LIST_DEL(&output_info->link);
618 tdm_output_remove_change_handler(output_info->output,
619 _tdm_server_cb_output_change, output_info);
621 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
622 wl_resource_destroy(v->resource);
629 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
630 const char *name, uint32_t id)
632 tdm_private_server *private_server = wl_resource_get_user_data(resource);
633 tdm_server_output_info *output_info;
634 struct wl_resource *output_resource = NULL;
636 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
639 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
641 /* LCOV_EXCL_START */
643 TDM_ERR("There is no '%s' output", name);
644 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
645 "There is no '%s' output", name);
652 wl_resource_create(client, &wl_tdm_output_interface,
653 wl_resource_get_version(resource), id);
654 if (!output_resource) {
655 /* LCOV_EXCL_START */
657 wl_resource_post_no_memory(resource);
658 TDM_ERR("wl_resource_create failed");
664 output_info = calloc(1, sizeof * output_info);
666 /* LCOV_EXCL_START */
668 wl_resource_post_no_memory(resource);
669 wl_resource_destroy(output_resource);
670 TDM_ERR("alloc failed");
676 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
677 if (ret != TDM_ERROR_NONE) {
678 wl_resource_post_no_memory(resource);
679 wl_resource_destroy(output_resource);
681 TDM_ERR("tdm_output_add_change_handler failed");
685 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
686 output_info->private_server = private_server;
687 output_info->resource = output_resource;
688 output_info->output = output;
689 LIST_INITHEAD(&output_info->vblank_list);
691 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
692 output_info, destroy_output_callback);
694 ret = tdm_output_get_conn_status(output, &status);
695 wl_tdm_output_send_connection(output_resource, status, ret);
697 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
698 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
699 const tdm_output_mode *mode = NULL;
700 unsigned int hdisplay, vdisplay, vrefresh;
702 ret = tdm_output_get_mode(output, &mode);
703 hdisplay = (mode) ? mode->hdisplay : 0;
704 vdisplay = (mode) ? mode->vdisplay : 0;
705 vrefresh = (mode) ? mode->vrefresh : 0;
706 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
708 ret = tdm_output_get_dpms(output, &dpms_value);
709 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
711 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
712 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
716 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
718 tdm_server_voutput_info *voutput_info;
719 tdm_private_server *private_server;
721 tdm_error ret = TDM_ERROR_NONE;
723 voutput_info = wl_resource_get_user_data(resource);
725 private_server = voutput_info->private_server;
726 output = voutput_info->output;
729 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
731 if (ret != TDM_ERROR_NONE)
732 TDM_ERR("_tdm_voutput_cb_destroy fail");
734 wl_resource_destroy(resource);
738 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
739 struct wl_resource *resource,
740 struct wl_array *modes)
742 tdm_server_voutput_info *voutput_info;
743 tdm_output_mode *mode;
744 int size, count = 0, i = 0;
746 voutput_info = wl_resource_get_user_data(resource);
748 voutput_info->available_modes.count = 0;
749 if (voutput_info->available_modes.modes)
750 free(voutput_info->available_modes.modes);
752 wl_array_for_each(mode, modes)
754 size = sizeof(tdm_output_mode);
756 voutput_info->available_modes.modes = malloc(count * size);
757 voutput_info->available_modes.count = count;
759 wl_array_for_each(mode, modes)
760 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
764 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
765 unsigned int mmwidth, unsigned int mmheight)
767 tdm_server_voutput_info *voutput_info;
769 voutput_info = wl_resource_get_user_data(resource);
771 voutput_info->mmwidth = mmwidth;
772 voutput_info->mmheight = mmheight;
776 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
778 tdm_server_voutput_info *voutput_info;
780 voutput_info = wl_resource_get_user_data(resource);
781 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
783 tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
784 tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
785 tdm_output_set_connect(voutput_info->output);
789 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
791 tdm_server_voutput_info *voutput_info;
793 voutput_info = wl_resource_get_user_data(resource);
794 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
796 /* Do free resources when it's being disconnected */
797 free(voutput_info->available_modes.modes);
798 voutput_info->available_modes.modes = NULL;
799 voutput_info->available_modes.count = 0;
800 voutput_info->mmwidth = 0;
801 voutput_info->mmheight = 0;
803 tdm_output_set_disconnect(voutput_info->output);
807 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
809 tdm_server_voutput_info *voutput_info;
811 voutput_info = wl_resource_get_user_data(resource);
812 if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED)
817 // tdm_output_commit_done(voutput_info->output, voutput_info->attach_buffer->buffer);
818 voutput_info->committing = 0;
819 voutput_info->attach_buffer = NULL;
822 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
823 _tdm_voutput_cb_destroy,
824 _tdm_voutput_cb_set_available_modes,
825 _tdm_voutput_cb_set_physical_size,
826 _tdm_voutput_cb_connect,
827 _tdm_voutput_cb_disconnect,
828 _tdm_voutput_cb_commit_done
832 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
834 wl_resource_destroy(wl_buffer);
837 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
838 _tdm_voutput_wl_buffer_destroy
842 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
844 tdm_server_voutput_info *voutput_info = NULL;
845 tdm_server_voutput_buffer *vb = NULL;
847 voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
848 TDM_RETURN_IF_FAIL(voutput_info != NULL);
850 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
851 if (vb->wl_buffer == wl_buffer) {
852 tbm_surface_internal_unref(vb->buffer);
853 wl_resource_set_user_data(wl_buffer, NULL);
860 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
862 struct wl_client *wl_client;
863 struct wl_resource *wl_buffer = NULL;
865 wl_client = wl_resource_get_client(voutput_info->resource);
867 /* create a wl_buffer resource */
868 wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
869 TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
871 wl_resource_set_implementation(wl_buffer,
872 (void (**)(void)) &_tdm_voutput_buffer_impementation,
873 voutput_info, _tdm_voutput_buffer_destory);
879 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
880 tbm_surface_h buffer)
882 int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
883 struct wl_resource *wl_buffer = NULL;
884 int num_buf, is_fd = -1, i;
885 tbm_surface_info_s info;
887 struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
890 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
891 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
893 if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
894 TDM_ERR("Failed to create buffer from surface");
898 if (info.num_planes > 3) {
899 TDM_ERR("invalid num_planes(%d)", info.num_planes);
903 num_buf = tbm_surface_internal_get_num_bos(buffer);
905 TDM_ERR("surface doesn't have any bo.");
909 for (i = 0; i < num_buf; i++) {
910 tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
912 TDM_ERR("Failed to get bo from surface");
916 /* try to get fd first */
917 if (is_fd == -1 || is_fd == 1) {
918 bufs[i] = tbm_bo_export_fd(bo);
919 if (is_fd == -1 && bufs[i] >= 0)
923 /* if fail to get fd, try to get name second */
924 if (is_fd == -1 || is_fd == 0) {
925 bufs[i] = tbm_bo_export(bo);
926 if (is_fd == -1 && bufs[i] > 0)
931 (is_fd == 1 && bufs[i] < 0) ||
932 (is_fd == 0 && bufs[i] <= 0)) {
933 TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
938 wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
940 TDM_ERR("Failed to create wl_buffer");
944 wl_array_init(&plane_buf_idx);
945 wl_array_init(&plane_offset);
946 wl_array_init(&plane_stride);
947 wl_array_init(&plane_size);
949 for (i = 0; i < 3; i++) {
950 p = wl_array_add(&plane_buf_idx, sizeof(int));
951 *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
952 p = wl_array_add(&plane_offset, sizeof(int));
953 *p = info.planes[i].offset;
954 p = wl_array_add(&plane_stride, sizeof(int));
955 *p = info.planes[i].stride;
956 p = wl_array_add(&plane_size, sizeof(int));
957 *p = info.planes[i].size;
961 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
963 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
964 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
965 flags, num_buf, bufs[0],
966 (bufs[1] == -1) ? bufs[0] : bufs[1],
967 (bufs[2] == -1) ? bufs[0] : bufs[2]);
969 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
971 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
972 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
974 num_buf, bufs[0], bufs[1], bufs[2]);
976 wl_array_release(&plane_buf_idx);
977 wl_array_release(&plane_offset);
978 wl_array_release(&plane_stride);
979 wl_array_release(&plane_size);
981 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
982 if (is_fd == 1 && (bufs[i] >= 0))
989 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
990 if (is_fd == 1 && (bufs[i] >= 0))
997 tdm_server_voutput_buffer *
998 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
1000 tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
1002 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1003 if (vb && vb->buffer == buffer)
1007 tbm_surface_internal_ref(buffer);
1008 voutput_buffer = calloc(1, sizeof *voutput_buffer);
1009 if (!voutput_buffer) {
1010 /* LCOV_EXCL_START */
1012 TDM_ERR("fail calloc");
1013 tbm_surface_internal_unref(buffer);
1016 /* LCOV_EXCL_STOP */
1019 voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, buffer);
1020 if (!voutput_buffer->wl_buffer) {
1021 /* LCOV_EXCL_START */
1023 TDM_ERR("fail export buffer");
1024 free(voutput_buffer);
1025 tbm_surface_internal_unref(buffer);
1028 /* LCOV_EXCL_STOP */
1031 voutput_buffer->buffer = buffer;
1032 LIST_ADDTAIL(&voutput_info->link, &voutput_info->buffer_list);
1034 return voutput_buffer;
1038 tdm_output_send_buffer(tdm_output *output, tbm_surface_h buffer)
1040 tdm_private_server *private_server = keep_private_server;
1041 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1042 tdm_server_voutput_buffer *voutput_buffer = NULL;
1044 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1046 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1047 if (vo && vo->output == output) {
1052 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1053 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer == NULL, TDM_ERROR_OPERATION_FAILED);
1055 voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer);
1056 TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1058 voutput_info->attach_buffer = voutput_buffer;
1060 wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
1062 return TDM_ERROR_NONE;
1066 tdm_output_commit_buffer(tdm_output *output)
1068 tdm_private_server *private_server = keep_private_server;
1069 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1071 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1073 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1074 if (vo && vo->output == output) {
1079 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1080 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
1081 TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
1083 voutput_info->committing = 1;
1085 wl_tdm_voutput_send_commit(voutput_info->resource);
1087 return TDM_ERROR_NONE;
1091 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1093 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1094 tdm_server_voutput_buffer *vb, *vbb;
1095 TDM_RETURN_IF_FAIL(voutput_info != NULL);
1097 LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
1100 LIST_DEL(&vb->link);
1103 wl_resource_destroy(vb->wl_buffer);
1106 LIST_DEL(&voutput_info->link);
1108 /* Do free your own resource */
1113 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1115 struct wl_resource *voutput_resource = NULL;
1116 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1117 tdm_server_voutput_info *voutput_info;
1121 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1123 TDM_ERR("There is '%s' output, cannot create.", name);
1124 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1125 "There is '%s' output", name);
1129 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
1131 TDM_ERR("output creation fail(%s)(%d).", name, ret);
1132 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1133 "%s output creation fail", name);
1138 wl_resource_create(client, &wl_tdm_voutput_interface,
1139 wl_resource_get_version(resource), id);
1140 if (!voutput_resource) {
1141 /* LCOV_EXCL_START */
1143 wl_resource_post_no_memory(resource);
1144 TDM_ERR("wl_resource_create failed");
1147 /* LCOV_EXCL_STOP */
1150 voutput_info = calloc(1, sizeof * voutput_info);
1151 if (!voutput_info) {
1152 /* LCOV_EXCL_START */
1154 wl_resource_post_no_memory(resource);
1155 wl_resource_destroy(voutput_resource);
1156 TDM_ERR("alloc failed");
1159 /* LCOV_EXCL_STOP */
1162 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1163 voutput_info->private_server = private_server;
1164 voutput_info->resource = voutput_resource;
1165 voutput_info->output = output;
1166 LIST_INITHEAD(&voutput_info->output_list);
1167 LIST_INITHEAD(&voutput_info->buffer_list);
1169 wl_resource_set_implementation(voutput_resource,
1170 &tdm_voutput_implementation,
1172 tdm_voutput_cb_resource_destroy);
1174 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1177 /* LCOV_EXCL_START */
1179 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1181 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1182 tdm_private_loop *private_loop = private_server->private_loop;
1183 char message[TDM_SERVER_REPLY_MSG_LEN];
1185 int len = sizeof(message), size;
1188 wl_client_get_credentials(client, NULL, &uid, NULL);
1191 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1192 TDM_ERR("%s", message);
1194 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1197 size = sizeof(message) - len;
1200 wl_client_flush(client);
1203 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1204 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1206 strncpy(buffer, m, copylen);
1210 buffer[copylen] = '\0';
1212 wl_tdm_send_debug_message(resource, buffer);
1215 wl_tdm_send_debug_done(resource);
1217 /* LCOV_EXCL_STOP */
1219 static const struct wl_tdm_interface tdm_implementation = {
1220 _tdm_server_cb_debug,
1221 _tdm_server_cb_create_output,
1222 _tdm_server_cb_create_virtual_output
1226 destroy_client(struct wl_resource *resource)
1228 tdm_server_client_info *c = NULL, *cc = NULL;
1229 struct wl_client *client;
1231 client = wl_resource_get_client(resource);
1232 TDM_RETURN_IF_FAIL(client != NULL);
1234 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1235 if (c->resource == resource) {
1244 _tdm_server_bind(struct wl_client *client, void *data,
1245 uint32_t version, uint32_t id)
1247 struct wl_resource *resource;
1248 tdm_server_client_info *cinfo;
1250 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1252 /* LCOV_EXCL_START */
1254 wl_client_post_no_memory(client);
1257 /* LCOV_EXCL_STOP */
1260 cinfo = calloc(1, sizeof(tdm_server_client_info));
1262 /* LCOV_EXCL_START */
1264 wl_client_post_no_memory(client);
1265 wl_resource_destroy(resource);
1268 /* LCOV_EXCL_STOP */
1271 cinfo->resource = resource;
1273 LIST_ADDTAIL(&cinfo->link, &client_list);
1274 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1275 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1277 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1281 tdm_server_init(tdm_private_loop *private_loop)
1283 tdm_private_server *private_server;
1285 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1286 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1288 if (private_loop->private_server)
1289 return TDM_ERROR_NONE;
1291 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1292 /* LCOV_EXCL_START */
1294 TDM_ERR("createing a tdm-socket failed");
1295 return TDM_ERROR_OPERATION_FAILED;
1297 /* LCOV_EXCL_STOP */
1300 private_server = calloc(1, sizeof * private_server);
1301 if (!private_server) {
1302 TDM_ERR("alloc failed");
1303 return TDM_ERROR_OUT_OF_MEMORY;
1306 LIST_INITHEAD(&private_server->output_list);
1307 LIST_INITHEAD(&private_server->voutput_list);
1308 LIST_INITHEAD(&private_server->wait_list);
1310 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1311 private_server, _tdm_server_bind)) {
1312 /* LCOV_EXCL_START */
1314 TDM_ERR("creating a global resource failed");
1315 free(private_server);
1316 return TDM_ERROR_OUT_OF_MEMORY;
1318 /* LCOV_EXCL_STOP */
1321 private_server->private_loop = private_loop;
1322 private_loop->private_server = private_server;
1323 keep_private_server = private_server;
1325 LIST_INITHEAD(&client_list);
1327 return TDM_ERROR_NONE;
1331 tdm_server_deinit(tdm_private_loop *private_loop)
1333 tdm_server_output_info *o = NULL, *oo = NULL;
1334 tdm_server_voutput_info *vo = NULL, *voo = NULL;
1335 tdm_server_wait_info *w = NULL, *ww = NULL;
1336 tdm_server_client_info *c = NULL, *cc = NULL;
1337 tdm_private_server *private_server;
1339 if (!private_loop->private_server)
1342 private_server = private_loop->private_server;
1344 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1348 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1349 wl_resource_destroy(o->resource);
1352 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1353 wl_resource_destroy(vo->resource);
1356 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1357 wl_resource_destroy(c->resource);
1360 free(private_server);
1361 private_loop->private_server = NULL;
1362 keep_private_server = NULL;
1365 /* LCOV_EXCL_START */
1367 tdm_server_get_client_name(pid_t pid)
1369 tdm_server_client_info *c = NULL;
1371 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1373 return (const char*)c->name;
1378 /* LCOV_EXCL_STOP */
1380 /* LCOV_EXCL_START */
1382 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1384 tdm_private_server *private_server = keep_private_server;
1385 tdm_server_output_info *output_info = NULL;
1387 if (!keep_private_server)
1388 return TDM_ERROR_NONE;
1390 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1391 tdm_server_vblank_info *vblank_info = NULL;
1393 if (output && output_info->output != output)
1396 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1397 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1401 return TDM_ERROR_NONE;
1403 /* LCOV_EXCL_STOP */