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,
736 uint32_t hsync_start,
741 uint32_t vsync_start,
750 tdm_server_voutput_info *voutput_info;
751 tdm_output_mode *tmp_modes, *old_modes;
752 tdm_output_mode *new_mode;
755 voutput_info = wl_resource_get_user_data(resource);
757 count = voutput_info->available_modes.count;
758 old_modes = voutput_info->available_modes.modes;
759 if (index >= count) {
761 tmp_modes = malloc(count * sizeof(*tmp_modes));
762 memcpy(tmp_modes, old_modes, count * sizeof(tdm_output_mode));
765 voutput_info->available_modes.count = index + 1;
766 voutput_info->available_modes.modes =
767 realloc(voutput_info->available_modes.modes,
768 sizeof(tdm_output_mode) * (index + 1));
771 memcpy(voutput_info->available_modes.modes, tmp_modes, count * sizeof(tdm_output_mode));
776 new_mode = &voutput_info->available_modes.modes[index];
777 new_mode->clock = clock;
778 new_mode->hdisplay = hdisplay;
779 new_mode->hsync_start = hsync_start;
780 new_mode->hsync_end = hsync_end;
781 new_mode->htotal = htotal;
782 new_mode->hskew= hskew;
783 new_mode->vdisplay= vdisplay;
784 new_mode->vsync_start= vsync_start;
785 new_mode->vsync_end = vsync_end;
786 new_mode->vtotal = vtotal;
787 new_mode->vscan = vscan;
788 new_mode->vrefresh = vrefresh;
789 new_mode->flags = flags;
790 new_mode->type = type;
793 strncpy(new_mode->name, name, len);
794 new_mode->name[len] = '\0';
798 _tdm_voutput_cb_set_available_formats(struct wl_client *client,
799 struct wl_resource *resource,
800 struct wl_array *formats)
802 tdm_server_voutput_info *voutput_info;
804 int count = 0, i = 0;
806 voutput_info = wl_resource_get_user_data(resource);
808 voutput_info->available_formats.count = 0;
809 if (voutput_info->available_formats.formats)
810 free(voutput_info->available_formats.formats);
812 wl_array_for_each(f, formats)
815 voutput_info->available_formats.formats = malloc(count * sizeof(tbm_format));
816 voutput_info->available_formats.count = count;
818 wl_array_for_each(f, formats)
819 voutput_info->available_formats.formats[i++] = *f;
823 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
824 unsigned int mmwidth, unsigned int mmheight)
826 tdm_server_voutput_info *voutput_info;
828 voutput_info = wl_resource_get_user_data(resource);
830 voutput_info->mmwidth = mmwidth;
831 voutput_info->mmheight = mmheight;
835 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
837 tdm_server_voutput_info *voutput_info;
839 voutput_info = wl_resource_get_user_data(resource);
840 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
842 tdm_output_set_physical_size(voutput_info->output, voutput_info->mmwidth, voutput_info->mmheight);
843 tdm_output_set_available_mode(voutput_info->output, voutput_info->available_modes.modes, voutput_info->available_modes.count);
844 tdm_output_set_connect(voutput_info->output);
848 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
850 tdm_server_voutput_info *voutput_info;
852 voutput_info = wl_resource_get_user_data(resource);
853 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
855 /* Do free resources when it's being disconnected */
856 free(voutput_info->available_modes.modes);
857 voutput_info->available_modes.modes = NULL;
858 voutput_info->available_modes.count = 0;
859 voutput_info->mmwidth = 0;
860 voutput_info->mmheight = 0;
862 tdm_output_set_disconnect(voutput_info->output);
865 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
866 _tdm_voutput_cb_destroy,
867 _tdm_voutput_cb_set_available_modes,
868 _tdm_voutput_cb_set_available_formats,
869 _tdm_voutput_cb_set_physical_size,
870 _tdm_voutput_cb_connect,
871 _tdm_voutput_cb_disconnect
875 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
877 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
879 TDM_RETURN_IF_FAIL(voutput_info != NULL);
881 LIST_DEL(&voutput_info->link);
883 /* Do free your own resource */
888 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
890 struct wl_resource *voutput_resource = NULL;
891 tdm_private_server *private_server = wl_resource_get_user_data(resource);
892 tdm_server_voutput_info *voutput_info;
896 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
898 TDM_ERR("There is '%s' output, cannot create.", name);
899 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
900 "There is '%s' output", name);
904 output = tdm_display_create_output(private_server->private_loop->dpy, name, &ret);
906 TDM_ERR("output creation fail(%s)(%d).", name, ret);
907 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
908 "%s output creation fail", name);
913 wl_resource_create(client, &wl_tdm_voutput_interface,
914 wl_resource_get_version(resource), id);
915 if (!voutput_resource) {
916 /* LCOV_EXCL_START */
918 wl_resource_post_no_memory(resource);
919 TDM_ERR("wl_resource_create failed");
925 voutput_info = calloc(1, sizeof * voutput_info);
927 /* LCOV_EXCL_START */
929 wl_resource_post_no_memory(resource);
930 wl_resource_destroy(voutput_resource);
931 TDM_ERR("alloc failed");
937 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
938 voutput_info->private_server = private_server;
939 voutput_info->resource = voutput_resource;
940 voutput_info->output = output;
941 LIST_INITHEAD(&voutput_info->output_list);
943 wl_resource_set_implementation(voutput_resource,
944 &tdm_voutput_implementation,
946 tdm_voutput_cb_resource_destroy);
948 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
951 /* LCOV_EXCL_START */
953 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
955 tdm_private_server *private_server = wl_resource_get_user_data(resource);
956 tdm_private_loop *private_loop = private_server->private_loop;
957 char message[TDM_SERVER_REPLY_MSG_LEN];
959 int len = sizeof(message), size;
962 wl_client_get_credentials(client, NULL, &uid, NULL);
965 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
966 TDM_ERR("%s", message);
968 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
971 size = sizeof(message) - len;
974 wl_client_flush(client);
977 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
978 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
980 strncpy(buffer, m, copylen);
984 buffer[copylen] = '\0';
986 wl_tdm_send_debug_message(resource, buffer);
989 wl_tdm_send_debug_done(resource);
993 static const struct wl_tdm_interface tdm_implementation = {
994 _tdm_server_cb_debug,
995 _tdm_server_cb_create_output,
996 _tdm_server_cb_create_virtual_output
1000 destroy_client(struct wl_resource *resource)
1002 tdm_server_client_info *c = NULL, *cc = NULL;
1003 struct wl_client *client;
1005 client = wl_resource_get_client(resource);
1006 TDM_RETURN_IF_FAIL(client != NULL);
1008 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1009 if (c->resource == resource) {
1018 _tdm_server_bind(struct wl_client *client, void *data,
1019 uint32_t version, uint32_t id)
1021 struct wl_resource *resource;
1022 tdm_server_client_info *cinfo;
1024 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1026 /* LCOV_EXCL_START */
1028 wl_client_post_no_memory(client);
1031 /* LCOV_EXCL_STOP */
1034 cinfo = calloc(1, sizeof(tdm_server_client_info));
1036 /* LCOV_EXCL_START */
1038 wl_client_post_no_memory(client);
1039 wl_resource_destroy(resource);
1042 /* LCOV_EXCL_STOP */
1045 cinfo->resource = resource;
1047 LIST_ADDTAIL(&cinfo->link, &client_list);
1048 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1049 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1051 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1055 tdm_server_init(tdm_private_loop *private_loop)
1057 tdm_private_server *private_server;
1059 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1060 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1062 if (private_loop->private_server)
1063 return TDM_ERROR_NONE;
1065 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1066 /* LCOV_EXCL_START */
1068 TDM_ERR("createing a tdm-socket failed");
1069 return TDM_ERROR_OPERATION_FAILED;
1071 /* LCOV_EXCL_STOP */
1074 private_server = calloc(1, sizeof * private_server);
1075 if (!private_server) {
1076 TDM_ERR("alloc failed");
1077 return TDM_ERROR_OUT_OF_MEMORY;
1080 LIST_INITHEAD(&private_server->output_list);
1081 LIST_INITHEAD(&private_server->voutput_list);
1082 LIST_INITHEAD(&private_server->wait_list);
1084 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1085 private_server, _tdm_server_bind)) {
1086 /* LCOV_EXCL_START */
1088 TDM_ERR("creating a global resource failed");
1089 free(private_server);
1090 return TDM_ERROR_OUT_OF_MEMORY;
1092 /* LCOV_EXCL_STOP */
1095 private_server->private_loop = private_loop;
1096 private_loop->private_server = private_server;
1097 keep_private_server = private_server;
1099 LIST_INITHEAD(&client_list);
1101 return TDM_ERROR_NONE;
1105 tdm_server_deinit(tdm_private_loop *private_loop)
1107 tdm_server_output_info *o = NULL, *oo = NULL;
1108 // tdm_server_voutput_info *vo = NULL, *voo = NULL;
1109 tdm_server_wait_info *w = NULL, *ww = NULL;
1110 tdm_server_client_info *c = NULL, *cc = NULL;
1111 tdm_private_server *private_server;
1113 if (!private_loop->private_server)
1116 private_server = private_loop->private_server;
1118 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1122 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1123 wl_resource_destroy(o->resource);
1126 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1127 wl_resource_destroy(vo->resource);
1130 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1131 wl_resource_destroy(c->resource);
1134 free(private_server);
1135 private_loop->private_server = NULL;
1136 keep_private_server = NULL;
1139 /* LCOV_EXCL_START */
1141 tdm_server_get_client_name(pid_t pid)
1143 tdm_server_client_info *c = NULL;
1145 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1147 return (const char*)c->name;
1152 /* LCOV_EXCL_STOP */
1154 /* LCOV_EXCL_START */
1156 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1158 tdm_private_server *private_server = keep_private_server;
1159 tdm_server_output_info *output_info = NULL;
1161 if (!keep_private_server)
1162 return TDM_ERROR_NONE;
1164 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1165 tdm_server_vblank_info *vblank_info = NULL;
1167 if (output && output_info->output != output)
1170 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1171 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1175 return TDM_ERROR_NONE;
1177 /* LCOV_EXCL_STOP */