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;
76 } tdm_server_voutput_buffer;
78 typedef struct _tdm_server_voutput_info {
79 struct list_head link;
80 tdm_private_server *private_server;
81 struct wl_resource *resource;
85 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 unsigned int request_commit;
98 } tdm_server_voutput_info;
100 typedef struct _tdm_server_vblank_info {
101 struct list_head link;
102 tdm_server_output_info *output_info;
103 struct wl_resource *resource;
109 tdm_event_loop_source *vblank_timeout_timer;
110 } tdm_server_vblank_info;
112 typedef struct _tdm_server_wait_info {
113 struct list_head link;
114 tdm_server_vblank_info *vblank_info;
119 unsigned int timeout;
120 } tdm_server_wait_info;
122 typedef struct _tdm_server_client_info {
123 struct list_head link;
125 char name[TDM_NAME_LEN];
126 struct wl_resource *resource;
127 } tdm_server_client_info;
130 VBLANK_WAIT_TYPE_INTERVAL,
131 VBLANK_WAIT_TYPE_SEQUENCE,
132 } tdm_server_vblank_wait_type;
134 static tdm_private_server *keep_private_server;
135 static struct list_head client_list;
137 static void destroy_wait(tdm_server_wait_info *wait_info);
138 static void _tdm_server_vblank_timeout_update(tdm_server_vblank_info *vblank_info, int ms_delay);
141 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
143 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
147 snprintf(name, size, "Unknown");
149 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
150 h = fopen(proc, "r");
154 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
156 strncpy(pname, "NO NAME", sizeof(pname));
157 pname[sizeof(pname) - 1] = '\0';
160 strncpy(name, pname, size);
161 name[size - 1] = '\0';
166 /* LCOV_EXCL_START */
168 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
169 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
171 tdm_server_wait_info *found;
172 tdm_server_vblank_info *vblank_info;
173 tdm_server_output_info *output_info;
174 tdm_private_server *private_server;
175 tdm_private_loop *private_loop;
177 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
179 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
180 tdm_server_wait_info, link, found);
182 TDM_ERR("wait_info(%p) is destroyed", wait_info);
186 if (tdm_debug_module & TDM_DEBUG_VBLANK)
187 TDM_DBG("req_id(%d) done", wait_info->req_id);
189 vblank_info = wait_info->vblank_info;
190 output_info = vblank_info->output_info;
191 private_server = output_info->private_server;
192 private_loop = private_server->private_loop;
194 tdm_display_lock(private_loop->dpy);
195 _tdm_server_vblank_timeout_update(vblank_info, 0);
196 tdm_display_unlock(private_loop->dpy);
198 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
199 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
201 if (!wait_info->timeout)
202 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
203 sequence, tv_sec, tv_usec, error);
205 destroy_wait(wait_info);
209 /* LCOV_EXCL_START */
211 _tdm_server_timeout_timer_cb(void *user_data)
213 tdm_server_vblank_info *vblank_info = user_data;
214 tdm_server_wait_info *wait_info = NULL;
217 unsigned int tv_usec;
219 TDM_RETURN_VAL_IF_FAIL(vblank_info != NULL, TDM_ERROR_OPERATION_FAILED);
220 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
222 curr = tdm_helper_get_time();
223 tv_sec = TDM_TIME_SEC(curr);
224 tv_usec = TDM_TIME_USEC(curr);
226 LIST_FOR_EACH_ENTRY(wait_info, &keep_private_server->wait_list, link) {
227 if (wait_info->timeout) continue;
228 if (vblank_info != wait_info->vblank_info) continue;
230 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
231 0, tv_sec, tv_usec, TDM_ERROR_TIMEOUT);
232 TDM_ERR("tdm_server_vblank(%p) req_id(%d) timeout force send vblank", vblank_info, wait_info->req_id);
233 wait_info->timeout = 1;
236 return TDM_ERROR_NONE;
240 _tdm_server_vblank_timeout_update(tdm_server_vblank_info *vblank_info, int ms_delay)
242 tdm_server_output_info *output_info = vblank_info->output_info;
243 tdm_private_server *private_server = output_info->private_server;
244 tdm_private_loop *private_loop = private_server->private_loop;
247 if (!vblank_info->vblank_timeout_timer) {
248 vblank_info->vblank_timeout_timer =
249 tdm_event_loop_add_timer_handler(private_loop->dpy,
250 _tdm_server_timeout_timer_cb,
253 if (!vblank_info->vblank_timeout_timer) {
254 TDM_ERR("tdm_server_vblank(%p) couldn't add timer", vblank_info);
258 if (tdm_debug_module & TDM_DEBUG_VBLANK)
259 TDM_INFO("tdm_server_vblank(%p) create vblank timeout timer", vblank_info);
262 ret = tdm_event_loop_source_timer_update(vblank_info->vblank_timeout_timer, ms_delay);
263 if (ret != TDM_ERROR_NONE) {
264 TDM_ERR("tdm_server_vblank(%p) couldn't update timer", vblank_info);
270 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
271 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
273 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
277 /* LCOV_EXCL_START */
279 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
280 tdm_value value, void *user_data)
282 tdm_server_output_info *output_info = user_data;
283 struct wl_client *client;
285 tdm_output_conn_status status;
286 tdm_error ret = TDM_ERROR_NONE;
288 TDM_RETURN_IF_FAIL(output_info != NULL);
290 client = wl_resource_get_client(output_info->resource);
291 TDM_RETURN_IF_FAIL(client != NULL);
293 wl_client_get_credentials(client, &pid, NULL, NULL);
295 if (!output_info->watch_output_changes) {
296 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
300 TDM_DBG("send the output changes: %d, type:%d, value:%d", (unsigned int)pid, type, value.u32);
303 case TDM_OUTPUT_CHANGE_DPMS:
304 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
306 case TDM_OUTPUT_CHANGE_CONNECTION:
308 if (status == TDM_OUTPUT_CONN_STATUS_MODE_SETTED) {
309 const tdm_output_mode *mode = NULL;
310 unsigned int hdisplay, vdisplay, vrefresh;
312 ret = tdm_output_get_mode(output_info->output, &mode);
313 if (ret == TDM_ERROR_NONE) {
314 hdisplay = (mode) ? mode->hdisplay : 0;
315 vdisplay = (mode) ? mode->vdisplay : 0;
316 vrefresh = (mode) ? mode->vrefresh : 0;
318 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
320 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
323 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
332 destroy_wait(tdm_server_wait_info *wait_info)
334 LIST_DEL(&wait_info->link);
339 destroy_vblank_callback(struct wl_resource *resource)
341 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
342 tdm_server_output_info *output_info = vblank_info->output_info;
343 tdm_private_server *private_server = output_info->private_server;
344 tdm_private_loop *private_loop = private_server->private_loop;
345 tdm_server_wait_info *w = NULL, *ww = NULL;
347 TDM_RETURN_IF_FAIL(vblank_info != NULL);
349 if (vblank_info->vblank_timeout_timer) {
350 tdm_display_lock(private_loop->dpy);
351 tdm_event_loop_source_remove(vblank_info->vblank_timeout_timer);
352 tdm_display_unlock(private_loop->dpy);
355 LIST_DEL(&vblank_info->link);
357 if (vblank_info->vblank)
358 tdm_vblank_destroy(vblank_info->vblank);
360 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
361 if (w->vblank_info == vblank_info)
368 /* LCOV_EXCL_START */
370 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
372 wl_resource_destroy(resource);
376 /* LCOV_EXCL_START */
378 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
380 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
383 ret = tdm_vblank_set_name(vblank_info->vblank, name);
384 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
388 /* LCOV_EXCL_START */
390 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
392 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
395 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
396 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
400 /* LCOV_EXCL_START */
402 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
404 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
407 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
408 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
413 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
415 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
418 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
419 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
423 _tdm_server_vblank_wait_vblank(tdm_server_vblank_info *vblank_info, tdm_server_vblank_wait_type wait_type,
424 uint32_t wait_value, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
426 tdm_server_output_info *output_info = vblank_info->output_info;
427 tdm_private_server *private_server = output_info->private_server;
428 tdm_private_loop *private_loop = private_server->private_loop;
429 tdm_server_wait_info *wait_info;
430 unsigned int enable_fake = 0;
433 wait_info = calloc(1, sizeof * wait_info);
435 /* LCOV_EXCL_START */
437 TDM_ERR("alloc failed");
438 ret = TDM_ERROR_OUT_OF_MEMORY;
444 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
445 wait_info->vblank_info = vblank_info;
446 wait_info->req_id = req_id;
447 wait_info->req_time = TDM_TIME(req_sec, req_usec);
449 if (tdm_debug_module & TDM_DEBUG_VBLANK)
450 TDM_DBG("req_id(%d) wait", req_id);
452 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
453 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
455 if (wait_type == VBLANK_WAIT_TYPE_INTERVAL)
456 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, wait_value, _tdm_server_cb_vblank, wait_info);
458 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, wait_value, _tdm_server_cb_vblank, wait_info);
461 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
462 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
465 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
467 if (wait_type == VBLANK_WAIT_TYPE_INTERVAL) {
468 tdm_display_lock(private_loop->dpy);
469 _tdm_server_vblank_timeout_update(vblank_info, 1000);
470 tdm_display_unlock(private_loop->dpy);
476 /* LCOV_EXCL_START */
477 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
479 destroy_wait(wait_info);
484 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
485 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
487 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
489 _tdm_server_vblank_wait_vblank(vblank_info, VBLANK_WAIT_TYPE_INTERVAL, interval, req_id, req_sec, req_usec);
493 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
494 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
496 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
498 _tdm_server_vblank_wait_vblank(vblank_info, VBLANK_WAIT_TYPE_SEQUENCE, sequence, req_id, req_sec, req_usec);
501 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
502 _tdm_server_vblank_cb_destroy,
503 _tdm_server_vblank_cb_set_name,
504 _tdm_server_vblank_cb_set_fps,
505 _tdm_server_vblank_cb_set_offset,
506 _tdm_server_vblank_cb_set_enable_fake,
507 _tdm_server_vblank_cb_wait_vblank,
508 _tdm_server_vblank_cb_wait_vblank_seq,
511 /* LCOV_EXCL_START */
513 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
515 wl_resource_destroy(resource);
520 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
522 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
523 tdm_private_server *private_server = output_info->private_server;
524 tdm_private_loop *private_loop = private_server->private_loop;
525 struct wl_resource *vblank_resource;
527 tdm_server_vblank_info *vblank_info;
530 wl_resource_create(client, &wl_tdm_vblank_interface,
531 wl_resource_get_version(resource), id);
532 if (!vblank_resource) {
533 /* LCOV_EXCL_START */
535 wl_resource_post_no_memory(resource);
536 TDM_ERR("wl_resource_create failed");
542 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
544 /* LCOV_EXCL_START */
546 wl_resource_post_no_memory(resource);
547 wl_resource_destroy(vblank_resource);
548 TDM_ERR("tdm_vblank_create failed");
554 vblank_info = calloc(1, sizeof * vblank_info);
556 /* LCOV_EXCL_START */
558 wl_resource_post_no_memory(resource);
559 wl_resource_destroy(vblank_resource);
560 tdm_vblank_destroy(vblank);
561 TDM_ERR("alloc failed");
567 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
568 vblank_info->output_info = output_info;
569 vblank_info->resource = vblank_resource;
570 vblank_info->vblank = vblank;
571 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
573 tdm_vblank_set_resource(vblank, vblank_resource);
575 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
576 vblank_info, destroy_vblank_callback);
578 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
580 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
581 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
582 if (output == output_info->output)
583 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
589 /* LCOV_EXCL_START */
591 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
593 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
595 TDM_RETURN_IF_FAIL(output_info != NULL);
597 output_info->watch_output_changes = enable;
602 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
604 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
605 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
608 TDM_RETURN_IF_FAIL(output_info != NULL);
610 ret = tdm_output_get_conn_status(output_info->output, &status);
611 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
613 wl_tdm_output_send_connection(output_info->resource, status, ret);
618 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
622 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
624 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
625 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
628 TDM_RETURN_IF_FAIL(output_info != NULL);
630 ret = tdm_output_get_conn_status(output_info->output, &status);
631 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
633 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
634 const tdm_output_mode *mode = NULL;
635 unsigned int hdisplay, vdisplay, vrefresh;
637 ret = tdm_output_get_mode(output_info->output, &mode);
638 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
640 hdisplay = (mode) ? mode->hdisplay : 0;
641 vdisplay = (mode) ? mode->vdisplay : 0;
642 vrefresh = (mode) ? mode->vrefresh : 0;
644 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
646 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
651 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
655 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
657 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
658 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
661 TDM_RETURN_IF_FAIL(output_info != NULL);
663 ret = tdm_output_get_conn_status(output_info->output, &status);
664 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
666 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
667 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
669 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
670 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
672 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
674 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
679 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
682 static const struct wl_tdm_output_interface tdm_output_implementation = {
683 _tdm_server_output_cb_destroy,
684 _tdm_server_output_cb_create_vblank,
685 _tdm_server_output_cb_watch_output_changes,
686 _tdm_server_output_cb_get_connection,
687 _tdm_server_output_cb_get_mode,
688 _tdm_server_output_cb_get_dpms,
692 destroy_output_callback(struct wl_resource *resource)
694 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
695 tdm_server_vblank_info *v = NULL, *vv = NULL;
697 TDM_RETURN_IF_FAIL(output_info != NULL);
699 LIST_DEL(&output_info->link);
701 tdm_output_remove_change_handler(output_info->output,
702 _tdm_server_cb_output_change, output_info);
704 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
705 wl_resource_destroy(v->resource);
712 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
713 const char *name, uint32_t id)
715 tdm_private_server *private_server = wl_resource_get_user_data(resource);
716 tdm_server_output_info *output_info;
717 struct wl_resource *output_resource = NULL;
719 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
722 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
724 /* LCOV_EXCL_START */
726 TDM_ERR("There is no '%s' output", name);
727 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
728 "There is no '%s' output", name);
735 wl_resource_create(client, &wl_tdm_output_interface,
736 wl_resource_get_version(resource), id);
737 if (!output_resource) {
738 /* LCOV_EXCL_START */
740 wl_resource_post_no_memory(resource);
741 TDM_ERR("wl_resource_create failed");
747 output_info = calloc(1, sizeof * output_info);
749 /* LCOV_EXCL_START */
751 wl_resource_post_no_memory(resource);
752 wl_resource_destroy(output_resource);
753 TDM_ERR("alloc failed");
759 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
760 if (ret != TDM_ERROR_NONE) {
761 wl_resource_post_no_memory(resource);
762 wl_resource_destroy(output_resource);
764 TDM_ERR("tdm_output_add_change_handler failed");
768 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
769 output_info->private_server = private_server;
770 output_info->resource = output_resource;
771 output_info->output = output;
772 LIST_INITHEAD(&output_info->vblank_list);
774 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
775 output_info, destroy_output_callback);
777 ret = tdm_output_get_conn_status(output, &status);
778 wl_tdm_output_send_connection(output_resource, status, ret);
780 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
781 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
782 const tdm_output_mode *mode = NULL;
783 unsigned int hdisplay, vdisplay, vrefresh;
785 ret = tdm_output_get_mode(output, &mode);
786 hdisplay = (mode) ? mode->hdisplay : 0;
787 vdisplay = (mode) ? mode->vdisplay : 0;
788 vrefresh = (mode) ? mode->vrefresh : 0;
789 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
791 ret = tdm_output_get_dpms(output, &dpms_value);
792 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
794 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
795 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
799 /* LCOV_EXCL_START */
800 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
802 wl_resource_destroy(resource);
806 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
807 struct wl_resource *resource,
808 struct wl_array *modes)
810 tdm_server_voutput_info *voutput_info;
811 tdm_output_mode *mode;
812 int size, count = 0, i = 0;
814 voutput_info = wl_resource_get_user_data(resource);
816 voutput_info->available_modes.count = 0;
817 if (voutput_info->available_modes.modes)
818 free(voutput_info->available_modes.modes);
820 wl_array_for_each(mode, modes)
822 size = sizeof(tdm_output_mode);
824 voutput_info->available_modes.modes = malloc(count * size);
825 voutput_info->available_modes.count = count;
827 wl_array_for_each(mode, modes)
828 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
832 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
833 unsigned int mmwidth, unsigned int mmheight)
835 tdm_server_voutput_info *voutput_info;
837 voutput_info = wl_resource_get_user_data(resource);
839 voutput_info->mmwidth = mmwidth;
840 voutput_info->mmheight = mmheight;
844 _tdm_voutput_cb_set_mode(struct wl_client *client, struct wl_resource *resource, unsigned int index)
846 tdm_server_voutput_info *voutput_info = NULL;
847 tdm_output *output = NULL;
848 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
849 const tdm_output_mode *modes, *mode, *current_mode;
850 tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
854 voutput_info = wl_resource_get_user_data(resource);
855 TDM_RETURN_IF_FAIL(voutput_info != NULL);
856 output = voutput_info->output;
858 ret = tdm_output_get_conn_status(output, &status);
859 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
860 TDM_RETURN_IF_FAIL(status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED);
862 ret = tdm_output_get_available_modes(output, &modes, &count);
863 TDM_RETURN_IF_FAIL(index < count);
865 mode = &modes[index];
866 TDM_DBG("mode set request to index:%d (%dx%d, %d)", index, mode->hdisplay, mode->vdisplay, mode->vrefresh);
868 ret = tdm_output_get_mode(output, ¤t_mode);
869 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
870 if (mode == current_mode) {
871 TDM_DBG("same mode");
875 if (current_mode && ((mode->hdisplay != current_mode->hdisplay) || (mode->vdisplay != current_mode->vdisplay))) {
876 LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
877 if (vb->wl_buffer == voutput_info->attach_buffer->wl_buffer)
878 voutput_info->attach_buffer->need_reset = 1;
880 wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, vb->wl_buffer);
884 tdm_output_request_mode_set(voutput_info->output, index);
888 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
890 tdm_server_voutput_info *voutput_info;
892 voutput_info = wl_resource_get_user_data(resource);
893 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
895 voutput_info->request_commit = 1;
897 tdm_voutput_set_physical_size(voutput_info->voutput, voutput_info->mmwidth, voutput_info->mmheight);
898 tdm_voutput_set_available_mode(voutput_info->voutput, voutput_info->available_modes.modes, voutput_info->available_modes.count);
899 tdm_voutput_connect(voutput_info->voutput);
900 tdm_output_set_voutput_commit(voutput_info->voutput);
904 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
906 tdm_server_voutput_info *voutput_info;
908 voutput_info = wl_resource_get_user_data(resource);
909 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
911 /* Do free resources when it's being disconnected */
912 free(voutput_info->available_modes.modes);
913 voutput_info->available_modes.modes = NULL;
914 voutput_info->available_modes.count = 0;
915 voutput_info->mmwidth = 0;
916 voutput_info->mmheight = 0;
918 if (voutput_info->request_commit == 1) {
919 tdm_output_unset_voutput_commit(voutput_info->voutput);
920 voutput_info->request_commit = 0;
923 if (voutput_info->attach_buffer) {
924 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
925 tbm_surface_internal_unref(buffer);
926 voutput_info->committing = 0;
927 if (voutput_info->attach_buffer->need_reset)
928 wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, voutput_info->attach_buffer->wl_buffer);
929 voutput_info->attach_buffer = NULL;
930 tdm_voutput_commit_done(voutput_info->voutput);
933 tdm_voutput_disconnect(voutput_info->voutput);
937 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
939 tdm_server_voutput_info *voutput_info;
940 tbm_surface_h buffer;
942 voutput_info = wl_resource_get_user_data(resource);
943 if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED) {
944 TDM_DBG("not connected.");
948 buffer = voutput_info->attach_buffer->buffer;
949 tbm_surface_internal_unref(buffer);
950 voutput_info->committing = 0;
951 if (voutput_info->attach_buffer->need_reset)
952 wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, voutput_info->attach_buffer->wl_buffer);
953 voutput_info->attach_buffer = NULL;
955 if (voutput_info->request_commit == 1)
956 tdm_output_set_voutput_commit(voutput_info->voutput);
958 tdm_voutput_commit_done(voutput_info->voutput);
961 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
962 _tdm_voutput_cb_destroy,
963 _tdm_voutput_cb_set_available_modes,
964 _tdm_voutput_cb_set_physical_size,
965 _tdm_voutput_cb_set_mode,
966 _tdm_voutput_cb_connect,
967 _tdm_voutput_cb_disconnect,
968 _tdm_voutput_cb_commit_done
972 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
974 wl_resource_destroy(wl_buffer);
977 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
978 _tdm_voutput_wl_buffer_destroy
982 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
984 tdm_server_voutput_info *voutput_info = NULL;
985 tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
987 voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
988 TDM_RETURN_IF_FAIL(voutput_info != NULL);
990 LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
991 if (vb->wl_buffer == wl_buffer) {
992 tbm_surface_internal_unref(vb->buffer);
993 wl_resource_set_user_data(wl_buffer, NULL);
1000 struct wl_resource *
1001 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
1003 struct wl_client *wl_client;
1004 struct wl_resource *wl_buffer = NULL;
1006 wl_client = wl_resource_get_client(voutput_info->resource);
1008 /* create a wl_buffer resource */
1009 wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
1010 TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
1012 wl_resource_set_implementation(wl_buffer,
1013 (void (**)(void)) &_tdm_voutput_buffer_impementation,
1014 voutput_info, _tdm_voutput_buffer_destory);
1019 struct wl_resource *
1020 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
1021 tbm_surface_h buffer)
1023 int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
1024 struct wl_resource *wl_buffer = NULL;
1025 int num_buf, is_fd = -1, i;
1026 tbm_surface_info_s info;
1028 struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
1031 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
1032 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
1034 if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
1035 TDM_ERR("Failed to create buffer from surface");
1039 if (info.num_planes > 3) {
1040 TDM_ERR("invalid num_planes(%d)", info.num_planes);
1044 num_buf = tbm_surface_internal_get_num_bos(buffer);
1046 TDM_ERR("surface doesn't have any bo.");
1050 for (i = 0; i < num_buf; i++) {
1051 tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
1053 TDM_ERR("Failed to get bo from surface");
1057 /* try to get fd first */
1058 if (is_fd == -1 || is_fd == 1) {
1059 bufs[i] = tbm_bo_export_fd(bo);
1060 if (is_fd == -1 && bufs[i] >= 0)
1064 /* if fail to get fd, try to get name second */
1065 if (is_fd == -1 || is_fd == 0) {
1066 bufs[i] = tbm_bo_export(bo);
1067 if (is_fd == -1 && bufs[i] > 0)
1072 (is_fd == 1 && bufs[i] < 0) ||
1073 (is_fd == 0 && bufs[i] <= 0)) {
1074 TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
1079 wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
1081 TDM_ERR("Failed to create wl_buffer");
1085 wl_array_init(&plane_buf_idx);
1086 wl_array_init(&plane_offset);
1087 wl_array_init(&plane_stride);
1088 wl_array_init(&plane_size);
1090 for (i = 0; i < 3; i++) {
1091 p = wl_array_add(&plane_buf_idx, sizeof(int));
1092 *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
1093 p = wl_array_add(&plane_offset, sizeof(int));
1094 *p = info.planes[i].offset;
1095 p = wl_array_add(&plane_stride, sizeof(int));
1096 *p = info.planes[i].stride;
1097 p = wl_array_add(&plane_size, sizeof(int));
1098 *p = info.planes[i].size;
1102 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
1104 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1105 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1106 flags, num_buf, bufs[0],
1107 (bufs[1] == -1) ? bufs[0] : bufs[1],
1108 (bufs[2] == -1) ? bufs[0] : bufs[2]);
1110 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
1112 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1113 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1115 num_buf, bufs[0], bufs[1], bufs[2]);
1117 wl_array_release(&plane_buf_idx);
1118 wl_array_release(&plane_offset);
1119 wl_array_release(&plane_stride);
1120 wl_array_release(&plane_size);
1122 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1123 if (is_fd == 1 && (bufs[i] >= 0))
1130 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1131 if (is_fd == 1 && (bufs[i] >= 0))
1138 tdm_server_voutput_buffer *
1139 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
1141 tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
1143 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1144 if (vb && vb->buffer == buffer) {
1149 tbm_surface_internal_ref(buffer);
1150 voutput_buffer = calloc(1, sizeof *voutput_buffer);
1151 if (!voutput_buffer) {
1152 TDM_ERR("fail calloc");
1153 tbm_surface_internal_unref(buffer);
1157 voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, buffer);
1158 if (!voutput_buffer->wl_buffer) {
1159 TDM_ERR("fail export buffer");
1160 free(voutput_buffer);
1161 tbm_surface_internal_unref(buffer);
1165 voutput_buffer->buffer = buffer;
1166 LIST_ADDTAIL(&voutput_buffer->link, &voutput_info->buffer_list);
1168 return voutput_buffer;
1172 tdm_voutput_attach_buffer(tdm_voutput *voutput, tbm_surface_h buffer)
1174 tdm_private_server *private_server = keep_private_server;
1175 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1176 tdm_server_voutput_buffer *voutput_buffer = NULL;
1178 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1180 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1181 if (vo && vo->voutput == voutput) {
1186 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1187 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer == NULL, TDM_ERROR_OPERATION_FAILED);
1189 voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer);
1190 TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1192 voutput_info->attach_buffer = voutput_buffer;
1194 wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
1196 return TDM_ERROR_NONE;
1200 tdm_voutput_commit_buffer(tdm_voutput *voutput)
1202 tdm_private_server *private_server = keep_private_server;
1203 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1205 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1207 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1208 if (vo && vo->voutput == voutput) {
1213 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1214 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
1215 TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
1217 voutput_info->committing = 1;
1219 wl_tdm_voutput_send_commit(voutput_info->resource);
1221 return TDM_ERROR_NONE;
1225 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1227 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1228 tdm_server_voutput_buffer *vb = NULL;
1229 tdm_voutput *voutput;
1230 tdm_error ret = TDM_ERROR_NONE;
1232 TDM_RETURN_IF_FAIL(voutput_info != NULL);
1234 voutput = voutput_info->voutput;
1236 if (voutput_info->request_commit)
1237 tdm_output_unset_voutput_commit(voutput_info->voutput);
1240 if (voutput_info->request_commit == 1) {
1241 tdm_output_unset_voutput_commit(voutput_info->voutput);
1242 voutput_info->request_commit = 0;
1243 tdm_voutput_disconnect(voutput_info->voutput);
1246 ret = tdm_voutput_destroy(voutput);
1247 if (ret != TDM_ERROR_NONE)
1248 TDM_ERR("_tdm_voutput_cb_destroy fail");
1251 if (voutput_info->attach_buffer) {
1252 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
1253 tbm_surface_internal_unref(buffer);
1254 voutput_info->committing = 0;
1255 voutput_info->attach_buffer = NULL;
1258 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1260 wl_resource_destroy(vb->wl_buffer);
1263 LIST_DEL(&voutput_info->link);
1265 /* Do free your own resource */
1270 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1272 struct wl_resource *voutput_resource = NULL;
1273 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1274 tdm_server_voutput_info *voutput_info;
1275 tdm_voutput *voutput;
1279 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1281 TDM_ERR("There is '%s' output, cannot create.", name);
1282 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1283 "There is '%s' output", name);
1287 voutput = tdm_display_voutput_create(private_server->private_loop->dpy, name, &ret);
1289 TDM_ERR("voutput creation fail(%s)(%d).", name, ret);
1290 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1291 "%s output creation fail", name);
1295 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1297 TDM_ERR("There is no '%s' output.", name);
1298 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1299 "There is '%s' output", name);
1304 wl_resource_create(client, &wl_tdm_voutput_interface,
1305 wl_resource_get_version(resource), id);
1306 if (!voutput_resource) {
1307 /* LCOV_EXCL_START */
1309 wl_resource_post_no_memory(resource);
1310 TDM_ERR("wl_resource_create failed");
1313 /* LCOV_EXCL_STOP */
1316 voutput_info = calloc(1, sizeof * voutput_info);
1317 if (!voutput_info) {
1318 /* LCOV_EXCL_START */
1320 wl_resource_post_no_memory(resource);
1321 wl_resource_destroy(voutput_resource);
1322 TDM_ERR("alloc failed");
1325 /* LCOV_EXCL_STOP */
1328 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1329 voutput_info->private_server = private_server;
1330 voutput_info->resource = voutput_resource;
1331 voutput_info->voutput = voutput;
1332 voutput_info->output = output;
1333 LIST_INITHEAD(&voutput_info->buffer_list);
1335 wl_resource_set_implementation(voutput_resource,
1336 &tdm_voutput_implementation,
1338 tdm_voutput_cb_resource_destroy);
1340 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1342 /* LCOV_EXCL_STOP */
1344 /* LCOV_EXCL_START */
1346 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1348 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1349 tdm_private_loop *private_loop = private_server->private_loop;
1350 char message[TDM_SERVER_REPLY_MSG_LEN];
1352 int len = sizeof(message), size;
1355 wl_client_get_credentials(client, NULL, &uid, NULL);
1358 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1359 TDM_ERR("%s", message);
1361 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1364 size = sizeof(message) - len;
1367 wl_client_flush(client);
1370 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1371 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1373 memcpy(buffer, m, sizeof(buffer));
1374 buffer[sizeof(buffer) - 1] = '\0';
1379 buffer[copylen] = '\0';
1381 wl_tdm_send_debug_message(resource, buffer);
1384 wl_tdm_send_debug_done(resource);
1386 /* LCOV_EXCL_STOP */
1388 static const struct wl_tdm_interface tdm_implementation = {
1389 _tdm_server_cb_debug,
1390 _tdm_server_cb_create_output,
1391 _tdm_server_cb_create_virtual_output
1395 destroy_client(struct wl_resource *resource)
1397 tdm_server_client_info *c = NULL, *cc = NULL;
1398 struct wl_client *client;
1400 client = wl_resource_get_client(resource);
1401 TDM_RETURN_IF_FAIL(client != NULL);
1403 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1404 if (c->resource == resource) {
1413 _tdm_server_bind(struct wl_client *client, void *data,
1414 uint32_t version, uint32_t id)
1416 struct wl_resource *resource;
1417 tdm_server_client_info *cinfo;
1419 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1421 /* LCOV_EXCL_START */
1423 wl_client_post_no_memory(client);
1426 /* LCOV_EXCL_STOP */
1429 cinfo = calloc(1, sizeof(tdm_server_client_info));
1431 /* LCOV_EXCL_START */
1433 wl_client_post_no_memory(client);
1434 wl_resource_destroy(resource);
1437 /* LCOV_EXCL_STOP */
1440 cinfo->resource = resource;
1442 LIST_ADDTAIL(&cinfo->link, &client_list);
1443 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1444 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1446 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1450 tdm_server_init(tdm_private_loop *private_loop)
1452 tdm_private_server *private_server;
1454 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1455 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1457 if (private_loop->private_server)
1458 return TDM_ERROR_NONE;
1460 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1461 /* LCOV_EXCL_START */
1463 TDM_ERR("createing a tdm-socket failed");
1464 return TDM_ERROR_OPERATION_FAILED;
1466 /* LCOV_EXCL_STOP */
1469 private_server = calloc(1, sizeof * private_server);
1470 if (!private_server) {
1471 TDM_ERR("alloc failed");
1472 return TDM_ERROR_OUT_OF_MEMORY;
1475 LIST_INITHEAD(&private_server->output_list);
1476 LIST_INITHEAD(&private_server->voutput_list);
1477 LIST_INITHEAD(&private_server->wait_list);
1479 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1480 private_server, _tdm_server_bind)) {
1481 /* LCOV_EXCL_START */
1483 TDM_ERR("creating a global resource failed");
1484 free(private_server);
1485 return TDM_ERROR_OUT_OF_MEMORY;
1487 /* LCOV_EXCL_STOP */
1490 private_server->private_loop = private_loop;
1491 private_loop->private_server = private_server;
1492 keep_private_server = private_server;
1494 LIST_INITHEAD(&client_list);
1496 return TDM_ERROR_NONE;
1500 tdm_server_deinit(tdm_private_loop *private_loop)
1502 tdm_server_output_info *o = NULL, *oo = NULL;
1503 tdm_server_voutput_info *vo = NULL, *voo = NULL;
1504 tdm_server_wait_info *w = NULL, *ww = NULL;
1505 tdm_server_client_info *c = NULL, *cc = NULL;
1506 tdm_private_server *private_server;
1508 if (!private_loop->private_server)
1511 private_server = private_loop->private_server;
1513 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1517 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1518 wl_resource_destroy(o->resource);
1521 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1522 wl_resource_destroy(vo->resource);
1525 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1526 wl_resource_destroy(c->resource);
1529 free(private_server);
1530 private_loop->private_server = NULL;
1531 keep_private_server = NULL;
1534 /* LCOV_EXCL_START */
1536 tdm_server_get_client_name(pid_t pid)
1538 tdm_server_client_info *c = NULL;
1540 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1542 return (const char*)c->name;
1547 /* LCOV_EXCL_STOP */
1549 /* LCOV_EXCL_START */
1551 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1553 tdm_private_server *private_server = keep_private_server;
1554 tdm_server_output_info *output_info = NULL;
1556 if (!keep_private_server)
1557 return TDM_ERROR_NONE;
1559 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1560 tdm_server_vblank_info *vblank_info = NULL;
1562 if (output && output_info->output != output)
1565 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1566 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1570 return TDM_ERROR_NONE;
1572 /* LCOV_EXCL_STOP */