blob: 5d003a94b7985677c959884c1fbee8bc49f52cfa [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
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024
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
Kristian Høgsberg380deee2012-05-21 17:12:41 -040092struct weston_wm_window {
93 struct weston_wm *wm;
94 xcb_window_t id;
95 xcb_window_t frame_id;
96 cairo_surface_t *cairo_surface;
97 struct weston_surface *surface;
98 struct shell_surface *shsurf;
99 struct wl_listener surface_destroy_listener;
100 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400101 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400102 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300103 int pid;
104 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105 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;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500112 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400113 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300114 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500115 int fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400116};
117
118static struct weston_wm_window *
119get_wm_window(struct weston_surface *surface);
120
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400121static void
122weston_wm_window_schedule_repaint(struct weston_wm_window *window);
123
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400124const char *
125get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
126{
127 xcb_get_atom_name_cookie_t cookie;
128 xcb_get_atom_name_reply_t *reply;
129 xcb_generic_error_t *e;
130 static char buffer[64];
131
132 if (atom == XCB_ATOM_NONE)
133 return "None";
134
135 cookie = xcb_get_atom_name (c, atom);
136 reply = xcb_get_atom_name_reply (c, cookie, &e);
137 snprintf(buffer, sizeof buffer, "%.*s",
138 xcb_get_atom_name_name_length (reply),
139 xcb_get_atom_name_name (reply));
140 free(reply);
141
142 return buffer;
143}
144
Tiago Vignatti90fada42012-07-16 12:02:08 -0400145static xcb_cursor_t
146xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
147{
148 xcb_connection_t *c = wm->conn;
149 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
150 xcb_screen_t *screen = s.data;
151 xcb_gcontext_t gc;
152 xcb_pixmap_t pix;
153 xcb_render_picture_t pic;
154 xcb_cursor_t cursor;
155 int stride = img->width * 4;
156
157 pix = xcb_generate_id(c);
158 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
159
160 pic = xcb_generate_id(c);
161 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
162
163 gc = xcb_generate_id(c);
164 xcb_create_gc(c, gc, pix, 0, 0);
165
166 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
167 img->width, img->height, 0, 0, 0, 32,
168 stride * img->height, (uint8_t *) img->pixels);
169 xcb_free_gc(c, gc);
170
171 cursor = xcb_generate_id(c);
172 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
173
174 xcb_render_free_picture(c, pic);
175 xcb_free_pixmap(c, pix);
176
177 return cursor;
178}
179
180static xcb_cursor_t
181xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
182{
183 /* TODO: treat animated cursors as well */
184 if (images->nimage != 1)
185 return -1;
186
187 return xcb_cursor_image_load_cursor(wm, images->images[0]);
188}
189
190static xcb_cursor_t
191xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
192{
193 xcb_cursor_t cursor;
194 XcursorImages *images;
195 char *v = NULL;
196 int size = 0;
197
198 if (!file)
199 return 0;
200
201 v = getenv ("XCURSOR_SIZE");
202 if (v)
203 size = atoi(v);
204
205 if (!size)
206 size = 32;
207
208 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300209 if (!images)
210 return -1;
211
Tiago Vignatti90fada42012-07-16 12:02:08 -0400212 cursor = xcb_cursor_images_load_cursor (wm, images);
213 XcursorImagesDestroy (images);
214
215 return cursor;
216}
217
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400218void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400219dump_property(struct weston_wm *wm,
220 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400221{
222 int32_t *incr_value;
223 const char *text_value, *name;
224 xcb_atom_t *atom_value;
225 int width, len;
226 uint32_t i;
227
Martin Minarik6d118362012-06-07 18:01:59 +0200228 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400229 if (reply == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200230 weston_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400231 return;
232 }
233
Martin Minarik6d118362012-06-07 18:01:59 +0200234 width += weston_log_continue(
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400235 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400236 get_atom_name(wm->conn, reply->type),
237 reply->format,
238 xcb_get_property_value_length(reply),
239 reply->value_len);
240
241 if (reply->type == wm->atom.incr) {
242 incr_value = xcb_get_property_value(reply);
Martin Minarik6d118362012-06-07 18:01:59 +0200243 weston_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400244 } else if (reply->type == wm->atom.utf8_string ||
245 reply->type == wm->atom.string) {
246 text_value = xcb_get_property_value(reply);
247 if (reply->value_len > 40)
248 len = 40;
249 else
250 len = reply->value_len;
Martin Minarik6d118362012-06-07 18:01:59 +0200251 weston_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400252 } else if (reply->type == XCB_ATOM_ATOM) {
253 atom_value = xcb_get_property_value(reply);
254 for (i = 0; i < reply->value_len; i++) {
255 name = get_atom_name(wm->conn, atom_value[i]);
256 if (width + strlen(name) + 2 > 78) {
Martin Minarik6d118362012-06-07 18:01:59 +0200257 weston_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400258 width = 4;
259 } else if (i > 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 width += weston_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400261 }
262
Martin Minarik6d118362012-06-07 18:01:59 +0200263 width += weston_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400264 }
Martin Minarik6d118362012-06-07 18:01:59 +0200265 weston_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400266 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400268 }
269}
270
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200271static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400272read_and_dump_property(struct weston_wm *wm,
273 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400274{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400275 xcb_get_property_reply_t *reply;
276 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400277
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400278 cookie = xcb_get_property(wm->conn, 0, window,
279 property, XCB_ATOM_ANY, 0, 2048);
280 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400281
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400282 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400283
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400284 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400285}
286
287/* We reuse some predefined, but otherwise useles atoms */
288#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
289#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500290#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400291
292static void
293weston_wm_window_read_properties(struct weston_wm_window *window)
294{
295 struct weston_wm *wm = window->wm;
296
297#define F(field) offsetof(struct weston_wm_window, field)
298 const struct {
299 xcb_atom_t atom;
300 xcb_atom_t type;
301 int offset;
302 } props[] = {
303 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
304 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
305 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
306 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500307 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400308 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
309 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300310 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400311 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300312 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400313 };
314#undef F
315
316 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
317 xcb_get_property_reply_t *reply;
318 void *p;
319 uint32_t *xid;
320 xcb_atom_t *atom;
321 uint32_t i;
322 struct motif_wm_hints *hints;
323
324 if (!window->properties_dirty)
325 return;
326 window->properties_dirty = 0;
327
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400328 for (i = 0; i < ARRAY_LENGTH(props); i++)
329 cookie[i] = xcb_get_property(wm->conn,
330 0, /* delete */
331 window->id,
332 props[i].atom,
333 XCB_ATOM_ANY, 0, 2048);
334
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300335 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400336 for (i = 0; i < ARRAY_LENGTH(props); i++) {
337 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
338 if (!reply)
339 /* Bad window, typically */
340 continue;
341 if (reply->type == XCB_ATOM_NONE) {
342 /* No such property */
343 free(reply);
344 continue;
345 }
346
347 p = ((char *) window + props[i].offset);
348
349 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300350 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400351 case XCB_ATOM_STRING:
352 /* FIXME: We're using this for both string and
353 utf8_string */
354 if (*(char **) p)
355 free(*(char **) p);
356
357 *(char **) p =
358 strndup(xcb_get_property_value(reply),
359 xcb_get_property_value_length(reply));
360 break;
361 case XCB_ATOM_WINDOW:
362 xid = xcb_get_property_value(reply);
363 *(struct weston_wm_window **) p =
364 hash_table_lookup(wm->window_hash, *xid);
365 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300366 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400367 case XCB_ATOM_ATOM:
368 atom = xcb_get_property_value(reply);
369 *(xcb_atom_t *) p = *atom;
370 break;
371 case TYPE_WM_PROTOCOLS:
372 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500373 case TYPE_NET_WM_STATE:
374 window->fullscreen = 0;
375 atom = xcb_get_property_value(reply);
376 for (i = 0; i < reply->value_len; i++)
377 if (atom[i] == wm->atom.net_wm_state_fullscreen)
378 window->fullscreen = 1;
379 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400380 case TYPE_MOTIF_WM_HINTS:
381 hints = xcb_get_property_value(reply);
382 if (hints->flags & MWM_HINTS_DECORATIONS)
383 window->decorate = hints->decorations > 0;
384 break;
385 default:
386 break;
387 }
388 free(reply);
389 }
390}
391
392static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400393weston_wm_window_get_frame_size(struct weston_wm_window *window,
394 int *width, int *height)
395{
396 struct theme *t = window->wm->theme;
397
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500398 if (window->fullscreen) {
399 *width = window->width;
400 *height = window->height;
401 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400402 *width = window->width + (t->margin + t->width) * 2;
403 *height = window->height +
404 t->margin * 2 + t->width + t->titlebar_height;
405 } else {
406 *width = window->width + t->margin * 2;
407 *height = window->height + t->margin * 2;
408 }
409}
410
411static void
412weston_wm_window_get_child_position(struct weston_wm_window *window,
413 int *x, int *y)
414{
415 struct theme *t = window->wm->theme;
416
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500417 if (window->fullscreen) {
418 *x = 0;
419 *y = 0;
420 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400421 *x = t->margin + t->width;
422 *y = t->margin + t->titlebar_height;
423 } else {
424 *x = t->margin;
425 *y = t->margin;
426 }
427}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400428
429static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500430weston_wm_window_send_configure_notify(struct weston_wm_window *window)
431{
432 xcb_configure_notify_event_t configure_notify;
433 struct weston_wm *wm = window->wm;
434 int x, y;
435
436 weston_wm_window_get_child_position(window, &x, &y);
437 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
438 configure_notify.pad0 = 0;
439 configure_notify.event = window->id;
440 configure_notify.window = window->id;
441 configure_notify.above_sibling = XCB_WINDOW_NONE;
442 configure_notify.x = x;
443 configure_notify.y = y;
444 configure_notify.width = window->width;
445 configure_notify.height = window->height;
446 configure_notify.border_width = 0;
447 configure_notify.override_redirect = 0;
448 configure_notify.pad1 = 0;
449
450 xcb_send_event(wm->conn, 0, window->id,
451 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
452 (char *) &configure_notify);
453}
454
455static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400456weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
457{
458 xcb_configure_request_event_t *configure_request =
459 (xcb_configure_request_event_t *) event;
460 struct weston_wm_window *window;
461 uint32_t mask, values[16];
462 int x, y, width, height, i = 0;
463
Martin Minarik6d118362012-06-07 18:01:59 +0200464 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400465 configure_request->window,
466 configure_request->x, configure_request->y,
467 configure_request->width, configure_request->height);
468
469 window = hash_table_lookup(wm->window_hash, configure_request->window);
470
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500471 if (window->fullscreen) {
472 weston_wm_window_send_configure_notify(window);
473 return;
474 }
475
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400476 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
477 window->width = configure_request->width;
478 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
479 window->height = configure_request->height;
480
481 weston_wm_window_get_child_position(window, &x, &y);
482 values[i++] = x;
483 values[i++] = y;
484 values[i++] = window->width;
485 values[i++] = window->height;
486 values[i++] = 0;
487 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
488 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
489 XCB_CONFIG_WINDOW_BORDER_WIDTH;
490 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
491 values[i++] = configure_request->sibling;
492 mask |= XCB_CONFIG_WINDOW_SIBLING;
493 }
494 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
495 values[i++] = configure_request->stack_mode;
496 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
497 }
498
499 xcb_configure_window(wm->conn, window->id, mask, values);
500
501 weston_wm_window_get_frame_size(window, &width, &height);
502 values[0] = width;
503 values[1] = height;
504 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
505 xcb_configure_window(wm->conn, window->frame_id, mask, values);
506
507 weston_wm_window_schedule_repaint(window);
508}
509
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400510static void
511weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
512{
513 xcb_configure_notify_event_t *configure_notify =
514 (xcb_configure_notify_event_t *) event;
515 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300516 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400517
518 window = hash_table_lookup(wm->window_hash, configure_notify->window);
519
Martin Minarik6d118362012-06-07 18:01:59 +0200520 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400521 configure_notify->window == window->id ? "client" : "frame",
522 configure_notify->window,
523 configure_notify->x, configure_notify->y,
524 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300525
526 /* resize falls here */
527 if (configure_notify->window != window->id)
528 return;
529
530 weston_wm_window_get_child_position(window, &x, &y);
531 window->x = configure_notify->x - x;
532 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400533}
534
535static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300536weston_wm_kill_client(struct wl_listener *listener, void *data)
537{
538 struct weston_surface *surface = data;
539 struct weston_wm_window *window = get_wm_window(surface);
540 char name[1024];
541
542 if (!window)
543 return;
544
545 gethostname(name, 1024);
546
547 /* this is only one heuristic to guess the PID of a client is valid,
548 * assuming it's compliant with icccm and ewmh. Non-compliants and
549 * remote applications of course fail. */
550 if (!strcmp(window->machine, name) && window->pid != 0)
551 kill(window->pid, SIGKILL);
552}
553
554static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400555weston_wm_window_activate(struct wl_listener *listener, void *data)
556{
557 struct weston_surface *surface = data;
558 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600559 struct weston_wm *wm =
560 container_of(listener, struct weston_wm, activate_listener);
561 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400562
Scott Moreau85ecac02012-05-21 15:49:14 -0600563 if (window) {
564 client_message.response_type = XCB_CLIENT_MESSAGE;
565 client_message.format = 32;
566 client_message.window = window->id;
567 client_message.type = wm->atom.wm_protocols;
568 client_message.data.data32[0] = wm->atom.wm_take_focus;
569 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
570
571 xcb_send_event(wm->conn, 0, window->id,
572 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
573 (char *) &client_message);
574
575 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
576 window->id, XCB_TIME_CURRENT_TIME);
577 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400578 xcb_set_input_focus (wm->conn,
579 XCB_INPUT_FOCUS_POINTER_ROOT,
580 XCB_NONE,
581 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600582 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400583
584 if (wm->focus_window)
585 weston_wm_window_schedule_repaint(wm->focus_window);
586 wm->focus_window = window;
Tiago Vignattice1baa82012-07-20 23:09:55 +0300587 if (window)
588 wm->focus_latest = window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400589 if (wm->focus_window)
590 weston_wm_window_schedule_repaint(wm->focus_window);
591}
592
593static int
594our_resource(struct weston_wm *wm, uint32_t id)
595{
596 const xcb_setup_t *setup;
597
598 setup = xcb_get_setup(wm->conn);
599
600 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
601}
602
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400603#define ICCCM_WITHDRAWN_STATE 0
604#define ICCCM_NORMAL_STATE 1
605#define ICCCM_ICONIC_STATE 3
606
607static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500608weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400609{
610 struct weston_wm *wm = window->wm;
611 uint32_t property[2];
612
613 property[0] = state;
614 property[1] = XCB_WINDOW_NONE;
615
616 xcb_change_property(wm->conn,
617 XCB_PROP_MODE_REPLACE,
618 window->id,
619 wm->atom.wm_state,
620 wm->atom.wm_state,
621 32, /* format */
622 2, property);
623}
624
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400625static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500626weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
627{
628 struct weston_wm *wm = window->wm;
629 uint32_t property[1];
630 int i;
631
632 i = 0;
633 if (window->fullscreen)
634 property[i++] = wm->atom.net_wm_state_fullscreen;
635
636 xcb_change_property(wm->conn,
637 XCB_PROP_MODE_REPLACE,
638 window->id,
639 wm->atom.net_wm_state,
640 XCB_ATOM_ATOM,
641 32, /* format */
642 i, property);
643}
644
645static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400646weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
647{
648 xcb_map_request_event_t *map_request =
649 (xcb_map_request_event_t *) event;
650 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400651 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400652 int x, y, width, height;
653
654 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200655 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400656 map_request->window);
657 return;
658 }
659
660 window = hash_table_lookup(wm->window_hash, map_request->window);
661
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400662 if (window->frame_id)
663 return;
664
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400665 weston_wm_window_read_properties(window);
666
667 weston_wm_window_get_frame_size(window, &width, &height);
668 weston_wm_window_get_child_position(window, &x, &y);
669
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400670 values[0] = wm->screen->black_pixel;
671 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400672 XCB_EVENT_MASK_KEY_PRESS |
673 XCB_EVENT_MASK_KEY_RELEASE |
674 XCB_EVENT_MASK_BUTTON_PRESS |
675 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400676 XCB_EVENT_MASK_POINTER_MOTION |
677 XCB_EVENT_MASK_ENTER_WINDOW |
678 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400679 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400680 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400681 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400682
683 window->frame_id = xcb_generate_id(wm->conn);
684 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400685 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400686 window->frame_id,
687 wm->screen->root,
688 0, 0,
689 width, height,
690 0,
691 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400692 wm->visual_id,
693 XCB_CW_BORDER_PIXEL |
694 XCB_CW_EVENT_MASK |
695 XCB_CW_COLORMAP, values);
696
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400697 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
698
699 values[0] = 0;
700 xcb_configure_window(wm->conn, window->id,
701 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
702
Martin Minarik6d118362012-06-07 18:01:59 +0200703 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400704 window->id, window, window->frame_id);
705
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500706 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
707 weston_wm_window_set_net_wm_state(window);
708
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400709 xcb_map_window(wm->conn, map_request->window);
710 xcb_map_window(wm->conn, window->frame_id);
711
712 window->cairo_surface =
713 cairo_xcb_surface_create_with_xrender_format(wm->conn,
714 wm->screen,
715 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400716 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400717 width, height);
718
719 hash_table_insert(wm->window_hash, window->frame_id, window);
720}
721
722static void
723weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
724{
725 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
726
727 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200728 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400729 map_notify->window);
730 return;
731 }
732
Martin Minarik6d118362012-06-07 18:01:59 +0200733 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400734}
735
736static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400737weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
738{
739 xcb_unmap_notify_event_t *unmap_notify =
740 (xcb_unmap_notify_event_t *) event;
741 struct weston_wm_window *window;
742
Martin Minarik6d118362012-06-07 18:01:59 +0200743 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400744 unmap_notify->window,
745 unmap_notify->event,
746 our_resource(wm, unmap_notify->window) ? ", ours" : "");
747
748 if (our_resource(wm, unmap_notify->window))
749 return;
750
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400751 if (unmap_notify->response_type & 0x80)
752 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
753 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400754 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400755
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400756 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400757 if (window->repaint_source)
758 wl_event_source_remove(window->repaint_source);
759 if (window->cairo_surface)
760 cairo_surface_destroy(window->cairo_surface);
761
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400762 if (window->frame_id) {
763 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
764 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500765 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400766 hash_table_remove(wm->window_hash, window->frame_id);
767 window->frame_id = XCB_WINDOW_NONE;
768 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400769
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400770 if (wm->focus_window == window)
771 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400772 if (window->surface)
773 wl_list_remove(&window->surface_destroy_listener.link);
774 window->surface = NULL;
775}
776
777static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400778weston_wm_window_draw_decoration(void *data)
779{
780 struct weston_wm_window *window = data;
781 struct weston_wm *wm = window->wm;
782 struct theme *t = wm->theme;
783 cairo_t *cr;
784 int x, y, width, height;
785 const char *title;
786 uint32_t flags = 0;
787
788 weston_wm_window_read_properties(window);
789
790 window->repaint_source = NULL;
791
792 weston_wm_window_get_frame_size(window, &width, &height);
793 weston_wm_window_get_child_position(window, &x, &y);
794
795 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
796 cr = cairo_create(window->cairo_surface);
797
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500798 if (window->fullscreen) {
799 /* nothing */
800 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400801 if (wm->focus_window == window)
802 flags |= THEME_FRAME_ACTIVE;
803
804 if (window->name)
805 title = window->name;
806 else
807 title = "untitled";
808
809 theme_render_frame(t, cr, width, height, title, flags);
810 } else {
811 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
812 cairo_set_source_rgba(cr, 0, 0, 0, 0);
813 cairo_paint(cr);
814
815 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
816 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
817 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
818 }
819
820 cairo_destroy(cr);
821
822 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700823 pixman_region32_fini(&window->surface->pending.opaque);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400824 /* We leave an extra pixel around the X window area to
825 * make sure we don't sample from the undefined alpha
826 * channel when filtering. */
Kristian Høgsberg25bb6962013-02-14 22:01:04 -0500827 pixman_region32_init_rect(&window->surface->pending.opaque,
828 x - 1, y - 1,
829 window->width + 2,
830 window->height + 2);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200831 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500832 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400833
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500834 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500835 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500836 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400837 t->margin, t->margin,
838 width - 2 * t->margin,
839 height - 2 * t->margin);
840 }
841}
842
843static void
844weston_wm_window_schedule_repaint(struct weston_wm_window *window)
845{
846 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300847 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400848
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400849 if (window->frame_id == XCB_WINDOW_NONE) {
850 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300851 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700852 pixman_region32_fini(&window->surface->pending.opaque);
853 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300854 width, height);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200855 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400856 }
857 return;
858 }
859
860 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400861 return;
862
863 window->repaint_source =
864 wl_event_loop_add_idle(wm->server->loop,
865 weston_wm_window_draw_decoration,
866 window);
867}
868
869static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400870weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
871{
872 xcb_property_notify_event_t *property_notify =
873 (xcb_property_notify_event_t *) event;
874 struct weston_wm_window *window;
875
876 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000877 if (!window)
878 return;
879
880 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400881
Martin Minarik6d118362012-06-07 18:01:59 +0200882 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400883 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400884 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200885 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400886 else
887 read_and_dump_property(wm, property_notify->window,
888 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400889
890 if (property_notify->atom == wm->atom.net_wm_name ||
891 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400892 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400893}
894
895static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400896weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300897 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400898{
899 struct weston_wm_window *window;
900 uint32_t values[1];
901
902 window = malloc(sizeof *window);
903 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200904 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400905 return;
906 }
907
908 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
909 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
910
911 memset(window, 0, sizeof *window);
912 window->wm = wm;
913 window->id = id;
914 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300915 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400916 window->width = width;
917 window->height = height;
918
919 hash_table_insert(wm->window_hash, id, window);
920}
921
922static void
923weston_wm_window_destroy(struct weston_wm_window *window)
924{
925 hash_table_remove(window->wm->window_hash, window->id);
926 free(window);
927}
928
929static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400930weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
931{
932 xcb_create_notify_event_t *create_notify =
933 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400934
Martin Minarik6d118362012-06-07 18:01:59 +0200935 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 create_notify->window,
937 create_notify->width, create_notify->height,
938 create_notify->override_redirect ? ", override" : "",
939 our_resource(wm, create_notify->window) ? ", ours" : "");
940
941 if (our_resource(wm, create_notify->window))
942 return;
943
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400944 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300945 create_notify->width, create_notify->height,
946 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947}
948
949static void
950weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
951{
952 xcb_destroy_notify_event_t *destroy_notify =
953 (xcb_destroy_notify_event_t *) event;
954 struct weston_wm_window *window;
955
Martin Minarik6d118362012-06-07 18:01:59 +0200956 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400957 destroy_notify->window,
958 destroy_notify->event,
959 our_resource(wm, destroy_notify->window) ? ", ours" : "");
960
961 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400962 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400963
964 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400965 weston_wm_window_destroy(window);
966}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400967
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400968static void
969weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
970{
971 xcb_reparent_notify_event_t *reparent_notify =
972 (xcb_reparent_notify_event_t *) event;
973 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400974
Martin Minarik6d118362012-06-07 18:01:59 +0200975 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400976 reparent_notify->window,
977 reparent_notify->parent,
978 reparent_notify->event);
979
980 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300981 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
982 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400983 } else if (!our_resource(wm, reparent_notify->parent)) {
984 window = hash_table_lookup(wm->window_hash,
985 reparent_notify->window);
986 weston_wm_window_destroy(window);
987 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400988}
989
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400990struct weston_seat *
991weston_wm_pick_seat(struct weston_wm *wm)
992{
993 return container_of(wm->server->compositor->seat_list.next,
994 struct weston_seat, link);
995}
996
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400997static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400998weston_wm_window_handle_moveresize(struct weston_wm_window *window,
999 xcb_client_message_event_t *client_message)
1000{
1001 static const int map[] = {
1002 THEME_LOCATION_RESIZING_TOP_LEFT,
1003 THEME_LOCATION_RESIZING_TOP,
1004 THEME_LOCATION_RESIZING_TOP_RIGHT,
1005 THEME_LOCATION_RESIZING_RIGHT,
1006 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1007 THEME_LOCATION_RESIZING_BOTTOM,
1008 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1009 THEME_LOCATION_RESIZING_LEFT
1010 };
1011
1012 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001013 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001014 int detail;
1015 struct weston_shell_interface *shell_interface =
1016 &wm->server->compositor->shell_interface;
1017
Kristian Høgsberge3148752013-05-06 23:19:49 -04001018 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001019 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001020 return;
1021
1022 detail = client_message->data.data32[2];
1023 switch (detail) {
1024 case _NET_WM_MOVERESIZE_MOVE:
1025 shell_interface->move(window->shsurf, seat);
1026 break;
1027 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1028 case _NET_WM_MOVERESIZE_SIZE_TOP:
1029 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1030 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1031 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1032 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1033 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1034 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1035 shell_interface->resize(window->shsurf, seat, map[detail]);
1036 break;
1037 case _NET_WM_MOVERESIZE_CANCEL:
1038 break;
1039 }
1040}
1041
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001042#define _NET_WM_STATE_REMOVE 0
1043#define _NET_WM_STATE_ADD 1
1044#define _NET_WM_STATE_TOGGLE 2
1045
1046static int
1047update_state(int action, int *state)
1048{
1049 int new_state, changed;
1050
1051 switch (action) {
1052 case _NET_WM_STATE_REMOVE:
1053 new_state = 0;
1054 break;
1055 case _NET_WM_STATE_ADD:
1056 new_state = 1;
1057 break;
1058 case _NET_WM_STATE_TOGGLE:
1059 new_state = !*state;
1060 break;
1061 default:
1062 return 0;
1063 }
1064
1065 changed = (*state != new_state);
1066 *state = new_state;
1067
1068 return changed;
1069}
1070
1071static void
1072weston_wm_window_configure(void *data);
1073
1074static void
1075weston_wm_window_handle_state(struct weston_wm_window *window,
1076 xcb_client_message_event_t *client_message)
1077{
1078 struct weston_wm *wm = window->wm;
1079 struct weston_shell_interface *shell_interface =
1080 &wm->server->compositor->shell_interface;
1081 uint32_t action, property;
1082
1083 action = client_message->data.data32[0];
1084 property = client_message->data.data32[1];
1085
1086 if (property == wm->atom.net_wm_state_fullscreen &&
1087 update_state(action, &window->fullscreen)) {
1088 weston_wm_window_set_net_wm_state(window);
1089 if (window->fullscreen) {
1090 window->saved_width = window->width;
1091 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001092
1093 if (window->shsurf)
1094 shell_interface->set_fullscreen(window->shsurf,
1095 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1096 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001097 } else {
1098 shell_interface->set_toplevel(window->shsurf);
1099 window->width = window->saved_width;
1100 window->height = window->saved_height;
1101 weston_wm_window_configure(window);
1102 }
1103 }
1104}
1105
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001106static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001107weston_wm_handle_client_message(struct weston_wm *wm,
1108 xcb_generic_event_t *event)
1109{
1110 xcb_client_message_event_t *client_message =
1111 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001113
1114 window = hash_table_lookup(wm->window_hash, client_message->window);
1115
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001116 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1117 get_atom_name(wm->conn, client_message->type),
1118 client_message->data.data32[0],
1119 client_message->data.data32[1],
1120 client_message->data.data32[2],
1121 client_message->data.data32[3],
1122 client_message->data.data32[4],
1123 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001125 if (client_message->type == wm->atom.net_wm_moveresize)
1126 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001127 else if (client_message->type == wm->atom.net_wm_state)
1128 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001129}
1130
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001131enum cursor_type {
1132 XWM_CURSOR_TOP,
1133 XWM_CURSOR_BOTTOM,
1134 XWM_CURSOR_LEFT,
1135 XWM_CURSOR_RIGHT,
1136 XWM_CURSOR_TOP_LEFT,
1137 XWM_CURSOR_TOP_RIGHT,
1138 XWM_CURSOR_BOTTOM_LEFT,
1139 XWM_CURSOR_BOTTOM_RIGHT,
1140 XWM_CURSOR_LEFT_PTR,
1141};
1142
1143static const char *cursors[] = {
1144 "top_side",
1145 "bottom_side",
1146 "left_side",
1147 "right_side",
1148 "top_left_corner",
1149 "top_right_corner",
1150 "bottom_left_corner",
1151 "bottom_right_corner",
1152 "left_ptr"
1153};
1154
1155static void
1156weston_wm_create_cursors(struct weston_wm *wm)
1157{
1158 int i, count = ARRAY_LENGTH(cursors);
1159
1160 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1161 for (i = 0; i < count; i++) {
1162 wm->cursors[i] =
1163 xcb_cursor_library_load_cursor(wm, cursors[i]);
1164 }
1165
1166 wm->last_cursor = -1;
1167}
1168
1169static void
1170weston_wm_destroy_cursors(struct weston_wm *wm)
1171{
1172 uint8_t i;
1173
1174 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1175 xcb_free_cursor(wm->conn, wm->cursors[i]);
1176
1177 free(wm->cursors);
1178}
1179
1180static int
1181get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1182{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001183 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001184
1185 switch (location) {
1186 case THEME_LOCATION_RESIZING_TOP:
1187 return XWM_CURSOR_TOP;
1188 case THEME_LOCATION_RESIZING_BOTTOM:
1189 return XWM_CURSOR_BOTTOM;
1190 case THEME_LOCATION_RESIZING_LEFT:
1191 return XWM_CURSOR_LEFT;
1192 case THEME_LOCATION_RESIZING_RIGHT:
1193 return XWM_CURSOR_RIGHT;
1194 case THEME_LOCATION_RESIZING_TOP_LEFT:
1195 return XWM_CURSOR_TOP_LEFT;
1196 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1197 return XWM_CURSOR_TOP_RIGHT;
1198 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1199 return XWM_CURSOR_BOTTOM_LEFT;
1200 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1201 return XWM_CURSOR_BOTTOM_RIGHT;
1202 case THEME_LOCATION_EXTERIOR:
1203 case THEME_LOCATION_TITLEBAR:
1204 default:
1205 return XWM_CURSOR_LEFT_PTR;
1206 }
1207}
1208
1209static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001210weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1211 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001212{
1213 uint32_t cursor_value_list;
1214
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001215 if (wm->last_cursor == cursor)
1216 return;
1217
1218 wm->last_cursor = cursor;
1219
1220 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001221 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001222 XCB_CW_CURSOR, &cursor_value_list);
1223 xcb_flush(wm->conn);
1224}
1225
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001226static void
1227weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1228{
1229 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1230 struct weston_shell_interface *shell_interface =
1231 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001232 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001233 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001234 enum theme_location location;
1235 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001236 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001237
Martin Minarik6d118362012-06-07 18:01:59 +02001238 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001239 button->response_type == XCB_BUTTON_PRESS ?
1240 "PRESS" : "RELEASE", button->detail);
1241
1242 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001243 weston_wm_window_get_frame_size(window, &width, &height);
1244
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001245 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001246 button->detail == 1) {
1247 location = theme_get_location(t,
1248 button->event_x,
1249 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001250 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001251
1252 switch (location) {
1253 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001254 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001255 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001256 case THEME_LOCATION_RESIZING_TOP:
1257 case THEME_LOCATION_RESIZING_BOTTOM:
1258 case THEME_LOCATION_RESIZING_LEFT:
1259 case THEME_LOCATION_RESIZING_RIGHT:
1260 case THEME_LOCATION_RESIZING_TOP_LEFT:
1261 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1262 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1263 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1264 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001265 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001266 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001267 default:
1268 break;
1269 }
1270 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001271}
1272
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001273static void
1274weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1275{
1276 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1277 struct weston_wm_window *window;
1278 int cursor, width, height;
1279
1280 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001281 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001282 return;
1283
1284 weston_wm_window_get_frame_size(window, &width, &height);
1285 cursor = get_cursor_for_location(wm->theme, width, height,
1286 motion->event_x, motion->event_y);
1287
Tiago Vignattic1903232012-07-16 12:15:37 -04001288 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001289}
1290
1291static void
1292weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1293{
1294 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1295 struct weston_wm_window *window;
1296 int cursor, width, height;
1297
1298 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001299 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001300 return;
1301
1302 weston_wm_window_get_frame_size(window, &width, &height);
1303 cursor = get_cursor_for_location(wm->theme, width, height,
1304 enter->event_x, enter->event_y);
1305
Tiago Vignattic1903232012-07-16 12:15:37 -04001306 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001307}
1308
1309static void
1310weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1311{
1312 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1313 struct weston_wm_window *window;
1314
1315 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001316 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001317 return;
1318
Tiago Vignattic1903232012-07-16 12:15:37 -04001319 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001320}
1321
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001322static int
1323weston_wm_handle_event(int fd, uint32_t mask, void *data)
1324{
1325 struct weston_wm *wm = data;
1326 xcb_generic_event_t *event;
1327 int count = 0;
1328
1329 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1330 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001331 free(event);
1332 count++;
1333 continue;
1334 }
1335
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001336 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001337 case XCB_BUTTON_PRESS:
1338 case XCB_BUTTON_RELEASE:
1339 weston_wm_handle_button(wm, event);
1340 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001341 case XCB_ENTER_NOTIFY:
1342 weston_wm_handle_enter(wm, event);
1343 break;
1344 case XCB_LEAVE_NOTIFY:
1345 weston_wm_handle_leave(wm, event);
1346 break;
1347 case XCB_MOTION_NOTIFY:
1348 weston_wm_handle_motion(wm, event);
1349 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001350 case XCB_CREATE_NOTIFY:
1351 weston_wm_handle_create_notify(wm, event);
1352 break;
1353 case XCB_MAP_REQUEST:
1354 weston_wm_handle_map_request(wm, event);
1355 break;
1356 case XCB_MAP_NOTIFY:
1357 weston_wm_handle_map_notify(wm, event);
1358 break;
1359 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001360 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001361 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001362 case XCB_REPARENT_NOTIFY:
1363 weston_wm_handle_reparent_notify(wm, event);
1364 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001365 case XCB_CONFIGURE_REQUEST:
1366 weston_wm_handle_configure_request(wm, event);
1367 break;
1368 case XCB_CONFIGURE_NOTIFY:
1369 weston_wm_handle_configure_notify(wm, event);
1370 break;
1371 case XCB_DESTROY_NOTIFY:
1372 weston_wm_handle_destroy_notify(wm, event);
1373 break;
1374 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001375 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001376 break;
1377 case XCB_PROPERTY_NOTIFY:
1378 weston_wm_handle_property_notify(wm, event);
1379 break;
1380 case XCB_CLIENT_MESSAGE:
1381 weston_wm_handle_client_message(wm, event);
1382 break;
1383 }
1384
1385 free(event);
1386 count++;
1387 }
1388
1389 xcb_flush(wm->conn);
1390
1391 return count;
1392}
1393
1394static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001395weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1396{
1397 xcb_depth_iterator_t d_iter;
1398 xcb_visualtype_iterator_t vt_iter;
1399 xcb_visualtype_t *visualtype;
1400
1401 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1402 visualtype = NULL;
1403 while (d_iter.rem > 0) {
1404 if (d_iter.data->depth == 32) {
1405 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1406 visualtype = vt_iter.data;
1407 break;
1408 }
1409
1410 xcb_depth_next(&d_iter);
1411 }
1412
1413 if (visualtype == NULL) {
1414 weston_log("no 32 bit visualtype\n");
1415 return;
1416 }
1417
1418 wm->visual_id = visualtype->visual_id;
1419 wm->colormap = xcb_generate_id(wm->conn);
1420 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1421 wm->colormap, wm->screen->root, wm->visual_id);
1422}
1423
1424static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001425weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001426{
1427
1428#define F(field) offsetof(struct weston_wm, field)
1429
1430 static const struct { const char *name; int offset; } atoms[] = {
1431 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1432 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1433 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001434 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001435 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001436 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001437 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001438 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001439 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1440 { "_NET_WM_STATE", F(atom.net_wm_state) },
1441 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1442 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1443 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1444 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1445
1446 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1447 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1448 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1449 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1450 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1451 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1452 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001453 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1454 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001455 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1456 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1457 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1458 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1459 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1460
1461 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1462 { "_NET_SUPPORTING_WM_CHECK",
1463 F(atom.net_supporting_wm_check) },
1464 { "_NET_SUPPORTED", F(atom.net_supported) },
1465 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1466 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001467 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001468 { "TARGETS", F(atom.targets) },
1469 { "UTF8_STRING", F(atom.utf8_string) },
1470 { "_WL_SELECTION", F(atom.wl_selection) },
1471 { "INCR", F(atom.incr) },
1472 { "TIMESTAMP", F(atom.timestamp) },
1473 { "MULTIPLE", F(atom.multiple) },
1474 { "UTF8_STRING" , F(atom.utf8_string) },
1475 { "COMPOUND_TEXT", F(atom.compound_text) },
1476 { "TEXT", F(atom.text) },
1477 { "STRING", F(atom.string) },
1478 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1479 { "text/plain", F(atom.text_plain) },
1480 };
1481#undef F
1482
1483 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1484 xcb_xfixes_query_version_reply_t *xfixes_reply;
1485 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1486 xcb_intern_atom_reply_t *reply;
1487 xcb_render_query_pict_formats_reply_t *formats_reply;
1488 xcb_render_query_pict_formats_cookie_t formats_cookie;
1489 xcb_render_pictforminfo_t *formats;
1490 uint32_t i;
1491
1492 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1493
1494 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1495
1496 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1497 cookies[i] = xcb_intern_atom (wm->conn, 0,
1498 strlen(atoms[i].name),
1499 atoms[i].name);
1500
1501 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1502 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1503 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1504 free(reply);
1505 }
1506
1507 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1508 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001509 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001510
1511 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1512 XCB_XFIXES_MAJOR_VERSION,
1513 XCB_XFIXES_MINOR_VERSION);
1514 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1515 xfixes_cookie, NULL);
1516
Martin Minarik6d118362012-06-07 18:01:59 +02001517 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001518 xfixes_reply->major_version, xfixes_reply->minor_version);
1519
1520 free(xfixes_reply);
1521
1522 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1523 formats_cookie, 0);
1524 if (formats_reply == NULL)
1525 return;
1526
1527 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001528 for (i = 0; i < formats_reply->num_formats; i++) {
1529 if (formats[i].direct.red_mask != 0xff &&
1530 formats[i].direct.red_shift != 16)
1531 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001532 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1533 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001534 wm->format_rgb = formats[i];
1535 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1536 formats[i].depth == 32 &&
1537 formats[i].direct.alpha_mask == 0xff &&
1538 formats[i].direct.alpha_shift == 24)
1539 wm->format_rgba = formats[i];
1540 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001541
1542 free(formats_reply);
1543}
1544
1545static void
1546weston_wm_create_wm_window(struct weston_wm *wm)
1547{
1548 static const char name[] = "Weston WM";
1549
1550 wm->wm_window = xcb_generate_id(wm->conn);
1551 xcb_create_window(wm->conn,
1552 XCB_COPY_FROM_PARENT,
1553 wm->wm_window,
1554 wm->screen->root,
1555 0, 0,
1556 10, 10,
1557 0,
1558 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1559 wm->screen->root_visual,
1560 0, NULL);
1561
1562 xcb_change_property(wm->conn,
1563 XCB_PROP_MODE_REPLACE,
1564 wm->wm_window,
1565 wm->atom.net_supporting_wm_check,
1566 XCB_ATOM_WINDOW,
1567 32, /* format */
1568 1, &wm->wm_window);
1569
1570 xcb_change_property(wm->conn,
1571 XCB_PROP_MODE_REPLACE,
1572 wm->wm_window,
1573 wm->atom.net_wm_name,
1574 wm->atom.utf8_string,
1575 8, /* format */
1576 strlen(name), name);
1577
1578 xcb_change_property(wm->conn,
1579 XCB_PROP_MODE_REPLACE,
1580 wm->screen->root,
1581 wm->atom.net_supporting_wm_check,
1582 XCB_ATOM_WINDOW,
1583 32, /* format */
1584 1, &wm->wm_window);
1585
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001586 /* Claim the WM_S0 selection even though we don't suport
1587 * the --replace functionality. */
1588 xcb_set_selection_owner(wm->conn,
1589 wm->wm_window,
1590 wm->atom.wm_s0,
1591 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001592}
1593
1594struct weston_wm *
1595weston_wm_create(struct weston_xserver *wxs)
1596{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001597 struct weston_wm *wm;
1598 struct wl_event_loop *loop;
1599 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001600 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001601 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001602 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001603
1604 wm = malloc(sizeof *wm);
1605 if (wm == NULL)
1606 return NULL;
1607
1608 memset(wm, 0, sizeof *wm);
1609 wm->server = wxs;
1610 wm->window_hash = hash_table_create();
1611 if (wm->window_hash == NULL) {
1612 free(wm);
1613 return NULL;
1614 }
1615
1616 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001617 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001618 hash_table_destroy(wm->window_hash);
1619 free(wm);
1620 return NULL;
1621 }
1622
1623 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001624 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001625 close(sv[1]);
1626
1627 /* xcb_connect_to_fd takes ownership of the fd. */
1628 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1629 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001630 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001631 close(sv[0]);
1632 hash_table_destroy(wm->window_hash);
1633 free(wm);
1634 return NULL;
1635 }
1636
1637 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1638 wm->screen = s.data;
1639
1640 loop = wl_display_get_event_loop(wxs->wl_display);
1641 wm->source =
1642 wl_event_loop_add_fd(loop, sv[0],
1643 WL_EVENT_READABLE,
1644 weston_wm_handle_event, wm);
1645 wl_event_source_check(wm->source);
1646
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001647 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001648 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001649
1650 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001651 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1652 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1653 XCB_EVENT_MASK_PROPERTY_CHANGE;
1654 xcb_change_window_attributes(wm->conn, wm->screen->root,
1655 XCB_CW_EVENT_MASK, values);
1656 wm->theme = theme_create();
1657
1658 weston_wm_create_wm_window(wm);
1659
1660 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001661 supported[1] = wm->atom.net_wm_state;
1662 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001663 xcb_change_property(wm->conn,
1664 XCB_PROP_MODE_REPLACE,
1665 wm->screen->root,
1666 wm->atom.net_supported,
1667 XCB_ATOM_ATOM,
1668 32, /* format */
1669 ARRAY_LENGTH(supported), supported);
1670
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001671 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001672
1673 xcb_flush(wm->conn);
1674
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001675 wm->activate_listener.notify = weston_wm_window_activate;
1676 wl_signal_add(&wxs->compositor->activate_signal,
1677 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001678 wm->kill_listener.notify = weston_wm_kill_client;
1679 wl_signal_add(&wxs->compositor->kill_signal,
1680 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001681
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001682 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001683 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001684
Martin Minarik6d118362012-06-07 18:01:59 +02001685 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001686
1687 return wm;
1688}
1689
1690void
1691weston_wm_destroy(struct weston_wm *wm)
1692{
1693 /* FIXME: Free windows in hash. */
1694 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001695 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001696 xcb_disconnect(wm->conn);
1697 wl_event_source_remove(wm->source);
1698 wl_list_remove(&wm->selection_listener.link);
1699 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001700 wl_list_remove(&wm->kill_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001701
1702 free(wm);
1703}
1704
1705static void
1706surface_destroy(struct wl_listener *listener, void *data)
1707{
1708 struct weston_wm_window *window =
1709 container_of(listener,
1710 struct weston_wm_window, surface_destroy_listener);
1711
Martin Minarik6d118362012-06-07 18:01:59 +02001712 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001713}
1714
1715static struct weston_wm_window *
1716get_wm_window(struct weston_surface *surface)
1717{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718 struct wl_listener *listener;
1719
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001720 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001721 if (listener)
1722 return container_of(listener, struct weston_wm_window,
1723 surface_destroy_listener);
1724
1725 return NULL;
1726}
1727
1728static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001729weston_wm_window_configure(void *data)
1730{
1731 struct weston_wm_window *window = data;
1732 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001733 uint32_t values[4];
1734 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001735
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001736 weston_wm_window_get_child_position(window, &x, &y);
1737 values[0] = x;
1738 values[1] = y;
1739 values[2] = window->width;
1740 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001741 xcb_configure_window(wm->conn,
1742 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001743 XCB_CONFIG_WINDOW_X |
1744 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001745 XCB_CONFIG_WINDOW_WIDTH |
1746 XCB_CONFIG_WINDOW_HEIGHT,
1747 values);
1748
1749 weston_wm_window_get_frame_size(window, &width, &height);
1750 values[0] = width;
1751 values[1] = height;
1752 xcb_configure_window(wm->conn,
1753 window->frame_id,
1754 XCB_CONFIG_WINDOW_WIDTH |
1755 XCB_CONFIG_WINDOW_HEIGHT,
1756 values);
1757
1758 window->configure_source = NULL;
1759
1760 weston_wm_window_schedule_repaint(window);
1761}
1762
1763static void
1764send_configure(struct weston_surface *surface,
1765 uint32_t edges, int32_t width, int32_t height)
1766{
1767 struct weston_wm_window *window = get_wm_window(surface);
1768 struct weston_wm *wm = window->wm;
1769 struct theme *t = window->wm->theme;
1770
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001771 if (window->fullscreen) {
1772 window->width = width;
1773 window->height = height;
1774 } else if (window->decorate) {
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001775 window->width = width - 2 * (t->margin + t->width);
1776 window->height = height - 2 * t->margin -
1777 t->titlebar_height - t->width;
1778 } else {
1779 window->width = width - 2 * t->margin;
1780 window->height = height - 2 * t->margin;
1781 }
1782
1783 if (window->configure_source)
1784 return;
1785
1786 window->configure_source =
1787 wl_event_loop_add_idle(wm->server->loop,
1788 weston_wm_window_configure, window);
1789}
1790
1791static const struct weston_shell_client shell_client = {
1792 send_configure
1793};
1794
1795static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001796xserver_map_shell_surface(struct weston_wm *wm,
1797 struct weston_wm_window *window)
1798{
1799 struct weston_shell_interface *shell_interface =
1800 &wm->server->compositor->shell_interface;
1801 struct weston_wm_window *parent;
1802 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001803 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001804
1805 if (!shell_interface->create_shell_surface)
1806 return;
1807
1808 window->shsurf =
1809 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001810 window->surface,
1811 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001812
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001813 if (window->fullscreen) {
1814 window->saved_width = window->width;
1815 window->saved_height = window->height;
1816 shell_interface->set_fullscreen(window->shsurf,
1817 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1818 0, NULL);
1819 return;
1820 } else if (!window->override_redirect) {
1821 /* ICCCM 4.1.1 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001822 shell_interface->set_toplevel(window->shsurf);
1823 return;
1824 }
1825
Tiago Vignattice1baa82012-07-20 23:09:55 +03001826 /* not all non-toplevel has transient_for set. So we need this
1827 * workaround to guess a parent that will determine the relative
1828 * position of the transient surface */
1829 if (!window->transient_for)
1830 parent_id = wm->focus_latest->id;
1831 else
1832 parent_id = window->transient_for->id;
1833
1834 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001835
1836 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1837 if (!parent->decorate && parent->override_redirect) {
1838 x = parent->x + t->margin;
1839 y = parent->y + t->margin;
1840 }
1841
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001842 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001843 window->x + t->margin - x,
1844 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001845 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1846}
1847
1848static void
1849xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1850 struct wl_resource *surface_resource, uint32_t id)
1851{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001852 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001853 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001854 struct weston_surface *surface =
1855 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001856 struct weston_wm_window *window;
1857
1858 if (client != wxs->client)
1859 return;
1860
1861 window = hash_table_lookup(wm->window_hash, id);
1862 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001863 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001864 return;
1865 }
1866
Martin Minarik6d118362012-06-07 18:01:59 +02001867 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001868
1869 weston_wm_window_read_properties(window);
1870
1871 window->surface = (struct weston_surface *) surface;
1872 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001873 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001874 &window->surface_destroy_listener);
1875
1876 weston_wm_window_schedule_repaint(window);
1877 xserver_map_shell_surface(wm, window);
1878}
1879
1880const struct xserver_interface xserver_implementation = {
1881 xserver_set_window_id
1882};