1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* Copyright © 2012 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <poll.h>
#include <wayland-client.h>
#include <GLES2/gl2.h> /* needed for GLfloat */
#include <linux/input.h>
struct display {
struct wl_display *display;
struct wl_compositor *compositor;
struct input *input;
struct output *output;
};
struct input {
struct wl_seat *seat;
struct wl_pointer *pointer;
struct wl_keyboard *keyboard;
GLfloat x, y;
uint32_t button_mask;
struct surface *pointer_focus;
struct surface *keyboard_focus;
};
struct output {
struct wl_output *output;
int x, y;
int width, height;
};
struct surface {
struct wl_surface *surface;
struct output *output;
};
static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t x, wl_fixed_t y)
{
struct input *input = data;
input->pointer_focus = wl_surface_get_user_data(surface);
input->x = wl_fixed_to_double(x);
input->y = wl_fixed_to_double(y);
}
static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct input *input = data;
input->pointer_focus = NULL;
}
static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
struct input *input = data;
input->x = wl_fixed_to_double(x);
input->y = wl_fixed_to_double(y);
}
static void
pointer_handle_button(void *data, struct wl_pointer *pointer,
uint32_t serial, uint32_t time, uint32_t button,
uint32_t state_w)
{
struct input *input = data;
uint32_t bit;
enum wl_pointer_button_state state = state_w;
bit = 1 << (button - BTN_LEFT);
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
input->button_mask |= bit;
else
input->button_mask &= ~bit;
}
static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
}
static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size)
{
close(fd);
}
static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
struct input *input = data;
input->keyboard_focus = wl_surface_get_user_data(surface);
}
static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface)
{
struct input *input = data;
input->keyboard_focus = NULL;
}
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t state)
{
}
static const struct wl_pointer_listener pointer_listener = {
pointer_handle_enter,
pointer_handle_leave,
pointer_handle_motion,
pointer_handle_button,
pointer_handle_axis,
};
static const struct wl_keyboard_listener keyboard_listener = {
keyboard_handle_keymap,
keyboard_handle_enter,
keyboard_handle_leave,
keyboard_handle_key,
};
static void
seat_handle_capabilities(void *data, struct wl_seat *seat,
enum wl_seat_capability caps)
{
struct input *input = data;
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
input->pointer = wl_seat_get_pointer(seat);
wl_pointer_set_user_data(input->pointer, input);
wl_pointer_add_listener(input->pointer, &pointer_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
wl_pointer_destroy(input->pointer);
input->pointer = NULL;
}
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
input->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_set_user_data(input->keyboard, input);
wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
wl_keyboard_destroy(input->keyboard);
input->keyboard = NULL;
}
}
static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
};
static void
output_handle_geometry(void *data,
struct wl_output *wl_output,
int x, int y,
int physical_width,
int physical_height,
int subpixel,
const char *make,
const char *model)
{
struct output *output = data;
output->x = x;
output->y = y;
}
static void
output_handle_mode(void *data,
struct wl_output *wl_output,
uint32_t flags,
int width,
int height,
int refresh)
{
struct output *output = data;
if (flags & WL_OUTPUT_MODE_CURRENT) {
output->width = width;
output->height = height;
}
}
static const struct wl_output_listener output_listener = {
output_handle_geometry,
output_handle_mode
};
static void
handle_global(struct wl_display *_display, uint32_t id,
const char *interface, uint32_t version, void *data)
{
struct display *display = data;
struct input *input;
struct output *output;
if (strcmp(interface, "wl_compositor") == 0) {
display->compositor =
wl_display_bind(display->display,
id, &wl_compositor_interface);
} else if (strcmp(interface, "wl_seat") == 0) {
input = malloc(sizeof *input);
input->seat = wl_display_bind(display->display, id,
&wl_seat_interface);
input->pointer_focus = NULL;
input->keyboard_focus = NULL;
wl_seat_add_listener(input->seat, &seat_listener, input);
display->input = input;
} else if (strcmp(interface, "wl_output") == 0) {
output = malloc(sizeof *output);
output->output = wl_display_bind(display->display,
id, &wl_output_interface);
wl_output_add_listener(output->output,
&output_listener, output);
display->output = output;
fprintf(stderr, "created output global %p\n", display->output);
}
}
static void
surface_enter(void *data,
struct wl_surface *wl_surface, struct wl_output *output)
{
struct surface *surface = data;
surface->output = wl_output_get_user_data(output);
fprintf(stderr, "got surface enter, output %p\n", surface->output);
}
static void
surface_leave(void *data,
struct wl_surface *wl_surface, struct wl_output *output)
{
struct surface *surface = data;
surface->output = NULL;
}
static const struct wl_surface_listener surface_listener = {
surface_enter,
surface_leave
};
static void
create_surface(int fd, struct display *display)
{
struct surface *surface;
char buf[64];
int len;
surface = malloc(sizeof *surface);
assert(surface);
surface->surface = wl_compositor_create_surface(display->compositor);
wl_surface_add_listener(surface->surface, &surface_listener, surface);
wl_display_flush(display->display);
len = snprintf(buf, sizeof buf, "surface %d\n",
wl_proxy_get_id((struct wl_proxy *) surface->surface));
assert(write(fd, buf, len) == len);
poll(NULL, 0, 100); /* Wait for next frame where we'll get events. */
wl_display_roundtrip(display->display);
assert(surface->output == display->output);
assert(display->input->pointer_focus == surface);
assert(display->input->x == 50);
assert(display->input->y == 50);
}
int main(int argc, char *argv[])
{
struct display *display;
char buf[256], *p;
int ret, fd;
display = malloc(sizeof *display);
assert(display);
display->display = wl_display_connect(NULL);
assert(display->display);
wl_display_add_global_listener(display->display,
handle_global, display);
wl_display_iterate(display->display, WL_DISPLAY_READABLE);
wl_display_roundtrip(display->display);
fd = 0;
p = getenv("TEST_SOCKET");
if (p)
fd = strtol(p, NULL, 0);
while (1) {
ret = read(fd, buf, sizeof buf);
if (ret == -1) {
fprintf(stderr, "read error: fd %d, %m\n", fd);
return -1;
}
fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
if (strncmp(buf, "bye\n", ret) == 0) {
return 0;
} else if (strncmp(buf, "create-surface\n", ret) == 0) {
create_surface(fd, display);
} else {
fprintf(stderr, "unknown command %.*s\n", ret, buf);
return -1;
}
}
assert(0);
}
|