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;
74 tbm_surface_h surface;
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;
96 } tdm_server_voutput_info;
98 typedef struct _tdm_server_vblank_info {
99 struct list_head link;
100 tdm_server_output_info *output_info;
101 struct wl_resource *resource;
105 } tdm_server_vblank_info;
107 typedef struct _tdm_server_wait_info {
108 struct list_head link;
109 tdm_server_vblank_info *vblank_info;
113 } tdm_server_wait_info;
115 typedef struct _tdm_server_client_info {
116 struct list_head link;
118 char name[TDM_NAME_LEN];
119 struct wl_resource *resource;
120 } tdm_server_client_info;
122 static tdm_private_server *keep_private_server;
123 static struct list_head client_list;
125 static void destroy_wait(tdm_server_wait_info *wait_info);
128 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
130 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
134 snprintf(name, size, "Unknown");
136 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
137 h = fopen(proc, "r");
141 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
143 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
146 pname[len - 1] = '\0';
148 strncpy(name, pname, size - 1);
149 name[size - 1] = '\0';
154 /* LCOV_EXCL_START */
156 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
157 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
159 tdm_server_wait_info *found;
160 tdm_server_vblank_info *vblank_info;
162 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
164 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
165 tdm_server_wait_info, link, found);
167 TDM_ERR("wait_info(%p) is destroyed", wait_info);
171 if (tdm_debug_module & TDM_DEBUG_VBLANK)
172 TDM_DBG("req_id(%d) done", wait_info->req_id);
174 vblank_info = wait_info->vblank_info;
176 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
177 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
179 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
180 sequence, tv_sec, tv_usec, error);
182 destroy_wait(wait_info);
186 /* LCOV_EXCL_START */
188 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
189 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
191 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
195 /* LCOV_EXCL_START */
197 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
198 tdm_value value, void *user_data)
200 tdm_server_output_info *output_info = user_data;
201 struct wl_client *client;
204 TDM_RETURN_IF_FAIL(output_info != NULL);
206 client = wl_resource_get_client(output_info->resource);
207 TDM_RETURN_IF_FAIL(client != NULL);
209 wl_client_get_credentials(client, &pid, NULL, NULL);
211 if (!output_info->watch_output_changes) {
212 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
216 TDM_DBG("send the output changes: %d", (unsigned int)pid);
219 case TDM_OUTPUT_CHANGE_DPMS:
220 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
222 case TDM_OUTPUT_CHANGE_CONNECTION:
223 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
232 destroy_wait(tdm_server_wait_info *wait_info)
234 LIST_DEL(&wait_info->link);
239 destroy_vblank_callback(struct wl_resource *resource)
241 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
242 tdm_server_wait_info *w = NULL, *ww = NULL;
244 TDM_RETURN_IF_FAIL(vblank_info != NULL);
246 LIST_DEL(&vblank_info->link);
248 if (vblank_info->vblank)
249 tdm_vblank_destroy(vblank_info->vblank);
251 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
252 if (w->vblank_info == vblank_info)
259 /* LCOV_EXCL_START */
261 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
263 wl_resource_destroy(resource);
267 /* LCOV_EXCL_START */
269 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
271 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
274 ret = tdm_vblank_set_name(vblank_info->vblank, name);
275 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
279 /* LCOV_EXCL_START */
281 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
283 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
286 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
287 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
291 /* LCOV_EXCL_START */
293 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
295 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
298 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
299 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
304 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
306 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
309 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
310 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
314 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
315 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
317 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
318 tdm_server_output_info *output_info = vblank_info->output_info;
319 tdm_private_server *private_server = output_info->private_server;
320 tdm_server_wait_info *wait_info;
321 unsigned int enable_fake = 0;
324 wait_info = calloc(1, sizeof * wait_info);
326 /* LCOV_EXCL_START */
328 TDM_ERR("alloc failed");
329 ret = TDM_ERROR_OUT_OF_MEMORY;
335 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
336 wait_info->vblank_info = vblank_info;
337 wait_info->req_id = req_id;
338 wait_info->req_time = TDM_TIME(req_sec, req_usec);
340 if (tdm_debug_module & TDM_DEBUG_VBLANK)
341 TDM_DBG("req_id(%d) wait", req_id);
343 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
344 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
346 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
348 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
349 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
352 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
356 /* LCOV_EXCL_START */
358 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
360 destroy_wait(wait_info);
366 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
367 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
369 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
370 tdm_server_output_info *output_info = vblank_info->output_info;
371 tdm_private_server *private_server = output_info->private_server;
372 tdm_server_wait_info *wait_info;
373 unsigned int enable_fake = 0;
376 wait_info = calloc(1, sizeof * wait_info);
378 /* LCOV_EXCL_START */
380 TDM_ERR("alloc failed");
381 ret = TDM_ERROR_OUT_OF_MEMORY;
387 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
388 wait_info->vblank_info = vblank_info;
389 wait_info->req_id = req_id;
390 wait_info->req_time = TDM_TIME(req_sec, req_usec);
392 if (tdm_debug_module & TDM_DEBUG_VBLANK)
393 TDM_DBG("req_id(%d) wait", req_id);
395 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
396 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
398 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
400 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
401 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
404 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
408 /* LCOV_EXCL_START */
410 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
412 destroy_wait(wait_info);
417 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
418 _tdm_server_vblank_cb_destroy,
419 _tdm_server_vblank_cb_set_name,
420 _tdm_server_vblank_cb_set_fps,
421 _tdm_server_vblank_cb_set_offset,
422 _tdm_server_vblank_cb_set_enable_fake,
423 _tdm_server_vblank_cb_wait_vblank,
424 _tdm_server_vblank_cb_wait_vblank_seq,
427 /* LCOV_EXCL_START */
429 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
431 wl_resource_destroy(resource);
436 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
438 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
439 tdm_private_server *private_server = output_info->private_server;
440 tdm_private_loop *private_loop = private_server->private_loop;
441 struct wl_resource *vblank_resource;
443 tdm_server_vblank_info *vblank_info;
446 wl_resource_create(client, &wl_tdm_vblank_interface,
447 wl_resource_get_version(resource), id);
448 if (!vblank_resource) {
449 /* LCOV_EXCL_START */
451 wl_resource_post_no_memory(resource);
452 TDM_ERR("wl_resource_create failed");
458 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
460 /* LCOV_EXCL_START */
462 wl_resource_post_no_memory(resource);
463 wl_resource_destroy(vblank_resource);
464 TDM_ERR("tdm_vblank_create failed");
470 vblank_info = calloc(1, sizeof * vblank_info);
472 /* LCOV_EXCL_START */
474 wl_resource_post_no_memory(resource);
475 wl_resource_destroy(vblank_resource);
476 tdm_vblank_destroy(vblank);
477 TDM_ERR("alloc failed");
483 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
484 vblank_info->output_info = output_info;
485 vblank_info->resource = vblank_resource;
486 vblank_info->vblank = vblank;
487 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
489 tdm_vblank_set_resource(vblank, vblank_resource);
491 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
492 vblank_info, destroy_vblank_callback);
494 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
496 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
497 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
498 if (output == output_info->output)
499 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
505 /* LCOV_EXCL_START */
507 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
509 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
511 TDM_RETURN_IF_FAIL(output_info != NULL);
513 output_info->watch_output_changes = enable;
518 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
520 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
521 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
524 TDM_RETURN_IF_FAIL(output_info != NULL);
526 ret = tdm_output_get_conn_status(output_info->output, &status);
527 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
529 wl_tdm_output_send_connection(output_info->resource, status, ret);
534 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
538 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
540 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
541 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
544 TDM_RETURN_IF_FAIL(output_info != NULL);
546 ret = tdm_output_get_conn_status(output_info->output, &status);
547 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
549 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
550 const tdm_output_mode *mode = NULL;
551 unsigned int hdisplay, vdisplay, vrefresh;
553 ret = tdm_output_get_mode(output_info->output, &mode);
554 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
556 hdisplay = (mode) ? mode->hdisplay : 0;
557 vdisplay = (mode) ? mode->vdisplay : 0;
558 vrefresh = (mode) ? mode->vrefresh : 0;
560 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
562 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
567 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
571 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
573 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
574 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
577 TDM_RETURN_IF_FAIL(output_info != NULL);
579 ret = tdm_output_get_conn_status(output_info->output, &status);
580 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
582 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
583 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
585 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
586 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
588 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
590 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
595 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
598 static const struct wl_tdm_output_interface tdm_output_implementation = {
599 _tdm_server_output_cb_destroy,
600 _tdm_server_output_cb_create_vblank,
601 _tdm_server_output_cb_watch_output_changes,
602 _tdm_server_output_cb_get_connection,
603 _tdm_server_output_cb_get_mode,
604 _tdm_server_output_cb_get_dpms,
608 destroy_output_callback(struct wl_resource *resource)
610 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
611 tdm_server_vblank_info *v = NULL, *vv = NULL;
613 TDM_RETURN_IF_FAIL(output_info != NULL);
615 LIST_DEL(&output_info->link);
617 tdm_output_remove_change_handler(output_info->output,
618 _tdm_server_cb_output_change, output_info);
620 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
621 wl_resource_destroy(v->resource);
628 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
629 const char *name, uint32_t id)
631 tdm_private_server *private_server = wl_resource_get_user_data(resource);
632 tdm_server_output_info *output_info;
633 struct wl_resource *output_resource = NULL;
635 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
638 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
640 /* LCOV_EXCL_START */
642 TDM_ERR("There is no '%s' output", name);
643 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
644 "There is no '%s' output", name);
651 wl_resource_create(client, &wl_tdm_output_interface,
652 wl_resource_get_version(resource), id);
653 if (!output_resource) {
654 /* LCOV_EXCL_START */
656 wl_resource_post_no_memory(resource);
657 TDM_ERR("wl_resource_create failed");
663 output_info = calloc(1, sizeof * output_info);
665 /* LCOV_EXCL_START */
667 wl_resource_post_no_memory(resource);
668 wl_resource_destroy(output_resource);
669 TDM_ERR("alloc failed");
675 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
676 if (ret != TDM_ERROR_NONE) {
677 wl_resource_post_no_memory(resource);
678 wl_resource_destroy(output_resource);
680 TDM_ERR("tdm_output_add_change_handler failed");
684 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
685 output_info->private_server = private_server;
686 output_info->resource = output_resource;
687 output_info->output = output;
688 LIST_INITHEAD(&output_info->vblank_list);
690 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
691 output_info, destroy_output_callback);
693 ret = tdm_output_get_conn_status(output, &status);
694 wl_tdm_output_send_connection(output_resource, status, ret);
696 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
697 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
698 const tdm_output_mode *mode = NULL;
699 unsigned int hdisplay, vdisplay, vrefresh;
701 ret = tdm_output_get_mode(output, &mode);
702 hdisplay = (mode) ? mode->hdisplay : 0;
703 vdisplay = (mode) ? mode->vdisplay : 0;
704 vrefresh = (mode) ? mode->vrefresh : 0;
705 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
707 ret = tdm_output_get_dpms(output, &dpms_value);
708 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
710 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
711 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
715 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
717 tdm_server_voutput_info *voutput_info;
718 tdm_private_server *private_server;
720 tdm_error ret = TDM_ERROR_NONE;
722 voutput_info = wl_resource_get_user_data(resource);
724 private_server = voutput_info->private_server;
725 output = voutput_info->output;
728 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
730 if (ret != TDM_ERROR_NONE)
731 TDM_ERR("_tdm_voutput_cb_destroy fail");
733 wl_resource_destroy(resource);
737 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
738 struct wl_resource *resource,
739 struct wl_array *modes)
741 tdm_server_voutput_info *voutput_info;
742 tdm_output_mode *mode;
743 int size, count = 0, i = 0;
745 voutput_info = wl_resource_get_user_data(resource);
747 voutput_info->available_modes.count = 0;
748 if (voutput_info->available_modes.modes)
749 free(voutput_info->available_modes.modes);
751 wl_array_for_each(mode, modes)
753 size = sizeof(tdm_output_mode);
755 voutput_info->available_modes.modes = malloc(count * size);
756 voutput_info->available_modes.count = count;
758 wl_array_for_each(mode, modes)
759 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
763 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
764 unsigned int mmwidth, unsigned int mmheight)
766 tdm_server_voutput_info *voutput_info;
768 voutput_info = wl_resource_get_user_data(resource);
770 voutput_info->mmwidth = mmwidth;
771 voutput_info->mmheight = mmheight;
775 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
777 tdm_server_voutput_info *voutput_info;
779 voutput_info = wl_resource_get_user_data(resource);
780 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
782 tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
783 tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
784 tdm_output_set_connect(voutput_info->output);
788 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
790 tdm_server_voutput_info *voutput_info;
792 voutput_info = wl_resource_get_user_data(resource);
793 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
795 /* Do free resources when it's being disconnected */
796 free(voutput_info->available_modes.modes);
797 voutput_info->available_modes.modes = NULL;
798 voutput_info->available_modes.count = 0;
799 voutput_info->mmwidth = 0;
800 voutput_info->mmheight = 0;
802 tdm_output_set_disconnect(voutput_info->output);
806 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
808 tdm_server_voutput_info *voutput_info;
810 voutput_info = wl_resource_get_user_data(resource);
811 if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED)
818 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
819 _tdm_voutput_cb_destroy,
820 _tdm_voutput_cb_set_available_modes,
821 _tdm_voutput_cb_set_physical_size,
822 _tdm_voutput_cb_connect,
823 _tdm_voutput_cb_disconnect,
824 _tdm_voutput_cb_commit_done
828 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
830 wl_resource_destroy(wl_buffer);
833 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
834 _tdm_voutput_wl_buffer_destroy
838 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
840 tdm_server_voutput_info *voutput_info = NULL;
841 tdm_server_voutput_buffer *vb = NULL;
843 voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
844 TDM_RETURN_IF_FAIL(voutput_info != NULL);
846 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
847 if (vb->wl_buffer == wl_buffer) {
848 tbm_surface_internal_unref(vb->surface);
849 wl_resource_set_user_data(wl_buffer, NULL);
856 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
858 struct wl_client *wl_client;
859 struct wl_resource *wl_buffer = NULL;
861 wl_client = wl_resource_get_client(voutput_info->resource);
863 /* create a wl_buffer resource */
864 wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
865 TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
867 wl_resource_set_implementation(wl_buffer,
868 (void (**)(void)) &_tdm_voutput_buffer_impementation,
869 voutput_info, _tdm_voutput_buffer_destory);
875 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
876 tbm_surface_h surface)
878 int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
879 struct wl_resource *wl_buffer = NULL;
880 int num_buf, is_fd = -1, i;
881 tbm_surface_info_s info;
883 struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
886 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
887 TDM_RETURN_VAL_IF_FAIL(surface != NULL, NULL);
889 if (tbm_surface_get_info(surface, &info) != TBM_SURFACE_ERROR_NONE) {
890 TDM_ERR("Failed to create buffer from surface");
894 if (info.num_planes > 3) {
895 TDM_ERR("invalid num_planes(%d)", info.num_planes);
899 num_buf = tbm_surface_internal_get_num_bos(surface);
901 TDM_ERR("surface doesn't have any bo.");
905 for (i = 0; i < num_buf; i++) {
906 tbm_bo bo = tbm_surface_internal_get_bo(surface, i);
908 TDM_ERR("Failed to get bo from surface");
912 /* try to get fd first */
913 if (is_fd == -1 || is_fd == 1) {
914 bufs[i] = tbm_bo_export_fd(bo);
915 if (is_fd == -1 && bufs[i] >= 0)
919 /* if fail to get fd, try to get name second */
920 if (is_fd == -1 || is_fd == 0) {
921 bufs[i] = tbm_bo_export(bo);
922 if (is_fd == -1 && bufs[i] > 0)
927 (is_fd == 1 && bufs[i] < 0) ||
928 (is_fd == 0 && bufs[i] <= 0)) {
929 TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
934 wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
936 TDM_ERR("Failed to create wl_buffer");
940 wl_array_init(&plane_buf_idx);
941 wl_array_init(&plane_offset);
942 wl_array_init(&plane_stride);
943 wl_array_init(&plane_size);
945 for (i = 0; i < 3; i++) {
946 p = wl_array_add(&plane_buf_idx, sizeof(int));
947 *p = tbm_surface_internal_get_plane_bo_idx(surface, i);
948 p = wl_array_add(&plane_offset, sizeof(int));
949 *p = info.planes[i].offset;
950 p = wl_array_add(&plane_stride, sizeof(int));
951 *p = info.planes[i].stride;
952 p = wl_array_add(&plane_size, sizeof(int));
953 *p = info.planes[i].size;
957 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
959 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
960 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
961 flags, num_buf, bufs[0],
962 (bufs[1] == -1) ? bufs[0] : bufs[1],
963 (bufs[2] == -1) ? bufs[0] : bufs[2]);
965 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
967 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
968 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
970 num_buf, bufs[0], bufs[1], bufs[2]);
972 wl_array_release(&plane_buf_idx);
973 wl_array_release(&plane_offset);
974 wl_array_release(&plane_stride);
975 wl_array_release(&plane_size);
977 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
978 if (is_fd == 1 && (bufs[i] >= 0))
985 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
986 if (is_fd == 1 && (bufs[i] >= 0))
993 tdm_server_voutput_buffer *
994 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h surface)
996 tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
998 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
999 if (vb && vb->surface == surface)
1003 tbm_surface_internal_ref(surface);
1004 voutput_buffer = calloc(1, sizeof *voutput_buffer);
1005 if (!voutput_buffer) {
1006 /* LCOV_EXCL_START */
1008 TDM_ERR("fail calloc");
1009 tbm_surface_internal_unref(surface);
1012 /* LCOV_EXCL_STOP */
1015 voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, surface);
1016 if (!voutput_buffer->wl_buffer) {
1017 /* LCOV_EXCL_START */
1019 TDM_ERR("fail export buffer");
1020 free(voutput_buffer);
1021 tbm_surface_internal_unref(surface);
1024 /* LCOV_EXCL_STOP */
1027 voutput_buffer->surface = surface;
1028 LIST_ADDTAIL(&voutput_info->link, &voutput_info->buffer_list);
1030 return voutput_buffer;
1034 tdm_output_send_buffer(tdm_output *output, tbm_surface_h surface)
1036 tdm_private_server *private_server = keep_private_server;
1037 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1038 tdm_server_voutput_buffer *voutput_buffer = NULL;
1040 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1042 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1043 if (vo && vo->output == output) {
1048 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1050 voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, surface);
1051 TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1053 voutput_info->attach_buffer = voutput_buffer;
1055 return TDM_ERROR_NONE;
1059 tdm_output_commit_buffer(tdm_output *output)
1061 tdm_private_server *private_server = keep_private_server;
1062 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1064 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1066 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1067 if (vo && vo->output == output) {
1072 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1073 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
1075 wl_tdm_voutput_send_commit(voutput_info->resource);
1077 return TDM_ERROR_NONE;
1081 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1083 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1084 tdm_server_voutput_buffer *vb, *vbb;
1085 TDM_RETURN_IF_FAIL(voutput_info != NULL);
1087 LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
1090 LIST_DEL(&vb->link);
1093 wl_resource_destroy(vb->wl_buffer);
1096 LIST_DEL(&voutput_info->link);
1098 /* Do free your own resource */
1103 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1105 struct wl_resource *voutput_resource = NULL;
1106 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1107 tdm_server_voutput_info *voutput_info;
1111 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1113 TDM_ERR("There is '%s' output, cannot create.", name);
1114 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1115 "There is '%s' output", name);
1119 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
1121 TDM_ERR("output creation fail(%s)(%d).", name, ret);
1122 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1123 "%s output creation fail", name);
1128 wl_resource_create(client, &wl_tdm_voutput_interface,
1129 wl_resource_get_version(resource), id);
1130 if (!voutput_resource) {
1131 /* LCOV_EXCL_START */
1133 wl_resource_post_no_memory(resource);
1134 TDM_ERR("wl_resource_create failed");
1137 /* LCOV_EXCL_STOP */
1140 voutput_info = calloc(1, sizeof * voutput_info);
1141 if (!voutput_info) {
1142 /* LCOV_EXCL_START */
1144 wl_resource_post_no_memory(resource);
1145 wl_resource_destroy(voutput_resource);
1146 TDM_ERR("alloc failed");
1149 /* LCOV_EXCL_STOP */
1152 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1153 voutput_info->private_server = private_server;
1154 voutput_info->resource = voutput_resource;
1155 voutput_info->output = output;
1156 LIST_INITHEAD(&voutput_info->output_list);
1157 LIST_INITHEAD(&voutput_info->buffer_list);
1159 wl_resource_set_implementation(voutput_resource,
1160 &tdm_voutput_implementation,
1162 tdm_voutput_cb_resource_destroy);
1164 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1167 /* LCOV_EXCL_START */
1169 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1171 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1172 tdm_private_loop *private_loop = private_server->private_loop;
1173 char message[TDM_SERVER_REPLY_MSG_LEN];
1175 int len = sizeof(message), size;
1178 wl_client_get_credentials(client, NULL, &uid, NULL);
1181 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1182 TDM_ERR("%s", message);
1184 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1187 size = sizeof(message) - len;
1190 wl_client_flush(client);
1193 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1194 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1196 strncpy(buffer, m, copylen);
1200 buffer[copylen] = '\0';
1202 wl_tdm_send_debug_message(resource, buffer);
1205 wl_tdm_send_debug_done(resource);
1207 /* LCOV_EXCL_STOP */
1209 static const struct wl_tdm_interface tdm_implementation = {
1210 _tdm_server_cb_debug,
1211 _tdm_server_cb_create_output,
1212 _tdm_server_cb_create_virtual_output
1216 destroy_client(struct wl_resource *resource)
1218 tdm_server_client_info *c = NULL, *cc = NULL;
1219 struct wl_client *client;
1221 client = wl_resource_get_client(resource);
1222 TDM_RETURN_IF_FAIL(client != NULL);
1224 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1225 if (c->resource == resource) {
1234 _tdm_server_bind(struct wl_client *client, void *data,
1235 uint32_t version, uint32_t id)
1237 struct wl_resource *resource;
1238 tdm_server_client_info *cinfo;
1240 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1242 /* LCOV_EXCL_START */
1244 wl_client_post_no_memory(client);
1247 /* LCOV_EXCL_STOP */
1250 cinfo = calloc(1, sizeof(tdm_server_client_info));
1252 /* LCOV_EXCL_START */
1254 wl_client_post_no_memory(client);
1255 wl_resource_destroy(resource);
1258 /* LCOV_EXCL_STOP */
1261 cinfo->resource = resource;
1263 LIST_ADDTAIL(&cinfo->link, &client_list);
1264 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1265 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1267 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1271 tdm_server_init(tdm_private_loop *private_loop)
1273 tdm_private_server *private_server;
1275 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1276 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1278 if (private_loop->private_server)
1279 return TDM_ERROR_NONE;
1281 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1282 /* LCOV_EXCL_START */
1284 TDM_ERR("createing a tdm-socket failed");
1285 return TDM_ERROR_OPERATION_FAILED;
1287 /* LCOV_EXCL_STOP */
1290 private_server = calloc(1, sizeof * private_server);
1291 if (!private_server) {
1292 TDM_ERR("alloc failed");
1293 return TDM_ERROR_OUT_OF_MEMORY;
1296 LIST_INITHEAD(&private_server->output_list);
1297 LIST_INITHEAD(&private_server->voutput_list);
1298 LIST_INITHEAD(&private_server->wait_list);
1300 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1301 private_server, _tdm_server_bind)) {
1302 /* LCOV_EXCL_START */
1304 TDM_ERR("creating a global resource failed");
1305 free(private_server);
1306 return TDM_ERROR_OUT_OF_MEMORY;
1308 /* LCOV_EXCL_STOP */
1311 private_server->private_loop = private_loop;
1312 private_loop->private_server = private_server;
1313 keep_private_server = private_server;
1315 LIST_INITHEAD(&client_list);
1317 return TDM_ERROR_NONE;
1321 tdm_server_deinit(tdm_private_loop *private_loop)
1323 tdm_server_output_info *o = NULL, *oo = NULL;
1324 tdm_server_voutput_info *vo = NULL, *voo = NULL;
1325 tdm_server_wait_info *w = NULL, *ww = NULL;
1326 tdm_server_client_info *c = NULL, *cc = NULL;
1327 tdm_private_server *private_server;
1329 if (!private_loop->private_server)
1332 private_server = private_loop->private_server;
1334 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1338 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1339 wl_resource_destroy(o->resource);
1342 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1343 wl_resource_destroy(vo->resource);
1346 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1347 wl_resource_destroy(c->resource);
1350 free(private_server);
1351 private_loop->private_server = NULL;
1352 keep_private_server = NULL;
1355 /* LCOV_EXCL_START */
1357 tdm_server_get_client_name(pid_t pid)
1359 tdm_server_client_info *c = NULL;
1361 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1363 return (const char*)c->name;
1368 /* LCOV_EXCL_STOP */
1370 /* LCOV_EXCL_START */
1372 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1374 tdm_private_server *private_server = keep_private_server;
1375 tdm_server_output_info *output_info = NULL;
1377 if (!keep_private_server)
1378 return TDM_ERROR_NONE;
1380 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1381 tdm_server_vblank_info *vblank_info = NULL;
1383 if (output && output_info->output != output)
1386 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1387 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1391 return TDM_ERROR_NONE;
1393 /* LCOV_EXCL_STOP */