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 **************************************************************************/
46 #include "tdm_client.h"
48 #include "tdm_macro.h"
51 #include "tdm_private.h"
52 #include "tdm-client-protocol.h"
54 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
56 typedef struct _tdm_private_client {
57 struct wl_display *display;
58 struct wl_registry *registry;
60 struct list_head output_list;
62 tdm_private_client_vblank *temp_vblank;
65 typedef struct _tdm_private_client_output {
66 tdm_private_client *private_client;
68 char name[TDM_NAME_LEN];
69 struct wl_tdm_output *output;
73 tdm_output_conn_status connection;
75 struct list_head vblank_list;
76 struct list_head change_handler_list;
80 struct list_head link;
81 } tdm_private_client_output;
83 struct _tdm_private_client_vblank {
84 tdm_private_client_output *private_output;
86 struct wl_tdm_vblank *vblank;
87 struct list_head wait_list;
92 unsigned int enable_fake;
96 struct list_head link;
99 typedef struct _tdm_client_output_handler_info {
100 tdm_private_client_output *private_output;
102 tdm_client_output_change_handler func;
105 struct list_head link;
106 } tdm_client_output_handler_info;
108 typedef struct _tdm_client_wait_info {
109 tdm_private_client_vblank *private_vblank;
111 tdm_client_vblank_handler func;
115 unsigned int req_sec;
116 unsigned int req_usec;
119 struct list_head link;
120 } tdm_client_wait_info;
123 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
124 uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
125 uint32_t tv_usec, uint32_t error)
127 tdm_private_client_vblank *private_vblank = data;
128 tdm_client_wait_info *w = NULL, *ww = NULL;
130 TDM_RETURN_IF_FAIL(private_vblank != NULL);
132 TDM_TRACE_COUNT(ClientDoneVBlank, req_id);
134 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
135 if (w->req_id != req_id)
139 w->func(private_vblank, error, sequence, tv_sec, tv_usec, w->user_data);
150 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
151 _tdm_client_vblank_cb_done,
155 _tdm_client_output_destroy(tdm_private_client_output *private_output)
157 tdm_private_client_vblank *v = NULL, *vv = NULL;
158 tdm_client_output_handler_info *h = NULL, *hh = NULL;
160 LIST_DEL(&private_output->link);
162 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
163 TDM_ERR("vblanks SHOULD be destroyed first!");
165 v->private_output = NULL;
168 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
173 wl_tdm_output_destroy(private_output->output);
175 free(private_output);
179 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
180 uint32_t width, uint32_t height, uint32_t refresh)
182 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
184 TDM_RETURN_IF_FAIL(private_output != NULL);
186 private_output->width = width;
187 private_output->height = height;
188 private_output->refresh = refresh;
190 TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
191 private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
192 width, height, refresh);
196 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value)
198 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
199 tdm_client_output_handler_info *h = NULL;
202 TDM_RETURN_IF_FAIL(private_output != NULL);
204 if (private_output->connection == value)
207 private_output->connection = value;
209 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
211 wl_proxy_get_id((struct wl_proxy*)private_output->output),
215 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
217 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
222 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value)
224 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
225 tdm_client_output_handler_info *h = NULL;
228 TDM_RETURN_IF_FAIL(private_output != NULL);
230 if (private_output->dpms == value)
233 private_output->dpms = value;
235 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
237 wl_proxy_get_id((struct wl_proxy*)private_output->output),
241 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
243 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
247 static const struct wl_tdm_output_listener tdm_client_output_listener = {
248 _tdm_client_output_cb_mode,
249 _tdm_client_output_cb_connection,
250 _tdm_client_output_cb_dpms,
254 _tdm_client_cb_global(void *data, struct wl_registry *registry,
255 uint32_t name, const char *interface,
258 tdm_private_client *private_client = data;
260 if (strncmp(interface, "wl_tdm", 6) == 0) {
261 private_client->tdm =
262 wl_registry_bind(registry, name, &wl_tdm_interface, version);
263 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
265 wl_display_flush(private_client->display);
270 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
274 static const struct wl_registry_listener tdm_client_registry_listener = {
275 _tdm_client_cb_global,
276 _tdm_client_cb_global_remove
280 tdm_client_create(tdm_error *error)
282 tdm_private_client *private_client;
284 private_client = calloc(1, sizeof *private_client);
285 if (!private_client) {
286 TDM_ERR("alloc failed");
288 *error = TDM_ERROR_OUT_OF_MEMORY;
292 LIST_INITHEAD(&private_client->output_list);
294 private_client->display = wl_display_connect("tdm-socket");
295 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
297 private_client->registry = wl_display_get_registry(private_client->display);
298 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
300 wl_registry_add_listener(private_client->registry,
301 &tdm_client_registry_listener, private_client);
302 wl_display_roundtrip(private_client->display);
304 /* check global objects */
305 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
308 *error = TDM_ERROR_NONE;
310 return (tdm_client*)private_client;
312 tdm_client_destroy((tdm_client*)private_client);
314 *error = TDM_ERROR_OPERATION_FAILED;
319 tdm_client_destroy(tdm_client *client)
321 tdm_private_client *private_client = (tdm_private_client*)client;
322 tdm_private_client_output *o = NULL, *oo = NULL;
327 if (private_client->temp_vblank)
328 tdm_client_vblank_destroy(private_client->temp_vblank);
330 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
331 _tdm_client_output_destroy(o);
334 if (private_client->tdm)
335 wl_tdm_destroy(private_client->tdm);
336 if (private_client->registry)
337 wl_registry_destroy(private_client->registry);
338 if (private_client->display)
339 wl_display_disconnect(private_client->display);
341 free(private_client);
345 tdm_client_get_fd(tdm_client *client, int *fd)
347 tdm_private_client *private_client;
349 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
350 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
352 private_client = (tdm_private_client*)client;
354 *fd = wl_display_get_fd(private_client->display);
356 return TDM_ERROR_OPERATION_FAILED;
358 return TDM_ERROR_NONE;
362 tdm_client_handle_events(tdm_client *client)
364 tdm_private_client *private_client;
366 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
368 private_client = (tdm_private_client*)client;
370 wl_display_dispatch(private_client->display);
372 return TDM_ERROR_NONE;
375 typedef struct _tdm_client_vblank_temp {
376 tdm_client_vblank_handler2 func;
378 } tdm_client_vblank_temp;
381 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
382 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
384 tdm_client_vblank_temp *vblank_temp = user_data;
386 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
388 if (vblank_temp->func)
389 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
395 tdm_client_wait_vblank(tdm_client *client, char *name,
396 int sw_timer, int interval, int sync,
397 tdm_client_vblank_handler2 func, void *user_data)
399 tdm_private_client *private_client = (tdm_private_client*)client;
400 tdm_client_output *output;
401 tdm_client_vblank_temp *vblank_temp;
404 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
405 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
406 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
408 if (!private_client->temp_vblank) {
409 output = tdm_client_get_output(client, name, &ret);
410 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
412 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
413 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
416 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
417 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
419 vblank_temp = calloc(1, sizeof *vblank_temp);
420 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
422 vblank_temp->func = func;
423 vblank_temp->user_data = user_data;
425 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
429 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
431 tdm_private_client *private_client;
432 tdm_private_client_output *private_output = NULL;
435 *error = TDM_ERROR_NONE;
438 TDM_ERR("'!client' failed");
440 *error = TDM_ERROR_INVALID_PARAMETER;
444 private_client = (tdm_private_client*)client;
449 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
450 if (!strncmp(private_output->name, name, TDM_NAME_LEN))
451 return (tdm_client_output*)private_output;
454 private_output = calloc(1, sizeof *private_output);
455 if (!private_output) {
456 TDM_ERR("alloc failed");
458 *error = TDM_ERROR_OUT_OF_MEMORY;
462 private_output->private_client = private_client;
464 snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
465 private_output->output = wl_tdm_create_output(private_client->tdm, private_output->name);
466 if (!private_output->output) {
467 TDM_ERR("couldn't create output resource");
468 free(private_output);
470 *error = TDM_ERROR_OUT_OF_MEMORY;
474 LIST_INITHEAD(&private_output->vblank_list);
475 LIST_INITHEAD(&private_output->change_handler_list);
476 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
478 wl_tdm_output_add_listener(private_output->output,
479 &tdm_client_output_listener, private_output);
480 wl_display_roundtrip(private_client->display);
482 return (tdm_client_output*)private_output;
486 tdm_client_output_add_change_handler(tdm_client_output *output,
487 tdm_client_output_change_handler func,
490 tdm_private_client_output *private_output;
491 tdm_client_output_handler_info *h;
493 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
494 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
496 private_output = (tdm_private_client_output*)output;
498 h = calloc(1, sizeof *h);
499 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
501 h->private_output = private_output;
503 h->user_data = user_data;
504 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
506 return TDM_ERROR_NOT_IMPLEMENTED;
510 tdm_client_output_remove_change_handler(tdm_client_output *output,
511 tdm_client_output_change_handler func,
514 tdm_private_client_output *private_output;
515 tdm_client_output_handler_info *h = NULL;
517 TDM_RETURN_IF_FAIL(output != NULL);
518 TDM_RETURN_IF_FAIL(func != NULL);
520 private_output = (tdm_private_client_output*)output;
522 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
523 if (h->func != func || h->user_data != user_data)
533 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
535 tdm_private_client_output *private_output;
537 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
538 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
540 private_output = (tdm_private_client_output*)output;
542 *refresh = private_output->refresh;
544 return TDM_ERROR_NONE;
548 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
550 tdm_private_client_output *private_output;
552 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
553 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
555 private_output = (tdm_private_client_output*)output;
557 *status = private_output->connection;
559 return TDM_ERROR_NONE;
563 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
565 tdm_private_client_output *private_output;
567 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
568 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
570 private_output = (tdm_private_client_output*)output;
572 *dpms = private_output->dpms;
574 return TDM_ERROR_NONE;
578 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
580 tdm_private_client_output *private_output;
581 tdm_private_client_vblank *private_vblank;
584 *error = TDM_ERROR_NONE;
587 TDM_ERR("'!output' failed");
589 *error = TDM_ERROR_INVALID_PARAMETER;
593 private_output = (tdm_private_client_output*)output;
595 private_vblank = calloc(1, sizeof *private_vblank);
596 if (!private_vblank) {
597 TDM_ERR("alloc failed");
599 *error = TDM_ERROR_OUT_OF_MEMORY;
603 private_vblank->private_output = private_output;
605 private_vblank->vblank = wl_tdm_output_create_vblank(private_output->output);
606 if (!private_vblank->vblank) {
607 TDM_ERR("couldn't create vblank resource");
608 free(private_vblank);
610 *error = TDM_ERROR_OUT_OF_MEMORY;
615 private_vblank->fps = private_output->refresh;
616 private_vblank->offset = 0;
617 private_vblank->enable_fake = 0;
619 LIST_INITHEAD(&private_vblank->wait_list);
620 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
622 wl_tdm_vblank_add_listener(private_vblank->vblank,
623 &tdm_client_vblank_listener, private_vblank);
625 return (tdm_client_vblank*)private_vblank;
629 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
631 tdm_private_client_vblank *private_vblank;
632 tdm_client_wait_info *w = NULL, *ww = NULL;
634 TDM_RETURN_IF_FAIL(vblank != NULL);
636 private_vblank = vblank;
637 LIST_DEL(&private_vblank->link);
639 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
644 wl_tdm_vblank_destroy(private_vblank->vblank);
646 free(private_vblank);
650 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
652 tdm_private_client_vblank *private_vblank;
654 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
656 private_vblank = vblank;
657 private_vblank->sync = sync;
659 return TDM_ERROR_NONE;
663 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
665 tdm_private_client_vblank *private_vblank;
667 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
668 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
670 private_vblank = vblank;
671 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
673 if (private_vblank->fps == fps)
674 return TDM_ERROR_NONE;
675 private_vblank->fps = fps;
677 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
679 return TDM_ERROR_NONE;
683 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
685 tdm_private_client_vblank *private_vblank;
687 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
689 private_vblank = vblank;
690 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
692 if (private_vblank->offset == offset_ms)
693 return TDM_ERROR_NONE;
694 private_vblank->offset = offset_ms;
696 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
698 return TDM_ERROR_NONE;
702 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
704 tdm_private_client_vblank *private_vblank;
706 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
708 private_vblank = vblank;
710 if (private_vblank->enable_fake == enable_fake)
711 return TDM_ERROR_NONE;
712 private_vblank->enable_fake = enable_fake;
714 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
716 return TDM_ERROR_NONE;
720 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
722 tdm_private_client *private_client;
723 tdm_private_client_output *private_output;
724 tdm_private_client_vblank *private_vblank;
725 tdm_client_wait_info *w;
729 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
730 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
731 /* can't support "interval 0" and "getting current_msc" things because
732 * there is a socket communication between TDM client and server. It's impossible
733 * to return the current msc or sequence immediately.
735 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
737 private_vblank = vblank;
738 private_output = private_vblank->private_output;
739 private_client = private_output->private_client;
741 if (!private_vblank->started)
742 private_vblank->started = 1;
744 if (private_output->dpms != TDM_OUTPUT_DPMS_ON && !private_vblank->enable_fake) {
745 TDM_INFO("dpms off");
746 return TDM_ERROR_DPMS_OFF;
749 w = calloc(1, sizeof *w);
751 TDM_ERR("alloc failed");
752 return TDM_ERROR_OUT_OF_MEMORY;
755 w->private_vblank = private_vblank;
757 w->user_data = user_data;
759 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
761 clock_gettime(CLOCK_MONOTONIC, &tp);
762 w->req_id = ++private_output->req_id;
763 w->req_sec = (unsigned int)tp.tv_sec;
764 w->req_usec = (unsigned int)(tp.tv_nsec / 1000);
765 w->need_free = (private_vblank->sync) ? 0 : 1;
767 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, w->req_sec, w->req_usec);
769 TDM_TRACE_COUNT(ClientWaitVBlank, w->req_id);
771 if (!private_vblank->sync) {
772 wl_display_flush(private_client->display);
773 return TDM_ERROR_NONE;
776 while (ret != -1 && !w->need_free)
777 ret = wl_display_dispatch(private_client->display);
779 clock_gettime(CLOCK_MONOTONIC, &tp);
780 TDM_DBG("block during %d us",
781 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
782 - (w->req_sec * 1000000 + w->req_usec));
787 return TDM_ERROR_NONE;