blob: e705fec16453822d8896c64f7cf2bc53e46a9706 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
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#define _GNU_SOURCE
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>
Tiago Vignatti90fada42012-07-16 12:02:08 -040034#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040035
36#include "xwayland.h"
37
38#include "../../shared/cairo-util.h"
39#include "../compositor.h"
40#include "xserver-server-protocol.h"
41#include "hash.h"
42
43struct motif_wm_hints {
44 uint32_t flags;
45 uint32_t functions;
46 uint32_t decorations;
47 int32_t input_mode;
48 uint32_t status;
49};
50
51#define MWM_HINTS_FUNCTIONS (1L << 0)
52#define MWM_HINTS_DECORATIONS (1L << 1)
53#define MWM_HINTS_INPUT_MODE (1L << 2)
54#define MWM_HINTS_STATUS (1L << 3)
55
56#define MWM_FUNC_ALL (1L << 0)
57#define MWM_FUNC_RESIZE (1L << 1)
58#define MWM_FUNC_MOVE (1L << 2)
59#define MWM_FUNC_MINIMIZE (1L << 3)
60#define MWM_FUNC_MAXIMIZE (1L << 4)
61#define MWM_FUNC_CLOSE (1L << 5)
62
63#define MWM_DECOR_ALL (1L << 0)
64#define MWM_DECOR_BORDER (1L << 1)
65#define MWM_DECOR_RESIZEH (1L << 2)
66#define MWM_DECOR_TITLE (1L << 3)
67#define MWM_DECOR_MENU (1L << 4)
68#define MWM_DECOR_MINIMIZE (1L << 5)
69#define MWM_DECOR_MAXIMIZE (1L << 6)
70
71#define MWM_INPUT_MODELESS 0
72#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
73#define MWM_INPUT_SYSTEM_MODAL 2
74#define MWM_INPUT_FULL_APPLICATION_MODAL 3
75#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
76
77#define MWM_TEAROFF_WINDOW (1L<<0)
78
79#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
80#define _NET_WM_MOVERESIZE_SIZE_TOP 1
81#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
82#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
83#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
84#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
85#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
86#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
87#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
88#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
89#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
90#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
91
92
93
94struct weston_wm_window {
95 struct weston_wm *wm;
96 xcb_window_t id;
97 xcb_window_t frame_id;
98 cairo_surface_t *cairo_surface;
99 struct weston_surface *surface;
100 struct shell_surface *shsurf;
101 struct wl_listener surface_destroy_listener;
102 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400103 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400104 int properties_dirty;
105 char *class;
106 char *name;
107 struct weston_wm_window *transient_for;
108 uint32_t protocols;
109 xcb_atom_t type;
110 int width, height;
111 int x, y;
112 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300113 int override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400114};
115
116static struct weston_wm_window *
117get_wm_window(struct weston_surface *surface);
118
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400119static void
120weston_wm_window_schedule_repaint(struct weston_wm_window *window);
121
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400122const char *
123get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
124{
125 xcb_get_atom_name_cookie_t cookie;
126 xcb_get_atom_name_reply_t *reply;
127 xcb_generic_error_t *e;
128 static char buffer[64];
129
130 if (atom == XCB_ATOM_NONE)
131 return "None";
132
133 cookie = xcb_get_atom_name (c, atom);
134 reply = xcb_get_atom_name_reply (c, cookie, &e);
135 snprintf(buffer, sizeof buffer, "%.*s",
136 xcb_get_atom_name_name_length (reply),
137 xcb_get_atom_name_name (reply));
138 free(reply);
139
140 return buffer;
141}
142
Tiago Vignatti90fada42012-07-16 12:02:08 -0400143static xcb_cursor_t
144xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
145{
146 xcb_connection_t *c = wm->conn;
147 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
148 xcb_screen_t *screen = s.data;
149 xcb_gcontext_t gc;
150 xcb_pixmap_t pix;
151 xcb_render_picture_t pic;
152 xcb_cursor_t cursor;
153 int stride = img->width * 4;
154
155 pix = xcb_generate_id(c);
156 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
157
158 pic = xcb_generate_id(c);
159 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
160
161 gc = xcb_generate_id(c);
162 xcb_create_gc(c, gc, pix, 0, 0);
163
164 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
165 img->width, img->height, 0, 0, 0, 32,
166 stride * img->height, (uint8_t *) img->pixels);
167 xcb_free_gc(c, gc);
168
169 cursor = xcb_generate_id(c);
170 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
171
172 xcb_render_free_picture(c, pic);
173 xcb_free_pixmap(c, pix);
174
175 return cursor;
176}
177
178static xcb_cursor_t
179xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
180{
181 /* TODO: treat animated cursors as well */
182 if (images->nimage != 1)
183 return -1;
184
185 return xcb_cursor_image_load_cursor(wm, images->images[0]);
186}
187
188static xcb_cursor_t
189xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
190{
191 xcb_cursor_t cursor;
192 XcursorImages *images;
193 char *v = NULL;
194 int size = 0;
195
196 if (!file)
197 return 0;
198
199 v = getenv ("XCURSOR_SIZE");
200 if (v)
201 size = atoi(v);
202
203 if (!size)
204 size = 32;
205
206 images = XcursorLibraryLoadImages (file, NULL, size);
207 cursor = xcb_cursor_images_load_cursor (wm, images);
208 XcursorImagesDestroy (images);
209
210 return cursor;
211}
212
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400213void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400214dump_property(struct weston_wm *wm,
215 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400216{
217 int32_t *incr_value;
218 const char *text_value, *name;
219 xcb_atom_t *atom_value;
220 int width, len;
221 uint32_t i;
222
Martin Minarik6d118362012-06-07 18:01:59 +0200223 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400224 if (reply == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200225 weston_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400226 return;
227 }
228
Martin Minarik6d118362012-06-07 18:01:59 +0200229 width += weston_log_continue(
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400230 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400231 get_atom_name(wm->conn, reply->type),
232 reply->format,
233 xcb_get_property_value_length(reply),
234 reply->value_len);
235
236 if (reply->type == wm->atom.incr) {
237 incr_value = xcb_get_property_value(reply);
Martin Minarik6d118362012-06-07 18:01:59 +0200238 weston_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400239 } else if (reply->type == wm->atom.utf8_string ||
240 reply->type == wm->atom.string) {
241 text_value = xcb_get_property_value(reply);
242 if (reply->value_len > 40)
243 len = 40;
244 else
245 len = reply->value_len;
Martin Minarik6d118362012-06-07 18:01:59 +0200246 weston_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400247 } else if (reply->type == XCB_ATOM_ATOM) {
248 atom_value = xcb_get_property_value(reply);
249 for (i = 0; i < reply->value_len; i++) {
250 name = get_atom_name(wm->conn, atom_value[i]);
251 if (width + strlen(name) + 2 > 78) {
Martin Minarik6d118362012-06-07 18:01:59 +0200252 weston_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400253 width = 4;
254 } else if (i > 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200255 width += weston_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400256 }
257
Martin Minarik6d118362012-06-07 18:01:59 +0200258 width += weston_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400259 }
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400261 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200262 weston_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400263 }
264}
265
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400266void
267read_and_dump_property(struct weston_wm *wm,
268 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400269{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400270 xcb_get_property_reply_t *reply;
271 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400272
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400273 cookie = xcb_get_property(wm->conn, 0, window,
274 property, XCB_ATOM_ANY, 0, 2048);
275 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400276
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400277 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400278
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400279 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400280}
281
282/* We reuse some predefined, but otherwise useles atoms */
283#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
284#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
285
286static void
287weston_wm_window_read_properties(struct weston_wm_window *window)
288{
289 struct weston_wm *wm = window->wm;
290
291#define F(field) offsetof(struct weston_wm_window, field)
292 const struct {
293 xcb_atom_t atom;
294 xcb_atom_t type;
295 int offset;
296 } props[] = {
297 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
298 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
299 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
300 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
301 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
302 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
303 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
304 };
305#undef F
306
307 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
308 xcb_get_property_reply_t *reply;
309 void *p;
310 uint32_t *xid;
311 xcb_atom_t *atom;
312 uint32_t i;
313 struct motif_wm_hints *hints;
314
315 if (!window->properties_dirty)
316 return;
317 window->properties_dirty = 0;
318
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400319 for (i = 0; i < ARRAY_LENGTH(props); i++)
320 cookie[i] = xcb_get_property(wm->conn,
321 0, /* delete */
322 window->id,
323 props[i].atom,
324 XCB_ATOM_ANY, 0, 2048);
325
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300326 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400327 for (i = 0; i < ARRAY_LENGTH(props); i++) {
328 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
329 if (!reply)
330 /* Bad window, typically */
331 continue;
332 if (reply->type == XCB_ATOM_NONE) {
333 /* No such property */
334 free(reply);
335 continue;
336 }
337
338 p = ((char *) window + props[i].offset);
339
340 switch (props[i].type) {
341 case XCB_ATOM_STRING:
342 /* FIXME: We're using this for both string and
343 utf8_string */
344 if (*(char **) p)
345 free(*(char **) p);
346
347 *(char **) p =
348 strndup(xcb_get_property_value(reply),
349 xcb_get_property_value_length(reply));
350 break;
351 case XCB_ATOM_WINDOW:
352 xid = xcb_get_property_value(reply);
353 *(struct weston_wm_window **) p =
354 hash_table_lookup(wm->window_hash, *xid);
355 break;
356 case XCB_ATOM_ATOM:
357 atom = xcb_get_property_value(reply);
358 *(xcb_atom_t *) p = *atom;
359 break;
360 case TYPE_WM_PROTOCOLS:
361 break;
362 case TYPE_MOTIF_WM_HINTS:
363 hints = xcb_get_property_value(reply);
364 if (hints->flags & MWM_HINTS_DECORATIONS)
365 window->decorate = hints->decorations > 0;
366 break;
367 default:
368 break;
369 }
370 free(reply);
371 }
372}
373
374static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400375weston_wm_window_get_frame_size(struct weston_wm_window *window,
376 int *width, int *height)
377{
378 struct theme *t = window->wm->theme;
379
380 if (window->decorate) {
381 *width = window->width + (t->margin + t->width) * 2;
382 *height = window->height +
383 t->margin * 2 + t->width + t->titlebar_height;
384 } else {
385 *width = window->width + t->margin * 2;
386 *height = window->height + t->margin * 2;
387 }
388}
389
390static void
391weston_wm_window_get_child_position(struct weston_wm_window *window,
392 int *x, int *y)
393{
394 struct theme *t = window->wm->theme;
395
396 if (window->decorate) {
397 *x = t->margin + t->width;
398 *y = t->margin + t->titlebar_height;
399 } else {
400 *x = t->margin;
401 *y = t->margin;
402 }
403}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400404
405static void
406weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
407{
408 xcb_configure_request_event_t *configure_request =
409 (xcb_configure_request_event_t *) event;
410 struct weston_wm_window *window;
411 uint32_t mask, values[16];
412 int x, y, width, height, i = 0;
413
Martin Minarik6d118362012-06-07 18:01:59 +0200414 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400415 configure_request->window,
416 configure_request->x, configure_request->y,
417 configure_request->width, configure_request->height);
418
419 window = hash_table_lookup(wm->window_hash, configure_request->window);
420
421 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
422 window->width = configure_request->width;
423 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
424 window->height = configure_request->height;
425
426 weston_wm_window_get_child_position(window, &x, &y);
427 values[i++] = x;
428 values[i++] = y;
429 values[i++] = window->width;
430 values[i++] = window->height;
431 values[i++] = 0;
432 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
433 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
434 XCB_CONFIG_WINDOW_BORDER_WIDTH;
435 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
436 values[i++] = configure_request->sibling;
437 mask |= XCB_CONFIG_WINDOW_SIBLING;
438 }
439 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
440 values[i++] = configure_request->stack_mode;
441 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
442 }
443
444 xcb_configure_window(wm->conn, window->id, mask, values);
445
446 weston_wm_window_get_frame_size(window, &width, &height);
447 values[0] = width;
448 values[1] = height;
449 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
450 xcb_configure_window(wm->conn, window->frame_id, mask, values);
451
452 weston_wm_window_schedule_repaint(window);
453}
454
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400455static void
456weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
457{
458 xcb_configure_notify_event_t *configure_notify =
459 (xcb_configure_notify_event_t *) event;
460 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300461 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400462
463 window = hash_table_lookup(wm->window_hash, configure_notify->window);
464
Martin Minarik6d118362012-06-07 18:01:59 +0200465 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400466 configure_notify->window == window->id ? "client" : "frame",
467 configure_notify->window,
468 configure_notify->x, configure_notify->y,
469 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300470
471 /* resize falls here */
472 if (configure_notify->window != window->id)
473 return;
474
475 weston_wm_window_get_child_position(window, &x, &y);
476 window->x = configure_notify->x - x;
477 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400478}
479
480static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400481weston_wm_window_activate(struct wl_listener *listener, void *data)
482{
483 struct weston_surface *surface = data;
484 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600485 struct weston_wm *wm =
486 container_of(listener, struct weston_wm, activate_listener);
487 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488
Scott Moreau85ecac02012-05-21 15:49:14 -0600489 if (window) {
490 client_message.response_type = XCB_CLIENT_MESSAGE;
491 client_message.format = 32;
492 client_message.window = window->id;
493 client_message.type = wm->atom.wm_protocols;
494 client_message.data.data32[0] = wm->atom.wm_take_focus;
495 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
496
497 xcb_send_event(wm->conn, 0, window->id,
498 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
499 (char *) &client_message);
500
501 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
502 window->id, XCB_TIME_CURRENT_TIME);
503 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400504 xcb_set_input_focus (wm->conn,
505 XCB_INPUT_FOCUS_POINTER_ROOT,
506 XCB_NONE,
507 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600508 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400509
510 if (wm->focus_window)
511 weston_wm_window_schedule_repaint(wm->focus_window);
512 wm->focus_window = window;
Tiago Vignattice1baa82012-07-20 23:09:55 +0300513 if (window)
514 wm->focus_latest = window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400515 if (wm->focus_window)
516 weston_wm_window_schedule_repaint(wm->focus_window);
517}
518
519static int
520our_resource(struct weston_wm *wm, uint32_t id)
521{
522 const xcb_setup_t *setup;
523
524 setup = xcb_get_setup(wm->conn);
525
526 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
527}
528
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400529#define ICCCM_WITHDRAWN_STATE 0
530#define ICCCM_NORMAL_STATE 1
531#define ICCCM_ICONIC_STATE 3
532
533static void
534weston_wm_window_set_state(struct weston_wm_window *window, int32_t state)
535{
536 struct weston_wm *wm = window->wm;
537 uint32_t property[2];
538
539 property[0] = state;
540 property[1] = XCB_WINDOW_NONE;
541
542 xcb_change_property(wm->conn,
543 XCB_PROP_MODE_REPLACE,
544 window->id,
545 wm->atom.wm_state,
546 wm->atom.wm_state,
547 32, /* format */
548 2, property);
549}
550
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400551static void
552weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
553{
554 xcb_map_request_event_t *map_request =
555 (xcb_map_request_event_t *) event;
556 struct weston_wm_window *window;
557 uint32_t values[1];
558 int x, y, width, height;
559
560 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200561 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400562 map_request->window);
563 return;
564 }
565
566 window = hash_table_lookup(wm->window_hash, map_request->window);
567
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400568 if (window->frame_id)
569 return;
570
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400571 weston_wm_window_read_properties(window);
572
573 weston_wm_window_get_frame_size(window, &width, &height);
574 weston_wm_window_get_child_position(window, &x, &y);
575
576 values[0] =
577 XCB_EVENT_MASK_KEY_PRESS |
578 XCB_EVENT_MASK_KEY_RELEASE |
579 XCB_EVENT_MASK_BUTTON_PRESS |
580 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400581 XCB_EVENT_MASK_POINTER_MOTION |
582 XCB_EVENT_MASK_ENTER_WINDOW |
583 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400584 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400585 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400586
587 window->frame_id = xcb_generate_id(wm->conn);
588 xcb_create_window(wm->conn,
589 XCB_COPY_FROM_PARENT,
590 window->frame_id,
591 wm->screen->root,
592 0, 0,
593 width, height,
594 0,
595 XCB_WINDOW_CLASS_INPUT_OUTPUT,
596 wm->screen->root_visual,
597 XCB_CW_EVENT_MASK, values);
598 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
599
600 values[0] = 0;
601 xcb_configure_window(wm->conn, window->id,
602 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
603
Martin Minarik6d118362012-06-07 18:01:59 +0200604 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400605 window->id, window, window->frame_id);
606
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400607 xcb_map_window(wm->conn, map_request->window);
608 xcb_map_window(wm->conn, window->frame_id);
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400609 weston_wm_window_set_state(window, ICCCM_NORMAL_STATE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400610
611 window->cairo_surface =
612 cairo_xcb_surface_create_with_xrender_format(wm->conn,
613 wm->screen,
614 window->frame_id,
Kristian Høgsberge89cef32012-07-16 11:57:08 -0400615 &wm->format_rgb,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400616 width, height);
617
618 hash_table_insert(wm->window_hash, window->frame_id, window);
619}
620
621static void
622weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
623{
624 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
625
626 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200627 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400628 map_notify->window);
629 return;
630 }
631
Martin Minarik6d118362012-06-07 18:01:59 +0200632 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400633}
634
635static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400636weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
637{
638 xcb_unmap_notify_event_t *unmap_notify =
639 (xcb_unmap_notify_event_t *) event;
640 struct weston_wm_window *window;
641
Martin Minarik6d118362012-06-07 18:01:59 +0200642 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400643 unmap_notify->window,
644 unmap_notify->event,
645 our_resource(wm, unmap_notify->window) ? ", ours" : "");
646
647 if (our_resource(wm, unmap_notify->window))
648 return;
649
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400650 if (unmap_notify->response_type & 0x80)
651 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
652 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400653 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400654
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400655 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400656 if (window->repaint_source)
657 wl_event_source_remove(window->repaint_source);
658 if (window->cairo_surface)
659 cairo_surface_destroy(window->cairo_surface);
660
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400661 if (window->frame_id) {
662 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
663 xcb_destroy_window(wm->conn, window->frame_id);
664 weston_wm_window_set_state(window, ICCCM_WITHDRAWN_STATE);
665 hash_table_remove(wm->window_hash, window->frame_id);
666 window->frame_id = XCB_WINDOW_NONE;
667 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400668
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400669 if (wm->focus_window == window)
670 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400671 if (window->surface)
672 wl_list_remove(&window->surface_destroy_listener.link);
673 window->surface = NULL;
674}
675
676static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400677weston_wm_window_draw_decoration(void *data)
678{
679 struct weston_wm_window *window = data;
680 struct weston_wm *wm = window->wm;
681 struct theme *t = wm->theme;
682 cairo_t *cr;
683 int x, y, width, height;
684 const char *title;
685 uint32_t flags = 0;
686
687 weston_wm_window_read_properties(window);
688
689 window->repaint_source = NULL;
690
691 weston_wm_window_get_frame_size(window, &width, &height);
692 weston_wm_window_get_child_position(window, &x, &y);
693
694 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
695 cr = cairo_create(window->cairo_surface);
696
697 if (window->decorate) {
698 if (wm->focus_window == window)
699 flags |= THEME_FRAME_ACTIVE;
700
701 if (window->name)
702 title = window->name;
703 else
704 title = "untitled";
705
706 theme_render_frame(t, cr, width, height, title, flags);
707 } else {
708 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
709 cairo_set_source_rgba(cr, 0, 0, 0, 0);
710 cairo_paint(cr);
711
712 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
713 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
714 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
715 }
716
717 cairo_destroy(cr);
718
719 if (window->surface) {
720 /* We leave an extra pixel around the X window area to
721 * make sure we don't sample from the undefined alpha
722 * channel when filtering. */
723 window->surface->opaque_rect[0] =
724 (double) (x - 1) / width;
725 window->surface->opaque_rect[1] =
726 (double) (x + window->width + 1) / width;
727 window->surface->opaque_rect[2] =
728 (double) (y - 1) / height;
729 window->surface->opaque_rect[3] =
730 (double) (y + window->height + 1) / height;
731
732 pixman_region32_init_rect(&window->surface->input,
733 t->margin, t->margin,
734 width - 2 * t->margin,
735 height - 2 * t->margin);
736 }
737}
738
739static void
740weston_wm_window_schedule_repaint(struct weston_wm_window *window)
741{
742 struct weston_wm *wm = window->wm;
743
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400744 if (window->frame_id == XCB_WINDOW_NONE) {
745 if (window->surface != NULL) {
746 window->surface->opaque_rect[0] = 0.0;
747 window->surface->opaque_rect[1] = 1.0;
748 window->surface->opaque_rect[2] = 0.0;
749 window->surface->opaque_rect[3] = 1.0;
750 }
751 return;
752 }
753
754 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400755 return;
756
757 window->repaint_source =
758 wl_event_loop_add_idle(wm->server->loop,
759 weston_wm_window_draw_decoration,
760 window);
761}
762
763static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
765{
766 xcb_property_notify_event_t *property_notify =
767 (xcb_property_notify_event_t *) event;
768 struct weston_wm_window *window;
769
770 window = hash_table_lookup(wm->window_hash, property_notify->window);
771 if (window)
772 window->properties_dirty = 1;
773
Martin Minarik6d118362012-06-07 18:01:59 +0200774 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400775 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400776 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200777 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400778 else
779 read_and_dump_property(wm, property_notify->window,
780 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400781
782 if (property_notify->atom == wm->atom.net_wm_name ||
783 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400784 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400785}
786
787static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400788weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300789 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400790{
791 struct weston_wm_window *window;
792 uint32_t values[1];
793
794 window = malloc(sizeof *window);
795 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200796 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400797 return;
798 }
799
800 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
801 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
802
803 memset(window, 0, sizeof *window);
804 window->wm = wm;
805 window->id = id;
806 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300807 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400808 window->width = width;
809 window->height = height;
810
811 hash_table_insert(wm->window_hash, id, window);
812}
813
814static void
815weston_wm_window_destroy(struct weston_wm_window *window)
816{
817 hash_table_remove(window->wm->window_hash, window->id);
818 free(window);
819}
820
821static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400822weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
823{
824 xcb_create_notify_event_t *create_notify =
825 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400826
Martin Minarik6d118362012-06-07 18:01:59 +0200827 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400828 create_notify->window,
829 create_notify->width, create_notify->height,
830 create_notify->override_redirect ? ", override" : "",
831 our_resource(wm, create_notify->window) ? ", ours" : "");
832
833 if (our_resource(wm, create_notify->window))
834 return;
835
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400836 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300837 create_notify->width, create_notify->height,
838 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839}
840
841static void
842weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
843{
844 xcb_destroy_notify_event_t *destroy_notify =
845 (xcb_destroy_notify_event_t *) event;
846 struct weston_wm_window *window;
847
Martin Minarik6d118362012-06-07 18:01:59 +0200848 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400849 destroy_notify->window,
850 destroy_notify->event,
851 our_resource(wm, destroy_notify->window) ? ", ours" : "");
852
853 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400854 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400855
856 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400857 weston_wm_window_destroy(window);
858}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400859
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400860static void
861weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
862{
863 xcb_reparent_notify_event_t *reparent_notify =
864 (xcb_reparent_notify_event_t *) event;
865 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400866
Martin Minarik6d118362012-06-07 18:01:59 +0200867 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400868 reparent_notify->window,
869 reparent_notify->parent,
870 reparent_notify->event);
871
872 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300873 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
874 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400875 } else if (!our_resource(wm, reparent_notify->parent)) {
876 window = hash_table_lookup(wm->window_hash,
877 reparent_notify->window);
878 weston_wm_window_destroy(window);
879 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400880}
881
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400882struct weston_seat *
883weston_wm_pick_seat(struct weston_wm *wm)
884{
885 return container_of(wm->server->compositor->seat_list.next,
886 struct weston_seat, link);
887}
888
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400889static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400890weston_wm_window_handle_moveresize(struct weston_wm_window *window,
891 xcb_client_message_event_t *client_message)
892{
893 static const int map[] = {
894 THEME_LOCATION_RESIZING_TOP_LEFT,
895 THEME_LOCATION_RESIZING_TOP,
896 THEME_LOCATION_RESIZING_TOP_RIGHT,
897 THEME_LOCATION_RESIZING_RIGHT,
898 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
899 THEME_LOCATION_RESIZING_BOTTOM,
900 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
901 THEME_LOCATION_RESIZING_LEFT
902 };
903
904 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400905 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400906 int detail;
907 struct weston_shell_interface *shell_interface =
908 &wm->server->compositor->shell_interface;
909
910 if (seat->seat.pointer->button_count != 1 ||
911 seat->seat.pointer->focus != &window->surface->surface)
912 return;
913
914 detail = client_message->data.data32[2];
915 switch (detail) {
916 case _NET_WM_MOVERESIZE_MOVE:
917 shell_interface->move(window->shsurf, seat);
918 break;
919 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
920 case _NET_WM_MOVERESIZE_SIZE_TOP:
921 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
922 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
923 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
924 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
925 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
926 case _NET_WM_MOVERESIZE_SIZE_LEFT:
927 shell_interface->resize(window->shsurf, seat, map[detail]);
928 break;
929 case _NET_WM_MOVERESIZE_CANCEL:
930 break;
931 }
932}
933
934static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400935weston_wm_handle_client_message(struct weston_wm *wm,
936 xcb_generic_event_t *event)
937{
938 xcb_client_message_event_t *client_message =
939 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400940 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400941
942 window = hash_table_lookup(wm->window_hash, client_message->window);
943
Martin Minarik6d118362012-06-07 18:01:59 +0200944 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945 get_atom_name(wm->conn, client_message->type),
946 client_message->data.data32[0],
947 client_message->data.data32[1],
948 client_message->data.data32[2],
949 client_message->data.data32[3],
950 client_message->data.data32[4]);
951
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400952 if (client_message->type == wm->atom.net_wm_moveresize)
953 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400954}
955
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400956enum cursor_type {
957 XWM_CURSOR_TOP,
958 XWM_CURSOR_BOTTOM,
959 XWM_CURSOR_LEFT,
960 XWM_CURSOR_RIGHT,
961 XWM_CURSOR_TOP_LEFT,
962 XWM_CURSOR_TOP_RIGHT,
963 XWM_CURSOR_BOTTOM_LEFT,
964 XWM_CURSOR_BOTTOM_RIGHT,
965 XWM_CURSOR_LEFT_PTR,
966};
967
968static const char *cursors[] = {
969 "top_side",
970 "bottom_side",
971 "left_side",
972 "right_side",
973 "top_left_corner",
974 "top_right_corner",
975 "bottom_left_corner",
976 "bottom_right_corner",
977 "left_ptr"
978};
979
980static void
981weston_wm_create_cursors(struct weston_wm *wm)
982{
983 int i, count = ARRAY_LENGTH(cursors);
984
985 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
986 for (i = 0; i < count; i++) {
987 wm->cursors[i] =
988 xcb_cursor_library_load_cursor(wm, cursors[i]);
989 }
990
991 wm->last_cursor = -1;
992}
993
994static void
995weston_wm_destroy_cursors(struct weston_wm *wm)
996{
997 uint8_t i;
998
999 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1000 xcb_free_cursor(wm->conn, wm->cursors[i]);
1001
1002 free(wm->cursors);
1003}
1004
1005static int
1006get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1007{
1008 int location = theme_get_location(t, x, y, width, height);
1009
1010 switch (location) {
1011 case THEME_LOCATION_RESIZING_TOP:
1012 return XWM_CURSOR_TOP;
1013 case THEME_LOCATION_RESIZING_BOTTOM:
1014 return XWM_CURSOR_BOTTOM;
1015 case THEME_LOCATION_RESIZING_LEFT:
1016 return XWM_CURSOR_LEFT;
1017 case THEME_LOCATION_RESIZING_RIGHT:
1018 return XWM_CURSOR_RIGHT;
1019 case THEME_LOCATION_RESIZING_TOP_LEFT:
1020 return XWM_CURSOR_TOP_LEFT;
1021 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1022 return XWM_CURSOR_TOP_RIGHT;
1023 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1024 return XWM_CURSOR_BOTTOM_LEFT;
1025 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1026 return XWM_CURSOR_BOTTOM_RIGHT;
1027 case THEME_LOCATION_EXTERIOR:
1028 case THEME_LOCATION_TITLEBAR:
1029 default:
1030 return XWM_CURSOR_LEFT_PTR;
1031 }
1032}
1033
1034static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001035weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1036 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001037{
1038 uint32_t cursor_value_list;
1039
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001040 if (wm->last_cursor == cursor)
1041 return;
1042
1043 wm->last_cursor = cursor;
1044
1045 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001046 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001047 XCB_CW_CURSOR, &cursor_value_list);
1048 xcb_flush(wm->conn);
1049}
1050
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001051static void
1052weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1053{
1054 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1055 struct weston_shell_interface *shell_interface =
1056 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001057 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001058 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001059 enum theme_location location;
1060 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001061 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062
Martin Minarik6d118362012-06-07 18:01:59 +02001063 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001064 button->response_type == XCB_BUTTON_PRESS ?
1065 "PRESS" : "RELEASE", button->detail);
1066
1067 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001068 weston_wm_window_get_frame_size(window, &width, &height);
1069
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001071 button->detail == 1) {
1072 location = theme_get_location(t,
1073 button->event_x,
1074 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001075 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001076
1077 switch (location) {
1078 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001079 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001080 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001081 case THEME_LOCATION_RESIZING_TOP:
1082 case THEME_LOCATION_RESIZING_BOTTOM:
1083 case THEME_LOCATION_RESIZING_LEFT:
1084 case THEME_LOCATION_RESIZING_RIGHT:
1085 case THEME_LOCATION_RESIZING_TOP_LEFT:
1086 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1087 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1088 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1089 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001090 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001091 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001092 default:
1093 break;
1094 }
1095 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001096}
1097
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001098static void
1099weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1100{
1101 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1102 struct weston_wm_window *window;
1103 int cursor, width, height;
1104
1105 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001106 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001107 return;
1108
1109 weston_wm_window_get_frame_size(window, &width, &height);
1110 cursor = get_cursor_for_location(wm->theme, width, height,
1111 motion->event_x, motion->event_y);
1112
Tiago Vignattic1903232012-07-16 12:15:37 -04001113 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001114}
1115
1116static void
1117weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1118{
1119 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1120 struct weston_wm_window *window;
1121 int cursor, width, height;
1122
1123 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001124 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001125 return;
1126
1127 weston_wm_window_get_frame_size(window, &width, &height);
1128 cursor = get_cursor_for_location(wm->theme, width, height,
1129 enter->event_x, enter->event_y);
1130
Tiago Vignattic1903232012-07-16 12:15:37 -04001131 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001132}
1133
1134static void
1135weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1136{
1137 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1138 struct weston_wm_window *window;
1139
1140 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001141 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001142 return;
1143
Tiago Vignattic1903232012-07-16 12:15:37 -04001144 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001145}
1146
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001147static int
1148weston_wm_handle_event(int fd, uint32_t mask, void *data)
1149{
1150 struct weston_wm *wm = data;
1151 xcb_generic_event_t *event;
1152 int count = 0;
1153
1154 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1155 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001156 free(event);
1157 count++;
1158 continue;
1159 }
1160
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001161 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001162 case XCB_BUTTON_PRESS:
1163 case XCB_BUTTON_RELEASE:
1164 weston_wm_handle_button(wm, event);
1165 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001166 case XCB_ENTER_NOTIFY:
1167 weston_wm_handle_enter(wm, event);
1168 break;
1169 case XCB_LEAVE_NOTIFY:
1170 weston_wm_handle_leave(wm, event);
1171 break;
1172 case XCB_MOTION_NOTIFY:
1173 weston_wm_handle_motion(wm, event);
1174 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001175 case XCB_CREATE_NOTIFY:
1176 weston_wm_handle_create_notify(wm, event);
1177 break;
1178 case XCB_MAP_REQUEST:
1179 weston_wm_handle_map_request(wm, event);
1180 break;
1181 case XCB_MAP_NOTIFY:
1182 weston_wm_handle_map_notify(wm, event);
1183 break;
1184 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001185 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001186 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001187 case XCB_REPARENT_NOTIFY:
1188 weston_wm_handle_reparent_notify(wm, event);
1189 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001190 case XCB_CONFIGURE_REQUEST:
1191 weston_wm_handle_configure_request(wm, event);
1192 break;
1193 case XCB_CONFIGURE_NOTIFY:
1194 weston_wm_handle_configure_notify(wm, event);
1195 break;
1196 case XCB_DESTROY_NOTIFY:
1197 weston_wm_handle_destroy_notify(wm, event);
1198 break;
1199 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001200 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001201 break;
1202 case XCB_PROPERTY_NOTIFY:
1203 weston_wm_handle_property_notify(wm, event);
1204 break;
1205 case XCB_CLIENT_MESSAGE:
1206 weston_wm_handle_client_message(wm, event);
1207 break;
1208 }
1209
1210 free(event);
1211 count++;
1212 }
1213
1214 xcb_flush(wm->conn);
1215
1216 return count;
1217}
1218
1219static void
1220wxs_wm_get_resources(struct weston_wm *wm)
1221{
1222
1223#define F(field) offsetof(struct weston_wm, field)
1224
1225 static const struct { const char *name; int offset; } atoms[] = {
1226 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1227 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1228 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001229 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001230 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001231 { "_NET_WM_NAME", F(atom.net_wm_name) },
1232 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1233 { "_NET_WM_STATE", F(atom.net_wm_state) },
1234 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1235 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1236 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1237 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1238
1239 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1240 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1241 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1242 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1243 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1244 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1245 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001246 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1247 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001248 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1249 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1250 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1251 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1252 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1253
1254 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1255 { "_NET_SUPPORTING_WM_CHECK",
1256 F(atom.net_supporting_wm_check) },
1257 { "_NET_SUPPORTED", F(atom.net_supported) },
1258 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1259 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001260 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001261 { "TARGETS", F(atom.targets) },
1262 { "UTF8_STRING", F(atom.utf8_string) },
1263 { "_WL_SELECTION", F(atom.wl_selection) },
1264 { "INCR", F(atom.incr) },
1265 { "TIMESTAMP", F(atom.timestamp) },
1266 { "MULTIPLE", F(atom.multiple) },
1267 { "UTF8_STRING" , F(atom.utf8_string) },
1268 { "COMPOUND_TEXT", F(atom.compound_text) },
1269 { "TEXT", F(atom.text) },
1270 { "STRING", F(atom.string) },
1271 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1272 { "text/plain", F(atom.text_plain) },
1273 };
1274#undef F
1275
1276 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1277 xcb_xfixes_query_version_reply_t *xfixes_reply;
1278 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1279 xcb_intern_atom_reply_t *reply;
1280 xcb_render_query_pict_formats_reply_t *formats_reply;
1281 xcb_render_query_pict_formats_cookie_t formats_cookie;
1282 xcb_render_pictforminfo_t *formats;
1283 uint32_t i;
1284
1285 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1286
1287 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1288
1289 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1290 cookies[i] = xcb_intern_atom (wm->conn, 0,
1291 strlen(atoms[i].name),
1292 atoms[i].name);
1293
1294 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1295 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1296 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1297 free(reply);
1298 }
1299
1300 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1301 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001302 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001303
1304 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1305 XCB_XFIXES_MAJOR_VERSION,
1306 XCB_XFIXES_MINOR_VERSION);
1307 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1308 xfixes_cookie, NULL);
1309
Martin Minarik6d118362012-06-07 18:01:59 +02001310 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001311 xfixes_reply->major_version, xfixes_reply->minor_version);
1312
1313 free(xfixes_reply);
1314
1315 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1316 formats_cookie, 0);
1317 if (formats_reply == NULL)
1318 return;
1319
1320 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001321 for (i = 0; i < formats_reply->num_formats; i++) {
1322 if (formats[i].direct.red_mask != 0xff &&
1323 formats[i].direct.red_shift != 16)
1324 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001325 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1326 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001327 wm->format_rgb = formats[i];
1328 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1329 formats[i].depth == 32 &&
1330 formats[i].direct.alpha_mask == 0xff &&
1331 formats[i].direct.alpha_shift == 24)
1332 wm->format_rgba = formats[i];
1333 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001334
1335 free(formats_reply);
1336}
1337
1338static void
1339weston_wm_create_wm_window(struct weston_wm *wm)
1340{
1341 static const char name[] = "Weston WM";
1342
1343 wm->wm_window = xcb_generate_id(wm->conn);
1344 xcb_create_window(wm->conn,
1345 XCB_COPY_FROM_PARENT,
1346 wm->wm_window,
1347 wm->screen->root,
1348 0, 0,
1349 10, 10,
1350 0,
1351 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1352 wm->screen->root_visual,
1353 0, NULL);
1354
1355 xcb_change_property(wm->conn,
1356 XCB_PROP_MODE_REPLACE,
1357 wm->wm_window,
1358 wm->atom.net_supporting_wm_check,
1359 XCB_ATOM_WINDOW,
1360 32, /* format */
1361 1, &wm->wm_window);
1362
1363 xcb_change_property(wm->conn,
1364 XCB_PROP_MODE_REPLACE,
1365 wm->wm_window,
1366 wm->atom.net_wm_name,
1367 wm->atom.utf8_string,
1368 8, /* format */
1369 strlen(name), name);
1370
1371 xcb_change_property(wm->conn,
1372 XCB_PROP_MODE_REPLACE,
1373 wm->screen->root,
1374 wm->atom.net_supporting_wm_check,
1375 XCB_ATOM_WINDOW,
1376 32, /* format */
1377 1, &wm->wm_window);
1378
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001379 /* Claim the WM_S0 selection even though we don't suport
1380 * the --replace functionality. */
1381 xcb_set_selection_owner(wm->conn,
1382 wm->wm_window,
1383 wm->atom.wm_s0,
1384 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001385}
1386
1387struct weston_wm *
1388weston_wm_create(struct weston_xserver *wxs)
1389{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001390 struct weston_wm *wm;
1391 struct wl_event_loop *loop;
1392 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001393 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001394 int sv[2];
1395 xcb_atom_t supported[1];
1396
1397 wm = malloc(sizeof *wm);
1398 if (wm == NULL)
1399 return NULL;
1400
1401 memset(wm, 0, sizeof *wm);
1402 wm->server = wxs;
1403 wm->window_hash = hash_table_create();
1404 if (wm->window_hash == NULL) {
1405 free(wm);
1406 return NULL;
1407 }
1408
1409 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001410 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001411 hash_table_destroy(wm->window_hash);
1412 free(wm);
1413 return NULL;
1414 }
1415
1416 xserver_send_client(wxs->resource, sv[1]);
1417 wl_client_flush(wxs->resource->client);
1418 close(sv[1]);
1419
1420 /* xcb_connect_to_fd takes ownership of the fd. */
1421 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1422 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001423 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001424 close(sv[0]);
1425 hash_table_destroy(wm->window_hash);
1426 free(wm);
1427 return NULL;
1428 }
1429
1430 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1431 wm->screen = s.data;
1432
1433 loop = wl_display_get_event_loop(wxs->wl_display);
1434 wm->source =
1435 wl_event_loop_add_fd(loop, sv[0],
1436 WL_EVENT_READABLE,
1437 weston_wm_handle_event, wm);
1438 wl_event_source_check(wm->source);
1439
1440 wxs_wm_get_resources(wm);
1441
1442 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001443 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1444 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1445 XCB_EVENT_MASK_PROPERTY_CHANGE;
1446 xcb_change_window_attributes(wm->conn, wm->screen->root,
1447 XCB_CW_EVENT_MASK, values);
1448 wm->theme = theme_create();
1449
1450 weston_wm_create_wm_window(wm);
1451
1452 supported[0] = wm->atom.net_wm_moveresize;
1453 xcb_change_property(wm->conn,
1454 XCB_PROP_MODE_REPLACE,
1455 wm->screen->root,
1456 wm->atom.net_supported,
1457 XCB_ATOM_ATOM,
1458 32, /* format */
1459 ARRAY_LENGTH(supported), supported);
1460
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001461 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001462
1463 xcb_flush(wm->conn);
1464
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001465 wm->activate_listener.notify = weston_wm_window_activate;
1466 wl_signal_add(&wxs->compositor->activate_signal,
1467 &wm->activate_listener);
1468
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001469 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001470 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001471
Martin Minarik6d118362012-06-07 18:01:59 +02001472 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001473
1474 return wm;
1475}
1476
1477void
1478weston_wm_destroy(struct weston_wm *wm)
1479{
1480 /* FIXME: Free windows in hash. */
1481 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001482 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001483 xcb_disconnect(wm->conn);
1484 wl_event_source_remove(wm->source);
1485 wl_list_remove(&wm->selection_listener.link);
1486 wl_list_remove(&wm->activate_listener.link);
1487
1488 free(wm);
1489}
1490
1491static void
1492surface_destroy(struct wl_listener *listener, void *data)
1493{
1494 struct weston_wm_window *window =
1495 container_of(listener,
1496 struct weston_wm_window, surface_destroy_listener);
1497
Martin Minarik6d118362012-06-07 18:01:59 +02001498 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001499}
1500
1501static struct weston_wm_window *
1502get_wm_window(struct weston_surface *surface)
1503{
1504 struct wl_resource *resource = &surface->surface.resource;
1505 struct wl_listener *listener;
1506
1507 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1508 if (listener)
1509 return container_of(listener, struct weston_wm_window,
1510 surface_destroy_listener);
1511
1512 return NULL;
1513}
1514
1515static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001516weston_wm_window_configure(void *data)
1517{
1518 struct weston_wm_window *window = data;
1519 struct weston_wm *wm = window->wm;
1520 uint32_t values[2];
1521 int width, height;
1522
1523 values[0] = window->width;
1524 values[1] = window->height;
1525 xcb_configure_window(wm->conn,
1526 window->id,
1527 XCB_CONFIG_WINDOW_WIDTH |
1528 XCB_CONFIG_WINDOW_HEIGHT,
1529 values);
1530
1531 weston_wm_window_get_frame_size(window, &width, &height);
1532 values[0] = width;
1533 values[1] = height;
1534 xcb_configure_window(wm->conn,
1535 window->frame_id,
1536 XCB_CONFIG_WINDOW_WIDTH |
1537 XCB_CONFIG_WINDOW_HEIGHT,
1538 values);
1539
1540 window->configure_source = NULL;
1541
1542 weston_wm_window_schedule_repaint(window);
1543}
1544
1545static void
1546send_configure(struct weston_surface *surface,
1547 uint32_t edges, int32_t width, int32_t height)
1548{
1549 struct weston_wm_window *window = get_wm_window(surface);
1550 struct weston_wm *wm = window->wm;
1551 struct theme *t = window->wm->theme;
1552
1553 if (window->decorate) {
1554 window->width = width - 2 * (t->margin + t->width);
1555 window->height = height - 2 * t->margin -
1556 t->titlebar_height - t->width;
1557 } else {
1558 window->width = width - 2 * t->margin;
1559 window->height = height - 2 * t->margin;
1560 }
1561
1562 if (window->configure_source)
1563 return;
1564
1565 window->configure_source =
1566 wl_event_loop_add_idle(wm->server->loop,
1567 weston_wm_window_configure, window);
1568}
1569
1570static const struct weston_shell_client shell_client = {
1571 send_configure
1572};
1573
1574static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001575xserver_map_shell_surface(struct weston_wm *wm,
1576 struct weston_wm_window *window)
1577{
1578 struct weston_shell_interface *shell_interface =
1579 &wm->server->compositor->shell_interface;
1580 struct weston_wm_window *parent;
1581 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001582 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001583
1584 if (!shell_interface->create_shell_surface)
1585 return;
1586
1587 window->shsurf =
1588 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001589 window->surface,
1590 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001591
Tiago Vignatti771241e2012-06-04 20:01:45 +03001592 /* ICCCM 4.1.1 */
Tiago Vignattice1baa82012-07-20 23:09:55 +03001593 if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001594 shell_interface->set_toplevel(window->shsurf);
1595 return;
1596 }
1597
Tiago Vignattice1baa82012-07-20 23:09:55 +03001598 /* not all non-toplevel has transient_for set. So we need this
1599 * workaround to guess a parent that will determine the relative
1600 * position of the transient surface */
1601 if (!window->transient_for)
1602 parent_id = wm->focus_latest->id;
1603 else
1604 parent_id = window->transient_for->id;
1605
1606 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001607
1608 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1609 if (!parent->decorate && parent->override_redirect) {
1610 x = parent->x + t->margin;
1611 y = parent->y + t->margin;
1612 }
1613
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001614 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001615 window->x + t->margin - x,
1616 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001617 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1618}
1619
1620static void
1621xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1622 struct wl_resource *surface_resource, uint32_t id)
1623{
1624 struct weston_xserver *wxs = resource->data;
1625 struct weston_wm *wm = wxs->wm;
1626 struct wl_surface *surface = surface_resource->data;
1627 struct weston_wm_window *window;
1628
1629 if (client != wxs->client)
1630 return;
1631
1632 window = hash_table_lookup(wm->window_hash, id);
1633 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001634 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001635 return;
1636 }
1637
Martin Minarik6d118362012-06-07 18:01:59 +02001638 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001639
1640 weston_wm_window_read_properties(window);
1641
1642 window->surface = (struct weston_surface *) surface;
1643 window->surface_destroy_listener.notify = surface_destroy;
1644 wl_signal_add(&surface->resource.destroy_signal,
1645 &window->surface_destroy_listener);
1646
1647 weston_wm_window_schedule_repaint(window);
1648 xserver_map_shell_surface(wm, window);
1649}
1650
1651const struct xserver_interface xserver_implementation = {
1652 xserver_set_window_id
1653};