blob: e67cac182c89b250be5b8f8a7715e283bbe3f7c0 [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"
Martin Minarik6d118362012-06-07 18:01:59 +020040#include "../log.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041#include "xserver-server-protocol.h"
42#include "hash.h"
43
44struct motif_wm_hints {
45 uint32_t flags;
46 uint32_t functions;
47 uint32_t decorations;
48 int32_t input_mode;
49 uint32_t status;
50};
51
52#define MWM_HINTS_FUNCTIONS (1L << 0)
53#define MWM_HINTS_DECORATIONS (1L << 1)
54#define MWM_HINTS_INPUT_MODE (1L << 2)
55#define MWM_HINTS_STATUS (1L << 3)
56
57#define MWM_FUNC_ALL (1L << 0)
58#define MWM_FUNC_RESIZE (1L << 1)
59#define MWM_FUNC_MOVE (1L << 2)
60#define MWM_FUNC_MINIMIZE (1L << 3)
61#define MWM_FUNC_MAXIMIZE (1L << 4)
62#define MWM_FUNC_CLOSE (1L << 5)
63
64#define MWM_DECOR_ALL (1L << 0)
65#define MWM_DECOR_BORDER (1L << 1)
66#define MWM_DECOR_RESIZEH (1L << 2)
67#define MWM_DECOR_TITLE (1L << 3)
68#define MWM_DECOR_MENU (1L << 4)
69#define MWM_DECOR_MINIMIZE (1L << 5)
70#define MWM_DECOR_MAXIMIZE (1L << 6)
71
72#define MWM_INPUT_MODELESS 0
73#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
74#define MWM_INPUT_SYSTEM_MODAL 2
75#define MWM_INPUT_FULL_APPLICATION_MODAL 3
76#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
77
78#define MWM_TEAROFF_WINDOW (1L<<0)
79
80#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
81#define _NET_WM_MOVERESIZE_SIZE_TOP 1
82#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
83#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
84#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
85#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
86#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
87#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
88#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
89#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
90#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
91#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
92
93
94
95struct weston_wm_window {
96 struct weston_wm *wm;
97 xcb_window_t id;
98 xcb_window_t frame_id;
99 cairo_surface_t *cairo_surface;
100 struct weston_surface *surface;
101 struct shell_surface *shsurf;
102 struct wl_listener surface_destroy_listener;
103 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400104 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105 int properties_dirty;
106 char *class;
107 char *name;
108 struct weston_wm_window *transient_for;
109 uint32_t protocols;
110 xcb_atom_t type;
111 int width, height;
112 int x, y;
113 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300114 int override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400115};
116
117static struct weston_wm_window *
118get_wm_window(struct weston_surface *surface);
119
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400120static void
121weston_wm_window_schedule_repaint(struct weston_wm_window *window);
122
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400123const char *
124get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
125{
126 xcb_get_atom_name_cookie_t cookie;
127 xcb_get_atom_name_reply_t *reply;
128 xcb_generic_error_t *e;
129 static char buffer[64];
130
131 if (atom == XCB_ATOM_NONE)
132 return "None";
133
134 cookie = xcb_get_atom_name (c, atom);
135 reply = xcb_get_atom_name_reply (c, cookie, &e);
136 snprintf(buffer, sizeof buffer, "%.*s",
137 xcb_get_atom_name_name_length (reply),
138 xcb_get_atom_name_name (reply));
139 free(reply);
140
141 return buffer;
142}
143
Tiago Vignatti90fada42012-07-16 12:02:08 -0400144static xcb_cursor_t
145xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
146{
147 xcb_connection_t *c = wm->conn;
148 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
149 xcb_screen_t *screen = s.data;
150 xcb_gcontext_t gc;
151 xcb_pixmap_t pix;
152 xcb_render_picture_t pic;
153 xcb_cursor_t cursor;
154 int stride = img->width * 4;
155
156 pix = xcb_generate_id(c);
157 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
158
159 pic = xcb_generate_id(c);
160 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
161
162 gc = xcb_generate_id(c);
163 xcb_create_gc(c, gc, pix, 0, 0);
164
165 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
166 img->width, img->height, 0, 0, 0, 32,
167 stride * img->height, (uint8_t *) img->pixels);
168 xcb_free_gc(c, gc);
169
170 cursor = xcb_generate_id(c);
171 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
172
173 xcb_render_free_picture(c, pic);
174 xcb_free_pixmap(c, pix);
175
176 return cursor;
177}
178
179static xcb_cursor_t
180xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
181{
182 /* TODO: treat animated cursors as well */
183 if (images->nimage != 1)
184 return -1;
185
186 return xcb_cursor_image_load_cursor(wm, images->images[0]);
187}
188
189static xcb_cursor_t
190xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
191{
192 xcb_cursor_t cursor;
193 XcursorImages *images;
194 char *v = NULL;
195 int size = 0;
196
197 if (!file)
198 return 0;
199
200 v = getenv ("XCURSOR_SIZE");
201 if (v)
202 size = atoi(v);
203
204 if (!size)
205 size = 32;
206
207 images = XcursorLibraryLoadImages (file, NULL, size);
208 cursor = xcb_cursor_images_load_cursor (wm, images);
209 XcursorImagesDestroy (images);
210
211 return cursor;
212}
213
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400214void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400215dump_property(struct weston_wm *wm,
216 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400217{
218 int32_t *incr_value;
219 const char *text_value, *name;
220 xcb_atom_t *atom_value;
221 int width, len;
222 uint32_t i;
223
Martin Minarik6d118362012-06-07 18:01:59 +0200224 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400225 if (reply == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200226 weston_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400227 return;
228 }
229
Martin Minarik6d118362012-06-07 18:01:59 +0200230 width += weston_log_continue(
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400231 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400232 get_atom_name(wm->conn, reply->type),
233 reply->format,
234 xcb_get_property_value_length(reply),
235 reply->value_len);
236
237 if (reply->type == wm->atom.incr) {
238 incr_value = xcb_get_property_value(reply);
Martin Minarik6d118362012-06-07 18:01:59 +0200239 weston_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400240 } else if (reply->type == wm->atom.utf8_string ||
241 reply->type == wm->atom.string) {
242 text_value = xcb_get_property_value(reply);
243 if (reply->value_len > 40)
244 len = 40;
245 else
246 len = reply->value_len;
Martin Minarik6d118362012-06-07 18:01:59 +0200247 weston_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400248 } else if (reply->type == XCB_ATOM_ATOM) {
249 atom_value = xcb_get_property_value(reply);
250 for (i = 0; i < reply->value_len; i++) {
251 name = get_atom_name(wm->conn, atom_value[i]);
252 if (width + strlen(name) + 2 > 78) {
Martin Minarik6d118362012-06-07 18:01:59 +0200253 weston_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400254 width = 4;
255 } else if (i > 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200256 width += weston_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400257 }
258
Martin Minarik6d118362012-06-07 18:01:59 +0200259 width += weston_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400260 }
Martin Minarik6d118362012-06-07 18:01:59 +0200261 weston_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400262 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200263 weston_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400264 }
265}
266
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400267void
268read_and_dump_property(struct weston_wm *wm,
269 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400270{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400271 xcb_get_property_reply_t *reply;
272 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400273
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400274 cookie = xcb_get_property(wm->conn, 0, window,
275 property, XCB_ATOM_ANY, 0, 2048);
276 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400277
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400278 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400279
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400280 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400281}
282
283/* We reuse some predefined, but otherwise useles atoms */
284#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
285#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
286
287static void
288weston_wm_window_read_properties(struct weston_wm_window *window)
289{
290 struct weston_wm *wm = window->wm;
291
292#define F(field) offsetof(struct weston_wm_window, field)
293 const struct {
294 xcb_atom_t atom;
295 xcb_atom_t type;
296 int offset;
297 } props[] = {
298 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
299 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
300 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
301 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
302 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
303 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
304 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
305 };
306#undef F
307
308 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
309 xcb_get_property_reply_t *reply;
310 void *p;
311 uint32_t *xid;
312 xcb_atom_t *atom;
313 uint32_t i;
314 struct motif_wm_hints *hints;
315
316 if (!window->properties_dirty)
317 return;
318 window->properties_dirty = 0;
319
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320 for (i = 0; i < ARRAY_LENGTH(props); i++)
321 cookie[i] = xcb_get_property(wm->conn,
322 0, /* delete */
323 window->id,
324 props[i].atom,
325 XCB_ATOM_ANY, 0, 2048);
326
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300327 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400328 for (i = 0; i < ARRAY_LENGTH(props); i++) {
329 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
330 if (!reply)
331 /* Bad window, typically */
332 continue;
333 if (reply->type == XCB_ATOM_NONE) {
334 /* No such property */
335 free(reply);
336 continue;
337 }
338
339 p = ((char *) window + props[i].offset);
340
341 switch (props[i].type) {
342 case XCB_ATOM_STRING:
343 /* FIXME: We're using this for both string and
344 utf8_string */
345 if (*(char **) p)
346 free(*(char **) p);
347
348 *(char **) p =
349 strndup(xcb_get_property_value(reply),
350 xcb_get_property_value_length(reply));
351 break;
352 case XCB_ATOM_WINDOW:
353 xid = xcb_get_property_value(reply);
354 *(struct weston_wm_window **) p =
355 hash_table_lookup(wm->window_hash, *xid);
356 break;
357 case XCB_ATOM_ATOM:
358 atom = xcb_get_property_value(reply);
359 *(xcb_atom_t *) p = *atom;
360 break;
361 case TYPE_WM_PROTOCOLS:
362 break;
363 case TYPE_MOTIF_WM_HINTS:
364 hints = xcb_get_property_value(reply);
365 if (hints->flags & MWM_HINTS_DECORATIONS)
366 window->decorate = hints->decorations > 0;
367 break;
368 default:
369 break;
370 }
371 free(reply);
372 }
373}
374
375static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400376weston_wm_window_get_frame_size(struct weston_wm_window *window,
377 int *width, int *height)
378{
379 struct theme *t = window->wm->theme;
380
381 if (window->decorate) {
382 *width = window->width + (t->margin + t->width) * 2;
383 *height = window->height +
384 t->margin * 2 + t->width + t->titlebar_height;
385 } else {
386 *width = window->width + t->margin * 2;
387 *height = window->height + t->margin * 2;
388 }
389}
390
391static void
392weston_wm_window_get_child_position(struct weston_wm_window *window,
393 int *x, int *y)
394{
395 struct theme *t = window->wm->theme;
396
397 if (window->decorate) {
398 *x = t->margin + t->width;
399 *y = t->margin + t->titlebar_height;
400 } else {
401 *x = t->margin;
402 *y = t->margin;
403 }
404}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400405
406static void
407weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
408{
409 xcb_configure_request_event_t *configure_request =
410 (xcb_configure_request_event_t *) event;
411 struct weston_wm_window *window;
412 uint32_t mask, values[16];
413 int x, y, width, height, i = 0;
414
Martin Minarik6d118362012-06-07 18:01:59 +0200415 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400416 configure_request->window,
417 configure_request->x, configure_request->y,
418 configure_request->width, configure_request->height);
419
420 window = hash_table_lookup(wm->window_hash, configure_request->window);
421
422 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
423 window->width = configure_request->width;
424 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
425 window->height = configure_request->height;
426
427 weston_wm_window_get_child_position(window, &x, &y);
428 values[i++] = x;
429 values[i++] = y;
430 values[i++] = window->width;
431 values[i++] = window->height;
432 values[i++] = 0;
433 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
434 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
435 XCB_CONFIG_WINDOW_BORDER_WIDTH;
436 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
437 values[i++] = configure_request->sibling;
438 mask |= XCB_CONFIG_WINDOW_SIBLING;
439 }
440 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
441 values[i++] = configure_request->stack_mode;
442 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
443 }
444
445 xcb_configure_window(wm->conn, window->id, mask, values);
446
447 weston_wm_window_get_frame_size(window, &width, &height);
448 values[0] = width;
449 values[1] = height;
450 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
451 xcb_configure_window(wm->conn, window->frame_id, mask, values);
452
453 weston_wm_window_schedule_repaint(window);
454}
455
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400456static void
457weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
458{
459 xcb_configure_notify_event_t *configure_notify =
460 (xcb_configure_notify_event_t *) event;
461 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300462 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400463
464 window = hash_table_lookup(wm->window_hash, configure_notify->window);
465
Martin Minarik6d118362012-06-07 18:01:59 +0200466 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400467 configure_notify->window == window->id ? "client" : "frame",
468 configure_notify->window,
469 configure_notify->x, configure_notify->y,
470 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300471
472 /* resize falls here */
473 if (configure_notify->window != window->id)
474 return;
475
476 weston_wm_window_get_child_position(window, &x, &y);
477 window->x = configure_notify->x - x;
478 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400479}
480
481static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400482weston_wm_window_activate(struct wl_listener *listener, void *data)
483{
484 struct weston_surface *surface = data;
485 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600486 struct weston_wm *wm =
487 container_of(listener, struct weston_wm, activate_listener);
488 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400489
Scott Moreau85ecac02012-05-21 15:49:14 -0600490 if (window) {
491 client_message.response_type = XCB_CLIENT_MESSAGE;
492 client_message.format = 32;
493 client_message.window = window->id;
494 client_message.type = wm->atom.wm_protocols;
495 client_message.data.data32[0] = wm->atom.wm_take_focus;
496 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
497
498 xcb_send_event(wm->conn, 0, window->id,
499 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
500 (char *) &client_message);
501
502 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
503 window->id, XCB_TIME_CURRENT_TIME);
504 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400505 xcb_set_input_focus (wm->conn,
506 XCB_INPUT_FOCUS_POINTER_ROOT,
507 XCB_NONE,
508 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600509 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400510
511 if (wm->focus_window)
512 weston_wm_window_schedule_repaint(wm->focus_window);
513 wm->focus_window = window;
Tiago Vignattice1baa82012-07-20 23:09:55 +0300514 if (window)
515 wm->focus_latest = window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400516 if (wm->focus_window)
517 weston_wm_window_schedule_repaint(wm->focus_window);
518}
519
520static int
521our_resource(struct weston_wm *wm, uint32_t id)
522{
523 const xcb_setup_t *setup;
524
525 setup = xcb_get_setup(wm->conn);
526
527 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
528}
529
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400530#define ICCCM_WITHDRAWN_STATE 0
531#define ICCCM_NORMAL_STATE 1
532#define ICCCM_ICONIC_STATE 3
533
534static void
535weston_wm_window_set_state(struct weston_wm_window *window, int32_t state)
536{
537 struct weston_wm *wm = window->wm;
538 uint32_t property[2];
539
540 property[0] = state;
541 property[1] = XCB_WINDOW_NONE;
542
543 xcb_change_property(wm->conn,
544 XCB_PROP_MODE_REPLACE,
545 window->id,
546 wm->atom.wm_state,
547 wm->atom.wm_state,
548 32, /* format */
549 2, property);
550}
551
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400552static void
553weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
554{
555 xcb_map_request_event_t *map_request =
556 (xcb_map_request_event_t *) event;
557 struct weston_wm_window *window;
558 uint32_t values[1];
559 int x, y, width, height;
560
561 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200562 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400563 map_request->window);
564 return;
565 }
566
567 window = hash_table_lookup(wm->window_hash, map_request->window);
568
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400569 if (window->frame_id)
570 return;
571
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400572 weston_wm_window_read_properties(window);
573
574 weston_wm_window_get_frame_size(window, &width, &height);
575 weston_wm_window_get_child_position(window, &x, &y);
576
577 values[0] =
578 XCB_EVENT_MASK_KEY_PRESS |
579 XCB_EVENT_MASK_KEY_RELEASE |
580 XCB_EVENT_MASK_BUTTON_PRESS |
581 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400582 XCB_EVENT_MASK_POINTER_MOTION |
583 XCB_EVENT_MASK_ENTER_WINDOW |
584 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400585 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400586 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400587
588 window->frame_id = xcb_generate_id(wm->conn);
589 xcb_create_window(wm->conn,
590 XCB_COPY_FROM_PARENT,
591 window->frame_id,
592 wm->screen->root,
593 0, 0,
594 width, height,
595 0,
596 XCB_WINDOW_CLASS_INPUT_OUTPUT,
597 wm->screen->root_visual,
598 XCB_CW_EVENT_MASK, values);
599 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
600
601 values[0] = 0;
602 xcb_configure_window(wm->conn, window->id,
603 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
604
Martin Minarik6d118362012-06-07 18:01:59 +0200605 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400606 window->id, window, window->frame_id);
607
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400608 xcb_map_window(wm->conn, map_request->window);
609 xcb_map_window(wm->conn, window->frame_id);
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400610 weston_wm_window_set_state(window, ICCCM_NORMAL_STATE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400611
612 window->cairo_surface =
613 cairo_xcb_surface_create_with_xrender_format(wm->conn,
614 wm->screen,
615 window->frame_id,
Kristian Høgsberge89cef32012-07-16 11:57:08 -0400616 &wm->format_rgb,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400617 width, height);
618
619 hash_table_insert(wm->window_hash, window->frame_id, window);
620}
621
622static void
623weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
624{
625 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
626
627 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200628 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400629 map_notify->window);
630 return;
631 }
632
Martin Minarik6d118362012-06-07 18:01:59 +0200633 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400634}
635
636static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400637weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
638{
639 xcb_unmap_notify_event_t *unmap_notify =
640 (xcb_unmap_notify_event_t *) event;
641 struct weston_wm_window *window;
642
Martin Minarik6d118362012-06-07 18:01:59 +0200643 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400644 unmap_notify->window,
645 unmap_notify->event,
646 our_resource(wm, unmap_notify->window) ? ", ours" : "");
647
648 if (our_resource(wm, unmap_notify->window))
649 return;
650
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400651 if (unmap_notify->response_type & 0x80)
652 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
653 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400654 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400655
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400656 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400657 if (window->repaint_source)
658 wl_event_source_remove(window->repaint_source);
659 if (window->cairo_surface)
660 cairo_surface_destroy(window->cairo_surface);
661
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400662 if (window->frame_id) {
663 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
664 xcb_destroy_window(wm->conn, window->frame_id);
665 weston_wm_window_set_state(window, ICCCM_WITHDRAWN_STATE);
666 hash_table_remove(wm->window_hash, window->frame_id);
667 window->frame_id = XCB_WINDOW_NONE;
668 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400669
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400670 if (wm->focus_window == window)
671 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400672 if (window->surface)
673 wl_list_remove(&window->surface_destroy_listener.link);
674 window->surface = NULL;
675}
676
677static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400678weston_wm_window_draw_decoration(void *data)
679{
680 struct weston_wm_window *window = data;
681 struct weston_wm *wm = window->wm;
682 struct theme *t = wm->theme;
683 cairo_t *cr;
684 int x, y, width, height;
685 const char *title;
686 uint32_t flags = 0;
687
688 weston_wm_window_read_properties(window);
689
690 window->repaint_source = NULL;
691
692 weston_wm_window_get_frame_size(window, &width, &height);
693 weston_wm_window_get_child_position(window, &x, &y);
694
695 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
696 cr = cairo_create(window->cairo_surface);
697
698 if (window->decorate) {
699 if (wm->focus_window == window)
700 flags |= THEME_FRAME_ACTIVE;
701
702 if (window->name)
703 title = window->name;
704 else
705 title = "untitled";
706
707 theme_render_frame(t, cr, width, height, title, flags);
708 } else {
709 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
710 cairo_set_source_rgba(cr, 0, 0, 0, 0);
711 cairo_paint(cr);
712
713 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
714 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
715 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
716 }
717
718 cairo_destroy(cr);
719
720 if (window->surface) {
721 /* We leave an extra pixel around the X window area to
722 * make sure we don't sample from the undefined alpha
723 * channel when filtering. */
724 window->surface->opaque_rect[0] =
725 (double) (x - 1) / width;
726 window->surface->opaque_rect[1] =
727 (double) (x + window->width + 1) / width;
728 window->surface->opaque_rect[2] =
729 (double) (y - 1) / height;
730 window->surface->opaque_rect[3] =
731 (double) (y + window->height + 1) / height;
732
733 pixman_region32_init_rect(&window->surface->input,
734 t->margin, t->margin,
735 width - 2 * t->margin,
736 height - 2 * t->margin);
737 }
738}
739
740static void
741weston_wm_window_schedule_repaint(struct weston_wm_window *window)
742{
743 struct weston_wm *wm = window->wm;
744
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400745 if (window->frame_id == XCB_WINDOW_NONE) {
746 if (window->surface != NULL) {
747 window->surface->opaque_rect[0] = 0.0;
748 window->surface->opaque_rect[1] = 1.0;
749 window->surface->opaque_rect[2] = 0.0;
750 window->surface->opaque_rect[3] = 1.0;
751 }
752 return;
753 }
754
755 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400756 return;
757
758 window->repaint_source =
759 wl_event_loop_add_idle(wm->server->loop,
760 weston_wm_window_draw_decoration,
761 window);
762}
763
764static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400765weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
766{
767 xcb_property_notify_event_t *property_notify =
768 (xcb_property_notify_event_t *) event;
769 struct weston_wm_window *window;
770
771 window = hash_table_lookup(wm->window_hash, property_notify->window);
772 if (window)
773 window->properties_dirty = 1;
774
Martin Minarik6d118362012-06-07 18:01:59 +0200775 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400776 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400777 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200778 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400779 else
780 read_and_dump_property(wm, property_notify->window,
781 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400782
783 if (property_notify->atom == wm->atom.net_wm_name ||
784 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400785 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400786}
787
788static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400789weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300790 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400791{
792 struct weston_wm_window *window;
793 uint32_t values[1];
794
795 window = malloc(sizeof *window);
796 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200797 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400798 return;
799 }
800
801 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
802 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
803
804 memset(window, 0, sizeof *window);
805 window->wm = wm;
806 window->id = id;
807 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300808 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400809 window->width = width;
810 window->height = height;
811
812 hash_table_insert(wm->window_hash, id, window);
813}
814
815static void
816weston_wm_window_destroy(struct weston_wm_window *window)
817{
818 hash_table_remove(window->wm->window_hash, window->id);
819 free(window);
820}
821
822static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400823weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
824{
825 xcb_create_notify_event_t *create_notify =
826 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400827
Martin Minarik6d118362012-06-07 18:01:59 +0200828 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400829 create_notify->window,
830 create_notify->width, create_notify->height,
831 create_notify->override_redirect ? ", override" : "",
832 our_resource(wm, create_notify->window) ? ", ours" : "");
833
834 if (our_resource(wm, create_notify->window))
835 return;
836
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400837 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300838 create_notify->width, create_notify->height,
839 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840}
841
842static void
843weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
844{
845 xcb_destroy_notify_event_t *destroy_notify =
846 (xcb_destroy_notify_event_t *) event;
847 struct weston_wm_window *window;
848
Martin Minarik6d118362012-06-07 18:01:59 +0200849 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400850 destroy_notify->window,
851 destroy_notify->event,
852 our_resource(wm, destroy_notify->window) ? ", ours" : "");
853
854 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400855 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400856
857 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400858 weston_wm_window_destroy(window);
859}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400860
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400861static void
862weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
863{
864 xcb_reparent_notify_event_t *reparent_notify =
865 (xcb_reparent_notify_event_t *) event;
866 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400867
Martin Minarik6d118362012-06-07 18:01:59 +0200868 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400869 reparent_notify->window,
870 reparent_notify->parent,
871 reparent_notify->event);
872
873 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300874 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
875 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400876 } else if (!our_resource(wm, reparent_notify->parent)) {
877 window = hash_table_lookup(wm->window_hash,
878 reparent_notify->window);
879 weston_wm_window_destroy(window);
880 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400881}
882
883static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400884weston_wm_window_handle_moveresize(struct weston_wm_window *window,
885 xcb_client_message_event_t *client_message)
886{
887 static const int map[] = {
888 THEME_LOCATION_RESIZING_TOP_LEFT,
889 THEME_LOCATION_RESIZING_TOP,
890 THEME_LOCATION_RESIZING_TOP_RIGHT,
891 THEME_LOCATION_RESIZING_RIGHT,
892 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
893 THEME_LOCATION_RESIZING_BOTTOM,
894 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
895 THEME_LOCATION_RESIZING_LEFT
896 };
897
898 struct weston_wm *wm = window->wm;
899 struct weston_seat *seat = wm->server->compositor->seat;
900 int detail;
901 struct weston_shell_interface *shell_interface =
902 &wm->server->compositor->shell_interface;
903
904 if (seat->seat.pointer->button_count != 1 ||
905 seat->seat.pointer->focus != &window->surface->surface)
906 return;
907
908 detail = client_message->data.data32[2];
909 switch (detail) {
910 case _NET_WM_MOVERESIZE_MOVE:
911 shell_interface->move(window->shsurf, seat);
912 break;
913 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
914 case _NET_WM_MOVERESIZE_SIZE_TOP:
915 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
916 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
917 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
918 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
919 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
920 case _NET_WM_MOVERESIZE_SIZE_LEFT:
921 shell_interface->resize(window->shsurf, seat, map[detail]);
922 break;
923 case _NET_WM_MOVERESIZE_CANCEL:
924 break;
925 }
926}
927
928static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400929weston_wm_handle_client_message(struct weston_wm *wm,
930 xcb_generic_event_t *event)
931{
932 xcb_client_message_event_t *client_message =
933 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400934 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400935
936 window = hash_table_lookup(wm->window_hash, client_message->window);
937
Martin Minarik6d118362012-06-07 18:01:59 +0200938 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400939 get_atom_name(wm->conn, client_message->type),
940 client_message->data.data32[0],
941 client_message->data.data32[1],
942 client_message->data.data32[2],
943 client_message->data.data32[3],
944 client_message->data.data32[4]);
945
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400946 if (client_message->type == wm->atom.net_wm_moveresize)
947 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948}
949
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400950enum cursor_type {
951 XWM_CURSOR_TOP,
952 XWM_CURSOR_BOTTOM,
953 XWM_CURSOR_LEFT,
954 XWM_CURSOR_RIGHT,
955 XWM_CURSOR_TOP_LEFT,
956 XWM_CURSOR_TOP_RIGHT,
957 XWM_CURSOR_BOTTOM_LEFT,
958 XWM_CURSOR_BOTTOM_RIGHT,
959 XWM_CURSOR_LEFT_PTR,
960};
961
962static const char *cursors[] = {
963 "top_side",
964 "bottom_side",
965 "left_side",
966 "right_side",
967 "top_left_corner",
968 "top_right_corner",
969 "bottom_left_corner",
970 "bottom_right_corner",
971 "left_ptr"
972};
973
974static void
975weston_wm_create_cursors(struct weston_wm *wm)
976{
977 int i, count = ARRAY_LENGTH(cursors);
978
979 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
980 for (i = 0; i < count; i++) {
981 wm->cursors[i] =
982 xcb_cursor_library_load_cursor(wm, cursors[i]);
983 }
984
985 wm->last_cursor = -1;
986}
987
988static void
989weston_wm_destroy_cursors(struct weston_wm *wm)
990{
991 uint8_t i;
992
993 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
994 xcb_free_cursor(wm->conn, wm->cursors[i]);
995
996 free(wm->cursors);
997}
998
999static int
1000get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1001{
1002 int location = theme_get_location(t, x, y, width, height);
1003
1004 switch (location) {
1005 case THEME_LOCATION_RESIZING_TOP:
1006 return XWM_CURSOR_TOP;
1007 case THEME_LOCATION_RESIZING_BOTTOM:
1008 return XWM_CURSOR_BOTTOM;
1009 case THEME_LOCATION_RESIZING_LEFT:
1010 return XWM_CURSOR_LEFT;
1011 case THEME_LOCATION_RESIZING_RIGHT:
1012 return XWM_CURSOR_RIGHT;
1013 case THEME_LOCATION_RESIZING_TOP_LEFT:
1014 return XWM_CURSOR_TOP_LEFT;
1015 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1016 return XWM_CURSOR_TOP_RIGHT;
1017 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1018 return XWM_CURSOR_BOTTOM_LEFT;
1019 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1020 return XWM_CURSOR_BOTTOM_RIGHT;
1021 case THEME_LOCATION_EXTERIOR:
1022 case THEME_LOCATION_TITLEBAR:
1023 default:
1024 return XWM_CURSOR_LEFT_PTR;
1025 }
1026}
1027
1028static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001029weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1030 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001031{
1032 uint32_t cursor_value_list;
1033
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001034 if (wm->last_cursor == cursor)
1035 return;
1036
1037 wm->last_cursor = cursor;
1038
1039 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001040 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001041 XCB_CW_CURSOR, &cursor_value_list);
1042 xcb_flush(wm->conn);
1043}
1044
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001045static void
1046weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1047{
1048 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1049 struct weston_shell_interface *shell_interface =
1050 &wm->server->compositor->shell_interface;
1051 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001052 enum theme_location location;
1053 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001054 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001055
Martin Minarik6d118362012-06-07 18:01:59 +02001056 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001057 button->response_type == XCB_BUTTON_PRESS ?
1058 "PRESS" : "RELEASE", button->detail);
1059
1060 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001061 weston_wm_window_get_frame_size(window, &width, &height);
1062
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001064 button->detail == 1) {
1065 location = theme_get_location(t,
1066 button->event_x,
1067 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001068 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001069
1070 switch (location) {
1071 case THEME_LOCATION_TITLEBAR:
1072 shell_interface->move(window->shsurf,
1073 wm->server->compositor->seat);
1074 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001075 case THEME_LOCATION_RESIZING_TOP:
1076 case THEME_LOCATION_RESIZING_BOTTOM:
1077 case THEME_LOCATION_RESIZING_LEFT:
1078 case THEME_LOCATION_RESIZING_RIGHT:
1079 case THEME_LOCATION_RESIZING_TOP_LEFT:
1080 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1081 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1082 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1083 shell_interface->resize(window->shsurf,
1084 wm->server->compositor->seat,
1085 location);
1086 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001087 default:
1088 break;
1089 }
1090 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001091}
1092
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001093static void
1094weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1095{
1096 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1097 struct weston_wm_window *window;
1098 int cursor, width, height;
1099
1100 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001101 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001102 return;
1103
1104 weston_wm_window_get_frame_size(window, &width, &height);
1105 cursor = get_cursor_for_location(wm->theme, width, height,
1106 motion->event_x, motion->event_y);
1107
Tiago Vignattic1903232012-07-16 12:15:37 -04001108 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001109}
1110
1111static void
1112weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1113{
1114 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1115 struct weston_wm_window *window;
1116 int cursor, width, height;
1117
1118 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001119 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001120 return;
1121
1122 weston_wm_window_get_frame_size(window, &width, &height);
1123 cursor = get_cursor_for_location(wm->theme, width, height,
1124 enter->event_x, enter->event_y);
1125
Tiago Vignattic1903232012-07-16 12:15:37 -04001126 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001127}
1128
1129static void
1130weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1131{
1132 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1133 struct weston_wm_window *window;
1134
1135 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001136 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001137 return;
1138
Tiago Vignattic1903232012-07-16 12:15:37 -04001139 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001140}
1141
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001142static int
1143weston_wm_handle_event(int fd, uint32_t mask, void *data)
1144{
1145 struct weston_wm *wm = data;
1146 xcb_generic_event_t *event;
1147 int count = 0;
1148
1149 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1150 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001151 free(event);
1152 count++;
1153 continue;
1154 }
1155
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001156 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001157 case XCB_BUTTON_PRESS:
1158 case XCB_BUTTON_RELEASE:
1159 weston_wm_handle_button(wm, event);
1160 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001161 case XCB_ENTER_NOTIFY:
1162 weston_wm_handle_enter(wm, event);
1163 break;
1164 case XCB_LEAVE_NOTIFY:
1165 weston_wm_handle_leave(wm, event);
1166 break;
1167 case XCB_MOTION_NOTIFY:
1168 weston_wm_handle_motion(wm, event);
1169 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001170 case XCB_CREATE_NOTIFY:
1171 weston_wm_handle_create_notify(wm, event);
1172 break;
1173 case XCB_MAP_REQUEST:
1174 weston_wm_handle_map_request(wm, event);
1175 break;
1176 case XCB_MAP_NOTIFY:
1177 weston_wm_handle_map_notify(wm, event);
1178 break;
1179 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001180 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001181 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001182 case XCB_REPARENT_NOTIFY:
1183 weston_wm_handle_reparent_notify(wm, event);
1184 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001185 case XCB_CONFIGURE_REQUEST:
1186 weston_wm_handle_configure_request(wm, event);
1187 break;
1188 case XCB_CONFIGURE_NOTIFY:
1189 weston_wm_handle_configure_notify(wm, event);
1190 break;
1191 case XCB_DESTROY_NOTIFY:
1192 weston_wm_handle_destroy_notify(wm, event);
1193 break;
1194 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001195 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001196 break;
1197 case XCB_PROPERTY_NOTIFY:
1198 weston_wm_handle_property_notify(wm, event);
1199 break;
1200 case XCB_CLIENT_MESSAGE:
1201 weston_wm_handle_client_message(wm, event);
1202 break;
1203 }
1204
1205 free(event);
1206 count++;
1207 }
1208
1209 xcb_flush(wm->conn);
1210
1211 return count;
1212}
1213
1214static void
1215wxs_wm_get_resources(struct weston_wm *wm)
1216{
1217
1218#define F(field) offsetof(struct weston_wm, field)
1219
1220 static const struct { const char *name; int offset; } atoms[] = {
1221 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1222 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1223 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001224 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001225 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001226 { "_NET_WM_NAME", F(atom.net_wm_name) },
1227 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1228 { "_NET_WM_STATE", F(atom.net_wm_state) },
1229 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1230 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1231 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1232 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1233
1234 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1235 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1236 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1237 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1238 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1239 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1240 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001241 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1242 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001243 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1244 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1245 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1246 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1247 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1248
1249 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1250 { "_NET_SUPPORTING_WM_CHECK",
1251 F(atom.net_supporting_wm_check) },
1252 { "_NET_SUPPORTED", F(atom.net_supported) },
1253 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1254 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001255 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001256 { "TARGETS", F(atom.targets) },
1257 { "UTF8_STRING", F(atom.utf8_string) },
1258 { "_WL_SELECTION", F(atom.wl_selection) },
1259 { "INCR", F(atom.incr) },
1260 { "TIMESTAMP", F(atom.timestamp) },
1261 { "MULTIPLE", F(atom.multiple) },
1262 { "UTF8_STRING" , F(atom.utf8_string) },
1263 { "COMPOUND_TEXT", F(atom.compound_text) },
1264 { "TEXT", F(atom.text) },
1265 { "STRING", F(atom.string) },
1266 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1267 { "text/plain", F(atom.text_plain) },
1268 };
1269#undef F
1270
1271 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1272 xcb_xfixes_query_version_reply_t *xfixes_reply;
1273 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1274 xcb_intern_atom_reply_t *reply;
1275 xcb_render_query_pict_formats_reply_t *formats_reply;
1276 xcb_render_query_pict_formats_cookie_t formats_cookie;
1277 xcb_render_pictforminfo_t *formats;
1278 uint32_t i;
1279
1280 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1281
1282 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1283
1284 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1285 cookies[i] = xcb_intern_atom (wm->conn, 0,
1286 strlen(atoms[i].name),
1287 atoms[i].name);
1288
1289 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1290 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1291 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1292 free(reply);
1293 }
1294
1295 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1296 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001297 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001298
1299 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1300 XCB_XFIXES_MAJOR_VERSION,
1301 XCB_XFIXES_MINOR_VERSION);
1302 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1303 xfixes_cookie, NULL);
1304
Martin Minarik6d118362012-06-07 18:01:59 +02001305 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001306 xfixes_reply->major_version, xfixes_reply->minor_version);
1307
1308 free(xfixes_reply);
1309
1310 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1311 formats_cookie, 0);
1312 if (formats_reply == NULL)
1313 return;
1314
1315 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001316 for (i = 0; i < formats_reply->num_formats; i++) {
1317 if (formats[i].direct.red_mask != 0xff &&
1318 formats[i].direct.red_shift != 16)
1319 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001320 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1321 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001322 wm->format_rgb = formats[i];
1323 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1324 formats[i].depth == 32 &&
1325 formats[i].direct.alpha_mask == 0xff &&
1326 formats[i].direct.alpha_shift == 24)
1327 wm->format_rgba = formats[i];
1328 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001329
1330 free(formats_reply);
1331}
1332
1333static void
1334weston_wm_create_wm_window(struct weston_wm *wm)
1335{
1336 static const char name[] = "Weston WM";
1337
1338 wm->wm_window = xcb_generate_id(wm->conn);
1339 xcb_create_window(wm->conn,
1340 XCB_COPY_FROM_PARENT,
1341 wm->wm_window,
1342 wm->screen->root,
1343 0, 0,
1344 10, 10,
1345 0,
1346 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1347 wm->screen->root_visual,
1348 0, NULL);
1349
1350 xcb_change_property(wm->conn,
1351 XCB_PROP_MODE_REPLACE,
1352 wm->wm_window,
1353 wm->atom.net_supporting_wm_check,
1354 XCB_ATOM_WINDOW,
1355 32, /* format */
1356 1, &wm->wm_window);
1357
1358 xcb_change_property(wm->conn,
1359 XCB_PROP_MODE_REPLACE,
1360 wm->wm_window,
1361 wm->atom.net_wm_name,
1362 wm->atom.utf8_string,
1363 8, /* format */
1364 strlen(name), name);
1365
1366 xcb_change_property(wm->conn,
1367 XCB_PROP_MODE_REPLACE,
1368 wm->screen->root,
1369 wm->atom.net_supporting_wm_check,
1370 XCB_ATOM_WINDOW,
1371 32, /* format */
1372 1, &wm->wm_window);
1373
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001374 /* Claim the WM_S0 selection even though we don't suport
1375 * the --replace functionality. */
1376 xcb_set_selection_owner(wm->conn,
1377 wm->wm_window,
1378 wm->atom.wm_s0,
1379 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001380}
1381
1382struct weston_wm *
1383weston_wm_create(struct weston_xserver *wxs)
1384{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001385 struct weston_wm *wm;
1386 struct wl_event_loop *loop;
1387 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001388 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001389 int sv[2];
1390 xcb_atom_t supported[1];
1391
1392 wm = malloc(sizeof *wm);
1393 if (wm == NULL)
1394 return NULL;
1395
1396 memset(wm, 0, sizeof *wm);
1397 wm->server = wxs;
1398 wm->window_hash = hash_table_create();
1399 if (wm->window_hash == NULL) {
1400 free(wm);
1401 return NULL;
1402 }
1403
1404 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001405 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001406 hash_table_destroy(wm->window_hash);
1407 free(wm);
1408 return NULL;
1409 }
1410
1411 xserver_send_client(wxs->resource, sv[1]);
1412 wl_client_flush(wxs->resource->client);
1413 close(sv[1]);
1414
1415 /* xcb_connect_to_fd takes ownership of the fd. */
1416 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1417 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001418 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001419 close(sv[0]);
1420 hash_table_destroy(wm->window_hash);
1421 free(wm);
1422 return NULL;
1423 }
1424
1425 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1426 wm->screen = s.data;
1427
1428 loop = wl_display_get_event_loop(wxs->wl_display);
1429 wm->source =
1430 wl_event_loop_add_fd(loop, sv[0],
1431 WL_EVENT_READABLE,
1432 weston_wm_handle_event, wm);
1433 wl_event_source_check(wm->source);
1434
1435 wxs_wm_get_resources(wm);
1436
1437 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001438 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1439 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1440 XCB_EVENT_MASK_PROPERTY_CHANGE;
1441 xcb_change_window_attributes(wm->conn, wm->screen->root,
1442 XCB_CW_EVENT_MASK, values);
1443 wm->theme = theme_create();
1444
1445 weston_wm_create_wm_window(wm);
1446
1447 supported[0] = wm->atom.net_wm_moveresize;
1448 xcb_change_property(wm->conn,
1449 XCB_PROP_MODE_REPLACE,
1450 wm->screen->root,
1451 wm->atom.net_supported,
1452 XCB_ATOM_ATOM,
1453 32, /* format */
1454 ARRAY_LENGTH(supported), supported);
1455
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001456 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001457
1458 xcb_flush(wm->conn);
1459
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001460 wm->activate_listener.notify = weston_wm_window_activate;
1461 wl_signal_add(&wxs->compositor->activate_signal,
1462 &wm->activate_listener);
1463
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001464 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001465 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001466
Martin Minarik6d118362012-06-07 18:01:59 +02001467 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001468
1469 return wm;
1470}
1471
1472void
1473weston_wm_destroy(struct weston_wm *wm)
1474{
1475 /* FIXME: Free windows in hash. */
1476 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001477 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001478 xcb_disconnect(wm->conn);
1479 wl_event_source_remove(wm->source);
1480 wl_list_remove(&wm->selection_listener.link);
1481 wl_list_remove(&wm->activate_listener.link);
1482
1483 free(wm);
1484}
1485
1486static void
1487surface_destroy(struct wl_listener *listener, void *data)
1488{
1489 struct weston_wm_window *window =
1490 container_of(listener,
1491 struct weston_wm_window, surface_destroy_listener);
1492
Martin Minarik6d118362012-06-07 18:01:59 +02001493 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001494}
1495
1496static struct weston_wm_window *
1497get_wm_window(struct weston_surface *surface)
1498{
1499 struct wl_resource *resource = &surface->surface.resource;
1500 struct wl_listener *listener;
1501
1502 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1503 if (listener)
1504 return container_of(listener, struct weston_wm_window,
1505 surface_destroy_listener);
1506
1507 return NULL;
1508}
1509
1510static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001511weston_wm_window_configure(void *data)
1512{
1513 struct weston_wm_window *window = data;
1514 struct weston_wm *wm = window->wm;
1515 uint32_t values[2];
1516 int width, height;
1517
1518 values[0] = window->width;
1519 values[1] = window->height;
1520 xcb_configure_window(wm->conn,
1521 window->id,
1522 XCB_CONFIG_WINDOW_WIDTH |
1523 XCB_CONFIG_WINDOW_HEIGHT,
1524 values);
1525
1526 weston_wm_window_get_frame_size(window, &width, &height);
1527 values[0] = width;
1528 values[1] = height;
1529 xcb_configure_window(wm->conn,
1530 window->frame_id,
1531 XCB_CONFIG_WINDOW_WIDTH |
1532 XCB_CONFIG_WINDOW_HEIGHT,
1533 values);
1534
1535 window->configure_source = NULL;
1536
1537 weston_wm_window_schedule_repaint(window);
1538}
1539
1540static void
1541send_configure(struct weston_surface *surface,
1542 uint32_t edges, int32_t width, int32_t height)
1543{
1544 struct weston_wm_window *window = get_wm_window(surface);
1545 struct weston_wm *wm = window->wm;
1546 struct theme *t = window->wm->theme;
1547
1548 if (window->decorate) {
1549 window->width = width - 2 * (t->margin + t->width);
1550 window->height = height - 2 * t->margin -
1551 t->titlebar_height - t->width;
1552 } else {
1553 window->width = width - 2 * t->margin;
1554 window->height = height - 2 * t->margin;
1555 }
1556
1557 if (window->configure_source)
1558 return;
1559
1560 window->configure_source =
1561 wl_event_loop_add_idle(wm->server->loop,
1562 weston_wm_window_configure, window);
1563}
1564
1565static const struct weston_shell_client shell_client = {
1566 send_configure
1567};
1568
1569static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001570xserver_map_shell_surface(struct weston_wm *wm,
1571 struct weston_wm_window *window)
1572{
1573 struct weston_shell_interface *shell_interface =
1574 &wm->server->compositor->shell_interface;
1575 struct weston_wm_window *parent;
1576 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001577 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001578
1579 if (!shell_interface->create_shell_surface)
1580 return;
1581
1582 window->shsurf =
1583 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001584 window->surface,
1585 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001586
Tiago Vignatti771241e2012-06-04 20:01:45 +03001587 /* ICCCM 4.1.1 */
Tiago Vignattice1baa82012-07-20 23:09:55 +03001588 if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001589 shell_interface->set_toplevel(window->shsurf);
1590 return;
1591 }
1592
Tiago Vignattice1baa82012-07-20 23:09:55 +03001593 /* not all non-toplevel has transient_for set. So we need this
1594 * workaround to guess a parent that will determine the relative
1595 * position of the transient surface */
1596 if (!window->transient_for)
1597 parent_id = wm->focus_latest->id;
1598 else
1599 parent_id = window->transient_for->id;
1600
1601 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001602
1603 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1604 if (!parent->decorate && parent->override_redirect) {
1605 x = parent->x + t->margin;
1606 y = parent->y + t->margin;
1607 }
1608
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001609 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001610 window->x + t->margin - x,
1611 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001612 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1613}
1614
1615static void
1616xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1617 struct wl_resource *surface_resource, uint32_t id)
1618{
1619 struct weston_xserver *wxs = resource->data;
1620 struct weston_wm *wm = wxs->wm;
1621 struct wl_surface *surface = surface_resource->data;
1622 struct weston_wm_window *window;
1623
1624 if (client != wxs->client)
1625 return;
1626
1627 window = hash_table_lookup(wm->window_hash, id);
1628 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001629 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001630 return;
1631 }
1632
Martin Minarik6d118362012-06-07 18:01:59 +02001633 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001634
1635 weston_wm_window_read_properties(window);
1636
1637 window->surface = (struct weston_surface *) surface;
1638 window->surface_destroy_listener.notify = surface_destroy;
1639 wl_signal_add(&surface->resource.destroy_signal,
1640 &window->surface_destroy_listener);
1641
1642 weston_wm_window_schedule_repaint(window);
1643 xserver_map_shell_surface(wm, window);
1644}
1645
1646const struct xserver_interface xserver_implementation = {
1647 xserver_set_window_id
1648};