fix weston launch error
[platform/upstream/weston.git] / tests / input-timestamps-helper.c
1 /*
2  * Copyright © 2017 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "input-timestamps-helper.h"
34 #include "protocol/input-timestamps-unstable-v1-client-protocol.h"
35 #include "shared/timespec-util.h"
36 #include "shared/zalloc.h"
37 #include "weston-test-client-helper.h"
38
39 struct input_timestamps {
40         struct zwp_input_timestamps_v1 *proxy;
41 };
42
43 static struct zwp_input_timestamps_manager_v1 *
44 get_input_timestamps_manager(struct client *client)
45 {
46         struct global *g;
47         struct global *global_ts = NULL;
48         struct zwp_input_timestamps_manager_v1 *ts = NULL;
49
50         wl_list_for_each(g, &client->global_list, link) {
51                 if (strcmp(g->interface, zwp_input_timestamps_manager_v1_interface.name))
52                         continue;
53
54                 if (global_ts)
55                         assert(!"Multiple input timestamp managers");
56
57                 global_ts = g;
58         }
59
60         assert(global_ts);
61         assert(global_ts->version == 1);
62
63         ts = wl_registry_bind(client->wl_registry, global_ts->name,
64                               &zwp_input_timestamps_manager_v1_interface, 1);
65         assert(ts);
66
67         return ts;
68 }
69
70 static void
71 input_timestamp(void *data,
72                 struct zwp_input_timestamps_v1 *zwp_input_timestamps_v1,
73                 uint32_t tv_sec_hi,
74                 uint32_t tv_sec_lo,
75                 uint32_t tv_nsec)
76 {
77         struct timespec *timestamp = data;
78
79         timespec_from_proto(timestamp, tv_sec_hi, tv_sec_lo,
80                             tv_nsec);
81
82         fprintf(stderr, "test-client: got input timestamp %ld.%ld\n",
83                 timestamp->tv_sec, timestamp->tv_nsec);
84 }
85
86 static const struct zwp_input_timestamps_v1_listener
87 input_timestamps_listener = {
88        .timestamp = input_timestamp,
89 };
90
91 struct input_timestamps *
92 input_timestamps_create_for_keyboard(struct client *client)
93 {
94         struct zwp_input_timestamps_manager_v1 *manager =
95                 get_input_timestamps_manager(client);
96         struct timespec *timestamp= &client->input->keyboard->input_timestamp;
97         struct input_timestamps *input_ts;
98
99         input_ts = zalloc(sizeof *input_ts);
100         assert(input_ts);
101
102         input_ts->proxy =
103                 zwp_input_timestamps_manager_v1_get_keyboard_timestamps(
104                         manager, client->input->keyboard->wl_keyboard);
105         assert(input_ts->proxy);
106
107         zwp_input_timestamps_v1_add_listener(input_ts->proxy,
108                                              &input_timestamps_listener,
109                                              timestamp);
110
111         zwp_input_timestamps_manager_v1_destroy(manager);
112
113         client_roundtrip(client);
114
115         return input_ts;
116 }
117
118 struct input_timestamps *
119 input_timestamps_create_for_pointer(struct client *client)
120 {
121         struct zwp_input_timestamps_manager_v1 *manager =
122                 get_input_timestamps_manager(client);
123         struct timespec *timestamp= &client->input->pointer->input_timestamp;
124         struct input_timestamps *input_ts;
125
126         input_ts = zalloc(sizeof *input_ts);
127         assert(input_ts);
128
129         input_ts->proxy =
130                 zwp_input_timestamps_manager_v1_get_pointer_timestamps(
131                         manager, client->input->pointer->wl_pointer);
132         assert(input_ts->proxy);
133
134         zwp_input_timestamps_v1_add_listener(input_ts->proxy,
135                                              &input_timestamps_listener,
136                                              timestamp);
137
138         zwp_input_timestamps_manager_v1_destroy(manager);
139
140         client_roundtrip(client);
141
142         return input_ts;
143 }
144
145 struct input_timestamps *
146 input_timestamps_create_for_touch(struct client *client)
147 {
148         struct zwp_input_timestamps_manager_v1 *manager =
149                 get_input_timestamps_manager(client);
150         struct timespec *timestamp= &client->input->touch->input_timestamp;
151         struct input_timestamps *input_ts;
152
153         input_ts = zalloc(sizeof *input_ts);
154         assert(input_ts);
155
156         input_ts->proxy =
157                 zwp_input_timestamps_manager_v1_get_touch_timestamps(
158                         manager, client->input->touch->wl_touch);
159         assert(input_ts->proxy);
160
161         zwp_input_timestamps_v1_add_listener(input_ts->proxy,
162                                              &input_timestamps_listener,
163                                              timestamp);
164
165         zwp_input_timestamps_manager_v1_destroy(manager);
166
167         client_roundtrip(client);
168
169         return input_ts;
170 }
171
172 void
173 input_timestamps_destroy(struct input_timestamps *input_ts)
174 {
175         zwp_input_timestamps_v1_destroy(input_ts->proxy);
176         free(input_ts);
177 }