• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2011 Benjamin Franzke
3  * Copyright © 2010 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "config.h"
26 
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdbool.h>
32 #include <assert.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35 #include <signal.h>
36 #include <errno.h>
37 
38 #include <wayland-client.h>
39 #include "shared/os-compatibility.h"
40 #include <libweston/zalloc.h>
41 #include "xdg-shell-client-protocol.h"
42 #include "fullscreen-shell-unstable-v1-client-protocol.h"
43 
44 struct display {
45 	struct wl_display *display;
46 	struct wl_registry *registry;
47 	struct wl_compositor *compositor;
48 	struct xdg_wm_base *wm_base;
49 	struct zwp_fullscreen_shell_v1 *fshell;
50 	struct wl_shm *shm;
51 	bool has_xrgb;
52 };
53 
54 struct buffer {
55 	struct wl_buffer *buffer;
56 	void *shm_data;
57 	int busy;
58 };
59 
60 struct window {
61 	struct display *display;
62 	int width, height;
63 	struct wl_surface *surface;
64 	struct xdg_surface *xdg_surface;
65 	struct xdg_toplevel *xdg_toplevel;
66 	struct buffer buffers[2];
67 	struct buffer *prev_buffer;
68 	struct wl_callback *callback;
69 	bool wait_for_configure;
70 };
71 
72 static int running = 1;
73 
74 static void
75 redraw(void *data, struct wl_callback *callback, uint32_t time);
76 
77 static void
buffer_release(void * data,struct wl_buffer * buffer)78 buffer_release(void *data, struct wl_buffer *buffer)
79 {
80 	struct buffer *mybuf = data;
81 
82 	mybuf->busy = 0;
83 }
84 
85 static const struct wl_buffer_listener buffer_listener = {
86 	buffer_release
87 };
88 
89 static int
create_shm_buffer(struct display * display,struct buffer * buffer,int width,int height,uint32_t format)90 create_shm_buffer(struct display *display, struct buffer *buffer,
91 		  int width, int height, uint32_t format)
92 {
93 	struct wl_shm_pool *pool;
94 	int fd, size, stride;
95 	void *data;
96 
97 	stride = width * 4;
98 	size = stride * height;
99 
100 	fd = os_create_anonymous_file(size);
101 	if (fd < 0) {
102 		fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
103 			size, strerror(errno));
104 		return -1;
105 	}
106 
107 	data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
108 	if (data == MAP_FAILED) {
109 		fprintf(stderr, "mmap failed: %s\n", strerror(errno));
110 		close(fd);
111 		return -1;
112 	}
113 
114 	pool = wl_shm_create_pool(display->shm, fd, size);
115 	buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
116 						   width, height,
117 						   stride, format);
118 	wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
119 	wl_shm_pool_destroy(pool);
120 	close(fd);
121 
122 	buffer->shm_data = data;
123 
124 	return 0;
125 }
126 
127 static void
handle_xdg_surface_configure(void * data,struct xdg_surface * surface,uint32_t serial)128 handle_xdg_surface_configure(void *data, struct xdg_surface *surface,
129 			     uint32_t serial)
130 {
131 	struct window *window = data;
132 
133 	xdg_surface_ack_configure(surface, serial);
134 
135 	if (window->wait_for_configure) {
136 		redraw(window, NULL, 0);
137 		window->wait_for_configure = false;
138 	}
139 }
140 
141 static const struct xdg_surface_listener xdg_surface_listener = {
142 	handle_xdg_surface_configure,
143 };
144 
145 static void
handle_xdg_toplevel_configure(void * data,struct xdg_toplevel * xdg_toplevel,int32_t width,int32_t height,struct wl_array * state)146 handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
147 			      int32_t width, int32_t height,
148 			      struct wl_array *state)
149 {
150 }
151 
152 static void
handle_xdg_toplevel_close(void * data,struct xdg_toplevel * xdg_toplevel)153 handle_xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
154 {
155 	running = 0;
156 }
157 
158 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
159 	handle_xdg_toplevel_configure,
160 	handle_xdg_toplevel_close,
161 };
162 
163 static struct window *
create_window(struct display * display,int width,int height)164 create_window(struct display *display, int width, int height)
165 {
166 	struct window *window;
167 
168 	window = zalloc(sizeof *window);
169 	if (!window)
170 		return NULL;
171 
172 	window->callback = NULL;
173 	window->display = display;
174 	window->width = width;
175 	window->height = height;
176 	window->surface = wl_compositor_create_surface(display->compositor);
177 
178 	if (display->wm_base) {
179 		window->xdg_surface =
180 			xdg_wm_base_get_xdg_surface(display->wm_base,
181 						    window->surface);
182 		assert(window->xdg_surface);
183 		xdg_surface_add_listener(window->xdg_surface,
184 					 &xdg_surface_listener, window);
185 
186 		window->xdg_toplevel =
187 			xdg_surface_get_toplevel(window->xdg_surface);
188 		assert(window->xdg_toplevel);
189 		xdg_toplevel_add_listener(window->xdg_toplevel,
190 					  &xdg_toplevel_listener, window);
191 
192 		xdg_toplevel_set_title(window->xdg_toplevel, "simple-shm");
193 		wl_surface_commit(window->surface);
194 		window->wait_for_configure = true;
195 	} else if (display->fshell) {
196 		zwp_fullscreen_shell_v1_present_surface(display->fshell,
197 							window->surface,
198 							ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
199 							NULL);
200 	} else {
201 		assert(0);
202 	}
203 
204 	return window;
205 }
206 
207 static void
destroy_window(struct window * window)208 destroy_window(struct window *window)
209 {
210 	if (window->callback)
211 		wl_callback_destroy(window->callback);
212 
213 	if (window->buffers[0].buffer)
214 		wl_buffer_destroy(window->buffers[0].buffer);
215 	if (window->buffers[1].buffer)
216 		wl_buffer_destroy(window->buffers[1].buffer);
217 
218 	if (window->xdg_toplevel)
219 		xdg_toplevel_destroy(window->xdg_toplevel);
220 	if (window->xdg_surface)
221 		xdg_surface_destroy(window->xdg_surface);
222 	wl_surface_destroy(window->surface);
223 	free(window);
224 }
225 
226 static struct buffer *
window_next_buffer(struct window * window)227 window_next_buffer(struct window *window)
228 {
229 	struct buffer *buffer;
230 	int ret = 0;
231 
232 	if (!window->buffers[0].busy)
233 		buffer = &window->buffers[0];
234 	else if (!window->buffers[1].busy)
235 		buffer = &window->buffers[1];
236 	else
237 		return NULL;
238 
239 	if (!buffer->buffer) {
240 		ret = create_shm_buffer(window->display, buffer,
241 					window->width, window->height,
242 					WL_SHM_FORMAT_XRGB8888);
243 
244 		if (ret < 0)
245 			return NULL;
246 
247 		/* paint the padding */
248 		memset(buffer->shm_data, 0xff,
249 		       window->width * window->height * 4);
250 	}
251 
252 	return buffer;
253 }
254 
255 static void
paint_pixels(void * image,int padding,int width,int height,uint32_t time)256 paint_pixels(void *image, int padding, int width, int height, uint32_t time)
257 {
258 	const int halfh = padding + (height - padding * 2) / 2;
259 	const int halfw = padding + (width  - padding * 2) / 2;
260 	int ir, or;
261 	uint32_t *pixel = image;
262 	int y;
263 
264 	/* squared radii thresholds */
265 	or = (halfw < halfh ? halfw : halfh) - 8;
266 	ir = or - 32;
267 	or *= or;
268 	ir *= ir;
269 
270 	pixel += padding * width;
271 	for (y = padding; y < height - padding; y++) {
272 		int x;
273 		int y2 = (y - halfh) * (y - halfh);
274 
275 		pixel += padding;
276 		for (x = padding; x < width - padding; x++) {
277 			uint32_t v;
278 
279 			/* squared distance from center */
280 			int r2 = (x - halfw) * (x - halfw) + y2;
281 
282 			if (r2 < ir)
283 				v = (r2 / 32 + time / 64) * 0x0080401;
284 			else if (r2 < or)
285 				v = (y + time / 32) * 0x0080401;
286 			else
287 				v = (x + time / 16) * 0x0080401;
288 			v &= 0x00ffffff;
289 
290 			/* cross if compositor uses X from XRGB as alpha */
291 			if (abs(x - y) > 6 && abs(x + y - height) > 6)
292 				v |= 0xff000000;
293 
294 			*pixel++ = v;
295 		}
296 
297 		pixel += padding;
298 	}
299 }
300 
301 static const struct wl_callback_listener frame_listener;
302 
303 static void
redraw(void * data,struct wl_callback * callback,uint32_t time)304 redraw(void *data, struct wl_callback *callback, uint32_t time)
305 {
306 	struct window *window = data;
307 	struct buffer *buffer;
308 
309 	buffer = window_next_buffer(window);
310 	if (!buffer) {
311 		fprintf(stderr,
312 			!callback ? "Failed to create the first buffer.\n" :
313 			"Both buffers busy at redraw(). Server bug?\n");
314 		abort();
315 	}
316 
317 	paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
318 
319 	wl_surface_attach(window->surface, buffer->buffer, 0, 0);
320 	wl_surface_damage(window->surface,
321 			  20, 20, window->width - 40, window->height - 40);
322 
323 	if (callback)
324 		wl_callback_destroy(callback);
325 
326 	window->callback = wl_surface_frame(window->surface);
327 	wl_callback_add_listener(window->callback, &frame_listener, window);
328 	wl_surface_commit(window->surface);
329 	buffer->busy = 1;
330 }
331 
332 static const struct wl_callback_listener frame_listener = {
333 	redraw
334 };
335 
336 static void
shm_format(void * data,struct wl_shm * wl_shm,uint32_t format)337 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
338 {
339 	struct display *d = data;
340 
341 	if (format == WL_SHM_FORMAT_XRGB8888)
342 		d->has_xrgb = true;
343 }
344 
345 struct wl_shm_listener shm_listener = {
346 	shm_format
347 };
348 
349 static void
xdg_wm_base_ping(void * data,struct xdg_wm_base * shell,uint32_t serial)350 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
351 {
352 	xdg_wm_base_pong(shell, serial);
353 }
354 
355 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
356 	xdg_wm_base_ping,
357 };
358 
359 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)360 registry_handle_global(void *data, struct wl_registry *registry,
361 		       uint32_t id, const char *interface, uint32_t version)
362 {
363 	struct display *d = data;
364 
365 	if (strcmp(interface, "wl_compositor") == 0) {
366 		d->compositor =
367 			wl_registry_bind(registry,
368 					 id, &wl_compositor_interface, 1);
369 	} else if (strcmp(interface, "xdg_wm_base") == 0) {
370 		d->wm_base = wl_registry_bind(registry,
371 					      id, &xdg_wm_base_interface, 1);
372 		xdg_wm_base_add_listener(d->wm_base, &xdg_wm_base_listener, d);
373 	} else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
374 		d->fshell = wl_registry_bind(registry,
375 					     id, &zwp_fullscreen_shell_v1_interface, 1);
376 	} else if (strcmp(interface, "wl_shm") == 0) {
377 		d->shm = wl_registry_bind(registry,
378 					  id, &wl_shm_interface, 1);
379 		wl_shm_add_listener(d->shm, &shm_listener, d);
380 	}
381 }
382 
383 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)384 registry_handle_global_remove(void *data, struct wl_registry *registry,
385 			      uint32_t name)
386 {
387 }
388 
389 static const struct wl_registry_listener registry_listener = {
390 	registry_handle_global,
391 	registry_handle_global_remove
392 };
393 
394 static struct display *
create_display(void)395 create_display(void)
396 {
397 	struct display *display;
398 
399 	display = malloc(sizeof *display);
400 	if (display == NULL) {
401 		fprintf(stderr, "out of memory\n");
402 		exit(1);
403 	}
404 	display->display = wl_display_connect(NULL);
405 	assert(display->display);
406 
407 	display->has_xrgb = false;
408 	display->registry = wl_display_get_registry(display->display);
409 	wl_registry_add_listener(display->registry,
410 				 &registry_listener, display);
411 	wl_display_roundtrip(display->display);
412 	if (display->shm == NULL) {
413 		fprintf(stderr, "No wl_shm global\n");
414 		exit(1);
415 	}
416 
417 	wl_display_roundtrip(display->display);
418 
419 	/*
420 	 * Why do we need two roundtrips here?
421 	 *
422 	 * wl_display_get_registry() sends a request to the server, to which
423 	 * the server replies by emitting the wl_registry.global events.
424 	 * The first wl_display_roundtrip() sends wl_display.sync. The server
425 	 * first processes the wl_display.get_registry which includes sending
426 	 * the global events, and then processes the sync. Therefore when the
427 	 * sync (roundtrip) returns, we are guaranteed to have received and
428 	 * processed all the global events.
429 	 *
430 	 * While we are inside the first wl_display_roundtrip(), incoming
431 	 * events are dispatched, which causes registry_handle_global() to
432 	 * be called for each global. One of these globals is wl_shm.
433 	 * registry_handle_global() sends wl_registry.bind request for the
434 	 * wl_shm global. However, wl_registry.bind request is sent after
435 	 * the first wl_display.sync, so the reply to the sync comes before
436 	 * the initial events of the wl_shm object.
437 	 *
438 	 * The initial events that get sent as a reply to binding to wl_shm
439 	 * include wl_shm.format. These tell us which pixel formats are
440 	 * supported, and we need them before we can create buffers. They
441 	 * don't change at runtime, so we receive them as part of init.
442 	 *
443 	 * When the reply to the first sync comes, the server may or may not
444 	 * have sent the initial wl_shm events. Therefore we need the second
445 	 * wl_display_roundtrip() call here.
446 	 *
447 	 * The server processes the wl_registry.bind for wl_shm first, and
448 	 * the second wl_display.sync next. During our second call to
449 	 * wl_display_roundtrip() the initial wl_shm events are received and
450 	 * processed. Finally, when the reply to the second wl_display.sync
451 	 * arrives, it guarantees we have processed all wl_shm initial events.
452 	 *
453 	 * This sequence contains two examples on how wl_display_roundtrip()
454 	 * can be used to guarantee, that all reply events to a request
455 	 * have been received and processed. This is a general Wayland
456 	 * technique.
457 	 */
458 
459 	if (!display->has_xrgb) {
460 		fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
461 		exit(1);
462 	}
463 
464 	return display;
465 }
466 
467 static void
destroy_display(struct display * display)468 destroy_display(struct display *display)
469 {
470 	if (display->shm)
471 		wl_shm_destroy(display->shm);
472 
473 	if (display->wm_base)
474 		xdg_wm_base_destroy(display->wm_base);
475 
476 	if (display->fshell)
477 		zwp_fullscreen_shell_v1_release(display->fshell);
478 
479 	if (display->compositor)
480 		wl_compositor_destroy(display->compositor);
481 
482 	wl_registry_destroy(display->registry);
483 	wl_display_flush(display->display);
484 	wl_display_disconnect(display->display);
485 	free(display);
486 }
487 
488 static void
signal_int(int signum)489 signal_int(int signum)
490 {
491 	running = 0;
492 }
493 
494 int
main(int argc,char ** argv)495 main(int argc, char **argv)
496 {
497 	struct sigaction sigint;
498 	struct display *display;
499 	struct window *window;
500 	int ret = 0;
501 
502 	display = create_display();
503 	window = create_window(display, 250, 250);
504 	if (!window)
505 		return 1;
506 
507 	sigint.sa_handler = signal_int;
508 	sigemptyset(&sigint.sa_mask);
509 	sigint.sa_flags = SA_RESETHAND;
510 	sigaction(SIGINT, &sigint, NULL);
511 
512 	/* Initialise damage to full surface, so the padding gets painted */
513 	wl_surface_damage(window->surface, 0, 0,
514 			  window->width, window->height);
515 
516 	if (!window->wait_for_configure)
517 		redraw(window, NULL, 0);
518 
519 	while (running && ret != -1)
520 		ret = wl_display_dispatch(display->display);
521 
522 	fprintf(stderr, "simple-shm exiting\n");
523 
524 	destroy_window(window);
525 	destroy_display(display);
526 
527 	return 0;
528 }
529