blob: 6b9e38b8fe74ec3a150c43adc2a194bcbfa28181 [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;
596 if (wm->focus_window)
597 weston_wm_window_schedule_repaint(wm->focus_window);
598}
599
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300600static void
601weston_wm_window_transform(struct wl_listener *listener, void *data)
602{
603 struct weston_surface *surface = data;
604 struct weston_wm_window *window = get_wm_window(surface);
605 struct weston_wm *wm =
606 container_of(listener, struct weston_wm, transform_listener);
607 struct weston_output *output = surface->output;
608 uint32_t mask, values[2];
609 float sxf, syf;
610 int sx, sy;
611 static int old_sx = -1, old_sy = -1;
612
613 if (!window || !wm)
614 return;
615
616 if (!weston_surface_is_mapped(surface))
617 return;
618
619 weston_surface_to_global_float(surface, output->x, output->y,
620 &sxf, &syf);
621
622 sx = (int) sxf;
623 sy = (int) syf;
624
625 if (old_sx == sx && old_sy == sy)
626 return;
627
628 values[0] = sx;
629 values[1] = sy;
630 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
631
632 xcb_configure_window(wm->conn, window->frame_id, mask, values);
633 xcb_flush(wm->conn);
634
635 old_sx = sx;
636 old_sy = sy;
637}
638
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400639static int
640our_resource(struct weston_wm *wm, uint32_t id)
641{
642 const xcb_setup_t *setup;
643
644 setup = xcb_get_setup(wm->conn);
645
646 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
647}
648
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400649#define ICCCM_WITHDRAWN_STATE 0
650#define ICCCM_NORMAL_STATE 1
651#define ICCCM_ICONIC_STATE 3
652
653static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500654weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400655{
656 struct weston_wm *wm = window->wm;
657 uint32_t property[2];
658
659 property[0] = state;
660 property[1] = XCB_WINDOW_NONE;
661
662 xcb_change_property(wm->conn,
663 XCB_PROP_MODE_REPLACE,
664 window->id,
665 wm->atom.wm_state,
666 wm->atom.wm_state,
667 32, /* format */
668 2, property);
669}
670
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400671static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500672weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
673{
674 struct weston_wm *wm = window->wm;
675 uint32_t property[1];
676 int i;
677
678 i = 0;
679 if (window->fullscreen)
680 property[i++] = wm->atom.net_wm_state_fullscreen;
681
682 xcb_change_property(wm->conn,
683 XCB_PROP_MODE_REPLACE,
684 window->id,
685 wm->atom.net_wm_state,
686 XCB_ATOM_ATOM,
687 32, /* format */
688 i, property);
689}
690
691static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400692weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
693{
694 xcb_map_request_event_t *map_request =
695 (xcb_map_request_event_t *) event;
696 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400697 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400698 int x, y, width, height;
699
700 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200701 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400702 map_request->window);
703 return;
704 }
705
706 window = hash_table_lookup(wm->window_hash, map_request->window);
707
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400708 if (window->frame_id)
709 return;
710
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400711 weston_wm_window_read_properties(window);
712
713 weston_wm_window_get_frame_size(window, &width, &height);
714 weston_wm_window_get_child_position(window, &x, &y);
715
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400716 values[0] = wm->screen->black_pixel;
717 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400718 XCB_EVENT_MASK_KEY_PRESS |
719 XCB_EVENT_MASK_KEY_RELEASE |
720 XCB_EVENT_MASK_BUTTON_PRESS |
721 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400722 XCB_EVENT_MASK_POINTER_MOTION |
723 XCB_EVENT_MASK_ENTER_WINDOW |
724 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400725 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400726 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400727 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400728
729 window->frame_id = xcb_generate_id(wm->conn);
730 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400731 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732 window->frame_id,
733 wm->screen->root,
734 0, 0,
735 width, height,
736 0,
737 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400738 wm->visual_id,
739 XCB_CW_BORDER_PIXEL |
740 XCB_CW_EVENT_MASK |
741 XCB_CW_COLORMAP, values);
742
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
744
745 values[0] = 0;
746 xcb_configure_window(wm->conn, window->id,
747 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
748
Martin Minarik6d118362012-06-07 18:01:59 +0200749 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400750 window->id, window, window->frame_id);
751
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500752 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
753 weston_wm_window_set_net_wm_state(window);
754
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400755 xcb_map_window(wm->conn, map_request->window);
756 xcb_map_window(wm->conn, window->frame_id);
757
758 window->cairo_surface =
759 cairo_xcb_surface_create_with_xrender_format(wm->conn,
760 wm->screen,
761 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400762 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400763 width, height);
764
765 hash_table_insert(wm->window_hash, window->frame_id, window);
766}
767
768static void
769weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
770{
771 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
772
773 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200774 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400775 map_notify->window);
776 return;
777 }
778
Martin Minarik6d118362012-06-07 18:01:59 +0200779 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400780}
781
782static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400783weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
784{
785 xcb_unmap_notify_event_t *unmap_notify =
786 (xcb_unmap_notify_event_t *) event;
787 struct weston_wm_window *window;
788
Martin Minarik6d118362012-06-07 18:01:59 +0200789 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400790 unmap_notify->window,
791 unmap_notify->event,
792 our_resource(wm, unmap_notify->window) ? ", ours" : "");
793
794 if (our_resource(wm, unmap_notify->window))
795 return;
796
MoD31700122013-06-11 19:58:55 -0500797 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400798 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
799 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400800 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400801
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400802 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400803 if (window->repaint_source)
804 wl_event_source_remove(window->repaint_source);
805 if (window->cairo_surface)
806 cairo_surface_destroy(window->cairo_surface);
807
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400808 if (window->frame_id) {
809 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
810 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500811 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400812 hash_table_remove(wm->window_hash, window->frame_id);
813 window->frame_id = XCB_WINDOW_NONE;
814 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400815
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400816 if (wm->focus_window == window)
817 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400818 if (window->surface)
819 wl_list_remove(&window->surface_destroy_listener.link);
820 window->surface = NULL;
821}
822
823static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400824weston_wm_window_draw_decoration(void *data)
825{
826 struct weston_wm_window *window = data;
827 struct weston_wm *wm = window->wm;
828 struct theme *t = wm->theme;
829 cairo_t *cr;
830 int x, y, width, height;
831 const char *title;
832 uint32_t flags = 0;
833
834 weston_wm_window_read_properties(window);
835
836 window->repaint_source = NULL;
837
838 weston_wm_window_get_frame_size(window, &width, &height);
839 weston_wm_window_get_child_position(window, &x, &y);
840
841 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
842 cr = cairo_create(window->cairo_surface);
843
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500844 if (window->fullscreen) {
845 /* nothing */
846 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400847 if (wm->focus_window == window)
848 flags |= THEME_FRAME_ACTIVE;
849
850 if (window->name)
851 title = window->name;
852 else
853 title = "untitled";
854
855 theme_render_frame(t, cr, width, height, title, flags);
856 } else {
857 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
858 cairo_set_source_rgba(cr, 0, 0, 0, 0);
859 cairo_paint(cr);
860
861 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
862 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
863 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
864 }
865
866 cairo_destroy(cr);
867
868 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700869 pixman_region32_fini(&window->surface->pending.opaque);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400870 /* We leave an extra pixel around the X window area to
871 * make sure we don't sample from the undefined alpha
872 * channel when filtering. */
Kristian Høgsberg25bb6962013-02-14 22:01:04 -0500873 pixman_region32_init_rect(&window->surface->pending.opaque,
874 x - 1, y - 1,
875 window->width + 2,
876 window->height + 2);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200877 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500878 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400879
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500880 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500881 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500882 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883 t->margin, t->margin,
884 width - 2 * t->margin,
885 height - 2 * t->margin);
886 }
887}
888
889static void
890weston_wm_window_schedule_repaint(struct weston_wm_window *window)
891{
892 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300893 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400895 if (window->frame_id == XCB_WINDOW_NONE) {
896 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300897 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700898 pixman_region32_fini(&window->surface->pending.opaque);
899 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300900 width, height);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200901 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400902 }
903 return;
904 }
905
906 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400907 return;
908
909 window->repaint_source =
910 wl_event_loop_add_idle(wm->server->loop,
911 weston_wm_window_draw_decoration,
912 window);
913}
914
915static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400916weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
917{
918 xcb_property_notify_event_t *property_notify =
919 (xcb_property_notify_event_t *) event;
920 struct weston_wm_window *window;
921
922 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000923 if (!window)
924 return;
925
926 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400927
Martin Minarik6d118362012-06-07 18:01:59 +0200928 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400929 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400930 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200931 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400932 else
933 read_and_dump_property(wm, property_notify->window,
934 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400935
936 if (property_notify->atom == wm->atom.net_wm_name ||
937 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400938 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400939}
940
941static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400942weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300943 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400944{
945 struct weston_wm_window *window;
946 uint32_t values[1];
947
948 window = malloc(sizeof *window);
949 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200950 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400951 return;
952 }
953
954 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
955 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
956
957 memset(window, 0, sizeof *window);
958 window->wm = wm;
959 window->id = id;
960 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300961 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400962 window->width = width;
963 window->height = height;
964
965 hash_table_insert(wm->window_hash, id, window);
966}
967
968static void
969weston_wm_window_destroy(struct weston_wm_window *window)
970{
971 hash_table_remove(window->wm->window_hash, window->id);
972 free(window);
973}
974
975static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400976weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
977{
978 xcb_create_notify_event_t *create_notify =
979 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400980
Martin Minarik6d118362012-06-07 18:01:59 +0200981 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400982 create_notify->window,
983 create_notify->width, create_notify->height,
984 create_notify->override_redirect ? ", override" : "",
985 our_resource(wm, create_notify->window) ? ", ours" : "");
986
987 if (our_resource(wm, create_notify->window))
988 return;
989
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400990 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300991 create_notify->width, create_notify->height,
992 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400993}
994
995static void
996weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
997{
998 xcb_destroy_notify_event_t *destroy_notify =
999 (xcb_destroy_notify_event_t *) event;
1000 struct weston_wm_window *window;
1001
Martin Minarik6d118362012-06-07 18:01:59 +02001002 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001003 destroy_notify->window,
1004 destroy_notify->event,
1005 our_resource(wm, destroy_notify->window) ? ", ours" : "");
1006
1007 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001008 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001009
1010 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001011 weston_wm_window_destroy(window);
1012}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001013
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001014static void
1015weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1016{
1017 xcb_reparent_notify_event_t *reparent_notify =
1018 (xcb_reparent_notify_event_t *) event;
1019 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001020
Martin Minarik6d118362012-06-07 18:01:59 +02001021 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001022 reparent_notify->window,
1023 reparent_notify->parent,
1024 reparent_notify->event);
1025
1026 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001027 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1028 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001029 } else if (!our_resource(wm, reparent_notify->parent)) {
1030 window = hash_table_lookup(wm->window_hash,
1031 reparent_notify->window);
1032 weston_wm_window_destroy(window);
1033 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001034}
1035
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001036struct weston_seat *
1037weston_wm_pick_seat(struct weston_wm *wm)
1038{
1039 return container_of(wm->server->compositor->seat_list.next,
1040 struct weston_seat, link);
1041}
1042
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001043static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001044weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1045 xcb_client_message_event_t *client_message)
1046{
1047 static const int map[] = {
1048 THEME_LOCATION_RESIZING_TOP_LEFT,
1049 THEME_LOCATION_RESIZING_TOP,
1050 THEME_LOCATION_RESIZING_TOP_RIGHT,
1051 THEME_LOCATION_RESIZING_RIGHT,
1052 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1053 THEME_LOCATION_RESIZING_BOTTOM,
1054 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1055 THEME_LOCATION_RESIZING_LEFT
1056 };
1057
1058 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001059 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001060 int detail;
1061 struct weston_shell_interface *shell_interface =
1062 &wm->server->compositor->shell_interface;
1063
Kristian Høgsberge3148752013-05-06 23:19:49 -04001064 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001065 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001066 return;
1067
1068 detail = client_message->data.data32[2];
1069 switch (detail) {
1070 case _NET_WM_MOVERESIZE_MOVE:
1071 shell_interface->move(window->shsurf, seat);
1072 break;
1073 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1074 case _NET_WM_MOVERESIZE_SIZE_TOP:
1075 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1076 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1077 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1078 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1079 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1080 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1081 shell_interface->resize(window->shsurf, seat, map[detail]);
1082 break;
1083 case _NET_WM_MOVERESIZE_CANCEL:
1084 break;
1085 }
1086}
1087
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001088#define _NET_WM_STATE_REMOVE 0
1089#define _NET_WM_STATE_ADD 1
1090#define _NET_WM_STATE_TOGGLE 2
1091
1092static int
1093update_state(int action, int *state)
1094{
1095 int new_state, changed;
1096
1097 switch (action) {
1098 case _NET_WM_STATE_REMOVE:
1099 new_state = 0;
1100 break;
1101 case _NET_WM_STATE_ADD:
1102 new_state = 1;
1103 break;
1104 case _NET_WM_STATE_TOGGLE:
1105 new_state = !*state;
1106 break;
1107 default:
1108 return 0;
1109 }
1110
1111 changed = (*state != new_state);
1112 *state = new_state;
1113
1114 return changed;
1115}
1116
1117static void
1118weston_wm_window_configure(void *data);
1119
1120static void
1121weston_wm_window_handle_state(struct weston_wm_window *window,
1122 xcb_client_message_event_t *client_message)
1123{
1124 struct weston_wm *wm = window->wm;
1125 struct weston_shell_interface *shell_interface =
1126 &wm->server->compositor->shell_interface;
1127 uint32_t action, property;
1128
1129 action = client_message->data.data32[0];
1130 property = client_message->data.data32[1];
1131
1132 if (property == wm->atom.net_wm_state_fullscreen &&
1133 update_state(action, &window->fullscreen)) {
1134 weston_wm_window_set_net_wm_state(window);
1135 if (window->fullscreen) {
1136 window->saved_width = window->width;
1137 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001138
1139 if (window->shsurf)
1140 shell_interface->set_fullscreen(window->shsurf,
1141 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1142 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001143 } else {
1144 shell_interface->set_toplevel(window->shsurf);
1145 window->width = window->saved_width;
1146 window->height = window->saved_height;
1147 weston_wm_window_configure(window);
1148 }
1149 }
1150}
1151
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001152static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001153weston_wm_handle_client_message(struct weston_wm *wm,
1154 xcb_generic_event_t *event)
1155{
1156 xcb_client_message_event_t *client_message =
1157 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001158 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001159
1160 window = hash_table_lookup(wm->window_hash, client_message->window);
1161
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001162 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1163 get_atom_name(wm->conn, client_message->type),
1164 client_message->data.data32[0],
1165 client_message->data.data32[1],
1166 client_message->data.data32[2],
1167 client_message->data.data32[3],
1168 client_message->data.data32[4],
1169 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001170
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001171 if (client_message->type == wm->atom.net_wm_moveresize)
1172 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001173 else if (client_message->type == wm->atom.net_wm_state)
1174 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001175}
1176
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001177enum cursor_type {
1178 XWM_CURSOR_TOP,
1179 XWM_CURSOR_BOTTOM,
1180 XWM_CURSOR_LEFT,
1181 XWM_CURSOR_RIGHT,
1182 XWM_CURSOR_TOP_LEFT,
1183 XWM_CURSOR_TOP_RIGHT,
1184 XWM_CURSOR_BOTTOM_LEFT,
1185 XWM_CURSOR_BOTTOM_RIGHT,
1186 XWM_CURSOR_LEFT_PTR,
1187};
1188
1189static const char *cursors[] = {
1190 "top_side",
1191 "bottom_side",
1192 "left_side",
1193 "right_side",
1194 "top_left_corner",
1195 "top_right_corner",
1196 "bottom_left_corner",
1197 "bottom_right_corner",
1198 "left_ptr"
1199};
1200
1201static void
1202weston_wm_create_cursors(struct weston_wm *wm)
1203{
1204 int i, count = ARRAY_LENGTH(cursors);
1205
1206 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1207 for (i = 0; i < count; i++) {
1208 wm->cursors[i] =
1209 xcb_cursor_library_load_cursor(wm, cursors[i]);
1210 }
1211
1212 wm->last_cursor = -1;
1213}
1214
1215static void
1216weston_wm_destroy_cursors(struct weston_wm *wm)
1217{
1218 uint8_t i;
1219
1220 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1221 xcb_free_cursor(wm->conn, wm->cursors[i]);
1222
1223 free(wm->cursors);
1224}
1225
1226static int
1227get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1228{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001229 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001230
1231 switch (location) {
1232 case THEME_LOCATION_RESIZING_TOP:
1233 return XWM_CURSOR_TOP;
1234 case THEME_LOCATION_RESIZING_BOTTOM:
1235 return XWM_CURSOR_BOTTOM;
1236 case THEME_LOCATION_RESIZING_LEFT:
1237 return XWM_CURSOR_LEFT;
1238 case THEME_LOCATION_RESIZING_RIGHT:
1239 return XWM_CURSOR_RIGHT;
1240 case THEME_LOCATION_RESIZING_TOP_LEFT:
1241 return XWM_CURSOR_TOP_LEFT;
1242 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1243 return XWM_CURSOR_TOP_RIGHT;
1244 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1245 return XWM_CURSOR_BOTTOM_LEFT;
1246 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1247 return XWM_CURSOR_BOTTOM_RIGHT;
1248 case THEME_LOCATION_EXTERIOR:
1249 case THEME_LOCATION_TITLEBAR:
1250 default:
1251 return XWM_CURSOR_LEFT_PTR;
1252 }
1253}
1254
1255static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001256weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1257 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001258{
1259 uint32_t cursor_value_list;
1260
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001261 if (wm->last_cursor == cursor)
1262 return;
1263
1264 wm->last_cursor = cursor;
1265
1266 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001267 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001268 XCB_CW_CURSOR, &cursor_value_list);
1269 xcb_flush(wm->conn);
1270}
1271
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001272static void
1273weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1274{
1275 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1276 struct weston_shell_interface *shell_interface =
1277 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001278 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001279 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001280 enum theme_location location;
1281 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001282 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001283
Martin Minarik6d118362012-06-07 18:01:59 +02001284 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001285 button->response_type == XCB_BUTTON_PRESS ?
1286 "PRESS" : "RELEASE", button->detail);
1287
1288 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001289 weston_wm_window_get_frame_size(window, &width, &height);
1290
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001291 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001292 button->detail == 1) {
1293 location = theme_get_location(t,
1294 button->event_x,
1295 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001296 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001297
1298 switch (location) {
1299 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001300 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001301 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001302 case THEME_LOCATION_RESIZING_TOP:
1303 case THEME_LOCATION_RESIZING_BOTTOM:
1304 case THEME_LOCATION_RESIZING_LEFT:
1305 case THEME_LOCATION_RESIZING_RIGHT:
1306 case THEME_LOCATION_RESIZING_TOP_LEFT:
1307 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1308 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1309 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1310 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001311 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001312 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001313 default:
1314 break;
1315 }
1316 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001317}
1318
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001319static void
1320weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1321{
1322 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1323 struct weston_wm_window *window;
1324 int cursor, width, height;
1325
1326 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001327 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001328 return;
1329
1330 weston_wm_window_get_frame_size(window, &width, &height);
1331 cursor = get_cursor_for_location(wm->theme, width, height,
1332 motion->event_x, motion->event_y);
1333
Tiago Vignattic1903232012-07-16 12:15:37 -04001334 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001335}
1336
1337static void
1338weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1339{
1340 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1341 struct weston_wm_window *window;
1342 int cursor, width, height;
1343
1344 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001345 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001346 return;
1347
1348 weston_wm_window_get_frame_size(window, &width, &height);
1349 cursor = get_cursor_for_location(wm->theme, width, height,
1350 enter->event_x, enter->event_y);
1351
Tiago Vignattic1903232012-07-16 12:15:37 -04001352 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001353}
1354
1355static void
1356weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1357{
1358 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1359 struct weston_wm_window *window;
1360
1361 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001362 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001363 return;
1364
Tiago Vignattic1903232012-07-16 12:15:37 -04001365 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001366}
1367
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001368static int
1369weston_wm_handle_event(int fd, uint32_t mask, void *data)
1370{
1371 struct weston_wm *wm = data;
1372 xcb_generic_event_t *event;
1373 int count = 0;
1374
1375 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1376 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001377 free(event);
1378 count++;
1379 continue;
1380 }
1381
MoD31700122013-06-11 19:58:55 -05001382 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001383 case XCB_BUTTON_PRESS:
1384 case XCB_BUTTON_RELEASE:
1385 weston_wm_handle_button(wm, event);
1386 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001387 case XCB_ENTER_NOTIFY:
1388 weston_wm_handle_enter(wm, event);
1389 break;
1390 case XCB_LEAVE_NOTIFY:
1391 weston_wm_handle_leave(wm, event);
1392 break;
1393 case XCB_MOTION_NOTIFY:
1394 weston_wm_handle_motion(wm, event);
1395 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001396 case XCB_CREATE_NOTIFY:
1397 weston_wm_handle_create_notify(wm, event);
1398 break;
1399 case XCB_MAP_REQUEST:
1400 weston_wm_handle_map_request(wm, event);
1401 break;
1402 case XCB_MAP_NOTIFY:
1403 weston_wm_handle_map_notify(wm, event);
1404 break;
1405 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001406 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001407 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001408 case XCB_REPARENT_NOTIFY:
1409 weston_wm_handle_reparent_notify(wm, event);
1410 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001411 case XCB_CONFIGURE_REQUEST:
1412 weston_wm_handle_configure_request(wm, event);
1413 break;
1414 case XCB_CONFIGURE_NOTIFY:
1415 weston_wm_handle_configure_notify(wm, event);
1416 break;
1417 case XCB_DESTROY_NOTIFY:
1418 weston_wm_handle_destroy_notify(wm, event);
1419 break;
1420 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +02001421 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001422 break;
1423 case XCB_PROPERTY_NOTIFY:
1424 weston_wm_handle_property_notify(wm, event);
1425 break;
1426 case XCB_CLIENT_MESSAGE:
1427 weston_wm_handle_client_message(wm, event);
1428 break;
1429 }
1430
1431 free(event);
1432 count++;
1433 }
1434
1435 xcb_flush(wm->conn);
1436
1437 return count;
1438}
1439
1440static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001441weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1442{
1443 xcb_depth_iterator_t d_iter;
1444 xcb_visualtype_iterator_t vt_iter;
1445 xcb_visualtype_t *visualtype;
1446
1447 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1448 visualtype = NULL;
1449 while (d_iter.rem > 0) {
1450 if (d_iter.data->depth == 32) {
1451 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1452 visualtype = vt_iter.data;
1453 break;
1454 }
1455
1456 xcb_depth_next(&d_iter);
1457 }
1458
1459 if (visualtype == NULL) {
1460 weston_log("no 32 bit visualtype\n");
1461 return;
1462 }
1463
1464 wm->visual_id = visualtype->visual_id;
1465 wm->colormap = xcb_generate_id(wm->conn);
1466 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1467 wm->colormap, wm->screen->root, wm->visual_id);
1468}
1469
1470static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001471weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001472{
1473
1474#define F(field) offsetof(struct weston_wm, field)
1475
1476 static const struct { const char *name; int offset; } atoms[] = {
1477 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1478 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1479 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001480 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001481 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001482 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001483 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001484 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001485 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1486 { "_NET_WM_STATE", F(atom.net_wm_state) },
1487 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1488 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1489 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1490 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1491
1492 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1493 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1494 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1495 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1496 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1497 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1498 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001499 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1500 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001501 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1502 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1503 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1504 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1505 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1506
1507 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1508 { "_NET_SUPPORTING_WM_CHECK",
1509 F(atom.net_supporting_wm_check) },
1510 { "_NET_SUPPORTED", F(atom.net_supported) },
1511 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1512 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001513 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001514 { "TARGETS", F(atom.targets) },
1515 { "UTF8_STRING", F(atom.utf8_string) },
1516 { "_WL_SELECTION", F(atom.wl_selection) },
1517 { "INCR", F(atom.incr) },
1518 { "TIMESTAMP", F(atom.timestamp) },
1519 { "MULTIPLE", F(atom.multiple) },
1520 { "UTF8_STRING" , F(atom.utf8_string) },
1521 { "COMPOUND_TEXT", F(atom.compound_text) },
1522 { "TEXT", F(atom.text) },
1523 { "STRING", F(atom.string) },
1524 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1525 { "text/plain", F(atom.text_plain) },
1526 };
1527#undef F
1528
1529 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1530 xcb_xfixes_query_version_reply_t *xfixes_reply;
1531 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1532 xcb_intern_atom_reply_t *reply;
1533 xcb_render_query_pict_formats_reply_t *formats_reply;
1534 xcb_render_query_pict_formats_cookie_t formats_cookie;
1535 xcb_render_pictforminfo_t *formats;
1536 uint32_t i;
1537
1538 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1539
1540 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1541
1542 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1543 cookies[i] = xcb_intern_atom (wm->conn, 0,
1544 strlen(atoms[i].name),
1545 atoms[i].name);
1546
1547 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1548 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1549 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1550 free(reply);
1551 }
1552
1553 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1554 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001555 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001556
1557 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1558 XCB_XFIXES_MAJOR_VERSION,
1559 XCB_XFIXES_MINOR_VERSION);
1560 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1561 xfixes_cookie, NULL);
1562
Martin Minarik6d118362012-06-07 18:01:59 +02001563 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001564 xfixes_reply->major_version, xfixes_reply->minor_version);
1565
1566 free(xfixes_reply);
1567
1568 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1569 formats_cookie, 0);
1570 if (formats_reply == NULL)
1571 return;
1572
1573 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001574 for (i = 0; i < formats_reply->num_formats; i++) {
1575 if (formats[i].direct.red_mask != 0xff &&
1576 formats[i].direct.red_shift != 16)
1577 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001578 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1579 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001580 wm->format_rgb = formats[i];
1581 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1582 formats[i].depth == 32 &&
1583 formats[i].direct.alpha_mask == 0xff &&
1584 formats[i].direct.alpha_shift == 24)
1585 wm->format_rgba = formats[i];
1586 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001587
1588 free(formats_reply);
1589}
1590
1591static void
1592weston_wm_create_wm_window(struct weston_wm *wm)
1593{
1594 static const char name[] = "Weston WM";
1595
1596 wm->wm_window = xcb_generate_id(wm->conn);
1597 xcb_create_window(wm->conn,
1598 XCB_COPY_FROM_PARENT,
1599 wm->wm_window,
1600 wm->screen->root,
1601 0, 0,
1602 10, 10,
1603 0,
1604 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1605 wm->screen->root_visual,
1606 0, NULL);
1607
1608 xcb_change_property(wm->conn,
1609 XCB_PROP_MODE_REPLACE,
1610 wm->wm_window,
1611 wm->atom.net_supporting_wm_check,
1612 XCB_ATOM_WINDOW,
1613 32, /* format */
1614 1, &wm->wm_window);
1615
1616 xcb_change_property(wm->conn,
1617 XCB_PROP_MODE_REPLACE,
1618 wm->wm_window,
1619 wm->atom.net_wm_name,
1620 wm->atom.utf8_string,
1621 8, /* format */
1622 strlen(name), name);
1623
1624 xcb_change_property(wm->conn,
1625 XCB_PROP_MODE_REPLACE,
1626 wm->screen->root,
1627 wm->atom.net_supporting_wm_check,
1628 XCB_ATOM_WINDOW,
1629 32, /* format */
1630 1, &wm->wm_window);
1631
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001632 /* Claim the WM_S0 selection even though we don't suport
1633 * the --replace functionality. */
1634 xcb_set_selection_owner(wm->conn,
1635 wm->wm_window,
1636 wm->atom.wm_s0,
1637 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001638}
1639
1640struct weston_wm *
1641weston_wm_create(struct weston_xserver *wxs)
1642{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001643 struct weston_wm *wm;
1644 struct wl_event_loop *loop;
1645 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001646 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001647 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001648 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001649
1650 wm = malloc(sizeof *wm);
1651 if (wm == NULL)
1652 return NULL;
1653
1654 memset(wm, 0, sizeof *wm);
1655 wm->server = wxs;
1656 wm->window_hash = hash_table_create();
1657 if (wm->window_hash == NULL) {
1658 free(wm);
1659 return NULL;
1660 }
1661
1662 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001663 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001664 hash_table_destroy(wm->window_hash);
1665 free(wm);
1666 return NULL;
1667 }
1668
1669 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001670 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001671 close(sv[1]);
1672
1673 /* xcb_connect_to_fd takes ownership of the fd. */
1674 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1675 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001676 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001677 close(sv[0]);
1678 hash_table_destroy(wm->window_hash);
1679 free(wm);
1680 return NULL;
1681 }
1682
1683 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1684 wm->screen = s.data;
1685
1686 loop = wl_display_get_event_loop(wxs->wl_display);
1687 wm->source =
1688 wl_event_loop_add_fd(loop, sv[0],
1689 WL_EVENT_READABLE,
1690 weston_wm_handle_event, wm);
1691 wl_event_source_check(wm->source);
1692
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001693 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001694 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001695
1696 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001697 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1698 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1699 XCB_EVENT_MASK_PROPERTY_CHANGE;
1700 xcb_change_window_attributes(wm->conn, wm->screen->root,
1701 XCB_CW_EVENT_MASK, values);
1702 wm->theme = theme_create();
1703
1704 weston_wm_create_wm_window(wm);
1705
1706 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001707 supported[1] = wm->atom.net_wm_state;
1708 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001709 xcb_change_property(wm->conn,
1710 XCB_PROP_MODE_REPLACE,
1711 wm->screen->root,
1712 wm->atom.net_supported,
1713 XCB_ATOM_ATOM,
1714 32, /* format */
1715 ARRAY_LENGTH(supported), supported);
1716
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001717 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718
1719 xcb_flush(wm->conn);
1720
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001721 wm->activate_listener.notify = weston_wm_window_activate;
1722 wl_signal_add(&wxs->compositor->activate_signal,
1723 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001724 wm->transform_listener.notify = weston_wm_window_transform;
1725 wl_signal_add(&wxs->compositor->transform_signal,
1726 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001727 wm->kill_listener.notify = weston_wm_kill_client;
1728 wl_signal_add(&wxs->compositor->kill_signal,
1729 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001730
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001731 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001732 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001733
Martin Minarik6d118362012-06-07 18:01:59 +02001734 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001735
1736 return wm;
1737}
1738
1739void
1740weston_wm_destroy(struct weston_wm *wm)
1741{
1742 /* FIXME: Free windows in hash. */
1743 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001744 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001745 xcb_disconnect(wm->conn);
1746 wl_event_source_remove(wm->source);
1747 wl_list_remove(&wm->selection_listener.link);
1748 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001749 wl_list_remove(&wm->kill_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001750
1751 free(wm);
1752}
1753
1754static void
1755surface_destroy(struct wl_listener *listener, void *data)
1756{
1757 struct weston_wm_window *window =
1758 container_of(listener,
1759 struct weston_wm_window, surface_destroy_listener);
1760
Martin Minarik6d118362012-06-07 18:01:59 +02001761 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001762}
1763
1764static struct weston_wm_window *
1765get_wm_window(struct weston_surface *surface)
1766{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001767 struct wl_listener *listener;
1768
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001769 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001770 if (listener)
1771 return container_of(listener, struct weston_wm_window,
1772 surface_destroy_listener);
1773
1774 return NULL;
1775}
1776
1777static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001778weston_wm_window_configure(void *data)
1779{
1780 struct weston_wm_window *window = data;
1781 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001782 uint32_t values[4];
1783 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001784
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001785 weston_wm_window_get_child_position(window, &x, &y);
1786 values[0] = x;
1787 values[1] = y;
1788 values[2] = window->width;
1789 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001790 xcb_configure_window(wm->conn,
1791 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001792 XCB_CONFIG_WINDOW_X |
1793 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001794 XCB_CONFIG_WINDOW_WIDTH |
1795 XCB_CONFIG_WINDOW_HEIGHT,
1796 values);
1797
1798 weston_wm_window_get_frame_size(window, &width, &height);
1799 values[0] = width;
1800 values[1] = height;
1801 xcb_configure_window(wm->conn,
1802 window->frame_id,
1803 XCB_CONFIG_WINDOW_WIDTH |
1804 XCB_CONFIG_WINDOW_HEIGHT,
1805 values);
1806
1807 window->configure_source = NULL;
1808
1809 weston_wm_window_schedule_repaint(window);
1810}
1811
1812static void
1813send_configure(struct weston_surface *surface,
1814 uint32_t edges, int32_t width, int32_t height)
1815{
1816 struct weston_wm_window *window = get_wm_window(surface);
1817 struct weston_wm *wm = window->wm;
1818 struct theme *t = window->wm->theme;
1819
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001820 if (window->fullscreen) {
1821 window->width = width;
1822 window->height = height;
1823 } else if (window->decorate) {
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001824 window->width = width - 2 * (t->margin + t->width);
1825 window->height = height - 2 * t->margin -
1826 t->titlebar_height - t->width;
1827 } else {
1828 window->width = width - 2 * t->margin;
1829 window->height = height - 2 * t->margin;
1830 }
1831
1832 if (window->configure_source)
1833 return;
1834
1835 window->configure_source =
1836 wl_event_loop_add_idle(wm->server->loop,
1837 weston_wm_window_configure, window);
1838}
1839
1840static const struct weston_shell_client shell_client = {
1841 send_configure
1842};
1843
1844static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001845xserver_map_shell_surface(struct weston_wm *wm,
1846 struct weston_wm_window *window)
1847{
1848 struct weston_shell_interface *shell_interface =
1849 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001850 struct theme *t = window->wm->theme;
1851
1852 if (!shell_interface->create_shell_surface)
1853 return;
1854
1855 window->shsurf =
1856 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001857 window->surface,
1858 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001859
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001860 if (window->fullscreen) {
1861 window->saved_width = window->width;
1862 window->saved_height = window->height;
1863 shell_interface->set_fullscreen(window->shsurf,
1864 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1865 0, NULL);
1866 return;
1867 } else if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001868 shell_interface->set_toplevel(window->shsurf);
1869 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001870 } else {
1871 shell_interface->set_xwayland(window->shsurf,
1872 window->x + t->margin,
1873 window->y + t->margin,
1874 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001875 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001876}
1877
1878static void
1879xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1880 struct wl_resource *surface_resource, uint32_t id)
1881{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001882 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001884 struct weston_surface *surface =
1885 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001886 struct weston_wm_window *window;
1887
1888 if (client != wxs->client)
1889 return;
1890
1891 window = hash_table_lookup(wm->window_hash, id);
1892 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001893 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001894 return;
1895 }
1896
Martin Minarik6d118362012-06-07 18:01:59 +02001897 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001898
1899 weston_wm_window_read_properties(window);
1900
1901 window->surface = (struct weston_surface *) surface;
1902 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001903 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001904 &window->surface_destroy_listener);
1905
1906 weston_wm_window_schedule_repaint(window);
1907 xserver_map_shell_surface(wm, window);
1908}
1909
1910const struct xserver_interface xserver_implementation = {
1911 xserver_set_window_id
1912};