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 #define WL_HIDE_DEPRECATED
42 #include <tdm-server-protocol.h>
44 #include "tdm_private.h"
47 * - tdm server doesn't care about thread things.
48 * - DO NOT use the TDM internal functions here.
49 * However, the internal function which does lock/unlock the mutex of
50 * private_display in itself can be called.
51 * - DO NOT use the tdm_private_display structure here.
52 * - The callback function things can be called in main thread.
55 struct _tdm_private_server {
56 tdm_private_loop *private_loop;
57 struct list_head output_list;
58 struct list_head voutput_list;
59 struct list_head wait_list;
62 typedef struct _tdm_server_output_info {
63 struct list_head link;
64 tdm_private_server *private_server;
65 struct wl_resource *resource;
67 struct list_head vblank_list;
68 unsigned int watch_output_changes;
69 } tdm_server_output_info;
71 typedef struct _tdm_server_voutput_buffer {
72 struct list_head link;
73 struct wl_resource *wl_buffer;
75 } tdm_server_voutput_buffer;
77 typedef struct _tdm_server_voutput_info {
78 struct list_head link;
79 tdm_private_server *private_server;
80 struct wl_resource *resource;
84 tdm_output_conn_status status;
87 tdm_output_mode *modes;
91 unsigned int mmheight;
92 } tdm_server_voutput_info;
94 typedef struct _tdm_server_vblank_info {
95 struct list_head link;
96 tdm_server_output_info *output_info;
97 struct wl_resource *resource;
101 } tdm_server_vblank_info;
103 typedef struct _tdm_server_wait_info {
104 struct list_head link;
105 tdm_server_vblank_info *vblank_info;
109 } tdm_server_wait_info;
111 typedef struct _tdm_server_client_info {
112 struct list_head link;
114 char name[TDM_NAME_LEN];
115 struct wl_resource *resource;
116 } tdm_server_client_info;
118 static tdm_private_server *keep_private_server;
119 static struct list_head client_list;
121 static void destroy_wait(tdm_server_wait_info *wait_info);
124 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
126 char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
130 snprintf(name, size, "Unknown");
132 snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
133 h = fopen(proc, "r");
137 len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
139 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
142 pname[len - 1] = '\0';
144 strncpy(name, pname, size - 1);
145 name[size - 1] = '\0';
150 /* LCOV_EXCL_START */
152 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
153 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
155 tdm_server_wait_info *found;
156 tdm_server_vblank_info *vblank_info;
158 TDM_RETURN_IF_FAIL(keep_private_server != NULL);
160 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
161 tdm_server_wait_info, link, found);
163 TDM_ERR("wait_info(%p) is destroyed", wait_info);
167 if (tdm_debug_module & TDM_DEBUG_VBLANK)
168 TDM_DBG("req_id(%d) done", wait_info->req_id);
170 vblank_info = wait_info->vblank_info;
172 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
173 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
175 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
176 sequence, tv_sec, tv_usec, error);
178 destroy_wait(wait_info);
182 /* LCOV_EXCL_START */
184 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
185 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
187 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
191 /* LCOV_EXCL_START */
193 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
194 tdm_value value, void *user_data)
196 tdm_server_output_info *output_info = user_data;
197 struct wl_client *client;
199 tdm_output_conn_status status;
200 tdm_error ret = TDM_ERROR_NONE;
202 TDM_RETURN_IF_FAIL(output_info != NULL);
204 client = wl_resource_get_client(output_info->resource);
205 TDM_RETURN_IF_FAIL(client != NULL);
207 wl_client_get_credentials(client, &pid, NULL, NULL);
209 if (!output_info->watch_output_changes) {
210 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
214 TDM_DBG("send the output changes: %d, type:%d, value:%d", (unsigned int)pid, type, value.u32);
217 case TDM_OUTPUT_CHANGE_DPMS:
218 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
220 case TDM_OUTPUT_CHANGE_CONNECTION:
222 if (status == TDM_OUTPUT_CONN_STATUS_MODE_SETTED) {
223 const tdm_output_mode *mode = NULL;
224 unsigned int hdisplay, vdisplay, vrefresh;
226 ret = tdm_output_get_mode(output_info->output, &mode);
227 if (ret == TDM_ERROR_NONE) {
228 hdisplay = (mode) ? mode->hdisplay : 0;
229 vdisplay = (mode) ? mode->vdisplay : 0;
230 vrefresh = (mode) ? mode->vrefresh : 0;
232 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
234 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
237 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
246 destroy_wait(tdm_server_wait_info *wait_info)
248 LIST_DEL(&wait_info->link);
253 destroy_vblank_callback(struct wl_resource *resource)
255 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
256 tdm_server_wait_info *w = NULL, *ww = NULL;
258 TDM_RETURN_IF_FAIL(vblank_info != NULL);
260 LIST_DEL(&vblank_info->link);
262 if (vblank_info->vblank)
263 tdm_vblank_destroy(vblank_info->vblank);
265 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
266 if (w->vblank_info == vblank_info)
273 /* LCOV_EXCL_START */
275 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
277 wl_resource_destroy(resource);
281 /* LCOV_EXCL_START */
283 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
285 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
288 ret = tdm_vblank_set_name(vblank_info->vblank, name);
289 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
293 /* LCOV_EXCL_START */
295 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
297 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
300 ret = tdm_vblank_set_fps(vblank_info->vblank, fps);
301 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
305 /* LCOV_EXCL_START */
307 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
309 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
312 ret = tdm_vblank_set_offset(vblank_info->vblank, offset);
313 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
318 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
320 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
323 ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
324 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
328 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
329 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
331 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
332 tdm_server_output_info *output_info = vblank_info->output_info;
333 tdm_private_server *private_server = output_info->private_server;
334 tdm_server_wait_info *wait_info;
335 unsigned int enable_fake = 0;
338 wait_info = calloc(1, sizeof * wait_info);
340 /* LCOV_EXCL_START */
342 TDM_ERR("alloc failed");
343 ret = TDM_ERROR_OUT_OF_MEMORY;
349 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
350 wait_info->vblank_info = vblank_info;
351 wait_info->req_id = req_id;
352 wait_info->req_time = TDM_TIME(req_sec, req_usec);
354 if (tdm_debug_module & TDM_DEBUG_VBLANK)
355 TDM_DBG("req_id(%d) wait", req_id);
357 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
358 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
360 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
362 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
363 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
366 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
370 /* LCOV_EXCL_START */
372 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
374 destroy_wait(wait_info);
380 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
381 uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
383 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
384 tdm_server_output_info *output_info = vblank_info->output_info;
385 tdm_private_server *private_server = output_info->private_server;
386 tdm_server_wait_info *wait_info;
387 unsigned int enable_fake = 0;
390 wait_info = calloc(1, sizeof * wait_info);
392 /* LCOV_EXCL_START */
394 TDM_ERR("alloc failed");
395 ret = TDM_ERROR_OUT_OF_MEMORY;
401 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
402 wait_info->vblank_info = vblank_info;
403 wait_info->req_id = req_id;
404 wait_info->req_time = TDM_TIME(req_sec, req_usec);
406 if (tdm_debug_module & TDM_DEBUG_VBLANK)
407 TDM_DBG("req_id(%d) wait", req_id);
409 if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK)
410 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp);
412 ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
414 tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
415 if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
418 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
422 /* LCOV_EXCL_START */
424 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
426 destroy_wait(wait_info);
431 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
432 _tdm_server_vblank_cb_destroy,
433 _tdm_server_vblank_cb_set_name,
434 _tdm_server_vblank_cb_set_fps,
435 _tdm_server_vblank_cb_set_offset,
436 _tdm_server_vblank_cb_set_enable_fake,
437 _tdm_server_vblank_cb_wait_vblank,
438 _tdm_server_vblank_cb_wait_vblank_seq,
441 /* LCOV_EXCL_START */
443 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
445 wl_resource_destroy(resource);
450 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
452 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
453 tdm_private_server *private_server = output_info->private_server;
454 tdm_private_loop *private_loop = private_server->private_loop;
455 struct wl_resource *vblank_resource;
457 tdm_server_vblank_info *vblank_info;
460 wl_resource_create(client, &wl_tdm_vblank_interface,
461 wl_resource_get_version(resource), id);
462 if (!vblank_resource) {
463 /* LCOV_EXCL_START */
465 wl_resource_post_no_memory(resource);
466 TDM_ERR("wl_resource_create failed");
472 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
474 /* LCOV_EXCL_START */
476 wl_resource_post_no_memory(resource);
477 wl_resource_destroy(vblank_resource);
478 TDM_ERR("tdm_vblank_create failed");
484 vblank_info = calloc(1, sizeof * vblank_info);
486 /* LCOV_EXCL_START */
488 wl_resource_post_no_memory(resource);
489 wl_resource_destroy(vblank_resource);
490 tdm_vblank_destroy(vblank);
491 TDM_ERR("alloc failed");
497 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
498 vblank_info->output_info = output_info;
499 vblank_info->resource = vblank_resource;
500 vblank_info->vblank = vblank;
501 vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank);
503 tdm_vblank_set_resource(vblank, vblank_resource);
505 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
506 vblank_info, destroy_vblank_callback);
508 wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp);
510 if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) {
511 tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL);
512 if (output == output_info->output)
513 wl_tdm_vblank_send_ttrace(vblank_info->resource, 1);
519 /* LCOV_EXCL_START */
521 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
523 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
525 TDM_RETURN_IF_FAIL(output_info != NULL);
527 output_info->watch_output_changes = enable;
532 _tdm_server_output_cb_get_connection(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 wl_tdm_output_send_connection(output_info->resource, status, ret);
548 wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
552 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
554 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
555 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
558 TDM_RETURN_IF_FAIL(output_info != NULL);
560 ret = tdm_output_get_conn_status(output_info->output, &status);
561 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
563 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
564 const tdm_output_mode *mode = NULL;
565 unsigned int hdisplay, vdisplay, vrefresh;
567 ret = tdm_output_get_mode(output_info->output, &mode);
568 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
570 hdisplay = (mode) ? mode->hdisplay : 0;
571 vdisplay = (mode) ? mode->vdisplay : 0;
572 vrefresh = (mode) ? mode->vrefresh : 0;
574 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
576 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
581 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
585 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
587 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
588 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
591 TDM_RETURN_IF_FAIL(output_info != NULL);
593 ret = tdm_output_get_conn_status(output_info->output, &status);
594 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
596 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
597 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
599 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
600 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
602 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
604 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
609 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
612 static const struct wl_tdm_output_interface tdm_output_implementation = {
613 _tdm_server_output_cb_destroy,
614 _tdm_server_output_cb_create_vblank,
615 _tdm_server_output_cb_watch_output_changes,
616 _tdm_server_output_cb_get_connection,
617 _tdm_server_output_cb_get_mode,
618 _tdm_server_output_cb_get_dpms,
622 destroy_output_callback(struct wl_resource *resource)
624 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
625 tdm_server_vblank_info *v = NULL, *vv = NULL;
627 TDM_RETURN_IF_FAIL(output_info != NULL);
629 LIST_DEL(&output_info->link);
631 tdm_output_remove_change_handler(output_info->output,
632 _tdm_server_cb_output_change, output_info);
634 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
635 wl_resource_destroy(v->resource);
642 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
643 const char *name, uint32_t id)
645 tdm_private_server *private_server = wl_resource_get_user_data(resource);
646 tdm_server_output_info *output_info;
647 struct wl_resource *output_resource = NULL;
649 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
652 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
654 /* LCOV_EXCL_START */
656 TDM_ERR("There is no '%s' output", name);
657 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
658 "There is no '%s' output", name);
665 wl_resource_create(client, &wl_tdm_output_interface,
666 wl_resource_get_version(resource), id);
667 if (!output_resource) {
668 /* LCOV_EXCL_START */
670 wl_resource_post_no_memory(resource);
671 TDM_ERR("wl_resource_create failed");
677 output_info = calloc(1, sizeof * output_info);
679 /* LCOV_EXCL_START */
681 wl_resource_post_no_memory(resource);
682 wl_resource_destroy(output_resource);
683 TDM_ERR("alloc failed");
689 ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
690 if (ret != TDM_ERROR_NONE) {
691 wl_resource_post_no_memory(resource);
692 wl_resource_destroy(output_resource);
694 TDM_ERR("tdm_output_add_change_handler failed");
698 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
699 output_info->private_server = private_server;
700 output_info->resource = output_resource;
701 output_info->output = output;
702 LIST_INITHEAD(&output_info->vblank_list);
704 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
705 output_info, destroy_output_callback);
707 ret = tdm_output_get_conn_status(output, &status);
708 wl_tdm_output_send_connection(output_resource, status, ret);
710 if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
711 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
712 const tdm_output_mode *mode = NULL;
713 unsigned int hdisplay, vdisplay, vrefresh;
715 ret = tdm_output_get_mode(output, &mode);
716 hdisplay = (mode) ? mode->hdisplay : 0;
717 vdisplay = (mode) ? mode->vdisplay : 0;
718 vrefresh = (mode) ? mode->vrefresh : 0;
719 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
721 ret = tdm_output_get_dpms(output, &dpms_value);
722 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
724 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
725 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
729 static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
731 wl_resource_destroy(resource);
735 _tdm_voutput_cb_set_available_modes(struct wl_client *client,
736 struct wl_resource *resource,
737 struct wl_array *modes)
739 tdm_server_voutput_info *voutput_info;
740 tdm_output_mode *mode;
741 int size, count = 0, i = 0;
743 voutput_info = wl_resource_get_user_data(resource);
745 voutput_info->available_modes.count = 0;
746 if (voutput_info->available_modes.modes)
747 free(voutput_info->available_modes.modes);
749 wl_array_for_each(mode, modes)
751 size = sizeof(tdm_output_mode);
753 voutput_info->available_modes.modes = malloc(count * size);
754 voutput_info->available_modes.count = count;
756 wl_array_for_each(mode, modes)
757 memcpy(&voutput_info->available_modes.modes[i++], mode, size);
761 _tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
762 unsigned int mmwidth, unsigned int mmheight)
764 tdm_server_voutput_info *voutput_info;
766 voutput_info = wl_resource_get_user_data(resource);
768 voutput_info->mmwidth = mmwidth;
769 voutput_info->mmheight = mmheight;
773 _tdm_voutput_cb_set_mode(struct wl_client *client, struct wl_resource *resource, unsigned int index)
775 tdm_server_voutput_info *voutput_info = NULL;
776 tdm_output *output = NULL;
777 tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
778 const tdm_output_mode *modes, *mode;
783 voutput_info = wl_resource_get_user_data(resource);
784 TDM_RETURN_IF_FAIL(voutput_info != NULL);
785 output = voutput_info->output;
787 ret = tdm_output_get_conn_status(output, &status);
788 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
789 TDM_RETURN_IF_FAIL(status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED);
791 ret = tdm_output_get_available_modes(output, &modes, &count);
792 TDM_RETURN_IF_FAIL(index < count);
794 mode = &modes[index];
795 TDM_DBG("mode set request to index:%d (%dx%d, %d)", index, mode->hdisplay, mode->vdisplay, mode->vrefresh);
797 ret = tdm_output_set_mode(output, mode);
798 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
800 tdm_output_request_mode_set(voutput_info->output, index);
804 _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
806 tdm_server_voutput_info *voutput_info;
808 voutput_info = wl_resource_get_user_data(resource);
809 voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
811 tdm_voutput_set_physical_size(voutput_info->voutput, voutput_info->mmwidth, voutput_info->mmheight);
812 tdm_voutput_set_available_mode(voutput_info->voutput, voutput_info->available_modes.modes, voutput_info->available_modes.count);
813 tdm_voutput_connect(voutput_info->voutput);
817 _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
819 tdm_server_voutput_info *voutput_info;
821 voutput_info = wl_resource_get_user_data(resource);
822 voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
824 /* Do free resources when it's being disconnected */
825 free(voutput_info->available_modes.modes);
826 voutput_info->available_modes.modes = NULL;
827 voutput_info->available_modes.count = 0;
828 voutput_info->mmwidth = 0;
829 voutput_info->mmheight = 0;
831 tdm_voutput_disconnect(voutput_info->voutput);
835 _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
840 static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
841 _tdm_voutput_cb_destroy,
842 _tdm_voutput_cb_set_available_modes,
843 _tdm_voutput_cb_set_physical_size,
844 _tdm_voutput_cb_set_mode,
845 _tdm_voutput_cb_connect,
846 _tdm_voutput_cb_disconnect,
847 _tdm_voutput_cb_commit_done
851 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
853 tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
854 tdm_voutput *voutput;
855 tdm_error ret = TDM_ERROR_NONE;
857 TDM_RETURN_IF_FAIL(voutput_info != NULL);
859 voutput = voutput_info->voutput;
862 ret = tdm_voutput_destroy(voutput);
863 if (ret != TDM_ERROR_NONE)
864 TDM_ERR("_tdm_voutput_cb_destroy fail");
866 LIST_DEL(&voutput_info->link);
868 /* Do free your own resource */
873 _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
875 struct wl_resource *voutput_resource = NULL;
876 tdm_private_server *private_server = wl_resource_get_user_data(resource);
877 tdm_server_voutput_info *voutput_info;
878 tdm_voutput *voutput;
882 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
884 TDM_ERR("There is '%s' output, cannot create.", name);
885 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
886 "There is '%s' output", name);
890 voutput = tdm_voutput_create(private_server->private_loop->dpy, name, &ret);
892 TDM_ERR("voutput creation fail(%s)(%d).", name, ret);
893 wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
894 "%s output creation fail", name);
898 output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
900 TDM_ERR("There is no '%s' output.", name);
901 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
902 "There is '%s' output", name);
907 wl_resource_create(client, &wl_tdm_voutput_interface,
908 wl_resource_get_version(resource), id);
909 if (!voutput_resource) {
910 /* LCOV_EXCL_START */
912 wl_resource_post_no_memory(resource);
913 TDM_ERR("wl_resource_create failed");
919 voutput_info = calloc(1, sizeof * voutput_info);
921 /* LCOV_EXCL_START */
923 wl_resource_post_no_memory(resource);
924 wl_resource_destroy(voutput_resource);
925 TDM_ERR("alloc failed");
931 LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
932 voutput_info->private_server = private_server;
933 voutput_info->resource = voutput_resource;
934 voutput_info->voutput = voutput;
935 voutput_info->output = output;
937 wl_resource_set_implementation(voutput_resource,
938 &tdm_voutput_implementation,
940 tdm_voutput_cb_resource_destroy);
942 wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
945 /* LCOV_EXCL_START */
947 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
949 tdm_private_server *private_server = wl_resource_get_user_data(resource);
950 tdm_private_loop *private_loop = private_server->private_loop;
951 char message[TDM_SERVER_REPLY_MSG_LEN];
953 int len = sizeof(message), size;
956 wl_client_get_credentials(client, NULL, &uid, NULL);
959 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
960 TDM_ERR("%s", message);
962 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
965 size = sizeof(message) - len;
968 wl_client_flush(client);
971 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
972 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
974 strncpy(buffer, m, copylen);
978 buffer[copylen] = '\0';
980 wl_tdm_send_debug_message(resource, buffer);
983 wl_tdm_send_debug_done(resource);
987 static const struct wl_tdm_interface tdm_implementation = {
988 _tdm_server_cb_debug,
989 _tdm_server_cb_create_output,
990 _tdm_server_cb_create_virtual_output
994 destroy_client(struct wl_resource *resource)
996 tdm_server_client_info *c = NULL, *cc = NULL;
997 struct wl_client *client;
999 client = wl_resource_get_client(resource);
1000 TDM_RETURN_IF_FAIL(client != NULL);
1002 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1003 if (c->resource == resource) {
1012 _tdm_server_bind(struct wl_client *client, void *data,
1013 uint32_t version, uint32_t id)
1015 struct wl_resource *resource;
1016 tdm_server_client_info *cinfo;
1018 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
1020 /* LCOV_EXCL_START */
1022 wl_client_post_no_memory(client);
1025 /* LCOV_EXCL_STOP */
1028 cinfo = calloc(1, sizeof(tdm_server_client_info));
1030 /* LCOV_EXCL_START */
1032 wl_client_post_no_memory(client);
1033 wl_resource_destroy(resource);
1036 /* LCOV_EXCL_STOP */
1039 cinfo->resource = resource;
1041 LIST_ADDTAIL(&cinfo->link, &client_list);
1042 wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
1043 _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
1045 wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
1049 tdm_server_init(tdm_private_loop *private_loop)
1051 tdm_private_server *private_server;
1053 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
1054 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
1056 if (private_loop->private_server)
1057 return TDM_ERROR_NONE;
1059 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
1060 /* LCOV_EXCL_START */
1062 TDM_ERR("createing a tdm-socket failed");
1063 return TDM_ERROR_OPERATION_FAILED;
1065 /* LCOV_EXCL_STOP */
1068 private_server = calloc(1, sizeof * private_server);
1069 if (!private_server) {
1070 TDM_ERR("alloc failed");
1071 return TDM_ERROR_OUT_OF_MEMORY;
1074 LIST_INITHEAD(&private_server->output_list);
1075 LIST_INITHEAD(&private_server->voutput_list);
1076 LIST_INITHEAD(&private_server->wait_list);
1078 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
1079 private_server, _tdm_server_bind)) {
1080 /* LCOV_EXCL_START */
1082 TDM_ERR("creating a global resource failed");
1083 free(private_server);
1084 return TDM_ERROR_OUT_OF_MEMORY;
1086 /* LCOV_EXCL_STOP */
1089 private_server->private_loop = private_loop;
1090 private_loop->private_server = private_server;
1091 keep_private_server = private_server;
1093 LIST_INITHEAD(&client_list);
1095 return TDM_ERROR_NONE;
1099 tdm_server_deinit(tdm_private_loop *private_loop)
1101 tdm_server_output_info *o = NULL, *oo = NULL;
1102 tdm_server_voutput_info *vo = NULL, *voo = NULL;
1103 tdm_server_wait_info *w = NULL, *ww = NULL;
1104 tdm_server_client_info *c = NULL, *cc = NULL;
1105 tdm_private_server *private_server;
1107 if (!private_loop->private_server)
1110 private_server = private_loop->private_server;
1112 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
1116 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
1117 wl_resource_destroy(o->resource);
1120 LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) {
1121 wl_resource_destroy(vo->resource);
1124 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
1125 wl_resource_destroy(c->resource);
1128 free(private_server);
1129 private_loop->private_server = NULL;
1130 keep_private_server = NULL;
1133 /* LCOV_EXCL_START */
1135 tdm_server_get_client_name(pid_t pid)
1137 tdm_server_client_info *c = NULL;
1139 LIST_FOR_EACH_ENTRY(c, &client_list, link) {
1141 return (const char*)c->name;
1146 /* LCOV_EXCL_STOP */
1148 /* LCOV_EXCL_START */
1150 tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable)
1152 tdm_private_server *private_server = keep_private_server;
1153 tdm_server_output_info *output_info = NULL;
1155 if (!keep_private_server)
1156 return TDM_ERROR_NONE;
1158 LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) {
1159 tdm_server_vblank_info *vblank_info = NULL;
1161 if (output && output_info->output != output)
1164 LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) {
1165 wl_tdm_vblank_send_ttrace(vblank_info->resource, enable);
1169 return TDM_ERROR_NONE;
1171 /* LCOV_EXCL_STOP */