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 <sc1.lim@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 wait_list;
59 typedef struct _tdm_server_output_info {
60 struct list_head link;
61 tdm_private_server *private_server;
62 struct wl_resource *resource;
64 struct list_head vblank_list;
65 unsigned int watch_output_changes;
66 } tdm_server_output_info;
68 typedef struct _tdm_server_vblank_info {
69 struct list_head link;
70 tdm_server_output_info *output_info;
71 struct wl_resource *resource;
75 } tdm_server_vblank_info;
77 typedef struct _tdm_server_wait_info {
78 struct list_head link;
79 tdm_server_vblank_info *vblank_info;
83 } tdm_server_wait_info;
85 typedef struct _tdm_server_client_info {
86 struct list_head link;
88 char name[TDM_NAME_LEN];
89 struct wl_resource *resource;
90 } tdm_server_client_info;
92 static tdm_private_server *keep_private_server;
93 static struct list_head client_list;
95 static void destroy_wait(tdm_server_wait_info *wait_info);
98 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
100 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
104 snprintf(name, size, "Unknown");
106 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
107 h = fopen(proc, "r");
111 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
113 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
116 pname[len - 1] = '\0';
118 strncpy(name, pname, size - 1);
119 name[size - 1] = '\0';
125 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
127 tdm_private_loop *private_loop = private_server->private_loop;
128 tdm_output *found = NULL;
130 if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
131 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
136 tdm_display_get_output_count(private_loop->dpy, &count);
138 for (i = 0; i < count; i++) {
139 tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
140 tdm_output_conn_status status;
141 const char *model = NULL;
144 ret = tdm_output_get_conn_status(output, &status);
145 if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
148 ret = tdm_output_get_model_info(output, NULL, &model, NULL);
152 if (strncmp(model, name, TDM_NAME_LEN))
163 /* LCOV_EXCL_START */
165 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
166 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
168 tdm_server_wait_info *found;
169 tdm_server_vblank_info *vblank_info;
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;
185 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
186 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
188 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
189 sequence, tv_sec, tv_usec, error);
191 destroy_wait(wait_info);
195 /* LCOV_EXCL_START */
197 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
198 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
200 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
204 /* LCOV_EXCL_START */
206 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
207 tdm_value value, void *user_data)
209 tdm_server_output_info *output_info = user_data;
210 struct wl_client *client;
213 TDM_RETURN_IF_FAIL(output_info != NULL);
215 client = wl_resource_get_client(output_info->resource);
216 TDM_RETURN_IF_FAIL(client != NULL);
218 wl_client_get_credentials(client, &pid, NULL, NULL);
220 if (!output_info->watch_output_changes) {
221 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
225 TDM_DBG("send the output changes: %d", (unsigned int)pid);
228 case TDM_OUTPUT_CHANGE_DPMS:
229 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
231 case TDM_OUTPUT_CHANGE_CONNECTION:
232 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
241 destroy_wait(tdm_server_wait_info *wait_info)
243 LIST_DEL(&wait_info->link);
248 destroy_vblank_callback(struct wl_resource *resource)
250 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
251 tdm_server_wait_info *w = NULL, *ww = NULL;
253 TDM_RETURN_IF_FAIL(vblank_info != NULL);
255 LIST_DEL(&vblank_info->link);
257 if (vblank_info->vblank)
258 tdm_vblank_destroy(vblank_info->vblank);
260 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
261 if (w->vblank_info == vblank_info)
268 /* LCOV_EXCL_START */
270 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
272 wl_resource_destroy(resource);
276 /* LCOV_EXCL_START */
278 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
280 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
282 tdm_vblank_set_name(vblank_info->vblank, name);
286 /* LCOV_EXCL_START */
288 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
290 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
292 tdm_vblank_set_fps(vblank_info->vblank, fps);
296 /* LCOV_EXCL_START */
298 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
300 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
302 tdm_vblank_set_offset(vblank_info->vblank, offset);
307 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
309 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
311 tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
315 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
316 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
318 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
319 tdm_server_output_info *output_info = vblank_info->output_info;
320 tdm_private_server *private_server = output_info->private_server;
321 tdm_server_wait_info *wait_info;
322 unsigned int enable_fake = 0;
325 wait_info = calloc(1, sizeof * wait_info);
327 /* LCOV_EXCL_START */
329 TDM_ERR("alloc failed");
330 ret = TDM_ERROR_OUT_OF_MEMORY;
336 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
337 wait_info->vblank_info = vblank_info;
338 wait_info->req_id = req_id;
339 wait_info->req_time = TDM_TIME(req_sec, req_usec);
341 if (tdm_debug_module & TDM_DEBUG_VBLANK)
342 TDM_DBG("req_id(%d) wait", req_id);
344 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
345 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
347 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
349 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
350 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
353 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
357 /* LCOV_EXCL_START */
359 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
361 destroy_wait(wait_info);
367 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
368 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
370 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
371 tdm_server_output_info *output_info = vblank_info->output_info;
372 tdm_private_server *private_server = output_info->private_server;
373 tdm_server_wait_info *wait_info;
374 unsigned int enable_fake = 0;
377 wait_info = calloc(1, sizeof * wait_info);
379 /* LCOV_EXCL_START */
381 TDM_ERR("alloc failed");
382 ret = TDM_ERROR_OUT_OF_MEMORY;
388 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
389 wait_info->vblank_info = vblank_info;
390 wait_info->req_id = req_id;
391 wait_info->req_time = TDM_TIME(req_sec, req_usec);
393 if (tdm_debug_module & TDM_DEBUG_VBLANK)
394 TDM_DBG("req_id(%d) wait", req_id);
396 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
397 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
399 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
401 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
402 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
405 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
409 /* LCOV_EXCL_START */
411 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
413 destroy_wait(wait_info);
418 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
419 _tdm_server_vblank_cb_destroy,
420 _tdm_server_vblank_cb_set_name,
421 _tdm_server_vblank_cb_set_fps,
422 _tdm_server_vblank_cb_set_offset,
423 _tdm_server_vblank_cb_set_enable_fake,
424 _tdm_server_vblank_cb_wait_vblank,
425 _tdm_server_vblank_cb_wait_vblank_seq,
428 /* LCOV_EXCL_START */
430 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
432 wl_resource_destroy(resource);
437 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
439 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
440 tdm_private_server *private_server = output_info->private_server;
441 tdm_private_loop *private_loop = private_server->private_loop;
442 struct wl_resource *vblank_resource;
444 tdm_server_vblank_info *vblank_info;
447 wl_resource_create(client, &wl_tdm_vblank_interface,
448 wl_resource_get_version(resource), id);
449 if (!vblank_resource) {
450 /* LCOV_EXCL_START */
452 wl_resource_post_no_memory(resource);
453 TDM_ERR("wl_resource_create failed");
459 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
461 /* LCOV_EXCL_START */
463 wl_resource_post_no_memory(resource);
464 wl_resource_destroy(vblank_resource);
465 TDM_ERR("tdm_vblank_create failed");
471 vblank_info = calloc(1, sizeof * vblank_info);
473 /* LCOV_EXCL_START */
475 wl_resource_post_no_memory(resource);
476 wl_resource_destroy(vblank_resource);
477 tdm_vblank_destroy(vblank);
478 TDM_ERR("alloc failed");
484 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
485 vblank_info->output_info = output_info;
486 vblank_info->resource = vblank_resource;
487 vblank_info->vblank = vblank;
488 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
490 tdm_vblank_set_resource(vblank, vblank_resource);
492 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
493 vblank_info, destroy_vblank_callback);
495 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
497 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
498 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
499 if (output == output_info->output)
500 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
506 /* LCOV_EXCL_START */
508 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
510 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
512 TDM_RETURN_IF_FAIL(output_info != NULL);
514 output_info->watch_output_changes = enable;
519 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
521 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
522 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
525 TDM_RETURN_IF_FAIL(output_info != NULL);
527 ret = tdm_output_get_conn_status(output_info->output, &status);
528 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
530 wl_tdm_output_send_connection(output_info->resource, status, ret);
535 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
539 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
541 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
542 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
545 TDM_RETURN_IF_FAIL(output_info != NULL);
547 ret = tdm_output_get_conn_status(output_info->output, &status);
548 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
550 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
551 const tdm_output_mode *mode = NULL;
552 unsigned int hdisplay, vdisplay, vrefresh;
554 ret = tdm_output_get_mode(output_info->output, &mode);
555 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
557 hdisplay = (mode) ? mode->hdisplay : 0;
558 vdisplay = (mode) ? mode->vdisplay : 0;
559 vrefresh = (mode) ? mode->vrefresh : 0;
561 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
563 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
568 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
572 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
574 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
575 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
578 TDM_RETURN_IF_FAIL(output_info != NULL);
580 ret = tdm_output_get_conn_status(output_info->output, &status);
581 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
583 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
584 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
586 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
587 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
589 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
591 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
596 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
599 static const struct wl_tdm_output_interface tdm_output_implementation = {
600 _tdm_server_output_cb_destroy,
601 _tdm_server_output_cb_create_vblank,
602 _tdm_server_output_cb_watch_output_changes,
603 _tdm_server_output_cb_get_connection,
604 _tdm_server_output_cb_get_mode,
605 _tdm_server_output_cb_get_dpms,
609 destroy_output_callback(struct wl_resource *resource)
611 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
612 tdm_server_vblank_info *v = NULL, *vv = NULL;
614 TDM_RETURN_IF_FAIL(output_info != NULL);
616 LIST_DEL(&output_info->link);
618 tdm_output_remove_change_handler(output_info->output,
619 _tdm_server_cb_output_change, output_info);
621 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
622 wl_resource_destroy(v->resource);
629 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
630 const char *name, uint32_t id)
632 tdm_private_server *private_server = wl_resource_get_user_data(resource);
633 tdm_server_output_info *output_info;
634 struct wl_resource *output_resource = NULL;
636 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
639 output = _tdm_server_find_output(private_server, name);
641 /* LCOV_EXCL_START */
643 TDM_ERR("There is no '%s' output", name);
644 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
645 "There is no '%s' output", name);
652 wl_resource_create(client, &wl_tdm_output_interface,
653 wl_resource_get_version(resource), id);
654 if (!output_resource) {
655 /* LCOV_EXCL_START */
657 wl_resource_post_no_memory(resource);
658 TDM_ERR("wl_resource_create failed");
664 output_info = calloc(1, sizeof * output_info);
666 /* LCOV_EXCL_START */
668 wl_resource_post_no_memory(resource);
669 wl_resource_destroy(output_resource);
670 TDM_ERR("alloc failed");
676 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
677 output_info->private_server = private_server;
678 output_info->resource = output_resource;
679 output_info->output = output;
680 LIST_INITHEAD(&output_info->vblank_list);
682 tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
684 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
685 output_info, destroy_output_callback);
687 ret = tdm_output_get_conn_status(output, &status);
688 wl_tdm_output_send_connection(output_resource, status, ret);
690 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
691 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
692 const tdm_output_mode *mode = NULL;
693 unsigned int hdisplay, vdisplay, vrefresh;
695 ret = tdm_output_get_mode(output, &mode);
696 hdisplay = (mode) ? mode->hdisplay : 0;
697 vdisplay = (mode) ? mode->vdisplay : 0;
698 vrefresh = (mode) ? mode->vrefresh : 0;
699 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
701 ret = tdm_output_get_dpms(output, &dpms_value);
702 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
704 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
705 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
709 /* LCOV_EXCL_START */
711 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
713 tdm_private_server *private_server = wl_resource_get_user_data(resource);
714 tdm_private_loop *private_loop = private_server->private_loop;
715 char message[TDM_SERVER_REPLY_MSG_LEN];
717 int len = sizeof(message), size;
720 wl_client_get_credentials(client, NULL, &uid, NULL);
723 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
724 TDM_ERR("%s", message);
726 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
729 size = sizeof(message) - len;
732 wl_client_flush(client);
735 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
736 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
738 strncpy(buffer, m, copylen);
742 buffer[copylen] = '\0';
744 wl_tdm_send_debug_message(resource, buffer);
747 wl_tdm_send_debug_done(resource);
751 static const struct wl_tdm_interface tdm_implementation = {
752 _tdm_server_cb_debug,
753 _tdm_server_cb_create_output,
757 destroy_client(struct wl_resource *resource)
759 tdm_server_client_info *c = NULL, *cc = NULL;
760 struct wl_client *client;
762 client = wl_resource_get_client(resource);
763 TDM_RETURN_IF_FAIL(client != NULL);
765 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
766 if (c->resource == resource) {
775 _tdm_server_bind(struct wl_client *client, void *data,
776 uint32_t version, uint32_t id)
778 struct wl_resource *resource;
779 tdm_server_client_info *cinfo;
781 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
783 /* LCOV_EXCL_START */
785 wl_client_post_no_memory(client);
791 cinfo = calloc(1, sizeof(tdm_server_client_info));
793 /* LCOV_EXCL_START */
795 wl_client_post_no_memory(client);
796 wl_resource_destroy(resource);
802 cinfo->resource = resource;
804 LIST_ADDTAIL(&cinfo->link, &client_list);
805 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
806 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
808 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
812 tdm_server_init(tdm_private_loop *private_loop)
814 tdm_private_server *private_server;
816 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
817 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
819 if (private_loop->private_server)
820 return TDM_ERROR_NONE;
822 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
823 /* LCOV_EXCL_START */
825 TDM_ERR("createing a tdm-socket failed");
826 return TDM_ERROR_OPERATION_FAILED;
831 private_server = calloc(1, sizeof * private_server);
832 if (!private_server) {
833 TDM_ERR("alloc failed");
834 return TDM_ERROR_OUT_OF_MEMORY;
837 LIST_INITHEAD(&private_server->output_list);
838 LIST_INITHEAD(&private_server->wait_list);
840 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
841 private_server, _tdm_server_bind)) {
842 /* LCOV_EXCL_START */
844 TDM_ERR("creating a global resource failed");
845 free(private_server);
846 return TDM_ERROR_OUT_OF_MEMORY;
851 private_server->private_loop = private_loop;
852 private_loop->private_server = private_server;
853 keep_private_server = private_server;
855 LIST_INITHEAD(&client_list);
857 return TDM_ERROR_NONE;
861 tdm_server_deinit(tdm_private_loop *private_loop)
863 tdm_server_output_info *o = NULL, *oo = NULL;
864 tdm_server_wait_info *w = NULL, *ww = NULL;
865 tdm_server_client_info *c = NULL, *cc = NULL;
866 tdm_private_server *private_server;
868 if (!private_loop->private_server)
871 private_server = private_loop->private_server;
873 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
877 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
878 wl_resource_destroy(o->resource);
881 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
882 wl_resource_destroy(c->resource);
885 free(private_server);
886 private_loop->private_server = NULL;
887 keep_private_server = NULL;
890 /* LCOV_EXCL_START */
892 tdm_server_get_client_name(pid_t pid)
894 tdm_server_client_info *c = NULL;
896 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
898 return (const char*)c->name;
905 /* LCOV_EXCL_START */
907 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
909 tdm_private_server *private_server = keep_private_server;
910 tdm_server_output_info *output_info = NULL;
912 if (!keep_private_server)
913 return TDM_ERROR_NONE;
915 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
916 tdm_server_vblank_info *vblank_info = NULL;
918 if (output && output_info->output != output)
921 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
922 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
926 return TDM_ERROR_NONE;