apply tizen coding rule
[platform/core/uifw/libtdm.git] / tools / tdm_test_client.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <poll.h>
39 #include <errno.h>
40 #include <time.h>
41 #include <stdint.h>
42
43 #include <tdm_client.h>
44 #include <tdm_helper.h>
45
46 static int
47 get_time_in_micros(void)
48 {
49         struct timespec tp;
50
51         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
52                 return (int)(tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
53
54         return 0;
55 }
56
57 static void
58 _client_vblank_handler(unsigned int sequence, unsigned int tv_sec,
59                                            unsigned int tv_usec, void *user_data)
60 {
61         int client, vblank;
62         static int prev = 0;
63
64         client = get_time_in_micros();
65         vblank = tv_sec * 1000000 + tv_usec;
66
67         if (vblank - prev > 16966 || vblank - prev < 16366) /* +0.3 ~ -0.3 ms */
68                 printf("vblank              : %d us\n", vblank - prev);
69
70         if (client - vblank > 2000) /* 2ms */
71                 printf("kernel -> tdm-client: %d us\n", client - vblank);
72
73         prev = vblank;
74 }
75
76
77 int
78 main(int argc, char *argv[])
79 {
80         tdm_client *client;
81         tdm_client_error error;
82         int fd = -1;
83         struct pollfd fds;
84         int sync;
85
86         if (argc < 2) {
87                 printf("Usage: %s\n", argv[0]);
88                 printf("\t%s 0 : non-sync\n", argv[0]);
89                 printf("\t%s 1 : sync\n", argv[0]);
90                 exit(1);
91         }
92
93         sync = atoi(argv[1]);
94
95         client = tdm_client_create(&error);
96         if (error != TDM_CLIENT_ERROR_NONE) {
97                 printf("tdm_client_create failed\n");
98                 exit(1);
99         }
100
101         error = tdm_client_get_fd(client, &fd);
102         if (error != TDM_CLIENT_ERROR_NONE || fd < 0) {
103                 printf("tdm_client_get_fd failed\n");
104                 goto done;
105         }
106
107         fds.events = POLLIN;
108         fds.fd = fd;
109         fds.revents = 0;
110
111         while (1) {
112                 int ret;
113
114                 error = tdm_client_wait_vblank(client, "unknown-0", 1, 1, sync,
115                                                                            _client_vblank_handler, NULL);
116                 if (error != TDM_CLIENT_ERROR_NONE) {
117                         printf("tdm_client_wait_vblank failed\n");
118                         goto done;
119                 }
120
121                 if (!sync) {
122                         ret = poll(&fds, 1, -1);
123                         if (ret < 0) {
124                                 if (errno == EBUSY)  /* normal case */
125                                         continue;
126                                 else {
127                                         printf("poll failed: %m\n");
128                                         goto done;
129                                 }
130                         }
131
132                         error = tdm_client_handle_events(client);
133                         if (error != TDM_CLIENT_ERROR_NONE)
134                                 printf("tdm_client_handle_events failed\n");
135                 }
136         }
137
138 done:
139         tdm_client_destroy(client);
140         return 0;
141 }