blob: 65eb11a062bf9b32ac561fa72d7f15b138275b78 [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) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300720 pixman_region32_fini(&window->surface->opaque);
721 pixman_region32_init_rect(&window->surface->opaque, 0, 0,
722 width, height);
723
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400724 /* We leave an extra pixel around the X window area to
725 * make sure we don't sample from the undefined alpha
726 * channel when filtering. */
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300727 pixman_region32_intersect_rect(&window->surface->opaque,
728 &window->surface->opaque,
729 x - 1, y - 1,
730 window->width + 2,
731 window->height + 2);
732 window->surface->geometry.dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400733
734 pixman_region32_init_rect(&window->surface->input,
735 t->margin, t->margin,
736 width - 2 * t->margin,
737 height - 2 * t->margin);
738 }
739}
740
741static void
742weston_wm_window_schedule_repaint(struct weston_wm_window *window)
743{
744 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300745 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400747 if (window->frame_id == XCB_WINDOW_NONE) {
748 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300749 weston_wm_window_get_frame_size(window, &width, &height);
750 pixman_region32_fini(&window->surface->opaque);
751 pixman_region32_init_rect(&window->surface->opaque, 0, 0,
752 width, height);
753 window->surface->geometry.dirty = 1;
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400754 }
755 return;
756 }
757
758 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759 return;
760
761 window->repaint_source =
762 wl_event_loop_add_idle(wm->server->loop,
763 weston_wm_window_draw_decoration,
764 window);
765}
766
767static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400768weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
769{
770 xcb_property_notify_event_t *property_notify =
771 (xcb_property_notify_event_t *) event;
772 struct weston_wm_window *window;
773
774 window = hash_table_lookup(wm->window_hash, property_notify->window);
775 if (window)
776 window->properties_dirty = 1;
777
Martin Minarik6d118362012-06-07 18:01:59 +0200778 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400779 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400780 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200781 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400782 else
783 read_and_dump_property(wm, property_notify->window,
784 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400785
786 if (property_notify->atom == wm->atom.net_wm_name ||
787 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400788 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400789}
790
791static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400792weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300793 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400794{
795 struct weston_wm_window *window;
796 uint32_t values[1];
797
798 window = malloc(sizeof *window);
799 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200800 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400801 return;
802 }
803
804 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
805 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
806
807 memset(window, 0, sizeof *window);
808 window->wm = wm;
809 window->id = id;
810 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300811 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400812 window->width = width;
813 window->height = height;
814
815 hash_table_insert(wm->window_hash, id, window);
816}
817
818static void
819weston_wm_window_destroy(struct weston_wm_window *window)
820{
821 hash_table_remove(window->wm->window_hash, window->id);
822 free(window);
823}
824
825static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400826weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
827{
828 xcb_create_notify_event_t *create_notify =
829 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400830
Martin Minarik6d118362012-06-07 18:01:59 +0200831 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400832 create_notify->window,
833 create_notify->width, create_notify->height,
834 create_notify->override_redirect ? ", override" : "",
835 our_resource(wm, create_notify->window) ? ", ours" : "");
836
837 if (our_resource(wm, create_notify->window))
838 return;
839
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400840 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300841 create_notify->width, create_notify->height,
842 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400843}
844
845static void
846weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
847{
848 xcb_destroy_notify_event_t *destroy_notify =
849 (xcb_destroy_notify_event_t *) event;
850 struct weston_wm_window *window;
851
Martin Minarik6d118362012-06-07 18:01:59 +0200852 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400853 destroy_notify->window,
854 destroy_notify->event,
855 our_resource(wm, destroy_notify->window) ? ", ours" : "");
856
857 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400858 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400859
860 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400861 weston_wm_window_destroy(window);
862}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400864static void
865weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
866{
867 xcb_reparent_notify_event_t *reparent_notify =
868 (xcb_reparent_notify_event_t *) event;
869 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400870
Martin Minarik6d118362012-06-07 18:01:59 +0200871 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400872 reparent_notify->window,
873 reparent_notify->parent,
874 reparent_notify->event);
875
876 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300877 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
878 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400879 } else if (!our_resource(wm, reparent_notify->parent)) {
880 window = hash_table_lookup(wm->window_hash,
881 reparent_notify->window);
882 weston_wm_window_destroy(window);
883 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400884}
885
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400886struct weston_seat *
887weston_wm_pick_seat(struct weston_wm *wm)
888{
889 return container_of(wm->server->compositor->seat_list.next,
890 struct weston_seat, link);
891}
892
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400893static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400894weston_wm_window_handle_moveresize(struct weston_wm_window *window,
895 xcb_client_message_event_t *client_message)
896{
897 static const int map[] = {
898 THEME_LOCATION_RESIZING_TOP_LEFT,
899 THEME_LOCATION_RESIZING_TOP,
900 THEME_LOCATION_RESIZING_TOP_RIGHT,
901 THEME_LOCATION_RESIZING_RIGHT,
902 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
903 THEME_LOCATION_RESIZING_BOTTOM,
904 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
905 THEME_LOCATION_RESIZING_LEFT
906 };
907
908 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400909 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400910 int detail;
911 struct weston_shell_interface *shell_interface =
912 &wm->server->compositor->shell_interface;
913
914 if (seat->seat.pointer->button_count != 1 ||
915 seat->seat.pointer->focus != &window->surface->surface)
916 return;
917
918 detail = client_message->data.data32[2];
919 switch (detail) {
920 case _NET_WM_MOVERESIZE_MOVE:
921 shell_interface->move(window->shsurf, seat);
922 break;
923 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
924 case _NET_WM_MOVERESIZE_SIZE_TOP:
925 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
926 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
927 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
928 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
929 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
930 case _NET_WM_MOVERESIZE_SIZE_LEFT:
931 shell_interface->resize(window->shsurf, seat, map[detail]);
932 break;
933 case _NET_WM_MOVERESIZE_CANCEL:
934 break;
935 }
936}
937
938static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400939weston_wm_handle_client_message(struct weston_wm *wm,
940 xcb_generic_event_t *event)
941{
942 xcb_client_message_event_t *client_message =
943 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400944 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945
946 window = hash_table_lookup(wm->window_hash, client_message->window);
947
Martin Minarik6d118362012-06-07 18:01:59 +0200948 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400949 get_atom_name(wm->conn, client_message->type),
950 client_message->data.data32[0],
951 client_message->data.data32[1],
952 client_message->data.data32[2],
953 client_message->data.data32[3],
954 client_message->data.data32[4]);
955
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400956 if (client_message->type == wm->atom.net_wm_moveresize)
957 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400958}
959
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400960enum cursor_type {
961 XWM_CURSOR_TOP,
962 XWM_CURSOR_BOTTOM,
963 XWM_CURSOR_LEFT,
964 XWM_CURSOR_RIGHT,
965 XWM_CURSOR_TOP_LEFT,
966 XWM_CURSOR_TOP_RIGHT,
967 XWM_CURSOR_BOTTOM_LEFT,
968 XWM_CURSOR_BOTTOM_RIGHT,
969 XWM_CURSOR_LEFT_PTR,
970};
971
972static const char *cursors[] = {
973 "top_side",
974 "bottom_side",
975 "left_side",
976 "right_side",
977 "top_left_corner",
978 "top_right_corner",
979 "bottom_left_corner",
980 "bottom_right_corner",
981 "left_ptr"
982};
983
984static void
985weston_wm_create_cursors(struct weston_wm *wm)
986{
987 int i, count = ARRAY_LENGTH(cursors);
988
989 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
990 for (i = 0; i < count; i++) {
991 wm->cursors[i] =
992 xcb_cursor_library_load_cursor(wm, cursors[i]);
993 }
994
995 wm->last_cursor = -1;
996}
997
998static void
999weston_wm_destroy_cursors(struct weston_wm *wm)
1000{
1001 uint8_t i;
1002
1003 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1004 xcb_free_cursor(wm->conn, wm->cursors[i]);
1005
1006 free(wm->cursors);
1007}
1008
1009static int
1010get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1011{
1012 int location = theme_get_location(t, x, y, width, height);
1013
1014 switch (location) {
1015 case THEME_LOCATION_RESIZING_TOP:
1016 return XWM_CURSOR_TOP;
1017 case THEME_LOCATION_RESIZING_BOTTOM:
1018 return XWM_CURSOR_BOTTOM;
1019 case THEME_LOCATION_RESIZING_LEFT:
1020 return XWM_CURSOR_LEFT;
1021 case THEME_LOCATION_RESIZING_RIGHT:
1022 return XWM_CURSOR_RIGHT;
1023 case THEME_LOCATION_RESIZING_TOP_LEFT:
1024 return XWM_CURSOR_TOP_LEFT;
1025 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1026 return XWM_CURSOR_TOP_RIGHT;
1027 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1028 return XWM_CURSOR_BOTTOM_LEFT;
1029 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1030 return XWM_CURSOR_BOTTOM_RIGHT;
1031 case THEME_LOCATION_EXTERIOR:
1032 case THEME_LOCATION_TITLEBAR:
1033 default:
1034 return XWM_CURSOR_LEFT_PTR;
1035 }
1036}
1037
1038static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001039weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1040 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001041{
1042 uint32_t cursor_value_list;
1043
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001044 if (wm->last_cursor == cursor)
1045 return;
1046
1047 wm->last_cursor = cursor;
1048
1049 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001050 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001051 XCB_CW_CURSOR, &cursor_value_list);
1052 xcb_flush(wm->conn);
1053}
1054
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001055static void
1056weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1057{
1058 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1059 struct weston_shell_interface *shell_interface =
1060 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001061 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001063 enum theme_location location;
1064 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001065 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001066
Martin Minarik6d118362012-06-07 18:01:59 +02001067 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001068 button->response_type == XCB_BUTTON_PRESS ?
1069 "PRESS" : "RELEASE", button->detail);
1070
1071 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001072 weston_wm_window_get_frame_size(window, &width, &height);
1073
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001074 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001075 button->detail == 1) {
1076 location = theme_get_location(t,
1077 button->event_x,
1078 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001079 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001080
1081 switch (location) {
1082 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001083 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001084 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001085 case THEME_LOCATION_RESIZING_TOP:
1086 case THEME_LOCATION_RESIZING_BOTTOM:
1087 case THEME_LOCATION_RESIZING_LEFT:
1088 case THEME_LOCATION_RESIZING_RIGHT:
1089 case THEME_LOCATION_RESIZING_TOP_LEFT:
1090 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1091 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1092 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1093 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001094 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001095 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001096 default:
1097 break;
1098 }
1099 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100}
1101
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001102static void
1103weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1104{
1105 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1106 struct weston_wm_window *window;
1107 int cursor, width, height;
1108
1109 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001110 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001111 return;
1112
1113 weston_wm_window_get_frame_size(window, &width, &height);
1114 cursor = get_cursor_for_location(wm->theme, width, height,
1115 motion->event_x, motion->event_y);
1116
Tiago Vignattic1903232012-07-16 12:15:37 -04001117 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001118}
1119
1120static void
1121weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1122{
1123 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1124 struct weston_wm_window *window;
1125 int cursor, width, height;
1126
1127 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001128 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001129 return;
1130
1131 weston_wm_window_get_frame_size(window, &width, &height);
1132 cursor = get_cursor_for_location(wm->theme, width, height,
1133 enter->event_x, enter->event_y);
1134
Tiago Vignattic1903232012-07-16 12:15:37 -04001135 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001136}
1137
1138static void
1139weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1140{
1141 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1142 struct weston_wm_window *window;
1143
1144 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001145 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001146 return;
1147
Tiago Vignattic1903232012-07-16 12:15:37 -04001148 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001149}
1150
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001151static int
1152weston_wm_handle_event(int fd, uint32_t mask, void *data)
1153{
1154 struct weston_wm *wm = data;
1155 xcb_generic_event_t *event;
1156 int count = 0;
1157
1158 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1159 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001160 free(event);
1161 count++;
1162 continue;
1163 }
1164
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001165 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001166 case XCB_BUTTON_PRESS:
1167 case XCB_BUTTON_RELEASE:
1168 weston_wm_handle_button(wm, event);
1169 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001170 case XCB_ENTER_NOTIFY:
1171 weston_wm_handle_enter(wm, event);
1172 break;
1173 case XCB_LEAVE_NOTIFY:
1174 weston_wm_handle_leave(wm, event);
1175 break;
1176 case XCB_MOTION_NOTIFY:
1177 weston_wm_handle_motion(wm, event);
1178 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001179 case XCB_CREATE_NOTIFY:
1180 weston_wm_handle_create_notify(wm, event);
1181 break;
1182 case XCB_MAP_REQUEST:
1183 weston_wm_handle_map_request(wm, event);
1184 break;
1185 case XCB_MAP_NOTIFY:
1186 weston_wm_handle_map_notify(wm, event);
1187 break;
1188 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001189 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001190 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001191 case XCB_REPARENT_NOTIFY:
1192 weston_wm_handle_reparent_notify(wm, event);
1193 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001194 case XCB_CONFIGURE_REQUEST:
1195 weston_wm_handle_configure_request(wm, event);
1196 break;
1197 case XCB_CONFIGURE_NOTIFY:
1198 weston_wm_handle_configure_notify(wm, event);
1199 break;
1200 case XCB_DESTROY_NOTIFY:
1201 weston_wm_handle_destroy_notify(wm, event);
1202 break;
1203 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001204 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001205 break;
1206 case XCB_PROPERTY_NOTIFY:
1207 weston_wm_handle_property_notify(wm, event);
1208 break;
1209 case XCB_CLIENT_MESSAGE:
1210 weston_wm_handle_client_message(wm, event);
1211 break;
1212 }
1213
1214 free(event);
1215 count++;
1216 }
1217
1218 xcb_flush(wm->conn);
1219
1220 return count;
1221}
1222
1223static void
1224wxs_wm_get_resources(struct weston_wm *wm)
1225{
1226
1227#define F(field) offsetof(struct weston_wm, field)
1228
1229 static const struct { const char *name; int offset; } atoms[] = {
1230 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1231 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1232 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001233 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001234 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001235 { "_NET_WM_NAME", F(atom.net_wm_name) },
1236 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1237 { "_NET_WM_STATE", F(atom.net_wm_state) },
1238 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1239 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1240 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1241 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1242
1243 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1244 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1245 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1246 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1247 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1248 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1249 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001250 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1251 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001252 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1253 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1254 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1255 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1256 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1257
1258 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1259 { "_NET_SUPPORTING_WM_CHECK",
1260 F(atom.net_supporting_wm_check) },
1261 { "_NET_SUPPORTED", F(atom.net_supported) },
1262 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1263 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001264 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265 { "TARGETS", F(atom.targets) },
1266 { "UTF8_STRING", F(atom.utf8_string) },
1267 { "_WL_SELECTION", F(atom.wl_selection) },
1268 { "INCR", F(atom.incr) },
1269 { "TIMESTAMP", F(atom.timestamp) },
1270 { "MULTIPLE", F(atom.multiple) },
1271 { "UTF8_STRING" , F(atom.utf8_string) },
1272 { "COMPOUND_TEXT", F(atom.compound_text) },
1273 { "TEXT", F(atom.text) },
1274 { "STRING", F(atom.string) },
1275 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1276 { "text/plain", F(atom.text_plain) },
1277 };
1278#undef F
1279
1280 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1281 xcb_xfixes_query_version_reply_t *xfixes_reply;
1282 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1283 xcb_intern_atom_reply_t *reply;
1284 xcb_render_query_pict_formats_reply_t *formats_reply;
1285 xcb_render_query_pict_formats_cookie_t formats_cookie;
1286 xcb_render_pictforminfo_t *formats;
1287 uint32_t i;
1288
1289 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1290
1291 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1292
1293 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1294 cookies[i] = xcb_intern_atom (wm->conn, 0,
1295 strlen(atoms[i].name),
1296 atoms[i].name);
1297
1298 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1299 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1300 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1301 free(reply);
1302 }
1303
1304 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1305 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001306 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001307
1308 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1309 XCB_XFIXES_MAJOR_VERSION,
1310 XCB_XFIXES_MINOR_VERSION);
1311 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1312 xfixes_cookie, NULL);
1313
Martin Minarik6d118362012-06-07 18:01:59 +02001314 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001315 xfixes_reply->major_version, xfixes_reply->minor_version);
1316
1317 free(xfixes_reply);
1318
1319 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1320 formats_cookie, 0);
1321 if (formats_reply == NULL)
1322 return;
1323
1324 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001325 for (i = 0; i < formats_reply->num_formats; i++) {
1326 if (formats[i].direct.red_mask != 0xff &&
1327 formats[i].direct.red_shift != 16)
1328 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001329 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1330 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001331 wm->format_rgb = formats[i];
1332 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1333 formats[i].depth == 32 &&
1334 formats[i].direct.alpha_mask == 0xff &&
1335 formats[i].direct.alpha_shift == 24)
1336 wm->format_rgba = formats[i];
1337 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001338
1339 free(formats_reply);
1340}
1341
1342static void
1343weston_wm_create_wm_window(struct weston_wm *wm)
1344{
1345 static const char name[] = "Weston WM";
1346
1347 wm->wm_window = xcb_generate_id(wm->conn);
1348 xcb_create_window(wm->conn,
1349 XCB_COPY_FROM_PARENT,
1350 wm->wm_window,
1351 wm->screen->root,
1352 0, 0,
1353 10, 10,
1354 0,
1355 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1356 wm->screen->root_visual,
1357 0, NULL);
1358
1359 xcb_change_property(wm->conn,
1360 XCB_PROP_MODE_REPLACE,
1361 wm->wm_window,
1362 wm->atom.net_supporting_wm_check,
1363 XCB_ATOM_WINDOW,
1364 32, /* format */
1365 1, &wm->wm_window);
1366
1367 xcb_change_property(wm->conn,
1368 XCB_PROP_MODE_REPLACE,
1369 wm->wm_window,
1370 wm->atom.net_wm_name,
1371 wm->atom.utf8_string,
1372 8, /* format */
1373 strlen(name), name);
1374
1375 xcb_change_property(wm->conn,
1376 XCB_PROP_MODE_REPLACE,
1377 wm->screen->root,
1378 wm->atom.net_supporting_wm_check,
1379 XCB_ATOM_WINDOW,
1380 32, /* format */
1381 1, &wm->wm_window);
1382
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001383 /* Claim the WM_S0 selection even though we don't suport
1384 * the --replace functionality. */
1385 xcb_set_selection_owner(wm->conn,
1386 wm->wm_window,
1387 wm->atom.wm_s0,
1388 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001389}
1390
1391struct weston_wm *
1392weston_wm_create(struct weston_xserver *wxs)
1393{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001394 struct weston_wm *wm;
1395 struct wl_event_loop *loop;
1396 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001397 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001398 int sv[2];
1399 xcb_atom_t supported[1];
1400
1401 wm = malloc(sizeof *wm);
1402 if (wm == NULL)
1403 return NULL;
1404
1405 memset(wm, 0, sizeof *wm);
1406 wm->server = wxs;
1407 wm->window_hash = hash_table_create();
1408 if (wm->window_hash == NULL) {
1409 free(wm);
1410 return NULL;
1411 }
1412
1413 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001414 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001415 hash_table_destroy(wm->window_hash);
1416 free(wm);
1417 return NULL;
1418 }
1419
1420 xserver_send_client(wxs->resource, sv[1]);
1421 wl_client_flush(wxs->resource->client);
1422 close(sv[1]);
1423
1424 /* xcb_connect_to_fd takes ownership of the fd. */
1425 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1426 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001427 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001428 close(sv[0]);
1429 hash_table_destroy(wm->window_hash);
1430 free(wm);
1431 return NULL;
1432 }
1433
1434 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1435 wm->screen = s.data;
1436
1437 loop = wl_display_get_event_loop(wxs->wl_display);
1438 wm->source =
1439 wl_event_loop_add_fd(loop, sv[0],
1440 WL_EVENT_READABLE,
1441 weston_wm_handle_event, wm);
1442 wl_event_source_check(wm->source);
1443
1444 wxs_wm_get_resources(wm);
1445
1446 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001447 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1448 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1449 XCB_EVENT_MASK_PROPERTY_CHANGE;
1450 xcb_change_window_attributes(wm->conn, wm->screen->root,
1451 XCB_CW_EVENT_MASK, values);
1452 wm->theme = theme_create();
1453
1454 weston_wm_create_wm_window(wm);
1455
1456 supported[0] = wm->atom.net_wm_moveresize;
1457 xcb_change_property(wm->conn,
1458 XCB_PROP_MODE_REPLACE,
1459 wm->screen->root,
1460 wm->atom.net_supported,
1461 XCB_ATOM_ATOM,
1462 32, /* format */
1463 ARRAY_LENGTH(supported), supported);
1464
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001465 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001466
1467 xcb_flush(wm->conn);
1468
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001469 wm->activate_listener.notify = weston_wm_window_activate;
1470 wl_signal_add(&wxs->compositor->activate_signal,
1471 &wm->activate_listener);
1472
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001473 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001474 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001475
Martin Minarik6d118362012-06-07 18:01:59 +02001476 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001477
1478 return wm;
1479}
1480
1481void
1482weston_wm_destroy(struct weston_wm *wm)
1483{
1484 /* FIXME: Free windows in hash. */
1485 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001486 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001487 xcb_disconnect(wm->conn);
1488 wl_event_source_remove(wm->source);
1489 wl_list_remove(&wm->selection_listener.link);
1490 wl_list_remove(&wm->activate_listener.link);
1491
1492 free(wm);
1493}
1494
1495static void
1496surface_destroy(struct wl_listener *listener, void *data)
1497{
1498 struct weston_wm_window *window =
1499 container_of(listener,
1500 struct weston_wm_window, surface_destroy_listener);
1501
Martin Minarik6d118362012-06-07 18:01:59 +02001502 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001503}
1504
1505static struct weston_wm_window *
1506get_wm_window(struct weston_surface *surface)
1507{
1508 struct wl_resource *resource = &surface->surface.resource;
1509 struct wl_listener *listener;
1510
1511 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1512 if (listener)
1513 return container_of(listener, struct weston_wm_window,
1514 surface_destroy_listener);
1515
1516 return NULL;
1517}
1518
1519static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001520weston_wm_window_configure(void *data)
1521{
1522 struct weston_wm_window *window = data;
1523 struct weston_wm *wm = window->wm;
1524 uint32_t values[2];
1525 int width, height;
1526
1527 values[0] = window->width;
1528 values[1] = window->height;
1529 xcb_configure_window(wm->conn,
1530 window->id,
1531 XCB_CONFIG_WINDOW_WIDTH |
1532 XCB_CONFIG_WINDOW_HEIGHT,
1533 values);
1534
1535 weston_wm_window_get_frame_size(window, &width, &height);
1536 values[0] = width;
1537 values[1] = height;
1538 xcb_configure_window(wm->conn,
1539 window->frame_id,
1540 XCB_CONFIG_WINDOW_WIDTH |
1541 XCB_CONFIG_WINDOW_HEIGHT,
1542 values);
1543
1544 window->configure_source = NULL;
1545
1546 weston_wm_window_schedule_repaint(window);
1547}
1548
1549static void
1550send_configure(struct weston_surface *surface,
1551 uint32_t edges, int32_t width, int32_t height)
1552{
1553 struct weston_wm_window *window = get_wm_window(surface);
1554 struct weston_wm *wm = window->wm;
1555 struct theme *t = window->wm->theme;
1556
1557 if (window->decorate) {
1558 window->width = width - 2 * (t->margin + t->width);
1559 window->height = height - 2 * t->margin -
1560 t->titlebar_height - t->width;
1561 } else {
1562 window->width = width - 2 * t->margin;
1563 window->height = height - 2 * t->margin;
1564 }
1565
1566 if (window->configure_source)
1567 return;
1568
1569 window->configure_source =
1570 wl_event_loop_add_idle(wm->server->loop,
1571 weston_wm_window_configure, window);
1572}
1573
1574static const struct weston_shell_client shell_client = {
1575 send_configure
1576};
1577
1578static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001579xserver_map_shell_surface(struct weston_wm *wm,
1580 struct weston_wm_window *window)
1581{
1582 struct weston_shell_interface *shell_interface =
1583 &wm->server->compositor->shell_interface;
1584 struct weston_wm_window *parent;
1585 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001586 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001587
1588 if (!shell_interface->create_shell_surface)
1589 return;
1590
1591 window->shsurf =
1592 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001593 window->surface,
1594 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001595
Tiago Vignatti771241e2012-06-04 20:01:45 +03001596 /* ICCCM 4.1.1 */
Tiago Vignattice1baa82012-07-20 23:09:55 +03001597 if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001598 shell_interface->set_toplevel(window->shsurf);
1599 return;
1600 }
1601
Tiago Vignattice1baa82012-07-20 23:09:55 +03001602 /* not all non-toplevel has transient_for set. So we need this
1603 * workaround to guess a parent that will determine the relative
1604 * position of the transient surface */
1605 if (!window->transient_for)
1606 parent_id = wm->focus_latest->id;
1607 else
1608 parent_id = window->transient_for->id;
1609
1610 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001611
1612 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1613 if (!parent->decorate && parent->override_redirect) {
1614 x = parent->x + t->margin;
1615 y = parent->y + t->margin;
1616 }
1617
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001618 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001619 window->x + t->margin - x,
1620 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001621 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1622}
1623
1624static void
1625xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1626 struct wl_resource *surface_resource, uint32_t id)
1627{
1628 struct weston_xserver *wxs = resource->data;
1629 struct weston_wm *wm = wxs->wm;
1630 struct wl_surface *surface = surface_resource->data;
1631 struct weston_wm_window *window;
1632
1633 if (client != wxs->client)
1634 return;
1635
1636 window = hash_table_lookup(wm->window_hash, id);
1637 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001638 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001639 return;
1640 }
1641
Martin Minarik6d118362012-06-07 18:01:59 +02001642 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001643
1644 weston_wm_window_read_properties(window);
1645
1646 window->surface = (struct weston_surface *) surface;
1647 window->surface_destroy_listener.notify = surface_destroy;
1648 wl_signal_add(&surface->resource.destroy_signal,
1649 &window->surface_destroy_listener);
1650
1651 weston_wm_window_schedule_repaint(window);
1652 xserver_map_shell_surface(wm, window);
1653}
1654
1655const struct xserver_interface xserver_implementation = {
1656 xserver_set_window_id
1657};