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