1 /*
2  * Copyright © 2011 Intel Corporation
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 <stdlib.h>
29 #include <stdint.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <signal.h>
39 #include <limits.h>
40 #include <assert.h>
41 #include <X11/Xcursor/Xcursor.h>
42 #include <linux/input.h>
43 
44 #include <libweston/libweston.h>
45 #include "xwayland.h"
46 #include "xwayland-internal-interface.h"
47 
48 #include "cairo-util.h"
49 #include "hash.h"
50 #include "shared/helpers.h"
51 
52 struct wm_size_hints {
53 	uint32_t flags;
54 	int32_t x, y;
55 	int32_t width, height;	/* should set so old wm's don't mess up */
56 	int32_t min_width, min_height;
57 	int32_t max_width, max_height;
58 	int32_t width_inc, height_inc;
59 	struct {
60 		int32_t x;
61 		int32_t y;
62 	} min_aspect, max_aspect;
63 	int32_t base_width, base_height;
64 	int32_t win_gravity;
65 };
66 
67 #define USPosition	(1L << 0)
68 #define USSize		(1L << 1)
69 #define PPosition	(1L << 2)
70 #define PSize		(1L << 3)
71 #define PMinSize	(1L << 4)
72 #define PMaxSize	(1L << 5)
73 #define PResizeInc	(1L << 6)
74 #define PAspect		(1L << 7)
75 #define PBaseSize	(1L << 8)
76 #define PWinGravity	(1L << 9)
77 
78 struct motif_wm_hints {
79 	uint32_t flags;
80 	uint32_t functions;
81 	uint32_t decorations;
82 	int32_t input_mode;
83 	uint32_t status;
84 };
85 
86 #define MWM_HINTS_FUNCTIONS     (1L << 0)
87 #define MWM_HINTS_DECORATIONS   (1L << 1)
88 #define MWM_HINTS_INPUT_MODE    (1L << 2)
89 #define MWM_HINTS_STATUS        (1L << 3)
90 
91 #define MWM_FUNC_ALL            (1L << 0)
92 #define MWM_FUNC_RESIZE         (1L << 1)
93 #define MWM_FUNC_MOVE           (1L << 2)
94 #define MWM_FUNC_MINIMIZE       (1L << 3)
95 #define MWM_FUNC_MAXIMIZE       (1L << 4)
96 #define MWM_FUNC_CLOSE          (1L << 5)
97 
98 #define MWM_DECOR_ALL           (1L << 0)
99 #define MWM_DECOR_BORDER        (1L << 1)
100 #define MWM_DECOR_RESIZEH       (1L << 2)
101 #define MWM_DECOR_TITLE         (1L << 3)
102 #define MWM_DECOR_MENU          (1L << 4)
103 #define MWM_DECOR_MINIMIZE      (1L << 5)
104 #define MWM_DECOR_MAXIMIZE      (1L << 6)
105 
106 #define MWM_DECOR_EVERYTHING \
107 	(MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
108 	 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
109 
110 #define MWM_INPUT_MODELESS 0
111 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
112 #define MWM_INPUT_SYSTEM_MODAL 2
113 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
114 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
115 
116 #define MWM_TEAROFF_WINDOW      (1L<<0)
117 
118 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
119 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
120 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
121 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
122 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
123 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
124 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
125 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
126 #define _NET_WM_MOVERESIZE_MOVE              8   /* movement only */
127 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9   /* size via keyboard */
128 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10   /* move via keyboard */
129 #define _NET_WM_MOVERESIZE_CANCEL           11   /* cancel operation */
130 
131 struct weston_output_weak_ref {
132 	struct weston_output *output;
133 	struct wl_listener destroy_listener;
134 };
135 
136 struct weston_wm_window {
137 	struct weston_wm *wm;
138 	xcb_window_t id;
139 	xcb_window_t frame_id;
140 	struct frame *frame;
141 	cairo_surface_t *cairo_surface;
142 	uint32_t surface_id;
143 	struct weston_surface *surface;
144 	struct weston_desktop_xwayland_surface *shsurf;
145 	struct wl_listener surface_destroy_listener;
146 	struct wl_event_source *repaint_source;
147 	struct wl_event_source *configure_source;
148 	int properties_dirty;
149 	int pid;
150 	char *machine;
151 	char *class;
152 	char *name;
153 	struct weston_wm_window *transient_for;
154 	uint32_t protocols;
155 	xcb_atom_t type;
156 	int width, height;
157 	int x;
158 	int y;
159 	bool pos_dirty;
160 	int map_request_x;
161 	int map_request_y;
162 	struct weston_output_weak_ref legacy_fullscreen_output;
163 	int saved_width, saved_height;
164 	int decorate;
165 	uint32_t last_button_time;
166 	int did_double;
167 	int override_redirect;
168 	int fullscreen;
169 	int has_alpha;
170 	int delete_window;
171 	int maximized_vert;
172 	int maximized_horz;
173 	struct wm_size_hints size_hints;
174 	struct motif_wm_hints motif_hints;
175 	struct wl_list link;
176 };
177 
178 static struct weston_wm_window *
179 get_wm_window(struct weston_surface *surface);
180 
181 static void
182 weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
183 
184 static void
185 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
186 
187 static int
188 legacy_fullscreen(struct weston_wm *wm,
189 		  struct weston_wm_window *window,
190 		  struct weston_output **output_ret);
191 
192 static void
193 xserver_map_shell_surface(struct weston_wm_window *window,
194 			  struct weston_surface *surface);
195 
196 static bool
wm_debug_is_enabled(struct weston_wm * wm)197 wm_debug_is_enabled(struct weston_wm *wm)
198 {
199 	return weston_log_scope_is_enabled(wm->server->wm_debug);
200 }
201 
202 static void __attribute__ ((format (printf, 2, 3)))
wm_printf(struct weston_wm * wm,const char * fmt,...)203 wm_printf(struct weston_wm *wm, const char *fmt, ...)
204 {
205 	va_list ap;
206 	char timestr[128];
207 
208 	if (wm_debug_is_enabled(wm))
209 		weston_log_scope_printf(wm->server->wm_debug, "%s ",
210 				weston_log_scope_timestamp(wm->server->wm_debug,
211 				timestr, sizeof timestr));
212 
213 	va_start(ap, fmt);
214 	weston_log_scope_vprintf(wm->server->wm_debug, fmt, ap);
215 	va_end(ap);
216 }
217 static void
weston_output_weak_ref_init(struct weston_output_weak_ref * ref)218 weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
219 {
220 	ref->output = NULL;
221 }
222 
223 static void
weston_output_weak_ref_clear(struct weston_output_weak_ref * ref)224 weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
225 {
226 	if (!ref->output)
227 		return;
228 
229 	wl_list_remove(&ref->destroy_listener.link);
230 	ref->output = NULL;
231 }
232 
233 static void
weston_output_weak_ref_handle_destroy(struct wl_listener * listener,void * data)234 weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
235 {
236 	struct weston_output_weak_ref *ref;
237 
238 	ref = wl_container_of(listener, ref, destroy_listener);
239 	assert(ref->output == data);
240 
241 	weston_output_weak_ref_clear(ref);
242 }
243 
244 static void
weston_output_weak_ref_set(struct weston_output_weak_ref * ref,struct weston_output * output)245 weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
246 			   struct weston_output *output)
247 {
248 	weston_output_weak_ref_clear(ref);
249 
250 	if (!output)
251 		return;
252 
253 	ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
254 	wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
255 	ref->output = output;
256 }
257 
258 static bool __attribute__ ((warn_unused_result))
wm_lookup_window(struct weston_wm * wm,xcb_window_t hash,struct weston_wm_window ** window)259 wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
260 		 struct weston_wm_window **window)
261 {
262 	*window = hash_table_lookup(wm->window_hash, hash);
263 	if (*window)
264 		return true;
265 	return false;
266 }
267 
268 const char *
get_atom_name(xcb_connection_t * c,xcb_atom_t atom)269 get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
270 {
271 	xcb_get_atom_name_cookie_t cookie;
272 	xcb_get_atom_name_reply_t *reply;
273 	xcb_generic_error_t *e;
274 	static char buffer[64];
275 
276 	if (atom == XCB_ATOM_NONE)
277 		return "None";
278 
279 	cookie = xcb_get_atom_name (c, atom);
280 	reply = xcb_get_atom_name_reply (c, cookie, &e);
281 
282 	if (reply) {
283 		snprintf(buffer, sizeof buffer, "%.*s",
284 			 xcb_get_atom_name_name_length (reply),
285 			 xcb_get_atom_name_name (reply));
286 	} else {
287 		snprintf(buffer, sizeof buffer, "(atom %u)", atom);
288 	}
289 
290 	free(reply);
291 
292 	return buffer;
293 }
294 
295 static xcb_cursor_t
xcb_cursor_image_load_cursor(struct weston_wm * wm,const XcursorImage * img)296 xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
297 {
298 	xcb_connection_t *c = wm->conn;
299 	xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
300 	xcb_screen_t *screen = s.data;
301 	xcb_gcontext_t gc;
302 	xcb_pixmap_t pix;
303 	xcb_render_picture_t pic;
304 	xcb_cursor_t cursor;
305 	int stride = img->width * 4;
306 
307 	pix = xcb_generate_id(c);
308 	xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
309 
310 	pic = xcb_generate_id(c);
311 	xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
312 
313 	gc = xcb_generate_id(c);
314 	xcb_create_gc(c, gc, pix, 0, 0);
315 
316 	xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
317 		      img->width, img->height, 0, 0, 0, 32,
318 		      stride * img->height, (uint8_t *) img->pixels);
319 	xcb_free_gc(c, gc);
320 
321 	cursor = xcb_generate_id(c);
322 	xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
323 
324 	xcb_render_free_picture(c, pic);
325 	xcb_free_pixmap(c, pix);
326 
327 	return cursor;
328 }
329 
330 static xcb_cursor_t
xcb_cursor_images_load_cursor(struct weston_wm * wm,const XcursorImages * images)331 xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
332 {
333 	/* TODO: treat animated cursors as well */
334 	if (images->nimage != 1)
335 		return -1;
336 
337 	return xcb_cursor_image_load_cursor(wm, images->images[0]);
338 }
339 
340 static xcb_cursor_t
xcb_cursor_library_load_cursor(struct weston_wm * wm,const char * file)341 xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
342 {
343 	xcb_cursor_t cursor;
344 	XcursorImages *images;
345 	char *v = NULL;
346 	int size = 0;
347 
348 	if (!file)
349 		return 0;
350 
351 	v = getenv ("XCURSOR_SIZE");
352 	if (v)
353 		size = atoi(v);
354 
355 	if (!size)
356 		size = 32;
357 
358 	images = XcursorLibraryLoadImages (file, NULL, size);
359 	if (!images)
360 		return -1;
361 
362 	cursor = xcb_cursor_images_load_cursor (wm, images);
363 	XcursorImagesDestroy (images);
364 
365 	return cursor;
366 }
367 
368 static unsigned
dump_cardinal_array_elem(FILE * fp,unsigned format,void * arr,unsigned len,unsigned ind)369 dump_cardinal_array_elem(FILE *fp, unsigned format,
370 			 void *arr, unsigned len, unsigned ind)
371 {
372 	const char *comma;
373 
374 	/* If more than 16 elements, print 0-14, ..., last */
375 	if (ind > 14 && ind < len - 1) {
376 		fprintf(fp, ", ...");
377 		return len - 1;
378 	}
379 
380 	comma = ind ? ", " : "";
381 
382 	switch (format) {
383 	case 32:
384 		fprintf(fp, "%s%" PRIu32, comma, ((uint32_t *)arr)[ind]);
385 		break;
386 	case 16:
387 		fprintf(fp, "%s%" PRIu16, comma, ((uint16_t *)arr)[ind]);
388 		break;
389 	case 8:
390 		fprintf(fp, "%s%" PRIu8, comma, ((uint8_t *)arr)[ind]);
391 		break;
392 	default:
393 		fprintf(fp, "%s???", comma);
394 	}
395 
396 	return ind + 1;
397 }
398 
399 static void
dump_cardinal_array(FILE * fp,xcb_get_property_reply_t * reply)400 dump_cardinal_array(FILE *fp, xcb_get_property_reply_t *reply)
401 {
402 	unsigned i = 0;
403 	void *arr;
404 	char *str = NULL;
405 
406 	assert(reply->type == XCB_ATOM_CARDINAL);
407 
408 	arr = xcb_get_property_value(reply);
409 
410 	fprintf(fp, "[");
411 	while (i < reply->value_len)
412 		i = dump_cardinal_array_elem(fp, reply->format,
413 					     arr, reply->value_len, i);
414 	fprintf(fp, "]");
415 
416 	free(str);
417 }
418 
419 void
dump_property(FILE * fp,struct weston_wm * wm,xcb_atom_t property,xcb_get_property_reply_t * reply)420 dump_property(FILE *fp, struct weston_wm *wm,
421 	      xcb_atom_t property, xcb_get_property_reply_t *reply)
422 {
423 	int32_t *incr_value;
424 	const char *text_value, *name;
425 	xcb_atom_t *atom_value;
426 	xcb_window_t *window_value;
427 	int width, len;
428 	uint32_t i;
429 
430 	width = fprintf(fp, "%s: ", get_atom_name(wm->conn, property));
431 	if (reply == NULL) {
432 		fprintf(fp, "(no reply)\n");
433 		return;
434 	}
435 
436 	width += fprintf(fp, "%s/%d, length %d (value_len %d): ",
437 			 get_atom_name(wm->conn, reply->type),
438 			 reply->format,
439 			 xcb_get_property_value_length(reply),
440 			 reply->value_len);
441 
442 	if (reply->type == wm->atom.incr) {
443 		incr_value = xcb_get_property_value(reply);
444 		fprintf(fp, "%d\n", *incr_value);
445 	} else if (reply->type == wm->atom.utf8_string ||
446 	           reply->type == wm->atom.string) {
447 		text_value = xcb_get_property_value(reply);
448 		if (reply->value_len > 40)
449 			len = 40;
450 		else
451 			len = reply->value_len;
452 		fprintf(fp, "\"%.*s\"\n", len, text_value);
453 	} else if (reply->type == XCB_ATOM_ATOM) {
454 		atom_value = xcb_get_property_value(reply);
455 		for (i = 0; i < reply->value_len; i++) {
456 			name = get_atom_name(wm->conn, atom_value[i]);
457 			if (width + strlen(name) + 2 > 78) {
458 				fprintf(fp, "\n    ");
459 				width = 4;
460 			} else if (i > 0) {
461 				width +=  fprintf(fp, ", ");
462 			}
463 
464 			width +=  fprintf(fp, "%s", name);
465 		}
466 		fprintf(fp, "\n");
467 	} else if (reply->type == XCB_ATOM_CARDINAL) {
468 		dump_cardinal_array(fp, reply);
469 	} else if (reply->type == XCB_ATOM_WINDOW && reply->format == 32) {
470 		window_value = xcb_get_property_value(reply);
471 		fprintf(fp, "win %u\n", *window_value);
472 	} else {
473 		fprintf(fp, "huh?\n");
474 	}
475 }
476 
477 static void
read_and_dump_property(FILE * fp,struct weston_wm * wm,xcb_window_t window,xcb_atom_t property)478 read_and_dump_property(FILE *fp, struct weston_wm *wm,
479 		       xcb_window_t window, xcb_atom_t property)
480 {
481 	xcb_get_property_reply_t *reply;
482 	xcb_get_property_cookie_t cookie;
483 
484 	cookie = xcb_get_property(wm->conn, 0, window,
485 				  property, XCB_ATOM_ANY, 0, 2048);
486 	reply = xcb_get_property_reply(wm->conn, cookie, NULL);
487 
488 	dump_property(fp, wm, property, reply);
489 
490 	free(reply);
491 }
492 
493 /* We reuse some predefined, but otherwise useles atoms
494  * as local type placeholders that never touch the X11 server,
495  * to make weston_wm_window_read_properties() less exceptional.
496  */
497 #define TYPE_WM_PROTOCOLS	XCB_ATOM_CUT_BUFFER0
498 #define TYPE_MOTIF_WM_HINTS	XCB_ATOM_CUT_BUFFER1
499 #define TYPE_NET_WM_STATE	XCB_ATOM_CUT_BUFFER2
500 #define TYPE_WM_NORMAL_HINTS	XCB_ATOM_CUT_BUFFER3
501 
502 static void
weston_wm_window_read_properties(struct weston_wm_window * window)503 weston_wm_window_read_properties(struct weston_wm_window *window)
504 {
505 	struct weston_wm *wm = window->wm;
506 
507 #define F(field) (&window->field)
508 	const struct {
509 		xcb_atom_t atom;
510 		xcb_atom_t type;
511 		void *ptr;
512 	} props[] = {
513 		{ XCB_ATOM_WM_CLASS,           XCB_ATOM_STRING,            F(class) },
514 		{ XCB_ATOM_WM_NAME,            XCB_ATOM_STRING,            F(name) },
515 		{ XCB_ATOM_WM_TRANSIENT_FOR,   XCB_ATOM_WINDOW,            F(transient_for) },
516 		{ wm->atom.wm_protocols,       TYPE_WM_PROTOCOLS,          NULL },
517 		{ wm->atom.wm_normal_hints,    TYPE_WM_NORMAL_HINTS,       NULL },
518 		{ wm->atom.net_wm_state,       TYPE_NET_WM_STATE,          NULL },
519 		{ wm->atom.net_wm_window_type, XCB_ATOM_ATOM,              F(type) },
520 		{ wm->atom.net_wm_name,        XCB_ATOM_STRING,            F(name) },
521 		{ wm->atom.net_wm_pid,         XCB_ATOM_CARDINAL,          F(pid) },
522 		{ wm->atom.motif_wm_hints,     TYPE_MOTIF_WM_HINTS,        NULL },
523 		{ wm->atom.wm_client_machine,  XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
524 	};
525 #undef F
526 
527 	xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
528 	xcb_get_property_reply_t *reply;
529 	void *p;
530 	uint32_t *xid;
531 	xcb_atom_t *atom;
532 	uint32_t i;
533 	char name[1024];
534 
535 	if (!window->properties_dirty)
536 		return;
537 	window->properties_dirty = 0;
538 
539 	for (i = 0; i < ARRAY_LENGTH(props); i++)
540 		cookie[i] = xcb_get_property(wm->conn,
541 					     0, /* delete */
542 					     window->id,
543 					     props[i].atom,
544 					     XCB_ATOM_ANY, 0, 2048);
545 
546 	window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
547 	window->size_hints.flags = 0;
548 	window->motif_hints.flags = 0;
549 	window->delete_window = 0;
550 
551 	for (i = 0; i < ARRAY_LENGTH(props); i++)  {
552 		reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
553 		if (!reply)
554 			/* Bad window, typically */
555 			continue;
556 		if (reply->type == XCB_ATOM_NONE) {
557 			/* No such property */
558 			free(reply);
559 			continue;
560 		}
561 
562 		p = props[i].ptr;
563 
564 		switch (props[i].type) {
565 		case XCB_ATOM_WM_CLIENT_MACHINE:
566 		case XCB_ATOM_STRING:
567 			/* FIXME: We're using this for both string and
568 			   utf8_string */
569 			if (*(char **) p)
570 				free(*(char **) p);
571 
572 			*(char **) p =
573 				strndup(xcb_get_property_value(reply),
574 					xcb_get_property_value_length(reply));
575 			break;
576 		case XCB_ATOM_WINDOW:
577 			xid = xcb_get_property_value(reply);
578 			if (!wm_lookup_window(wm, *xid, p))
579 				weston_log("XCB_ATOM_WINDOW contains window"
580 					   " id not found in hash table.\n");
581 			break;
582 		case XCB_ATOM_CARDINAL:
583 		case XCB_ATOM_ATOM:
584 			atom = xcb_get_property_value(reply);
585 			*(xcb_atom_t *) p = *atom;
586 			break;
587 		case TYPE_WM_PROTOCOLS:
588 			atom = xcb_get_property_value(reply);
589 			for (i = 0; i < reply->value_len; i++)
590 				if (atom[i] == wm->atom.wm_delete_window) {
591 					window->delete_window = 1;
592 					break;
593 				}
594 			break;
595 		case TYPE_WM_NORMAL_HINTS:
596 			memcpy(&window->size_hints,
597 			       xcb_get_property_value(reply),
598 			       sizeof window->size_hints);
599 			break;
600 		case TYPE_NET_WM_STATE:
601 			window->fullscreen = 0;
602 			atom = xcb_get_property_value(reply);
603 			for (i = 0; i < reply->value_len; i++) {
604 				if (atom[i] == wm->atom.net_wm_state_fullscreen)
605 					window->fullscreen = 1;
606 				if (atom[i] == wm->atom.net_wm_state_maximized_vert)
607 					window->maximized_vert = 1;
608 				if (atom[i] == wm->atom.net_wm_state_maximized_horz)
609 					window->maximized_horz = 1;
610 			}
611 			break;
612 		case TYPE_MOTIF_WM_HINTS:
613 			memcpy(&window->motif_hints,
614 			       xcb_get_property_value(reply),
615 			       sizeof window->motif_hints);
616 			if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
617 				if (window->motif_hints.decorations & MWM_DECOR_ALL)
618 					/* MWM_DECOR_ALL means all except the other values listed. */
619 					window->decorate =
620 						MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
621 				else
622 					window->decorate =
623 						window->motif_hints.decorations;
624 			}
625 			break;
626 		default:
627 			break;
628 		}
629 		free(reply);
630 	}
631 
632 	if (window->pid > 0) {
633 		gethostname(name, sizeof(name));
634 		for (i = 0; i < sizeof(name); i++) {
635 			if (name[i] == '\0')
636 				break;
637 		}
638 		if (i == sizeof(name))
639 			name[0] = '\0'; /* ignore stupid hostnames */
640 
641 		/* this is only one heuristic to guess the PID of a client is
642 		* valid, assuming it's compliant with icccm and ewmh.
643 		* Non-compliants and remote applications of course fail. */
644 		if (!window->machine || strcmp(window->machine, name))
645 			window->pid = 0;
646 	}
647 }
648 
649 #undef TYPE_WM_PROTOCOLS
650 #undef TYPE_MOTIF_WM_HINTS
651 #undef TYPE_NET_WM_STATE
652 #undef TYPE_WM_NORMAL_HINTS
653 
654 static void
weston_wm_window_get_frame_size(struct weston_wm_window * window,int * width,int * height)655 weston_wm_window_get_frame_size(struct weston_wm_window *window,
656 				int *width, int *height)
657 {
658 	struct theme *t = window->wm->theme;
659 
660 	if (window->fullscreen) {
661 		*width = window->width;
662 		*height = window->height;
663 	} else if (window->decorate && window->frame) {
664 		*width = frame_width(window->frame);
665 		*height = frame_height(window->frame);
666 	} else {
667 		*width = window->width + t->margin * 2;
668 		*height = window->height + t->margin * 2;
669 	}
670 }
671 
672 static void
weston_wm_window_get_child_position(struct weston_wm_window * window,int * x,int * y)673 weston_wm_window_get_child_position(struct weston_wm_window *window,
674 				    int *x, int *y)
675 {
676 	struct theme *t = window->wm->theme;
677 
678 	if (window->fullscreen) {
679 		*x = 0;
680 		*y = 0;
681 	} else if (window->decorate && window->frame) {
682 		frame_interior(window->frame, x, y, NULL, NULL);
683 	} else {
684 		*x = t->margin;
685 		*y = t->margin;
686 	}
687 }
688 
689 static void
weston_wm_window_send_configure_notify(struct weston_wm_window * window)690 weston_wm_window_send_configure_notify(struct weston_wm_window *window)
691 {
692 	xcb_configure_notify_event_t configure_notify;
693 	struct weston_wm *wm = window->wm;
694 	int x, y;
695 
696 	weston_wm_window_get_child_position(window, &x, &y);
697 	configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
698 	configure_notify.pad0 = 0;
699 	configure_notify.event = window->id;
700 	configure_notify.window = window->id;
701 	configure_notify.above_sibling = XCB_WINDOW_NONE;
702 	configure_notify.x = x;
703 	configure_notify.y = y;
704 	configure_notify.width = window->width;
705 	configure_notify.height = window->height;
706 	configure_notify.border_width = 0;
707 	configure_notify.override_redirect = 0;
708 	configure_notify.pad1 = 0;
709 
710 	xcb_send_event(wm->conn, 0, window->id,
711 		       XCB_EVENT_MASK_STRUCTURE_NOTIFY,
712 		       (char *) &configure_notify);
713 }
714 
715 static void
weston_wm_handle_configure_request(struct weston_wm * wm,xcb_generic_event_t * event)716 weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
717 {
718 	xcb_configure_request_event_t *configure_request =
719 		(xcb_configure_request_event_t *) event;
720 	struct weston_wm_window *window;
721 	uint32_t mask, values[16];
722 	int x, y, width, height, i = 0;
723 
724 	wm_printf(wm, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
725 		  configure_request->window,
726 		  configure_request->x, configure_request->y,
727 		  configure_request->width, configure_request->height);
728 
729 	if (!wm_lookup_window(wm, configure_request->window, &window))
730 		return;
731 
732 	if (window->fullscreen) {
733 		weston_wm_window_send_configure_notify(window);
734 		return;
735 	}
736 
737 	if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
738 		window->width = configure_request->width;
739 	if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
740 		window->height = configure_request->height;
741 
742 	if (window->frame)
743 		frame_resize_inside(window->frame, window->width, window->height);
744 
745 	weston_wm_window_get_child_position(window, &x, &y);
746 	values[i++] = x;
747 	values[i++] = y;
748 	values[i++] = window->width;
749 	values[i++] = window->height;
750 	values[i++] = 0;
751 	mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
752 		XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
753 		XCB_CONFIG_WINDOW_BORDER_WIDTH;
754 	if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
755 		values[i++] = configure_request->sibling;
756 		mask |= XCB_CONFIG_WINDOW_SIBLING;
757 	}
758 	if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
759 		values[i++] = configure_request->stack_mode;
760 		mask |= XCB_CONFIG_WINDOW_STACK_MODE;
761 	}
762 
763 	xcb_configure_window(wm->conn, window->id, mask, values);
764 
765 	weston_wm_window_get_frame_size(window, &width, &height);
766 	values[0] = width;
767 	values[1] = height;
768 	mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
769 	xcb_configure_window(wm->conn, window->frame_id, mask, values);
770 
771 	weston_wm_window_schedule_repaint(window);
772 }
773 
774 static int
our_resource(struct weston_wm * wm,uint32_t id)775 our_resource(struct weston_wm *wm, uint32_t id)
776 {
777 	const xcb_setup_t *setup;
778 
779 	setup = xcb_get_setup(wm->conn);
780 
781 	return (id & ~setup->resource_id_mask) == setup->resource_id_base;
782 }
783 
784 static void
weston_wm_handle_configure_notify(struct weston_wm * wm,xcb_generic_event_t * event)785 weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
786 {
787 	xcb_configure_notify_event_t *configure_notify =
788 		(xcb_configure_notify_event_t *) event;
789 	const struct weston_desktop_xwayland_interface *xwayland_api =
790 		wm->server->compositor->xwayland_interface;
791 	struct weston_wm_window *window;
792 
793 	wm_printf(wm, "XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d%s\n",
794 		  configure_notify->window,
795 		  configure_notify->x, configure_notify->y,
796 		  configure_notify->width, configure_notify->height,
797 		  configure_notify->override_redirect ? ", override" : "");
798 
799 	if (!wm_lookup_window(wm, configure_notify->window, &window))
800 		return;
801 
802 	window->x = configure_notify->x;
803 	window->y = configure_notify->y;
804 	window->pos_dirty = false;
805 
806 	if (window->override_redirect) {
807 		window->width = configure_notify->width;
808 		window->height = configure_notify->height;
809 		if (window->frame)
810 			frame_resize_inside(window->frame,
811 					    window->width, window->height);
812 
813 		/* We should check if shsurf has been created because sometimes
814 		 * there are races
815 		 * (configure_notify is sent before xserver_map_surface) */
816 		if (window->shsurf)
817 			xwayland_api->set_xwayland(window->shsurf,
818 						   window->x, window->y);
819 	}
820 }
821 
822 static void
weston_wm_kill_client(struct wl_listener * listener,void * data)823 weston_wm_kill_client(struct wl_listener *listener, void *data)
824 {
825 	struct weston_surface *surface = data;
826 	struct weston_wm_window *window = get_wm_window(surface);
827 	if (!window)
828 		return;
829 
830 	if (window->pid > 0)
831 		kill(window->pid, SIGKILL);
832 }
833 
834 static void
weston_wm_create_surface(struct wl_listener * listener,void * data)835 weston_wm_create_surface(struct wl_listener *listener, void *data)
836 {
837 	struct weston_surface *surface = data;
838 	struct weston_wm *wm =
839 		container_of(listener,
840 			     struct weston_wm, create_surface_listener);
841 	struct weston_wm_window *window;
842 
843 	if (wl_resource_get_client(surface->resource) != wm->server->client)
844 		return;
845 
846 	wm_printf(wm, "XWM: create weston_surface %p\n", surface);
847 
848 	wl_list_for_each(window, &wm->unpaired_window_list, link)
849 		if (window->surface_id ==
850 		    wl_resource_get_id(surface->resource)) {
851 			xserver_map_shell_surface(window, surface);
852 			window->surface_id = 0;
853 			wl_list_remove(&window->link);
854 			break;
855 		}
856 }
857 
858 static void
weston_wm_send_focus_window(struct weston_wm * wm,struct weston_wm_window * window)859 weston_wm_send_focus_window(struct weston_wm *wm,
860 			    struct weston_wm_window *window)
861 {
862 	xcb_client_message_event_t client_message;
863 
864 	if (window) {
865 		uint32_t values[1];
866 
867 		if (window->override_redirect)
868 			return;
869 
870 		client_message.response_type = XCB_CLIENT_MESSAGE;
871 		client_message.format = 32;
872 		client_message.window = window->id;
873 		client_message.type = wm->atom.wm_protocols;
874 		client_message.data.data32[0] = wm->atom.wm_take_focus;
875 		client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
876 
877 		xcb_send_event(wm->conn, 0, window->id,
878 			       XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
879 			       (char *) &client_message);
880 
881 		xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
882 				     window->id, XCB_TIME_CURRENT_TIME);
883 
884 		values[0] = XCB_STACK_MODE_ABOVE;
885 		xcb_configure_window (wm->conn, window->frame_id,
886 				      XCB_CONFIG_WINDOW_STACK_MODE, values);
887 	} else {
888 		xcb_set_input_focus (wm->conn,
889 				     XCB_INPUT_FOCUS_POINTER_ROOT,
890 				     XCB_NONE,
891 				     XCB_TIME_CURRENT_TIME);
892 	}
893 }
894 
895 static void
weston_wm_window_activate(struct wl_listener * listener,void * data)896 weston_wm_window_activate(struct wl_listener *listener, void *data)
897 {
898 	struct weston_surface_activation_data *activation_data = data;
899 	struct weston_surface *surface = activation_data->surface;
900 	struct weston_wm_window *window = NULL;
901 	struct weston_wm *wm =
902 		container_of(listener, struct weston_wm, activate_listener);
903 
904 	if (surface) {
905 		window = get_wm_window(surface);
906 	}
907 
908 	if (window) {
909 		weston_wm_set_net_active_window(wm, window->id);
910 	} else {
911 		weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
912 	}
913 
914 	weston_wm_send_focus_window(wm, window);
915 
916 	if (wm->focus_window) {
917 		if (wm->focus_window->frame)
918 			frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
919 		weston_wm_window_schedule_repaint(wm->focus_window);
920 	}
921 	wm->focus_window = window;
922 	if (wm->focus_window) {
923 		if (wm->focus_window->frame)
924 			frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
925 		weston_wm_window_schedule_repaint(wm->focus_window);
926 	}
927 
928 	xcb_flush(wm->conn);
929 
930 }
931 
932 /** Control Xwayland wl_surface.commit behaviour
933  *
934  * This function sets the "_XWAYLAND_ALLOW_COMMITS" property of the frame window
935  * (not the content window!) to \p allow.
936  *
937  * If the property is set to \c true, Xwayland will commit whenever it likes.
938  * If the property is set to \c false, Xwayland will not commit.
939  * If the property is not set at all, Xwayland assumes it is \c true.
940  *
941  * \param window The XWM window to control.
942  * \param allow Whether Xwayland is allowed to wl_surface.commit for the window.
943  */
944 static void
weston_wm_window_set_allow_commits(struct weston_wm_window * window,bool allow)945 weston_wm_window_set_allow_commits(struct weston_wm_window *window, bool allow)
946 {
947 	struct weston_wm *wm = window->wm;
948 	uint32_t property[1];
949 
950 	assert(window->frame_id != XCB_WINDOW_NONE);
951 
952 	property[0] = allow ? 1 : 0;
953 
954 	xcb_change_property(wm->conn,
955 			    XCB_PROP_MODE_REPLACE,
956 			    window->frame_id,
957 			    wm->atom.allow_commits,
958 			    XCB_ATOM_CARDINAL,
959 			    32, /* format */
960 			    1, property);
961 }
962 
963 #define ICCCM_WITHDRAWN_STATE	0
964 #define ICCCM_NORMAL_STATE	1
965 #define ICCCM_ICONIC_STATE	3
966 
967 static void
weston_wm_window_set_wm_state(struct weston_wm_window * window,int32_t state)968 weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
969 {
970 	struct weston_wm *wm = window->wm;
971 	uint32_t property[2];
972 
973 	property[0] = state;
974 	property[1] = XCB_WINDOW_NONE;
975 
976 	xcb_change_property(wm->conn,
977 			    XCB_PROP_MODE_REPLACE,
978 			    window->id,
979 			    wm->atom.wm_state,
980 			    wm->atom.wm_state,
981 			    32, /* format */
982 			    2, property);
983 }
984 
985 static void
weston_wm_window_set_net_wm_state(struct weston_wm_window * window)986 weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
987 {
988 	struct weston_wm *wm = window->wm;
989 	uint32_t property[3];
990 	int i;
991 
992 	i = 0;
993 	if (window->fullscreen)
994 		property[i++] = wm->atom.net_wm_state_fullscreen;
995 	if (window->maximized_vert)
996 		property[i++] = wm->atom.net_wm_state_maximized_vert;
997 	if (window->maximized_horz)
998 		property[i++] = wm->atom.net_wm_state_maximized_horz;
999 
1000 	xcb_change_property(wm->conn,
1001 			    XCB_PROP_MODE_REPLACE,
1002 			    window->id,
1003 			    wm->atom.net_wm_state,
1004 			    XCB_ATOM_ATOM,
1005 			    32, /* format */
1006 			    i, property);
1007 }
1008 
1009 static void
weston_wm_window_create_frame(struct weston_wm_window * window)1010 weston_wm_window_create_frame(struct weston_wm_window *window)
1011 {
1012 	struct weston_wm *wm = window->wm;
1013 	uint32_t values[3];
1014 	int x, y, width, height;
1015 	int buttons = FRAME_BUTTON_CLOSE;
1016 
1017 	if (window->decorate & MWM_DECOR_MAXIMIZE)
1018 		buttons |= FRAME_BUTTON_MAXIMIZE;
1019 
1020 	window->frame = frame_create(window->wm->theme,
1021 				     window->width, window->height,
1022 				     buttons, window->name, NULL);
1023 	frame_resize_inside(window->frame, window->width, window->height);
1024 
1025 	weston_wm_window_get_frame_size(window, &width, &height);
1026 	weston_wm_window_get_child_position(window, &x, &y);
1027 
1028 	values[0] = wm->screen->black_pixel;
1029 	values[1] =
1030 		XCB_EVENT_MASK_KEY_PRESS |
1031 		XCB_EVENT_MASK_KEY_RELEASE |
1032 		XCB_EVENT_MASK_BUTTON_PRESS |
1033 		XCB_EVENT_MASK_BUTTON_RELEASE |
1034 		XCB_EVENT_MASK_POINTER_MOTION |
1035 		XCB_EVENT_MASK_ENTER_WINDOW |
1036 		XCB_EVENT_MASK_LEAVE_WINDOW |
1037 		XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1038 		XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
1039 	values[2] = wm->colormap;
1040 
1041 	window->frame_id = xcb_generate_id(wm->conn);
1042 	xcb_create_window(wm->conn,
1043 			  32,
1044 			  window->frame_id,
1045 			  wm->screen->root,
1046 			  0, 0,
1047 			  width, height,
1048 			  0,
1049 			  XCB_WINDOW_CLASS_INPUT_OUTPUT,
1050 			  wm->visual_id,
1051 			  XCB_CW_BORDER_PIXEL |
1052 			  XCB_CW_EVENT_MASK |
1053 			  XCB_CW_COLORMAP, values);
1054 
1055 	xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
1056 
1057 	values[0] = 0;
1058 	xcb_configure_window(wm->conn, window->id,
1059 			     XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
1060 
1061 	window->cairo_surface =
1062 		cairo_xcb_surface_create_with_xrender_format(wm->conn,
1063 							     wm->screen,
1064 							     window->frame_id,
1065 							     &wm->format_rgba,
1066 							     width, height);
1067 
1068 	hash_table_insert(wm->window_hash, window->frame_id, window);
1069 }
1070 
1071 /*
1072  * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
1073  * Passing a <0 desktop value deletes the property.
1074  */
1075 static void
weston_wm_window_set_virtual_desktop(struct weston_wm_window * window,int desktop)1076 weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
1077 				     int desktop)
1078 {
1079 	if (desktop >= 0) {
1080 		xcb_change_property(window->wm->conn,
1081 		                    XCB_PROP_MODE_REPLACE,
1082 		                    window->id,
1083 		                    window->wm->atom.net_wm_desktop,
1084 		                    XCB_ATOM_CARDINAL,
1085 		                    32, /* format */
1086 		                    1, &desktop);
1087 	} else {
1088 		xcb_delete_property(window->wm->conn,
1089 		                    window->id,
1090 		                    window->wm->atom.net_wm_desktop);
1091 	}
1092 }
1093 
1094 static void
weston_wm_handle_map_request(struct weston_wm * wm,xcb_generic_event_t * event)1095 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1096 {
1097 	xcb_map_request_event_t *map_request =
1098 		(xcb_map_request_event_t *) event;
1099 	struct weston_wm_window *window;
1100 	struct weston_output *output;
1101 
1102 	if (our_resource(wm, map_request->window)) {
1103 		wm_printf(wm, "XCB_MAP_REQUEST (window %d, ours)\n",
1104 			  map_request->window);
1105 		return;
1106 	}
1107 
1108 	if (!wm_lookup_window(wm, map_request->window, &window))
1109 		return;
1110 
1111 	weston_wm_window_read_properties(window);
1112 
1113 	/* For a new Window, MapRequest happens before the Window is realized
1114 	 * in Xwayland. We do the real xcb_map_window() here as a response to
1115 	 * MapRequest. The Window will get realized (wl_surface created in
1116 	 * Wayland and WL_SURFACE_ID sent in X11) when it has been mapped for
1117 	 * real.
1118 	 *
1119 	 * MapRequest only happens for (X11) unmapped Windows. On UnmapNotify,
1120 	 * we reset shsurf to NULL, so even if X11 connection races far ahead
1121 	 * of the Wayland connection and the X11 client is repeatedly mapping
1122 	 * and unmapping, we will never have shsurf set on MapRequest.
1123 	 */
1124 	assert(!window->shsurf);
1125 
1126 	window->map_request_x = window->x;
1127 	window->map_request_y = window->y;
1128 
1129 	if (window->frame_id == XCB_WINDOW_NONE)
1130 		weston_wm_window_create_frame(window); /* sets frame_id */
1131 	assert(window->frame_id != XCB_WINDOW_NONE);
1132 
1133 	wm_printf(wm, "XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1134 		  window->id, window, window->frame_id,
1135 		  window->width, window->height,
1136 		  window->map_request_x, window->map_request_y);
1137 
1138 	weston_wm_window_set_allow_commits(window, false);
1139 	weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1140 	weston_wm_window_set_net_wm_state(window);
1141 	weston_wm_window_set_virtual_desktop(window, 0);
1142 
1143 	if (legacy_fullscreen(wm, window, &output)) {
1144 		window->fullscreen = 1;
1145 		weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1146 					   output);
1147 	}
1148 
1149 	xcb_map_window(wm->conn, map_request->window);
1150 	xcb_map_window(wm->conn, window->frame_id);
1151 
1152 	/* Mapped in the X server, we can draw immediately.
1153 	 * Cannot set pending state though, no weston_surface until
1154 	 * xserver_map_shell_surface() time. */
1155 	weston_wm_window_schedule_repaint(window);
1156 }
1157 
1158 static void
weston_wm_handle_map_notify(struct weston_wm * wm,xcb_generic_event_t * event)1159 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1160 {
1161 	xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1162 
1163 	if (our_resource(wm, map_notify->window)) {
1164 		wm_printf(wm, "XCB_MAP_NOTIFY (window %d, ours)\n",
1165 			  map_notify->window);
1166 			return;
1167 	}
1168 
1169 	wm_printf(wm, "XCB_MAP_NOTIFY (window %d%s)\n", map_notify->window,
1170 		  map_notify->override_redirect ? ", override" : "");
1171 }
1172 
1173 static void
weston_wm_handle_unmap_notify(struct weston_wm * wm,xcb_generic_event_t * event)1174 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1175 {
1176 	xcb_unmap_notify_event_t *unmap_notify =
1177 		(xcb_unmap_notify_event_t *) event;
1178 	struct weston_wm_window *window;
1179 
1180 	wm_printf(wm, "XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1181 		  unmap_notify->window,
1182 		  unmap_notify->event,
1183 		  our_resource(wm, unmap_notify->window) ? ", ours" : "");
1184 
1185 	if (our_resource(wm, unmap_notify->window))
1186 		return;
1187 
1188 	if (unmap_notify->response_type & SEND_EVENT_MASK)
1189 		/* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1190 		 * as it may come in after we've destroyed the window. */
1191 		return;
1192 
1193 	if (!wm_lookup_window(wm, unmap_notify->window, &window))
1194 		return;
1195 
1196 	if (window->surface_id) {
1197 		/* Make sure we're not on the unpaired surface list or we
1198 		 * could be assigned a surface during surface creation that
1199 		 * was mapped before this unmap request.
1200 		 */
1201 		wl_list_remove(&window->link);
1202 		window->surface_id = 0;
1203 	}
1204 	if (wm->focus_window == window)
1205 		wm->focus_window = NULL;
1206 	if (window->surface)
1207 		wl_list_remove(&window->surface_destroy_listener.link);
1208 	window->surface = NULL;
1209 	window->shsurf = NULL;
1210 
1211 	weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1212 	weston_wm_window_set_virtual_desktop(window, -1);
1213 
1214 	xcb_unmap_window(wm->conn, window->frame_id);
1215 }
1216 
1217 static void
weston_wm_window_draw_decoration(struct weston_wm_window * window)1218 weston_wm_window_draw_decoration(struct weston_wm_window *window)
1219 {
1220 	cairo_t *cr;
1221 	int width, height;
1222 
1223 	wm_printf(window->wm, "XWM: draw decoration, win %d\n", window->id);
1224 
1225 	weston_wm_window_get_frame_size(window, &width, &height);
1226 
1227 	cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1228 	cr = cairo_create(window->cairo_surface);
1229 
1230 	if (window->fullscreen) {
1231 		/* nothing */
1232 	} else if (window->decorate) {
1233 		frame_set_title(window->frame, window->name);
1234 		frame_repaint(window->frame, cr);
1235 	} else {
1236 		cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1237 		cairo_set_source_rgba(cr, 0, 0, 0, 0);
1238 		cairo_paint(cr);
1239 
1240 		render_shadow(cr, window->wm->theme->shadow,
1241 			      2, 2, width + 8, height + 8, 64, 64);
1242 	}
1243 
1244 	cairo_destroy(cr);
1245 	cairo_surface_flush(window->cairo_surface);
1246 	xcb_flush(window->wm->conn);
1247 }
1248 
1249 static void
weston_wm_window_set_pending_state(struct weston_wm_window * window)1250 weston_wm_window_set_pending_state(struct weston_wm_window *window)
1251 {
1252 	int x, y, width, height;
1253 	int32_t input_x, input_y, input_w, input_h;
1254 	const struct weston_desktop_xwayland_interface *xwayland_interface =
1255 		window->wm->server->compositor->xwayland_interface;
1256 
1257 	if (!window->surface)
1258 		return;
1259 
1260 	weston_wm_window_get_frame_size(window, &width, &height);
1261 	weston_wm_window_get_child_position(window, &x, &y);
1262 
1263 	pixman_region32_fini(&window->surface->pending.opaque);
1264 	if (window->has_alpha) {
1265 		pixman_region32_init(&window->surface->pending.opaque);
1266 	} else {
1267 		/* We leave an extra pixel around the X window area to
1268 		 * make sure we don't sample from the undefined alpha
1269 		 * channel when filtering. */
1270 		pixman_region32_init_rect(&window->surface->pending.opaque,
1271 					  x - 1, y - 1,
1272 					  window->width + 2,
1273 					  window->height + 2);
1274 	}
1275 
1276 	if (window->decorate && !window->fullscreen) {
1277 		frame_input_rect(window->frame, &input_x, &input_y,
1278 				 &input_w, &input_h);
1279 	} else {
1280 		input_x = x;
1281 		input_y = y;
1282 		input_w = width;
1283 		input_h = height;
1284 	}
1285 
1286 	wm_printf(window->wm, "XWM: win %d geometry: %d,%d %dx%d\n",
1287 		  window->id, input_x, input_y, input_w, input_h);
1288 
1289 	pixman_region32_fini(&window->surface->pending.input);
1290 	pixman_region32_init_rect(&window->surface->pending.input,
1291 				  input_x, input_y, input_w, input_h);
1292 
1293 	xwayland_interface->set_window_geometry(window->shsurf,
1294 						input_x, input_y,
1295 						input_w, input_h);
1296 	if (window->name)
1297 		xwayland_interface->set_title(window->shsurf, window->name);
1298 	if (window->pid > 0)
1299 		xwayland_interface->set_pid(window->shsurf, window->pid);
1300 }
1301 
1302 static void
weston_wm_window_do_repaint(void * data)1303 weston_wm_window_do_repaint(void *data)
1304 {
1305 	struct weston_wm_window *window = data;
1306 
1307 	window->repaint_source = NULL;
1308 
1309 	weston_wm_window_read_properties(window);
1310 
1311 	weston_wm_window_draw_decoration(window);
1312 	weston_wm_window_set_pending_state(window);
1313 }
1314 
1315 static void
weston_wm_window_set_pending_state_OR(struct weston_wm_window * window)1316 weston_wm_window_set_pending_state_OR(struct weston_wm_window *window)
1317 {
1318 	int width, height;
1319 
1320 	/* for override-redirect windows */
1321 	assert(window->frame_id == XCB_WINDOW_NONE);
1322 
1323 	if (!window->surface)
1324 		return;
1325 
1326 	weston_wm_window_get_frame_size(window, &width, &height);
1327 	pixman_region32_fini(&window->surface->pending.opaque);
1328 	if (window->has_alpha) {
1329 		pixman_region32_init(&window->surface->pending.opaque);
1330 	} else {
1331 		pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1332 					  width, height);
1333 	}
1334 }
1335 
1336 static void
weston_wm_window_schedule_repaint(struct weston_wm_window * window)1337 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1338 {
1339 	struct weston_wm *wm = window->wm;
1340 
1341 	if (window->frame_id == XCB_WINDOW_NONE) {
1342 		/* Override-redirect windows go through here, but we
1343 		 * cannot assert(window->override_redirect); because
1344 		 * we do not deal with changing OR flag yet.
1345 		 * XXX: handle OR flag changes in message handlers
1346 		 */
1347 		weston_wm_window_set_pending_state_OR(window);
1348 		return;
1349 	}
1350 
1351 	if (window->repaint_source)
1352 		return;
1353 
1354 	wm_printf(wm, "XWM: schedule repaint, win %d\n", window->id);
1355 
1356 	window->repaint_source =
1357 		wl_event_loop_add_idle(wm->server->loop,
1358 				       weston_wm_window_do_repaint, window);
1359 }
1360 
1361 static void
weston_wm_handle_property_notify(struct weston_wm * wm,xcb_generic_event_t * event)1362 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1363 {
1364 	xcb_property_notify_event_t *property_notify =
1365 		(xcb_property_notify_event_t *) event;
1366 	struct weston_wm_window *window;
1367 	FILE *fp = NULL;
1368 	char *logstr;
1369 	size_t logsize;
1370 	char timestr[128];
1371 
1372 	if (!wm_lookup_window(wm, property_notify->window, &window))
1373 		return;
1374 
1375 	window->properties_dirty = 1;
1376 
1377 	if (wm_debug_is_enabled(wm))
1378 		fp = open_memstream(&logstr, &logsize);
1379 
1380 	if (fp) {
1381 		fprintf(fp, "%s XCB_PROPERTY_NOTIFY: window %d, ",
1382 			weston_log_scope_timestamp(wm->server->wm_debug,
1383 			timestr, sizeof timestr),
1384 			property_notify->window);
1385 		if (property_notify->state == XCB_PROPERTY_DELETE)
1386 			fprintf(fp, "deleted %s\n",
1387 					get_atom_name(wm->conn, property_notify->atom));
1388 		else
1389 			read_and_dump_property(fp, wm, property_notify->window,
1390 					       property_notify->atom);
1391 
1392 		if (fclose(fp) == 0)
1393 			weston_log_scope_write(wm->server->wm_debug,
1394 						 logstr, logsize);
1395 		free(logstr);
1396 	} else {
1397 		/* read_and_dump_property() is a X11 roundtrip.
1398 		 * Mimic it to maintain ordering semantics between debug
1399 		 * and non-debug paths.
1400 		 */
1401 		get_atom_name(wm->conn, property_notify->atom);
1402 	}
1403 
1404 	if (property_notify->atom == wm->atom.net_wm_name ||
1405 	    property_notify->atom == XCB_ATOM_WM_NAME)
1406 		weston_wm_window_schedule_repaint(window);
1407 }
1408 
1409 static void
weston_wm_window_create(struct weston_wm * wm,xcb_window_t id,int width,int height,int x,int y,int override)1410 weston_wm_window_create(struct weston_wm *wm,
1411 			xcb_window_t id, int width, int height, int x, int y, int override)
1412 {
1413 	struct weston_wm_window *window;
1414 	uint32_t values[1];
1415 	xcb_get_geometry_cookie_t geometry_cookie;
1416 	xcb_get_geometry_reply_t *geometry_reply;
1417 
1418 	window = zalloc(sizeof *window);
1419 	if (window == NULL) {
1420 		wm_printf(wm, "failed to allocate window\n");
1421 		return;
1422 	}
1423 
1424 	geometry_cookie = xcb_get_geometry(wm->conn, id);
1425 
1426 	values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1427                     XCB_EVENT_MASK_FOCUS_CHANGE;
1428 	xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1429 
1430 	window->wm = wm;
1431 	window->id = id;
1432 	window->properties_dirty = 1;
1433 	window->override_redirect = override;
1434 	window->width = width;
1435 	window->height = height;
1436 	window->x = x;
1437 	window->y = y;
1438 	window->pos_dirty = false;
1439 	window->map_request_x = INT_MIN; /* out of range for valid positions */
1440 	window->map_request_y = INT_MIN; /* out of range for valid positions */
1441 	weston_output_weak_ref_init(&window->legacy_fullscreen_output);
1442 
1443 	geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1444 	/* technically we should use XRender and check the visual format's
1445 	alpha_mask, but checking depth is simpler and works in all known cases */
1446 	if (geometry_reply != NULL)
1447 		window->has_alpha = geometry_reply->depth == 32;
1448 	free(geometry_reply);
1449 
1450 	hash_table_insert(wm->window_hash, id, window);
1451 }
1452 
1453 static void
weston_wm_window_destroy(struct weston_wm_window * window)1454 weston_wm_window_destroy(struct weston_wm_window *window)
1455 {
1456 	struct weston_wm *wm = window->wm;
1457 
1458 	weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1459 
1460 	if (window->repaint_source)
1461 		wl_event_source_remove(window->repaint_source);
1462 	if (window->cairo_surface)
1463 		cairo_surface_destroy(window->cairo_surface);
1464 
1465 	if (window->frame_id) {
1466 		xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1467 		xcb_destroy_window(wm->conn, window->frame_id);
1468 		weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1469 		weston_wm_window_set_virtual_desktop(window, -1);
1470 		hash_table_remove(wm->window_hash, window->frame_id);
1471 		window->frame_id = XCB_WINDOW_NONE;
1472 	}
1473 
1474 	if (window->frame)
1475 		frame_destroy(window->frame);
1476 
1477 	if (window->surface_id)
1478 		wl_list_remove(&window->link);
1479 
1480 	if (window->surface)
1481 		wl_list_remove(&window->surface_destroy_listener.link);
1482 
1483 	hash_table_remove(window->wm->window_hash, window->id);
1484 	free(window);
1485 }
1486 
1487 static void
weston_wm_handle_create_notify(struct weston_wm * wm,xcb_generic_event_t * event)1488 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1489 {
1490 	xcb_create_notify_event_t *create_notify =
1491 		(xcb_create_notify_event_t *) event;
1492 
1493 	wm_printf(wm, "XCB_CREATE_NOTIFY (window %d, at (%d, %d), width %d, height %d%s%s)\n",
1494 		  create_notify->window,
1495 		  create_notify->x, create_notify->y,
1496 		  create_notify->width, create_notify->height,
1497 		  create_notify->override_redirect ? ", override" : "",
1498 		  our_resource(wm, create_notify->window) ? ", ours" : "");
1499 
1500 	if (our_resource(wm, create_notify->window))
1501 		return;
1502 
1503 	weston_wm_window_create(wm, create_notify->window,
1504 				create_notify->width, create_notify->height,
1505 				create_notify->x, create_notify->y,
1506 				create_notify->override_redirect);
1507 }
1508 
1509 static void
weston_wm_handle_destroy_notify(struct weston_wm * wm,xcb_generic_event_t * event)1510 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1511 {
1512 	xcb_destroy_notify_event_t *destroy_notify =
1513 		(xcb_destroy_notify_event_t *) event;
1514 	struct weston_wm_window *window;
1515 
1516 	wm_printf(wm, "XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1517 		  destroy_notify->window,
1518 		  destroy_notify->event,
1519 		  our_resource(wm, destroy_notify->window) ? ", ours" : "");
1520 
1521 	if (our_resource(wm, destroy_notify->window))
1522 		return;
1523 
1524 	if (!wm_lookup_window(wm, destroy_notify->window, &window))
1525 		return;
1526 
1527 	weston_wm_window_destroy(window);
1528 }
1529 
1530 static void
weston_wm_handle_reparent_notify(struct weston_wm * wm,xcb_generic_event_t * event)1531 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1532 {
1533 	xcb_reparent_notify_event_t *reparent_notify =
1534 		(xcb_reparent_notify_event_t *) event;
1535 	struct weston_wm_window *window;
1536 
1537 	wm_printf(wm, "XCB_REPARENT_NOTIFY (window %d, parent %d, event %d%s)\n",
1538 		  reparent_notify->window,
1539 		  reparent_notify->parent,
1540 		  reparent_notify->event,
1541 		  reparent_notify->override_redirect ? ", override" : "");
1542 
1543 	if (reparent_notify->parent == wm->screen->root) {
1544 		weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1545 					reparent_notify->x, reparent_notify->y,
1546 					reparent_notify->override_redirect);
1547 	} else if (!our_resource(wm, reparent_notify->parent)) {
1548 		if (!wm_lookup_window(wm, reparent_notify->window, &window))
1549 			return;
1550 		weston_wm_window_destroy(window);
1551 	}
1552 }
1553 
1554 struct weston_seat *
weston_wm_pick_seat(struct weston_wm * wm)1555 weston_wm_pick_seat(struct weston_wm *wm)
1556 {
1557 	struct wl_list *seats = wm->server->compositor->seat_list.next;
1558 	if (wl_list_empty(seats))
1559 		return NULL;
1560 	return container_of(seats, struct weston_seat, link);
1561 }
1562 
1563 static struct weston_seat *
weston_wm_pick_seat_for_window(struct weston_wm_window * window)1564 weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1565 {
1566 	struct weston_wm *wm = window->wm;
1567 	struct weston_seat *seat, *s;
1568 
1569 	seat = NULL;
1570 	wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
1571 		struct weston_pointer *pointer = weston_seat_get_pointer(s);
1572 		struct weston_pointer *old_pointer =
1573 			weston_seat_get_pointer(seat);
1574 
1575 		if (pointer && pointer->focus &&
1576 		    pointer->focus->surface == window->surface &&
1577 		    pointer->button_count > 0 &&
1578 		    (!seat ||
1579 		     pointer->grab_serial -
1580 		     old_pointer->grab_serial < (1 << 30)))
1581 			seat = s;
1582 	}
1583 
1584 	return seat;
1585 }
1586 
1587 static void
weston_wm_window_handle_moveresize(struct weston_wm_window * window,xcb_client_message_event_t * client_message)1588 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1589 				   xcb_client_message_event_t *client_message)
1590 {
1591 	static const int map[] = {
1592 		THEME_LOCATION_RESIZING_TOP_LEFT,
1593 		THEME_LOCATION_RESIZING_TOP,
1594 		THEME_LOCATION_RESIZING_TOP_RIGHT,
1595 		THEME_LOCATION_RESIZING_RIGHT,
1596 		THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1597 		THEME_LOCATION_RESIZING_BOTTOM,
1598 		THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1599 		THEME_LOCATION_RESIZING_LEFT
1600 	};
1601 
1602 	struct weston_wm *wm = window->wm;
1603 	struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
1604 	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1605 	int detail;
1606 	const struct weston_desktop_xwayland_interface *xwayland_interface =
1607 		wm->server->compositor->xwayland_interface;
1608 
1609 	if (!pointer || pointer->button_count != 1
1610 	    || !pointer->focus
1611 	    || pointer->focus->surface != window->surface)
1612 		return;
1613 
1614 	detail = client_message->data.data32[2];
1615 	switch (detail) {
1616 	case _NET_WM_MOVERESIZE_MOVE:
1617 		xwayland_interface->move(window->shsurf, pointer);
1618 		break;
1619 	case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1620 	case _NET_WM_MOVERESIZE_SIZE_TOP:
1621 	case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1622 	case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1623 	case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1624 	case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1625 	case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1626 	case _NET_WM_MOVERESIZE_SIZE_LEFT:
1627 		xwayland_interface->resize(window->shsurf, pointer, map[detail]);
1628 		break;
1629 	case _NET_WM_MOVERESIZE_CANCEL:
1630 		break;
1631 	}
1632 }
1633 
1634 #define _NET_WM_STATE_REMOVE	0
1635 #define _NET_WM_STATE_ADD	1
1636 #define _NET_WM_STATE_TOGGLE	2
1637 
1638 static int
update_state(int action,int * state)1639 update_state(int action, int *state)
1640 {
1641 	int new_state, changed;
1642 
1643 	switch (action) {
1644 	case _NET_WM_STATE_REMOVE:
1645 		new_state = 0;
1646 		break;
1647 	case _NET_WM_STATE_ADD:
1648 		new_state = 1;
1649 		break;
1650 	case _NET_WM_STATE_TOGGLE:
1651 		new_state = !*state;
1652 		break;
1653 	default:
1654 		return 0;
1655 	}
1656 
1657 	changed = (*state != new_state);
1658 	*state = new_state;
1659 
1660 	return changed;
1661 }
1662 
1663 static void
1664 weston_wm_window_configure(void *data);
1665 
1666 static void
weston_wm_window_set_toplevel(struct weston_wm_window * window)1667 weston_wm_window_set_toplevel(struct weston_wm_window *window)
1668 {
1669 	const struct weston_desktop_xwayland_interface *xwayland_interface =
1670 		window->wm->server->compositor->xwayland_interface;
1671 
1672 	xwayland_interface->set_toplevel(window->shsurf);
1673 	window->width = window->saved_width;
1674 	window->height = window->saved_height;
1675 	if (window->frame)
1676 		frame_resize_inside(window->frame,
1677 					window->width,
1678 					window->height);
1679 	weston_wm_window_configure(window);
1680 }
1681 
1682 static inline bool
weston_wm_window_is_maximized(struct weston_wm_window * window)1683 weston_wm_window_is_maximized(struct weston_wm_window *window)
1684 {
1685 	return window->maximized_horz && window->maximized_vert;
1686 }
1687 
1688 static void
weston_wm_window_handle_state(struct weston_wm_window * window,xcb_client_message_event_t * client_message)1689 weston_wm_window_handle_state(struct weston_wm_window *window,
1690 			      xcb_client_message_event_t *client_message)
1691 {
1692 	struct weston_wm *wm = window->wm;
1693 	const struct weston_desktop_xwayland_interface *xwayland_interface =
1694 		wm->server->compositor->xwayland_interface;
1695 	uint32_t action, property1, property2;
1696 	int maximized = weston_wm_window_is_maximized(window);
1697 
1698 	action = client_message->data.data32[0];
1699 	property1 = client_message->data.data32[1];
1700 	property2 = client_message->data.data32[2];
1701 
1702 	if ((property1 == wm->atom.net_wm_state_fullscreen ||
1703 	     property2 == wm->atom.net_wm_state_fullscreen) &&
1704 	    update_state(action, &window->fullscreen)) {
1705 		weston_wm_window_set_net_wm_state(window);
1706 		if (window->fullscreen) {
1707 			window->saved_width = window->width;
1708 			window->saved_height = window->height;
1709 
1710 			if (window->shsurf)
1711 				xwayland_interface->set_fullscreen(window->shsurf,
1712 								   NULL);
1713 		} else {
1714 			if (window->shsurf)
1715 				weston_wm_window_set_toplevel(window);
1716 		}
1717 	} else {
1718 		if ((property1 == wm->atom.net_wm_state_maximized_vert ||
1719 		     property2 == wm->atom.net_wm_state_maximized_vert) &&
1720 		    update_state(action, &window->maximized_vert))
1721 			weston_wm_window_set_net_wm_state(window);
1722 		if ((property1 == wm->atom.net_wm_state_maximized_horz ||
1723 		     property2 == wm->atom.net_wm_state_maximized_horz) &&
1724 		    update_state(action, &window->maximized_horz))
1725 			weston_wm_window_set_net_wm_state(window);
1726 
1727 		if (maximized != weston_wm_window_is_maximized(window)) {
1728 			if (weston_wm_window_is_maximized(window)) {
1729 				window->saved_width = window->width;
1730 				window->saved_height = window->height;
1731 
1732 				if (window->shsurf)
1733 					xwayland_interface->set_maximized(window->shsurf);
1734 			} else if (window->shsurf) {
1735 				weston_wm_window_set_toplevel(window);
1736 			}
1737 		}
1738 	}
1739 }
1740 
1741 static void
surface_destroy(struct wl_listener * listener,void * data)1742 surface_destroy(struct wl_listener *listener, void *data)
1743 {
1744 	struct weston_wm_window *window =
1745 		container_of(listener,
1746 			     struct weston_wm_window, surface_destroy_listener);
1747 
1748 	wm_printf(window->wm, "surface for xid %d destroyed\n", window->id);
1749 
1750 	/* This should have been freed by the shell.
1751 	 * Don't try to use it later. */
1752 	window->shsurf = NULL;
1753 	window->surface = NULL;
1754 }
1755 
1756 static void
weston_wm_window_handle_surface_id(struct weston_wm_window * window,xcb_client_message_event_t * client_message)1757 weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1758 				   xcb_client_message_event_t *client_message)
1759 {
1760 	struct weston_wm *wm = window->wm;
1761 	struct wl_resource *resource;
1762 
1763 	if (window->surface_id != 0) {
1764 		wm_printf(wm, "already have surface id for window %d\n",
1765 			  window->id);
1766 		return;
1767 	}
1768 
1769 	/* Xwayland will send the wayland requests to create the
1770 	 * wl_surface before sending this client message.  Even so, we
1771 	 * can end up handling the X event before the wayland requests
1772 	 * and thus when we try to look up the surface ID, the surface
1773 	 * hasn't been created yet.  In that case put the window on
1774 	 * the unpaired window list and continue when the surface gets
1775 	 * created. */
1776 	uint32_t id = client_message->data.data32[0];
1777 	resource = wl_client_get_object(wm->server->client, id);
1778 	if (resource) {
1779 		window->surface_id = 0;
1780 		xserver_map_shell_surface(window,
1781 					  wl_resource_get_user_data(resource));
1782 	}
1783 	else {
1784 		window->surface_id = id;
1785 		wl_list_insert(&wm->unpaired_window_list, &window->link);
1786 	}
1787 }
1788 
1789 static void
weston_wm_handle_client_message(struct weston_wm * wm,xcb_generic_event_t * event)1790 weston_wm_handle_client_message(struct weston_wm *wm,
1791 				xcb_generic_event_t *event)
1792 {
1793 	xcb_client_message_event_t *client_message =
1794 		(xcb_client_message_event_t *) event;
1795 	struct weston_wm_window *window;
1796 
1797 	wm_printf(wm, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1798 		  get_atom_name(wm->conn, client_message->type),
1799 		  client_message->data.data32[0],
1800 		  client_message->data.data32[1],
1801 		  client_message->data.data32[2],
1802 		  client_message->data.data32[3],
1803 		  client_message->data.data32[4],
1804 		  client_message->window);
1805 
1806 	/* The window may get created and destroyed before we actually
1807 	 * handle the message.  If it doesn't exist, bail.
1808 	 */
1809 	if (!wm_lookup_window(wm, client_message->window, &window))
1810 		return;
1811 
1812 	if (client_message->type == wm->atom.net_wm_moveresize)
1813 		weston_wm_window_handle_moveresize(window, client_message);
1814 	else if (client_message->type == wm->atom.net_wm_state)
1815 		weston_wm_window_handle_state(window, client_message);
1816 	else if (client_message->type == wm->atom.wl_surface_id)
1817 		weston_wm_window_handle_surface_id(window, client_message);
1818 }
1819 
1820 enum cursor_type {
1821 	XWM_CURSOR_TOP,
1822 	XWM_CURSOR_BOTTOM,
1823 	XWM_CURSOR_LEFT,
1824 	XWM_CURSOR_RIGHT,
1825 	XWM_CURSOR_TOP_LEFT,
1826 	XWM_CURSOR_TOP_RIGHT,
1827 	XWM_CURSOR_BOTTOM_LEFT,
1828 	XWM_CURSOR_BOTTOM_RIGHT,
1829 	XWM_CURSOR_LEFT_PTR,
1830 };
1831 
1832 /*
1833  * The following correspondences between file names and cursors was copied
1834  * from: https://bugs.kde.org/attachment.cgi?id=67313
1835  */
1836 
1837 static const char *bottom_left_corners[] = {
1838 	"bottom_left_corner",
1839 	"sw-resize",
1840 	"size_bdiag"
1841 };
1842 
1843 static const char *bottom_right_corners[] = {
1844 	"bottom_right_corner",
1845 	"se-resize",
1846 	"size_fdiag"
1847 };
1848 
1849 static const char *bottom_sides[] = {
1850 	"bottom_side",
1851 	"s-resize",
1852 	"size_ver"
1853 };
1854 
1855 static const char *left_ptrs[] = {
1856 	"left_ptr",
1857 	"default",
1858 	"top_left_arrow",
1859 	"left-arrow"
1860 };
1861 
1862 static const char *left_sides[] = {
1863 	"left_side",
1864 	"w-resize",
1865 	"size_hor"
1866 };
1867 
1868 static const char *right_sides[] = {
1869 	"right_side",
1870 	"e-resize",
1871 	"size_hor"
1872 };
1873 
1874 static const char *top_left_corners[] = {
1875 	"top_left_corner",
1876 	"nw-resize",
1877 	"size_fdiag"
1878 };
1879 
1880 static const char *top_right_corners[] = {
1881 	"top_right_corner",
1882 	"ne-resize",
1883 	"size_bdiag"
1884 };
1885 
1886 static const char *top_sides[] = {
1887 	"top_side",
1888 	"n-resize",
1889 	"size_ver"
1890 };
1891 
1892 struct cursor_alternatives {
1893 	const char **names;
1894 	size_t count;
1895 };
1896 
1897 static const struct cursor_alternatives cursors[] = {
1898 	{top_sides, ARRAY_LENGTH(top_sides)},
1899 	{bottom_sides, ARRAY_LENGTH(bottom_sides)},
1900 	{left_sides, ARRAY_LENGTH(left_sides)},
1901 	{right_sides, ARRAY_LENGTH(right_sides)},
1902 	{top_left_corners, ARRAY_LENGTH(top_left_corners)},
1903 	{top_right_corners, ARRAY_LENGTH(top_right_corners)},
1904 	{bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1905 	{bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1906 	{left_ptrs, ARRAY_LENGTH(left_ptrs)},
1907 };
1908 
1909 static void
weston_wm_create_cursors(struct weston_wm * wm)1910 weston_wm_create_cursors(struct weston_wm *wm)
1911 {
1912 	const char *name;
1913 	int i, count = ARRAY_LENGTH(cursors);
1914 	size_t j;
1915 
1916 	wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1917 	for (i = 0; i < count; i++) {
1918 		for (j = 0; j < cursors[i].count; j++) {
1919 			name = cursors[i].names[j];
1920 			wm->cursors[i] =
1921 				xcb_cursor_library_load_cursor(wm, name);
1922 			if (wm->cursors[i] != (xcb_cursor_t)-1)
1923 				break;
1924 		}
1925 	}
1926 
1927 	wm->last_cursor = -1;
1928 }
1929 
1930 static void
weston_wm_destroy_cursors(struct weston_wm * wm)1931 weston_wm_destroy_cursors(struct weston_wm *wm)
1932 {
1933 	uint8_t i;
1934 
1935 	for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1936 		xcb_free_cursor(wm->conn, wm->cursors[i]);
1937 
1938 	free(wm->cursors);
1939 }
1940 
1941 static int
get_cursor_for_location(enum theme_location location)1942 get_cursor_for_location(enum theme_location location)
1943 {
1944 	switch (location) {
1945 		case THEME_LOCATION_RESIZING_TOP:
1946 			return XWM_CURSOR_TOP;
1947 		case THEME_LOCATION_RESIZING_BOTTOM:
1948 			return XWM_CURSOR_BOTTOM;
1949 		case THEME_LOCATION_RESIZING_LEFT:
1950 			return XWM_CURSOR_LEFT;
1951 		case THEME_LOCATION_RESIZING_RIGHT:
1952 			return XWM_CURSOR_RIGHT;
1953 		case THEME_LOCATION_RESIZING_TOP_LEFT:
1954 			return XWM_CURSOR_TOP_LEFT;
1955 		case THEME_LOCATION_RESIZING_TOP_RIGHT:
1956 			return XWM_CURSOR_TOP_RIGHT;
1957 		case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1958 			return XWM_CURSOR_BOTTOM_LEFT;
1959 		case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1960 			return XWM_CURSOR_BOTTOM_RIGHT;
1961 		case THEME_LOCATION_EXTERIOR:
1962 		case THEME_LOCATION_TITLEBAR:
1963 		default:
1964 			return XWM_CURSOR_LEFT_PTR;
1965 	}
1966 }
1967 
1968 static void
weston_wm_window_set_cursor(struct weston_wm * wm,xcb_window_t window_id,int cursor)1969 weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1970 			    int cursor)
1971 {
1972 	uint32_t cursor_value_list;
1973 
1974 	if (wm->last_cursor == cursor)
1975 		return;
1976 
1977 	wm->last_cursor = cursor;
1978 
1979 	cursor_value_list = wm->cursors[cursor];
1980 	xcb_change_window_attributes (wm->conn, window_id,
1981 				      XCB_CW_CURSOR, &cursor_value_list);
1982 	xcb_flush(wm->conn);
1983 }
1984 
1985 static void
weston_wm_window_close(struct weston_wm_window * window,xcb_timestamp_t time)1986 weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
1987 {
1988 	xcb_client_message_event_t client_message;
1989 
1990 	if (window->delete_window) {
1991 		client_message.response_type = XCB_CLIENT_MESSAGE;
1992 		client_message.format = 32;
1993 		client_message.window = window->id;
1994 		client_message.type = window->wm->atom.wm_protocols;
1995 		client_message.data.data32[0] =
1996 			window->wm->atom.wm_delete_window;
1997 		client_message.data.data32[1] = time;
1998 
1999 		xcb_send_event(window->wm->conn, 0, window->id,
2000 			       XCB_EVENT_MASK_NO_EVENT,
2001 			       (char *) &client_message);
2002 	} else {
2003 		xcb_kill_client(window->wm->conn, window->id);
2004 	}
2005 }
2006 
2007 #define DOUBLE_CLICK_PERIOD 250
2008 static void
weston_wm_handle_button(struct weston_wm * wm,xcb_generic_event_t * event)2009 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
2010 {
2011 	xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
2012 	const struct weston_desktop_xwayland_interface *xwayland_interface =
2013 		wm->server->compositor->xwayland_interface;
2014 	struct weston_seat *seat;
2015 	struct weston_pointer *pointer;
2016 	struct weston_wm_window *window;
2017 	enum theme_location location;
2018 	enum wl_pointer_button_state button_state;
2019 	uint32_t button_id;
2020 	uint32_t double_click = 0;
2021 
2022 	wm_printf(wm, "XCB_BUTTON_%s (detail %d)\n",
2023 		  button->response_type == XCB_BUTTON_PRESS ?
2024 		  "PRESS" : "RELEASE", button->detail);
2025 
2026 	if (!wm_lookup_window(wm, button->event, &window) ||
2027 	    !window->decorate)
2028 		return;
2029 
2030 	if (button->detail != 1 && button->detail != 2)
2031 		return;
2032 
2033 	seat = weston_wm_pick_seat_for_window(window);
2034 	pointer = weston_seat_get_pointer(seat);
2035 
2036 	button_state = button->response_type == XCB_BUTTON_PRESS ?
2037 		WL_POINTER_BUTTON_STATE_PRESSED :
2038 		WL_POINTER_BUTTON_STATE_RELEASED;
2039 	button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
2040 
2041 	if (button_state == WL_POINTER_BUTTON_STATE_PRESSED) {
2042 		if (button->time - window->last_button_time <= DOUBLE_CLICK_PERIOD) {
2043 			double_click = 1;
2044 			window->did_double = 1;
2045 		} else
2046 			window->did_double = 0;
2047 
2048 		window->last_button_time = button->time;
2049 	} else if (window->did_double == 1) {
2050 		double_click = 1;
2051 		window->did_double = 0;
2052 	}
2053 
2054 	/* Make sure we're looking at the right location.  The frame
2055 	 * could have received a motion event from a pointer from a
2056 	 * different wl_seat, but under X it looks like our core
2057 	 * pointer moved.  Move the frame pointer to the button press
2058 	 * location before deciding what to do. */
2059 	location = frame_pointer_motion(window->frame, NULL,
2060 					button->event_x, button->event_y);
2061 	if (double_click)
2062 		location = frame_double_click(window->frame, NULL,
2063 					      button_id, button_state);
2064 	else
2065 		location = frame_pointer_button(window->frame, NULL,
2066 						button_id, button_state);
2067 
2068 	if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2069 		weston_wm_window_schedule_repaint(window);
2070 
2071 	if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
2072 		if (pointer)
2073 			xwayland_interface->move(window->shsurf, pointer);
2074 		frame_status_clear(window->frame, FRAME_STATUS_MOVE);
2075 	}
2076 
2077 	if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
2078 		if (pointer)
2079 			xwayland_interface->resize(window->shsurf, pointer, location);
2080 		frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
2081 	}
2082 
2083 	if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
2084 		weston_wm_window_close(window, button->time);
2085 		frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
2086 	}
2087 
2088 	if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
2089 		window->maximized_horz = !window->maximized_horz;
2090 		window->maximized_vert = !window->maximized_vert;
2091 		if (weston_wm_window_is_maximized(window)) {
2092 			window->saved_width = window->width;
2093 			window->saved_height = window->height;
2094 			xwayland_interface->set_maximized(window->shsurf);
2095 		} else {
2096 			weston_wm_window_set_toplevel(window);
2097 		}
2098 		frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
2099 	}
2100 }
2101 
2102 static void
weston_wm_handle_motion(struct weston_wm * wm,xcb_generic_event_t * event)2103 weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
2104 {
2105 	xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
2106 	struct weston_wm_window *window;
2107 	enum theme_location location;
2108 	int cursor;
2109 
2110 	if (!wm_lookup_window(wm, motion->event, &window) ||
2111 	    !window->decorate)
2112 		return;
2113 
2114 	location = frame_pointer_motion(window->frame, NULL,
2115 					motion->event_x, motion->event_y);
2116 	if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2117 		weston_wm_window_schedule_repaint(window);
2118 
2119 	cursor = get_cursor_for_location(location);
2120 	weston_wm_window_set_cursor(wm, window->frame_id, cursor);
2121 }
2122 
2123 static void
weston_wm_handle_enter(struct weston_wm * wm,xcb_generic_event_t * event)2124 weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
2125 {
2126 	xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
2127 	struct weston_wm_window *window;
2128 	enum theme_location location;
2129 	int cursor;
2130 
2131 	if (!wm_lookup_window(wm, enter->event, &window) ||
2132 	    !window->decorate)
2133 		return;
2134 
2135 	location = frame_pointer_enter(window->frame, NULL,
2136 				       enter->event_x, enter->event_y);
2137 	if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2138 		weston_wm_window_schedule_repaint(window);
2139 
2140 	cursor = get_cursor_for_location(location);
2141 	weston_wm_window_set_cursor(wm, window->frame_id, cursor);
2142 }
2143 
2144 static void
weston_wm_handle_leave(struct weston_wm * wm,xcb_generic_event_t * event)2145 weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
2146 {
2147 	xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
2148 	struct weston_wm_window *window;
2149 
2150 	if (!wm_lookup_window(wm, leave->event, &window) ||
2151 	    !window->decorate)
2152 		return;
2153 
2154 	frame_pointer_leave(window->frame, NULL);
2155 	if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2156 		weston_wm_window_schedule_repaint(window);
2157 
2158 	weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
2159 }
2160 
2161 static void
weston_wm_handle_focus_in(struct weston_wm * wm,xcb_generic_event_t * event)2162 weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
2163 {
2164 	xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
2165 
2166 	/* Do not interfere with grabs */
2167 	if (focus->mode == XCB_NOTIFY_MODE_GRAB ||
2168 	    focus->mode == XCB_NOTIFY_MODE_UNGRAB)
2169 		return;
2170 
2171 	/* Do not let X clients change the focus behind the compositor's
2172 	 * back. Reset the focus to the old one if it changed. */
2173 	if (!wm->focus_window || focus->event != wm->focus_window->id)
2174 		weston_wm_send_focus_window(wm, wm->focus_window);
2175 }
2176 
2177 static int
weston_wm_handle_event(int fd,uint32_t mask,void * data)2178 weston_wm_handle_event(int fd, uint32_t mask, void *data)
2179 {
2180 	struct weston_wm *wm = data;
2181 	xcb_generic_event_t *event;
2182 	int count = 0;
2183 
2184 	while (event = xcb_poll_for_event(wm->conn), event != NULL) {
2185 		if (weston_wm_handle_selection_event(wm, event)) {
2186 			free(event);
2187 			count++;
2188 			continue;
2189 		}
2190 
2191 		if (weston_wm_handle_dnd_event(wm, event)) {
2192 			free(event);
2193 			count++;
2194 			continue;
2195 		}
2196 
2197 		switch (EVENT_TYPE(event)) {
2198 		case XCB_BUTTON_PRESS:
2199 		case XCB_BUTTON_RELEASE:
2200 			weston_wm_handle_button(wm, event);
2201 			break;
2202 		case XCB_ENTER_NOTIFY:
2203 			weston_wm_handle_enter(wm, event);
2204 			break;
2205 		case XCB_LEAVE_NOTIFY:
2206 			weston_wm_handle_leave(wm, event);
2207 			break;
2208 		case XCB_MOTION_NOTIFY:
2209 			weston_wm_handle_motion(wm, event);
2210 			break;
2211 		case XCB_CREATE_NOTIFY:
2212 			weston_wm_handle_create_notify(wm, event);
2213 			break;
2214 		case XCB_MAP_REQUEST:
2215 			weston_wm_handle_map_request(wm, event);
2216 			break;
2217 		case XCB_MAP_NOTIFY:
2218 			weston_wm_handle_map_notify(wm, event);
2219 			break;
2220 		case XCB_UNMAP_NOTIFY:
2221 			weston_wm_handle_unmap_notify(wm, event);
2222 			break;
2223 		case XCB_REPARENT_NOTIFY:
2224 			weston_wm_handle_reparent_notify(wm, event);
2225 			break;
2226 		case XCB_CONFIGURE_REQUEST:
2227 			weston_wm_handle_configure_request(wm, event);
2228 			break;
2229 		case XCB_CONFIGURE_NOTIFY:
2230 			weston_wm_handle_configure_notify(wm, event);
2231 			break;
2232 		case XCB_DESTROY_NOTIFY:
2233 			weston_wm_handle_destroy_notify(wm, event);
2234 			break;
2235 		case XCB_MAPPING_NOTIFY:
2236 			wm_printf(wm, "XCB_MAPPING_NOTIFY\n");
2237 			break;
2238 		case XCB_PROPERTY_NOTIFY:
2239 			weston_wm_handle_property_notify(wm, event);
2240 			break;
2241 		case XCB_CLIENT_MESSAGE:
2242 			weston_wm_handle_client_message(wm, event);
2243 			break;
2244 		case XCB_FOCUS_IN:
2245 			weston_wm_handle_focus_in(wm, event);
2246 			break;
2247 		}
2248 
2249 		free(event);
2250 		count++;
2251 	}
2252 
2253 	if (count != 0)
2254 		xcb_flush(wm->conn);
2255 
2256 	return count;
2257 }
2258 
2259 static void
weston_wm_set_net_active_window(struct weston_wm * wm,xcb_window_t window)2260 weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2261 	xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2262 			wm->screen->root, wm->atom.net_active_window,
2263 			wm->atom.window, 32, 1, &window);
2264 }
2265 
2266 static void
weston_wm_get_visual_and_colormap(struct weston_wm * wm)2267 weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2268 {
2269 	xcb_depth_iterator_t d_iter;
2270 	xcb_visualtype_iterator_t vt_iter;
2271 	xcb_visualtype_t *visualtype;
2272 
2273 	d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2274 	visualtype = NULL;
2275 	while (d_iter.rem > 0) {
2276 		if (d_iter.data->depth == 32) {
2277 			vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2278 			visualtype = vt_iter.data;
2279 			break;
2280 		}
2281 
2282 		xcb_depth_next(&d_iter);
2283 	}
2284 
2285 	if (visualtype == NULL) {
2286 		weston_log("no 32 bit visualtype\n");
2287 		return;
2288 	}
2289 
2290 	wm->visual_id = visualtype->visual_id;
2291 	wm->colormap = xcb_generate_id(wm->conn);
2292 	xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2293 			    wm->colormap, wm->screen->root, wm->visual_id);
2294 }
2295 
2296 static void
weston_wm_get_resources(struct weston_wm * wm)2297 weston_wm_get_resources(struct weston_wm *wm)
2298 {
2299 
2300 #define F(field) offsetof(struct weston_wm, field)
2301 
2302 	static const struct { const char *name; int offset; } atoms[] = {
2303 		{ "WM_PROTOCOLS",	F(atom.wm_protocols) },
2304 		{ "WM_NORMAL_HINTS",	F(atom.wm_normal_hints) },
2305 		{ "WM_TAKE_FOCUS",	F(atom.wm_take_focus) },
2306 		{ "WM_DELETE_WINDOW",	F(atom.wm_delete_window) },
2307 		{ "WM_STATE",		F(atom.wm_state) },
2308 		{ "WM_S0",		F(atom.wm_s0) },
2309 		{ "WM_CLIENT_MACHINE",	F(atom.wm_client_machine) },
2310 		{ "_NET_WM_CM_S0",	F(atom.net_wm_cm_s0) },
2311 		{ "_NET_WM_NAME",	F(atom.net_wm_name) },
2312 		{ "_NET_WM_PID",	F(atom.net_wm_pid) },
2313 		{ "_NET_WM_ICON",	F(atom.net_wm_icon) },
2314 		{ "_NET_WM_STATE",	F(atom.net_wm_state) },
2315 		{ "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2316 		{ "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
2317 		{ "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2318 		{ "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2319 		{ "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
2320 		{ "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
2321 		{ "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2322 
2323 		{ "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2324 		{ "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2325 		{ "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2326 		{ "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2327 		{ "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2328 		{ "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2329 		{ "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
2330 		{ "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2331 		{ "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
2332 		{ "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2333 		{ "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2334 		{ "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2335 		{ "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2336 		{ "_NET_WM_WINDOW_TYPE_NORMAL",	F(atom.net_wm_window_type_normal) },
2337 
2338 		{ "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2339 		{ "_NET_SUPPORTING_WM_CHECK",
2340 					F(atom.net_supporting_wm_check) },
2341 		{ "_NET_SUPPORTED",     F(atom.net_supported) },
2342 		{ "_NET_ACTIVE_WINDOW",     F(atom.net_active_window) },
2343 		{ "_MOTIF_WM_HINTS",	F(atom.motif_wm_hints) },
2344 		{ "CLIPBOARD",		F(atom.clipboard) },
2345 		{ "CLIPBOARD_MANAGER",	F(atom.clipboard_manager) },
2346 		{ "TARGETS",		F(atom.targets) },
2347 		{ "UTF8_STRING",	F(atom.utf8_string) },
2348 		{ "_WL_SELECTION",	F(atom.wl_selection) },
2349 		{ "INCR",		F(atom.incr) },
2350 		{ "TIMESTAMP",		F(atom.timestamp) },
2351 		{ "MULTIPLE",		F(atom.multiple) },
2352 		{ "UTF8_STRING"	,	F(atom.utf8_string) },
2353 		{ "COMPOUND_TEXT",	F(atom.compound_text) },
2354 		{ "TEXT",		F(atom.text) },
2355 		{ "STRING",		F(atom.string) },
2356 		{ "WINDOW",		F(atom.window) },
2357 		{ "text/plain;charset=utf-8",	F(atom.text_plain_utf8) },
2358 		{ "text/plain",		F(atom.text_plain) },
2359 		{ "XdndSelection",	F(atom.xdnd_selection) },
2360 		{ "XdndAware",		F(atom.xdnd_aware) },
2361 		{ "XdndEnter",		F(atom.xdnd_enter) },
2362 		{ "XdndLeave",		F(atom.xdnd_leave) },
2363 		{ "XdndDrop",		F(atom.xdnd_drop) },
2364 		{ "XdndStatus",		F(atom.xdnd_status) },
2365 		{ "XdndFinished",	F(atom.xdnd_finished) },
2366 		{ "XdndTypeList",	F(atom.xdnd_type_list) },
2367 		{ "XdndActionCopy",	F(atom.xdnd_action_copy) },
2368 		{ "_XWAYLAND_ALLOW_COMMITS",	F(atom.allow_commits) },
2369 		{ "WL_SURFACE_ID",	F(atom.wl_surface_id) }
2370 	};
2371 #undef F
2372 
2373 	xcb_xfixes_query_version_cookie_t xfixes_cookie;
2374 	xcb_xfixes_query_version_reply_t *xfixes_reply;
2375 	xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2376 	xcb_intern_atom_reply_t *reply;
2377 	xcb_render_query_pict_formats_reply_t *formats_reply;
2378 	xcb_render_query_pict_formats_cookie_t formats_cookie;
2379 	xcb_render_pictforminfo_t *formats;
2380 	uint32_t i;
2381 
2382 	xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
2383 	xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
2384 
2385 	formats_cookie = xcb_render_query_pict_formats(wm->conn);
2386 
2387 	for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2388 		cookies[i] = xcb_intern_atom (wm->conn, 0,
2389 					      strlen(atoms[i].name),
2390 					      atoms[i].name);
2391 
2392 	for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2393 		reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2394 		*(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2395 		free(reply);
2396 	}
2397 
2398 	wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2399 	if (!wm->xfixes || !wm->xfixes->present)
2400 		weston_log("xfixes not available\n");
2401 
2402 	xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2403 						 XCB_XFIXES_MAJOR_VERSION,
2404 						 XCB_XFIXES_MINOR_VERSION);
2405 	xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2406 						      xfixes_cookie, NULL);
2407 
2408 	weston_log("xfixes version: %d.%d\n",
2409 	       xfixes_reply->major_version, xfixes_reply->minor_version);
2410 
2411 	free(xfixes_reply);
2412 
2413 	formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2414 							    formats_cookie, 0);
2415 	if (formats_reply == NULL)
2416 		return;
2417 
2418 	formats = xcb_render_query_pict_formats_formats(formats_reply);
2419 	for (i = 0; i < formats_reply->num_formats; i++) {
2420 		if (formats[i].direct.red_mask != 0xff &&
2421 		    formats[i].direct.red_shift != 16)
2422 			continue;
2423 		if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2424 		    formats[i].depth == 24)
2425 			wm->format_rgb = formats[i];
2426 		if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2427 		    formats[i].depth == 32 &&
2428 		    formats[i].direct.alpha_mask == 0xff &&
2429 		    formats[i].direct.alpha_shift == 24)
2430 			wm->format_rgba = formats[i];
2431 	}
2432 
2433 	free(formats_reply);
2434 }
2435 
2436 static void
weston_wm_create_wm_window(struct weston_wm * wm)2437 weston_wm_create_wm_window(struct weston_wm *wm)
2438 {
2439 	static const char name[] = "Weston WM";
2440 
2441 	wm->wm_window = xcb_generate_id(wm->conn);
2442 	xcb_create_window(wm->conn,
2443 			  XCB_COPY_FROM_PARENT,
2444 			  wm->wm_window,
2445 			  wm->screen->root,
2446 			  0, 0,
2447 			  10, 10,
2448 			  0,
2449 			  XCB_WINDOW_CLASS_INPUT_OUTPUT,
2450 			  wm->screen->root_visual,
2451 			  0, NULL);
2452 
2453 	xcb_change_property(wm->conn,
2454 			    XCB_PROP_MODE_REPLACE,
2455 			    wm->wm_window,
2456 			    wm->atom.net_supporting_wm_check,
2457 			    XCB_ATOM_WINDOW,
2458 			    32, /* format */
2459 			    1, &wm->wm_window);
2460 
2461 	xcb_change_property(wm->conn,
2462 			    XCB_PROP_MODE_REPLACE,
2463 			    wm->wm_window,
2464 			    wm->atom.net_wm_name,
2465 			    wm->atom.utf8_string,
2466 			    8, /* format */
2467 			    strlen(name), name);
2468 
2469 	xcb_change_property(wm->conn,
2470 			    XCB_PROP_MODE_REPLACE,
2471 			    wm->screen->root,
2472 			    wm->atom.net_supporting_wm_check,
2473 			    XCB_ATOM_WINDOW,
2474 			    32, /* format */
2475 			    1, &wm->wm_window);
2476 
2477 	/* Claim the WM_S0 selection even though we don't support
2478 	 * the --replace functionality. */
2479 	xcb_set_selection_owner(wm->conn,
2480 				wm->wm_window,
2481 				wm->atom.wm_s0,
2482 				XCB_TIME_CURRENT_TIME);
2483 
2484 	xcb_set_selection_owner(wm->conn,
2485 				wm->wm_window,
2486 				wm->atom.net_wm_cm_s0,
2487 				XCB_TIME_CURRENT_TIME);
2488 }
2489 
2490 struct weston_wm *
weston_wm_create(struct weston_xserver * wxs,int fd)2491 weston_wm_create(struct weston_xserver *wxs, int fd)
2492 {
2493 	struct weston_wm *wm;
2494 	struct wl_event_loop *loop;
2495 	xcb_screen_iterator_t s;
2496 	uint32_t values[1];
2497 	xcb_atom_t supported[6];
2498 
2499 	wm = zalloc(sizeof *wm);
2500 	if (wm == NULL)
2501 		return NULL;
2502 
2503 	wm->server = wxs;
2504 	wm->window_hash = hash_table_create();
2505 	if (wm->window_hash == NULL) {
2506 		free(wm);
2507 		return NULL;
2508 	}
2509 
2510 	/* xcb_connect_to_fd takes ownership of the fd. */
2511 	wm->conn = xcb_connect_to_fd(fd, NULL);
2512 	if (xcb_connection_has_error(wm->conn)) {
2513 		weston_log("xcb_connect_to_fd failed\n");
2514 		close(fd);
2515 		hash_table_destroy(wm->window_hash);
2516 		free(wm);
2517 		return NULL;
2518 	}
2519 
2520 	s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2521 	wm->screen = s.data;
2522 
2523 	loop = wl_display_get_event_loop(wxs->wl_display);
2524 	wm->source =
2525 		wl_event_loop_add_fd(loop, fd,
2526 				     WL_EVENT_READABLE,
2527 				     weston_wm_handle_event, wm);
2528 	wl_event_source_check(wm->source);
2529 
2530 	weston_wm_get_resources(wm);
2531 	weston_wm_get_visual_and_colormap(wm);
2532 
2533 	values[0] =
2534 		XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2535 		XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2536 		XCB_EVENT_MASK_PROPERTY_CHANGE;
2537 	xcb_change_window_attributes(wm->conn, wm->screen->root,
2538 				     XCB_CW_EVENT_MASK, values);
2539 
2540 	xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2541 					  XCB_COMPOSITE_REDIRECT_MANUAL);
2542 
2543 	wm->theme = theme_create();
2544 
2545 	supported[0] = wm->atom.net_wm_moveresize;
2546 	supported[1] = wm->atom.net_wm_state;
2547 	supported[2] = wm->atom.net_wm_state_fullscreen;
2548 	supported[3] = wm->atom.net_wm_state_maximized_vert;
2549 	supported[4] = wm->atom.net_wm_state_maximized_horz;
2550 	supported[5] = wm->atom.net_active_window;
2551 	xcb_change_property(wm->conn,
2552 			    XCB_PROP_MODE_REPLACE,
2553 			    wm->screen->root,
2554 			    wm->atom.net_supported,
2555 			    XCB_ATOM_ATOM,
2556 			    32, /* format */
2557 			    ARRAY_LENGTH(supported), supported);
2558 
2559 	weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2560 
2561 	weston_wm_selection_init(wm);
2562 
2563 	weston_wm_dnd_init(wm);
2564 
2565 	xcb_flush(wm->conn);
2566 
2567 	wm->create_surface_listener.notify = weston_wm_create_surface;
2568 	wl_signal_add(&wxs->compositor->create_surface_signal,
2569 		      &wm->create_surface_listener);
2570 	wm->activate_listener.notify = weston_wm_window_activate;
2571 	wl_signal_add(&wxs->compositor->activate_signal,
2572 		      &wm->activate_listener);
2573 	wm->kill_listener.notify = weston_wm_kill_client;
2574 	wl_signal_add(&wxs->compositor->kill_signal,
2575 		      &wm->kill_listener);
2576 	wl_list_init(&wm->unpaired_window_list);
2577 
2578 	weston_wm_create_cursors(wm);
2579 	weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
2580 
2581 	/* Create wm window and take WM_S0 selection last, which
2582 	 * signals to Xwayland that we're done with setup. */
2583 	weston_wm_create_wm_window(wm);
2584 
2585 	weston_log("created wm, root %d\n", wm->screen->root);
2586 
2587 	return wm;
2588 }
2589 
2590 void
weston_wm_destroy(struct weston_wm * wm)2591 weston_wm_destroy(struct weston_wm *wm)
2592 {
2593 	/* FIXME: Free windows in hash. */
2594 	hash_table_destroy(wm->window_hash);
2595 	weston_wm_destroy_cursors(wm);
2596 	xcb_disconnect(wm->conn);
2597 	wl_event_source_remove(wm->source);
2598 	wl_list_remove(&wm->selection_listener.link);
2599 	wl_list_remove(&wm->activate_listener.link);
2600 	wl_list_remove(&wm->kill_listener.link);
2601 	wl_list_remove(&wm->create_surface_listener.link);
2602 
2603 	free(wm);
2604 }
2605 
2606 static struct weston_wm_window *
get_wm_window(struct weston_surface * surface)2607 get_wm_window(struct weston_surface *surface)
2608 {
2609 	struct wl_listener *listener;
2610 
2611 	listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
2612 	if (listener)
2613 		return container_of(listener, struct weston_wm_window,
2614 				    surface_destroy_listener);
2615 
2616 	return NULL;
2617 }
2618 
2619 static bool
is_wm_window(struct weston_surface * surface)2620 is_wm_window(struct weston_surface *surface)
2621 {
2622 	return get_wm_window(surface) != NULL;
2623 }
2624 
2625 static void
weston_wm_window_configure(void * data)2626 weston_wm_window_configure(void *data)
2627 {
2628 	struct weston_wm_window *window = data;
2629 	struct weston_wm *wm = window->wm;
2630 	uint32_t values[4];
2631 	int x, y, width, height;
2632 
2633 	weston_wm_window_get_child_position(window, &x, &y);
2634 	values[0] = x;
2635 	values[1] = y;
2636 	values[2] = window->width;
2637 	values[3] = window->height;
2638 	xcb_configure_window(wm->conn,
2639 			     window->id,
2640 			     XCB_CONFIG_WINDOW_X |
2641 			     XCB_CONFIG_WINDOW_Y |
2642 			     XCB_CONFIG_WINDOW_WIDTH |
2643 			     XCB_CONFIG_WINDOW_HEIGHT,
2644 			     values);
2645 
2646 	weston_wm_window_get_frame_size(window, &width, &height);
2647 	values[0] = width;
2648 	values[1] = height;
2649 	xcb_configure_window(wm->conn,
2650 			     window->frame_id,
2651 			     XCB_CONFIG_WINDOW_WIDTH |
2652 			     XCB_CONFIG_WINDOW_HEIGHT,
2653 			     values);
2654 
2655 	window->configure_source = NULL;
2656 
2657 	weston_wm_window_schedule_repaint(window);
2658 }
2659 
2660 static void
send_configure(struct weston_surface * surface,int32_t width,int32_t height)2661 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
2662 {
2663 	struct weston_wm_window *window = get_wm_window(surface);
2664 	struct weston_wm *wm;
2665 	struct theme *t;
2666 	int new_width, new_height;
2667 	int vborder, hborder;
2668 
2669 	if (!window || !window->wm)
2670 		return;
2671 	wm = window->wm;
2672 	t = wm->theme;
2673 
2674 	if (window->decorate && !window->fullscreen) {
2675 		hborder = 2 * t->width;
2676 		vborder = t->titlebar_height + t->width;
2677 	} else {
2678 		hborder = 0;
2679 		vborder = 0;
2680 	}
2681 
2682 	if (width > hborder)
2683 		new_width = width - hborder;
2684 	else
2685 		new_width = 1;
2686 
2687 	if (height > vborder)
2688 		new_height = height - vborder;
2689 	else
2690 		new_height = 1;
2691 
2692 	if (window->width == new_width && window->height == new_height)
2693 		return;
2694 
2695 	window->width = new_width;
2696 	window->height = new_height;
2697 
2698 	if (window->frame)
2699 		frame_resize_inside(window->frame, window->width, window->height);
2700 
2701 	if (window->configure_source)
2702 		return;
2703 
2704 	window->configure_source =
2705 		wl_event_loop_add_idle(wm->server->loop,
2706 				       weston_wm_window_configure, window);
2707 }
2708 
2709 static void
send_position(struct weston_surface * surface,int32_t x,int32_t y)2710 send_position(struct weston_surface *surface, int32_t x, int32_t y)
2711 {
2712 	struct weston_wm_window *window = get_wm_window(surface);
2713 	struct weston_wm *wm;
2714 	uint32_t mask, values[2];
2715 
2716 	if (!window || !window->wm)
2717 		return;
2718 
2719 	wm = window->wm;
2720 	/* We use pos_dirty to tell whether a configure message is in flight.
2721 	 * This is needed in case we send two configure events in a very
2722 	 * short time, since window->x/y is set in after a roundtrip, hence
2723 	 * we cannot just check if the current x and y are different. */
2724 	if (window->x != x || window->y != y || window->pos_dirty) {
2725 		window->pos_dirty = true;
2726 		values[0] = x;
2727 		values[1] = y;
2728 		mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2729 
2730 		xcb_configure_window(wm->conn, window->frame_id, mask, values);
2731 		xcb_flush(wm->conn);
2732 	}
2733 }
2734 
2735 static const struct weston_xwayland_client_interface shell_client = {
2736 	send_configure,
2737 };
2738 
2739 static int
legacy_fullscreen(struct weston_wm * wm,struct weston_wm_window * window,struct weston_output ** output_ret)2740 legacy_fullscreen(struct weston_wm *wm,
2741 		  struct weston_wm_window *window,
2742 		  struct weston_output **output_ret)
2743 {
2744 	struct weston_compositor *compositor = wm->server->compositor;
2745 	struct weston_output *output;
2746 	uint32_t minmax = PMinSize | PMaxSize;
2747 	int matching_size;
2748 
2749 	/* Heuristics for detecting legacy fullscreen windows... */
2750 
2751 	wl_list_for_each(output, &compositor->output_list, link) {
2752 		if (output->x == window->x &&
2753 		    output->y == window->y &&
2754 		    output->width == window->width &&
2755 		    output->height == window->height &&
2756 		    window->override_redirect) {
2757 			*output_ret = output;
2758 			return 1;
2759 		}
2760 
2761 		matching_size = 0;
2762 		if ((window->size_hints.flags & (USSize |PSize)) &&
2763 		    window->size_hints.width == output->width &&
2764 		    window->size_hints.height == output->height)
2765 			matching_size = 1;
2766 		if ((window->size_hints.flags & minmax) == minmax &&
2767 		    window->size_hints.min_width == output->width &&
2768 		    window->size_hints.min_height == output->height &&
2769 		    window->size_hints.max_width == output->width &&
2770 		    window->size_hints.max_height == output->height)
2771 			matching_size = 1;
2772 
2773 		if (matching_size && !window->decorate &&
2774 		    (window->size_hints.flags & (USPosition | PPosition)) &&
2775 		    window->size_hints.x == output->x &&
2776 		    window->size_hints.y == output->y) {
2777 			*output_ret = output;
2778 			return 1;
2779 		}
2780 	}
2781 
2782 	return 0;
2783 }
2784 
2785 static bool
weston_wm_window_is_positioned(struct weston_wm_window * window)2786 weston_wm_window_is_positioned(struct weston_wm_window *window)
2787 {
2788 	if (window->map_request_x == INT_MIN ||
2789 	    window->map_request_y == INT_MIN)
2790 		weston_log("XWM warning: win %d did not see map request\n",
2791 			   window->id);
2792 
2793 	return window->map_request_x != 0 || window->map_request_y != 0;
2794 }
2795 
2796 static bool
weston_wm_window_type_inactive(struct weston_wm_window * window)2797 weston_wm_window_type_inactive(struct weston_wm_window *window)
2798 {
2799 	struct weston_wm *wm = window->wm;
2800 
2801 	return window->type == wm->atom.net_wm_window_type_tooltip ||
2802 	       window->type == wm->atom.net_wm_window_type_dropdown ||
2803 	       window->type == wm->atom.net_wm_window_type_dnd ||
2804 	       window->type == wm->atom.net_wm_window_type_combo ||
2805 	       window->type == wm->atom.net_wm_window_type_popup ||
2806 	       window->type == wm->atom.net_wm_window_type_utility;
2807 }
2808 
2809 static void
xserver_map_shell_surface(struct weston_wm_window * window,struct weston_surface * surface)2810 xserver_map_shell_surface(struct weston_wm_window *window,
2811 			  struct weston_surface *surface)
2812 {
2813 	struct weston_wm *wm = window->wm;
2814 	struct weston_desktop_xwayland *xwayland =
2815 		wm->server->compositor->xwayland;
2816 	const struct weston_desktop_xwayland_interface *xwayland_interface =
2817 		wm->server->compositor->xwayland_interface;
2818 	struct weston_wm_window *parent;
2819 
2820 	/* This should be necessary only for override-redirected windows,
2821 	 * because otherwise MapRequest handler would have already updated
2822 	 * the properties. However, if X11 clients set properties after
2823 	 * sending MapWindow, here we can still process them. The decorations
2824 	 * have already been drawn once with the old property values, so if the
2825 	 * app changes something affecting decor after MapWindow, we glitch.
2826 	 * We only hit xserver_map_shell_surface() once per MapWindow and
2827 	 * wl_surface, so better ensure we get the window type right.
2828 	 */
2829 	weston_wm_window_read_properties(window);
2830 
2831 	/* A weston_wm_window may have many different surfaces assigned
2832 	 * throughout its life, so we must make sure to remove the listener
2833 	 * from the old surface signal list. */
2834 	if (window->surface)
2835 		wl_list_remove(&window->surface_destroy_listener.link);
2836 
2837 	window->surface = surface;
2838 	window->surface_destroy_listener.notify = surface_destroy;
2839 	wl_signal_add(&window->surface->destroy_signal,
2840 		      &window->surface_destroy_listener);
2841 
2842 	if (!xwayland_interface)
2843 		return;
2844 
2845 	if (window->surface->committed) {
2846 		weston_log("warning, unexpected in %s: "
2847 			   "surface's configure hook is already set.\n",
2848 			   __func__);
2849 		return;
2850 	}
2851 
2852 	window->shsurf =
2853 		xwayland_interface->create_surface(xwayland,
2854 						   window->surface,
2855 						   &shell_client);
2856 
2857 	wm_printf(wm, "XWM: map shell surface, win %d, weston_surface %p, xwayland surface %p\n",
2858 		  window->id, window->surface, window->shsurf);
2859 
2860 	if (window->name)
2861 		xwayland_interface->set_title(window->shsurf, window->name);
2862 	if (window->pid > 0)
2863 		xwayland_interface->set_pid(window->shsurf, window->pid);
2864 
2865 	if (window->fullscreen) {
2866 		window->saved_width = window->width;
2867 		window->saved_height = window->height;
2868 		xwayland_interface->set_fullscreen(window->shsurf,
2869 						   window->legacy_fullscreen_output.output);
2870 		return;
2871 	} else if (window->override_redirect) {
2872 		xwayland_interface->set_xwayland(window->shsurf,
2873 						 window->x, window->y);
2874 	} else if (window->transient_for && window->transient_for->surface) {
2875 		parent = window->transient_for;
2876 		if (weston_wm_window_type_inactive(window)) {
2877 			xwayland_interface->set_transient(window->shsurf,
2878 							  parent->surface,
2879 							  window->x - parent->x,
2880 							  window->y - parent->y);
2881 		} else {
2882 			xwayland_interface->set_toplevel(window->shsurf);
2883 			xwayland_interface->set_parent(window->shsurf,
2884 						       parent->surface);
2885 		}
2886 	} else if (weston_wm_window_is_maximized(window)) {
2887 		xwayland_interface->set_maximized(window->shsurf);
2888 	} else {
2889 		if (weston_wm_window_type_inactive(window)) {
2890 			xwayland_interface->set_xwayland(window->shsurf,
2891 							 window->x,
2892 							 window->y);
2893 		} else if (weston_wm_window_is_positioned(window)) {
2894 			xwayland_interface->set_toplevel_with_position(window->shsurf,
2895 								       window->map_request_x,
2896 								       window->map_request_y);
2897 		} else {
2898 			xwayland_interface->set_toplevel(window->shsurf);
2899 		}
2900 	}
2901 
2902 	if (window->frame_id == XCB_WINDOW_NONE) {
2903 		weston_wm_window_set_pending_state_OR(window);
2904 	} else {
2905 		weston_wm_window_set_pending_state(window);
2906 		weston_wm_window_set_allow_commits(window, true);
2907 		xcb_flush(wm->conn);
2908 	}
2909 }
2910 
2911 const struct weston_xwayland_surface_api surface_api = {
2912 	is_wm_window,
2913 	send_position,
2914 };
2915