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;
84 unsigned int mmheight;
85 } tdm_server_voutput_info;
87 typedef struct _tdm_server_vblank_info {
88 struct list_head link;
89 tdm_server_output_info *output_info;
90 struct wl_resource *resource;
94 } tdm_server_vblank_info;
96 typedef struct _tdm_server_wait_info {
97 struct list_head link;
98 tdm_server_vblank_info *vblank_info;
102 } tdm_server_wait_info;
104 typedef struct _tdm_server_client_info {
105 struct list_head link;
107 char name[TDM_NAME_LEN];
108 struct wl_resource *resource;
109 } tdm_server_client_info;
111 static tdm_private_server *keep_private_server;
112 static struct list_head client_list;
114 static void destroy_wait(tdm_server_wait_info *wait_info);
117 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
119 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
123 snprintf(name, size, "Unknown");
125 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
126 h = fopen(proc, "r");
130 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
132 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
135 pname[len - 1] = '\0';
137 strncpy(name, pname, size - 1);
138 name[size - 1] = '\0';
143 /* LCOV_EXCL_START */
145 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
146 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
148 tdm_server_wait_info *found;
149 tdm_server_vblank_info *vblank_info;
151 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
153 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
154 tdm_server_wait_info, link, found);
156 TDM_ERR("wait_info(%p) is destroyed", wait_info);
160 if (tdm_debug_module & TDM_DEBUG_VBLANK)
161 TDM_DBG("req_id(%d) done", wait_info->req_id);
163 vblank_info = wait_info->vblank_info;
165 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
166 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
168 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
169 sequence, tv_sec, tv_usec, error);
171 destroy_wait(wait_info);
175 /* LCOV_EXCL_START */
177 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
178 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
180 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
184 /* LCOV_EXCL_START */
186 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
187 tdm_value value, void *user_data)
189 tdm_server_output_info *output_info = user_data;
190 struct wl_client *client;
193 TDM_RETURN_IF_FAIL(output_info != NULL);
195 client = wl_resource_get_client(output_info->resource);
196 TDM_RETURN_IF_FAIL(client != NULL);
198 wl_client_get_credentials(client, &pid, NULL, NULL);
200 if (!output_info->watch_output_changes) {
201 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
205 TDM_DBG("send the output changes: %d", (unsigned int)pid);
208 case TDM_OUTPUT_CHANGE_DPMS:
209 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
211 case TDM_OUTPUT_CHANGE_CONNECTION:
212 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
221 destroy_wait(tdm_server_wait_info *wait_info)
223 LIST_DEL(&wait_info->link);
228 destroy_vblank_callback(struct wl_resource *resource)
230 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
231 tdm_server_wait_info *w = NULL, *ww = NULL;
233 TDM_RETURN_IF_FAIL(vblank_info != NULL);
235 LIST_DEL(&vblank_info->link);
237 if (vblank_info->vblank)
238 tdm_vblank_destroy(vblank_info->vblank);
240 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
241 if (w->vblank_info == vblank_info)
248 /* LCOV_EXCL_START */
250 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
252 wl_resource_destroy(resource);
256 /* LCOV_EXCL_START */
258 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
260 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
263 ret = tdm_vblank_set_name(vblank_info->vblank, name);
264 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
268 /* LCOV_EXCL_START */
270 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
272 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
275 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
276 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
280 /* LCOV_EXCL_START */
282 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
284 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
287 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
288 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
293 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
295 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
298 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
299 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
303 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
304 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
306 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
307 tdm_server_output_info *output_info = vblank_info->output_info;
308 tdm_private_server *private_server = output_info->private_server;
309 tdm_server_wait_info *wait_info;
310 unsigned int enable_fake = 0;
313 wait_info = calloc(1, sizeof * wait_info);
315 /* LCOV_EXCL_START */
317 TDM_ERR("alloc failed");
318 ret = TDM_ERROR_OUT_OF_MEMORY;
324 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
325 wait_info->vblank_info = vblank_info;
326 wait_info->req_id = req_id;
327 wait_info->req_time = TDM_TIME(req_sec, req_usec);
329 if (tdm_debug_module & TDM_DEBUG_VBLANK)
330 TDM_DBG("req_id(%d) wait", req_id);
332 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
333 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
335 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
337 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
338 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
341 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
345 /* LCOV_EXCL_START */
347 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
349 destroy_wait(wait_info);
355 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
356 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
358 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
359 tdm_server_output_info *output_info = vblank_info->output_info;
360 tdm_private_server *private_server = output_info->private_server;
361 tdm_server_wait_info *wait_info;
362 unsigned int enable_fake = 0;
365 wait_info = calloc(1, sizeof * wait_info);
367 /* LCOV_EXCL_START */
369 TDM_ERR("alloc failed");
370 ret = TDM_ERROR_OUT_OF_MEMORY;
376 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
377 wait_info->vblank_info = vblank_info;
378 wait_info->req_id = req_id;
379 wait_info->req_time = TDM_TIME(req_sec, req_usec);
381 if (tdm_debug_module & TDM_DEBUG_VBLANK)
382 TDM_DBG("req_id(%d) wait", req_id);
384 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
385 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
387 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
389 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
390 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
393 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
397 /* LCOV_EXCL_START */
399 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
401 destroy_wait(wait_info);
406 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
407 _tdm_server_vblank_cb_destroy,
408 _tdm_server_vblank_cb_set_name,
409 _tdm_server_vblank_cb_set_fps,
410 _tdm_server_vblank_cb_set_offset,
411 _tdm_server_vblank_cb_set_enable_fake,
412 _tdm_server_vblank_cb_wait_vblank,
413 _tdm_server_vblank_cb_wait_vblank_seq,
416 /* LCOV_EXCL_START */
418 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
420 wl_resource_destroy(resource);
425 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
427 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
428 tdm_private_server *private_server = output_info->private_server;
429 tdm_private_loop *private_loop = private_server->private_loop;
430 struct wl_resource *vblank_resource;
432 tdm_server_vblank_info *vblank_info;
435 wl_resource_create(client, &wl_tdm_vblank_interface,
436 wl_resource_get_version(resource), id);
437 if (!vblank_resource) {
438 /* LCOV_EXCL_START */
440 wl_resource_post_no_memory(resource);
441 TDM_ERR("wl_resource_create failed");
447 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
449 /* LCOV_EXCL_START */
451 wl_resource_post_no_memory(resource);
452 wl_resource_destroy(vblank_resource);
453 TDM_ERR("tdm_vblank_create failed");
459 vblank_info = calloc(1, sizeof * vblank_info);
461 /* LCOV_EXCL_START */
463 wl_resource_post_no_memory(resource);
464 wl_resource_destroy(vblank_resource);
465 tdm_vblank_destroy(vblank);
466 TDM_ERR("alloc failed");
472 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
473 vblank_info->output_info = output_info;
474 vblank_info->resource = vblank_resource;
475 vblank_info->vblank = vblank;
476 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
478 tdm_vblank_set_resource(vblank, vblank_resource);
480 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
481 vblank_info, destroy_vblank_callback);
483 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
485 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
486 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
487 if (output == output_info->output)
488 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
494 /* LCOV_EXCL_START */
496 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
498 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
500 TDM_RETURN_IF_FAIL(output_info != NULL);
502 output_info->watch_output_changes = enable;
507 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
509 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
510 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
513 TDM_RETURN_IF_FAIL(output_info != NULL);
515 ret = tdm_output_get_conn_status(output_info->output, &status);
516 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
518 wl_tdm_output_send_connection(output_info->resource, status, ret);
523 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
527 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
529 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
530 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
533 TDM_RETURN_IF_FAIL(output_info != NULL);
535 ret = tdm_output_get_conn_status(output_info->output, &status);
536 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
538 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
539 const tdm_output_mode *mode = NULL;
540 unsigned int hdisplay, vdisplay, vrefresh;
542 ret = tdm_output_get_mode(output_info->output, &mode);
543 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
545 hdisplay = (mode) ? mode->hdisplay : 0;
546 vdisplay = (mode) ? mode->vdisplay : 0;
547 vrefresh = (mode) ? mode->vrefresh : 0;
549 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
551 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
556 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
560 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
562 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
563 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
566 TDM_RETURN_IF_FAIL(output_info != NULL);
568 ret = tdm_output_get_conn_status(output_info->output, &status);
569 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
571 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
572 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
574 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
575 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
577 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
579 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
584 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
587 static const struct wl_tdm_output_interface tdm_output_implementation = {
588 _tdm_server_output_cb_destroy,
589 _tdm_server_output_cb_create_vblank,
590 _tdm_server_output_cb_watch_output_changes,
591 _tdm_server_output_cb_get_connection,
592 _tdm_server_output_cb_get_mode,
593 _tdm_server_output_cb_get_dpms,
597 destroy_output_callback(struct wl_resource *resource)
599 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
600 tdm_server_vblank_info *v = NULL, *vv = NULL;
602 TDM_RETURN_IF_FAIL(output_info != NULL);
604 LIST_DEL(&output_info->link);
606 tdm_output_remove_change_handler(output_info->output,
607 _tdm_server_cb_output_change, output_info);
609 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
610 wl_resource_destroy(v->resource);
617 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
618 const char *name, uint32_t id)
620 tdm_private_server *private_server = wl_resource_get_user_data(resource);
621 tdm_server_output_info *output_info;
622 struct wl_resource *output_resource = NULL;
624 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
627 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
629 /* LCOV_EXCL_START */
631 TDM_ERR("There is no '%s' output", name);
632 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
633 "There is no '%s' output", name);
640 wl_resource_create(client, &wl_tdm_output_interface,
641 wl_resource_get_version(resource), id);
642 if (!output_resource) {
643 /* LCOV_EXCL_START */
645 wl_resource_post_no_memory(resource);
646 TDM_ERR("wl_resource_create failed");
652 output_info = calloc(1, sizeof * output_info);
654 /* LCOV_EXCL_START */
656 wl_resource_post_no_memory(resource);
657 wl_resource_destroy(output_resource);
658 TDM_ERR("alloc failed");
664 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
665 if (ret != TDM_ERROR_NONE) {
666 wl_resource_post_no_memory(resource);
667 wl_resource_destroy(output_resource);
669 TDM_ERR("tdm_output_add_change_handler failed");
673 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
674 output_info->private_server = private_server;
675 output_info->resource = output_resource;
676 output_info->output = output;
677 LIST_INITHEAD(&output_info->vblank_list);
679 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
680 output_info, destroy_output_callback);
682 ret = tdm_output_get_conn_status(output, &status);
683 wl_tdm_output_send_connection(output_resource, status, ret);
685 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
686 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
687 const tdm_output_mode *mode = NULL;
688 unsigned int hdisplay, vdisplay, vrefresh;
690 ret = tdm_output_get_mode(output, &mode);
691 hdisplay = (mode) ? mode->hdisplay : 0;
692 vdisplay = (mode) ? mode->vdisplay : 0;
693 vrefresh = (mode) ? mode->vrefresh : 0;
694 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
696 ret = tdm_output_get_dpms(output, &dpms_value);
697 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
699 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
700 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
704 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
706 tdm_server_voutput_info *voutput_info;
707 tdm_private_server *private_server;
709 tdm_error ret = TDM_ERROR_NONE;
711 voutput_info = wl_resource_get_user_data(resource);
713 private_server = voutput_info->private_server;
714 output = voutput_info->output;
717 ret = tdm_display_destroy_output(private_server->private_loop->dpy, output);
719 if (ret != TDM_ERROR_NONE)
720 TDM_ERR("_tdm_voutput_cb_destroy fail");
722 wl_resource_destroy(resource);
726 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
727 struct wl_resource *resource,
731 uint32_t hsync_start,
736 uint32_t vsync_start,
745 tdm_server_voutput_info *voutput_info;
746 tdm_output_mode *tmp_modes, *old_modes;
747 tdm_output_mode *new_mode;
750 voutput_info = wl_resource_get_user_data(resource);
752 count = voutput_info->available_modes.count;
753 old_modes = voutput_info->available_modes.modes;
754 if (index >= count) {
756 tmp_modes = malloc(count * sizeof(*tmp_modes));
757 memcpy(tmp_modes, old_modes, count * sizeof(tdm_output_mode));
760 voutput_info->available_modes.count = index + 1;
761 voutput_info->available_modes.modes =
762 realloc(voutput_info->available_modes.modes,
763 sizeof(tdm_output_mode) * (index + 1));
766 memcpy(voutput_info->available_modes.modes, tmp_modes, count * sizeof(tdm_output_mode));
771 new_mode = &voutput_info->available_modes.modes[index];
772 new_mode->clock = clock;
773 new_mode->hdisplay = hdisplay;
774 new_mode->hsync_start = hsync_start;
775 new_mode->hsync_end = hsync_end;
776 new_mode->htotal = htotal;
777 new_mode->hskew= hskew;
778 new_mode->vdisplay= vdisplay;
779 new_mode->vsync_start= vsync_start;
780 new_mode->vsync_end = vsync_end;
781 new_mode->vtotal = vtotal;
782 new_mode->vscan = vscan;
783 new_mode->vrefresh = vrefresh;
784 new_mode->flags = flags;
785 new_mode->type = type;
788 strncpy(new_mode->name, name, len);
789 new_mode->name[len] = '\0';
793 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
794 unsigned int mmwidth, unsigned int mmheight)
796 tdm_server_voutput_info *voutput_info;
798 voutput_info = wl_resource_get_user_data(resource);
800 voutput_info->mmwidth = mmwidth;
801 voutput_info->mmheight = mmheight;
805 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
807 tdm_server_voutput_info *voutput_info;
809 voutput_info = wl_resource_get_user_data(resource);
810 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
812 tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
813 tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
814 tdm_output_set_connect(voutput_info->output);
818 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
820 tdm_server_voutput_info *voutput_info;
822 voutput_info = wl_resource_get_user_data(resource);
823 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
825 /* Do free resources when it's being disconnected */
826 free(voutput_info->available_modes.modes);
827 voutput_info->available_modes.modes = NULL;
828 voutput_info->available_modes.count = 0;
829 voutput_info->mmwidth = 0;
830 voutput_info->mmheight = 0;
832 tdm_output_set_disconnect(voutput_info->output);
835 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
836 _tdm_voutput_cb_destroy,
837 _tdm_voutput_cb_set_available_modes,
838 _tdm_voutput_cb_set_physical_size,
839 _tdm_voutput_cb_connect,
840 _tdm_voutput_cb_disconnect
844 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
846 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
848 TDM_RETURN_IF_FAIL(voutput_info != NULL);
850 LIST_DEL(&voutput_info->link);
852 /* Do free your own resource */
857 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
859 struct wl_resource *voutput_resource = NULL;
860 tdm_private_server *private_server = wl_resource_get_user_data(resource);
861 tdm_server_voutput_info *voutput_info;
865 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
867 TDM_ERR("There is '%s' output, cannot create.", name);
868 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
869 "There is '%s' output", name);
873 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
875 TDM_ERR("output creation fail(%s)(%d).", name, ret);
876 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
877 "%s output creation fail", name);
882 wl_resource_create(client, &wl_tdm_voutput_interface,
883 wl_resource_get_version(resource), id);
884 if (!voutput_resource) {
885 /* LCOV_EXCL_START */
887 wl_resource_post_no_memory(resource);
888 TDM_ERR("wl_resource_create failed");
894 voutput_info = calloc(1, sizeof * voutput_info);
896 /* LCOV_EXCL_START */
898 wl_resource_post_no_memory(resource);
899 wl_resource_destroy(voutput_resource);
900 TDM_ERR("alloc failed");
906 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
907 voutput_info->private_server = private_server;
908 voutput_info->resource = voutput_resource;
909 voutput_info->output = output;
910 LIST_INITHEAD(&voutput_info->output_list);
912 wl_resource_set_implementation(voutput_resource,
913 &tdm_voutput_implementation,
915 tdm_voutput_cb_resource_destroy);
917 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
920 /* LCOV_EXCL_START */
922 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
924 tdm_private_server *private_server = wl_resource_get_user_data(resource);
925 tdm_private_loop *private_loop = private_server->private_loop;
926 char message[TDM_SERVER_REPLY_MSG_LEN];
928 int len = sizeof(message), size;
931 wl_client_get_credentials(client, NULL, &uid, NULL);
934 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
935 TDM_ERR("%s", message);
937 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
940 size = sizeof(message) - len;
943 wl_client_flush(client);
946 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
947 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
949 strncpy(buffer, m, copylen);
953 buffer[copylen] = '\0';
955 wl_tdm_send_debug_message(resource, buffer);
958 wl_tdm_send_debug_done(resource);
962 static const struct wl_tdm_interface tdm_implementation = {
963 _tdm_server_cb_debug,
964 _tdm_server_cb_create_output,
965 _tdm_server_cb_create_virtual_output
969 destroy_client(struct wl_resource *resource)
971 tdm_server_client_info *c = NULL, *cc = NULL;
972 struct wl_client *client;
974 client = wl_resource_get_client(resource);
975 TDM_RETURN_IF_FAIL(client != NULL);
977 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
978 if (c->resource == resource) {
987 _tdm_server_bind(struct wl_client *client, void *data,
988 uint32_t version, uint32_t id)
990 struct wl_resource *resource;
991 tdm_server_client_info *cinfo;
993 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
995 /* LCOV_EXCL_START */
997 wl_client_post_no_memory(client);
1000 /* LCOV_EXCL_STOP */
1003 cinfo = calloc(1, sizeof(tdm_server_client_info));
1005 /* LCOV_EXCL_START */
1007 wl_client_post_no_memory(client);
1008 wl_resource_destroy(resource);
1011 /* LCOV_EXCL_STOP */
1014 cinfo->resource = resource;
1016 LIST_ADDTAIL(&cinfo->link, &client_list);
1017 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1018 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1020 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1024 tdm_server_init(tdm_private_loop *private_loop)
1026 tdm_private_server *private_server;
1028 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1029 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1031 if (private_loop->private_server)
1032 return TDM_ERROR_NONE;
1034 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1035 /* LCOV_EXCL_START */
1037 TDM_ERR("createing a tdm-socket failed");
1038 return TDM_ERROR_OPERATION_FAILED;
1040 /* LCOV_EXCL_STOP */
1043 private_server = calloc(1, sizeof * private_server);
1044 if (!private_server) {
1045 TDM_ERR("alloc failed");
1046 return TDM_ERROR_OUT_OF_MEMORY;
1049 LIST_INITHEAD(&private_server->output_list);
1050 LIST_INITHEAD(&private_server->voutput_list);
1051 LIST_INITHEAD(&private_server->wait_list);
1053 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1054 private_server, _tdm_server_bind)) {
1055 /* LCOV_EXCL_START */
1057 TDM_ERR("creating a global resource failed");
1058 free(private_server);
1059 return TDM_ERROR_OUT_OF_MEMORY;
1061 /* LCOV_EXCL_STOP */
1064 private_server->private_loop = private_loop;
1065 private_loop->private_server = private_server;
1066 keep_private_server = private_server;
1068 LIST_INITHEAD(&client_list);
1070 return TDM_ERROR_NONE;
1074 tdm_server_deinit(tdm_private_loop *private_loop)
1076 tdm_server_output_info *o = NULL, *oo = NULL;
1077 // tdm_server_voutput_info *vo = NULL, *voo = NULL;
1078 tdm_server_wait_info *w = NULL, *ww = NULL;
1079 tdm_server_client_info *c = NULL, *cc = NULL;
1080 tdm_private_server *private_server;
1082 if (!private_loop->private_server)
1085 private_server = private_loop->private_server;
1087 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1091 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1092 wl_resource_destroy(o->resource);
1095 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1096 wl_resource_destroy(vo->resource);
1099 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1100 wl_resource_destroy(c->resource);
1103 free(private_server);
1104 private_loop->private_server = NULL;
1105 keep_private_server = NULL;
1108 /* LCOV_EXCL_START */
1110 tdm_server_get_client_name(pid_t pid)
1112 tdm_server_client_info *c = NULL;
1114 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1116 return (const char*)c->name;
1121 /* LCOV_EXCL_STOP */
1123 /* LCOV_EXCL_START */
1125 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1127 tdm_private_server *private_server = keep_private_server;
1128 tdm_server_output_info *output_info = NULL;
1130 if (!keep_private_server)
1131 return TDM_ERROR_NONE;
1133 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1134 tdm_server_vblank_info *vblank_info = NULL;
1136 if (output && output_info->output != output)
1139 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1140 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1144 return TDM_ERROR_NONE;
1146 /* LCOV_EXCL_STOP */