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 #include <tdm-server-protocol.h>
42 #include "tdm_private.h"
45 * - tdm server doesn't care about thread things.
46 * - DO NOT use the TDM internal functions here.
47 * However, the internal function which does lock/unlock the mutex of
48 * private_display in itself can be called.
49 * - DO NOT use the tdm_private_display structure here.
50 * - The callback function things can be called in main thread.
53 struct _tdm_private_server {
54 tdm_private_loop *private_loop;
55 struct list_head output_list;
56 struct list_head voutput_list;
57 struct list_head wait_list;
60 typedef struct _tdm_server_output_info {
61 struct list_head link;
62 tdm_private_server *private_server;
63 struct wl_resource *resource;
65 struct list_head vblank_list;
66 unsigned int watch_output_changes;
67 } tdm_server_output_info;
69 typedef struct _tdm_server_voutput_info {
70 struct list_head link;
71 tdm_private_server *private_server;
72 struct wl_resource *resource;
74 struct list_head output_list;
75 } tdm_server_voutput_info;
77 typedef struct _tdm_server_vblank_info {
78 struct list_head link;
79 tdm_server_output_info *output_info;
80 struct wl_resource *resource;
84 } tdm_server_vblank_info;
86 typedef struct _tdm_server_wait_info {
87 struct list_head link;
88 tdm_server_vblank_info *vblank_info;
92 } tdm_server_wait_info;
94 typedef struct _tdm_server_client_info {
95 struct list_head link;
97 char name[TDM_NAME_LEN];
98 struct wl_resource *resource;
99 } tdm_server_client_info;
101 static tdm_private_server *keep_private_server;
102 static struct list_head client_list;
104 static void destroy_wait(tdm_server_wait_info *wait_info);
107 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
109 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
113 snprintf(name, size, "Unknown");
115 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
116 h = fopen(proc, "r");
120 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
122 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
125 pname[len - 1] = '\0';
127 strncpy(name, pname, size - 1);
128 name[size - 1] = '\0';
133 /* LCOV_EXCL_START */
135 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
136 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
138 tdm_server_wait_info *found;
139 tdm_server_vblank_info *vblank_info;
141 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
143 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
144 tdm_server_wait_info, link, found);
146 TDM_ERR("wait_info(%p) is destroyed", wait_info);
150 if (tdm_debug_module & TDM_DEBUG_VBLANK)
151 TDM_DBG("req_id(%d) done", wait_info->req_id);
153 vblank_info = wait_info->vblank_info;
155 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
156 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
158 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
159 sequence, tv_sec, tv_usec, error);
161 destroy_wait(wait_info);
165 /* LCOV_EXCL_START */
167 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
168 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
170 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
174 /* LCOV_EXCL_START */
176 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
177 tdm_value value, void *user_data)
179 tdm_server_output_info *output_info = user_data;
180 struct wl_client *client;
183 TDM_RETURN_IF_FAIL(output_info != NULL);
185 client = wl_resource_get_client(output_info->resource);
186 TDM_RETURN_IF_FAIL(client != NULL);
188 wl_client_get_credentials(client, &pid, NULL, NULL);
190 if (!output_info->watch_output_changes) {
191 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
195 TDM_DBG("send the output changes: %d", (unsigned int)pid);
198 case TDM_OUTPUT_CHANGE_DPMS:
199 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
201 case TDM_OUTPUT_CHANGE_CONNECTION:
202 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
211 destroy_wait(tdm_server_wait_info *wait_info)
213 LIST_DEL(&wait_info->link);
218 destroy_vblank_callback(struct wl_resource *resource)
220 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
221 tdm_server_wait_info *w = NULL, *ww = NULL;
223 TDM_RETURN_IF_FAIL(vblank_info != NULL);
225 LIST_DEL(&vblank_info->link);
227 if (vblank_info->vblank)
228 tdm_vblank_destroy(vblank_info->vblank);
230 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
231 if (w->vblank_info == vblank_info)
238 /* LCOV_EXCL_START */
240 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
242 wl_resource_destroy(resource);
246 /* LCOV_EXCL_START */
248 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
250 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
253 ret = tdm_vblank_set_name(vblank_info->vblank, name);
254 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
258 /* LCOV_EXCL_START */
260 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
262 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
265 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
266 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
270 /* LCOV_EXCL_START */
272 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
274 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
277 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
278 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
283 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
285 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
288 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
289 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
293 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
294 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
296 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
297 tdm_server_output_info *output_info = vblank_info->output_info;
298 tdm_private_server *private_server = output_info->private_server;
299 tdm_server_wait_info *wait_info;
300 unsigned int enable_fake = 0;
303 wait_info = calloc(1, sizeof * wait_info);
305 /* LCOV_EXCL_START */
307 TDM_ERR("alloc failed");
308 ret = TDM_ERROR_OUT_OF_MEMORY;
314 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
315 wait_info->vblank_info = vblank_info;
316 wait_info->req_id = req_id;
317 wait_info->req_time = TDM_TIME(req_sec, req_usec);
319 if (tdm_debug_module & TDM_DEBUG_VBLANK)
320 TDM_DBG("req_id(%d) wait", req_id);
322 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
323 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
325 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
327 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
328 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
331 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
335 /* LCOV_EXCL_START */
337 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
339 destroy_wait(wait_info);
345 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
346 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
348 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
349 tdm_server_output_info *output_info = vblank_info->output_info;
350 tdm_private_server *private_server = output_info->private_server;
351 tdm_server_wait_info *wait_info;
352 unsigned int enable_fake = 0;
355 wait_info = calloc(1, sizeof * wait_info);
357 /* LCOV_EXCL_START */
359 TDM_ERR("alloc failed");
360 ret = TDM_ERROR_OUT_OF_MEMORY;
366 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
367 wait_info->vblank_info = vblank_info;
368 wait_info->req_id = req_id;
369 wait_info->req_time = TDM_TIME(req_sec, req_usec);
371 if (tdm_debug_module & TDM_DEBUG_VBLANK)
372 TDM_DBG("req_id(%d) wait", req_id);
374 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
375 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
377 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
379 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
380 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
383 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
387 /* LCOV_EXCL_START */
389 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
391 destroy_wait(wait_info);
396 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
397 _tdm_server_vblank_cb_destroy,
398 _tdm_server_vblank_cb_set_name,
399 _tdm_server_vblank_cb_set_fps,
400 _tdm_server_vblank_cb_set_offset,
401 _tdm_server_vblank_cb_set_enable_fake,
402 _tdm_server_vblank_cb_wait_vblank,
403 _tdm_server_vblank_cb_wait_vblank_seq,
406 /* LCOV_EXCL_START */
408 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
410 wl_resource_destroy(resource);
415 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
417 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
418 tdm_private_server *private_server = output_info->private_server;
419 tdm_private_loop *private_loop = private_server->private_loop;
420 struct wl_resource *vblank_resource;
422 tdm_server_vblank_info *vblank_info;
425 wl_resource_create(client, &wl_tdm_vblank_interface,
426 wl_resource_get_version(resource), id);
427 if (!vblank_resource) {
428 /* LCOV_EXCL_START */
430 wl_resource_post_no_memory(resource);
431 TDM_ERR("wl_resource_create failed");
437 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
439 /* LCOV_EXCL_START */
441 wl_resource_post_no_memory(resource);
442 wl_resource_destroy(vblank_resource);
443 TDM_ERR("tdm_vblank_create failed");
449 vblank_info = calloc(1, sizeof * vblank_info);
451 /* LCOV_EXCL_START */
453 wl_resource_post_no_memory(resource);
454 wl_resource_destroy(vblank_resource);
455 tdm_vblank_destroy(vblank);
456 TDM_ERR("alloc failed");
462 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
463 vblank_info->output_info = output_info;
464 vblank_info->resource = vblank_resource;
465 vblank_info->vblank = vblank;
466 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
468 tdm_vblank_set_resource(vblank, vblank_resource);
470 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
471 vblank_info, destroy_vblank_callback);
473 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
475 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
476 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
477 if (output == output_info->output)
478 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
484 /* LCOV_EXCL_START */
486 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
488 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
490 TDM_RETURN_IF_FAIL(output_info != NULL);
492 output_info->watch_output_changes = enable;
497 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
499 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
500 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
503 TDM_RETURN_IF_FAIL(output_info != NULL);
505 ret = tdm_output_get_conn_status(output_info->output, &status);
506 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
508 wl_tdm_output_send_connection(output_info->resource, status, ret);
513 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
517 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
519 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
520 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
523 TDM_RETURN_IF_FAIL(output_info != NULL);
525 ret = tdm_output_get_conn_status(output_info->output, &status);
526 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
528 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
529 const tdm_output_mode *mode = NULL;
530 unsigned int hdisplay, vdisplay, vrefresh;
532 ret = tdm_output_get_mode(output_info->output, &mode);
533 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
535 hdisplay = (mode) ? mode->hdisplay : 0;
536 vdisplay = (mode) ? mode->vdisplay : 0;
537 vrefresh = (mode) ? mode->vrefresh : 0;
539 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
541 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
546 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
550 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
552 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
553 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
556 TDM_RETURN_IF_FAIL(output_info != NULL);
558 ret = tdm_output_get_conn_status(output_info->output, &status);
559 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
561 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
562 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
564 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
565 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
567 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
569 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
574 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
577 static const struct wl_tdm_output_interface tdm_output_implementation = {
578 _tdm_server_output_cb_destroy,
579 _tdm_server_output_cb_create_vblank,
580 _tdm_server_output_cb_watch_output_changes,
581 _tdm_server_output_cb_get_connection,
582 _tdm_server_output_cb_get_mode,
583 _tdm_server_output_cb_get_dpms,
587 destroy_output_callback(struct wl_resource *resource)
589 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
590 tdm_server_vblank_info *v = NULL, *vv = NULL;
592 TDM_RETURN_IF_FAIL(output_info != NULL);
594 LIST_DEL(&output_info->link);
596 tdm_output_remove_change_handler(output_info->output,
597 _tdm_server_cb_output_change, output_info);
599 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
600 wl_resource_destroy(v->resource);
607 destroy_voutput_callback(struct wl_resource *resource)
609 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
611 TDM_RETURN_IF_FAIL(voutput_info != NULL);
613 LIST_DEL(&voutput_info->link);
619 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
620 const char *name, uint32_t id)
622 tdm_private_server *private_server = wl_resource_get_user_data(resource);
623 tdm_server_output_info *output_info;
624 struct wl_resource *output_resource = NULL;
626 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
629 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
631 /* LCOV_EXCL_START */
633 TDM_ERR("There is no '%s' output", name);
634 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
635 "There is no '%s' output", name);
642 wl_resource_create(client, &wl_tdm_output_interface,
643 wl_resource_get_version(resource), id);
644 if (!output_resource) {
645 /* LCOV_EXCL_START */
647 wl_resource_post_no_memory(resource);
648 TDM_ERR("wl_resource_create failed");
654 output_info = calloc(1, sizeof * output_info);
656 /* LCOV_EXCL_START */
658 wl_resource_post_no_memory(resource);
659 wl_resource_destroy(output_resource);
660 TDM_ERR("alloc failed");
666 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
667 if (ret != TDM_ERROR_NONE) {
668 wl_resource_post_no_memory(resource);
669 wl_resource_destroy(output_resource);
671 TDM_ERR("tdm_output_add_change_handler failed");
675 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
676 output_info->private_server = private_server;
677 output_info->resource = output_resource;
678 output_info->output = output;
679 LIST_INITHEAD(&output_info->vblank_list);
681 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
682 output_info, destroy_output_callback);
684 ret = tdm_output_get_conn_status(output, &status);
685 wl_tdm_output_send_connection(output_resource, status, ret);
687 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
688 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
689 const tdm_output_mode *mode = NULL;
690 unsigned int hdisplay, vdisplay, vrefresh;
692 ret = tdm_output_get_mode(output, &mode);
693 hdisplay = (mode) ? mode->hdisplay : 0;
694 vdisplay = (mode) ? mode->vdisplay : 0;
695 vrefresh = (mode) ? mode->vrefresh : 0;
696 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
698 ret = tdm_output_get_dpms(output, &dpms_value);
699 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
701 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
702 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
707 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
709 struct wl_resource *voutput_resource = NULL;
710 tdm_private_server *private_server = wl_resource_get_user_data(resource);
711 tdm_server_voutput_info *voutput_info;
715 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
717 TDM_ERR("There is '%s' output, cannot create.", name);
718 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
719 "There is '%s' output", name);
723 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
725 TDM_ERR("output creation fail(%s)(%d).", name, ret);
726 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
727 "%s output creation fail", name);
732 wl_resource_create(client, &wl_tdm_voutput_interface,
733 wl_resource_get_version(resource), id);
734 if (!voutput_resource) {
735 /* LCOV_EXCL_START */
737 wl_resource_post_no_memory(resource);
738 TDM_ERR("wl_resource_create failed");
744 voutput_info = calloc(1, sizeof * voutput_info);
746 /* LCOV_EXCL_START */
748 wl_resource_post_no_memory(resource);
749 wl_resource_destroy(voutput_resource);
750 TDM_ERR("alloc failed");
756 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
757 voutput_info->private_server = private_server;
758 voutput_info->resource = voutput_resource;
759 voutput_info->output = output;
760 LIST_INITHEAD(&voutput_info->output_list);
762 wl_resource_set_implementation(voutput_resource, &tdm_voutput_implementation,
763 voutput_info, destroy_voutput_callback);
765 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
768 /* LCOV_EXCL_START */
770 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
772 tdm_private_server *private_server = wl_resource_get_user_data(resource);
773 tdm_private_loop *private_loop = private_server->private_loop;
774 char message[TDM_SERVER_REPLY_MSG_LEN];
776 int len = sizeof(message), size;
779 wl_client_get_credentials(client, NULL, &uid, NULL);
782 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
783 TDM_ERR("%s", message);
785 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
788 size = sizeof(message) - len;
791 wl_client_flush(client);
794 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
795 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
797 strncpy(buffer, m, copylen);
801 buffer[copylen] = '\0';
803 wl_tdm_send_debug_message(resource, buffer);
806 wl_tdm_send_debug_done(resource);
810 static const struct wl_tdm_interface tdm_implementation = {
811 _tdm_server_cb_debug,
812 _tdm_server_cb_create_output,
813 _tdm_server_cb_create_virtual_output
817 destroy_client(struct wl_resource *resource)
819 tdm_server_client_info *c = NULL, *cc = NULL;
820 struct wl_client *client;
822 client = wl_resource_get_client(resource);
823 TDM_RETURN_IF_FAIL(client != NULL);
825 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
826 if (c->resource == resource) {
835 _tdm_server_bind(struct wl_client *client, void *data,
836 uint32_t version, uint32_t id)
838 struct wl_resource *resource;
839 tdm_server_client_info *cinfo;
841 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
843 /* LCOV_EXCL_START */
845 wl_client_post_no_memory(client);
851 cinfo = calloc(1, sizeof(tdm_server_client_info));
853 /* LCOV_EXCL_START */
855 wl_client_post_no_memory(client);
856 wl_resource_destroy(resource);
862 cinfo->resource = resource;
864 LIST_ADDTAIL(&cinfo->link, &client_list);
865 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
866 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
868 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
872 tdm_server_init(tdm_private_loop *private_loop)
874 tdm_private_server *private_server;
876 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
877 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
879 if (private_loop->private_server)
880 return TDM_ERROR_NONE;
882 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
883 /* LCOV_EXCL_START */
885 TDM_ERR("createing a tdm-socket failed");
886 return TDM_ERROR_OPERATION_FAILED;
891 private_server = calloc(1, sizeof * private_server);
892 if (!private_server) {
893 TDM_ERR("alloc failed");
894 return TDM_ERROR_OUT_OF_MEMORY;
897 LIST_INITHEAD(&private_server->output_list);
898 LIST_INITHEAD(&private_server->voutput_list);
899 LIST_INITHEAD(&private_server->wait_list);
901 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
902 private_server, _tdm_server_bind)) {
903 /* LCOV_EXCL_START */
905 TDM_ERR("creating a global resource failed");
906 free(private_server);
907 return TDM_ERROR_OUT_OF_MEMORY;
912 private_server->private_loop = private_loop;
913 private_loop->private_server = private_server;
914 keep_private_server = private_server;
916 LIST_INITHEAD(&client_list);
918 return TDM_ERROR_NONE;
922 tdm_server_deinit(tdm_private_loop *private_loop)
924 tdm_server_output_info *o = NULL, *oo = NULL;
925 // tdm_server_voutput_info *vo = NULL, *voo = NULL;
926 tdm_server_wait_info *w = NULL, *ww = NULL;
927 tdm_server_client_info *c = NULL, *cc = NULL;
928 tdm_private_server *private_server;
930 if (!private_loop->private_server)
933 private_server = private_loop->private_server;
935 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
939 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
940 wl_resource_destroy(o->resource);
943 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
944 wl_resource_destroy(vo->resource);
947 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
948 wl_resource_destroy(c->resource);
951 free(private_server);
952 private_loop->private_server = NULL;
953 keep_private_server = NULL;
956 /* LCOV_EXCL_START */
958 tdm_server_get_client_name(pid_t pid)
960 tdm_server_client_info *c = NULL;
962 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
964 return (const char*)c->name;
971 /* LCOV_EXCL_START */
973 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
975 tdm_private_server *private_server = keep_private_server;
976 tdm_server_output_info *output_info = NULL;
978 if (!keep_private_server)
979 return TDM_ERROR_NONE;
981 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
982 tdm_server_vblank_info *vblank_info = NULL;
984 if (output && output_info->output != output)
987 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
988 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
992 return TDM_ERROR_NONE;