1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <sc1.lim@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
41 #include "tdm_private.h"
43 #include "tdm-server-protocol.h"
46 * - tdm server doesn't care about thread things.
47 * - DO NOT use the TDM internal functions here.
48 * However, the internal function which does lock/unlock the mutex of
49 * private_display in itself can be called.
50 * - DO NOT use the tdm_private_display structure here.
51 * - The callback function things can be called in main thread.
54 struct _tdm_private_server {
55 tdm_private_loop *private_loop;
56 struct list_head output_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 } tdm_server_output_info;
68 typedef struct _tdm_server_vblank_info {
69 struct list_head link;
70 tdm_server_output_info *output_info;
71 struct wl_resource *resource;
74 } tdm_server_vblank_info;
76 typedef struct _tdm_server_wait_info {
77 struct list_head link;
78 tdm_server_vblank_info *vblank_info;
81 } tdm_server_wait_info;
83 static tdm_private_server *keep_private_server;
85 static void destroy_wait(tdm_server_wait_info *wait_info);
88 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
90 tdm_private_loop *private_loop = private_server->private_loop;
91 tdm_output *found = NULL;
93 if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
94 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
99 tdm_display_get_output_count(private_loop->dpy, &count);
101 for (i = 0; i < count; i++) {
102 tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
103 tdm_output_conn_status status;
104 const char *model = NULL;
107 ret = tdm_output_get_conn_status(output, &status);
108 if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
111 ret = tdm_output_get_model_info(output, NULL, &model, NULL);
115 if (strncmp(model, name, TDM_NAME_LEN))
127 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
128 unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
130 tdm_server_wait_info *found;
131 tdm_server_vblank_info *vblank_info;
133 if (!keep_private_server)
136 LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
137 tdm_server_wait_info, link, found);
139 TDM_DBG("wait_info(%p) is destroyed", wait_info);
143 if (tdm_debug_module & TDM_DEBUG_VBLANK)
144 TDM_INFO("req_id(%d) done", wait_info->req_id);
146 vblank_info = wait_info->vblank_info;
147 wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
148 sequence, tv_sec, tv_usec, error);
149 destroy_wait(wait_info);
153 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
154 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
156 _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
160 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
161 tdm_value value, void *user_data)
163 tdm_server_output_info *output_info = user_data;
165 TDM_RETURN_IF_FAIL(output_info != NULL);
168 case TDM_OUTPUT_CHANGE_DPMS:
169 wl_tdm_output_send_dpms(output_info->resource, value.u32);
171 case TDM_OUTPUT_CHANGE_CONNECTION:
172 wl_tdm_output_send_connection(output_info->resource, value.u32);
180 destroy_wait(tdm_server_wait_info *wait_info)
182 LIST_DEL(&wait_info->link);
187 destroy_vblank_callback(struct wl_resource *resource)
189 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
190 tdm_server_wait_info *w = NULL, *ww = NULL;
192 TDM_RETURN_IF_FAIL(vblank_info != NULL);
194 if (vblank_info->vblank)
195 tdm_vblank_destroy(vblank_info->vblank);
197 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
198 if (w->vblank_info == vblank_info) {
203 LIST_DEL(&vblank_info->link);
208 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
210 wl_resource_destroy(resource);
214 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
216 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
218 tdm_vblank_set_fps(vblank_info->vblank, fps);
222 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
224 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
226 tdm_vblank_set_offset(vblank_info->vblank, offset);
230 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
232 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
234 tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
238 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
239 uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
241 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
242 tdm_server_output_info *output_info = vblank_info->output_info;
243 tdm_private_server *private_server = output_info->private_server;
244 tdm_server_wait_info *wait_info;
247 wait_info = calloc(1, sizeof *wait_info);
249 TDM_ERR("alloc failed");
250 ret = TDM_ERROR_OUT_OF_MEMORY;
254 LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
255 wait_info->vblank_info = vblank_info;
256 wait_info->req_id = req_id;
258 if (tdm_debug_module & TDM_DEBUG_VBLANK)
259 TDM_INFO("req_id(%d) wait", req_id);
261 ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
262 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
266 wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
269 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
270 _tdm_server_vblank_cb_destroy,
271 _tdm_server_vblank_cb_set_fps,
272 _tdm_server_vblank_cb_set_offset,
273 _tdm_server_vblank_cb_set_enable_fake,
274 _tdm_server_vblank_cb_wait_vblank,
278 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
280 wl_resource_destroy(resource);
284 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
286 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
287 tdm_private_server *private_server = output_info->private_server;
288 tdm_private_loop *private_loop = private_server->private_loop;
289 struct wl_resource *vblank_resource;
291 tdm_server_vblank_info *vblank_info;
294 wl_resource_create(client, &wl_tdm_vblank_interface,
295 wl_resource_get_version(resource), id);
296 if (!vblank_resource) {
297 wl_resource_post_no_memory(resource);
298 TDM_ERR("wl_resource_create failed");
302 vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
304 wl_resource_post_no_memory(resource);
305 wl_resource_destroy(vblank_resource);
306 TDM_ERR("tdm_vblank_create failed");
310 vblank_info = calloc(1, sizeof *vblank_info);
312 wl_resource_post_no_memory(resource);
313 wl_resource_destroy(vblank_resource);
314 tdm_vblank_destroy(vblank);
315 TDM_ERR("alloc failed");
319 LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
320 vblank_info->output_info = output_info;
321 vblank_info->resource = vblank_resource;
322 vblank_info->vblank = vblank;
324 wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
325 vblank_info, destroy_vblank_callback);
330 static const struct wl_tdm_output_interface tdm_output_implementation = {
331 _tdm_server_output_cb_destroy,
332 _tdm_server_output_cb_create_vblank,
336 destroy_output_callback(struct wl_resource *resource)
338 tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
339 tdm_server_vblank_info *v = NULL, *vv = NULL;
341 TDM_RETURN_IF_FAIL(output_info != NULL);
343 tdm_output_remove_change_handler(output_info->output,
344 _tdm_server_cb_output_change, output_info);
346 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
347 wl_resource_destroy(v->resource);
350 LIST_DEL(&output_info->link);
355 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
356 const char *name, uint32_t id)
358 tdm_private_server *private_server = wl_resource_get_user_data(resource);
359 tdm_server_output_info *output_info;
360 struct wl_resource *output_resource = NULL;
362 const tdm_output_mode *mode = NULL;
363 tdm_output_dpms dpms_value;
364 tdm_output_conn_status status;
366 output = _tdm_server_find_output(private_server, name);
368 TDM_ERR("There is no '%s' output", name);
369 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
370 "There is no '%s' output", name);
374 tdm_output_get_mode(output, &mode);
376 TDM_ERR("no mode for '%s' output", name);
377 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
378 "no mode for '%s' output", name);
382 tdm_output_get_dpms(output, &dpms_value);
383 tdm_output_get_conn_status(output, &status);
386 wl_resource_create(client, &wl_tdm_output_interface,
387 wl_resource_get_version(resource), id);
388 if (!output_resource) {
389 wl_resource_post_no_memory(resource);
390 TDM_ERR("wl_resource_create failed");
394 output_info = calloc(1, sizeof * output_info);
396 wl_resource_post_no_memory(resource);
397 wl_resource_destroy(output_resource);
398 TDM_ERR("alloc failed");
402 LIST_ADDTAIL(&output_info->link, &private_server->output_list);
403 output_info->private_server = private_server;
404 output_info->resource = output_resource;
405 output_info->output = output;
406 LIST_INITHEAD(&output_info->vblank_list);
408 tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
410 wl_resource_set_implementation(output_resource, &tdm_output_implementation,
411 output_info, destroy_output_callback);
413 wl_tdm_output_send_mode(output_resource, mode->hdisplay, mode->vdisplay, mode->vrefresh);
414 wl_tdm_output_send_dpms(output_resource, dpms_value);
415 wl_tdm_output_send_connection(output_resource, status);
419 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
421 tdm_private_server *private_server = wl_resource_get_user_data(resource);
422 tdm_private_loop *private_loop = private_server->private_loop;
424 char message[TDM_SERVER_REPLY_MSG_LEN];
425 int size = sizeof(message);
427 tdm_dbg_server_command(private_loop->dpy, options, message, &size);
428 wl_tdm_send_debug_done(resource, message);
431 static const struct wl_tdm_interface tdm_implementation = {
432 _tdm_server_cb_create_output,
433 _tdm_server_cb_debug,
437 _tdm_server_bind(struct wl_client *client, void *data,
438 uint32_t version, uint32_t id)
440 struct wl_resource *resource;
442 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
444 wl_client_post_no_memory(client);
448 wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
452 tdm_server_init(tdm_private_loop *private_loop)
454 tdm_private_server *private_server;
456 if (private_loop->private_server)
457 return TDM_ERROR_NONE;
459 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
460 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
462 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
463 TDM_ERR("createing a tdm-socket failed");
464 return TDM_ERROR_OPERATION_FAILED;
467 private_server = calloc(1, sizeof * private_server);
468 if (!private_server) {
469 TDM_ERR("alloc failed");
470 return TDM_ERROR_OUT_OF_MEMORY;
473 LIST_INITHEAD(&private_server->output_list);
474 LIST_INITHEAD(&private_server->wait_list);
476 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
477 private_server, _tdm_server_bind)) {
478 TDM_ERR("creating a global resource failed");
479 free(private_server);
480 return TDM_ERROR_OUT_OF_MEMORY;
483 private_server->private_loop = private_loop;
484 private_loop->private_server = private_server;
485 keep_private_server = private_server;
487 return TDM_ERROR_NONE;
491 tdm_server_deinit(tdm_private_loop *private_loop)
493 tdm_server_output_info *o = NULL, *oo = NULL;
494 tdm_server_wait_info *w = NULL, *ww = NULL;
495 tdm_private_server *private_server;
497 if (!private_loop->private_server)
500 private_server = private_loop->private_server;
502 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
506 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
507 wl_resource_destroy(o->resource);
510 free(private_server);
511 private_loop->private_server = NULL;
512 keep_private_server = NULL;