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
|
/* An input event callback that destroys its own surface. The app runner
* must not touch the surface's entry after the callback, and must not
* deliver the surface's remaining input. Needs a Wayland compositor. */
#include "platform/wayland/internal.h"
#include "yasl/app.h"
#include <assert.h>
#include <stdlib.h>
struct test_state {
int keys;
int buttons, motions, resets;
int end;
};
static void push_key(struct yasl_surface *s)
{
struct yasl_input_event ev = {
.surface = s,
.kind = YASL_INPUT_KEY,
.key = { .key = YASL_KEY_A, .action = YASL_KEY_PRESS },
};
assert(input_queue_push(&((struct wayland_surface *)s)->input, &ev));
}
static int on_init(void *ud, struct yasl_display *d)
{
struct test_state *state = ud;
if (state->end) {
struct wayland_display *display = (struct wayland_display *)d;
if (state->end == 1)
display->cbs->quit(display->ud);
else
display->cbs->lost(display->ud);
return YASL_APP_CONTINUE;
}
struct yasl_surface *s = yasl_surface_create_toplevel(d,
&(struct yasl_window_spec){ .title = "App Input Test", .size = {64, 64} });
assert(s);
struct input_queue *queue = &((struct wayland_surface *)s)->input;
input_queue_clear(queue);
struct yasl_input_event event = {.surface = s, .kind = YASL_INPUT_MOUSE,
.mouse = {.kind = YASL_PTR_PRESS, .device = YASL_PTR_DEV_ERASER,
.pos = {12, 34}, .pen = {.pressure = 50000, .tilt_x = -20, .tilt_y = 30}}};
assert(input_queue_push(queue, &event));
event.mouse.kind = YASL_PTR_MOTION;
event.mouse.pos = (struct yasl_vector2i){15, 39};
assert(input_queue_push(queue, &event));
struct yasl_input_event reset = {.surface = s, .kind = YASL_INPUT_RESET};
assert(input_queue_push(queue, &reset));
event.mouse.device = YASL_PTR_DEV_PEN;
event.mouse.pos = (struct yasl_vector2i){100, 200};
assert(input_queue_push(queue, &event));
push_key(s);
push_key(s);
return YASL_APP_CONTINUE;
}
static int on_event(void *ud, const struct yasl_event *ev)
{
struct test_state *state = ud;
if (ev->type == YASL_EVENT_MOUSE_BUTTON) {
assert(ev->mouse_button.device == YASL_PTR_DEV_ERASER);
assert(ev->mouse_button.pressed && ev->mouse_button.button == 0);
assert(ev->mouse_button.pos.x == 12 && ev->mouse_button.pos.y == 34);
assert(ev->mouse_button.pen.pressure == 50000);
assert(ev->mouse_button.pen.tilt_x == -20 && ev->mouse_button.pen.tilt_y == 30);
state->buttons++;
} else if (ev->type == YASL_EVENT_MOUSE_MOTION) {
if (!state->motions) {
assert(ev->mouse_motion.device == YASL_PTR_DEV_ERASER);
assert(ev->mouse_motion.delta.x == 3 && ev->mouse_motion.delta.y == 5);
} else {
assert(ev->mouse_motion.device == YASL_PTR_DEV_PEN);
assert(!ev->mouse_motion.delta.x && !ev->mouse_motion.delta.y);
}
assert(ev->mouse_motion.pen.pressure == 50000);
state->motions++;
} else if (ev->type == YASL_EVENT_INPUT_RESET) {
state->resets++;
} else if (ev->type == YASL_EVENT_KEY) {
state->keys++;
yasl_surface_destroy(ev->surface);
}
return YASL_APP_CONTINUE;
}
int main(void)
{
if (!getenv("WAYLAND_DISPLAY"))
return 77;
struct test_state state = {0};
int res = yasl_run(&(struct yasl_app_desc){
.app_id = "org.yasl.test.app-input",
.backend = YASL_BACKEND_WAYLAND,
.userdata = &state,
.init = on_init,
.event = on_event,
});
assert(res == 0);
assert(state.keys == 1);
assert(state.buttons == 1 && state.motions == 2 && state.resets == 1);
for (int end = 1; end <= 2; end++) {
state = (struct test_state){.end = end};
res = yasl_run(&(struct yasl_app_desc){
.backend = YASL_BACKEND_WAYLAND, .userdata = &state, .init = on_init,
});
assert(end == 1 ? res == 0 : res < 0);
}
return 0;
}
|