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;
84 tdm_output_conn_status status;
87 tdm_output_mode *modes;
91 unsigned int mmheight;
93 struct list_head buffer_list;
94 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;
107 tdm_event_loop_source *vblank_timeout_timer;
108 } tdm_server_vblank_info;
110 typedef struct _tdm_server_wait_info {
111 struct list_head link;
112 tdm_server_vblank_info *vblank_info;
117 unsigned int timeout;
118 } tdm_server_wait_info;
120 typedef struct _tdm_server_client_info {
121 struct list_head link;
123 char name[TDM_NAME_LEN];
124 struct wl_resource *resource;
125 } tdm_server_client_info;
127 static tdm_private_server *keep_private_server;
128 static struct list_head client_list;
130 static void destroy_wait(tdm_server_wait_info *wait_info);
131 static void _tdm_server_vblank_timeout_update(tdm_server_vblank_info *vblank_info, int ms_delay);
134 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
136 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
140 snprintf(name, size, "Unknown");
142 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
143 h = fopen(proc, "r");
147 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
149 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
152 pname[len - 1] = '\0';
154 strncpy(name, pname, size - 1);
155 name[size - 1] = '\0';
160 /* LCOV_EXCL_START */
162 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
163 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
165 tdm_server_wait_info *found;
166 tdm_server_vblank_info *vblank_info;
167 tdm_server_output_info *output_info;
168 tdm_private_server *private_server;
169 tdm_private_loop *private_loop;
171 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
173 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
174 tdm_server_wait_info, link, found);
176 TDM_ERR("wait_info(%p) is destroyed", wait_info);
180 if (tdm_debug_module & TDM_DEBUG_VBLANK)
181 TDM_DBG("req_id(%d) done", wait_info->req_id);
183 vblank_info = wait_info->vblank_info;
184 output_info = vblank_info->output_info;
185 private_server = output_info->private_server;
186 private_loop = private_server->private_loop;
188 tdm_display_lock(private_loop->dpy);
189 _tdm_server_vblank_timeout_update(vblank_info, 0);
190 tdm_display_unlock(private_loop->dpy);
192 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
193 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
195 if (!wait_info->timeout)
196 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
197 sequence, tv_sec, tv_usec, error);
199 destroy_wait(wait_info);
203 /* LCOV_EXCL_START */
205 _tdm_server_timeout_timer_cb(void *user_data)
207 tdm_server_vblank_info *vblank_info = user_data;
208 tdm_server_wait_info *wait_info = NULL;
211 unsigned int tv_usec;
213 TDM_RETURN_VAL_IF_FAIL(vblank_info != NULL, TDM_ERROR_OPERATION_FAILED);
214 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
216 curr = tdm_helper_get_time();
217 tv_sec = TDM_TIME_SEC(curr);
218 tv_usec = TDM_TIME_USEC(curr);
220 LIST_FOR_EACH_ENTRY(wait_info, &keep_private_server->wait_list, link) {
221 if (wait_info->timeout) continue;
222 if (vblank_info != wait_info->vblank_info) continue;
224 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
225 0, tv_sec, tv_usec, TDM_ERROR_TIMEOUT);
226 TDM_ERR("tdm_server_vblank(%p) req_id(%d) timeout force send vblank", vblank_info, wait_info->req_id);
227 wait_info->timeout = 1;
230 return TDM_ERROR_NONE;
234 _tdm_server_vblank_timeout_update(tdm_server_vblank_info *vblank_info, int ms_delay)
236 tdm_server_output_info *output_info = vblank_info->output_info;
237 tdm_private_server *private_server = output_info->private_server;
238 tdm_private_loop *private_loop = private_server->private_loop;
241 if (!vblank_info->vblank_timeout_timer) {
242 vblank_info->vblank_timeout_timer =
243 tdm_event_loop_add_timer_handler(private_loop->dpy,
244 _tdm_server_timeout_timer_cb,
247 if (!vblank_info->vblank_timeout_timer) {
248 TDM_ERR("tdm_server_vblank(%p) couldn't add timer", vblank_info);
252 if (tdm_debug_module & TDM_DEBUG_VBLANK)
253 TDM_INFO("tdm_server_vblank(%p) create vblank timeout timer", vblank_info);
256 ret = tdm_event_loop_source_timer_update(vblank_info->vblank_timeout_timer, ms_delay);
257 if (ret != TDM_ERROR_NONE) {
258 TDM_ERR("tdm_server_vblank(%p) couldn't update timer", vblank_info);
264 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
265 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
267 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
271 /* LCOV_EXCL_START */
273 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
274 tdm_value value, void *user_data)
276 tdm_server_output_info *output_info = user_data;
277 struct wl_client *client;
279 tdm_output_conn_status status;
280 tdm_error ret = TDM_ERROR_NONE;
282 TDM_RETURN_IF_FAIL(output_info != NULL);
284 client = wl_resource_get_client(output_info->resource);
285 TDM_RETURN_IF_FAIL(client != NULL);
287 wl_client_get_credentials(client, &pid, NULL, NULL);
289 if (!output_info->watch_output_changes) {
290 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
294 TDM_DBG("send the output changes: %d, type:%d, value:%d", (unsigned int)pid, type, value.u32);
297 case TDM_OUTPUT_CHANGE_DPMS:
298 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
300 case TDM_OUTPUT_CHANGE_CONNECTION:
302 if (status == TDM_OUTPUT_CONN_STATUS_MODE_SETTED) {
303 const tdm_output_mode *mode = NULL;
304 unsigned int hdisplay, vdisplay, vrefresh;
306 ret = tdm_output_get_mode(output_info->output, &mode);
307 if (ret == TDM_ERROR_NONE) {
308 hdisplay = (mode) ? mode->hdisplay : 0;
309 vdisplay = (mode) ? mode->vdisplay : 0;
310 vrefresh = (mode) ? mode->vrefresh : 0;
312 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
314 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
317 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
326 destroy_wait(tdm_server_wait_info *wait_info)
328 LIST_DEL(&wait_info->link);
333 destroy_vblank_callback(struct wl_resource *resource)
335 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
336 tdm_server_output_info *output_info = vblank_info->output_info;
337 tdm_private_server *private_server = output_info->private_server;
338 tdm_private_loop *private_loop = private_server->private_loop;
339 tdm_server_wait_info *w = NULL, *ww = NULL;
341 TDM_RETURN_IF_FAIL(vblank_info != NULL);
343 if (vblank_info->vblank_timeout_timer) {
344 tdm_display_lock(private_loop->dpy);
345 tdm_event_loop_source_remove(vblank_info->vblank_timeout_timer);
346 tdm_display_unlock(private_loop->dpy);
349 LIST_DEL(&vblank_info->link);
351 if (vblank_info->vblank)
352 tdm_vblank_destroy(vblank_info->vblank);
354 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
355 if (w->vblank_info == vblank_info)
362 /* LCOV_EXCL_START */
364 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
366 wl_resource_destroy(resource);
370 /* LCOV_EXCL_START */
372 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
374 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
377 ret = tdm_vblank_set_name(vblank_info->vblank, name);
378 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
382 /* LCOV_EXCL_START */
384 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
386 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
389 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
390 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
394 /* LCOV_EXCL_START */
396 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
398 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
401 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
402 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
407 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
409 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
412 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
413 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
417 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
418 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
420 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
421 tdm_server_output_info *output_info = vblank_info->output_info;
422 tdm_private_server *private_server = output_info->private_server;
423 tdm_private_loop *private_loop = private_server->private_loop;
424 tdm_server_wait_info *wait_info;
425 unsigned int enable_fake = 0;
428 wait_info = calloc(1, sizeof * wait_info);
430 /* LCOV_EXCL_START */
432 TDM_ERR("alloc failed");
433 ret = TDM_ERROR_OUT_OF_MEMORY;
439 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
440 wait_info->vblank_info = vblank_info;
441 wait_info->req_id = req_id;
442 wait_info->req_time = TDM_TIME(req_sec, req_usec);
444 if (tdm_debug_module & TDM_DEBUG_VBLANK)
445 TDM_DBG("req_id(%d) wait", req_id);
447 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
448 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
450 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
452 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
453 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
456 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
458 tdm_display_lock(private_loop->dpy);
459 _tdm_server_vblank_timeout_update(vblank_info, 1000);
460 tdm_display_unlock(private_loop->dpy);
464 /* LCOV_EXCL_START */
466 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
468 destroy_wait(wait_info);
474 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
475 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
477 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
478 tdm_server_output_info *output_info = vblank_info->output_info;
479 tdm_private_server *private_server = output_info->private_server;
480 tdm_server_wait_info *wait_info;
481 unsigned int enable_fake = 0;
484 wait_info = calloc(1, sizeof * wait_info);
486 /* LCOV_EXCL_START */
488 TDM_ERR("alloc failed");
489 ret = TDM_ERROR_OUT_OF_MEMORY;
495 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
496 wait_info->vblank_info = vblank_info;
497 wait_info->req_id = req_id;
498 wait_info->req_time = TDM_TIME(req_sec, req_usec);
500 if (tdm_debug_module & TDM_DEBUG_VBLANK)
501 TDM_DBG("req_id(%d) wait", req_id);
503 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
504 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
506 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
508 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
509 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
512 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
516 /* LCOV_EXCL_START */
518 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
520 destroy_wait(wait_info);
525 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
526 _tdm_server_vblank_cb_destroy,
527 _tdm_server_vblank_cb_set_name,
528 _tdm_server_vblank_cb_set_fps,
529 _tdm_server_vblank_cb_set_offset,
530 _tdm_server_vblank_cb_set_enable_fake,
531 _tdm_server_vblank_cb_wait_vblank,
532 _tdm_server_vblank_cb_wait_vblank_seq,
535 /* LCOV_EXCL_START */
537 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
539 wl_resource_destroy(resource);
544 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
546 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
547 tdm_private_server *private_server = output_info->private_server;
548 tdm_private_loop *private_loop = private_server->private_loop;
549 struct wl_resource *vblank_resource;
551 tdm_server_vblank_info *vblank_info;
554 wl_resource_create(client, &wl_tdm_vblank_interface,
555 wl_resource_get_version(resource), id);
556 if (!vblank_resource) {
557 /* LCOV_EXCL_START */
559 wl_resource_post_no_memory(resource);
560 TDM_ERR("wl_resource_create failed");
566 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
568 /* LCOV_EXCL_START */
570 wl_resource_post_no_memory(resource);
571 wl_resource_destroy(vblank_resource);
572 TDM_ERR("tdm_vblank_create failed");
578 vblank_info = calloc(1, sizeof * vblank_info);
580 /* LCOV_EXCL_START */
582 wl_resource_post_no_memory(resource);
583 wl_resource_destroy(vblank_resource);
584 tdm_vblank_destroy(vblank);
585 TDM_ERR("alloc failed");
591 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
592 vblank_info->output_info = output_info;
593 vblank_info->resource = vblank_resource;
594 vblank_info->vblank = vblank;
595 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
597 tdm_vblank_set_resource(vblank, vblank_resource);
599 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
600 vblank_info, destroy_vblank_callback);
602 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
604 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
605 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
606 if (output == output_info->output)
607 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
613 /* LCOV_EXCL_START */
615 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
617 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
619 TDM_RETURN_IF_FAIL(output_info != NULL);
621 output_info->watch_output_changes = enable;
626 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
628 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
629 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
632 TDM_RETURN_IF_FAIL(output_info != NULL);
634 ret = tdm_output_get_conn_status(output_info->output, &status);
635 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
637 wl_tdm_output_send_connection(output_info->resource, status, ret);
642 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
646 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
648 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
649 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
652 TDM_RETURN_IF_FAIL(output_info != NULL);
654 ret = tdm_output_get_conn_status(output_info->output, &status);
655 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
657 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
658 const tdm_output_mode *mode = NULL;
659 unsigned int hdisplay, vdisplay, vrefresh;
661 ret = tdm_output_get_mode(output_info->output, &mode);
662 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
664 hdisplay = (mode) ? mode->hdisplay : 0;
665 vdisplay = (mode) ? mode->vdisplay : 0;
666 vrefresh = (mode) ? mode->vrefresh : 0;
668 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
670 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
675 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
679 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
681 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
682 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
685 TDM_RETURN_IF_FAIL(output_info != NULL);
687 ret = tdm_output_get_conn_status(output_info->output, &status);
688 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
690 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
691 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
693 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
694 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
696 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
698 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
703 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
706 static const struct wl_tdm_output_interface tdm_output_implementation = {
707 _tdm_server_output_cb_destroy,
708 _tdm_server_output_cb_create_vblank,
709 _tdm_server_output_cb_watch_output_changes,
710 _tdm_server_output_cb_get_connection,
711 _tdm_server_output_cb_get_mode,
712 _tdm_server_output_cb_get_dpms,
716 destroy_output_callback(struct wl_resource *resource)
718 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
719 tdm_server_vblank_info *v = NULL, *vv = NULL;
721 TDM_RETURN_IF_FAIL(output_info != NULL);
723 LIST_DEL(&output_info->link);
725 tdm_output_remove_change_handler(output_info->output,
726 _tdm_server_cb_output_change, output_info);
728 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
729 wl_resource_destroy(v->resource);
736 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
737 const char *name, uint32_t id)
739 tdm_private_server *private_server = wl_resource_get_user_data(resource);
740 tdm_server_output_info *output_info;
741 struct wl_resource *output_resource = NULL;
743 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
746 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
748 /* LCOV_EXCL_START */
750 TDM_ERR("There is no '%s' output", name);
751 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
752 "There is no '%s' output", name);
759 wl_resource_create(client, &wl_tdm_output_interface,
760 wl_resource_get_version(resource), id);
761 if (!output_resource) {
762 /* LCOV_EXCL_START */
764 wl_resource_post_no_memory(resource);
765 TDM_ERR("wl_resource_create failed");
771 output_info = calloc(1, sizeof * output_info);
773 /* LCOV_EXCL_START */
775 wl_resource_post_no_memory(resource);
776 wl_resource_destroy(output_resource);
777 TDM_ERR("alloc failed");
783 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
784 if (ret != TDM_ERROR_NONE) {
785 wl_resource_post_no_memory(resource);
786 wl_resource_destroy(output_resource);
788 TDM_ERR("tdm_output_add_change_handler failed");
792 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
793 output_info->private_server = private_server;
794 output_info->resource = output_resource;
795 output_info->output = output;
796 LIST_INITHEAD(&output_info->vblank_list);
798 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
799 output_info, destroy_output_callback);
801 ret = tdm_output_get_conn_status(output, &status);
802 wl_tdm_output_send_connection(output_resource, status, ret);
804 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
805 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
806 const tdm_output_mode *mode = NULL;
807 unsigned int hdisplay, vdisplay, vrefresh;
809 ret = tdm_output_get_mode(output, &mode);
810 hdisplay = (mode) ? mode->hdisplay : 0;
811 vdisplay = (mode) ? mode->vdisplay : 0;
812 vrefresh = (mode) ? mode->vrefresh : 0;
813 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
815 ret = tdm_output_get_dpms(output, &dpms_value);
816 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
818 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
819 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
823 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
825 wl_resource_destroy(resource);
829 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
830 struct wl_resource *resource,
831 struct wl_array *modes)
833 tdm_server_voutput_info *voutput_info;
834 tdm_output_mode *mode;
835 int size, count = 0, i = 0;
837 voutput_info = wl_resource_get_user_data(resource);
839 voutput_info->available_modes.count = 0;
840 if (voutput_info->available_modes.modes)
841 free(voutput_info->available_modes.modes);
843 wl_array_for_each(mode, modes)
845 size = sizeof(tdm_output_mode);
847 voutput_info->available_modes.modes = malloc(count * size);
848 voutput_info->available_modes.count = count;
850 wl_array_for_each(mode, modes)
851 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
855 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
856 unsigned int mmwidth, unsigned int mmheight)
858 tdm_server_voutput_info *voutput_info;
860 voutput_info = wl_resource_get_user_data(resource);
862 voutput_info->mmwidth = mmwidth;
863 voutput_info->mmheight = mmheight;
867 _tdm_voutput_cb_set_mode(struct wl_client *client, struct wl_resource *resource, unsigned int index)
869 tdm_server_voutput_info *voutput_info = NULL;
870 tdm_output *output = NULL;
871 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
872 const tdm_output_mode *modes, *mode;
877 voutput_info = wl_resource_get_user_data(resource);
878 TDM_RETURN_IF_FAIL(voutput_info != NULL);
879 output = voutput_info->output;
881 ret = tdm_output_get_conn_status(output, &status);
882 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
883 TDM_RETURN_IF_FAIL(status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED);
885 ret = tdm_output_get_available_modes(output, &modes, &count);
886 TDM_RETURN_IF_FAIL(index < count);
888 mode = &modes[index];
889 TDM_DBG("mode set request to index:%d (%dx%d, %d)", index, mode->hdisplay, mode->vdisplay, mode->vrefresh);
891 ret = tdm_output_set_mode(output, mode);
892 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
894 tdm_output_request_mode_set(voutput_info->output, index);
898 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
900 tdm_server_voutput_info *voutput_info;
902 voutput_info = wl_resource_get_user_data(resource);
903 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
905 tdm_voutput_set_physical_size(voutput_info->voutput, voutput_info->mmwidth, voutput_info->mmheight);
906 tdm_voutput_set_available_mode(voutput_info->voutput, voutput_info->available_modes.modes, voutput_info->available_modes.count);
907 tdm_voutput_connect(voutput_info->voutput);
911 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
913 tdm_server_voutput_info *voutput_info;
915 voutput_info = wl_resource_get_user_data(resource);
916 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
918 /* Do free resources when it's being disconnected */
919 free(voutput_info->available_modes.modes);
920 voutput_info->available_modes.modes = NULL;
921 voutput_info->available_modes.count = 0;
922 voutput_info->mmwidth = 0;
923 voutput_info->mmheight = 0;
925 if (voutput_info->attach_buffer) {
926 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
927 tbm_surface_internal_unref(buffer);
928 voutput_info->committing = 0;
929 voutput_info->attach_buffer = NULL;
932 tdm_voutput_disconnect(voutput_info->voutput);
936 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
938 tdm_server_voutput_info *voutput_info;
939 tbm_surface_h buffer;
941 voutput_info = wl_resource_get_user_data(resource);
942 if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED) {
943 TDM_DBG("not connected.");
947 buffer = voutput_info->attach_buffer->buffer;
948 tbm_surface_internal_unref(buffer);
949 voutput_info->committing = 0;
950 voutput_info->attach_buffer = NULL;
952 tdm_voutput_commit_done(voutput_info->voutput);
955 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
956 _tdm_voutput_cb_destroy,
957 _tdm_voutput_cb_set_available_modes,
958 _tdm_voutput_cb_set_physical_size,
959 _tdm_voutput_cb_set_mode,
960 _tdm_voutput_cb_connect,
961 _tdm_voutput_cb_disconnect,
962 _tdm_voutput_cb_commit_done
966 _tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
968 wl_resource_destroy(wl_buffer);
971 static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
972 _tdm_voutput_wl_buffer_destroy
976 _tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
978 tdm_server_voutput_info *voutput_info = NULL;
979 tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
981 voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
982 TDM_RETURN_IF_FAIL(voutput_info != NULL);
984 LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
985 if (vb->wl_buffer == wl_buffer) {
986 tbm_surface_internal_unref(vb->buffer);
987 wl_resource_set_user_data(wl_buffer, NULL);
995 _tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
997 struct wl_client *wl_client;
998 struct wl_resource *wl_buffer = NULL;
1000 wl_client = wl_resource_get_client(voutput_info->resource);
1002 /* create a wl_buffer resource */
1003 wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
1004 TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
1006 wl_resource_set_implementation(wl_buffer,
1007 (void (**)(void)) &_tdm_voutput_buffer_impementation,
1008 voutput_info, _tdm_voutput_buffer_destory);
1013 struct wl_resource *
1014 _tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
1015 tbm_surface_h buffer)
1017 int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
1018 struct wl_resource *wl_buffer = NULL;
1019 int num_buf, is_fd = -1, i;
1020 tbm_surface_info_s info;
1022 struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
1025 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
1026 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
1028 if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
1029 TDM_ERR("Failed to create buffer from surface");
1033 if (info.num_planes > 3) {
1034 TDM_ERR("invalid num_planes(%d)", info.num_planes);
1038 num_buf = tbm_surface_internal_get_num_bos(buffer);
1040 TDM_ERR("surface doesn't have any bo.");
1044 for (i = 0; i < num_buf; i++) {
1045 tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
1047 TDM_ERR("Failed to get bo from surface");
1051 /* try to get fd first */
1052 if (is_fd == -1 || is_fd == 1) {
1053 bufs[i] = tbm_bo_export_fd(bo);
1054 if (is_fd == -1 && bufs[i] >= 0)
1058 /* if fail to get fd, try to get name second */
1059 if (is_fd == -1 || is_fd == 0) {
1060 bufs[i] = tbm_bo_export(bo);
1061 if (is_fd == -1 && bufs[i] > 0)
1066 (is_fd == 1 && bufs[i] < 0) ||
1067 (is_fd == 0 && bufs[i] <= 0)) {
1068 TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
1073 wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
1075 TDM_ERR("Failed to create wl_buffer");
1079 wl_array_init(&plane_buf_idx);
1080 wl_array_init(&plane_offset);
1081 wl_array_init(&plane_stride);
1082 wl_array_init(&plane_size);
1084 for (i = 0; i < 3; i++) {
1085 p = wl_array_add(&plane_buf_idx, sizeof(int));
1086 *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
1087 p = wl_array_add(&plane_offset, sizeof(int));
1088 *p = info.planes[i].offset;
1089 p = wl_array_add(&plane_stride, sizeof(int));
1090 *p = info.planes[i].stride;
1091 p = wl_array_add(&plane_size, sizeof(int));
1092 *p = info.planes[i].size;
1096 wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
1098 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1099 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1100 flags, num_buf, bufs[0],
1101 (bufs[1] == -1) ? bufs[0] : bufs[1],
1102 (bufs[2] == -1) ? bufs[0] : bufs[2]);
1104 wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
1106 info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
1107 &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
1109 num_buf, bufs[0], bufs[1], bufs[2]);
1111 wl_array_release(&plane_buf_idx);
1112 wl_array_release(&plane_offset);
1113 wl_array_release(&plane_stride);
1114 wl_array_release(&plane_size);
1116 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1117 if (is_fd == 1 && (bufs[i] >= 0))
1124 for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
1125 if (is_fd == 1 && (bufs[i] >= 0))
1132 tdm_server_voutput_buffer *
1133 _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
1135 tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
1137 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1138 if (vb && vb->buffer == buffer) {
1139 tbm_surface_internal_ref(vb->buffer);
1144 tbm_surface_internal_ref(buffer);
1145 voutput_buffer = calloc(1, sizeof *voutput_buffer);
1146 if (!voutput_buffer) {
1147 /* LCOV_EXCL_START */
1149 TDM_ERR("fail calloc");
1150 tbm_surface_internal_unref(buffer);
1153 /* LCOV_EXCL_STOP */
1156 voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, buffer);
1157 if (!voutput_buffer->wl_buffer) {
1158 /* LCOV_EXCL_START */
1160 TDM_ERR("fail export buffer");
1161 free(voutput_buffer);
1162 tbm_surface_internal_unref(buffer);
1165 /* LCOV_EXCL_STOP */
1168 voutput_buffer->buffer = buffer;
1169 LIST_ADDTAIL(&voutput_buffer->link, &voutput_info->buffer_list);
1171 return voutput_buffer;
1175 tdm_voutput_attach_buffer(tdm_voutput *voutput, tbm_surface_h buffer)
1177 tdm_private_server *private_server = keep_private_server;
1178 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1179 tdm_server_voutput_buffer *voutput_buffer = NULL;
1181 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1183 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1184 if (vo && vo->voutput == voutput) {
1189 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1190 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer == NULL, TDM_ERROR_OPERATION_FAILED);
1192 voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer);
1193 TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
1195 voutput_info->attach_buffer = voutput_buffer;
1197 wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
1199 return TDM_ERROR_NONE;
1203 tdm_voutput_commit_buffer(tdm_voutput *voutput)
1205 tdm_private_server *private_server = keep_private_server;
1206 tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
1208 TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
1210 LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
1211 if (vo && vo->voutput == voutput) {
1216 TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
1217 TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
1218 TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
1220 tbm_surface_internal_ref(voutput_info->attach_buffer->buffer);
1221 voutput_info->committing = 1;
1223 wl_tdm_voutput_send_commit(voutput_info->resource);
1225 return TDM_ERROR_NONE;
1229 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
1231 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
1232 tdm_server_voutput_buffer *vb = NULL;
1233 tdm_voutput *voutput;
1234 tdm_error ret = TDM_ERROR_NONE;
1236 TDM_RETURN_IF_FAIL(voutput_info != NULL);
1238 voutput = voutput_info->voutput;
1241 ret = tdm_voutput_destroy(voutput);
1242 if (ret != TDM_ERROR_NONE)
1243 TDM_ERR("_tdm_voutput_cb_destroy fail");
1245 if (voutput_info->attach_buffer) {
1246 tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
1247 tbm_surface_internal_unref(buffer);
1248 voutput_info->committing = 0;
1249 voutput_info->attach_buffer = NULL;
1252 LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
1254 wl_resource_destroy(vb->wl_buffer);
1257 LIST_DEL(&voutput_info->link);
1259 /* Do free your own resource */
1264 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
1266 struct wl_resource *voutput_resource = NULL;
1267 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1268 tdm_server_voutput_info *voutput_info;
1269 tdm_voutput *voutput;
1273 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1275 TDM_ERR("There is '%s' output, cannot create.", name);
1276 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1277 "There is '%s' output", name);
1281 voutput = tdm_voutput_create(private_server->private_loop->dpy, name, &ret);
1283 TDM_ERR("voutput creation fail(%s)(%d).", name, ret);
1284 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
1285 "%s output creation fail", name);
1289 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
1291 TDM_ERR("There is no '%s' output.", name);
1292 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1293 "There is '%s' output", name);
1298 wl_resource_create(client, &wl_tdm_voutput_interface,
1299 wl_resource_get_version(resource), id);
1300 if (!voutput_resource) {
1301 /* LCOV_EXCL_START */
1303 wl_resource_post_no_memory(resource);
1304 TDM_ERR("wl_resource_create failed");
1307 /* LCOV_EXCL_STOP */
1310 voutput_info = calloc(1, sizeof * voutput_info);
1311 if (!voutput_info) {
1312 /* LCOV_EXCL_START */
1314 wl_resource_post_no_memory(resource);
1315 wl_resource_destroy(voutput_resource);
1316 TDM_ERR("alloc failed");
1319 /* LCOV_EXCL_STOP */
1322 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
1323 voutput_info->private_server = private_server;
1324 voutput_info->resource = voutput_resource;
1325 voutput_info->voutput = voutput;
1326 voutput_info->output = output;
1327 LIST_INITHEAD(&voutput_info->buffer_list);
1329 wl_resource_set_implementation(voutput_resource,
1330 &tdm_voutput_implementation,
1332 tdm_voutput_cb_resource_destroy);
1334 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
1337 /* LCOV_EXCL_START */
1339 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
1341 tdm_private_server *private_server = wl_resource_get_user_data(resource);
1342 tdm_private_loop *private_loop = private_server->private_loop;
1343 char message[TDM_SERVER_REPLY_MSG_LEN];
1345 int len = sizeof(message), size;
1348 wl_client_get_credentials(client, NULL, &uid, NULL);
1351 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
1352 TDM_ERR("%s", message);
1354 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
1357 size = sizeof(message) - len;
1360 wl_client_flush(client);
1363 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
1364 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
1366 strncpy(buffer, m, copylen);
1370 buffer[copylen] = '\0';
1372 wl_tdm_send_debug_message(resource, buffer);
1375 wl_tdm_send_debug_done(resource);
1377 /* LCOV_EXCL_STOP */
1379 static const struct wl_tdm_interface tdm_implementation = {
1380 _tdm_server_cb_debug,
1381 _tdm_server_cb_create_output,
1382 _tdm_server_cb_create_virtual_output
1386 destroy_client(struct wl_resource *resource)
1388 tdm_server_client_info *c = NULL, *cc = NULL;
1389 struct wl_client *client;
1391 client = wl_resource_get_client(resource);
1392 TDM_RETURN_IF_FAIL(client != NULL);
1394 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1395 if (c->resource == resource) {
1404 _tdm_server_bind(struct wl_client *client, void *data,
1405 uint32_t version, uint32_t id)
1407 struct wl_resource *resource;
1408 tdm_server_client_info *cinfo;
1410 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1412 /* LCOV_EXCL_START */
1414 wl_client_post_no_memory(client);
1417 /* LCOV_EXCL_STOP */
1420 cinfo = calloc(1, sizeof(tdm_server_client_info));
1422 /* LCOV_EXCL_START */
1424 wl_client_post_no_memory(client);
1425 wl_resource_destroy(resource);
1428 /* LCOV_EXCL_STOP */
1431 cinfo->resource = resource;
1433 LIST_ADDTAIL(&cinfo->link, &client_list);
1434 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1435 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1437 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1441 tdm_server_init(tdm_private_loop *private_loop)
1443 tdm_private_server *private_server;
1445 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1446 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1448 if (private_loop->private_server)
1449 return TDM_ERROR_NONE;
1451 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1452 /* LCOV_EXCL_START */
1454 TDM_ERR("createing a tdm-socket failed");
1455 return TDM_ERROR_OPERATION_FAILED;
1457 /* LCOV_EXCL_STOP */
1460 private_server = calloc(1, sizeof * private_server);
1461 if (!private_server) {
1462 TDM_ERR("alloc failed");
1463 return TDM_ERROR_OUT_OF_MEMORY;
1466 LIST_INITHEAD(&private_server->output_list);
1467 LIST_INITHEAD(&private_server->voutput_list);
1468 LIST_INITHEAD(&private_server->wait_list);
1470 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1471 private_server, _tdm_server_bind)) {
1472 /* LCOV_EXCL_START */
1474 TDM_ERR("creating a global resource failed");
1475 free(private_server);
1476 return TDM_ERROR_OUT_OF_MEMORY;
1478 /* LCOV_EXCL_STOP */
1481 private_server->private_loop = private_loop;
1482 private_loop->private_server = private_server;
1483 keep_private_server = private_server;
1485 LIST_INITHEAD(&client_list);
1487 return TDM_ERROR_NONE;
1491 tdm_server_deinit(tdm_private_loop *private_loop)
1493 tdm_server_output_info *o = NULL, *oo = NULL;
1494 tdm_server_voutput_info *vo = NULL, *voo = NULL;
1495 tdm_server_wait_info *w = NULL, *ww = NULL;
1496 tdm_server_client_info *c = NULL, *cc = NULL;
1497 tdm_private_server *private_server;
1499 if (!private_loop->private_server)
1502 private_server = private_loop->private_server;
1504 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1508 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1509 wl_resource_destroy(o->resource);
1512 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1513 wl_resource_destroy(vo->resource);
1516 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1517 wl_resource_destroy(c->resource);
1520 free(private_server);
1521 private_loop->private_server = NULL;
1522 keep_private_server = NULL;
1525 /* LCOV_EXCL_START */
1527 tdm_server_get_client_name(pid_t pid)
1529 tdm_server_client_info *c = NULL;
1531 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1533 return (const char*)c->name;
1538 /* LCOV_EXCL_STOP */
1540 /* LCOV_EXCL_START */
1542 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1544 tdm_private_server *private_server = keep_private_server;
1545 tdm_server_output_info *output_info = NULL;
1547 if (!keep_private_server)
1548 return TDM_ERROR_NONE;
1550 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1551 tdm_server_vblank_info *vblank_info = NULL;
1553 if (output && output_info->output != output)
1556 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1557 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1561 return TDM_ERROR_NONE;
1563 /* LCOV_EXCL_STOP */