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;
76 tdm_output_conn_status status;
80 tdm_output_mode *modes;
89 unsigned int mmheight;
90 } tdm_server_voutput_info;
92 typedef struct _tdm_server_vblank_info {
93 struct list_head link;
94 tdm_server_output_info *output_info;
95 struct wl_resource *resource;
99 } tdm_server_vblank_info;
101 typedef struct _tdm_server_wait_info {
102 struct list_head link;
103 tdm_server_vblank_info *vblank_info;
107 } tdm_server_wait_info;
109 typedef struct _tdm_server_client_info {
110 struct list_head link;
112 char name[TDM_NAME_LEN];
113 struct wl_resource *resource;
114 } tdm_server_client_info;
116 static tdm_private_server *keep_private_server;
117 static struct list_head client_list;
119 static void destroy_wait(tdm_server_wait_info *wait_info);
122 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
124 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
128 snprintf(name, size, "Unknown");
130 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
131 h = fopen(proc, "r");
135 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
137 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
140 pname[len - 1] = '\0';
142 strncpy(name, pname, size - 1);
143 name[size - 1] = '\0';
148 /* LCOV_EXCL_START */
150 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
151 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
153 tdm_server_wait_info *found;
154 tdm_server_vblank_info *vblank_info;
156 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
158 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
159 tdm_server_wait_info, link, found);
161 TDM_ERR("wait_info(%p) is destroyed", wait_info);
165 if (tdm_debug_module & TDM_DEBUG_VBLANK)
166 TDM_DBG("req_id(%d) done", wait_info->req_id);
168 vblank_info = wait_info->vblank_info;
170 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
171 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
173 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
174 sequence, tv_sec, tv_usec, error);
176 destroy_wait(wait_info);
180 /* LCOV_EXCL_START */
182 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
183 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
185 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
189 /* LCOV_EXCL_START */
191 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
192 tdm_value value, void *user_data)
194 tdm_server_output_info *output_info = user_data;
195 struct wl_client *client;
198 TDM_RETURN_IF_FAIL(output_info != NULL);
200 client = wl_resource_get_client(output_info->resource);
201 TDM_RETURN_IF_FAIL(client != NULL);
203 wl_client_get_credentials(client, &pid, NULL, NULL);
205 if (!output_info->watch_output_changes) {
206 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
210 TDM_DBG("send the output changes: %d", (unsigned int)pid);
213 case TDM_OUTPUT_CHANGE_DPMS:
214 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
216 case TDM_OUTPUT_CHANGE_CONNECTION:
217 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
226 destroy_wait(tdm_server_wait_info *wait_info)
228 LIST_DEL(&wait_info->link);
233 destroy_vblank_callback(struct wl_resource *resource)
235 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
236 tdm_server_wait_info *w = NULL, *ww = NULL;
238 TDM_RETURN_IF_FAIL(vblank_info != NULL);
240 LIST_DEL(&vblank_info->link);
242 if (vblank_info->vblank)
243 tdm_vblank_destroy(vblank_info->vblank);
245 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
246 if (w->vblank_info == vblank_info)
253 /* LCOV_EXCL_START */
255 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
257 wl_resource_destroy(resource);
261 /* LCOV_EXCL_START */
263 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
265 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
268 ret = tdm_vblank_set_name(vblank_info->vblank, name);
269 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
273 /* LCOV_EXCL_START */
275 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
277 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
280 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
281 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
285 /* LCOV_EXCL_START */
287 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
289 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
292 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
293 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
298 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
300 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
303 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
304 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
308 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
309 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
311 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
312 tdm_server_output_info *output_info = vblank_info->output_info;
313 tdm_private_server *private_server = output_info->private_server;
314 tdm_server_wait_info *wait_info;
315 unsigned int enable_fake = 0;
318 wait_info = calloc(1, sizeof * wait_info);
320 /* LCOV_EXCL_START */
322 TDM_ERR("alloc failed");
323 ret = TDM_ERROR_OUT_OF_MEMORY;
329 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
330 wait_info->vblank_info = vblank_info;
331 wait_info->req_id = req_id;
332 wait_info->req_time = TDM_TIME(req_sec, req_usec);
334 if (tdm_debug_module & TDM_DEBUG_VBLANK)
335 TDM_DBG("req_id(%d) wait", req_id);
337 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
338 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
340 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
342 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
343 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
346 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
350 /* LCOV_EXCL_START */
352 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
354 destroy_wait(wait_info);
360 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
361 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
363 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
364 tdm_server_output_info *output_info = vblank_info->output_info;
365 tdm_private_server *private_server = output_info->private_server;
366 tdm_server_wait_info *wait_info;
367 unsigned int enable_fake = 0;
370 wait_info = calloc(1, sizeof * wait_info);
372 /* LCOV_EXCL_START */
374 TDM_ERR("alloc failed");
375 ret = TDM_ERROR_OUT_OF_MEMORY;
381 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
382 wait_info->vblank_info = vblank_info;
383 wait_info->req_id = req_id;
384 wait_info->req_time = TDM_TIME(req_sec, req_usec);
386 if (tdm_debug_module & TDM_DEBUG_VBLANK)
387 TDM_DBG("req_id(%d) wait", req_id);
389 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
390 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
392 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
394 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
395 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
398 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
402 /* LCOV_EXCL_START */
404 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
406 destroy_wait(wait_info);
411 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
412 _tdm_server_vblank_cb_destroy,
413 _tdm_server_vblank_cb_set_name,
414 _tdm_server_vblank_cb_set_fps,
415 _tdm_server_vblank_cb_set_offset,
416 _tdm_server_vblank_cb_set_enable_fake,
417 _tdm_server_vblank_cb_wait_vblank,
418 _tdm_server_vblank_cb_wait_vblank_seq,
421 /* LCOV_EXCL_START */
423 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
425 wl_resource_destroy(resource);
430 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
432 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
433 tdm_private_server *private_server = output_info->private_server;
434 tdm_private_loop *private_loop = private_server->private_loop;
435 struct wl_resource *vblank_resource;
437 tdm_server_vblank_info *vblank_info;
440 wl_resource_create(client, &wl_tdm_vblank_interface,
441 wl_resource_get_version(resource), id);
442 if (!vblank_resource) {
443 /* LCOV_EXCL_START */
445 wl_resource_post_no_memory(resource);
446 TDM_ERR("wl_resource_create failed");
452 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
454 /* LCOV_EXCL_START */
456 wl_resource_post_no_memory(resource);
457 wl_resource_destroy(vblank_resource);
458 TDM_ERR("tdm_vblank_create failed");
464 vblank_info = calloc(1, sizeof * vblank_info);
466 /* LCOV_EXCL_START */
468 wl_resource_post_no_memory(resource);
469 wl_resource_destroy(vblank_resource);
470 tdm_vblank_destroy(vblank);
471 TDM_ERR("alloc failed");
477 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
478 vblank_info->output_info = output_info;
479 vblank_info->resource = vblank_resource;
480 vblank_info->vblank = vblank;
481 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
483 tdm_vblank_set_resource(vblank, vblank_resource);
485 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
486 vblank_info, destroy_vblank_callback);
488 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
490 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
491 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
492 if (output == output_info->output)
493 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
499 /* LCOV_EXCL_START */
501 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
503 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
505 TDM_RETURN_IF_FAIL(output_info != NULL);
507 output_info->watch_output_changes = enable;
512 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
514 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
515 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
518 TDM_RETURN_IF_FAIL(output_info != NULL);
520 ret = tdm_output_get_conn_status(output_info->output, &status);
521 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
523 wl_tdm_output_send_connection(output_info->resource, status, ret);
528 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
532 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
534 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
535 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
538 TDM_RETURN_IF_FAIL(output_info != NULL);
540 ret = tdm_output_get_conn_status(output_info->output, &status);
541 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
543 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
544 const tdm_output_mode *mode = NULL;
545 unsigned int hdisplay, vdisplay, vrefresh;
547 ret = tdm_output_get_mode(output_info->output, &mode);
548 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
550 hdisplay = (mode) ? mode->hdisplay : 0;
551 vdisplay = (mode) ? mode->vdisplay : 0;
552 vrefresh = (mode) ? mode->vrefresh : 0;
554 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
556 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
561 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
565 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
567 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
568 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
571 TDM_RETURN_IF_FAIL(output_info != NULL);
573 ret = tdm_output_get_conn_status(output_info->output, &status);
574 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
576 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
577 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
579 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
580 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
582 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
584 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
589 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
592 static const struct wl_tdm_output_interface tdm_output_implementation = {
593 _tdm_server_output_cb_destroy,
594 _tdm_server_output_cb_create_vblank,
595 _tdm_server_output_cb_watch_output_changes,
596 _tdm_server_output_cb_get_connection,
597 _tdm_server_output_cb_get_mode,
598 _tdm_server_output_cb_get_dpms,
602 destroy_output_callback(struct wl_resource *resource)
604 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
605 tdm_server_vblank_info *v = NULL, *vv = NULL;
607 TDM_RETURN_IF_FAIL(output_info != NULL);
609 LIST_DEL(&output_info->link);
611 tdm_output_remove_change_handler(output_info->output,
612 _tdm_server_cb_output_change, output_info);
614 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
615 wl_resource_destroy(v->resource);
622 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
623 const char *name, uint32_t id)
625 tdm_private_server *private_server = wl_resource_get_user_data(resource);
626 tdm_server_output_info *output_info;
627 struct wl_resource *output_resource = NULL;
629 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
632 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
634 /* LCOV_EXCL_START */
636 TDM_ERR("There is no '%s' output", name);
637 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
638 "There is no '%s' output", name);
645 wl_resource_create(client, &wl_tdm_output_interface,
646 wl_resource_get_version(resource), id);
647 if (!output_resource) {
648 /* LCOV_EXCL_START */
650 wl_resource_post_no_memory(resource);
651 TDM_ERR("wl_resource_create failed");
657 output_info = calloc(1, sizeof * output_info);
659 /* LCOV_EXCL_START */
661 wl_resource_post_no_memory(resource);
662 wl_resource_destroy(output_resource);
663 TDM_ERR("alloc failed");
669 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
670 if (ret != TDM_ERROR_NONE) {
671 wl_resource_post_no_memory(resource);
672 wl_resource_destroy(output_resource);
674 TDM_ERR("tdm_output_add_change_handler failed");
678 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
679 output_info->private_server = private_server;
680 output_info->resource = output_resource;
681 output_info->output = output;
682 LIST_INITHEAD(&output_info->vblank_list);
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 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
711 tdm_server_voutput_info *voutput_info;
712 tdm_private_server *private_server;
714 tdm_error ret = TDM_ERROR_NONE;
716 voutput_info = wl_resource_get_user_data(resource);
718 private_server = voutput_info->private_server;
719 output = voutput_info->output;
722 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
724 if (ret != TDM_ERROR_NONE)
725 TDM_ERR("_tdm_voutput_cb_destroy fail");
727 wl_resource_destroy(resource);
731 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
732 struct wl_resource *resource,
733 struct wl_array *modes)
735 tdm_server_voutput_info *voutput_info;
736 tdm_output_mode *mode;
737 int size, count = 0, i = 0;
739 voutput_info = wl_resource_get_user_data(resource);
741 voutput_info->available_modes.count = 0;
742 if (voutput_info->available_modes.modes)
743 free(voutput_info->available_modes.modes);
745 wl_array_for_each(mode, modes)
747 size = sizeof(tdm_output_mode);
749 voutput_info->available_modes.modes = malloc(count * size);
750 voutput_info->available_modes.count = count;
752 wl_array_for_each(mode, modes)
753 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
757 _tdm_voutput_cb_set_available_formats(struct wl_client *client,
758 struct wl_resource *resource,
759 struct wl_array *formats)
761 tdm_server_voutput_info *voutput_info;
763 int count = 0, i = 0;
765 voutput_info = wl_resource_get_user_data(resource);
767 voutput_info->available_formats.count = 0;
768 if (voutput_info->available_formats.formats)
769 free(voutput_info->available_formats.formats);
771 wl_array_for_each(f, formats)
774 voutput_info->available_formats.formats = malloc(count * sizeof(tbm_format));
775 voutput_info->available_formats.count = count;
777 wl_array_for_each(f, formats)
778 voutput_info->available_formats.formats[i++] = *f;
782 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
783 unsigned int mmwidth, unsigned int mmheight)
785 tdm_server_voutput_info *voutput_info;
787 voutput_info = wl_resource_get_user_data(resource);
789 voutput_info->mmwidth = mmwidth;
790 voutput_info->mmheight = mmheight;
794 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
796 tdm_server_voutput_info *voutput_info;
798 voutput_info = wl_resource_get_user_data(resource);
799 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
801 tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
802 tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
803 tdm_output_set_connect(voutput_info->output);
807 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
809 tdm_server_voutput_info *voutput_info;
811 voutput_info = wl_resource_get_user_data(resource);
812 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
814 /* Do free resources when it's being disconnected */
815 free(voutput_info->available_modes.modes);
816 voutput_info->available_modes.modes = NULL;
817 voutput_info->available_modes.count = 0;
818 voutput_info->mmwidth = 0;
819 voutput_info->mmheight = 0;
821 tdm_output_set_disconnect(voutput_info->output);
824 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
825 _tdm_voutput_cb_destroy,
826 _tdm_voutput_cb_set_available_modes,
827 _tdm_voutput_cb_set_available_formats,
828 _tdm_voutput_cb_set_physical_size,
829 _tdm_voutput_cb_connect,
830 _tdm_voutput_cb_disconnect
834 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
836 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
838 TDM_RETURN_IF_FAIL(voutput_info != NULL);
840 LIST_DEL(&voutput_info->link);
842 /* Do free your own resource */
847 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
849 struct wl_resource *voutput_resource = NULL;
850 tdm_private_server *private_server = wl_resource_get_user_data(resource);
851 tdm_server_voutput_info *voutput_info;
855 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
857 TDM_ERR("There is '%s' output, cannot create.", name);
858 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
859 "There is '%s' output", name);
863 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
865 TDM_ERR("output creation fail(%s)(%d).", name, ret);
866 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
867 "%s output creation fail", name);
872 wl_resource_create(client, &wl_tdm_voutput_interface,
873 wl_resource_get_version(resource), id);
874 if (!voutput_resource) {
875 /* LCOV_EXCL_START */
877 wl_resource_post_no_memory(resource);
878 TDM_ERR("wl_resource_create failed");
884 voutput_info = calloc(1, sizeof * voutput_info);
886 /* LCOV_EXCL_START */
888 wl_resource_post_no_memory(resource);
889 wl_resource_destroy(voutput_resource);
890 TDM_ERR("alloc failed");
896 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
897 voutput_info->private_server = private_server;
898 voutput_info->resource = voutput_resource;
899 voutput_info->output = output;
900 LIST_INITHEAD(&voutput_info->output_list);
902 wl_resource_set_implementation(voutput_resource,
903 &tdm_voutput_implementation,
905 tdm_voutput_cb_resource_destroy);
907 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
910 /* LCOV_EXCL_START */
912 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
914 tdm_private_server *private_server = wl_resource_get_user_data(resource);
915 tdm_private_loop *private_loop = private_server->private_loop;
916 char message[TDM_SERVER_REPLY_MSG_LEN];
918 int len = sizeof(message), size;
921 wl_client_get_credentials(client, NULL, &uid, NULL);
924 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
925 TDM_ERR("%s", message);
927 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
930 size = sizeof(message) - len;
933 wl_client_flush(client);
936 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
937 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
939 strncpy(buffer, m, copylen);
943 buffer[copylen] = '\0';
945 wl_tdm_send_debug_message(resource, buffer);
948 wl_tdm_send_debug_done(resource);
952 static const struct wl_tdm_interface tdm_implementation = {
953 _tdm_server_cb_debug,
954 _tdm_server_cb_create_output,
955 _tdm_server_cb_create_virtual_output
959 destroy_client(struct wl_resource *resource)
961 tdm_server_client_info *c = NULL, *cc = NULL;
962 struct wl_client *client;
964 client = wl_resource_get_client(resource);
965 TDM_RETURN_IF_FAIL(client != NULL);
967 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
968 if (c->resource == resource) {
977 _tdm_server_bind(struct wl_client *client, void *data,
978 uint32_t version, uint32_t id)
980 struct wl_resource *resource;
981 tdm_server_client_info *cinfo;
983 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
985 /* LCOV_EXCL_START */
987 wl_client_post_no_memory(client);
993 cinfo = calloc(1, sizeof(tdm_server_client_info));
995 /* LCOV_EXCL_START */
997 wl_client_post_no_memory(client);
998 wl_resource_destroy(resource);
1001 /* LCOV_EXCL_STOP */
1004 cinfo->resource = resource;
1006 LIST_ADDTAIL(&cinfo->link, &client_list);
1007 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1008 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1010 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1014 tdm_server_init(tdm_private_loop *private_loop)
1016 tdm_private_server *private_server;
1018 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1019 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1021 if (private_loop->private_server)
1022 return TDM_ERROR_NONE;
1024 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1025 /* LCOV_EXCL_START */
1027 TDM_ERR("createing a tdm-socket failed");
1028 return TDM_ERROR_OPERATION_FAILED;
1030 /* LCOV_EXCL_STOP */
1033 private_server = calloc(1, sizeof * private_server);
1034 if (!private_server) {
1035 TDM_ERR("alloc failed");
1036 return TDM_ERROR_OUT_OF_MEMORY;
1039 LIST_INITHEAD(&private_server->output_list);
1040 LIST_INITHEAD(&private_server->voutput_list);
1041 LIST_INITHEAD(&private_server->wait_list);
1043 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1044 private_server, _tdm_server_bind)) {
1045 /* LCOV_EXCL_START */
1047 TDM_ERR("creating a global resource failed");
1048 free(private_server);
1049 return TDM_ERROR_OUT_OF_MEMORY;
1051 /* LCOV_EXCL_STOP */
1054 private_server->private_loop = private_loop;
1055 private_loop->private_server = private_server;
1056 keep_private_server = private_server;
1058 LIST_INITHEAD(&client_list);
1060 return TDM_ERROR_NONE;
1064 tdm_server_deinit(tdm_private_loop *private_loop)
1066 tdm_server_output_info *o = NULL, *oo = NULL;
1067 // tdm_server_voutput_info *vo = NULL, *voo = NULL;
1068 tdm_server_wait_info *w = NULL, *ww = NULL;
1069 tdm_server_client_info *c = NULL, *cc = NULL;
1070 tdm_private_server *private_server;
1072 if (!private_loop->private_server)
1075 private_server = private_loop->private_server;
1077 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1081 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1082 wl_resource_destroy(o->resource);
1085 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1086 wl_resource_destroy(vo->resource);
1089 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1090 wl_resource_destroy(c->resource);
1093 free(private_server);
1094 private_loop->private_server = NULL;
1095 keep_private_server = NULL;
1098 /* LCOV_EXCL_START */
1100 tdm_server_get_client_name(pid_t pid)
1102 tdm_server_client_info *c = NULL;
1104 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1106 return (const char*)c->name;
1111 /* LCOV_EXCL_STOP */
1113 /* LCOV_EXCL_START */
1115 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1117 tdm_private_server *private_server = keep_private_server;
1118 tdm_server_output_info *output_info = NULL;
1120 if (!keep_private_server)
1121 return TDM_ERROR_NONE;
1123 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1124 tdm_server_vblank_info *vblank_info = NULL;
1126 if (output && output_info->output != output)
1129 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1130 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1134 return TDM_ERROR_NONE;
1136 /* LCOV_EXCL_STOP */