fcedae5331a8baa681e8fe4678cfd355587cfc94
[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
79         client = tdm_client_create(&error);
80         if (error != TDM_CLIENT_ERROR_NONE) {
81                 printf("tdm_client_create failed\n");
82                 exit(1);
83         }
84
85         error = tdm_client_get_fd(client, &fd);
86         if (error != TDM_CLIENT_ERROR_NONE || fd < 0) {
87                 printf("tdm_client_get_fd failed\n");
88                 goto done;
89         }
90
91         fds.events = POLLIN;
92         fds.fd = fd;
93         fds.revents = 0;
94
95         while (1) {
96                 int ret;
97
98                 error = tdm_client_wait_vblank(client, "unknown-0", 1, 1, 0,
99                                                _client_vblank_handler, NULL);
100                 if (error != TDM_CLIENT_ERROR_NONE) {
101                         printf("tdm_client_wait_vblank failed\n");
102                         goto done;
103                 }
104
105                 ret = poll(&fds, 1, -1);
106                 if (ret < 0) {
107                         if (errno == EBUSY)  /* normal case */
108                                 continue;
109                         else {
110                                 printf("poll failed: %m\n");
111                                 goto done;
112                         }
113                 }
114
115                 error = tdm_client_handle_events(client);
116                 if (error != TDM_CLIENT_ERROR_NONE)
117                         printf("tdm_client_handle_events failed\n");
118         }
119
120 done:
121         tdm_client_destroy(client);
122         return 0;
123 }