Don't include xserver-server-protocol.h
[profile/ivi/weston-ivi-shell.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                         break;
685                 }       
686 }
687
688 static void
689 weston_wm_window_activate(struct wl_listener *listener, void *data)
690 {
691         struct weston_surface *surface = data;
692         struct weston_wm_window *window = NULL;
693         struct weston_wm *wm =
694                 container_of(listener, struct weston_wm, activate_listener);
695         xcb_client_message_event_t client_message;
696
697         if (surface) {
698                 window = get_wm_window(surface);
699         }
700
701         if (window) {
702                 client_message.response_type = XCB_CLIENT_MESSAGE;
703                 client_message.format = 32;
704                 client_message.window = window->id;
705                 client_message.type = wm->atom.wm_protocols;
706                 client_message.data.data32[0] = wm->atom.wm_take_focus;
707                 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
708
709                 xcb_send_event(wm->conn, 0, window->id, 
710                                XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
711                                (char *) &client_message);
712
713                 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
714                                      window->id, XCB_TIME_CURRENT_TIME);
715         } else {
716                 xcb_set_input_focus (wm->conn,
717                                      XCB_INPUT_FOCUS_POINTER_ROOT,
718                                      XCB_NONE,
719                                      XCB_TIME_CURRENT_TIME);
720         }
721
722         if (wm->focus_window) {
723                 if (wm->focus_window->frame)
724                         frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
725                 weston_wm_window_schedule_repaint(wm->focus_window);
726         }
727         wm->focus_window = window;
728         if (wm->focus_window) {
729                 if (wm->focus_window->frame)
730                         frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
731                 weston_wm_window_schedule_repaint(wm->focus_window);
732         }
733 }
734
735 static void
736 weston_wm_window_transform(struct wl_listener *listener, void *data)
737 {
738         struct weston_surface *surface = data;
739         struct weston_wm_window *window = get_wm_window(surface);
740         struct weston_wm *wm =
741                 container_of(listener, struct weston_wm, transform_listener);
742         uint32_t mask, values[2];
743
744         if (!window || !wm)
745                 return;
746
747         if (!window->view || !weston_view_is_mapped(window->view))
748                 return;
749
750         if (window->x != window->view->geometry.x ||
751             window->y != window->view->geometry.y) {
752                 values[0] = window->view->geometry.x;
753                 values[1] = window->view->geometry.y;
754                 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
755
756                 xcb_configure_window(wm->conn, window->frame_id, mask, values);
757                 xcb_flush(wm->conn);
758         }
759 }
760
761 #define ICCCM_WITHDRAWN_STATE   0
762 #define ICCCM_NORMAL_STATE      1
763 #define ICCCM_ICONIC_STATE      3
764
765 static void
766 weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
767 {
768         struct weston_wm *wm = window->wm;
769         uint32_t property[2];
770
771         property[0] = state;
772         property[1] = XCB_WINDOW_NONE;
773
774         xcb_change_property(wm->conn,
775                             XCB_PROP_MODE_REPLACE,
776                             window->id,
777                             wm->atom.wm_state,
778                             wm->atom.wm_state,
779                             32, /* format */
780                             2, property);
781 }
782
783 static void
784 weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
785 {
786         struct weston_wm *wm = window->wm;
787         uint32_t property[1];
788         int i;
789
790         i = 0;
791         if (window->fullscreen)
792                 property[i++] = wm->atom.net_wm_state_fullscreen;
793
794         xcb_change_property(wm->conn,
795                             XCB_PROP_MODE_REPLACE,
796                             window->id,
797                             wm->atom.net_wm_state,
798                             XCB_ATOM_ATOM,
799                             32, /* format */
800                             i, property);
801 }
802
803 static void
804 weston_wm_window_create_frame(struct weston_wm_window *window)
805 {
806         struct weston_wm *wm = window->wm;
807         uint32_t values[3];
808         int x, y, width, height;
809
810         window->frame = frame_create(window->wm->theme,
811                                      window->width, window->height,
812                                      FRAME_BUTTON_CLOSE, window->name);
813         frame_resize_inside(window->frame, window->width, window->height);
814
815         weston_wm_window_get_frame_size(window, &width, &height);
816         weston_wm_window_get_child_position(window, &x, &y);
817
818         values[0] = wm->screen->black_pixel;
819         values[1] =
820                 XCB_EVENT_MASK_KEY_PRESS |
821                 XCB_EVENT_MASK_KEY_RELEASE |
822                 XCB_EVENT_MASK_BUTTON_PRESS |
823                 XCB_EVENT_MASK_BUTTON_RELEASE |
824                 XCB_EVENT_MASK_POINTER_MOTION |
825                 XCB_EVENT_MASK_ENTER_WINDOW |
826                 XCB_EVENT_MASK_LEAVE_WINDOW |
827                 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
828                 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
829         values[2] = wm->colormap;
830
831         window->frame_id = xcb_generate_id(wm->conn);
832         xcb_create_window(wm->conn,
833                           32,
834                           window->frame_id,
835                           wm->screen->root,
836                           0, 0,
837                           width, height,
838                           0,
839                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
840                           wm->visual_id,
841                           XCB_CW_BORDER_PIXEL |
842                           XCB_CW_EVENT_MASK |
843                           XCB_CW_COLORMAP, values);
844
845         xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
846
847         values[0] = 0;
848         xcb_configure_window(wm->conn, window->id,
849                              XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
850
851         window->cairo_surface =
852                 cairo_xcb_surface_create_with_xrender_format(wm->conn,
853                                                              wm->screen,
854                                                              window->frame_id,
855                                                              &wm->format_rgba,
856                                                              width, height);
857
858         hash_table_insert(wm->window_hash, window->frame_id, window);
859 }
860
861 static void
862 weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
863 {
864         xcb_map_request_event_t *map_request =
865                 (xcb_map_request_event_t *) event;
866         struct weston_wm_window *window;
867
868         if (our_resource(wm, map_request->window)) {
869                 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
870                        map_request->window);
871                 return;
872         }
873
874         window = hash_table_lookup(wm->window_hash, map_request->window);
875
876         weston_wm_window_read_properties(window);
877
878         if (window->frame_id == XCB_WINDOW_NONE)
879                 weston_wm_window_create_frame(window);
880
881         wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
882                window->id, window, window->frame_id);
883
884         weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
885         weston_wm_window_set_net_wm_state(window);
886
887         xcb_map_window(wm->conn, map_request->window);
888         xcb_map_window(wm->conn, window->frame_id);
889 }
890
891 static void
892 weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
893 {
894         xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
895
896         if (our_resource(wm, map_notify->window)) {
897                 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
898                        map_notify->window);
899                         return;
900         }
901
902         wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
903 }
904
905 static void
906 weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
907 {
908         xcb_unmap_notify_event_t *unmap_notify =
909                 (xcb_unmap_notify_event_t *) event;
910         struct weston_wm_window *window;
911
912         wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
913                unmap_notify->window,
914                unmap_notify->event,
915                our_resource(wm, unmap_notify->window) ? ", ours" : "");
916
917         if (our_resource(wm, unmap_notify->window))
918                 return;
919
920         if (unmap_notify->response_type & SEND_EVENT_MASK)
921                 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
922                  * as it may come in after we've destroyed the window. */
923                 return;
924
925         window = hash_table_lookup(wm->window_hash, unmap_notify->window);
926         if (wm->focus_window == window)
927                 wm->focus_window = NULL;
928         if (window->surface)
929                 wl_list_remove(&window->surface_destroy_listener.link);
930         window->surface = NULL;
931         window->shsurf = NULL;
932         window->view = NULL;
933         xcb_unmap_window(wm->conn, window->frame_id);
934 }
935
936 static void
937 weston_wm_window_draw_decoration(void *data)
938 {
939         struct weston_wm_window *window = data;
940         struct weston_wm *wm = window->wm;
941         struct theme *t = wm->theme;
942         cairo_t *cr;
943         int x, y, width, height;
944         int32_t input_x, input_y, input_w, input_h;
945
946         uint32_t flags = 0;
947
948         weston_wm_window_read_properties(window);
949
950         window->repaint_source = NULL;
951
952         weston_wm_window_get_frame_size(window, &width, &height);
953         weston_wm_window_get_child_position(window, &x, &y);
954
955         cairo_xcb_surface_set_size(window->cairo_surface, width, height);
956         cr = cairo_create(window->cairo_surface);
957
958         if (window->fullscreen) {
959                 /* nothing */
960         } else if (window->decorate) {
961                 if (wm->focus_window == window)
962                         flags |= THEME_FRAME_ACTIVE;
963
964                 frame_repaint(window->frame, cr);
965         } else {
966                 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
967                 cairo_set_source_rgba(cr, 0, 0, 0, 0);
968                 cairo_paint(cr);
969
970                 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
971                 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
972                 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
973         }
974
975         cairo_destroy(cr);
976
977         if (window->surface) {
978                 pixman_region32_fini(&window->surface->pending.opaque);
979                 if(window->has_alpha) {
980                         pixman_region32_init(&window->surface->pending.opaque);
981                 } else {
982                         /* We leave an extra pixel around the X window area to
983                          * make sure we don't sample from the undefined alpha
984                          * channel when filtering. */
985                         pixman_region32_init_rect(&window->surface->pending.opaque, 
986                                                   x - 1, y - 1,
987                                                   window->width + 2,
988                                                   window->height + 2);
989                 }
990                 if (window->view)
991                         weston_view_geometry_dirty(window->view);
992
993                 pixman_region32_fini(&window->surface->pending.input);
994
995                 if (window->fullscreen) {
996                         input_x = 0;
997                         input_y = 0;
998                         input_w = window->width;
999                         input_h = window->height;
1000                 } else if (window->decorate) {
1001                         frame_input_rect(window->frame, &input_x, &input_y,
1002                                          &input_w, &input_h);
1003                 }
1004
1005                 pixman_region32_init_rect(&window->surface->pending.input,
1006                                           input_x, input_y, input_w, input_h);
1007         }
1008 }
1009
1010 static void
1011 weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1012 {
1013         struct weston_wm *wm = window->wm;
1014         int width, height;
1015
1016         if (window->frame_id == XCB_WINDOW_NONE) {
1017                 if (window->surface != NULL) {
1018                         weston_wm_window_get_frame_size(window, &width, &height);
1019                         pixman_region32_fini(&window->surface->pending.opaque);
1020                         if(window->has_alpha) {
1021                                 pixman_region32_init(&window->surface->pending.opaque);
1022                         } else {
1023                                 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1024                                                           width, height);
1025                         }
1026                         if (window->view)
1027                                 weston_view_geometry_dirty(window->view);
1028                 }
1029                 return;
1030         }
1031
1032         if (window->repaint_source)
1033                 return;
1034
1035         window->repaint_source =
1036                 wl_event_loop_add_idle(wm->server->loop,
1037                                        weston_wm_window_draw_decoration,
1038                                        window);
1039 }
1040
1041 static void
1042 weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1043 {
1044         xcb_property_notify_event_t *property_notify =
1045                 (xcb_property_notify_event_t *) event;
1046         struct weston_wm_window *window;
1047
1048         window = hash_table_lookup(wm->window_hash, property_notify->window);
1049         if (!window)
1050                 return;
1051
1052         window->properties_dirty = 1;
1053
1054         wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
1055         if (property_notify->state == XCB_PROPERTY_DELETE)
1056                 wm_log("deleted\n");
1057         else
1058                 read_and_dump_property(wm, property_notify->window,
1059                                        property_notify->atom);
1060
1061         if (property_notify->atom == wm->atom.net_wm_name ||
1062             property_notify->atom == XCB_ATOM_WM_NAME)
1063                 weston_wm_window_schedule_repaint(window);
1064 }
1065
1066 static void
1067 weston_wm_window_create(struct weston_wm *wm,
1068                         xcb_window_t id, int width, int height, int x, int y, int override)
1069 {
1070         struct weston_wm_window *window;
1071         uint32_t values[1];
1072         xcb_get_geometry_cookie_t geometry_cookie;
1073         xcb_get_geometry_reply_t *geometry_reply;
1074
1075         window = zalloc(sizeof *window);
1076         if (window == NULL) {
1077                 wm_log("failed to allocate window\n");
1078                 return;
1079         }
1080
1081         geometry_cookie = xcb_get_geometry(wm->conn, id);
1082
1083         values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1084         xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1085
1086         window->wm = wm;
1087         window->id = id;
1088         window->properties_dirty = 1;
1089         window->override_redirect = override;
1090         window->width = width;
1091         window->height = height;
1092         window->x = x;
1093         window->y = y;
1094
1095         geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1096         /* technically we should use XRender and check the visual format's
1097         alpha_mask, but checking depth is simpler and works in all known cases */
1098         if(geometry_reply != NULL)
1099                 window->has_alpha = geometry_reply->depth == 32;
1100         free(geometry_reply);
1101
1102         hash_table_insert(wm->window_hash, id, window);
1103 }
1104
1105 static void
1106 weston_wm_window_destroy(struct weston_wm_window *window)
1107 {
1108         struct weston_wm *wm = window->wm;
1109
1110         if (window->repaint_source)
1111                 wl_event_source_remove(window->repaint_source);
1112         if (window->cairo_surface)
1113                 cairo_surface_destroy(window->cairo_surface);
1114
1115         if (window->frame_id) {
1116                 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1117                 xcb_destroy_window(wm->conn, window->frame_id);
1118                 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1119                 hash_table_remove(wm->window_hash, window->frame_id);
1120                 window->frame_id = XCB_WINDOW_NONE;
1121         }
1122
1123         hash_table_remove(window->wm->window_hash, window->id);
1124         free(window);
1125 }
1126
1127 static void
1128 weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1129 {
1130         xcb_create_notify_event_t *create_notify =
1131                 (xcb_create_notify_event_t *) event;
1132
1133         wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1134                create_notify->window,
1135                create_notify->width, create_notify->height,
1136                create_notify->override_redirect ? ", override" : "",
1137                our_resource(wm, create_notify->window) ? ", ours" : "");
1138
1139         if (our_resource(wm, create_notify->window))
1140                 return;
1141
1142         weston_wm_window_create(wm, create_notify->window,
1143                                 create_notify->width, create_notify->height,
1144                                 create_notify->x, create_notify->y,
1145                                 create_notify->override_redirect);
1146 }
1147
1148 static void
1149 weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1150 {
1151         xcb_destroy_notify_event_t *destroy_notify =
1152                 (xcb_destroy_notify_event_t *) event;
1153         struct weston_wm_window *window;
1154
1155         wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1156                destroy_notify->window,
1157                destroy_notify->event,
1158                our_resource(wm, destroy_notify->window) ? ", ours" : "");
1159
1160         if (our_resource(wm, destroy_notify->window))
1161                 return;
1162
1163         window = hash_table_lookup(wm->window_hash, destroy_notify->window);
1164         weston_wm_window_destroy(window);
1165 }
1166
1167 static void
1168 weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1169 {
1170         xcb_reparent_notify_event_t *reparent_notify =
1171                 (xcb_reparent_notify_event_t *) event;
1172         struct weston_wm_window *window;
1173
1174         wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1175                reparent_notify->window,
1176                reparent_notify->parent,
1177                reparent_notify->event);
1178
1179         if (reparent_notify->parent == wm->screen->root) {
1180                 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1181                                         reparent_notify->x, reparent_notify->y,
1182                                         reparent_notify->override_redirect);
1183         } else if (!our_resource(wm, reparent_notify->parent)) {
1184                 window = hash_table_lookup(wm->window_hash,
1185                                            reparent_notify->window);
1186                 weston_wm_window_destroy(window);
1187         }
1188 }
1189
1190 struct weston_seat *
1191 weston_wm_pick_seat(struct weston_wm *wm)
1192 {
1193         return container_of(wm->server->compositor->seat_list.next,
1194                             struct weston_seat, link);
1195 }
1196
1197 static void
1198 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1199                                    xcb_client_message_event_t *client_message)
1200 {
1201         static const int map[] = {
1202                 THEME_LOCATION_RESIZING_TOP_LEFT,
1203                 THEME_LOCATION_RESIZING_TOP,
1204                 THEME_LOCATION_RESIZING_TOP_RIGHT,
1205                 THEME_LOCATION_RESIZING_RIGHT,
1206                 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1207                 THEME_LOCATION_RESIZING_BOTTOM,
1208                 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1209                 THEME_LOCATION_RESIZING_LEFT
1210         };
1211
1212         struct weston_wm *wm = window->wm;
1213         struct weston_seat *seat = weston_wm_pick_seat(wm);
1214         int detail;
1215         struct weston_shell_interface *shell_interface =
1216                 &wm->server->compositor->shell_interface;
1217
1218         if (seat->pointer->button_count != 1 || !window->view
1219             || seat->pointer->focus != window->view)
1220                 return;
1221
1222         detail = client_message->data.data32[2];
1223         switch (detail) {
1224         case _NET_WM_MOVERESIZE_MOVE:
1225                 shell_interface->move(window->shsurf, seat);
1226                 break;
1227         case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1228         case _NET_WM_MOVERESIZE_SIZE_TOP:
1229         case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1230         case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1231         case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1232         case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1233         case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1234         case _NET_WM_MOVERESIZE_SIZE_LEFT:
1235                 shell_interface->resize(window->shsurf, seat, map[detail]);
1236                 break;
1237         case _NET_WM_MOVERESIZE_CANCEL:
1238                 break;
1239         }
1240 }
1241
1242 #define _NET_WM_STATE_REMOVE    0
1243 #define _NET_WM_STATE_ADD       1
1244 #define _NET_WM_STATE_TOGGLE    2
1245
1246 static int
1247 update_state(int action, int *state)
1248 {
1249         int new_state, changed;
1250
1251         switch (action) {
1252         case _NET_WM_STATE_REMOVE:
1253                 new_state = 0;
1254                 break;
1255         case _NET_WM_STATE_ADD:
1256                 new_state = 1;
1257                 break;
1258         case _NET_WM_STATE_TOGGLE:
1259                 new_state = !*state;
1260                 break;
1261         default:
1262                 return 0;
1263         }
1264
1265         changed = (*state != new_state);
1266         *state = new_state;
1267
1268         return changed;
1269 }
1270
1271 static void
1272 weston_wm_window_configure(void *data);
1273
1274 static void
1275 weston_wm_window_handle_state(struct weston_wm_window *window,
1276                               xcb_client_message_event_t *client_message)
1277 {
1278         struct weston_wm *wm = window->wm;
1279         struct weston_shell_interface *shell_interface =
1280                 &wm->server->compositor->shell_interface;
1281         uint32_t action, property;
1282
1283         action = client_message->data.data32[0];
1284         property = client_message->data.data32[1];
1285
1286         if (property == wm->atom.net_wm_state_fullscreen &&
1287             update_state(action, &window->fullscreen)) {
1288                 weston_wm_window_set_net_wm_state(window);
1289                 if (window->fullscreen) {
1290                         window->saved_width = window->width;
1291                         window->saved_height = window->height;
1292
1293                         if (window->shsurf)
1294                                 shell_interface->set_fullscreen(window->shsurf,
1295                                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1296                                                                 0, NULL);
1297                 } else {
1298                         shell_interface->set_toplevel(window->shsurf);
1299                         window->width = window->saved_width;
1300                         window->height = window->saved_height;
1301                         if (window->frame)
1302                                 frame_resize_inside(window->frame,
1303                                                     window->width,
1304                                                     window->height);
1305                         weston_wm_window_configure(window);
1306                 }
1307         }
1308 }
1309
1310 static void
1311 surface_destroy(struct wl_listener *listener, void *data)
1312 {
1313         struct weston_wm_window *window =
1314                 container_of(listener,
1315                              struct weston_wm_window, surface_destroy_listener);
1316
1317         wm_log("surface for xid %d destroyed\n", window->id);
1318
1319         /* This should have been freed by the shell.
1320          * Don't try to use it later. */
1321         window->shsurf = NULL;
1322         window->surface = NULL;
1323         window->view = NULL;
1324 }
1325
1326 static void
1327 weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1328                                    xcb_client_message_event_t *client_message)
1329 {
1330         struct weston_wm *wm = window->wm;
1331         struct wl_resource *resource;
1332
1333         if (window->surface_id != 0) {
1334                 wm_log("already have surface id for window %d\n", window->id);
1335                 return;
1336         }
1337
1338         /* Xwayland will send the wayland requests to create the
1339          * wl_surface before sending this client message.  Even so, we
1340          * can end up handling the X event before the wayland requests
1341          * and thus when we try to look up the surface ID, the surface
1342          * hasn't been created yet.  In that case put the window on
1343          * the unpaired window list and continue when the surface gets
1344          * created. */
1345         window->surface_id = client_message->data.data32[0];
1346         resource = wl_client_get_object(wm->server->client,
1347                                         window->surface_id);
1348         if (resource)
1349                 xserver_map_shell_surface(window,
1350                                           wl_resource_get_user_data(resource));
1351         else
1352                 wl_list_insert(&wm->unpaired_window_list, &window->link);
1353 }
1354
1355 static void
1356 weston_wm_handle_client_message(struct weston_wm *wm,
1357                                 xcb_generic_event_t *event)
1358 {
1359         xcb_client_message_event_t *client_message =
1360                 (xcb_client_message_event_t *) event;
1361         struct weston_wm_window *window;
1362
1363         window = hash_table_lookup(wm->window_hash, client_message->window);
1364
1365         wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1366                get_atom_name(wm->conn, client_message->type),
1367                client_message->data.data32[0],
1368                client_message->data.data32[1],
1369                client_message->data.data32[2],
1370                client_message->data.data32[3],
1371                client_message->data.data32[4],
1372                client_message->window);
1373
1374         if (client_message->type == wm->atom.net_wm_moveresize)
1375                 weston_wm_window_handle_moveresize(window, client_message);
1376         else if (client_message->type == wm->atom.net_wm_state)
1377                 weston_wm_window_handle_state(window, client_message);
1378         else if (client_message->type == wm->atom.wl_surface_id)
1379                 weston_wm_window_handle_surface_id(window, client_message);
1380 }
1381
1382 enum cursor_type {
1383         XWM_CURSOR_TOP,
1384         XWM_CURSOR_BOTTOM,
1385         XWM_CURSOR_LEFT,
1386         XWM_CURSOR_RIGHT,
1387         XWM_CURSOR_TOP_LEFT,
1388         XWM_CURSOR_TOP_RIGHT,
1389         XWM_CURSOR_BOTTOM_LEFT,
1390         XWM_CURSOR_BOTTOM_RIGHT,
1391         XWM_CURSOR_LEFT_PTR,
1392 };
1393
1394 /*
1395  * The following correspondences between file names and cursors was copied
1396  * from: https://bugs.kde.org/attachment.cgi?id=67313
1397  */
1398
1399 static const char *bottom_left_corners[] = {
1400         "bottom_left_corner",
1401         "sw-resize",
1402         "size_bdiag"
1403 };
1404
1405 static const char *bottom_right_corners[] = {
1406         "bottom_right_corner",
1407         "se-resize",
1408         "size_fdiag"
1409 };
1410
1411 static const char *bottom_sides[] = {
1412         "bottom_side",
1413         "s-resize",
1414         "size_ver"
1415 };
1416
1417 static const char *left_ptrs[] = {
1418         "left_ptr",
1419         "default",
1420         "top_left_arrow",
1421         "left-arrow"
1422 };
1423
1424 static const char *left_sides[] = {
1425         "left_side",
1426         "w-resize",
1427         "size_hor"
1428 };
1429
1430 static const char *right_sides[] = {
1431         "right_side",
1432         "e-resize",
1433         "size_hor"
1434 };
1435
1436 static const char *top_left_corners[] = {
1437         "top_left_corner",
1438         "nw-resize",
1439         "size_fdiag"
1440 };
1441
1442 static const char *top_right_corners[] = {
1443         "top_right_corner",
1444         "ne-resize",
1445         "size_bdiag"
1446 };
1447
1448 static const char *top_sides[] = {
1449         "top_side",
1450         "n-resize",
1451         "size_ver"
1452 };
1453
1454 struct cursor_alternatives {
1455         const char **names;
1456         size_t count;
1457 };
1458
1459 static const struct cursor_alternatives cursors[] = {
1460         {top_sides, ARRAY_LENGTH(top_sides)},
1461         {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1462         {left_sides, ARRAY_LENGTH(left_sides)},
1463         {right_sides, ARRAY_LENGTH(right_sides)},
1464         {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1465         {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1466         {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1467         {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1468         {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1469 };
1470
1471 static void
1472 weston_wm_create_cursors(struct weston_wm *wm)
1473 {
1474         const char *name;
1475         int i, count = ARRAY_LENGTH(cursors);
1476         size_t j;
1477
1478         wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1479         for (i = 0; i < count; i++) {
1480                 for (j = 0; j < cursors[i].count; j++) {
1481                         name = cursors[i].names[j];
1482                         wm->cursors[i] =
1483                                 xcb_cursor_library_load_cursor(wm, name);
1484                         if (wm->cursors[i] != (xcb_cursor_t)-1)
1485                                 break;
1486                 }
1487         }
1488
1489         wm->last_cursor = -1;
1490 }
1491
1492 static void
1493 weston_wm_destroy_cursors(struct weston_wm *wm)
1494 {
1495         uint8_t i;
1496
1497         for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1498                 xcb_free_cursor(wm->conn, wm->cursors[i]);
1499
1500         free(wm->cursors);
1501 }
1502
1503 static int
1504 get_cursor_for_location(enum theme_location location)
1505 {
1506         // int location = theme_get_location(t, x, y, width, height, 0);
1507
1508         switch (location) {
1509                 case THEME_LOCATION_RESIZING_TOP:
1510                         return XWM_CURSOR_TOP;
1511                 case THEME_LOCATION_RESIZING_BOTTOM:
1512                         return XWM_CURSOR_BOTTOM;
1513                 case THEME_LOCATION_RESIZING_LEFT:
1514                         return XWM_CURSOR_LEFT;
1515                 case THEME_LOCATION_RESIZING_RIGHT:
1516                         return XWM_CURSOR_RIGHT;
1517                 case THEME_LOCATION_RESIZING_TOP_LEFT:
1518                         return XWM_CURSOR_TOP_LEFT;
1519                 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1520                         return XWM_CURSOR_TOP_RIGHT;
1521                 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1522                         return XWM_CURSOR_BOTTOM_LEFT;
1523                 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1524                         return XWM_CURSOR_BOTTOM_RIGHT;
1525                 case THEME_LOCATION_EXTERIOR:
1526                 case THEME_LOCATION_TITLEBAR:
1527                 default:
1528                         return XWM_CURSOR_LEFT_PTR;
1529         }
1530 }
1531
1532 static void
1533 weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1534                             int cursor)
1535 {
1536         uint32_t cursor_value_list;
1537
1538         if (wm->last_cursor == cursor)
1539                 return;
1540
1541         wm->last_cursor = cursor;
1542
1543         cursor_value_list = wm->cursors[cursor];
1544         xcb_change_window_attributes (wm->conn, window_id,
1545                                       XCB_CW_CURSOR, &cursor_value_list);
1546         xcb_flush(wm->conn);
1547 }
1548
1549 static void
1550 weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
1551 {
1552         xcb_client_message_event_t client_message;
1553
1554         if (window->delete_window) {
1555                 client_message.response_type = XCB_CLIENT_MESSAGE;
1556                 client_message.format = 32;
1557                 client_message.window = window->id;
1558                 client_message.type = window->wm->atom.wm_protocols;
1559                 client_message.data.data32[0] =
1560                         window->wm->atom.wm_delete_window;
1561                 client_message.data.data32[1] = time;
1562
1563                 xcb_send_event(window->wm->conn, 0, window->id,
1564                                XCB_EVENT_MASK_NO_EVENT,
1565                                (char *) &client_message);
1566         } else {
1567                 xcb_kill_client(window->wm->conn, window->id);
1568         }
1569 }
1570
1571 static void
1572 weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1573 {
1574         xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1575         struct weston_shell_interface *shell_interface =
1576                 &wm->server->compositor->shell_interface;
1577         struct weston_seat *seat = weston_wm_pick_seat(wm);
1578         struct weston_wm_window *window;
1579         enum theme_location location;
1580         enum frame_button_state button_state;
1581         uint32_t button_id;
1582
1583         wm_log("XCB_BUTTON_%s (detail %d)\n",
1584                button->response_type == XCB_BUTTON_PRESS ?
1585                "PRESS" : "RELEASE", button->detail);
1586
1587         window = hash_table_lookup(wm->window_hash, button->event);
1588         if (!window || !window->decorate)
1589                 return;
1590
1591         if (button->detail != 1 && button->detail != 2)
1592                 return;
1593
1594         button_state = button->response_type == XCB_BUTTON_PRESS ?
1595                 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1596         button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1597
1598         location = frame_pointer_button(window->frame, NULL,
1599                                         button_id, button_state);
1600         if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1601                 weston_wm_window_schedule_repaint(window);
1602
1603         if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1604                 shell_interface->move(window->shsurf, seat);
1605                 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1606         }
1607
1608         if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1609                 shell_interface->resize(window->shsurf, seat, location);
1610                 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1611         }
1612
1613         if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
1614                 weston_wm_window_close(window, button->time);
1615                 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
1616         }
1617 }
1618
1619 static void
1620 weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1621 {
1622         xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1623         struct weston_wm_window *window;
1624         enum theme_location location;
1625         int cursor;
1626
1627         window = hash_table_lookup(wm->window_hash, motion->event);
1628         if (!window || !window->decorate)
1629                 return;
1630
1631         location = frame_pointer_motion(window->frame, NULL,
1632                                         motion->event_x, motion->event_y);
1633         if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1634                 weston_wm_window_schedule_repaint(window);
1635
1636         cursor = get_cursor_for_location(location);
1637         weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1638 }
1639
1640 static void
1641 weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1642 {
1643         xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1644         struct weston_wm_window *window;
1645         enum theme_location location;
1646         int cursor;
1647
1648         window = hash_table_lookup(wm->window_hash, enter->event);
1649         if (!window || !window->decorate)
1650                 return;
1651
1652         location = frame_pointer_enter(window->frame, NULL,
1653                                        enter->event_x, enter->event_y);
1654         if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1655                 weston_wm_window_schedule_repaint(window);
1656
1657         cursor = get_cursor_for_location(location);
1658         weston_wm_window_set_cursor(wm, window->frame_id, cursor);
1659 }
1660
1661 static void
1662 weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1663 {
1664         xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1665         struct weston_wm_window *window;
1666
1667         window = hash_table_lookup(wm->window_hash, leave->event);
1668         if (!window || !window->decorate)
1669                 return;
1670
1671         frame_pointer_leave(window->frame, NULL);
1672         if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1673                 weston_wm_window_schedule_repaint(window);
1674
1675         weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
1676 }
1677
1678 static int
1679 weston_wm_handle_event(int fd, uint32_t mask, void *data)
1680 {
1681         struct weston_wm *wm = data;
1682         xcb_generic_event_t *event;
1683         int count = 0;
1684
1685         while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1686                 if (weston_wm_handle_selection_event(wm, event)) {
1687                         free(event);
1688                         count++;
1689                         continue;
1690                 }
1691
1692                 if (weston_wm_handle_dnd_event(wm, event)) {
1693                         free(event);
1694                         count++;
1695                         continue;
1696                 }
1697
1698                 switch (EVENT_TYPE(event)) {
1699                 case XCB_BUTTON_PRESS:
1700                 case XCB_BUTTON_RELEASE:
1701                         weston_wm_handle_button(wm, event);
1702                         break;
1703                 case XCB_ENTER_NOTIFY:
1704                         weston_wm_handle_enter(wm, event);
1705                         break;
1706                 case XCB_LEAVE_NOTIFY:
1707                         weston_wm_handle_leave(wm, event);
1708                         break;
1709                 case XCB_MOTION_NOTIFY:
1710                         weston_wm_handle_motion(wm, event);
1711                         break;
1712                 case XCB_CREATE_NOTIFY:
1713                         weston_wm_handle_create_notify(wm, event);
1714                         break;
1715                 case XCB_MAP_REQUEST:
1716                         weston_wm_handle_map_request(wm, event);
1717                         break;
1718                 case XCB_MAP_NOTIFY:
1719                         weston_wm_handle_map_notify(wm, event);
1720                         break;
1721                 case XCB_UNMAP_NOTIFY:
1722                         weston_wm_handle_unmap_notify(wm, event);
1723                         break;
1724                 case XCB_REPARENT_NOTIFY:
1725                         weston_wm_handle_reparent_notify(wm, event);
1726                         break;
1727                 case XCB_CONFIGURE_REQUEST:
1728                         weston_wm_handle_configure_request(wm, event);
1729                         break;
1730                 case XCB_CONFIGURE_NOTIFY:
1731                         weston_wm_handle_configure_notify(wm, event);
1732                         break;
1733                 case XCB_DESTROY_NOTIFY:
1734                         weston_wm_handle_destroy_notify(wm, event);
1735                         break;
1736                 case XCB_MAPPING_NOTIFY:
1737                         wm_log("XCB_MAPPING_NOTIFY\n");
1738                         break;
1739                 case XCB_PROPERTY_NOTIFY:
1740                         weston_wm_handle_property_notify(wm, event);
1741                         break;
1742                 case XCB_CLIENT_MESSAGE:
1743                         weston_wm_handle_client_message(wm, event);
1744                         break;
1745                 }
1746
1747                 free(event);
1748                 count++;
1749         }
1750
1751         xcb_flush(wm->conn);
1752
1753         return count;
1754 }
1755
1756 static void
1757 weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1758 {
1759         xcb_depth_iterator_t d_iter;
1760         xcb_visualtype_iterator_t vt_iter;
1761         xcb_visualtype_t *visualtype;
1762
1763         d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1764         visualtype = NULL;
1765         while (d_iter.rem > 0) {
1766                 if (d_iter.data->depth == 32) {
1767                         vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1768                         visualtype = vt_iter.data;
1769                         break;
1770                 }
1771
1772                 xcb_depth_next(&d_iter);
1773         }
1774
1775         if (visualtype == NULL) {
1776                 weston_log("no 32 bit visualtype\n");
1777                 return;
1778         }
1779
1780         wm->visual_id = visualtype->visual_id;
1781         wm->colormap = xcb_generate_id(wm->conn);
1782         xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1783                             wm->colormap, wm->screen->root, wm->visual_id);
1784 }
1785
1786 static void
1787 weston_wm_get_resources(struct weston_wm *wm)
1788 {
1789
1790 #define F(field) offsetof(struct weston_wm, field)
1791
1792         static const struct { const char *name; int offset; } atoms[] = {
1793                 { "WM_PROTOCOLS",       F(atom.wm_protocols) },
1794                 { "WM_NORMAL_HINTS",    F(atom.wm_normal_hints) },
1795                 { "WM_TAKE_FOCUS",      F(atom.wm_take_focus) },
1796                 { "WM_DELETE_WINDOW",   F(atom.wm_delete_window) },
1797                 { "WM_STATE",           F(atom.wm_state) },
1798                 { "WM_S0",              F(atom.wm_s0) },
1799                 { "WM_CLIENT_MACHINE",  F(atom.wm_client_machine) },
1800                 { "_NET_WM_CM_S0",      F(atom.net_wm_cm_s0) },
1801                 { "_NET_WM_NAME",       F(atom.net_wm_name) },
1802                 { "_NET_WM_PID",        F(atom.net_wm_pid) },
1803                 { "_NET_WM_ICON",       F(atom.net_wm_icon) },
1804                 { "_NET_WM_STATE",      F(atom.net_wm_state) },
1805                 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1806                 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1807                 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1808                 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1809
1810                 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1811                 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1812                 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1813                 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1814                 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1815                 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1816                 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
1817                 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1818                 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
1819                 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1820                 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1821                 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1822                 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1823                 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1824
1825                 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1826                 { "_NET_SUPPORTING_WM_CHECK",
1827                                         F(atom.net_supporting_wm_check) },
1828                 { "_NET_SUPPORTED",     F(atom.net_supported) },
1829                 { "_MOTIF_WM_HINTS",    F(atom.motif_wm_hints) },
1830                 { "CLIPBOARD",          F(atom.clipboard) },
1831                 { "CLIPBOARD_MANAGER",  F(atom.clipboard_manager) },
1832                 { "TARGETS",            F(atom.targets) },
1833                 { "UTF8_STRING",        F(atom.utf8_string) },
1834                 { "_WL_SELECTION",      F(atom.wl_selection) },
1835                 { "INCR",               F(atom.incr) },
1836                 { "TIMESTAMP",          F(atom.timestamp) },
1837                 { "MULTIPLE",           F(atom.multiple) },
1838                 { "UTF8_STRING" ,       F(atom.utf8_string) },
1839                 { "COMPOUND_TEXT",      F(atom.compound_text) },
1840                 { "TEXT",               F(atom.text) },
1841                 { "STRING",             F(atom.string) },
1842                 { "text/plain;charset=utf-8",   F(atom.text_plain_utf8) },
1843                 { "text/plain",         F(atom.text_plain) },
1844                 { "XdndSelection",      F(atom.xdnd_selection) },
1845                 { "XdndAware",          F(atom.xdnd_aware) },
1846                 { "XdndEnter",          F(atom.xdnd_enter) },
1847                 { "XdndLeave",          F(atom.xdnd_leave) },
1848                 { "XdndDrop",           F(atom.xdnd_drop) },
1849                 { "XdndStatus",         F(atom.xdnd_status) },
1850                 { "XdndFinished",       F(atom.xdnd_finished) },
1851                 { "XdndTypeList",       F(atom.xdnd_type_list) },
1852                 { "XdndActionCopy",     F(atom.xdnd_action_copy) },
1853                 { "WL_SURFACE_ID",      F(atom.wl_surface_id) }
1854         };
1855 #undef F
1856
1857         xcb_xfixes_query_version_cookie_t xfixes_cookie;
1858         xcb_xfixes_query_version_reply_t *xfixes_reply;
1859         xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1860         xcb_intern_atom_reply_t *reply;
1861         xcb_render_query_pict_formats_reply_t *formats_reply;
1862         xcb_render_query_pict_formats_cookie_t formats_cookie;
1863         xcb_render_pictforminfo_t *formats;
1864         uint32_t i;
1865
1866         xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1867         xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
1868
1869         formats_cookie = xcb_render_query_pict_formats(wm->conn);
1870
1871         for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1872                 cookies[i] = xcb_intern_atom (wm->conn, 0,
1873                                               strlen(atoms[i].name),
1874                                               atoms[i].name);
1875
1876         for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1877                 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1878                 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1879                 free(reply);
1880         }
1881
1882         wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1883         if (!wm->xfixes || !wm->xfixes->present)
1884                 weston_log("xfixes not available\n");
1885
1886         xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1887                                                  XCB_XFIXES_MAJOR_VERSION,
1888                                                  XCB_XFIXES_MINOR_VERSION);
1889         xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1890                                                       xfixes_cookie, NULL);
1891
1892         weston_log("xfixes version: %d.%d\n",
1893                xfixes_reply->major_version, xfixes_reply->minor_version);
1894
1895         free(xfixes_reply);
1896
1897         formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1898                                                             formats_cookie, 0);
1899         if (formats_reply == NULL)
1900                 return;
1901
1902         formats = xcb_render_query_pict_formats_formats(formats_reply);
1903         for (i = 0; i < formats_reply->num_formats; i++) {
1904                 if (formats[i].direct.red_mask != 0xff &&
1905                     formats[i].direct.red_shift != 16)
1906                         continue;
1907                 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1908                     formats[i].depth == 24)
1909                         wm->format_rgb = formats[i];
1910                 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1911                     formats[i].depth == 32 &&
1912                     formats[i].direct.alpha_mask == 0xff &&
1913                     formats[i].direct.alpha_shift == 24)
1914                         wm->format_rgba = formats[i];
1915         }
1916
1917         free(formats_reply);
1918 }
1919
1920 static void
1921 weston_wm_create_wm_window(struct weston_wm *wm)
1922 {
1923         static const char name[] = "Weston WM";
1924
1925         wm->wm_window = xcb_generate_id(wm->conn);
1926         xcb_create_window(wm->conn,
1927                           XCB_COPY_FROM_PARENT,
1928                           wm->wm_window,
1929                           wm->screen->root,
1930                           0, 0,
1931                           10, 10,
1932                           0,
1933                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
1934                           wm->screen->root_visual,
1935                           0, NULL);
1936
1937         xcb_change_property(wm->conn,
1938                             XCB_PROP_MODE_REPLACE,
1939                             wm->wm_window,
1940                             wm->atom.net_supporting_wm_check,
1941                             XCB_ATOM_WINDOW,
1942                             32, /* format */
1943                             1, &wm->wm_window);
1944
1945         xcb_change_property(wm->conn,
1946                             XCB_PROP_MODE_REPLACE,
1947                             wm->wm_window,
1948                             wm->atom.net_wm_name,
1949                             wm->atom.utf8_string,
1950                             8, /* format */
1951                             strlen(name), name);
1952
1953         xcb_change_property(wm->conn,
1954                             XCB_PROP_MODE_REPLACE,
1955                             wm->screen->root,
1956                             wm->atom.net_supporting_wm_check,
1957                             XCB_ATOM_WINDOW,
1958                             32, /* format */
1959                             1, &wm->wm_window);
1960
1961         /* Claim the WM_S0 selection even though we don't suport
1962          * the --replace functionality. */
1963         xcb_set_selection_owner(wm->conn,
1964                                 wm->wm_window,
1965                                 wm->atom.wm_s0,
1966                                 XCB_TIME_CURRENT_TIME);
1967
1968         xcb_set_selection_owner(wm->conn,
1969                                 wm->wm_window,
1970                                 wm->atom.net_wm_cm_s0,
1971                                 XCB_TIME_CURRENT_TIME);
1972 }
1973
1974 struct weston_wm *
1975 weston_wm_create(struct weston_xserver *wxs, int fd)
1976 {
1977         struct weston_wm *wm;
1978         struct wl_event_loop *loop;
1979         xcb_screen_iterator_t s;
1980         uint32_t values[1];
1981         xcb_atom_t supported[3];
1982
1983         wm = zalloc(sizeof *wm);
1984         if (wm == NULL)
1985                 return NULL;
1986
1987         wm->server = wxs;
1988         wm->window_hash = hash_table_create();
1989         if (wm->window_hash == NULL) {
1990                 free(wm);
1991                 return NULL;
1992         }
1993
1994         /* xcb_connect_to_fd takes ownership of the fd. */
1995         wm->conn = xcb_connect_to_fd(fd, NULL);
1996         if (xcb_connection_has_error(wm->conn)) {
1997                 weston_log("xcb_connect_to_fd failed\n");
1998                 close(fd);
1999                 hash_table_destroy(wm->window_hash);
2000                 free(wm);
2001                 return NULL;
2002         }
2003
2004         s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2005         wm->screen = s.data;
2006
2007         loop = wl_display_get_event_loop(wxs->wl_display);
2008         wm->source =
2009                 wl_event_loop_add_fd(loop, fd,
2010                                      WL_EVENT_READABLE,
2011                                      weston_wm_handle_event, wm);
2012         wl_event_source_check(wm->source);
2013
2014         weston_wm_get_resources(wm);
2015         weston_wm_get_visual_and_colormap(wm);
2016
2017         values[0] =
2018                 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2019                 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2020                 XCB_EVENT_MASK_PROPERTY_CHANGE;
2021         xcb_change_window_attributes(wm->conn, wm->screen->root,
2022                                      XCB_CW_EVENT_MASK, values);
2023
2024         xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2025                                           XCB_COMPOSITE_REDIRECT_MANUAL);
2026
2027         wm->theme = theme_create();
2028
2029         supported[0] = wm->atom.net_wm_moveresize;
2030         supported[1] = wm->atom.net_wm_state;
2031         supported[2] = wm->atom.net_wm_state_fullscreen;
2032         xcb_change_property(wm->conn,
2033                             XCB_PROP_MODE_REPLACE,
2034                             wm->screen->root,
2035                             wm->atom.net_supported,
2036                             XCB_ATOM_ATOM,
2037                             32, /* format */
2038                             ARRAY_LENGTH(supported), supported);
2039
2040         weston_wm_selection_init(wm);
2041
2042         weston_wm_dnd_init(wm);
2043
2044         xcb_flush(wm->conn);
2045
2046         wm->create_surface_listener.notify = weston_wm_create_surface;
2047         wl_signal_add(&wxs->compositor->create_surface_signal,
2048                       &wm->create_surface_listener);
2049         wm->activate_listener.notify = weston_wm_window_activate;
2050         wl_signal_add(&wxs->compositor->activate_signal,
2051                       &wm->activate_listener);
2052         wm->transform_listener.notify = weston_wm_window_transform;
2053         wl_signal_add(&wxs->compositor->transform_signal,
2054                       &wm->transform_listener);
2055         wm->kill_listener.notify = weston_wm_kill_client;
2056         wl_signal_add(&wxs->compositor->kill_signal,
2057                       &wm->kill_listener);
2058         wl_list_init(&wm->unpaired_window_list);
2059
2060         weston_wm_create_cursors(wm);
2061         weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
2062
2063         /* Create wm window and take WM_S0 selection last, which
2064          * signals to Xwayland that we're done with setup. */
2065         weston_wm_create_wm_window(wm);
2066
2067         weston_log("created wm, root %d\n", wm->screen->root);
2068
2069         return wm;
2070 }
2071
2072 void
2073 weston_wm_destroy(struct weston_wm *wm)
2074 {
2075         /* FIXME: Free windows in hash. */
2076         hash_table_destroy(wm->window_hash);
2077         weston_wm_destroy_cursors(wm);
2078         xcb_disconnect(wm->conn);
2079         wl_event_source_remove(wm->source);
2080         wl_list_remove(&wm->selection_listener.link);
2081         wl_list_remove(&wm->activate_listener.link);
2082         wl_list_remove(&wm->kill_listener.link);
2083         wl_list_remove(&wm->transform_listener.link);
2084
2085         free(wm);
2086 }
2087
2088 static struct weston_wm_window *
2089 get_wm_window(struct weston_surface *surface)
2090 {
2091         struct wl_listener *listener;
2092
2093         listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
2094         if (listener)
2095                 return container_of(listener, struct weston_wm_window,
2096                                     surface_destroy_listener);
2097
2098         return NULL;
2099 }
2100
2101 static void
2102 weston_wm_window_configure(void *data)
2103 {
2104         struct weston_wm_window *window = data;
2105         struct weston_wm *wm = window->wm;
2106         uint32_t values[4];
2107         int x, y, width, height;
2108
2109         weston_wm_window_get_child_position(window, &x, &y);
2110         values[0] = x;
2111         values[1] = y;
2112         values[2] = window->width;
2113         values[3] = window->height;
2114         xcb_configure_window(wm->conn,
2115                              window->id,
2116                              XCB_CONFIG_WINDOW_X |
2117                              XCB_CONFIG_WINDOW_Y |
2118                              XCB_CONFIG_WINDOW_WIDTH |
2119                              XCB_CONFIG_WINDOW_HEIGHT,
2120                              values);
2121
2122         weston_wm_window_get_frame_size(window, &width, &height);
2123         values[0] = width;
2124         values[1] = height;
2125         xcb_configure_window(wm->conn,
2126                              window->frame_id,
2127                              XCB_CONFIG_WINDOW_WIDTH |
2128                              XCB_CONFIG_WINDOW_HEIGHT,
2129                              values);
2130
2131         window->configure_source = NULL;
2132
2133         weston_wm_window_schedule_repaint(window);
2134 }
2135
2136 static void
2137 send_configure(struct weston_surface *surface,
2138                uint32_t edges, int32_t width, int32_t height)
2139 {
2140         struct weston_wm_window *window = get_wm_window(surface);
2141         struct weston_wm *wm = window->wm;
2142         struct theme *t = window->wm->theme;
2143         int vborder, hborder;
2144
2145         if (window->fullscreen) {
2146                 hborder = 0;
2147                 vborder = 0;
2148         } else if (window->decorate) {
2149                 hborder = 2 * (t->margin + t->width);
2150                 vborder = 2 * t->margin + t->titlebar_height + t->width;
2151         } else {
2152                 hborder = 2 * t->margin;
2153                 vborder = 2 * t->margin;
2154         }
2155
2156         if (width > hborder)
2157                 window->width = width - hborder;
2158         else
2159                 window->width = 1;
2160
2161         if (height > vborder)
2162                 window->height = height - vborder;
2163         else
2164                 window->height = 1;
2165
2166         if (window->frame)
2167                 frame_resize_inside(window->frame, window->width, window->height);
2168
2169         if (window->configure_source)
2170                 return;
2171
2172         window->configure_source =
2173                 wl_event_loop_add_idle(wm->server->loop,
2174                                        weston_wm_window_configure, window);
2175 }
2176
2177 static const struct weston_shell_client shell_client = {
2178         send_configure
2179 };
2180
2181 static int
2182 legacy_fullscreen(struct weston_wm *wm,
2183                   struct weston_wm_window *window,
2184                   struct weston_output **output_ret)
2185 {
2186         struct weston_compositor *compositor = wm->server->compositor;
2187         struct weston_output *output;
2188         uint32_t minmax = PMinSize | PMaxSize;
2189         int matching_size;
2190
2191         /* Heuristics for detecting legacy fullscreen windows... */
2192
2193         wl_list_for_each(output, &compositor->output_list, link) {
2194                 if (output->x == window->x &&
2195                     output->y == window->y &&
2196                     output->width == window->width &&
2197                     output->height == window->height &&
2198                     window->override_redirect) {
2199                         *output_ret = output;
2200                         return 1;
2201                 }
2202
2203                 matching_size = 0;
2204                 if ((window->size_hints.flags & (USSize |PSize)) &&
2205                     window->size_hints.width == output->width &&
2206                     window->size_hints.height == output->height)
2207                         matching_size = 1;
2208                 if ((window->size_hints.flags & minmax) == minmax &&
2209                     window->size_hints.min_width == output->width &&
2210                     window->size_hints.min_height == output->height &&
2211                     window->size_hints.max_width == output->width &&
2212                     window->size_hints.max_height == output->height)
2213                         matching_size = 1;
2214
2215                 if (matching_size && !window->decorate &&
2216                     (window->size_hints.flags & (USPosition | PPosition)) &&
2217                     window->size_hints.x == output->x &&
2218                     window->size_hints.y == output->y) {
2219                         *output_ret = output;
2220                         return 1;
2221                 }
2222         }
2223
2224         return 0;
2225 }
2226
2227 static void
2228 xserver_map_shell_surface(struct weston_wm_window *window,
2229                           struct weston_surface *surface)
2230 {
2231         struct weston_wm *wm = window->wm;
2232         struct weston_shell_interface *shell_interface =
2233                 &wm->server->compositor->shell_interface;
2234         struct weston_output *output;
2235         struct weston_wm_window *parent;
2236
2237         weston_wm_window_read_properties(window);
2238
2239         /* A weston_wm_window may have many different surfaces assigned
2240          * throughout its life, so we must make sure to remove the listener
2241          * from the old surface signal list. */
2242         if (window->surface)
2243                 wl_list_remove(&window->surface_destroy_listener.link);
2244
2245         window->surface = surface;
2246         window->surface_destroy_listener.notify = surface_destroy;
2247         wl_signal_add(&window->surface->destroy_signal,
2248                       &window->surface_destroy_listener);
2249
2250         weston_wm_window_schedule_repaint(window);
2251
2252         if (!shell_interface->create_shell_surface)
2253                 return;
2254
2255         if (!shell_interface->get_primary_view)
2256                 return;
2257
2258         window->shsurf = 
2259                 shell_interface->create_shell_surface(shell_interface->shell,
2260                                                       window->surface,
2261                                                       &shell_client);
2262         window->view = shell_interface->get_primary_view(shell_interface->shell,
2263                                                          window->shsurf);
2264
2265         if (window->name)
2266                 shell_interface->set_title(window->shsurf, window->name);
2267
2268         if (window->fullscreen) {
2269                 window->saved_width = window->width;
2270                 window->saved_height = window->height;
2271                 shell_interface->set_fullscreen(window->shsurf,
2272                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2273                                                 0, NULL);
2274                 return;
2275         } else if (legacy_fullscreen(wm, window, &output)) {
2276                 window->fullscreen = 1;
2277                 shell_interface->set_fullscreen(window->shsurf,
2278                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2279                                                 0, output);
2280         } else if (window->override_redirect) {
2281                 shell_interface->set_xwayland(window->shsurf,
2282                                               window->x,
2283                                               window->y,
2284                                               WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2285         } else if (window->transient_for && window->transient_for->surface) {
2286                 parent = window->transient_for;
2287                 shell_interface->set_transient(window->shsurf,
2288                                                parent->surface,
2289                                                window->x - parent->x,
2290                                                window->y - parent->y, 0);
2291         } else {
2292                 shell_interface->set_toplevel(window->shsurf);
2293         }
2294 }