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