blob: 3fc56337ad4de4ac29e25dce9a2b0abf1ed71ca0 [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);
MoD55375b92013-06-11 19:59:42 -0500140
141 if(reply) {
142 snprintf(buffer, sizeof buffer, "%.*s",
143 xcb_get_atom_name_name_length (reply),
144 xcb_get_atom_name_name (reply));
145 } else {
146 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
147 }
148
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400149 free(reply);
150
151 return buffer;
152}
153
Tiago Vignatti90fada42012-07-16 12:02:08 -0400154static xcb_cursor_t
155xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
156{
157 xcb_connection_t *c = wm->conn;
158 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
159 xcb_screen_t *screen = s.data;
160 xcb_gcontext_t gc;
161 xcb_pixmap_t pix;
162 xcb_render_picture_t pic;
163 xcb_cursor_t cursor;
164 int stride = img->width * 4;
165
166 pix = xcb_generate_id(c);
167 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
168
169 pic = xcb_generate_id(c);
170 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
171
172 gc = xcb_generate_id(c);
173 xcb_create_gc(c, gc, pix, 0, 0);
174
175 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
176 img->width, img->height, 0, 0, 0, 32,
177 stride * img->height, (uint8_t *) img->pixels);
178 xcb_free_gc(c, gc);
179
180 cursor = xcb_generate_id(c);
181 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
182
183 xcb_render_free_picture(c, pic);
184 xcb_free_pixmap(c, pix);
185
186 return cursor;
187}
188
189static xcb_cursor_t
190xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
191{
192 /* TODO: treat animated cursors as well */
193 if (images->nimage != 1)
194 return -1;
195
196 return xcb_cursor_image_load_cursor(wm, images->images[0]);
197}
198
199static xcb_cursor_t
200xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
201{
202 xcb_cursor_t cursor;
203 XcursorImages *images;
204 char *v = NULL;
205 int size = 0;
206
207 if (!file)
208 return 0;
209
210 v = getenv ("XCURSOR_SIZE");
211 if (v)
212 size = atoi(v);
213
214 if (!size)
215 size = 32;
216
217 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300218 if (!images)
219 return -1;
220
Tiago Vignatti90fada42012-07-16 12:02:08 -0400221 cursor = xcb_cursor_images_load_cursor (wm, images);
222 XcursorImagesDestroy (images);
223
224 return cursor;
225}
226
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400227void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400228dump_property(struct weston_wm *wm,
229 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400230{
231 int32_t *incr_value;
232 const char *text_value, *name;
233 xcb_atom_t *atom_value;
234 int width, len;
235 uint32_t i;
236
Martin Minarik6d118362012-06-07 18:01:59 +0200237 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400238 if (reply == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200239 weston_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400240 return;
241 }
242
Martin Minarik6d118362012-06-07 18:01:59 +0200243 width += weston_log_continue(
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400244 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400245 get_atom_name(wm->conn, reply->type),
246 reply->format,
247 xcb_get_property_value_length(reply),
248 reply->value_len);
249
250 if (reply->type == wm->atom.incr) {
251 incr_value = xcb_get_property_value(reply);
Martin Minarik6d118362012-06-07 18:01:59 +0200252 weston_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400253 } else if (reply->type == wm->atom.utf8_string ||
254 reply->type == wm->atom.string) {
255 text_value = xcb_get_property_value(reply);
256 if (reply->value_len > 40)
257 len = 40;
258 else
259 len = reply->value_len;
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400261 } else if (reply->type == XCB_ATOM_ATOM) {
262 atom_value = xcb_get_property_value(reply);
263 for (i = 0; i < reply->value_len; i++) {
264 name = get_atom_name(wm->conn, atom_value[i]);
265 if (width + strlen(name) + 2 > 78) {
Martin Minarik6d118362012-06-07 18:01:59 +0200266 weston_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400267 width = 4;
268 } else if (i > 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200269 width += weston_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400270 }
271
Martin Minarik6d118362012-06-07 18:01:59 +0200272 width += weston_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400273 }
Martin Minarik6d118362012-06-07 18:01:59 +0200274 weston_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400275 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200276 weston_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400277 }
278}
279
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200280static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400281read_and_dump_property(struct weston_wm *wm,
282 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400283{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400284 xcb_get_property_reply_t *reply;
285 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400286
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400287 cookie = xcb_get_property(wm->conn, 0, window,
288 property, XCB_ATOM_ANY, 0, 2048);
289 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400290
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400291 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400292
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400293 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400294}
295
296/* We reuse some predefined, but otherwise useles atoms */
297#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
298#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500299#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400300
301static void
302weston_wm_window_read_properties(struct weston_wm_window *window)
303{
304 struct weston_wm *wm = window->wm;
305
306#define F(field) offsetof(struct weston_wm_window, field)
307 const struct {
308 xcb_atom_t atom;
309 xcb_atom_t type;
310 int offset;
311 } props[] = {
312 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
313 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
314 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
315 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500316 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400317 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
318 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300319 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300321 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400322 };
323#undef F
324
325 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
326 xcb_get_property_reply_t *reply;
327 void *p;
328 uint32_t *xid;
329 xcb_atom_t *atom;
330 uint32_t i;
331 struct motif_wm_hints *hints;
332
333 if (!window->properties_dirty)
334 return;
335 window->properties_dirty = 0;
336
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337 for (i = 0; i < ARRAY_LENGTH(props); i++)
338 cookie[i] = xcb_get_property(wm->conn,
339 0, /* delete */
340 window->id,
341 props[i].atom,
342 XCB_ATOM_ANY, 0, 2048);
343
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300344 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400345 for (i = 0; i < ARRAY_LENGTH(props); i++) {
346 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
347 if (!reply)
348 /* Bad window, typically */
349 continue;
350 if (reply->type == XCB_ATOM_NONE) {
351 /* No such property */
352 free(reply);
353 continue;
354 }
355
356 p = ((char *) window + props[i].offset);
357
358 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300359 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400360 case XCB_ATOM_STRING:
361 /* FIXME: We're using this for both string and
362 utf8_string */
363 if (*(char **) p)
364 free(*(char **) p);
365
366 *(char **) p =
367 strndup(xcb_get_property_value(reply),
368 xcb_get_property_value_length(reply));
369 break;
370 case XCB_ATOM_WINDOW:
371 xid = xcb_get_property_value(reply);
372 *(struct weston_wm_window **) p =
373 hash_table_lookup(wm->window_hash, *xid);
374 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300375 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400376 case XCB_ATOM_ATOM:
377 atom = xcb_get_property_value(reply);
378 *(xcb_atom_t *) p = *atom;
379 break;
380 case TYPE_WM_PROTOCOLS:
381 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500382 case TYPE_NET_WM_STATE:
383 window->fullscreen = 0;
384 atom = xcb_get_property_value(reply);
385 for (i = 0; i < reply->value_len; i++)
386 if (atom[i] == wm->atom.net_wm_state_fullscreen)
387 window->fullscreen = 1;
388 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389 case TYPE_MOTIF_WM_HINTS:
390 hints = xcb_get_property_value(reply);
391 if (hints->flags & MWM_HINTS_DECORATIONS)
392 window->decorate = hints->decorations > 0;
393 break;
394 default:
395 break;
396 }
397 free(reply);
398 }
399}
400
401static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400402weston_wm_window_get_frame_size(struct weston_wm_window *window,
403 int *width, int *height)
404{
405 struct theme *t = window->wm->theme;
406
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500407 if (window->fullscreen) {
408 *width = window->width;
409 *height = window->height;
410 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 *width = window->width + (t->margin + t->width) * 2;
412 *height = window->height +
413 t->margin * 2 + t->width + t->titlebar_height;
414 } else {
415 *width = window->width + t->margin * 2;
416 *height = window->height + t->margin * 2;
417 }
418}
419
420static void
421weston_wm_window_get_child_position(struct weston_wm_window *window,
422 int *x, int *y)
423{
424 struct theme *t = window->wm->theme;
425
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500426 if (window->fullscreen) {
427 *x = 0;
428 *y = 0;
429 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400430 *x = t->margin + t->width;
431 *y = t->margin + t->titlebar_height;
432 } else {
433 *x = t->margin;
434 *y = t->margin;
435 }
436}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400437
438static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500439weston_wm_window_send_configure_notify(struct weston_wm_window *window)
440{
441 xcb_configure_notify_event_t configure_notify;
442 struct weston_wm *wm = window->wm;
443 int x, y;
444
445 weston_wm_window_get_child_position(window, &x, &y);
446 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
447 configure_notify.pad0 = 0;
448 configure_notify.event = window->id;
449 configure_notify.window = window->id;
450 configure_notify.above_sibling = XCB_WINDOW_NONE;
451 configure_notify.x = x;
452 configure_notify.y = y;
453 configure_notify.width = window->width;
454 configure_notify.height = window->height;
455 configure_notify.border_width = 0;
456 configure_notify.override_redirect = 0;
457 configure_notify.pad1 = 0;
458
459 xcb_send_event(wm->conn, 0, window->id,
460 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
461 (char *) &configure_notify);
462}
463
464static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400465weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
466{
467 xcb_configure_request_event_t *configure_request =
468 (xcb_configure_request_event_t *) event;
469 struct weston_wm_window *window;
470 uint32_t mask, values[16];
471 int x, y, width, height, i = 0;
472
Martin Minarik6d118362012-06-07 18:01:59 +0200473 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400474 configure_request->window,
475 configure_request->x, configure_request->y,
476 configure_request->width, configure_request->height);
477
478 window = hash_table_lookup(wm->window_hash, configure_request->window);
479
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500480 if (window->fullscreen) {
481 weston_wm_window_send_configure_notify(window);
482 return;
483 }
484
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400485 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
486 window->width = configure_request->width;
487 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
488 window->height = configure_request->height;
489
490 weston_wm_window_get_child_position(window, &x, &y);
491 values[i++] = x;
492 values[i++] = y;
493 values[i++] = window->width;
494 values[i++] = window->height;
495 values[i++] = 0;
496 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
497 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
498 XCB_CONFIG_WINDOW_BORDER_WIDTH;
499 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
500 values[i++] = configure_request->sibling;
501 mask |= XCB_CONFIG_WINDOW_SIBLING;
502 }
503 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
504 values[i++] = configure_request->stack_mode;
505 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
506 }
507
508 xcb_configure_window(wm->conn, window->id, mask, values);
509
510 weston_wm_window_get_frame_size(window, &width, &height);
511 values[0] = width;
512 values[1] = height;
513 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
514 xcb_configure_window(wm->conn, window->frame_id, mask, values);
515
516 weston_wm_window_schedule_repaint(window);
517}
518
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400519static void
520weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
521{
522 xcb_configure_notify_event_t *configure_notify =
523 (xcb_configure_notify_event_t *) event;
524 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300525 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400526
527 window = hash_table_lookup(wm->window_hash, configure_notify->window);
528
Martin Minarik6d118362012-06-07 18:01:59 +0200529 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400530 configure_notify->window == window->id ? "client" : "frame",
531 configure_notify->window,
532 configure_notify->x, configure_notify->y,
533 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300534
535 /* resize falls here */
536 if (configure_notify->window != window->id)
537 return;
538
539 weston_wm_window_get_child_position(window, &x, &y);
540 window->x = configure_notify->x - x;
541 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400542}
543
544static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300545weston_wm_kill_client(struct wl_listener *listener, void *data)
546{
547 struct weston_surface *surface = data;
548 struct weston_wm_window *window = get_wm_window(surface);
549 char name[1024];
550
551 if (!window)
552 return;
553
554 gethostname(name, 1024);
555
556 /* this is only one heuristic to guess the PID of a client is valid,
557 * assuming it's compliant with icccm and ewmh. Non-compliants and
558 * remote applications of course fail. */
559 if (!strcmp(window->machine, name) && window->pid != 0)
560 kill(window->pid, SIGKILL);
561}
562
563static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400564weston_wm_window_activate(struct wl_listener *listener, void *data)
565{
566 struct weston_surface *surface = data;
567 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600568 struct weston_wm *wm =
569 container_of(listener, struct weston_wm, activate_listener);
570 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400571
Scott Moreau85ecac02012-05-21 15:49:14 -0600572 if (window) {
573 client_message.response_type = XCB_CLIENT_MESSAGE;
574 client_message.format = 32;
575 client_message.window = window->id;
576 client_message.type = wm->atom.wm_protocols;
577 client_message.data.data32[0] = wm->atom.wm_take_focus;
578 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
579
580 xcb_send_event(wm->conn, 0, window->id,
581 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
582 (char *) &client_message);
583
584 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
585 window->id, XCB_TIME_CURRENT_TIME);
586 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400587 xcb_set_input_focus (wm->conn,
588 XCB_INPUT_FOCUS_POINTER_ROOT,
589 XCB_NONE,
590 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600591 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400592
593 if (wm->focus_window)
594 weston_wm_window_schedule_repaint(wm->focus_window);
595 wm->focus_window = window;
Tiago Vignattice1baa82012-07-20 23:09:55 +0300596 if (window)
597 wm->focus_latest = window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400598 if (wm->focus_window)
599 weston_wm_window_schedule_repaint(wm->focus_window);
600}
601
602static int
603our_resource(struct weston_wm *wm, uint32_t id)
604{
605 const xcb_setup_t *setup;
606
607 setup = xcb_get_setup(wm->conn);
608
609 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
610}
611
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400612#define ICCCM_WITHDRAWN_STATE 0
613#define ICCCM_NORMAL_STATE 1
614#define ICCCM_ICONIC_STATE 3
615
616static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500617weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400618{
619 struct weston_wm *wm = window->wm;
620 uint32_t property[2];
621
622 property[0] = state;
623 property[1] = XCB_WINDOW_NONE;
624
625 xcb_change_property(wm->conn,
626 XCB_PROP_MODE_REPLACE,
627 window->id,
628 wm->atom.wm_state,
629 wm->atom.wm_state,
630 32, /* format */
631 2, property);
632}
633
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400634static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500635weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
636{
637 struct weston_wm *wm = window->wm;
638 uint32_t property[1];
639 int i;
640
641 i = 0;
642 if (window->fullscreen)
643 property[i++] = wm->atom.net_wm_state_fullscreen;
644
645 xcb_change_property(wm->conn,
646 XCB_PROP_MODE_REPLACE,
647 window->id,
648 wm->atom.net_wm_state,
649 XCB_ATOM_ATOM,
650 32, /* format */
651 i, property);
652}
653
654static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400655weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
656{
657 xcb_map_request_event_t *map_request =
658 (xcb_map_request_event_t *) event;
659 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400660 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400661 int x, y, width, height;
662
663 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200664 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400665 map_request->window);
666 return;
667 }
668
669 window = hash_table_lookup(wm->window_hash, map_request->window);
670
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400671 if (window->frame_id)
672 return;
673
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400674 weston_wm_window_read_properties(window);
675
676 weston_wm_window_get_frame_size(window, &width, &height);
677 weston_wm_window_get_child_position(window, &x, &y);
678
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400679 values[0] = wm->screen->black_pixel;
680 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400681 XCB_EVENT_MASK_KEY_PRESS |
682 XCB_EVENT_MASK_KEY_RELEASE |
683 XCB_EVENT_MASK_BUTTON_PRESS |
684 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400685 XCB_EVENT_MASK_POINTER_MOTION |
686 XCB_EVENT_MASK_ENTER_WINDOW |
687 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400688 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400689 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400690 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400691
692 window->frame_id = xcb_generate_id(wm->conn);
693 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400694 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400695 window->frame_id,
696 wm->screen->root,
697 0, 0,
698 width, height,
699 0,
700 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400701 wm->visual_id,
702 XCB_CW_BORDER_PIXEL |
703 XCB_CW_EVENT_MASK |
704 XCB_CW_COLORMAP, values);
705
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400706 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
707
708 values[0] = 0;
709 xcb_configure_window(wm->conn, window->id,
710 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
711
Martin Minarik6d118362012-06-07 18:01:59 +0200712 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400713 window->id, window, window->frame_id);
714
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500715 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
716 weston_wm_window_set_net_wm_state(window);
717
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400718 xcb_map_window(wm->conn, map_request->window);
719 xcb_map_window(wm->conn, window->frame_id);
720
721 window->cairo_surface =
722 cairo_xcb_surface_create_with_xrender_format(wm->conn,
723 wm->screen,
724 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400725 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400726 width, height);
727
728 hash_table_insert(wm->window_hash, window->frame_id, window);
729}
730
731static void
732weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
733{
734 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
735
736 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200737 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400738 map_notify->window);
739 return;
740 }
741
Martin Minarik6d118362012-06-07 18:01:59 +0200742 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743}
744
745static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400746weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
747{
748 xcb_unmap_notify_event_t *unmap_notify =
749 (xcb_unmap_notify_event_t *) event;
750 struct weston_wm_window *window;
751
Martin Minarik6d118362012-06-07 18:01:59 +0200752 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400753 unmap_notify->window,
754 unmap_notify->event,
755 our_resource(wm, unmap_notify->window) ? ", ours" : "");
756
757 if (our_resource(wm, unmap_notify->window))
758 return;
759
MoD31700122013-06-11 19:58:55 -0500760 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400761 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
762 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400763 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400764
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400765 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400766 if (window->repaint_source)
767 wl_event_source_remove(window->repaint_source);
768 if (window->cairo_surface)
769 cairo_surface_destroy(window->cairo_surface);
770
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400771 if (window->frame_id) {
772 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
773 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500774 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400775 hash_table_remove(wm->window_hash, window->frame_id);
776 window->frame_id = XCB_WINDOW_NONE;
777 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400778
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400779 if (wm->focus_window == window)
780 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400781 if (window->surface)
782 wl_list_remove(&window->surface_destroy_listener.link);
783 window->surface = NULL;
784}
785
786static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400787weston_wm_window_draw_decoration(void *data)
788{
789 struct weston_wm_window *window = data;
790 struct weston_wm *wm = window->wm;
791 struct theme *t = wm->theme;
792 cairo_t *cr;
793 int x, y, width, height;
794 const char *title;
795 uint32_t flags = 0;
796
797 weston_wm_window_read_properties(window);
798
799 window->repaint_source = NULL;
800
801 weston_wm_window_get_frame_size(window, &width, &height);
802 weston_wm_window_get_child_position(window, &x, &y);
803
804 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
805 cr = cairo_create(window->cairo_surface);
806
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500807 if (window->fullscreen) {
808 /* nothing */
809 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400810 if (wm->focus_window == window)
811 flags |= THEME_FRAME_ACTIVE;
812
813 if (window->name)
814 title = window->name;
815 else
816 title = "untitled";
817
818 theme_render_frame(t, cr, width, height, title, flags);
819 } else {
820 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
821 cairo_set_source_rgba(cr, 0, 0, 0, 0);
822 cairo_paint(cr);
823
824 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
825 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
826 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
827 }
828
829 cairo_destroy(cr);
830
831 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700832 pixman_region32_fini(&window->surface->pending.opaque);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400833 /* We leave an extra pixel around the X window area to
834 * make sure we don't sample from the undefined alpha
835 * channel when filtering. */
Kristian Høgsberg25bb6962013-02-14 22:01:04 -0500836 pixman_region32_init_rect(&window->surface->pending.opaque,
837 x - 1, y - 1,
838 window->width + 2,
839 window->height + 2);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200840 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500841 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400842
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500843 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500844 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500845 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400846 t->margin, t->margin,
847 width - 2 * t->margin,
848 height - 2 * t->margin);
849 }
850}
851
852static void
853weston_wm_window_schedule_repaint(struct weston_wm_window *window)
854{
855 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300856 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400857
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400858 if (window->frame_id == XCB_WINDOW_NONE) {
859 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300860 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700861 pixman_region32_fini(&window->surface->pending.opaque);
862 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300863 width, height);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200864 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400865 }
866 return;
867 }
868
869 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400870 return;
871
872 window->repaint_source =
873 wl_event_loop_add_idle(wm->server->loop,
874 weston_wm_window_draw_decoration,
875 window);
876}
877
878static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400879weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
880{
881 xcb_property_notify_event_t *property_notify =
882 (xcb_property_notify_event_t *) event;
883 struct weston_wm_window *window;
884
885 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000886 if (!window)
887 return;
888
889 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400890
Martin Minarik6d118362012-06-07 18:01:59 +0200891 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400892 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400893 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200894 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400895 else
896 read_and_dump_property(wm, property_notify->window,
897 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400898
899 if (property_notify->atom == wm->atom.net_wm_name ||
900 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400901 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400902}
903
904static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400905weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300906 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400907{
908 struct weston_wm_window *window;
909 uint32_t values[1];
910
911 window = malloc(sizeof *window);
912 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200913 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400914 return;
915 }
916
917 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
918 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
919
920 memset(window, 0, sizeof *window);
921 window->wm = wm;
922 window->id = id;
923 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300924 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400925 window->width = width;
926 window->height = height;
927
928 hash_table_insert(wm->window_hash, id, window);
929}
930
931static void
932weston_wm_window_destroy(struct weston_wm_window *window)
933{
934 hash_table_remove(window->wm->window_hash, window->id);
935 free(window);
936}
937
938static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400939weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
940{
941 xcb_create_notify_event_t *create_notify =
942 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400943
Martin Minarik6d118362012-06-07 18:01:59 +0200944 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945 create_notify->window,
946 create_notify->width, create_notify->height,
947 create_notify->override_redirect ? ", override" : "",
948 our_resource(wm, create_notify->window) ? ", ours" : "");
949
950 if (our_resource(wm, create_notify->window))
951 return;
952
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400953 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300954 create_notify->width, create_notify->height,
955 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400956}
957
958static void
959weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
960{
961 xcb_destroy_notify_event_t *destroy_notify =
962 (xcb_destroy_notify_event_t *) event;
963 struct weston_wm_window *window;
964
Martin Minarik6d118362012-06-07 18:01:59 +0200965 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400966 destroy_notify->window,
967 destroy_notify->event,
968 our_resource(wm, destroy_notify->window) ? ", ours" : "");
969
970 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400971 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972
973 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400974 weston_wm_window_destroy(window);
975}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400976
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400977static void
978weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
979{
980 xcb_reparent_notify_event_t *reparent_notify =
981 (xcb_reparent_notify_event_t *) event;
982 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400983
Martin Minarik6d118362012-06-07 18:01:59 +0200984 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400985 reparent_notify->window,
986 reparent_notify->parent,
987 reparent_notify->event);
988
989 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300990 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
991 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400992 } else if (!our_resource(wm, reparent_notify->parent)) {
993 window = hash_table_lookup(wm->window_hash,
994 reparent_notify->window);
995 weston_wm_window_destroy(window);
996 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400997}
998
Kristian Høgsberg5ba31892012-08-10 10:06:59 -0400999struct weston_seat *
1000weston_wm_pick_seat(struct weston_wm *wm)
1001{
1002 return container_of(wm->server->compositor->seat_list.next,
1003 struct weston_seat, link);
1004}
1005
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001006static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001007weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1008 xcb_client_message_event_t *client_message)
1009{
1010 static const int map[] = {
1011 THEME_LOCATION_RESIZING_TOP_LEFT,
1012 THEME_LOCATION_RESIZING_TOP,
1013 THEME_LOCATION_RESIZING_TOP_RIGHT,
1014 THEME_LOCATION_RESIZING_RIGHT,
1015 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1016 THEME_LOCATION_RESIZING_BOTTOM,
1017 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1018 THEME_LOCATION_RESIZING_LEFT
1019 };
1020
1021 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001022 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001023 int detail;
1024 struct weston_shell_interface *shell_interface =
1025 &wm->server->compositor->shell_interface;
1026
Kristian Høgsberge3148752013-05-06 23:19:49 -04001027 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001028 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001029 return;
1030
1031 detail = client_message->data.data32[2];
1032 switch (detail) {
1033 case _NET_WM_MOVERESIZE_MOVE:
1034 shell_interface->move(window->shsurf, seat);
1035 break;
1036 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1037 case _NET_WM_MOVERESIZE_SIZE_TOP:
1038 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1039 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1040 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1041 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1042 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1043 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1044 shell_interface->resize(window->shsurf, seat, map[detail]);
1045 break;
1046 case _NET_WM_MOVERESIZE_CANCEL:
1047 break;
1048 }
1049}
1050
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001051#define _NET_WM_STATE_REMOVE 0
1052#define _NET_WM_STATE_ADD 1
1053#define _NET_WM_STATE_TOGGLE 2
1054
1055static int
1056update_state(int action, int *state)
1057{
1058 int new_state, changed;
1059
1060 switch (action) {
1061 case _NET_WM_STATE_REMOVE:
1062 new_state = 0;
1063 break;
1064 case _NET_WM_STATE_ADD:
1065 new_state = 1;
1066 break;
1067 case _NET_WM_STATE_TOGGLE:
1068 new_state = !*state;
1069 break;
1070 default:
1071 return 0;
1072 }
1073
1074 changed = (*state != new_state);
1075 *state = new_state;
1076
1077 return changed;
1078}
1079
1080static void
1081weston_wm_window_configure(void *data);
1082
1083static void
1084weston_wm_window_handle_state(struct weston_wm_window *window,
1085 xcb_client_message_event_t *client_message)
1086{
1087 struct weston_wm *wm = window->wm;
1088 struct weston_shell_interface *shell_interface =
1089 &wm->server->compositor->shell_interface;
1090 uint32_t action, property;
1091
1092 action = client_message->data.data32[0];
1093 property = client_message->data.data32[1];
1094
1095 if (property == wm->atom.net_wm_state_fullscreen &&
1096 update_state(action, &window->fullscreen)) {
1097 weston_wm_window_set_net_wm_state(window);
1098 if (window->fullscreen) {
1099 window->saved_width = window->width;
1100 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001101
1102 if (window->shsurf)
1103 shell_interface->set_fullscreen(window->shsurf,
1104 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1105 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001106 } else {
1107 shell_interface->set_toplevel(window->shsurf);
1108 window->width = window->saved_width;
1109 window->height = window->saved_height;
1110 weston_wm_window_configure(window);
1111 }
1112 }
1113}
1114
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001115static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001116weston_wm_handle_client_message(struct weston_wm *wm,
1117 xcb_generic_event_t *event)
1118{
1119 xcb_client_message_event_t *client_message =
1120 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001122
1123 window = hash_table_lookup(wm->window_hash, client_message->window);
1124
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001125 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1126 get_atom_name(wm->conn, client_message->type),
1127 client_message->data.data32[0],
1128 client_message->data.data32[1],
1129 client_message->data.data32[2],
1130 client_message->data.data32[3],
1131 client_message->data.data32[4],
1132 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001133
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001134 if (client_message->type == wm->atom.net_wm_moveresize)
1135 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001136 else if (client_message->type == wm->atom.net_wm_state)
1137 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001138}
1139
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001140enum cursor_type {
1141 XWM_CURSOR_TOP,
1142 XWM_CURSOR_BOTTOM,
1143 XWM_CURSOR_LEFT,
1144 XWM_CURSOR_RIGHT,
1145 XWM_CURSOR_TOP_LEFT,
1146 XWM_CURSOR_TOP_RIGHT,
1147 XWM_CURSOR_BOTTOM_LEFT,
1148 XWM_CURSOR_BOTTOM_RIGHT,
1149 XWM_CURSOR_LEFT_PTR,
1150};
1151
1152static const char *cursors[] = {
1153 "top_side",
1154 "bottom_side",
1155 "left_side",
1156 "right_side",
1157 "top_left_corner",
1158 "top_right_corner",
1159 "bottom_left_corner",
1160 "bottom_right_corner",
1161 "left_ptr"
1162};
1163
1164static void
1165weston_wm_create_cursors(struct weston_wm *wm)
1166{
1167 int i, count = ARRAY_LENGTH(cursors);
1168
1169 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1170 for (i = 0; i < count; i++) {
1171 wm->cursors[i] =
1172 xcb_cursor_library_load_cursor(wm, cursors[i]);
1173 }
1174
1175 wm->last_cursor = -1;
1176}
1177
1178static void
1179weston_wm_destroy_cursors(struct weston_wm *wm)
1180{
1181 uint8_t i;
1182
1183 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1184 xcb_free_cursor(wm->conn, wm->cursors[i]);
1185
1186 free(wm->cursors);
1187}
1188
1189static int
1190get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1191{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001192 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001193
1194 switch (location) {
1195 case THEME_LOCATION_RESIZING_TOP:
1196 return XWM_CURSOR_TOP;
1197 case THEME_LOCATION_RESIZING_BOTTOM:
1198 return XWM_CURSOR_BOTTOM;
1199 case THEME_LOCATION_RESIZING_LEFT:
1200 return XWM_CURSOR_LEFT;
1201 case THEME_LOCATION_RESIZING_RIGHT:
1202 return XWM_CURSOR_RIGHT;
1203 case THEME_LOCATION_RESIZING_TOP_LEFT:
1204 return XWM_CURSOR_TOP_LEFT;
1205 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1206 return XWM_CURSOR_TOP_RIGHT;
1207 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1208 return XWM_CURSOR_BOTTOM_LEFT;
1209 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1210 return XWM_CURSOR_BOTTOM_RIGHT;
1211 case THEME_LOCATION_EXTERIOR:
1212 case THEME_LOCATION_TITLEBAR:
1213 default:
1214 return XWM_CURSOR_LEFT_PTR;
1215 }
1216}
1217
1218static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001219weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1220 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001221{
1222 uint32_t cursor_value_list;
1223
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001224 if (wm->last_cursor == cursor)
1225 return;
1226
1227 wm->last_cursor = cursor;
1228
1229 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001230 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001231 XCB_CW_CURSOR, &cursor_value_list);
1232 xcb_flush(wm->conn);
1233}
1234
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001235static void
1236weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1237{
1238 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1239 struct weston_shell_interface *shell_interface =
1240 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001241 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001242 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001243 enum theme_location location;
1244 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001245 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001246
Martin Minarik6d118362012-06-07 18:01:59 +02001247 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001248 button->response_type == XCB_BUTTON_PRESS ?
1249 "PRESS" : "RELEASE", button->detail);
1250
1251 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001252 weston_wm_window_get_frame_size(window, &width, &height);
1253
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001254 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001255 button->detail == 1) {
1256 location = theme_get_location(t,
1257 button->event_x,
1258 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001259 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001260
1261 switch (location) {
1262 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001263 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001264 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001265 case THEME_LOCATION_RESIZING_TOP:
1266 case THEME_LOCATION_RESIZING_BOTTOM:
1267 case THEME_LOCATION_RESIZING_LEFT:
1268 case THEME_LOCATION_RESIZING_RIGHT:
1269 case THEME_LOCATION_RESIZING_TOP_LEFT:
1270 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1271 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1272 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1273 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001274 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001275 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001276 default:
1277 break;
1278 }
1279 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001280}
1281
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001282static void
1283weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1284{
1285 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1286 struct weston_wm_window *window;
1287 int cursor, width, height;
1288
1289 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001290 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001291 return;
1292
1293 weston_wm_window_get_frame_size(window, &width, &height);
1294 cursor = get_cursor_for_location(wm->theme, width, height,
1295 motion->event_x, motion->event_y);
1296
Tiago Vignattic1903232012-07-16 12:15:37 -04001297 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001298}
1299
1300static void
1301weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1302{
1303 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1304 struct weston_wm_window *window;
1305 int cursor, width, height;
1306
1307 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001308 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001309 return;
1310
1311 weston_wm_window_get_frame_size(window, &width, &height);
1312 cursor = get_cursor_for_location(wm->theme, width, height,
1313 enter->event_x, enter->event_y);
1314
Tiago Vignattic1903232012-07-16 12:15:37 -04001315 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001316}
1317
1318static void
1319weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1320{
1321 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1322 struct weston_wm_window *window;
1323
1324 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001325 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001326 return;
1327
Tiago Vignattic1903232012-07-16 12:15:37 -04001328 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001329}
1330
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001331static int
1332weston_wm_handle_event(int fd, uint32_t mask, void *data)
1333{
1334 struct weston_wm *wm = data;
1335 xcb_generic_event_t *event;
1336 int count = 0;
1337
1338 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1339 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001340 free(event);
1341 count++;
1342 continue;
1343 }
1344
MoD31700122013-06-11 19:58:55 -05001345 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001346 case XCB_BUTTON_PRESS:
1347 case XCB_BUTTON_RELEASE:
1348 weston_wm_handle_button(wm, event);
1349 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001350 case XCB_ENTER_NOTIFY:
1351 weston_wm_handle_enter(wm, event);
1352 break;
1353 case XCB_LEAVE_NOTIFY:
1354 weston_wm_handle_leave(wm, event);
1355 break;
1356 case XCB_MOTION_NOTIFY:
1357 weston_wm_handle_motion(wm, event);
1358 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001359 case XCB_CREATE_NOTIFY:
1360 weston_wm_handle_create_notify(wm, event);
1361 break;
1362 case XCB_MAP_REQUEST:
1363 weston_wm_handle_map_request(wm, event);
1364 break;
1365 case XCB_MAP_NOTIFY:
1366 weston_wm_handle_map_notify(wm, event);
1367 break;
1368 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001369 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001370 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001371 case XCB_REPARENT_NOTIFY:
1372 weston_wm_handle_reparent_notify(wm, event);
1373 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374 case XCB_CONFIGURE_REQUEST:
1375 weston_wm_handle_configure_request(wm, event);
1376 break;
1377 case XCB_CONFIGURE_NOTIFY:
1378 weston_wm_handle_configure_notify(wm, event);
1379 break;
1380 case XCB_DESTROY_NOTIFY:
1381 weston_wm_handle_destroy_notify(wm, event);
1382 break;
1383 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001384 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001385 break;
1386 case XCB_PROPERTY_NOTIFY:
1387 weston_wm_handle_property_notify(wm, event);
1388 break;
1389 case XCB_CLIENT_MESSAGE:
1390 weston_wm_handle_client_message(wm, event);
1391 break;
1392 }
1393
1394 free(event);
1395 count++;
1396 }
1397
1398 xcb_flush(wm->conn);
1399
1400 return count;
1401}
1402
1403static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001404weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1405{
1406 xcb_depth_iterator_t d_iter;
1407 xcb_visualtype_iterator_t vt_iter;
1408 xcb_visualtype_t *visualtype;
1409
1410 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1411 visualtype = NULL;
1412 while (d_iter.rem > 0) {
1413 if (d_iter.data->depth == 32) {
1414 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1415 visualtype = vt_iter.data;
1416 break;
1417 }
1418
1419 xcb_depth_next(&d_iter);
1420 }
1421
1422 if (visualtype == NULL) {
1423 weston_log("no 32 bit visualtype\n");
1424 return;
1425 }
1426
1427 wm->visual_id = visualtype->visual_id;
1428 wm->colormap = xcb_generate_id(wm->conn);
1429 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1430 wm->colormap, wm->screen->root, wm->visual_id);
1431}
1432
1433static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001434weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001435{
1436
1437#define F(field) offsetof(struct weston_wm, field)
1438
1439 static const struct { const char *name; int offset; } atoms[] = {
1440 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1441 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1442 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001443 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001444 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001445 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001446 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001447 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001448 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1449 { "_NET_WM_STATE", F(atom.net_wm_state) },
1450 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1451 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1452 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1453 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1454
1455 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1456 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1457 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1458 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1459 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1460 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1461 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001462 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1463 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1465 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1466 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1467 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1468 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1469
1470 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1471 { "_NET_SUPPORTING_WM_CHECK",
1472 F(atom.net_supporting_wm_check) },
1473 { "_NET_SUPPORTED", F(atom.net_supported) },
1474 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1475 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001476 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001477 { "TARGETS", F(atom.targets) },
1478 { "UTF8_STRING", F(atom.utf8_string) },
1479 { "_WL_SELECTION", F(atom.wl_selection) },
1480 { "INCR", F(atom.incr) },
1481 { "TIMESTAMP", F(atom.timestamp) },
1482 { "MULTIPLE", F(atom.multiple) },
1483 { "UTF8_STRING" , F(atom.utf8_string) },
1484 { "COMPOUND_TEXT", F(atom.compound_text) },
1485 { "TEXT", F(atom.text) },
1486 { "STRING", F(atom.string) },
1487 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1488 { "text/plain", F(atom.text_plain) },
1489 };
1490#undef F
1491
1492 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1493 xcb_xfixes_query_version_reply_t *xfixes_reply;
1494 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1495 xcb_intern_atom_reply_t *reply;
1496 xcb_render_query_pict_formats_reply_t *formats_reply;
1497 xcb_render_query_pict_formats_cookie_t formats_cookie;
1498 xcb_render_pictforminfo_t *formats;
1499 uint32_t i;
1500
1501 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1502
1503 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1504
1505 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1506 cookies[i] = xcb_intern_atom (wm->conn, 0,
1507 strlen(atoms[i].name),
1508 atoms[i].name);
1509
1510 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1511 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1512 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1513 free(reply);
1514 }
1515
1516 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1517 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001518 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001519
1520 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1521 XCB_XFIXES_MAJOR_VERSION,
1522 XCB_XFIXES_MINOR_VERSION);
1523 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1524 xfixes_cookie, NULL);
1525
Martin Minarik6d118362012-06-07 18:01:59 +02001526 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001527 xfixes_reply->major_version, xfixes_reply->minor_version);
1528
1529 free(xfixes_reply);
1530
1531 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1532 formats_cookie, 0);
1533 if (formats_reply == NULL)
1534 return;
1535
1536 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001537 for (i = 0; i < formats_reply->num_formats; i++) {
1538 if (formats[i].direct.red_mask != 0xff &&
1539 formats[i].direct.red_shift != 16)
1540 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001541 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1542 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001543 wm->format_rgb = formats[i];
1544 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1545 formats[i].depth == 32 &&
1546 formats[i].direct.alpha_mask == 0xff &&
1547 formats[i].direct.alpha_shift == 24)
1548 wm->format_rgba = formats[i];
1549 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001550
1551 free(formats_reply);
1552}
1553
1554static void
1555weston_wm_create_wm_window(struct weston_wm *wm)
1556{
1557 static const char name[] = "Weston WM";
1558
1559 wm->wm_window = xcb_generate_id(wm->conn);
1560 xcb_create_window(wm->conn,
1561 XCB_COPY_FROM_PARENT,
1562 wm->wm_window,
1563 wm->screen->root,
1564 0, 0,
1565 10, 10,
1566 0,
1567 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1568 wm->screen->root_visual,
1569 0, NULL);
1570
1571 xcb_change_property(wm->conn,
1572 XCB_PROP_MODE_REPLACE,
1573 wm->wm_window,
1574 wm->atom.net_supporting_wm_check,
1575 XCB_ATOM_WINDOW,
1576 32, /* format */
1577 1, &wm->wm_window);
1578
1579 xcb_change_property(wm->conn,
1580 XCB_PROP_MODE_REPLACE,
1581 wm->wm_window,
1582 wm->atom.net_wm_name,
1583 wm->atom.utf8_string,
1584 8, /* format */
1585 strlen(name), name);
1586
1587 xcb_change_property(wm->conn,
1588 XCB_PROP_MODE_REPLACE,
1589 wm->screen->root,
1590 wm->atom.net_supporting_wm_check,
1591 XCB_ATOM_WINDOW,
1592 32, /* format */
1593 1, &wm->wm_window);
1594
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001595 /* Claim the WM_S0 selection even though we don't suport
1596 * the --replace functionality. */
1597 xcb_set_selection_owner(wm->conn,
1598 wm->wm_window,
1599 wm->atom.wm_s0,
1600 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001601}
1602
1603struct weston_wm *
1604weston_wm_create(struct weston_xserver *wxs)
1605{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001606 struct weston_wm *wm;
1607 struct wl_event_loop *loop;
1608 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001609 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001610 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001611 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001612
1613 wm = malloc(sizeof *wm);
1614 if (wm == NULL)
1615 return NULL;
1616
1617 memset(wm, 0, sizeof *wm);
1618 wm->server = wxs;
1619 wm->window_hash = hash_table_create();
1620 if (wm->window_hash == NULL) {
1621 free(wm);
1622 return NULL;
1623 }
1624
1625 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001626 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001627 hash_table_destroy(wm->window_hash);
1628 free(wm);
1629 return NULL;
1630 }
1631
1632 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001633 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001634 close(sv[1]);
1635
1636 /* xcb_connect_to_fd takes ownership of the fd. */
1637 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1638 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001639 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001640 close(sv[0]);
1641 hash_table_destroy(wm->window_hash);
1642 free(wm);
1643 return NULL;
1644 }
1645
1646 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1647 wm->screen = s.data;
1648
1649 loop = wl_display_get_event_loop(wxs->wl_display);
1650 wm->source =
1651 wl_event_loop_add_fd(loop, sv[0],
1652 WL_EVENT_READABLE,
1653 weston_wm_handle_event, wm);
1654 wl_event_source_check(wm->source);
1655
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001656 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001657 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001658
1659 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001660 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1661 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1662 XCB_EVENT_MASK_PROPERTY_CHANGE;
1663 xcb_change_window_attributes(wm->conn, wm->screen->root,
1664 XCB_CW_EVENT_MASK, values);
1665 wm->theme = theme_create();
1666
1667 weston_wm_create_wm_window(wm);
1668
1669 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001670 supported[1] = wm->atom.net_wm_state;
1671 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001672 xcb_change_property(wm->conn,
1673 XCB_PROP_MODE_REPLACE,
1674 wm->screen->root,
1675 wm->atom.net_supported,
1676 XCB_ATOM_ATOM,
1677 32, /* format */
1678 ARRAY_LENGTH(supported), supported);
1679
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001680 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001681
1682 xcb_flush(wm->conn);
1683
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001684 wm->activate_listener.notify = weston_wm_window_activate;
1685 wl_signal_add(&wxs->compositor->activate_signal,
1686 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001687 wm->kill_listener.notify = weston_wm_kill_client;
1688 wl_signal_add(&wxs->compositor->kill_signal,
1689 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001690
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001691 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001692 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001693
Martin Minarik6d118362012-06-07 18:01:59 +02001694 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001695
1696 return wm;
1697}
1698
1699void
1700weston_wm_destroy(struct weston_wm *wm)
1701{
1702 /* FIXME: Free windows in hash. */
1703 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001704 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001705 xcb_disconnect(wm->conn);
1706 wl_event_source_remove(wm->source);
1707 wl_list_remove(&wm->selection_listener.link);
1708 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001709 wl_list_remove(&wm->kill_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710
1711 free(wm);
1712}
1713
1714static void
1715surface_destroy(struct wl_listener *listener, void *data)
1716{
1717 struct weston_wm_window *window =
1718 container_of(listener,
1719 struct weston_wm_window, surface_destroy_listener);
1720
Martin Minarik6d118362012-06-07 18:01:59 +02001721 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001722}
1723
1724static struct weston_wm_window *
1725get_wm_window(struct weston_surface *surface)
1726{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001727 struct wl_listener *listener;
1728
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001729 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001730 if (listener)
1731 return container_of(listener, struct weston_wm_window,
1732 surface_destroy_listener);
1733
1734 return NULL;
1735}
1736
1737static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001738weston_wm_window_configure(void *data)
1739{
1740 struct weston_wm_window *window = data;
1741 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001742 uint32_t values[4];
1743 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001744
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001745 weston_wm_window_get_child_position(window, &x, &y);
1746 values[0] = x;
1747 values[1] = y;
1748 values[2] = window->width;
1749 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001750 xcb_configure_window(wm->conn,
1751 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001752 XCB_CONFIG_WINDOW_X |
1753 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001754 XCB_CONFIG_WINDOW_WIDTH |
1755 XCB_CONFIG_WINDOW_HEIGHT,
1756 values);
1757
1758 weston_wm_window_get_frame_size(window, &width, &height);
1759 values[0] = width;
1760 values[1] = height;
1761 xcb_configure_window(wm->conn,
1762 window->frame_id,
1763 XCB_CONFIG_WINDOW_WIDTH |
1764 XCB_CONFIG_WINDOW_HEIGHT,
1765 values);
1766
1767 window->configure_source = NULL;
1768
1769 weston_wm_window_schedule_repaint(window);
1770}
1771
1772static void
1773send_configure(struct weston_surface *surface,
1774 uint32_t edges, int32_t width, int32_t height)
1775{
1776 struct weston_wm_window *window = get_wm_window(surface);
1777 struct weston_wm *wm = window->wm;
1778 struct theme *t = window->wm->theme;
1779
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001780 if (window->fullscreen) {
1781 window->width = width;
1782 window->height = height;
1783 } else if (window->decorate) {
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001784 window->width = width - 2 * (t->margin + t->width);
1785 window->height = height - 2 * t->margin -
1786 t->titlebar_height - t->width;
1787 } else {
1788 window->width = width - 2 * t->margin;
1789 window->height = height - 2 * t->margin;
1790 }
1791
1792 if (window->configure_source)
1793 return;
1794
1795 window->configure_source =
1796 wl_event_loop_add_idle(wm->server->loop,
1797 weston_wm_window_configure, window);
1798}
1799
1800static const struct weston_shell_client shell_client = {
1801 send_configure
1802};
1803
1804static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805xserver_map_shell_surface(struct weston_wm *wm,
1806 struct weston_wm_window *window)
1807{
1808 struct weston_shell_interface *shell_interface =
1809 &wm->server->compositor->shell_interface;
1810 struct weston_wm_window *parent;
1811 struct theme *t = window->wm->theme;
Tiago Vignattice1baa82012-07-20 23:09:55 +03001812 int parent_id, x = 0, y = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001813
1814 if (!shell_interface->create_shell_surface)
1815 return;
1816
1817 window->shsurf =
1818 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001819 window->surface,
1820 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001821
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001822 if (window->fullscreen) {
1823 window->saved_width = window->width;
1824 window->saved_height = window->height;
1825 shell_interface->set_fullscreen(window->shsurf,
1826 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1827 0, NULL);
1828 return;
1829 } else if (!window->override_redirect) {
1830 /* ICCCM 4.1.1 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001831 shell_interface->set_toplevel(window->shsurf);
1832 return;
1833 }
1834
Tiago Vignattice1baa82012-07-20 23:09:55 +03001835 /* not all non-toplevel has transient_for set. So we need this
1836 * workaround to guess a parent that will determine the relative
1837 * position of the transient surface */
1838 if (!window->transient_for)
1839 parent_id = wm->focus_latest->id;
1840 else
1841 parent_id = window->transient_for->id;
1842
1843 parent = hash_table_lookup(wm->window_hash, parent_id);
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001844
1845 /* non-decorated and non-toplevel windows, e.g. sub-menus */
1846 if (!parent->decorate && parent->override_redirect) {
1847 x = parent->x + t->margin;
1848 y = parent->y + t->margin;
1849 }
1850
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001851 shell_interface->set_transient(window->shsurf, parent->surface,
Tiago Vignattie66fcee2012-07-20 23:09:54 +03001852 window->x + t->margin - x,
1853 window->y + t->margin - y,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001854 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1855}
1856
1857static void
1858xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1859 struct wl_resource *surface_resource, uint32_t id)
1860{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001861 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001862 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001863 struct weston_surface *surface =
1864 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001865 struct weston_wm_window *window;
1866
1867 if (client != wxs->client)
1868 return;
1869
1870 window = hash_table_lookup(wm->window_hash, id);
1871 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001872 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001873 return;
1874 }
1875
Martin Minarik6d118362012-06-07 18:01:59 +02001876 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001877
1878 weston_wm_window_read_properties(window);
1879
1880 window->surface = (struct weston_surface *) surface;
1881 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001882 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883 &window->surface_destroy_listener);
1884
1885 weston_wm_window_schedule_repaint(window);
1886 xserver_map_shell_surface(wm, window);
1887}
1888
1889const struct xserver_interface xserver_implementation = {
1890 xserver_set_window_id
1891};