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, type:%d, value:%d", (unsigned int)pid, type, value.u32);
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);
600 _tdm_server_output_cb_get_available_modes(struct wl_client *client, struct wl_resource *resource)
602 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
603 const tdm_output_mode *modes;
604 tdm_output_mode *mode;
605 struct wl_array array;
606 int i, size, count = 0;
609 ret = tdm_output_get_available_modes(output_info->output, &modes, &count);
610 if ((ret != TDM_ERROR_NONE) || (count == 0)) {
611 wl_tdm_output_send_available_modes(output_info->resource, NULL, ret);
615 size = sizeof(tdm_output_mode);
616 wl_array_init(&array);
617 for (i = 0; i < count; i++) {
618 mode = wl_array_add(&array, size);
619 memcpy(mode, &modes[i], size);
622 wl_tdm_output_send_available_modes(output_info->resource, &array, ret);
624 wl_array_release(&array);
628 _tdm_server_output_cb_set_mode(struct wl_client *client, struct wl_resource *resource, unsigned int index)
630 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
631 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
632 const tdm_output_mode *modes;
636 TDM_RETURN_IF_FAIL(output_info != NULL);
638 ret = tdm_output_get_conn_status(output_info->output, &status);
639 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
640 TDM_RETURN_IF_FAIL(status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED);
642 ret = tdm_output_get_available_modes(output_info->output, &modes, &count);
643 TDM_RETURN_IF_FAIL(index < count);
645 TDM_DBG("mode set request to index:%d", index);
647 tdm_output_call_mode_set_request(output_info->output, index);
650 static const struct wl_tdm_output_interface tdm_output_implementation = {
651 _tdm_server_output_cb_destroy,
652 _tdm_server_output_cb_create_vblank,
653 _tdm_server_output_cb_watch_output_changes,
654 _tdm_server_output_cb_get_connection,
655 _tdm_server_output_cb_get_mode,
656 _tdm_server_output_cb_get_dpms,
657 _tdm_server_output_cb_get_available_modes,
658 _tdm_server_output_cb_set_mode,
662 destroy_output_callback(struct wl_resource *resource)
664 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
665 tdm_server_vblank_info *v = NULL, *vv = NULL;
667 TDM_RETURN_IF_FAIL(output_info != NULL);
669 LIST_DEL(&output_info->link);
671 tdm_output_remove_change_handler(output_info->output,
672 _tdm_server_cb_output_change, output_info);
674 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
675 wl_resource_destroy(v->resource);
682 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
683 const char *name, uint32_t id)
685 tdm_private_server *private_server = wl_resource_get_user_data(resource);
686 tdm_server_output_info *output_info;
687 struct wl_resource *output_resource = NULL;
689 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
692 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
694 /* LCOV_EXCL_START */
696 TDM_ERR("There is no '%s' output", name);
697 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
698 "There is no '%s' output", name);
705 wl_resource_create(client, &wl_tdm_output_interface,
706 wl_resource_get_version(resource), id);
707 if (!output_resource) {
708 /* LCOV_EXCL_START */
710 wl_resource_post_no_memory(resource);
711 TDM_ERR("wl_resource_create failed");
717 output_info = calloc(1, sizeof * output_info);
719 /* LCOV_EXCL_START */
721 wl_resource_post_no_memory(resource);
722 wl_resource_destroy(output_resource);
723 TDM_ERR("alloc failed");
729 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
730 if (ret != TDM_ERROR_NONE) {
731 wl_resource_post_no_memory(resource);
732 wl_resource_destroy(output_resource);
734 TDM_ERR("tdm_output_add_change_handler failed");
738 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
739 output_info->private_server = private_server;
740 output_info->resource = output_resource;
741 output_info->output = output;
742 LIST_INITHEAD(&output_info->vblank_list);
744 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
745 output_info, destroy_output_callback);
747 ret = tdm_output_get_conn_status(output, &status);
748 wl_tdm_output_send_connection(output_resource, status, ret);
750 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
751 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
752 const tdm_output_mode *mode = NULL;
753 unsigned int hdisplay, vdisplay, vrefresh;
755 ret = tdm_output_get_mode(output, &mode);
756 hdisplay = (mode) ? mode->hdisplay : 0;
757 vdisplay = (mode) ? mode->vdisplay : 0;
758 vrefresh = (mode) ? mode->vrefresh : 0;
759 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
761 ret = tdm_output_get_dpms(output, &dpms_value);
762 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
764 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
765 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
769 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
771 wl_resource_destroy(resource);
775 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
776 struct wl_resource *resource,
777 struct wl_array *modes)
779 tdm_server_voutput_info *voutput_info;
780 tdm_output_mode *mode;
781 int size, count = 0, i = 0;
783 voutput_info = wl_resource_get_user_data(resource);
785 voutput_info->available_modes.count = 0;
786 if (voutput_info->available_modes.modes)
787 free(voutput_info->available_modes.modes);
789 wl_array_for_each(mode, modes)
791 size = sizeof(tdm_output_mode);
793 voutput_info->available_modes.modes = malloc(count * size);
794 voutput_info->available_modes.count = count;
796 wl_array_for_each(mode, modes)
797 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
801 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
802 unsigned int mmwidth, unsigned int mmheight)
804 tdm_server_voutput_info *voutput_info;
806 voutput_info = wl_resource_get_user_data(resource);
808 voutput_info->mmwidth = mmwidth;
809 voutput_info->mmheight = mmheight;
813 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
815 tdm_server_voutput_info *voutput_info;
817 voutput_info = wl_resource_get_user_data(resource);
818 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
820 tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
821 tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
822 tdm_output_set_connect(voutput_info->output);
826 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
828 tdm_server_voutput_info *voutput_info;
830 voutput_info = wl_resource_get_user_data(resource);
831 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
833 /* Do free resources when it's being disconnected */
834 free(voutput_info->available_modes.modes);
835 voutput_info->available_modes.modes = NULL;
836 voutput_info->available_modes.count = 0;
837 voutput_info->mmwidth = 0;
838 voutput_info->mmheight = 0;
840 if (voutput_info->attach_buffer) {
841 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
842 tbm_surface_internal_unref(buffer);
843 voutput_info->committing = 0;
844 voutput_info->attach_buffer = NULL;
847 tdm_output_set_disconnect(voutput_info->output);
851 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
853 tdm_server_voutput_info *voutput_info;
854 tbm_surface_h buffer;
856 voutput_info = wl_resource_get_user_data(resource);
857 if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED) {
858 TDM_DBG("not connected.");
862 buffer = voutput_info->attach_buffer->buffer;
863 tbm_surface_internal_unref(buffer);
864 voutput_info->committing = 0;
865 voutput_info->attach_buffer = NULL;
867 tdm_output_commit_done(voutput_info->output, buffer);
870 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
871 _tdm_voutput_cb_destroy,
872 _tdm_voutput_cb_set_available_modes,
873 _tdm_voutput_cb_set_physical_size,
874 _tdm_voutput_cb_connect,
875 _tdm_voutput_cb_disconnect,
876 _tdm_voutput_cb_commit_done
880 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
882 wl_resource_destroy(wl_buffer);
885 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
886 _tdm_voutput_wl_buffer_destroy
890 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
892 tdm_server_voutput_info *voutput_info = NULL;
893 tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
895 voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
896 TDM_RETURN_IF_FAIL(voutput_info != NULL);
898 LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
899 if (vb->wl_buffer == wl_buffer) {
900 tbm_surface_internal_unref(vb->buffer);
901 wl_resource_set_user_data(wl_buffer, NULL);
909 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
911 struct wl_client *wl_client;
912 struct wl_resource *wl_buffer = NULL;
914 wl_client = wl_resource_get_client(voutput_info->resource);
916 /* create a wl_buffer resource */
917 wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
918 TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
920 wl_resource_set_implementation(wl_buffer,
921 (void (**)(void)) &_tdm_voutput_buffer_impementation,
922 voutput_info, _tdm_voutput_buffer_destory);
928 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
929 tbm_surface_h buffer)
931 int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
932 struct wl_resource *wl_buffer = NULL;
933 int num_buf, is_fd = -1, i;
934 tbm_surface_info_s info;
936 struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
939 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
940 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
942 if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
943 TDM_ERR("Failed to create buffer from surface");
947 if (info.num_planes > 3) {
948 TDM_ERR("invalid num_planes(%d)", info.num_planes);
952 num_buf = tbm_surface_internal_get_num_bos(buffer);
954 TDM_ERR("surface doesn't have any bo.");
958 for (i = 0; i < num_buf; i++) {
959 tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
961 TDM_ERR("Failed to get bo from surface");
965 /* try to get fd first */
966 if (is_fd == -1 || is_fd == 1) {
967 bufs[i] = tbm_bo_export_fd(bo);
968 if (is_fd == -1 && bufs[i] >= 0)
972 /* if fail to get fd, try to get name second */
973 if (is_fd == -1 || is_fd == 0) {
974 bufs[i] = tbm_bo_export(bo);
975 if (is_fd == -1 && bufs[i] > 0)
980 (is_fd == 1 && bufs[i] < 0) ||
981 (is_fd == 0 && bufs[i] <= 0)) {
982 TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
987 wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
989 TDM_ERR("Failed to create wl_buffer");
993 wl_array_init(&plane_buf_idx);
994 wl_array_init(&plane_offset);
995 wl_array_init(&plane_stride);
996 wl_array_init(&plane_size);
998 for (i = 0; i < 3; i++) {
999 p = wl_array_add(&plane_buf_idx, sizeof(int));
1000 *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
1001 p = wl_array_add(&plane_offset, sizeof(int));
1002 *p = info.planes[i].offset;
1003 p = wl_array_add(&plane_stride, sizeof(int));
1004 *p = info.planes[i].stride;
1005 p = wl_array_add(&plane_size, sizeof(int));
1006 *p = info.planes[i].size;
1010 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
1012 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1013 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1014 flags, num_buf, bufs[0],
1015 (bufs[1] == -1) ? bufs[0] : bufs[1],
1016 (bufs[2] == -1) ? bufs[0] : bufs[2]);
1018 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
1020 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1021 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1023 num_buf, bufs[0], bufs[1], bufs[2]);
1025 wl_array_release(&plane_buf_idx);
1026 wl_array_release(&plane_offset);
1027 wl_array_release(&plane_stride);
1028 wl_array_release(&plane_size);
1030 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1031 if (is_fd == 1 && (bufs[i] >= 0))
1038 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1039 if (is_fd == 1 && (bufs[i] >= 0))
1046 tdm_server_voutput_buffer *
1047 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
1049 tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
1051 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1052 if (vb && vb->buffer == buffer) {
1053 tbm_surface_internal_ref(vb->buffer);
1058 tbm_surface_internal_ref(buffer);
1059 voutput_buffer = calloc(1, sizeof *voutput_buffer);
1060 if (!voutput_buffer) {
1061 /* LCOV_EXCL_START */
1063 TDM_ERR("fail calloc");
1064 tbm_surface_internal_unref(buffer);
1067 /* LCOV_EXCL_STOP */
1070 voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, buffer);
1071 if (!voutput_buffer->wl_buffer) {
1072 /* LCOV_EXCL_START */
1074 TDM_ERR("fail export buffer");
1075 free(voutput_buffer);
1076 tbm_surface_internal_unref(buffer);
1079 /* LCOV_EXCL_STOP */
1082 voutput_buffer->buffer = buffer;
1083 LIST_ADDTAIL(&voutput_buffer->link, &voutput_info->buffer_list);
1085 return voutput_buffer;
1089 tdm_output_attach_buffer(tdm_output *output, tbm_surface_h buffer)
1091 tdm_private_server *private_server = keep_private_server;
1092 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1093 tdm_server_voutput_buffer *voutput_buffer = NULL;
1095 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1097 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1098 if (vo && vo->output == output) {
1103 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1104 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer == NULL, TDM_ERROR_OPERATION_FAILED);
1106 voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer);
1107 TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1109 voutput_info->attach_buffer = voutput_buffer;
1111 wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
1113 return TDM_ERROR_NONE;
1117 tdm_output_commit_buffer(tdm_output *output)
1119 tdm_private_server *private_server = keep_private_server;
1120 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1122 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1124 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1125 if (vo && vo->output == output) {
1130 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1131 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
1132 TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
1134 tbm_surface_internal_ref(voutput_info->attach_buffer->buffer);
1135 voutput_info->committing = 1;
1137 wl_tdm_voutput_send_commit(voutput_info->resource);
1139 return TDM_ERROR_NONE;
1143 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1145 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1146 tdm_server_voutput_buffer *vb;
1147 tdm_private_server *private_server;
1149 tdm_error ret = TDM_ERROR_NONE;
1151 TDM_RETURN_IF_FAIL(voutput_info != NULL);
1153 private_server = voutput_info->private_server;
1154 output = voutput_info->output;
1157 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
1158 if (ret != TDM_ERROR_NONE)
1159 TDM_ERR("_tdm_voutput_cb_destroy fail");
1161 if (voutput_info->attach_buffer) {
1162 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
1163 tbm_surface_internal_unref(buffer);
1164 voutput_info->committing = 0;
1165 voutput_info->attach_buffer = NULL;
1168 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1172 wl_resource_destroy(vb->wl_buffer);
1175 LIST_DEL(&voutput_info->link);
1177 /* Do free your own resource */
1182 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1184 struct wl_resource *voutput_resource = NULL;
1185 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1186 tdm_server_voutput_info *voutput_info;
1190 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1192 TDM_ERR("There is '%s' output, cannot create.", name);
1193 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1194 "There is '%s' output", name);
1198 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
1200 TDM_ERR("output creation fail(%s)(%d).", name, ret);
1201 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1202 "%s output creation fail", name);
1207 wl_resource_create(client, &wl_tdm_voutput_interface,
1208 wl_resource_get_version(resource), id);
1209 if (!voutput_resource) {
1210 /* LCOV_EXCL_START */
1212 wl_resource_post_no_memory(resource);
1213 TDM_ERR("wl_resource_create failed");
1216 /* LCOV_EXCL_STOP */
1219 voutput_info = calloc(1, sizeof * voutput_info);
1220 if (!voutput_info) {
1221 /* LCOV_EXCL_START */
1223 wl_resource_post_no_memory(resource);
1224 wl_resource_destroy(voutput_resource);
1225 TDM_ERR("alloc failed");
1228 /* LCOV_EXCL_STOP */
1231 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1232 voutput_info->private_server = private_server;
1233 voutput_info->resource = voutput_resource;
1234 voutput_info->output = output;
1235 LIST_INITHEAD(&voutput_info->output_list);
1236 LIST_INITHEAD(&voutput_info->buffer_list);
1238 wl_resource_set_implementation(voutput_resource,
1239 &tdm_voutput_implementation,
1241 tdm_voutput_cb_resource_destroy);
1243 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1246 /* LCOV_EXCL_START */
1248 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1250 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1251 tdm_private_loop *private_loop = private_server->private_loop;
1252 char message[TDM_SERVER_REPLY_MSG_LEN];
1254 int len = sizeof(message), size;
1257 wl_client_get_credentials(client, NULL, &uid, NULL);
1260 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1261 TDM_ERR("%s", message);
1263 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1266 size = sizeof(message) - len;
1269 wl_client_flush(client);
1272 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1273 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1275 strncpy(buffer, m, copylen);
1279 buffer[copylen] = '\0';
1281 wl_tdm_send_debug_message(resource, buffer);
1284 wl_tdm_send_debug_done(resource);
1286 /* LCOV_EXCL_STOP */
1288 static const struct wl_tdm_interface tdm_implementation = {
1289 _tdm_server_cb_debug,
1290 _tdm_server_cb_create_output,
1291 _tdm_server_cb_create_virtual_output
1295 destroy_client(struct wl_resource *resource)
1297 tdm_server_client_info *c = NULL, *cc = NULL;
1298 struct wl_client *client;
1300 client = wl_resource_get_client(resource);
1301 TDM_RETURN_IF_FAIL(client != NULL);
1303 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1304 if (c->resource == resource) {
1313 _tdm_server_bind(struct wl_client *client, void *data,
1314 uint32_t version, uint32_t id)
1316 struct wl_resource *resource;
1317 tdm_server_client_info *cinfo;
1319 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1321 /* LCOV_EXCL_START */
1323 wl_client_post_no_memory(client);
1326 /* LCOV_EXCL_STOP */
1329 cinfo = calloc(1, sizeof(tdm_server_client_info));
1331 /* LCOV_EXCL_START */
1333 wl_client_post_no_memory(client);
1334 wl_resource_destroy(resource);
1337 /* LCOV_EXCL_STOP */
1340 cinfo->resource = resource;
1342 LIST_ADDTAIL(&cinfo->link, &client_list);
1343 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1344 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1346 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1350 tdm_server_init(tdm_private_loop *private_loop)
1352 tdm_private_server *private_server;
1354 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1355 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1357 if (private_loop->private_server)
1358 return TDM_ERROR_NONE;
1360 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1361 /* LCOV_EXCL_START */
1363 TDM_ERR("createing a tdm-socket failed");
1364 return TDM_ERROR_OPERATION_FAILED;
1366 /* LCOV_EXCL_STOP */
1369 private_server = calloc(1, sizeof * private_server);
1370 if (!private_server) {
1371 TDM_ERR("alloc failed");
1372 return TDM_ERROR_OUT_OF_MEMORY;
1375 LIST_INITHEAD(&private_server->output_list);
1376 LIST_INITHEAD(&private_server->voutput_list);
1377 LIST_INITHEAD(&private_server->wait_list);
1379 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1380 private_server, _tdm_server_bind)) {
1381 /* LCOV_EXCL_START */
1383 TDM_ERR("creating a global resource failed");
1384 free(private_server);
1385 return TDM_ERROR_OUT_OF_MEMORY;
1387 /* LCOV_EXCL_STOP */
1390 private_server->private_loop = private_loop;
1391 private_loop->private_server = private_server;
1392 keep_private_server = private_server;
1394 LIST_INITHEAD(&client_list);
1396 return TDM_ERROR_NONE;
1400 tdm_server_deinit(tdm_private_loop *private_loop)
1402 tdm_server_output_info *o = NULL, *oo = NULL;
1403 tdm_server_voutput_info *vo = NULL, *voo = NULL;
1404 tdm_server_wait_info *w = NULL, *ww = NULL;
1405 tdm_server_client_info *c = NULL, *cc = NULL;
1406 tdm_private_server *private_server;
1408 if (!private_loop->private_server)
1411 private_server = private_loop->private_server;
1413 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1417 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1418 wl_resource_destroy(o->resource);
1421 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1422 wl_resource_destroy(vo->resource);
1425 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1426 wl_resource_destroy(c->resource);
1429 free(private_server);
1430 private_loop->private_server = NULL;
1431 keep_private_server = NULL;
1434 /* LCOV_EXCL_START */
1436 tdm_server_get_client_name(pid_t pid)
1438 tdm_server_client_info *c = NULL;
1440 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1442 return (const char*)c->name;
1447 /* LCOV_EXCL_STOP */
1449 /* LCOV_EXCL_START */
1451 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1453 tdm_private_server *private_server = keep_private_server;
1454 tdm_server_output_info *output_info = NULL;
1456 if (!keep_private_server)
1457 return TDM_ERROR_NONE;
1459 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1460 tdm_server_vblank_info *vblank_info = NULL;
1462 if (output && output_info->output != output)
1465 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1466 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1470 return TDM_ERROR_NONE;
1472 /* LCOV_EXCL_STOP */