blob: e656eda37ff53f93d1b2bd05c5bcb7f4a1f0889f [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
MoD31700122013-06-11 19:58:55 -050092#define SEND_EVENT_MASK (0x80)
93#define EVENT_TYPE(event) ((event)->response_type & ~SEND_EVENT_MASK)
94
Kristian Høgsberg380deee2012-05-21 17:12:41 -040095struct weston_wm_window {
96 struct weston_wm *wm;
97 xcb_window_t id;
98 xcb_window_t frame_id;
99 cairo_surface_t *cairo_surface;
100 struct weston_surface *surface;
101 struct shell_surface *shsurf;
102 struct wl_listener surface_destroy_listener;
103 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400104 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300106 int pid;
107 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400108 char *class;
109 char *name;
110 struct weston_wm_window *transient_for;
111 uint32_t protocols;
112 xcb_atom_t type;
113 int width, height;
114 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500115 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400116 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300117 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500118 int fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400119};
120
121static struct weston_wm_window *
122get_wm_window(struct weston_surface *surface);
123
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400124static void
125weston_wm_window_schedule_repaint(struct weston_wm_window *window);
126
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400127const char *
128get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
129{
130 xcb_get_atom_name_cookie_t cookie;
131 xcb_get_atom_name_reply_t *reply;
132 xcb_generic_error_t *e;
133 static char buffer[64];
134
135 if (atom == XCB_ATOM_NONE)
136 return "None";
137
138 cookie = xcb_get_atom_name (c, atom);
139 reply = xcb_get_atom_name_reply (c, cookie, &e);
140 snprintf(buffer, sizeof buffer, "%.*s",
141 xcb_get_atom_name_name_length (reply),
142 xcb_get_atom_name_name (reply));
143 free(reply);
144
145 return buffer;
146}
147
Tiago Vignatti90fada42012-07-16 12:02:08 -0400148static xcb_cursor_t
149xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
150{
151 xcb_connection_t *c = wm->conn;
152 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
153 xcb_screen_t *screen = s.data;
154 xcb_gcontext_t gc;
155 xcb_pixmap_t pix;
156 xcb_render_picture_t pic;
157 xcb_cursor_t cursor;
158 int stride = img->width * 4;
159
160 pix = xcb_generate_id(c);
161 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
162
163 pic = xcb_generate_id(c);
164 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
165
166 gc = xcb_generate_id(c);
167 xcb_create_gc(c, gc, pix, 0, 0);
168
169 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
170 img->width, img->height, 0, 0, 0, 32,
171 stride * img->height, (uint8_t *) img->pixels);
172 xcb_free_gc(c, gc);
173
174 cursor = xcb_generate_id(c);
175 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
176
177 xcb_render_free_picture(c, pic);
178 xcb_free_pixmap(c, pix);
179
180 return cursor;
181}
182
183static xcb_cursor_t
184xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
185{
186 /* TODO: treat animated cursors as well */
187 if (images->nimage != 1)
188 return -1;
189
190 return xcb_cursor_image_load_cursor(wm, images->images[0]);
191}
192
193static xcb_cursor_t
194xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
195{
196 xcb_cursor_t cursor;
197 XcursorImages *images;
198 char *v = NULL;
199 int size = 0;
200
201 if (!file)
202 return 0;
203
204 v = getenv ("XCURSOR_SIZE");
205 if (v)
206 size = atoi(v);
207
208 if (!size)
209 size = 32;
210
211 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300212 if (!images)
213 return -1;
214
Tiago Vignatti90fada42012-07-16 12:02:08 -0400215 cursor = xcb_cursor_images_load_cursor (wm, images);
216 XcursorImagesDestroy (images);
217
218 return cursor;
219}
220
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400221void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400222dump_property(struct weston_wm *wm,
223 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400224{
225 int32_t *incr_value;
226 const char *text_value, *name;
227 xcb_atom_t *atom_value;
228 int width, len;
229 uint32_t i;
230
Martin Minarik6d118362012-06-07 18:01:59 +0200231 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400232 if (reply == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200233 weston_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400234 return;
235 }
236
Martin Minarik6d118362012-06-07 18:01:59 +0200237 width += weston_log_continue(
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400238 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400239 get_atom_name(wm->conn, reply->type),
240 reply->format,
241 xcb_get_property_value_length(reply),
242 reply->value_len);
243
244 if (reply->type == wm->atom.incr) {
245 incr_value = xcb_get_property_value(reply);
Martin Minarik6d118362012-06-07 18:01:59 +0200246 weston_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400247 } else if (reply->type == wm->atom.utf8_string ||
248 reply->type == wm->atom.string) {
249 text_value = xcb_get_property_value(reply);
250 if (reply->value_len > 40)
251 len = 40;
252 else
253 len = reply->value_len;
Martin Minarik6d118362012-06-07 18:01:59 +0200254 weston_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400255 } else if (reply->type == XCB_ATOM_ATOM) {
256 atom_value = xcb_get_property_value(reply);
257 for (i = 0; i < reply->value_len; i++) {
258 name = get_atom_name(wm->conn, atom_value[i]);
259 if (width + strlen(name) + 2 > 78) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400261 width = 4;
262 } else if (i > 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200263 width += weston_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400264 }
265
Martin Minarik6d118362012-06-07 18:01:59 +0200266 width += weston_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400267 }
Martin Minarik6d118362012-06-07 18:01:59 +0200268 weston_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400269 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200270 weston_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400271 }
272}
273
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200274static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400275read_and_dump_property(struct weston_wm *wm,
276 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400277{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400278 xcb_get_property_reply_t *reply;
279 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400280
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400281 cookie = xcb_get_property(wm->conn, 0, window,
282 property, XCB_ATOM_ANY, 0, 2048);
283 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400284
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400285 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400286
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400287 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400288}
289
290/* We reuse some predefined, but otherwise useles atoms */
291#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
292#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500293#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400294
295static void
296weston_wm_window_read_properties(struct weston_wm_window *window)
297{
298 struct weston_wm *wm = window->wm;
299
300#define F(field) offsetof(struct weston_wm_window, field)
301 const struct {
302 xcb_atom_t atom;
303 xcb_atom_t type;
304 int offset;
305 } props[] = {
306 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
307 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
308 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
309 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500310 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400311 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
312 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300313 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400314 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300315 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400316 };
317#undef F
318
319 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
320 xcb_get_property_reply_t *reply;
321 void *p;
322 uint32_t *xid;
323 xcb_atom_t *atom;
324 uint32_t i;
325 struct motif_wm_hints *hints;
326
327 if (!window->properties_dirty)
328 return;
329 window->properties_dirty = 0;
330
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400331 for (i = 0; i < ARRAY_LENGTH(props); i++)
332 cookie[i] = xcb_get_property(wm->conn,
333 0, /* delete */
334 window->id,
335 props[i].atom,
336 XCB_ATOM_ANY, 0, 2048);
337
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300338 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400339 for (i = 0; i < ARRAY_LENGTH(props); i++) {
340 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
341 if (!reply)
342 /* Bad window, typically */
343 continue;
344 if (reply->type == XCB_ATOM_NONE) {
345 /* No such property */
346 free(reply);
347 continue;
348 }
349
350 p = ((char *) window + props[i].offset);
351
352 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300353 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354 case XCB_ATOM_STRING:
355 /* FIXME: We're using this for both string and
356 utf8_string */
357 if (*(char **) p)
358 free(*(char **) p);
359
360 *(char **) p =
361 strndup(xcb_get_property_value(reply),
362 xcb_get_property_value_length(reply));
363 break;
364 case XCB_ATOM_WINDOW:
365 xid = xcb_get_property_value(reply);
366 *(struct weston_wm_window **) p =
367 hash_table_lookup(wm->window_hash, *xid);
368 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300369 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370 case XCB_ATOM_ATOM:
371 atom = xcb_get_property_value(reply);
372 *(xcb_atom_t *) p = *atom;
373 break;
374 case TYPE_WM_PROTOCOLS:
375 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500376 case TYPE_NET_WM_STATE:
377 window->fullscreen = 0;
378 atom = xcb_get_property_value(reply);
379 for (i = 0; i < reply->value_len; i++)
380 if (atom[i] == wm->atom.net_wm_state_fullscreen)
381 window->fullscreen = 1;
382 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383 case TYPE_MOTIF_WM_HINTS:
384 hints = xcb_get_property_value(reply);
385 if (hints->flags & MWM_HINTS_DECORATIONS)
386 window->decorate = hints->decorations > 0;
387 break;
388 default:
389 break;
390 }
391 free(reply);
392 }
393}
394
395static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400396weston_wm_window_get_frame_size(struct weston_wm_window *window,
397 int *width, int *height)
398{
399 struct theme *t = window->wm->theme;
400
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500401 if (window->fullscreen) {
402 *width = window->width;
403 *height = window->height;
404 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400405 *width = window->width + (t->margin + t->width) * 2;
406 *height = window->height +
407 t->margin * 2 + t->width + t->titlebar_height;
408 } else {
409 *width = window->width + t->margin * 2;
410 *height = window->height + t->margin * 2;
411 }
412}
413
414static void
415weston_wm_window_get_child_position(struct weston_wm_window *window,
416 int *x, int *y)
417{
418 struct theme *t = window->wm->theme;
419
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500420 if (window->fullscreen) {
421 *x = 0;
422 *y = 0;
423 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424 *x = t->margin + t->width;
425 *y = t->margin + t->titlebar_height;
426 } else {
427 *x = t->margin;
428 *y = t->margin;
429 }
430}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400431
432static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500433weston_wm_window_send_configure_notify(struct weston_wm_window *window)
434{
435 xcb_configure_notify_event_t configure_notify;
436 struct weston_wm *wm = window->wm;
437 int x, y;
438
439 weston_wm_window_get_child_position(window, &x, &y);
440 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
441 configure_notify.pad0 = 0;
442 configure_notify.event = window->id;
443 configure_notify.window = window->id;
444 configure_notify.above_sibling = XCB_WINDOW_NONE;
445 configure_notify.x = x;
446 configure_notify.y = y;
447 configure_notify.width = window->width;
448 configure_notify.height = window->height;
449 configure_notify.border_width = 0;
450 configure_notify.override_redirect = 0;
451 configure_notify.pad1 = 0;
452
453 xcb_send_event(wm->conn, 0, window->id,
454 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
455 (char *) &configure_notify);
456}
457
458static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400459weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
460{
461 xcb_configure_request_event_t *configure_request =
462 (xcb_configure_request_event_t *) event;
463 struct weston_wm_window *window;
464 uint32_t mask, values[16];
465 int x, y, width, height, i = 0;
466
Martin Minarik6d118362012-06-07 18:01:59 +0200467 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400468 configure_request->window,
469 configure_request->x, configure_request->y,
470 configure_request->width, configure_request->height);
471
472 window = hash_table_lookup(wm->window_hash, configure_request->window);
473
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500474 if (window->fullscreen) {
475 weston_wm_window_send_configure_notify(window);
476 return;
477 }
478
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400479 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
480 window->width = configure_request->width;
481 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
482 window->height = configure_request->height;
483
484 weston_wm_window_get_child_position(window, &x, &y);
485 values[i++] = x;
486 values[i++] = y;
487 values[i++] = window->width;
488 values[i++] = window->height;
489 values[i++] = 0;
490 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
491 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
492 XCB_CONFIG_WINDOW_BORDER_WIDTH;
493 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
494 values[i++] = configure_request->sibling;
495 mask |= XCB_CONFIG_WINDOW_SIBLING;
496 }
497 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
498 values[i++] = configure_request->stack_mode;
499 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
500 }
501
502 xcb_configure_window(wm->conn, window->id, mask, values);
503
504 weston_wm_window_get_frame_size(window, &width, &height);
505 values[0] = width;
506 values[1] = height;
507 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
508 xcb_configure_window(wm->conn, window->frame_id, mask, values);
509
510 weston_wm_window_schedule_repaint(window);
511}
512
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400513static void
514weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
515{
516 xcb_configure_notify_event_t *configure_notify =
517 (xcb_configure_notify_event_t *) event;
518 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300519 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400520
521 window = hash_table_lookup(wm->window_hash, configure_notify->window);
522
Martin Minarik6d118362012-06-07 18:01:59 +0200523 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400524 configure_notify->window == window->id ? "client" : "frame",
525 configure_notify->window,
526 configure_notify->x, configure_notify->y,
527 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300528
529 /* resize falls here */
530 if (configure_notify->window != window->id)
531 return;
532
533 weston_wm_window_get_child_position(window, &x, &y);
534 window->x = configure_notify->x - x;
535 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400536}
537
538static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300539weston_wm_kill_client(struct wl_listener *listener, void *data)
540{
541 struct weston_surface *surface = data;
542 struct weston_wm_window *window = get_wm_window(surface);
543 char name[1024];
544
545 if (!window)
546 return;
547
548 gethostname(name, 1024);
549
550 /* this is only one heuristic to guess the PID of a client is valid,
551 * assuming it's compliant with icccm and ewmh. Non-compliants and
552 * remote applications of course fail. */
553 if (!strcmp(window->machine, name) && window->pid != 0)
554 kill(window->pid, SIGKILL);
555}
556
557static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400558weston_wm_window_activate(struct wl_listener *listener, void *data)
559{
560 struct weston_surface *surface = data;
561 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600562 struct weston_wm *wm =
563 container_of(listener, struct weston_wm, activate_listener);
564 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400565
Scott Moreau85ecac02012-05-21 15:49:14 -0600566 if (window) {
567 client_message.response_type = XCB_CLIENT_MESSAGE;
568 client_message.format = 32;
569 client_message.window = window->id;
570 client_message.type = wm->atom.wm_protocols;
571 client_message.data.data32[0] = wm->atom.wm_take_focus;
572 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
573
574 xcb_send_event(wm->conn, 0, window->id,
575 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
576 (char *) &client_message);
577
578 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
579 window->id, XCB_TIME_CURRENT_TIME);
580 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400581 xcb_set_input_focus (wm->conn,
582 XCB_INPUT_FOCUS_POINTER_ROOT,
583 XCB_NONE,
584 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600585 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400586
587 if (wm->focus_window)
588 weston_wm_window_schedule_repaint(wm->focus_window);
589 wm->focus_window = window;
Tiago Vignattice1baa82012-07-20 23:09:55 +0300590 if (window)
591 wm->focus_latest = window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400592 if (wm->focus_window)
593 weston_wm_window_schedule_repaint(wm->focus_window);
594}
595
596static int
597our_resource(struct weston_wm *wm, uint32_t id)
598{
599 const xcb_setup_t *setup;
600
601 setup = xcb_get_setup(wm->conn);
602
603 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
604}
605
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400606#define ICCCM_WITHDRAWN_STATE 0
607#define ICCCM_NORMAL_STATE 1
608#define ICCCM_ICONIC_STATE 3
609
610static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500611weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400612{
613 struct weston_wm *wm = window->wm;
614 uint32_t property[2];
615
616 property[0] = state;
617 property[1] = XCB_WINDOW_NONE;
618
619 xcb_change_property(wm->conn,
620 XCB_PROP_MODE_REPLACE,
621 window->id,
622 wm->atom.wm_state,
623 wm->atom.wm_state,
624 32, /* format */
625 2, property);
626}
627
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400628static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500629weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
630{
631 struct weston_wm *wm = window->wm;
632 uint32_t property[1];
633 int i;
634
635 i = 0;
636 if (window->fullscreen)
637 property[i++] = wm->atom.net_wm_state_fullscreen;
638
639 xcb_change_property(wm->conn,
640 XCB_PROP_MODE_REPLACE,
641 window->id,
642 wm->atom.net_wm_state,
643 XCB_ATOM_ATOM,
644 32, /* format */
645 i, property);
646}
647
648static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400649weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
650{
651 xcb_map_request_event_t *map_request =
652 (xcb_map_request_event_t *) event;
653 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400654 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400655 int x, y, width, height;
656
657 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200658 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400659 map_request->window);
660 return;
661 }
662
663 window = hash_table_lookup(wm->window_hash, map_request->window);
664
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400665 if (window->frame_id)
666 return;
667
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400668 weston_wm_window_read_properties(window);
669
670 weston_wm_window_get_frame_size(window, &width, &height);
671 weston_wm_window_get_child_position(window, &x, &y);
672
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400673 values[0] = wm->screen->black_pixel;
674 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400675 XCB_EVENT_MASK_KEY_PRESS |
676 XCB_EVENT_MASK_KEY_RELEASE |
677 XCB_EVENT_MASK_BUTTON_PRESS |
678 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400679 XCB_EVENT_MASK_POINTER_MOTION |
680 XCB_EVENT_MASK_ENTER_WINDOW |
681 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400682 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400683 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400684 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400685
686 window->frame_id = xcb_generate_id(wm->conn);
687 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400688 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400689 window->frame_id,
690 wm->screen->root,
691 0, 0,
692 width, height,
693 0,
694 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400695 wm->visual_id,
696 XCB_CW_BORDER_PIXEL |
697 XCB_CW_EVENT_MASK |
698 XCB_CW_COLORMAP, values);
699
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400700 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
701
702 values[0] = 0;
703 xcb_configure_window(wm->conn, window->id,
704 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
705
Martin Minarik6d118362012-06-07 18:01:59 +0200706 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400707 window->id, window, window->frame_id);
708
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500709 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
710 weston_wm_window_set_net_wm_state(window);
711
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400712 xcb_map_window(wm->conn, map_request->window);
713 xcb_map_window(wm->conn, window->frame_id);
714
715 window->cairo_surface =
716 cairo_xcb_surface_create_with_xrender_format(wm->conn,
717 wm->screen,
718 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400719 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400720 width, height);
721
722 hash_table_insert(wm->window_hash, window->frame_id, window);
723}
724
725static void
726weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
727{
728 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
729
730 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200731 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732 map_notify->window);
733 return;
734 }
735
Martin Minarik6d118362012-06-07 18:01:59 +0200736 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737}
738
739static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400740weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
741{
742 xcb_unmap_notify_event_t *unmap_notify =
743 (xcb_unmap_notify_event_t *) event;
744 struct weston_wm_window *window;
745
Martin Minarik6d118362012-06-07 18:01:59 +0200746 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400747 unmap_notify->window,
748 unmap_notify->event,
749 our_resource(wm, unmap_notify->window) ? ", ours" : "");
750
751 if (our_resource(wm, unmap_notify->window))
752 return;
753
MoD31700122013-06-11 19:58:55 -0500754 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400755 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
756 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400757 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400758
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400759 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400760 if (window->repaint_source)
761 wl_event_source_remove(window->repaint_source);
762 if (window->cairo_surface)
763 cairo_surface_destroy(window->cairo_surface);
764
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400765 if (window->frame_id) {
766 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
767 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500768 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400769 hash_table_remove(wm->window_hash, window->frame_id);
770 window->frame_id = XCB_WINDOW_NONE;
771 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400772
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400773 if (wm->focus_window == window)
774 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400775 if (window->surface)
776 wl_list_remove(&window->surface_destroy_listener.link);
777 window->surface = NULL;
778}
779
780static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400781weston_wm_window_draw_decoration(void *data)
782{
783 struct weston_wm_window *window = data;
784 struct weston_wm *wm = window->wm;
785 struct theme *t = wm->theme;
786 cairo_t *cr;
787 int x, y, width, height;
788 const char *title;
789 uint32_t flags = 0;
790
791 weston_wm_window_read_properties(window);
792
793 window->repaint_source = NULL;
794
795 weston_wm_window_get_frame_size(window, &width, &height);
796 weston_wm_window_get_child_position(window, &x, &y);
797
798 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
799 cr = cairo_create(window->cairo_surface);
800
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500801 if (window->fullscreen) {
802 /* nothing */
803 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400804 if (wm->focus_window == window)
805 flags |= THEME_FRAME_ACTIVE;
806
807 if (window->name)
808 title = window->name;
809 else
810 title = "untitled";
811
812 theme_render_frame(t, cr, width, height, title, flags);
813 } else {
814 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
815 cairo_set_source_rgba(cr, 0, 0, 0, 0);
816 cairo_paint(cr);
817
818 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
819 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
820 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
821 }
822
823 cairo_destroy(cr);
824
825 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700826 pixman_region32_fini(&window->surface->pending.opaque);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400827 /* We leave an extra pixel around the X window area to
828 * make sure we don't sample from the undefined alpha
829 * channel when filtering. */
Kristian Høgsberg25bb6962013-02-14 22:01:04 -0500830 pixman_region32_init_rect(&window->surface->pending.opaque,
831 x - 1, y - 1,
832 window->width + 2,
833 window->height + 2);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200834 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500835 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400836
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500837 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500838 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500839 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840 t->margin, t->margin,
841 width - 2 * t->margin,
842 height - 2 * t->margin);
843 }
844}
845
846static void
847weston_wm_window_schedule_repaint(struct weston_wm_window *window)
848{
849 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300850 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400851
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400852 if (window->frame_id == XCB_WINDOW_NONE) {
853 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300854 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700855 pixman_region32_fini(&window->surface->pending.opaque);
856 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300857 width, height);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200858 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400859 }
860 return;
861 }
862
863 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400864 return;
865
866 window->repaint_source =
867 wl_event_loop_add_idle(wm->server->loop,
868 weston_wm_window_draw_decoration,
869 window);
870}
871
872static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400873weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
874{
875 xcb_property_notify_event_t *property_notify =
876 (xcb_property_notify_event_t *) event;
877 struct weston_wm_window *window;
878
879 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000880 if (!window)
881 return;
882
883 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400884
Martin Minarik6d118362012-06-07 18:01:59 +0200885 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400886 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400887 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200888 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400889 else
890 read_and_dump_property(wm, property_notify->window,
891 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400892
893 if (property_notify->atom == wm->atom.net_wm_name ||
894 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400895 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400896}
897
898static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400899weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300900 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400901{
902 struct weston_wm_window *window;
903 uint32_t values[1];
904
905 window = malloc(sizeof *window);
906 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200907 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400908 return;
909 }
910
911 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
912 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
913
914 memset(window, 0, sizeof *window);
915 window->wm = wm;
916 window->id = id;
917 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300918 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400919 window->width = width;
920 window->height = height;
921
922 hash_table_insert(wm->window_hash, id, window);
923}
924
925static void
926weston_wm_window_destroy(struct weston_wm_window *window)
927{
928 hash_table_remove(window->wm->window_hash, window->id);
929 free(window);
930}
931
932static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400933weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
934{
935 xcb_create_notify_event_t *create_notify =
936 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400937
Martin Minarik6d118362012-06-07 18:01:59 +0200938 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400939 create_notify->window,
940 create_notify->width, create_notify->height,
941 create_notify->override_redirect ? ", override" : "",
942 our_resource(wm, create_notify->window) ? ", ours" : "");
943
944 if (our_resource(wm, create_notify->window))
945 return;
946
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400947 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300948 create_notify->width, create_notify->height,
949 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400950}
951
952static void
953weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
954{
955 xcb_destroy_notify_event_t *destroy_notify =
956 (xcb_destroy_notify_event_t *) event;
957 struct weston_wm_window *window;
958
Martin Minarik6d118362012-06-07 18:01:59 +0200959 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400960 destroy_notify->window,
961 destroy_notify->event,
962 our_resource(wm, destroy_notify->window) ? ", ours" : "");
963
964 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400965 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400966
967 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400968 weston_wm_window_destroy(window);
969}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400970
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400971static void
972weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
973{
974 xcb_reparent_notify_event_t *reparent_notify =
975 (xcb_reparent_notify_event_t *) event;
976 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400977
Martin Minarik6d118362012-06-07 18:01:59 +0200978 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400979 reparent_notify->window,
980 reparent_notify->parent,
981 reparent_notify->event);
982
983 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300984 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
985 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400986 } else if (!our_resource(wm, reparent_notify->parent)) {
987 window = hash_table_lookup(wm->window_hash,
988 reparent_notify->window);
989 weston_wm_window_destroy(window);
990 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400991}
992
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400993struct weston_seat *
994weston_wm_pick_seat(struct weston_wm *wm)
995{
996 return container_of(wm->server->compositor->seat_list.next,
997 struct weston_seat, link);
998}
999
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001000static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001001weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1002 xcb_client_message_event_t *client_message)
1003{
1004 static const int map[] = {
1005 THEME_LOCATION_RESIZING_TOP_LEFT,
1006 THEME_LOCATION_RESIZING_TOP,
1007 THEME_LOCATION_RESIZING_TOP_RIGHT,
1008 THEME_LOCATION_RESIZING_RIGHT,
1009 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1010 THEME_LOCATION_RESIZING_BOTTOM,
1011 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1012 THEME_LOCATION_RESIZING_LEFT
1013 };
1014
1015 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001016 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001017 int detail;
1018 struct weston_shell_interface *shell_interface =
1019 &wm->server->compositor->shell_interface;
1020
Kristian Høgsberge3148752013-05-06 23:19:49 -04001021 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001022 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001023 return;
1024
1025 detail = client_message->data.data32[2];
1026 switch (detail) {
1027 case _NET_WM_MOVERESIZE_MOVE:
1028 shell_interface->move(window->shsurf, seat);
1029 break;
1030 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1031 case _NET_WM_MOVERESIZE_SIZE_TOP:
1032 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1033 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1034 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1035 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1036 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1037 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1038 shell_interface->resize(window->shsurf, seat, map[detail]);
1039 break;
1040 case _NET_WM_MOVERESIZE_CANCEL:
1041 break;
1042 }
1043}
1044
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001045#define _NET_WM_STATE_REMOVE 0
1046#define _NET_WM_STATE_ADD 1
1047#define _NET_WM_STATE_TOGGLE 2
1048
1049static int
1050update_state(int action, int *state)
1051{
1052 int new_state, changed;
1053
1054 switch (action) {
1055 case _NET_WM_STATE_REMOVE:
1056 new_state = 0;
1057 break;
1058 case _NET_WM_STATE_ADD:
1059 new_state = 1;
1060 break;
1061 case _NET_WM_STATE_TOGGLE:
1062 new_state = !*state;
1063 break;
1064 default:
1065 return 0;
1066 }
1067
1068 changed = (*state != new_state);
1069 *state = new_state;
1070
1071 return changed;
1072}
1073
1074static void
1075weston_wm_window_configure(void *data);
1076
1077static void
1078weston_wm_window_handle_state(struct weston_wm_window *window,
1079 xcb_client_message_event_t *client_message)
1080{
1081 struct weston_wm *wm = window->wm;
1082 struct weston_shell_interface *shell_interface =
1083 &wm->server->compositor->shell_interface;
1084 uint32_t action, property;
1085
1086 action = client_message->data.data32[0];
1087 property = client_message->data.data32[1];
1088
1089 if (property == wm->atom.net_wm_state_fullscreen &&
1090 update_state(action, &window->fullscreen)) {
1091 weston_wm_window_set_net_wm_state(window);
1092 if (window->fullscreen) {
1093 window->saved_width = window->width;
1094 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001095
1096 if (window->shsurf)
1097 shell_interface->set_fullscreen(window->shsurf,
1098 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1099 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001100 } else {
1101 shell_interface->set_toplevel(window->shsurf);
1102 window->width = window->saved_width;
1103 window->height = window->saved_height;
1104 weston_wm_window_configure(window);
1105 }
1106 }
1107}
1108
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001109static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001110weston_wm_handle_client_message(struct weston_wm *wm,
1111 xcb_generic_event_t *event)
1112{
1113 xcb_client_message_event_t *client_message =
1114 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001116
1117 window = hash_table_lookup(wm->window_hash, client_message->window);
1118
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001119 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1120 get_atom_name(wm->conn, client_message->type),
1121 client_message->data.data32[0],
1122 client_message->data.data32[1],
1123 client_message->data.data32[2],
1124 client_message->data.data32[3],
1125 client_message->data.data32[4],
1126 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001127
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001128 if (client_message->type == wm->atom.net_wm_moveresize)
1129 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001130 else if (client_message->type == wm->atom.net_wm_state)
1131 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001132}
1133
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001134enum cursor_type {
1135 XWM_CURSOR_TOP,
1136 XWM_CURSOR_BOTTOM,
1137 XWM_CURSOR_LEFT,
1138 XWM_CURSOR_RIGHT,
1139 XWM_CURSOR_TOP_LEFT,
1140 XWM_CURSOR_TOP_RIGHT,
1141 XWM_CURSOR_BOTTOM_LEFT,
1142 XWM_CURSOR_BOTTOM_RIGHT,
1143 XWM_CURSOR_LEFT_PTR,
1144};
1145
1146static const char *cursors[] = {
1147 "top_side",
1148 "bottom_side",
1149 "left_side",
1150 "right_side",
1151 "top_left_corner",
1152 "top_right_corner",
1153 "bottom_left_corner",
1154 "bottom_right_corner",
1155 "left_ptr"
1156};
1157
1158static void
1159weston_wm_create_cursors(struct weston_wm *wm)
1160{
1161 int i, count = ARRAY_LENGTH(cursors);
1162
1163 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1164 for (i = 0; i < count; i++) {
1165 wm->cursors[i] =
1166 xcb_cursor_library_load_cursor(wm, cursors[i]);
1167 }
1168
1169 wm->last_cursor = -1;
1170}
1171
1172static void
1173weston_wm_destroy_cursors(struct weston_wm *wm)
1174{
1175 uint8_t i;
1176
1177 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1178 xcb_free_cursor(wm->conn, wm->cursors[i]);
1179
1180 free(wm->cursors);
1181}
1182
1183static int
1184get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1185{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001186 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001187
1188 switch (location) {
1189 case THEME_LOCATION_RESIZING_TOP:
1190 return XWM_CURSOR_TOP;
1191 case THEME_LOCATION_RESIZING_BOTTOM:
1192 return XWM_CURSOR_BOTTOM;
1193 case THEME_LOCATION_RESIZING_LEFT:
1194 return XWM_CURSOR_LEFT;
1195 case THEME_LOCATION_RESIZING_RIGHT:
1196 return XWM_CURSOR_RIGHT;
1197 case THEME_LOCATION_RESIZING_TOP_LEFT:
1198 return XWM_CURSOR_TOP_LEFT;
1199 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1200 return XWM_CURSOR_TOP_RIGHT;
1201 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1202 return XWM_CURSOR_BOTTOM_LEFT;
1203 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1204 return XWM_CURSOR_BOTTOM_RIGHT;
1205 case THEME_LOCATION_EXTERIOR:
1206 case THEME_LOCATION_TITLEBAR:
1207 default:
1208 return XWM_CURSOR_LEFT_PTR;
1209 }
1210}
1211
1212static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001213weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1214 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001215{
1216 uint32_t cursor_value_list;
1217
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001218 if (wm->last_cursor == cursor)
1219 return;
1220
1221 wm->last_cursor = cursor;
1222
1223 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001224 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001225 XCB_CW_CURSOR, &cursor_value_list);
1226 xcb_flush(wm->conn);
1227}
1228
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001229static void
1230weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1231{
1232 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1233 struct weston_shell_interface *shell_interface =
1234 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001235 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001236 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001237 enum theme_location location;
1238 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001239 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240
Martin Minarik6d118362012-06-07 18:01:59 +02001241 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001242 button->response_type == XCB_BUTTON_PRESS ?
1243 "PRESS" : "RELEASE", button->detail);
1244
1245 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001246 weston_wm_window_get_frame_size(window, &width, &height);
1247
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001248 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001249 button->detail == 1) {
1250 location = theme_get_location(t,
1251 button->event_x,
1252 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001253 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001254
1255 switch (location) {
1256 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001257 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001258 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001259 case THEME_LOCATION_RESIZING_TOP:
1260 case THEME_LOCATION_RESIZING_BOTTOM:
1261 case THEME_LOCATION_RESIZING_LEFT:
1262 case THEME_LOCATION_RESIZING_RIGHT:
1263 case THEME_LOCATION_RESIZING_TOP_LEFT:
1264 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1265 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1266 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1267 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001268 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001269 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001270 default:
1271 break;
1272 }
1273 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001274}
1275
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001276static void
1277weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1278{
1279 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1280 struct weston_wm_window *window;
1281 int cursor, width, height;
1282
1283 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001284 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001285 return;
1286
1287 weston_wm_window_get_frame_size(window, &width, &height);
1288 cursor = get_cursor_for_location(wm->theme, width, height,
1289 motion->event_x, motion->event_y);
1290
Tiago Vignattic1903232012-07-16 12:15:37 -04001291 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001292}
1293
1294static void
1295weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1296{
1297 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1298 struct weston_wm_window *window;
1299 int cursor, width, height;
1300
1301 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001302 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001303 return;
1304
1305 weston_wm_window_get_frame_size(window, &width, &height);
1306 cursor = get_cursor_for_location(wm->theme, width, height,
1307 enter->event_x, enter->event_y);
1308
Tiago Vignattic1903232012-07-16 12:15:37 -04001309 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001310}
1311
1312static void
1313weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1314{
1315 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1316 struct weston_wm_window *window;
1317
1318 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001319 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001320 return;
1321
Tiago Vignattic1903232012-07-16 12:15:37 -04001322 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001323}
1324
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001325static int
1326weston_wm_handle_event(int fd, uint32_t mask, void *data)
1327{
1328 struct weston_wm *wm = data;
1329 xcb_generic_event_t *event;
1330 int count = 0;
1331
1332 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1333 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001334 free(event);
1335 count++;
1336 continue;
1337 }
1338
MoD31700122013-06-11 19:58:55 -05001339 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001340 case XCB_BUTTON_PRESS:
1341 case XCB_BUTTON_RELEASE:
1342 weston_wm_handle_button(wm, event);
1343 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001344 case XCB_ENTER_NOTIFY:
1345 weston_wm_handle_enter(wm, event);
1346 break;
1347 case XCB_LEAVE_NOTIFY:
1348 weston_wm_handle_leave(wm, event);
1349 break;
1350 case XCB_MOTION_NOTIFY:
1351 weston_wm_handle_motion(wm, event);
1352 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001353 case XCB_CREATE_NOTIFY:
1354 weston_wm_handle_create_notify(wm, event);
1355 break;
1356 case XCB_MAP_REQUEST:
1357 weston_wm_handle_map_request(wm, event);
1358 break;
1359 case XCB_MAP_NOTIFY:
1360 weston_wm_handle_map_notify(wm, event);
1361 break;
1362 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001363 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001364 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001365 case XCB_REPARENT_NOTIFY:
1366 weston_wm_handle_reparent_notify(wm, event);
1367 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001368 case XCB_CONFIGURE_REQUEST:
1369 weston_wm_handle_configure_request(wm, event);
1370 break;
1371 case XCB_CONFIGURE_NOTIFY:
1372 weston_wm_handle_configure_notify(wm, event);
1373 break;
1374 case XCB_DESTROY_NOTIFY:
1375 weston_wm_handle_destroy_notify(wm, event);
1376 break;
1377 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001378 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001379 break;
1380 case XCB_PROPERTY_NOTIFY:
1381 weston_wm_handle_property_notify(wm, event);
1382 break;
1383 case XCB_CLIENT_MESSAGE:
1384 weston_wm_handle_client_message(wm, event);
1385 break;
1386 }
1387
1388 free(event);
1389 count++;
1390 }
1391
1392 xcb_flush(wm->conn);
1393
1394 return count;
1395}
1396
1397static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001398weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1399{
1400 xcb_depth_iterator_t d_iter;
1401 xcb_visualtype_iterator_t vt_iter;
1402 xcb_visualtype_t *visualtype;
1403
1404 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1405 visualtype = NULL;
1406 while (d_iter.rem > 0) {
1407 if (d_iter.data->depth == 32) {
1408 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1409 visualtype = vt_iter.data;
1410 break;
1411 }
1412
1413 xcb_depth_next(&d_iter);
1414 }
1415
1416 if (visualtype == NULL) {
1417 weston_log("no 32 bit visualtype\n");
1418 return;
1419 }
1420
1421 wm->visual_id = visualtype->visual_id;
1422 wm->colormap = xcb_generate_id(wm->conn);
1423 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1424 wm->colormap, wm->screen->root, wm->visual_id);
1425}
1426
1427static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001428weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001429{
1430
1431#define F(field) offsetof(struct weston_wm, field)
1432
1433 static const struct { const char *name; int offset; } atoms[] = {
1434 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1435 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1436 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001437 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001438 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001439 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001440 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001441 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001442 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1443 { "_NET_WM_STATE", F(atom.net_wm_state) },
1444 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1445 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1446 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1447 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1448
1449 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1450 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1451 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1452 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1453 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1454 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1455 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001456 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1457 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001458 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1459 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1460 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1461 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1462 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1463
1464 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1465 { "_NET_SUPPORTING_WM_CHECK",
1466 F(atom.net_supporting_wm_check) },
1467 { "_NET_SUPPORTED", F(atom.net_supported) },
1468 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1469 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001470 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001471 { "TARGETS", F(atom.targets) },
1472 { "UTF8_STRING", F(atom.utf8_string) },
1473 { "_WL_SELECTION", F(atom.wl_selection) },
1474 { "INCR", F(atom.incr) },
1475 { "TIMESTAMP", F(atom.timestamp) },
1476 { "MULTIPLE", F(atom.multiple) },
1477 { "UTF8_STRING" , F(atom.utf8_string) },
1478 { "COMPOUND_TEXT", F(atom.compound_text) },
1479 { "TEXT", F(atom.text) },
1480 { "STRING", F(atom.string) },
1481 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1482 { "text/plain", F(atom.text_plain) },
1483 };
1484#undef F
1485
1486 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1487 xcb_xfixes_query_version_reply_t *xfixes_reply;
1488 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1489 xcb_intern_atom_reply_t *reply;
1490 xcb_render_query_pict_formats_reply_t *formats_reply;
1491 xcb_render_query_pict_formats_cookie_t formats_cookie;
1492 xcb_render_pictforminfo_t *formats;
1493 uint32_t i;
1494
1495 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1496
1497 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1498
1499 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1500 cookies[i] = xcb_intern_atom (wm->conn, 0,
1501 strlen(atoms[i].name),
1502 atoms[i].name);
1503
1504 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1505 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1506 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1507 free(reply);
1508 }
1509
1510 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1511 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001512 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001513
1514 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1515 XCB_XFIXES_MAJOR_VERSION,
1516 XCB_XFIXES_MINOR_VERSION);
1517 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1518 xfixes_cookie, NULL);
1519
Martin Minarik6d118362012-06-07 18:01:59 +02001520 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001521 xfixes_reply->major_version, xfixes_reply->minor_version);
1522
1523 free(xfixes_reply);
1524
1525 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1526 formats_cookie, 0);
1527 if (formats_reply == NULL)
1528 return;
1529
1530 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001531 for (i = 0; i < formats_reply->num_formats; i++) {
1532 if (formats[i].direct.red_mask != 0xff &&
1533 formats[i].direct.red_shift != 16)
1534 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1536 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001537 wm->format_rgb = formats[i];
1538 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1539 formats[i].depth == 32 &&
1540 formats[i].direct.alpha_mask == 0xff &&
1541 formats[i].direct.alpha_shift == 24)
1542 wm->format_rgba = formats[i];
1543 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001544
1545 free(formats_reply);
1546}
1547
1548static void
1549weston_wm_create_wm_window(struct weston_wm *wm)
1550{
1551 static const char name[] = "Weston WM";
1552
1553 wm->wm_window = xcb_generate_id(wm->conn);
1554 xcb_create_window(wm->conn,
1555 XCB_COPY_FROM_PARENT,
1556 wm->wm_window,
1557 wm->screen->root,
1558 0, 0,
1559 10, 10,
1560 0,
1561 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1562 wm->screen->root_visual,
1563 0, NULL);
1564
1565 xcb_change_property(wm->conn,
1566 XCB_PROP_MODE_REPLACE,
1567 wm->wm_window,
1568 wm->atom.net_supporting_wm_check,
1569 XCB_ATOM_WINDOW,
1570 32, /* format */
1571 1, &wm->wm_window);
1572
1573 xcb_change_property(wm->conn,
1574 XCB_PROP_MODE_REPLACE,
1575 wm->wm_window,
1576 wm->atom.net_wm_name,
1577 wm->atom.utf8_string,
1578 8, /* format */
1579 strlen(name), name);
1580
1581 xcb_change_property(wm->conn,
1582 XCB_PROP_MODE_REPLACE,
1583 wm->screen->root,
1584 wm->atom.net_supporting_wm_check,
1585 XCB_ATOM_WINDOW,
1586 32, /* format */
1587 1, &wm->wm_window);
1588
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001589 /* Claim the WM_S0 selection even though we don't suport
1590 * the --replace functionality. */
1591 xcb_set_selection_owner(wm->conn,
1592 wm->wm_window,
1593 wm->atom.wm_s0,
1594 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001595}
1596
1597struct weston_wm *
1598weston_wm_create(struct weston_xserver *wxs)
1599{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001600 struct weston_wm *wm;
1601 struct wl_event_loop *loop;
1602 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001603 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001604 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001605 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001606
1607 wm = malloc(sizeof *wm);
1608 if (wm == NULL)
1609 return NULL;
1610
1611 memset(wm, 0, sizeof *wm);
1612 wm->server = wxs;
1613 wm->window_hash = hash_table_create();
1614 if (wm->window_hash == NULL) {
1615 free(wm);
1616 return NULL;
1617 }
1618
1619 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001620 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001621 hash_table_destroy(wm->window_hash);
1622 free(wm);
1623 return NULL;
1624 }
1625
1626 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001627 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001628 close(sv[1]);
1629
1630 /* xcb_connect_to_fd takes ownership of the fd. */
1631 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1632 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001633 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001634 close(sv[0]);
1635 hash_table_destroy(wm->window_hash);
1636 free(wm);
1637 return NULL;
1638 }
1639
1640 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1641 wm->screen = s.data;
1642
1643 loop = wl_display_get_event_loop(wxs->wl_display);
1644 wm->source =
1645 wl_event_loop_add_fd(loop, sv[0],
1646 WL_EVENT_READABLE,
1647 weston_wm_handle_event, wm);
1648 wl_event_source_check(wm->source);
1649
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001650 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001651 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001652
1653 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001654 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1655 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1656 XCB_EVENT_MASK_PROPERTY_CHANGE;
1657 xcb_change_window_attributes(wm->conn, wm->screen->root,
1658 XCB_CW_EVENT_MASK, values);
1659 wm->theme = theme_create();
1660
1661 weston_wm_create_wm_window(wm);
1662
1663 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001664 supported[1] = wm->atom.net_wm_state;
1665 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001666 xcb_change_property(wm->conn,
1667 XCB_PROP_MODE_REPLACE,
1668 wm->screen->root,
1669 wm->atom.net_supported,
1670 XCB_ATOM_ATOM,
1671 32, /* format */
1672 ARRAY_LENGTH(supported), supported);
1673
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001674 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001675
1676 xcb_flush(wm->conn);
1677
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001678 wm->activate_listener.notify = weston_wm_window_activate;
1679 wl_signal_add(&wxs->compositor->activate_signal,
1680 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001681 wm->kill_listener.notify = weston_wm_kill_client;
1682 wl_signal_add(&wxs->compositor->kill_signal,
1683 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001684
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001685 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001686 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001687
Martin Minarik6d118362012-06-07 18:01:59 +02001688 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001689
1690 return wm;
1691}
1692
1693void
1694weston_wm_destroy(struct weston_wm *wm)
1695{
1696 /* FIXME: Free windows in hash. */
1697 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001698 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001699 xcb_disconnect(wm->conn);
1700 wl_event_source_remove(wm->source);
1701 wl_list_remove(&wm->selection_listener.link);
1702 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001703 wl_list_remove(&wm->kill_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001704
1705 free(wm);
1706}
1707
1708static void
1709surface_destroy(struct wl_listener *listener, void *data)
1710{
1711 struct weston_wm_window *window =
1712 container_of(listener,
1713 struct weston_wm_window, surface_destroy_listener);
1714
Martin Minarik6d118362012-06-07 18:01:59 +02001715 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001716}
1717
1718static struct weston_wm_window *
1719get_wm_window(struct weston_surface *surface)
1720{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001721 struct wl_listener *listener;
1722
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001723 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001724 if (listener)
1725 return container_of(listener, struct weston_wm_window,
1726 surface_destroy_listener);
1727
1728 return NULL;
1729}
1730
1731static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001732weston_wm_window_configure(void *data)
1733{
1734 struct weston_wm_window *window = data;
1735 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001736 uint32_t values[4];
1737 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001738
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001739 weston_wm_window_get_child_position(window, &x, &y);
1740 values[0] = x;
1741 values[1] = y;
1742 values[2] = window->width;
1743 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001744 xcb_configure_window(wm->conn,
1745 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001746 XCB_CONFIG_WINDOW_X |
1747 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001748 XCB_CONFIG_WINDOW_WIDTH |
1749 XCB_CONFIG_WINDOW_HEIGHT,
1750 values);
1751
1752 weston_wm_window_get_frame_size(window, &width, &height);
1753 values[0] = width;
1754 values[1] = height;
1755 xcb_configure_window(wm->conn,
1756 window->frame_id,
1757 XCB_CONFIG_WINDOW_WIDTH |
1758 XCB_CONFIG_WINDOW_HEIGHT,
1759 values);
1760
1761 window->configure_source = NULL;
1762
1763 weston_wm_window_schedule_repaint(window);
1764}
1765
1766static void
1767send_configure(struct weston_surface *surface,
1768 uint32_t edges, int32_t width, int32_t height)
1769{
1770 struct weston_wm_window *window = get_wm_window(surface);
1771 struct weston_wm *wm = window->wm;
1772 struct theme *t = window->wm->theme;
1773
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001774 if (window->fullscreen) {
1775 window->width = width;
1776 window->height = height;
1777 } else if (window->decorate) {
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001778 window->width = width - 2 * (t->margin + t->width);
1779 window->height = height - 2 * t->margin -
1780 t->titlebar_height - t->width;
1781 } else {
1782 window->width = width - 2 * t->margin;
1783 window->height = height - 2 * t->margin;
1784 }
1785
1786 if (window->configure_source)
1787 return;
1788
1789 window->configure_source =
1790 wl_event_loop_add_idle(wm->server->loop,
1791 weston_wm_window_configure, window);
1792}
1793
1794static const struct weston_shell_client shell_client = {
1795 send_configure
1796};
1797
1798static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001799xserver_map_shell_surface(struct weston_wm *wm,
1800 struct weston_wm_window *window)
1801{
1802 struct weston_shell_interface *shell_interface =
1803 &wm->server->compositor->shell_interface;
1804 struct weston_wm_window *parent;
1805 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001806 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001807
1808 if (!shell_interface->create_shell_surface)
1809 return;
1810
1811 window->shsurf =
1812 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001813 window->surface,
1814 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001815
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001816 if (window->fullscreen) {
1817 window->saved_width = window->width;
1818 window->saved_height = window->height;
1819 shell_interface->set_fullscreen(window->shsurf,
1820 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1821 0, NULL);
1822 return;
1823 } else if (!window->override_redirect) {
1824 /* ICCCM 4.1.1 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001825 shell_interface->set_toplevel(window->shsurf);
1826 return;
1827 }
1828
Tiago Vignattice1baa82012-07-20 23:09:55 +03001829 /* not all non-toplevel has transient_for set. So we need this
1830 * workaround to guess a parent that will determine the relative
1831 * position of the transient surface */
1832 if (!window->transient_for)
1833 parent_id = wm->focus_latest->id;
1834 else
1835 parent_id = window->transient_for->id;
1836
1837 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001838
1839 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1840 if (!parent->decorate && parent->override_redirect) {
1841 x = parent->x + t->margin;
1842 y = parent->y + t->margin;
1843 }
1844
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001845 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001846 window->x + t->margin - x,
1847 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001848 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1849}
1850
1851static void
1852xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1853 struct wl_resource *surface_resource, uint32_t id)
1854{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001855 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001856 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001857 struct weston_surface *surface =
1858 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001859 struct weston_wm_window *window;
1860
1861 if (client != wxs->client)
1862 return;
1863
1864 window = hash_table_lookup(wm->window_hash, id);
1865 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001866 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001867 return;
1868 }
1869
Martin Minarik6d118362012-06-07 18:01:59 +02001870 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001871
1872 weston_wm_window_read_properties(window);
1873
1874 window->surface = (struct weston_surface *) surface;
1875 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001876 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001877 &window->surface_destroy_listener);
1878
1879 weston_wm_window_schedule_repaint(window);
1880 xserver_map_shell_surface(wm, window);
1881}
1882
1883const struct xserver_interface xserver_implementation = {
1884 xserver_set_window_id
1885};