1 #include <GLES2/gl2.h>
2 #include <linux/input-event-codes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <wayland-client.h>
7 #include <wayland-egl.h>
8 #include <wlr/render/egl.h>
9 #include "xdg-shell-client-protocol.h"
10 #include "pointer-constraints-unstable-v1-client-protocol.h"
11 
12 static int width = 512, height = 512;
13 
14 static struct wl_compositor *compositor = NULL;
15 static struct wl_seat *seat = NULL;
16 static struct xdg_wm_base *wm_base = NULL;
17 static struct zwp_pointer_constraints_v1 *pointer_constraints = NULL;
18 
19 struct wlr_egl egl;
20 struct wl_egl_window *egl_window;
21 struct wlr_egl_surface *egl_surface;
22 struct zwp_locked_pointer_v1* locked_pointer;
23 struct zwp_confined_pointer_v1* confined_pointer;
24 
25 enum {
26 	REGION_TYPE_NONE,
27 	REGION_TYPE_DISJOINT,
28 	REGION_TYPE_JOINT,
29 	REGION_TYPE_MAX
30 } region_type = REGION_TYPE_NONE;
31 
32 struct wl_region *regions[3];
33 
draw(void)34 static void draw(void) {
35 	eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context);
36 
37 	float color[] = {1.0, 1.0, 0.0, 1.0};
38 
39 	glViewport(0, 0, width, height);
40 	glClearColor(color[0], color[1], color[2], 1.0);
41 	glClear(GL_COLOR_BUFFER_BIT);
42 
43 	eglSwapBuffers(egl.display, egl_surface);
44 }
45 
pointer_handle_button(void * data,struct wl_pointer * pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state_w)46 static void pointer_handle_button(void *data, struct wl_pointer *pointer,
47 		uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w) {
48 	struct wl_surface *surface = data;
49 
50 	if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED) {
51 		region_type = (region_type + 1) % REGION_TYPE_MAX;
52 
53 		if (locked_pointer) {
54 			zwp_locked_pointer_v1_set_region(locked_pointer,
55 				regions[region_type]);
56 		} else if (confined_pointer) {
57 			zwp_confined_pointer_v1_set_region(confined_pointer,
58 				regions[region_type]);
59 		}
60 
61 		wl_surface_commit(surface);
62 	}
63 }
64 
pointer_handle_enter(void * data,struct wl_pointer * wl_pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t surface_x,wl_fixed_t surface_y)65 static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
66 		uint32_t serial, struct wl_surface *surface,
67 		wl_fixed_t surface_x, wl_fixed_t surface_y) {
68 	// This space intentionally left blank
69 }
70 
pointer_handle_leave(void * data,struct wl_pointer * wl_pointer,uint32_t serial,struct wl_surface * surface)71 static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
72 		uint32_t serial, struct wl_surface *surface) {
73 	// This space intentionally left blank
74 }
75 
pointer_handle_motion(void * data,struct wl_pointer * wl_pointer,uint32_t time,wl_fixed_t surface_x,wl_fixed_t surface_y)76 static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
77 		uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
78 	// This space intentionally left blank
79 }
80 
pointer_handle_axis(void * data,struct wl_pointer * wl_pointer,uint32_t time,uint32_t axis,wl_fixed_t value)81 static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
82 		uint32_t time, uint32_t axis, wl_fixed_t value) {
83 	// This space intentionally left blank
84 }
85 
pointer_handle_frame(void * data,struct wl_pointer * wl_pointer)86 static void pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) {
87 	// This space intentionally left blank
88 }
89 
pointer_handle_axis_source(void * data,struct wl_pointer * wl_pointer,uint32_t axis_source)90 static void pointer_handle_axis_source(void *data,
91 		struct wl_pointer *wl_pointer, uint32_t axis_source) {
92 	// This space intentionally left blank
93 }
94 
pointer_handle_axis_stop(void * data,struct wl_pointer * wl_pointer,uint32_t time,uint32_t axis)95 static void pointer_handle_axis_stop(void *data,
96 		struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
97 	// This space intentionally left blank
98 }
99 
pointer_handle_axis_discrete(void * data,struct wl_pointer * wl_pointer,uint32_t axis,int32_t discrete)100 static void pointer_handle_axis_discrete(void *data,
101 		struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) {
102 	// This space intentionally left blank
103 }
104 
105 static const struct wl_pointer_listener pointer_listener = {
106 	.enter = pointer_handle_enter,
107 	.leave = pointer_handle_leave,
108 	.motion = pointer_handle_motion,
109 	.button = pointer_handle_button,
110 	.axis = pointer_handle_axis,
111 	.frame = pointer_handle_frame,
112 	.axis_source = pointer_handle_axis_source,
113 	.axis_stop = pointer_handle_axis_stop,
114 	.axis_discrete = pointer_handle_axis_discrete,
115 };
116 
xdg_surface_handle_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)117 static void xdg_surface_handle_configure(void *data,
118 		struct xdg_surface *xdg_surface, uint32_t serial) {
119 	xdg_surface_ack_configure(xdg_surface, serial);
120 	wl_egl_window_resize(egl_window, width, height, 0, 0);
121 	draw();
122 }
123 
124 static const struct xdg_surface_listener xdg_surface_listener = {
125 	.configure = xdg_surface_handle_configure,
126 };
127 
xdg_toplevel_handle_configure(void * data,struct xdg_toplevel * xdg_toplevel,int32_t w,int32_t h,struct wl_array * states)128 static void xdg_toplevel_handle_configure(void *data,
129 		struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h,
130 		struct wl_array *states) {
131 	width = w;
132 	height = h;
133 }
134 
xdg_toplevel_handle_close(void * data,struct xdg_toplevel * xdg_toplevel)135 static void xdg_toplevel_handle_close(void *data,
136 		struct xdg_toplevel *xdg_toplevel) {
137 	exit(EXIT_SUCCESS);
138 }
139 
140 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
141 	.configure = xdg_toplevel_handle_configure,
142 	.close = xdg_toplevel_handle_close,
143 };
144 
handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)145 static void handle_global(void *data, struct wl_registry *registry,
146 		uint32_t name, const char *interface, uint32_t version) {
147 	if (strcmp(interface, wl_compositor_interface.name) == 0) {
148 		compositor = wl_registry_bind(registry, name,
149 			&wl_compositor_interface, 1);
150 	} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
151 		wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
152 	} else if (strcmp(interface, wl_seat_interface.name) == 0) {
153 		seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
154 	} else if (strcmp(interface,
155 			zwp_pointer_constraints_v1_interface.name) == 0) {
156 		pointer_constraints = wl_registry_bind(registry, name,
157 			&zwp_pointer_constraints_v1_interface, version);
158 	}
159 }
160 
161 static const struct wl_registry_listener registry_listener = {
162 	.global = handle_global,
163 	.global_remove = NULL,
164 };
165 
main(int argc,char ** argv)166 int main(int argc, char **argv) {
167 	if (argc != 4) {
168 		goto invalid_args;
169 	}
170 
171 	bool lock;
172 	if (strcmp(argv[1], "lock") == 0) {
173 		lock = true;
174 	} else if (strcmp(argv[1], "confine") == 0) {
175 		lock = false;
176 	} else {
177 		goto invalid_args;
178 	}
179 
180 	enum zwp_pointer_constraints_v1_lifetime lifetime;
181 	if (strcmp(argv[2], "oneshot") == 0) {
182 		lifetime = ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT;
183 	} else if (strcmp(argv[2], "persistent") == 0) {
184 		lifetime = ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT;
185 	} else {
186 		goto invalid_args;
187 	}
188 
189 	if (strcmp(argv[3], "no-region") == 0) {
190 		region_type = REGION_TYPE_NONE;
191 	} else if (strcmp(argv[3], "disjoint-region") == 0) {
192 		region_type = REGION_TYPE_DISJOINT;
193 	} else if (strcmp(argv[3], "joint-region") == 0) {
194 		region_type = REGION_TYPE_JOINT;
195 	}
196 
197 	struct wl_display *display = wl_display_connect(NULL);
198 
199 	struct wl_registry *registry = wl_display_get_registry(display);
200 	wl_registry_add_listener(registry, &registry_listener, NULL);
201 	wl_display_roundtrip(display);
202 
203 	struct wl_region *disjoint_region = wl_compositor_create_region(compositor);
204 	wl_region_add(disjoint_region, 0, 0, 255, 256);
205 	wl_region_add(disjoint_region, 257, 0, 255, 256);
206 	regions[REGION_TYPE_DISJOINT] = disjoint_region;
207 
208 	struct wl_region *joint_region = wl_compositor_create_region(compositor);
209 	wl_region_add(joint_region, 0, 0, 256, 256);
210 	wl_region_add(joint_region, 256, 0, 256, 256);
211 	wl_region_add(joint_region, 256, 256, 256, 256);
212 	regions[REGION_TYPE_JOINT] = joint_region;
213 
214 	wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL,
215 		WL_SHM_FORMAT_ARGB8888);
216 
217 	struct wl_surface *surface = wl_compositor_create_surface(compositor);
218 	struct xdg_surface *xdg_surface =
219 		xdg_wm_base_get_xdg_surface(wm_base, surface);
220 	struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
221 
222 	xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
223 	xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
224 
225 	struct wl_pointer *pointer = wl_seat_get_pointer(seat);
226 	wl_pointer_add_listener(pointer, &pointer_listener, surface);
227 
228 	if (lock) {
229 		locked_pointer = zwp_pointer_constraints_v1_lock_pointer(
230 			pointer_constraints, surface, pointer,
231 			regions[region_type], lifetime);
232 
233 		zwp_locked_pointer_v1_set_cursor_position_hint(locked_pointer,
234 			wl_fixed_from_int(128), wl_fixed_from_int(128));
235 	} else {
236 		confined_pointer = zwp_pointer_constraints_v1_confine_pointer(
237 			pointer_constraints, surface, pointer,
238 			regions[region_type], lifetime);
239 	}
240 
241 	wl_surface_commit(surface);
242 
243 	egl_window = wl_egl_window_create(surface, width, height);
244 	egl_surface = wlr_egl_create_surface(&egl, egl_window);
245 
246 	wl_display_roundtrip(display);
247 
248 	draw();
249 
250 	while (wl_display_dispatch(display) != -1) {}
251 
252 	return EXIT_SUCCESS;
253 
254 invalid_args:
255 	fprintf(stderr, "pointer-constraints <lock | confine> "
256 		"<oneshot | persistent> "
257 		"<no-region | disjoint-rejoin | joint-region>\n");
258 	return EXIT_FAILURE;
259 }
260