blob: d724d47071a56600cae653333edcaa631875e0e5 [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
882static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400883weston_wm_window_handle_moveresize(struct weston_wm_window *window,
884 xcb_client_message_event_t *client_message)
885{
886 static const int map[] = {
887 THEME_LOCATION_RESIZING_TOP_LEFT,
888 THEME_LOCATION_RESIZING_TOP,
889 THEME_LOCATION_RESIZING_TOP_RIGHT,
890 THEME_LOCATION_RESIZING_RIGHT,
891 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
892 THEME_LOCATION_RESIZING_BOTTOM,
893 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
894 THEME_LOCATION_RESIZING_LEFT
895 };
896
897 struct weston_wm *wm = window->wm;
898 struct weston_seat *seat = wm->server->compositor->seat;
899 int detail;
900 struct weston_shell_interface *shell_interface =
901 &wm->server->compositor->shell_interface;
902
903 if (seat->seat.pointer->button_count != 1 ||
904 seat->seat.pointer->focus != &window->surface->surface)
905 return;
906
907 detail = client_message->data.data32[2];
908 switch (detail) {
909 case _NET_WM_MOVERESIZE_MOVE:
910 shell_interface->move(window->shsurf, seat);
911 break;
912 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
913 case _NET_WM_MOVERESIZE_SIZE_TOP:
914 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
915 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
916 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
917 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
918 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
919 case _NET_WM_MOVERESIZE_SIZE_LEFT:
920 shell_interface->resize(window->shsurf, seat, map[detail]);
921 break;
922 case _NET_WM_MOVERESIZE_CANCEL:
923 break;
924 }
925}
926
927static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400928weston_wm_handle_client_message(struct weston_wm *wm,
929 xcb_generic_event_t *event)
930{
931 xcb_client_message_event_t *client_message =
932 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400933 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400934
935 window = hash_table_lookup(wm->window_hash, client_message->window);
936
Martin Minarik6d118362012-06-07 18:01:59 +0200937 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400938 get_atom_name(wm->conn, client_message->type),
939 client_message->data.data32[0],
940 client_message->data.data32[1],
941 client_message->data.data32[2],
942 client_message->data.data32[3],
943 client_message->data.data32[4]);
944
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400945 if (client_message->type == wm->atom.net_wm_moveresize)
946 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947}
948
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400949enum cursor_type {
950 XWM_CURSOR_TOP,
951 XWM_CURSOR_BOTTOM,
952 XWM_CURSOR_LEFT,
953 XWM_CURSOR_RIGHT,
954 XWM_CURSOR_TOP_LEFT,
955 XWM_CURSOR_TOP_RIGHT,
956 XWM_CURSOR_BOTTOM_LEFT,
957 XWM_CURSOR_BOTTOM_RIGHT,
958 XWM_CURSOR_LEFT_PTR,
959};
960
961static const char *cursors[] = {
962 "top_side",
963 "bottom_side",
964 "left_side",
965 "right_side",
966 "top_left_corner",
967 "top_right_corner",
968 "bottom_left_corner",
969 "bottom_right_corner",
970 "left_ptr"
971};
972
973static void
974weston_wm_create_cursors(struct weston_wm *wm)
975{
976 int i, count = ARRAY_LENGTH(cursors);
977
978 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
979 for (i = 0; i < count; i++) {
980 wm->cursors[i] =
981 xcb_cursor_library_load_cursor(wm, cursors[i]);
982 }
983
984 wm->last_cursor = -1;
985}
986
987static void
988weston_wm_destroy_cursors(struct weston_wm *wm)
989{
990 uint8_t i;
991
992 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
993 xcb_free_cursor(wm->conn, wm->cursors[i]);
994
995 free(wm->cursors);
996}
997
998static int
999get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1000{
1001 int location = theme_get_location(t, x, y, width, height);
1002
1003 switch (location) {
1004 case THEME_LOCATION_RESIZING_TOP:
1005 return XWM_CURSOR_TOP;
1006 case THEME_LOCATION_RESIZING_BOTTOM:
1007 return XWM_CURSOR_BOTTOM;
1008 case THEME_LOCATION_RESIZING_LEFT:
1009 return XWM_CURSOR_LEFT;
1010 case THEME_LOCATION_RESIZING_RIGHT:
1011 return XWM_CURSOR_RIGHT;
1012 case THEME_LOCATION_RESIZING_TOP_LEFT:
1013 return XWM_CURSOR_TOP_LEFT;
1014 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1015 return XWM_CURSOR_TOP_RIGHT;
1016 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1017 return XWM_CURSOR_BOTTOM_LEFT;
1018 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1019 return XWM_CURSOR_BOTTOM_RIGHT;
1020 case THEME_LOCATION_EXTERIOR:
1021 case THEME_LOCATION_TITLEBAR:
1022 default:
1023 return XWM_CURSOR_LEFT_PTR;
1024 }
1025}
1026
1027static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001028weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1029 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001030{
1031 uint32_t cursor_value_list;
1032
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001033 if (wm->last_cursor == cursor)
1034 return;
1035
1036 wm->last_cursor = cursor;
1037
1038 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001039 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001040 XCB_CW_CURSOR, &cursor_value_list);
1041 xcb_flush(wm->conn);
1042}
1043
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001044static void
1045weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1046{
1047 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1048 struct weston_shell_interface *shell_interface =
1049 &wm->server->compositor->shell_interface;
1050 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001051 enum theme_location location;
1052 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001053 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001054
Martin Minarik6d118362012-06-07 18:01:59 +02001055 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001056 button->response_type == XCB_BUTTON_PRESS ?
1057 "PRESS" : "RELEASE", button->detail);
1058
1059 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001060 weston_wm_window_get_frame_size(window, &width, &height);
1061
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001063 button->detail == 1) {
1064 location = theme_get_location(t,
1065 button->event_x,
1066 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001067 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001068
1069 switch (location) {
1070 case THEME_LOCATION_TITLEBAR:
1071 shell_interface->move(window->shsurf,
1072 wm->server->compositor->seat);
1073 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001074 case THEME_LOCATION_RESIZING_TOP:
1075 case THEME_LOCATION_RESIZING_BOTTOM:
1076 case THEME_LOCATION_RESIZING_LEFT:
1077 case THEME_LOCATION_RESIZING_RIGHT:
1078 case THEME_LOCATION_RESIZING_TOP_LEFT:
1079 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1080 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1081 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1082 shell_interface->resize(window->shsurf,
1083 wm->server->compositor->seat,
1084 location);
1085 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001086 default:
1087 break;
1088 }
1089 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001090}
1091
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001092static void
1093weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1094{
1095 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1096 struct weston_wm_window *window;
1097 int cursor, width, height;
1098
1099 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001100 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001101 return;
1102
1103 weston_wm_window_get_frame_size(window, &width, &height);
1104 cursor = get_cursor_for_location(wm->theme, width, height,
1105 motion->event_x, motion->event_y);
1106
Tiago Vignattic1903232012-07-16 12:15:37 -04001107 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001108}
1109
1110static void
1111weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1112{
1113 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1114 struct weston_wm_window *window;
1115 int cursor, width, height;
1116
1117 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001118 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001119 return;
1120
1121 weston_wm_window_get_frame_size(window, &width, &height);
1122 cursor = get_cursor_for_location(wm->theme, width, height,
1123 enter->event_x, enter->event_y);
1124
Tiago Vignattic1903232012-07-16 12:15:37 -04001125 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001126}
1127
1128static void
1129weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1130{
1131 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1132 struct weston_wm_window *window;
1133
1134 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001135 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001136 return;
1137
Tiago Vignattic1903232012-07-16 12:15:37 -04001138 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001139}
1140
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001141static int
1142weston_wm_handle_event(int fd, uint32_t mask, void *data)
1143{
1144 struct weston_wm *wm = data;
1145 xcb_generic_event_t *event;
1146 int count = 0;
1147
1148 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1149 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001150 free(event);
1151 count++;
1152 continue;
1153 }
1154
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001155 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001156 case XCB_BUTTON_PRESS:
1157 case XCB_BUTTON_RELEASE:
1158 weston_wm_handle_button(wm, event);
1159 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001160 case XCB_ENTER_NOTIFY:
1161 weston_wm_handle_enter(wm, event);
1162 break;
1163 case XCB_LEAVE_NOTIFY:
1164 weston_wm_handle_leave(wm, event);
1165 break;
1166 case XCB_MOTION_NOTIFY:
1167 weston_wm_handle_motion(wm, event);
1168 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001169 case XCB_CREATE_NOTIFY:
1170 weston_wm_handle_create_notify(wm, event);
1171 break;
1172 case XCB_MAP_REQUEST:
1173 weston_wm_handle_map_request(wm, event);
1174 break;
1175 case XCB_MAP_NOTIFY:
1176 weston_wm_handle_map_notify(wm, event);
1177 break;
1178 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001179 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001180 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001181 case XCB_REPARENT_NOTIFY:
1182 weston_wm_handle_reparent_notify(wm, event);
1183 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001184 case XCB_CONFIGURE_REQUEST:
1185 weston_wm_handle_configure_request(wm, event);
1186 break;
1187 case XCB_CONFIGURE_NOTIFY:
1188 weston_wm_handle_configure_notify(wm, event);
1189 break;
1190 case XCB_DESTROY_NOTIFY:
1191 weston_wm_handle_destroy_notify(wm, event);
1192 break;
1193 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001194 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001195 break;
1196 case XCB_PROPERTY_NOTIFY:
1197 weston_wm_handle_property_notify(wm, event);
1198 break;
1199 case XCB_CLIENT_MESSAGE:
1200 weston_wm_handle_client_message(wm, event);
1201 break;
1202 }
1203
1204 free(event);
1205 count++;
1206 }
1207
1208 xcb_flush(wm->conn);
1209
1210 return count;
1211}
1212
1213static void
1214wxs_wm_get_resources(struct weston_wm *wm)
1215{
1216
1217#define F(field) offsetof(struct weston_wm, field)
1218
1219 static const struct { const char *name; int offset; } atoms[] = {
1220 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1221 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1222 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001223 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001224 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001225 { "_NET_WM_NAME", F(atom.net_wm_name) },
1226 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1227 { "_NET_WM_STATE", F(atom.net_wm_state) },
1228 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1229 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1230 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1231 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1232
1233 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1234 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1235 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1236 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1237 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1238 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1239 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001240 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1241 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001242 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1243 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1244 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1245 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1246 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1247
1248 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1249 { "_NET_SUPPORTING_WM_CHECK",
1250 F(atom.net_supporting_wm_check) },
1251 { "_NET_SUPPORTED", F(atom.net_supported) },
1252 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1253 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001254 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001255 { "TARGETS", F(atom.targets) },
1256 { "UTF8_STRING", F(atom.utf8_string) },
1257 { "_WL_SELECTION", F(atom.wl_selection) },
1258 { "INCR", F(atom.incr) },
1259 { "TIMESTAMP", F(atom.timestamp) },
1260 { "MULTIPLE", F(atom.multiple) },
1261 { "UTF8_STRING" , F(atom.utf8_string) },
1262 { "COMPOUND_TEXT", F(atom.compound_text) },
1263 { "TEXT", F(atom.text) },
1264 { "STRING", F(atom.string) },
1265 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1266 { "text/plain", F(atom.text_plain) },
1267 };
1268#undef F
1269
1270 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1271 xcb_xfixes_query_version_reply_t *xfixes_reply;
1272 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1273 xcb_intern_atom_reply_t *reply;
1274 xcb_render_query_pict_formats_reply_t *formats_reply;
1275 xcb_render_query_pict_formats_cookie_t formats_cookie;
1276 xcb_render_pictforminfo_t *formats;
1277 uint32_t i;
1278
1279 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1280
1281 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1282
1283 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1284 cookies[i] = xcb_intern_atom (wm->conn, 0,
1285 strlen(atoms[i].name),
1286 atoms[i].name);
1287
1288 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1289 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1290 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1291 free(reply);
1292 }
1293
1294 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1295 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001296 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001297
1298 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1299 XCB_XFIXES_MAJOR_VERSION,
1300 XCB_XFIXES_MINOR_VERSION);
1301 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1302 xfixes_cookie, NULL);
1303
Martin Minarik6d118362012-06-07 18:01:59 +02001304 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001305 xfixes_reply->major_version, xfixes_reply->minor_version);
1306
1307 free(xfixes_reply);
1308
1309 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1310 formats_cookie, 0);
1311 if (formats_reply == NULL)
1312 return;
1313
1314 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001315 for (i = 0; i < formats_reply->num_formats; i++) {
1316 if (formats[i].direct.red_mask != 0xff &&
1317 formats[i].direct.red_shift != 16)
1318 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001319 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1320 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001321 wm->format_rgb = formats[i];
1322 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1323 formats[i].depth == 32 &&
1324 formats[i].direct.alpha_mask == 0xff &&
1325 formats[i].direct.alpha_shift == 24)
1326 wm->format_rgba = formats[i];
1327 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001328
1329 free(formats_reply);
1330}
1331
1332static void
1333weston_wm_create_wm_window(struct weston_wm *wm)
1334{
1335 static const char name[] = "Weston WM";
1336
1337 wm->wm_window = xcb_generate_id(wm->conn);
1338 xcb_create_window(wm->conn,
1339 XCB_COPY_FROM_PARENT,
1340 wm->wm_window,
1341 wm->screen->root,
1342 0, 0,
1343 10, 10,
1344 0,
1345 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1346 wm->screen->root_visual,
1347 0, NULL);
1348
1349 xcb_change_property(wm->conn,
1350 XCB_PROP_MODE_REPLACE,
1351 wm->wm_window,
1352 wm->atom.net_supporting_wm_check,
1353 XCB_ATOM_WINDOW,
1354 32, /* format */
1355 1, &wm->wm_window);
1356
1357 xcb_change_property(wm->conn,
1358 XCB_PROP_MODE_REPLACE,
1359 wm->wm_window,
1360 wm->atom.net_wm_name,
1361 wm->atom.utf8_string,
1362 8, /* format */
1363 strlen(name), name);
1364
1365 xcb_change_property(wm->conn,
1366 XCB_PROP_MODE_REPLACE,
1367 wm->screen->root,
1368 wm->atom.net_supporting_wm_check,
1369 XCB_ATOM_WINDOW,
1370 32, /* format */
1371 1, &wm->wm_window);
1372
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001373 /* Claim the WM_S0 selection even though we don't suport
1374 * the --replace functionality. */
1375 xcb_set_selection_owner(wm->conn,
1376 wm->wm_window,
1377 wm->atom.wm_s0,
1378 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001379}
1380
1381struct weston_wm *
1382weston_wm_create(struct weston_xserver *wxs)
1383{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001384 struct weston_wm *wm;
1385 struct wl_event_loop *loop;
1386 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001387 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001388 int sv[2];
1389 xcb_atom_t supported[1];
1390
1391 wm = malloc(sizeof *wm);
1392 if (wm == NULL)
1393 return NULL;
1394
1395 memset(wm, 0, sizeof *wm);
1396 wm->server = wxs;
1397 wm->window_hash = hash_table_create();
1398 if (wm->window_hash == NULL) {
1399 free(wm);
1400 return NULL;
1401 }
1402
1403 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001404 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001405 hash_table_destroy(wm->window_hash);
1406 free(wm);
1407 return NULL;
1408 }
1409
1410 xserver_send_client(wxs->resource, sv[1]);
1411 wl_client_flush(wxs->resource->client);
1412 close(sv[1]);
1413
1414 /* xcb_connect_to_fd takes ownership of the fd. */
1415 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1416 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001417 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001418 close(sv[0]);
1419 hash_table_destroy(wm->window_hash);
1420 free(wm);
1421 return NULL;
1422 }
1423
1424 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1425 wm->screen = s.data;
1426
1427 loop = wl_display_get_event_loop(wxs->wl_display);
1428 wm->source =
1429 wl_event_loop_add_fd(loop, sv[0],
1430 WL_EVENT_READABLE,
1431 weston_wm_handle_event, wm);
1432 wl_event_source_check(wm->source);
1433
1434 wxs_wm_get_resources(wm);
1435
1436 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001437 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1438 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1439 XCB_EVENT_MASK_PROPERTY_CHANGE;
1440 xcb_change_window_attributes(wm->conn, wm->screen->root,
1441 XCB_CW_EVENT_MASK, values);
1442 wm->theme = theme_create();
1443
1444 weston_wm_create_wm_window(wm);
1445
1446 supported[0] = wm->atom.net_wm_moveresize;
1447 xcb_change_property(wm->conn,
1448 XCB_PROP_MODE_REPLACE,
1449 wm->screen->root,
1450 wm->atom.net_supported,
1451 XCB_ATOM_ATOM,
1452 32, /* format */
1453 ARRAY_LENGTH(supported), supported);
1454
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001455 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001456
1457 xcb_flush(wm->conn);
1458
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001459 wm->activate_listener.notify = weston_wm_window_activate;
1460 wl_signal_add(&wxs->compositor->activate_signal,
1461 &wm->activate_listener);
1462
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001463 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001464 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001465
Martin Minarik6d118362012-06-07 18:01:59 +02001466 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001467
1468 return wm;
1469}
1470
1471void
1472weston_wm_destroy(struct weston_wm *wm)
1473{
1474 /* FIXME: Free windows in hash. */
1475 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001476 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001477 xcb_disconnect(wm->conn);
1478 wl_event_source_remove(wm->source);
1479 wl_list_remove(&wm->selection_listener.link);
1480 wl_list_remove(&wm->activate_listener.link);
1481
1482 free(wm);
1483}
1484
1485static void
1486surface_destroy(struct wl_listener *listener, void *data)
1487{
1488 struct weston_wm_window *window =
1489 container_of(listener,
1490 struct weston_wm_window, surface_destroy_listener);
1491
Martin Minarik6d118362012-06-07 18:01:59 +02001492 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001493}
1494
1495static struct weston_wm_window *
1496get_wm_window(struct weston_surface *surface)
1497{
1498 struct wl_resource *resource = &surface->surface.resource;
1499 struct wl_listener *listener;
1500
1501 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1502 if (listener)
1503 return container_of(listener, struct weston_wm_window,
1504 surface_destroy_listener);
1505
1506 return NULL;
1507}
1508
1509static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001510weston_wm_window_configure(void *data)
1511{
1512 struct weston_wm_window *window = data;
1513 struct weston_wm *wm = window->wm;
1514 uint32_t values[2];
1515 int width, height;
1516
1517 values[0] = window->width;
1518 values[1] = window->height;
1519 xcb_configure_window(wm->conn,
1520 window->id,
1521 XCB_CONFIG_WINDOW_WIDTH |
1522 XCB_CONFIG_WINDOW_HEIGHT,
1523 values);
1524
1525 weston_wm_window_get_frame_size(window, &width, &height);
1526 values[0] = width;
1527 values[1] = height;
1528 xcb_configure_window(wm->conn,
1529 window->frame_id,
1530 XCB_CONFIG_WINDOW_WIDTH |
1531 XCB_CONFIG_WINDOW_HEIGHT,
1532 values);
1533
1534 window->configure_source = NULL;
1535
1536 weston_wm_window_schedule_repaint(window);
1537}
1538
1539static void
1540send_configure(struct weston_surface *surface,
1541 uint32_t edges, int32_t width, int32_t height)
1542{
1543 struct weston_wm_window *window = get_wm_window(surface);
1544 struct weston_wm *wm = window->wm;
1545 struct theme *t = window->wm->theme;
1546
1547 if (window->decorate) {
1548 window->width = width - 2 * (t->margin + t->width);
1549 window->height = height - 2 * t->margin -
1550 t->titlebar_height - t->width;
1551 } else {
1552 window->width = width - 2 * t->margin;
1553 window->height = height - 2 * t->margin;
1554 }
1555
1556 if (window->configure_source)
1557 return;
1558
1559 window->configure_source =
1560 wl_event_loop_add_idle(wm->server->loop,
1561 weston_wm_window_configure, window);
1562}
1563
1564static const struct weston_shell_client shell_client = {
1565 send_configure
1566};
1567
1568static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001569xserver_map_shell_surface(struct weston_wm *wm,
1570 struct weston_wm_window *window)
1571{
1572 struct weston_shell_interface *shell_interface =
1573 &wm->server->compositor->shell_interface;
1574 struct weston_wm_window *parent;
1575 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001576 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001577
1578 if (!shell_interface->create_shell_surface)
1579 return;
1580
1581 window->shsurf =
1582 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001583 window->surface,
1584 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001585
Tiago Vignatti771241e2012-06-04 20:01:45 +03001586 /* ICCCM 4.1.1 */
Tiago Vignattice1baa82012-07-20 23:09:55 +03001587 if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001588 shell_interface->set_toplevel(window->shsurf);
1589 return;
1590 }
1591
Tiago Vignattice1baa82012-07-20 23:09:55 +03001592 /* not all non-toplevel has transient_for set. So we need this
1593 * workaround to guess a parent that will determine the relative
1594 * position of the transient surface */
1595 if (!window->transient_for)
1596 parent_id = wm->focus_latest->id;
1597 else
1598 parent_id = window->transient_for->id;
1599
1600 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001601
1602 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1603 if (!parent->decorate && parent->override_redirect) {
1604 x = parent->x + t->margin;
1605 y = parent->y + t->margin;
1606 }
1607
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001608 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001609 window->x + t->margin - x,
1610 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001611 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1612}
1613
1614static void
1615xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1616 struct wl_resource *surface_resource, uint32_t id)
1617{
1618 struct weston_xserver *wxs = resource->data;
1619 struct weston_wm *wm = wxs->wm;
1620 struct wl_surface *surface = surface_resource->data;
1621 struct weston_wm_window *window;
1622
1623 if (client != wxs->client)
1624 return;
1625
1626 window = hash_table_lookup(wm->window_hash, id);
1627 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001628 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001629 return;
1630 }
1631
Martin Minarik6d118362012-06-07 18:01:59 +02001632 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001633
1634 weston_wm_window_read_properties(window);
1635
1636 window->surface = (struct weston_surface *) surface;
1637 window->surface_destroy_listener.notify = surface_destroy;
1638 wl_signal_add(&surface->resource.destroy_signal,
1639 &window->surface_destroy_listener);
1640
1641 weston_wm_window_schedule_repaint(window);
1642 xserver_map_shell_surface(wm, window);
1643}
1644
1645const struct xserver_interface xserver_implementation = {
1646 xserver_set_window_id
1647};