5e1f1bbd643256355bb8da55b034a259be68ef76
[platform/core/uifw/libtdm.git] / tools / tdm_test_client.c
1 /*
2 Copyright (C) 2015 Samsung Electronics co., Ltd. All Rights Reserved.
3
4 Contact:
5       Changyeon Lee <cyeon.lee@samsung.com>,
6       JunKyeong Kim <jk0430.kim@samsung.com>,
7       Boram Park <boram1288.park@samsung.com>,
8       SooChan Lim <sc1.lim@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the "Software"),
12 to deal in the Software without restriction, including without limitation
13 the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 and/or sell copies of the Software, and to permit persons to whom the
15 Software is furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice (including the next
18 paragraph) shall be included in all copies or substantial portions of the
19 Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <poll.h>
33 #include <errno.h>
34 #include <time.h>
35 #include <stdint.h>
36
37 #include <tdm_client.h>
38 #include <tdm_helper.h>
39
40 static int
41 get_time_in_micros(void)
42 {
43         struct timespec tp;
44
45         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
46                 return (int)(tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
47
48         return 0;
49 }
50
51 static void
52 _client_vblank_handler(unsigned int sequence, unsigned int tv_sec,
53                        unsigned int tv_usec, void *user_data)
54 {
55         int client, vblank;
56         static int prev = 0;
57
58         client = get_time_in_micros();
59         vblank = tv_sec * 1000000 + tv_usec;
60
61         if (vblank - prev > 16966 || vblank - prev < 16366) /* +0.3 ~ -0.3 ms */
62                 printf("vblank              : %d us\n", vblank - prev);
63
64         if (client - vblank > 2000) /* 2ms */
65                 printf("kernel -> tdm-client: %d us\n", client - vblank);
66
67         prev = vblank;
68 }
69
70
71 int
72 main(int argc, char *argv[])
73 {
74         tdm_client *client;
75         tdm_client_error error;
76         int fd = -1;
77         struct pollfd fds;
78         int sync;
79
80         if (argc < 2) {
81                 printf("Usage: %s\n", argv[0]);
82                 printf("\t%s 0 : non-sync\n", argv[0]);
83                 printf("\t%s 1 : sync\n", argv[0]);
84                 exit(1);
85         }
86
87         sync = atoi(argv[1]);
88
89         client = tdm_client_create(&error);
90         if (error != TDM_CLIENT_ERROR_NONE) {
91                 printf("tdm_client_create failed\n");
92                 exit(1);
93         }
94
95         error = tdm_client_get_fd(client, &fd);
96         if (error != TDM_CLIENT_ERROR_NONE || fd < 0) {
97                 printf("tdm_client_get_fd failed\n");
98                 goto done;
99         }
100
101         fds.events = POLLIN;
102         fds.fd = fd;
103         fds.revents = 0;
104
105         while (1) {
106                 int ret;
107
108                 error = tdm_client_wait_vblank(client, "unknown-0", 1, 1, sync,
109                                                _client_vblank_handler, NULL);
110                 if (error != TDM_CLIENT_ERROR_NONE) {
111                         printf("tdm_client_wait_vblank failed\n");
112                         goto done;
113                 }
114
115                 if (!sync) {
116                         ret = poll(&fds, 1, -1);
117                         if (ret < 0) {
118                                 if (errno == EBUSY)  /* normal case */
119                                         continue;
120                                 else {
121                                         printf("poll failed: %m\n");
122                                         goto done;
123                                 }
124                         }
125
126                         error = tdm_client_handle_events(client);
127                         if (error != TDM_CLIENT_ERROR_NONE)
128                                 printf("tdm_client_handle_events failed\n");
129                 }
130         }
131
132 done:
133         tdm_client_destroy(client);
134         return 0;
135 }