blob: a8b949b8fb96dd70be3ef0285c52ff8054d76788 [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;
MoD384a11a2013-06-22 11:04:21 -0500119 int has_alpha;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400120};
121
122static struct weston_wm_window *
123get_wm_window(struct weston_surface *surface);
124
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400125static void
126weston_wm_window_schedule_repaint(struct weston_wm_window *window);
127
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400128static int __attribute__ ((format (printf, 1, 2)))
129wm_log(const char *fmt, ...)
130{
131#ifdef WM_DEBUG
132 int l;
133 va_list argp;
134
135 va_start(argp, fmt);
136 l = weston_vlog(fmt, argp);
137 va_end(argp);
138
139 return l;
140#else
141 return 0;
142#endif
143}
144
145static int __attribute__ ((format (printf, 1, 2)))
146wm_log_continue(const char *fmt, ...)
147{
148#ifdef WM_DEBUG
149 int l;
150 va_list argp;
151
152 va_start(argp, fmt);
153 l = weston_vlog_continue(fmt, argp);
154 va_end(argp);
155
156 return l;
157#else
158 return 0;
159#endif
160}
161
162
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400163const char *
164get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
165{
166 xcb_get_atom_name_cookie_t cookie;
167 xcb_get_atom_name_reply_t *reply;
168 xcb_generic_error_t *e;
169 static char buffer[64];
170
171 if (atom == XCB_ATOM_NONE)
172 return "None";
173
174 cookie = xcb_get_atom_name (c, atom);
175 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500176
177 if(reply) {
178 snprintf(buffer, sizeof buffer, "%.*s",
179 xcb_get_atom_name_name_length (reply),
180 xcb_get_atom_name_name (reply));
181 } else {
182 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
183 }
184
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400185 free(reply);
186
187 return buffer;
188}
189
Tiago Vignatti90fada42012-07-16 12:02:08 -0400190static xcb_cursor_t
191xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
192{
193 xcb_connection_t *c = wm->conn;
194 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
195 xcb_screen_t *screen = s.data;
196 xcb_gcontext_t gc;
197 xcb_pixmap_t pix;
198 xcb_render_picture_t pic;
199 xcb_cursor_t cursor;
200 int stride = img->width * 4;
201
202 pix = xcb_generate_id(c);
203 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
204
205 pic = xcb_generate_id(c);
206 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
207
208 gc = xcb_generate_id(c);
209 xcb_create_gc(c, gc, pix, 0, 0);
210
211 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
212 img->width, img->height, 0, 0, 0, 32,
213 stride * img->height, (uint8_t *) img->pixels);
214 xcb_free_gc(c, gc);
215
216 cursor = xcb_generate_id(c);
217 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
218
219 xcb_render_free_picture(c, pic);
220 xcb_free_pixmap(c, pix);
221
222 return cursor;
223}
224
225static xcb_cursor_t
226xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
227{
228 /* TODO: treat animated cursors as well */
229 if (images->nimage != 1)
230 return -1;
231
232 return xcb_cursor_image_load_cursor(wm, images->images[0]);
233}
234
235static xcb_cursor_t
236xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
237{
238 xcb_cursor_t cursor;
239 XcursorImages *images;
240 char *v = NULL;
241 int size = 0;
242
243 if (!file)
244 return 0;
245
246 v = getenv ("XCURSOR_SIZE");
247 if (v)
248 size = atoi(v);
249
250 if (!size)
251 size = 32;
252
253 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300254 if (!images)
255 return -1;
256
Tiago Vignatti90fada42012-07-16 12:02:08 -0400257 cursor = xcb_cursor_images_load_cursor (wm, images);
258 XcursorImagesDestroy (images);
259
260 return cursor;
261}
262
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400263void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400264dump_property(struct weston_wm *wm,
265 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400266{
267 int32_t *incr_value;
268 const char *text_value, *name;
269 xcb_atom_t *atom_value;
270 int width, len;
271 uint32_t i;
272
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400273 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400274 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400275 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400276 return;
277 }
278
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400279 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
280 get_atom_name(wm->conn, reply->type),
281 reply->format,
282 xcb_get_property_value_length(reply),
283 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400284
285 if (reply->type == wm->atom.incr) {
286 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400287 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400288 } else if (reply->type == wm->atom.utf8_string ||
289 reply->type == wm->atom.string) {
290 text_value = xcb_get_property_value(reply);
291 if (reply->value_len > 40)
292 len = 40;
293 else
294 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400295 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400296 } else if (reply->type == XCB_ATOM_ATOM) {
297 atom_value = xcb_get_property_value(reply);
298 for (i = 0; i < reply->value_len; i++) {
299 name = get_atom_name(wm->conn, atom_value[i]);
300 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400301 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400302 width = 4;
303 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400304 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400305 }
306
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400307 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400308 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400309 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400310 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400311 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312 }
313}
314
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200315static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400316read_and_dump_property(struct weston_wm *wm,
317 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400318{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400319 xcb_get_property_reply_t *reply;
320 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400321
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400322 cookie = xcb_get_property(wm->conn, 0, window,
323 property, XCB_ATOM_ANY, 0, 2048);
324 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400325
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400326 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400327
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400328 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400329}
330
331/* We reuse some predefined, but otherwise useles atoms */
332#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
333#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500334#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400335
336static void
337weston_wm_window_read_properties(struct weston_wm_window *window)
338{
339 struct weston_wm *wm = window->wm;
340
341#define F(field) offsetof(struct weston_wm_window, field)
342 const struct {
343 xcb_atom_t atom;
344 xcb_atom_t type;
345 int offset;
346 } props[] = {
347 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
348 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
349 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
350 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500351 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
353 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300354 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400355 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300356 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357 };
358#undef F
359
360 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
361 xcb_get_property_reply_t *reply;
362 void *p;
363 uint32_t *xid;
364 xcb_atom_t *atom;
365 uint32_t i;
366 struct motif_wm_hints *hints;
367
368 if (!window->properties_dirty)
369 return;
370 window->properties_dirty = 0;
371
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400372 for (i = 0; i < ARRAY_LENGTH(props); i++)
373 cookie[i] = xcb_get_property(wm->conn,
374 0, /* delete */
375 window->id,
376 props[i].atom,
377 XCB_ATOM_ANY, 0, 2048);
378
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300379 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400380 for (i = 0; i < ARRAY_LENGTH(props); i++) {
381 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
382 if (!reply)
383 /* Bad window, typically */
384 continue;
385 if (reply->type == XCB_ATOM_NONE) {
386 /* No such property */
387 free(reply);
388 continue;
389 }
390
391 p = ((char *) window + props[i].offset);
392
393 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300394 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395 case XCB_ATOM_STRING:
396 /* FIXME: We're using this for both string and
397 utf8_string */
398 if (*(char **) p)
399 free(*(char **) p);
400
401 *(char **) p =
402 strndup(xcb_get_property_value(reply),
403 xcb_get_property_value_length(reply));
404 break;
405 case XCB_ATOM_WINDOW:
406 xid = xcb_get_property_value(reply);
407 *(struct weston_wm_window **) p =
408 hash_table_lookup(wm->window_hash, *xid);
409 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300410 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 case XCB_ATOM_ATOM:
412 atom = xcb_get_property_value(reply);
413 *(xcb_atom_t *) p = *atom;
414 break;
415 case TYPE_WM_PROTOCOLS:
416 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500417 case TYPE_NET_WM_STATE:
418 window->fullscreen = 0;
419 atom = xcb_get_property_value(reply);
420 for (i = 0; i < reply->value_len; i++)
421 if (atom[i] == wm->atom.net_wm_state_fullscreen)
422 window->fullscreen = 1;
423 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424 case TYPE_MOTIF_WM_HINTS:
425 hints = xcb_get_property_value(reply);
426 if (hints->flags & MWM_HINTS_DECORATIONS)
427 window->decorate = hints->decorations > 0;
428 break;
429 default:
430 break;
431 }
432 free(reply);
433 }
434}
435
436static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400437weston_wm_window_get_frame_size(struct weston_wm_window *window,
438 int *width, int *height)
439{
440 struct theme *t = window->wm->theme;
441
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500442 if (window->fullscreen) {
443 *width = window->width;
444 *height = window->height;
445 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400446 *width = window->width + (t->margin + t->width) * 2;
447 *height = window->height +
448 t->margin * 2 + t->width + t->titlebar_height;
449 } else {
450 *width = window->width + t->margin * 2;
451 *height = window->height + t->margin * 2;
452 }
453}
454
455static void
456weston_wm_window_get_child_position(struct weston_wm_window *window,
457 int *x, int *y)
458{
459 struct theme *t = window->wm->theme;
460
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500461 if (window->fullscreen) {
462 *x = 0;
463 *y = 0;
464 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 *x = t->margin + t->width;
466 *y = t->margin + t->titlebar_height;
467 } else {
468 *x = t->margin;
469 *y = t->margin;
470 }
471}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400472
473static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500474weston_wm_window_send_configure_notify(struct weston_wm_window *window)
475{
476 xcb_configure_notify_event_t configure_notify;
477 struct weston_wm *wm = window->wm;
478 int x, y;
479
480 weston_wm_window_get_child_position(window, &x, &y);
481 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
482 configure_notify.pad0 = 0;
483 configure_notify.event = window->id;
484 configure_notify.window = window->id;
485 configure_notify.above_sibling = XCB_WINDOW_NONE;
486 configure_notify.x = x;
487 configure_notify.y = y;
488 configure_notify.width = window->width;
489 configure_notify.height = window->height;
490 configure_notify.border_width = 0;
491 configure_notify.override_redirect = 0;
492 configure_notify.pad1 = 0;
493
494 xcb_send_event(wm->conn, 0, window->id,
495 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
496 (char *) &configure_notify);
497}
498
499static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400500weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
501{
502 xcb_configure_request_event_t *configure_request =
503 (xcb_configure_request_event_t *) event;
504 struct weston_wm_window *window;
505 uint32_t mask, values[16];
506 int x, y, width, height, i = 0;
507
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400508 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
509 configure_request->window,
510 configure_request->x, configure_request->y,
511 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400512
513 window = hash_table_lookup(wm->window_hash, configure_request->window);
514
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500515 if (window->fullscreen) {
516 weston_wm_window_send_configure_notify(window);
517 return;
518 }
519
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400520 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
521 window->width = configure_request->width;
522 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
523 window->height = configure_request->height;
524
525 weston_wm_window_get_child_position(window, &x, &y);
526 values[i++] = x;
527 values[i++] = y;
528 values[i++] = window->width;
529 values[i++] = window->height;
530 values[i++] = 0;
531 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
532 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
533 XCB_CONFIG_WINDOW_BORDER_WIDTH;
534 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
535 values[i++] = configure_request->sibling;
536 mask |= XCB_CONFIG_WINDOW_SIBLING;
537 }
538 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
539 values[i++] = configure_request->stack_mode;
540 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
541 }
542
543 xcb_configure_window(wm->conn, window->id, mask, values);
544
545 weston_wm_window_get_frame_size(window, &width, &height);
546 values[0] = width;
547 values[1] = height;
548 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
549 xcb_configure_window(wm->conn, window->frame_id, mask, values);
550
551 weston_wm_window_schedule_repaint(window);
552}
553
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400554static int
555our_resource(struct weston_wm *wm, uint32_t id)
556{
557 const xcb_setup_t *setup;
558
559 setup = xcb_get_setup(wm->conn);
560
561 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
562}
563
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400564static void
565weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
566{
567 xcb_configure_notify_event_t *configure_notify =
568 (xcb_configure_notify_event_t *) event;
569 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300570 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400571
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400572 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400573 configure_notify->window,
574 configure_notify->x, configure_notify->y,
575 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300576
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400577 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300578 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700579 window->x = configure_notify->x;
580 window->y = configure_notify->y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400581}
582
583static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300584weston_wm_kill_client(struct wl_listener *listener, void *data)
585{
586 struct weston_surface *surface = data;
587 struct weston_wm_window *window = get_wm_window(surface);
588 char name[1024];
589
590 if (!window)
591 return;
592
593 gethostname(name, 1024);
594
595 /* this is only one heuristic to guess the PID of a client is valid,
596 * assuming it's compliant with icccm and ewmh. Non-compliants and
597 * remote applications of course fail. */
598 if (!strcmp(window->machine, name) && window->pid != 0)
599 kill(window->pid, SIGKILL);
600}
601
602static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400603weston_wm_window_activate(struct wl_listener *listener, void *data)
604{
605 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200606 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600607 struct weston_wm *wm =
608 container_of(listener, struct weston_wm, activate_listener);
609 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400610
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200611 if (surface) {
612 window = get_wm_window(surface);
613 }
614
Scott Moreau85ecac02012-05-21 15:49:14 -0600615 if (window) {
616 client_message.response_type = XCB_CLIENT_MESSAGE;
617 client_message.format = 32;
618 client_message.window = window->id;
619 client_message.type = wm->atom.wm_protocols;
620 client_message.data.data32[0] = wm->atom.wm_take_focus;
621 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
622
623 xcb_send_event(wm->conn, 0, window->id,
624 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
625 (char *) &client_message);
626
627 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
628 window->id, XCB_TIME_CURRENT_TIME);
629 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400630 xcb_set_input_focus (wm->conn,
631 XCB_INPUT_FOCUS_POINTER_ROOT,
632 XCB_NONE,
633 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600634 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400635
636 if (wm->focus_window)
637 weston_wm_window_schedule_repaint(wm->focus_window);
638 wm->focus_window = window;
639 if (wm->focus_window)
640 weston_wm_window_schedule_repaint(wm->focus_window);
641}
642
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300643static void
644weston_wm_window_transform(struct wl_listener *listener, void *data)
645{
646 struct weston_surface *surface = data;
647 struct weston_wm_window *window = get_wm_window(surface);
648 struct weston_wm *wm =
649 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300650 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300651
652 if (!window || !wm)
653 return;
654
655 if (!weston_surface_is_mapped(surface))
656 return;
657
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700658 if (window->x != surface->geometry.x ||
659 window->y != surface->geometry.y) {
660 values[0] = surface->geometry.x;
661 values[1] = surface->geometry.y;
662 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300663
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700664 xcb_configure_window(wm->conn, window->frame_id, mask, values);
665 xcb_flush(wm->conn);
666 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300667}
668
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400669#define ICCCM_WITHDRAWN_STATE 0
670#define ICCCM_NORMAL_STATE 1
671#define ICCCM_ICONIC_STATE 3
672
673static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500674weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400675{
676 struct weston_wm *wm = window->wm;
677 uint32_t property[2];
678
679 property[0] = state;
680 property[1] = XCB_WINDOW_NONE;
681
682 xcb_change_property(wm->conn,
683 XCB_PROP_MODE_REPLACE,
684 window->id,
685 wm->atom.wm_state,
686 wm->atom.wm_state,
687 32, /* format */
688 2, property);
689}
690
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400691static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500692weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
693{
694 struct weston_wm *wm = window->wm;
695 uint32_t property[1];
696 int i;
697
698 i = 0;
699 if (window->fullscreen)
700 property[i++] = wm->atom.net_wm_state_fullscreen;
701
702 xcb_change_property(wm->conn,
703 XCB_PROP_MODE_REPLACE,
704 window->id,
705 wm->atom.net_wm_state,
706 XCB_ATOM_ATOM,
707 32, /* format */
708 i, property);
709}
710
711static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400712weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
713{
714 xcb_map_request_event_t *map_request =
715 (xcb_map_request_event_t *) event;
716 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400717 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400718 int x, y, width, height;
719
720 if (our_resource(wm, map_request->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400721 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
722 map_request->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400723 return;
724 }
725
726 window = hash_table_lookup(wm->window_hash, map_request->window);
727
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400728 if (window->frame_id)
729 return;
730
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400731 weston_wm_window_read_properties(window);
732
733 weston_wm_window_get_frame_size(window, &width, &height);
734 weston_wm_window_get_child_position(window, &x, &y);
735
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400736 values[0] = wm->screen->black_pixel;
737 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400738 XCB_EVENT_MASK_KEY_PRESS |
739 XCB_EVENT_MASK_KEY_RELEASE |
740 XCB_EVENT_MASK_BUTTON_PRESS |
741 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400742 XCB_EVENT_MASK_POINTER_MOTION |
743 XCB_EVENT_MASK_ENTER_WINDOW |
744 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400746 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400747 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400748
749 window->frame_id = xcb_generate_id(wm->conn);
750 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400751 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400752 window->frame_id,
753 wm->screen->root,
754 0, 0,
755 width, height,
756 0,
757 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400758 wm->visual_id,
759 XCB_CW_BORDER_PIXEL |
760 XCB_CW_EVENT_MASK |
761 XCB_CW_COLORMAP, values);
762
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400763 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
764
765 values[0] = 0;
766 xcb_configure_window(wm->conn, window->id,
767 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
768
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400769 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
770 window->id, window, window->frame_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400771
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500772 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
773 weston_wm_window_set_net_wm_state(window);
774
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400775 xcb_map_window(wm->conn, map_request->window);
776 xcb_map_window(wm->conn, window->frame_id);
777
778 window->cairo_surface =
779 cairo_xcb_surface_create_with_xrender_format(wm->conn,
780 wm->screen,
781 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400782 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400783 width, height);
784
785 hash_table_insert(wm->window_hash, window->frame_id, window);
786}
787
788static void
789weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
790{
791 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
792
793 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400794 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
795 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400796 return;
797 }
798
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400799 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800}
801
802static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400803weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
804{
805 xcb_unmap_notify_event_t *unmap_notify =
806 (xcb_unmap_notify_event_t *) event;
807 struct weston_wm_window *window;
808
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400809 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
810 unmap_notify->window,
811 unmap_notify->event,
812 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400813
814 if (our_resource(wm, unmap_notify->window))
815 return;
816
MoD31700122013-06-11 19:58:55 -0500817 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400818 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
819 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400820 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400821
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400822 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400823 if (window->repaint_source)
824 wl_event_source_remove(window->repaint_source);
825 if (window->cairo_surface)
826 cairo_surface_destroy(window->cairo_surface);
827
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400828 if (window->frame_id) {
829 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
830 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500831 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400832 hash_table_remove(wm->window_hash, window->frame_id);
833 window->frame_id = XCB_WINDOW_NONE;
834 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400835
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400836 if (wm->focus_window == window)
837 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400838 if (window->surface)
839 wl_list_remove(&window->surface_destroy_listener.link);
840 window->surface = NULL;
841}
842
843static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400844weston_wm_window_draw_decoration(void *data)
845{
846 struct weston_wm_window *window = data;
847 struct weston_wm *wm = window->wm;
848 struct theme *t = wm->theme;
849 cairo_t *cr;
850 int x, y, width, height;
851 const char *title;
852 uint32_t flags = 0;
853
854 weston_wm_window_read_properties(window);
855
856 window->repaint_source = NULL;
857
858 weston_wm_window_get_frame_size(window, &width, &height);
859 weston_wm_window_get_child_position(window, &x, &y);
860
861 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
862 cr = cairo_create(window->cairo_surface);
863
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500864 if (window->fullscreen) {
865 /* nothing */
866 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867 if (wm->focus_window == window)
868 flags |= THEME_FRAME_ACTIVE;
869
870 if (window->name)
871 title = window->name;
872 else
873 title = "untitled";
874
875 theme_render_frame(t, cr, width, height, title, flags);
876 } else {
877 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
878 cairo_set_source_rgba(cr, 0, 0, 0, 0);
879 cairo_paint(cr);
880
881 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
882 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
883 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
884 }
885
886 cairo_destroy(cr);
887
888 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700889 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500890 if(window->has_alpha) {
891 pixman_region32_init(&window->surface->pending.opaque);
892 } else {
893 /* We leave an extra pixel around the X window area to
894 * make sure we don't sample from the undefined alpha
895 * channel when filtering. */
896 pixman_region32_init_rect(&window->surface->pending.opaque,
897 x - 1, y - 1,
898 window->width + 2,
899 window->height + 2);
900 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200901 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500902 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400903
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500904 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500905 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500906 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400907 t->margin, t->margin,
908 width - 2 * t->margin,
909 height - 2 * t->margin);
910 }
911}
912
913static void
914weston_wm_window_schedule_repaint(struct weston_wm_window *window)
915{
916 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300917 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400918
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400919 if (window->frame_id == XCB_WINDOW_NONE) {
920 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300921 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700922 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500923 if(window->has_alpha) {
924 pixman_region32_init(&window->surface->pending.opaque);
925 } else {
926 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
927 width, height);
928 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200929 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400930 }
931 return;
932 }
933
934 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400935 return;
936
937 window->repaint_source =
938 wl_event_loop_add_idle(wm->server->loop,
939 weston_wm_window_draw_decoration,
940 window);
941}
942
943static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400944weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
945{
946 xcb_property_notify_event_t *property_notify =
947 (xcb_property_notify_event_t *) event;
948 struct weston_wm_window *window;
949
950 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000951 if (!window)
952 return;
953
954 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400956 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400957 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400958 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400959 else
960 read_and_dump_property(wm, property_notify->window,
961 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400962
963 if (property_notify->atom == wm->atom.net_wm_name ||
964 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400965 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400966}
967
968static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400969weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300970 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400971{
972 struct weston_wm_window *window;
973 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -0500974 xcb_get_geometry_cookie_t geometry_cookie;
975 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400976
Peter Huttererf3d62272013-08-08 11:57:05 +1000977 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400978 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400979 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400980 return;
981 }
982
MoD384a11a2013-06-22 11:04:21 -0500983 geometry_cookie = xcb_get_geometry(wm->conn, id);
984
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400985 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
986 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
987
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400988 window->wm = wm;
989 window->id = id;
990 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300991 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400992 window->width = width;
993 window->height = height;
994
MoD384a11a2013-06-22 11:04:21 -0500995 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
996 /* technically we should use XRender and check the visual format's
997 alpha_mask, but checking depth is simpler and works in all known cases */
998 if(geometry_reply != NULL)
999 window->has_alpha = geometry_reply->depth == 32;
1000 free(geometry_reply);
1001
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001002 hash_table_insert(wm->window_hash, id, window);
1003}
1004
1005static void
1006weston_wm_window_destroy(struct weston_wm_window *window)
1007{
1008 hash_table_remove(window->wm->window_hash, window->id);
1009 free(window);
1010}
1011
1012static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001013weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1014{
1015 xcb_create_notify_event_t *create_notify =
1016 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001017
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001018 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1019 create_notify->window,
1020 create_notify->width, create_notify->height,
1021 create_notify->override_redirect ? ", override" : "",
1022 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001023
1024 if (our_resource(wm, create_notify->window))
1025 return;
1026
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001027 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001028 create_notify->width, create_notify->height,
1029 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001030}
1031
1032static void
1033weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1034{
1035 xcb_destroy_notify_event_t *destroy_notify =
1036 (xcb_destroy_notify_event_t *) event;
1037 struct weston_wm_window *window;
1038
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001039 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1040 destroy_notify->window,
1041 destroy_notify->event,
1042 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001043
1044 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001045 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001046
1047 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001048 weston_wm_window_destroy(window);
1049}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001051static void
1052weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1053{
1054 xcb_reparent_notify_event_t *reparent_notify =
1055 (xcb_reparent_notify_event_t *) event;
1056 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001057
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001058 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1059 reparent_notify->window,
1060 reparent_notify->parent,
1061 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001062
1063 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001064 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1065 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001066 } else if (!our_resource(wm, reparent_notify->parent)) {
1067 window = hash_table_lookup(wm->window_hash,
1068 reparent_notify->window);
1069 weston_wm_window_destroy(window);
1070 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001071}
1072
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001073struct weston_seat *
1074weston_wm_pick_seat(struct weston_wm *wm)
1075{
1076 return container_of(wm->server->compositor->seat_list.next,
1077 struct weston_seat, link);
1078}
1079
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001080static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001081weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1082 xcb_client_message_event_t *client_message)
1083{
1084 static const int map[] = {
1085 THEME_LOCATION_RESIZING_TOP_LEFT,
1086 THEME_LOCATION_RESIZING_TOP,
1087 THEME_LOCATION_RESIZING_TOP_RIGHT,
1088 THEME_LOCATION_RESIZING_RIGHT,
1089 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1090 THEME_LOCATION_RESIZING_BOTTOM,
1091 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1092 THEME_LOCATION_RESIZING_LEFT
1093 };
1094
1095 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001096 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001097 int detail;
1098 struct weston_shell_interface *shell_interface =
1099 &wm->server->compositor->shell_interface;
1100
Kristian Høgsberge3148752013-05-06 23:19:49 -04001101 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001102 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001103 return;
1104
1105 detail = client_message->data.data32[2];
1106 switch (detail) {
1107 case _NET_WM_MOVERESIZE_MOVE:
1108 shell_interface->move(window->shsurf, seat);
1109 break;
1110 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1111 case _NET_WM_MOVERESIZE_SIZE_TOP:
1112 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1113 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1114 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1115 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1116 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1117 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1118 shell_interface->resize(window->shsurf, seat, map[detail]);
1119 break;
1120 case _NET_WM_MOVERESIZE_CANCEL:
1121 break;
1122 }
1123}
1124
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001125#define _NET_WM_STATE_REMOVE 0
1126#define _NET_WM_STATE_ADD 1
1127#define _NET_WM_STATE_TOGGLE 2
1128
1129static int
1130update_state(int action, int *state)
1131{
1132 int new_state, changed;
1133
1134 switch (action) {
1135 case _NET_WM_STATE_REMOVE:
1136 new_state = 0;
1137 break;
1138 case _NET_WM_STATE_ADD:
1139 new_state = 1;
1140 break;
1141 case _NET_WM_STATE_TOGGLE:
1142 new_state = !*state;
1143 break;
1144 default:
1145 return 0;
1146 }
1147
1148 changed = (*state != new_state);
1149 *state = new_state;
1150
1151 return changed;
1152}
1153
1154static void
1155weston_wm_window_configure(void *data);
1156
1157static void
1158weston_wm_window_handle_state(struct weston_wm_window *window,
1159 xcb_client_message_event_t *client_message)
1160{
1161 struct weston_wm *wm = window->wm;
1162 struct weston_shell_interface *shell_interface =
1163 &wm->server->compositor->shell_interface;
1164 uint32_t action, property;
1165
1166 action = client_message->data.data32[0];
1167 property = client_message->data.data32[1];
1168
1169 if (property == wm->atom.net_wm_state_fullscreen &&
1170 update_state(action, &window->fullscreen)) {
1171 weston_wm_window_set_net_wm_state(window);
1172 if (window->fullscreen) {
1173 window->saved_width = window->width;
1174 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001175
1176 if (window->shsurf)
1177 shell_interface->set_fullscreen(window->shsurf,
1178 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1179 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001180 } else {
1181 shell_interface->set_toplevel(window->shsurf);
1182 window->width = window->saved_width;
1183 window->height = window->saved_height;
1184 weston_wm_window_configure(window);
1185 }
1186 }
1187}
1188
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001189static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001190weston_wm_handle_client_message(struct weston_wm *wm,
1191 xcb_generic_event_t *event)
1192{
1193 xcb_client_message_event_t *client_message =
1194 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001195 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001196
1197 window = hash_table_lookup(wm->window_hash, client_message->window);
1198
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001199 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1200 get_atom_name(wm->conn, client_message->type),
1201 client_message->data.data32[0],
1202 client_message->data.data32[1],
1203 client_message->data.data32[2],
1204 client_message->data.data32[3],
1205 client_message->data.data32[4],
1206 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001207
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001208 if (client_message->type == wm->atom.net_wm_moveresize)
1209 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001210 else if (client_message->type == wm->atom.net_wm_state)
1211 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001212}
1213
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001214enum cursor_type {
1215 XWM_CURSOR_TOP,
1216 XWM_CURSOR_BOTTOM,
1217 XWM_CURSOR_LEFT,
1218 XWM_CURSOR_RIGHT,
1219 XWM_CURSOR_TOP_LEFT,
1220 XWM_CURSOR_TOP_RIGHT,
1221 XWM_CURSOR_BOTTOM_LEFT,
1222 XWM_CURSOR_BOTTOM_RIGHT,
1223 XWM_CURSOR_LEFT_PTR,
1224};
1225
1226static const char *cursors[] = {
1227 "top_side",
1228 "bottom_side",
1229 "left_side",
1230 "right_side",
1231 "top_left_corner",
1232 "top_right_corner",
1233 "bottom_left_corner",
1234 "bottom_right_corner",
1235 "left_ptr"
1236};
1237
1238static void
1239weston_wm_create_cursors(struct weston_wm *wm)
1240{
1241 int i, count = ARRAY_LENGTH(cursors);
1242
1243 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1244 for (i = 0; i < count; i++) {
1245 wm->cursors[i] =
1246 xcb_cursor_library_load_cursor(wm, cursors[i]);
1247 }
1248
1249 wm->last_cursor = -1;
1250}
1251
1252static void
1253weston_wm_destroy_cursors(struct weston_wm *wm)
1254{
1255 uint8_t i;
1256
1257 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1258 xcb_free_cursor(wm->conn, wm->cursors[i]);
1259
1260 free(wm->cursors);
1261}
1262
1263static int
1264get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1265{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001266 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001267
1268 switch (location) {
1269 case THEME_LOCATION_RESIZING_TOP:
1270 return XWM_CURSOR_TOP;
1271 case THEME_LOCATION_RESIZING_BOTTOM:
1272 return XWM_CURSOR_BOTTOM;
1273 case THEME_LOCATION_RESIZING_LEFT:
1274 return XWM_CURSOR_LEFT;
1275 case THEME_LOCATION_RESIZING_RIGHT:
1276 return XWM_CURSOR_RIGHT;
1277 case THEME_LOCATION_RESIZING_TOP_LEFT:
1278 return XWM_CURSOR_TOP_LEFT;
1279 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1280 return XWM_CURSOR_TOP_RIGHT;
1281 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1282 return XWM_CURSOR_BOTTOM_LEFT;
1283 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1284 return XWM_CURSOR_BOTTOM_RIGHT;
1285 case THEME_LOCATION_EXTERIOR:
1286 case THEME_LOCATION_TITLEBAR:
1287 default:
1288 return XWM_CURSOR_LEFT_PTR;
1289 }
1290}
1291
1292static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001293weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1294 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001295{
1296 uint32_t cursor_value_list;
1297
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001298 if (wm->last_cursor == cursor)
1299 return;
1300
1301 wm->last_cursor = cursor;
1302
1303 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001304 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001305 XCB_CW_CURSOR, &cursor_value_list);
1306 xcb_flush(wm->conn);
1307}
1308
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001309static void
1310weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1311{
1312 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1313 struct weston_shell_interface *shell_interface =
1314 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001315 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001316 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001317 enum theme_location location;
1318 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001319 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001320
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001321 wm_log("XCB_BUTTON_%s (detail %d)\n",
1322 button->response_type == XCB_BUTTON_PRESS ?
1323 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001324
1325 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001326 weston_wm_window_get_frame_size(window, &width, &height);
1327
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001328 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001329 button->detail == 1) {
1330 location = theme_get_location(t,
1331 button->event_x,
1332 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001333 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001334
1335 switch (location) {
1336 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001337 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001338 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001339 case THEME_LOCATION_RESIZING_TOP:
1340 case THEME_LOCATION_RESIZING_BOTTOM:
1341 case THEME_LOCATION_RESIZING_LEFT:
1342 case THEME_LOCATION_RESIZING_RIGHT:
1343 case THEME_LOCATION_RESIZING_TOP_LEFT:
1344 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1345 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1346 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1347 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001348 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001349 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001350 default:
1351 break;
1352 }
1353 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001354}
1355
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001356static void
1357weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1358{
1359 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1360 struct weston_wm_window *window;
1361 int cursor, width, height;
1362
1363 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001364 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001365 return;
1366
1367 weston_wm_window_get_frame_size(window, &width, &height);
1368 cursor = get_cursor_for_location(wm->theme, width, height,
1369 motion->event_x, motion->event_y);
1370
Tiago Vignattic1903232012-07-16 12:15:37 -04001371 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001372}
1373
1374static void
1375weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1376{
1377 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1378 struct weston_wm_window *window;
1379 int cursor, width, height;
1380
1381 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001382 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001383 return;
1384
1385 weston_wm_window_get_frame_size(window, &width, &height);
1386 cursor = get_cursor_for_location(wm->theme, width, height,
1387 enter->event_x, enter->event_y);
1388
Tiago Vignattic1903232012-07-16 12:15:37 -04001389 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001390}
1391
1392static void
1393weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1394{
1395 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1396 struct weston_wm_window *window;
1397
1398 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001399 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001400 return;
1401
Tiago Vignattic1903232012-07-16 12:15:37 -04001402 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001403}
1404
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001405static int
1406weston_wm_handle_event(int fd, uint32_t mask, void *data)
1407{
1408 struct weston_wm *wm = data;
1409 xcb_generic_event_t *event;
1410 int count = 0;
1411
1412 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1413 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001414 free(event);
1415 count++;
1416 continue;
1417 }
1418
MoD31700122013-06-11 19:58:55 -05001419 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001420 case XCB_BUTTON_PRESS:
1421 case XCB_BUTTON_RELEASE:
1422 weston_wm_handle_button(wm, event);
1423 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001424 case XCB_ENTER_NOTIFY:
1425 weston_wm_handle_enter(wm, event);
1426 break;
1427 case XCB_LEAVE_NOTIFY:
1428 weston_wm_handle_leave(wm, event);
1429 break;
1430 case XCB_MOTION_NOTIFY:
1431 weston_wm_handle_motion(wm, event);
1432 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001433 case XCB_CREATE_NOTIFY:
1434 weston_wm_handle_create_notify(wm, event);
1435 break;
1436 case XCB_MAP_REQUEST:
1437 weston_wm_handle_map_request(wm, event);
1438 break;
1439 case XCB_MAP_NOTIFY:
1440 weston_wm_handle_map_notify(wm, event);
1441 break;
1442 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001443 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001444 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001445 case XCB_REPARENT_NOTIFY:
1446 weston_wm_handle_reparent_notify(wm, event);
1447 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001448 case XCB_CONFIGURE_REQUEST:
1449 weston_wm_handle_configure_request(wm, event);
1450 break;
1451 case XCB_CONFIGURE_NOTIFY:
1452 weston_wm_handle_configure_notify(wm, event);
1453 break;
1454 case XCB_DESTROY_NOTIFY:
1455 weston_wm_handle_destroy_notify(wm, event);
1456 break;
1457 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001458 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001459 break;
1460 case XCB_PROPERTY_NOTIFY:
1461 weston_wm_handle_property_notify(wm, event);
1462 break;
1463 case XCB_CLIENT_MESSAGE:
1464 weston_wm_handle_client_message(wm, event);
1465 break;
1466 }
1467
1468 free(event);
1469 count++;
1470 }
1471
1472 xcb_flush(wm->conn);
1473
1474 return count;
1475}
1476
1477static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001478weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1479{
1480 xcb_depth_iterator_t d_iter;
1481 xcb_visualtype_iterator_t vt_iter;
1482 xcb_visualtype_t *visualtype;
1483
1484 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1485 visualtype = NULL;
1486 while (d_iter.rem > 0) {
1487 if (d_iter.data->depth == 32) {
1488 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1489 visualtype = vt_iter.data;
1490 break;
1491 }
1492
1493 xcb_depth_next(&d_iter);
1494 }
1495
1496 if (visualtype == NULL) {
1497 weston_log("no 32 bit visualtype\n");
1498 return;
1499 }
1500
1501 wm->visual_id = visualtype->visual_id;
1502 wm->colormap = xcb_generate_id(wm->conn);
1503 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1504 wm->colormap, wm->screen->root, wm->visual_id);
1505}
1506
1507static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001508weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001509{
1510
1511#define F(field) offsetof(struct weston_wm, field)
1512
1513 static const struct { const char *name; int offset; } atoms[] = {
1514 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1515 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1516 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001517 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001518 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001519 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001520 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001521 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001522 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001523 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1524 { "_NET_WM_STATE", F(atom.net_wm_state) },
1525 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1526 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1527 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1528 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1529
1530 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1531 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1532 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1533 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1534 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1535 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1536 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001537 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1538 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001539 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1540 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1541 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1542 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1543 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1544
1545 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1546 { "_NET_SUPPORTING_WM_CHECK",
1547 F(atom.net_supporting_wm_check) },
1548 { "_NET_SUPPORTED", F(atom.net_supported) },
1549 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1550 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001551 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001552 { "TARGETS", F(atom.targets) },
1553 { "UTF8_STRING", F(atom.utf8_string) },
1554 { "_WL_SELECTION", F(atom.wl_selection) },
1555 { "INCR", F(atom.incr) },
1556 { "TIMESTAMP", F(atom.timestamp) },
1557 { "MULTIPLE", F(atom.multiple) },
1558 { "UTF8_STRING" , F(atom.utf8_string) },
1559 { "COMPOUND_TEXT", F(atom.compound_text) },
1560 { "TEXT", F(atom.text) },
1561 { "STRING", F(atom.string) },
1562 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1563 { "text/plain", F(atom.text_plain) },
1564 };
1565#undef F
1566
1567 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1568 xcb_xfixes_query_version_reply_t *xfixes_reply;
1569 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1570 xcb_intern_atom_reply_t *reply;
1571 xcb_render_query_pict_formats_reply_t *formats_reply;
1572 xcb_render_query_pict_formats_cookie_t formats_cookie;
1573 xcb_render_pictforminfo_t *formats;
1574 uint32_t i;
1575
1576 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1577
1578 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1579
1580 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1581 cookies[i] = xcb_intern_atom (wm->conn, 0,
1582 strlen(atoms[i].name),
1583 atoms[i].name);
1584
1585 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1586 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1587 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1588 free(reply);
1589 }
1590
1591 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1592 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001593 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001594
1595 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1596 XCB_XFIXES_MAJOR_VERSION,
1597 XCB_XFIXES_MINOR_VERSION);
1598 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1599 xfixes_cookie, NULL);
1600
Martin Minarik6d118362012-06-07 18:01:59 +02001601 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001602 xfixes_reply->major_version, xfixes_reply->minor_version);
1603
1604 free(xfixes_reply);
1605
1606 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1607 formats_cookie, 0);
1608 if (formats_reply == NULL)
1609 return;
1610
1611 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001612 for (i = 0; i < formats_reply->num_formats; i++) {
1613 if (formats[i].direct.red_mask != 0xff &&
1614 formats[i].direct.red_shift != 16)
1615 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001616 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1617 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001618 wm->format_rgb = formats[i];
1619 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1620 formats[i].depth == 32 &&
1621 formats[i].direct.alpha_mask == 0xff &&
1622 formats[i].direct.alpha_shift == 24)
1623 wm->format_rgba = formats[i];
1624 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001625
1626 free(formats_reply);
1627}
1628
1629static void
1630weston_wm_create_wm_window(struct weston_wm *wm)
1631{
1632 static const char name[] = "Weston WM";
1633
1634 wm->wm_window = xcb_generate_id(wm->conn);
1635 xcb_create_window(wm->conn,
1636 XCB_COPY_FROM_PARENT,
1637 wm->wm_window,
1638 wm->screen->root,
1639 0, 0,
1640 10, 10,
1641 0,
1642 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1643 wm->screen->root_visual,
1644 0, NULL);
1645
1646 xcb_change_property(wm->conn,
1647 XCB_PROP_MODE_REPLACE,
1648 wm->wm_window,
1649 wm->atom.net_supporting_wm_check,
1650 XCB_ATOM_WINDOW,
1651 32, /* format */
1652 1, &wm->wm_window);
1653
1654 xcb_change_property(wm->conn,
1655 XCB_PROP_MODE_REPLACE,
1656 wm->wm_window,
1657 wm->atom.net_wm_name,
1658 wm->atom.utf8_string,
1659 8, /* format */
1660 strlen(name), name);
1661
1662 xcb_change_property(wm->conn,
1663 XCB_PROP_MODE_REPLACE,
1664 wm->screen->root,
1665 wm->atom.net_supporting_wm_check,
1666 XCB_ATOM_WINDOW,
1667 32, /* format */
1668 1, &wm->wm_window);
1669
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001670 /* Claim the WM_S0 selection even though we don't suport
1671 * the --replace functionality. */
1672 xcb_set_selection_owner(wm->conn,
1673 wm->wm_window,
1674 wm->atom.wm_s0,
1675 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001676
1677 xcb_set_selection_owner(wm->conn,
1678 wm->wm_window,
1679 wm->atom.net_wm_cm_s0,
1680 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001681}
1682
1683struct weston_wm *
1684weston_wm_create(struct weston_xserver *wxs)
1685{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001686 struct weston_wm *wm;
1687 struct wl_event_loop *loop;
1688 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001689 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001690 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001691 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001692
Peter Huttererf3d62272013-08-08 11:57:05 +10001693 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001694 if (wm == NULL)
1695 return NULL;
1696
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001697 wm->server = wxs;
1698 wm->window_hash = hash_table_create();
1699 if (wm->window_hash == NULL) {
1700 free(wm);
1701 return NULL;
1702 }
1703
1704 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001705 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001706 hash_table_destroy(wm->window_hash);
1707 free(wm);
1708 return NULL;
1709 }
1710
1711 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001712 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001713 close(sv[1]);
1714
1715 /* xcb_connect_to_fd takes ownership of the fd. */
1716 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1717 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001718 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001719 close(sv[0]);
1720 hash_table_destroy(wm->window_hash);
1721 free(wm);
1722 return NULL;
1723 }
1724
1725 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1726 wm->screen = s.data;
1727
1728 loop = wl_display_get_event_loop(wxs->wl_display);
1729 wm->source =
1730 wl_event_loop_add_fd(loop, sv[0],
1731 WL_EVENT_READABLE,
1732 weston_wm_handle_event, wm);
1733 wl_event_source_check(wm->source);
1734
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001735 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001736 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001737
1738 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001739 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1740 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1741 XCB_EVENT_MASK_PROPERTY_CHANGE;
1742 xcb_change_window_attributes(wm->conn, wm->screen->root,
1743 XCB_CW_EVENT_MASK, values);
1744 wm->theme = theme_create();
1745
1746 weston_wm_create_wm_window(wm);
1747
1748 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001749 supported[1] = wm->atom.net_wm_state;
1750 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751 xcb_change_property(wm->conn,
1752 XCB_PROP_MODE_REPLACE,
1753 wm->screen->root,
1754 wm->atom.net_supported,
1755 XCB_ATOM_ATOM,
1756 32, /* format */
1757 ARRAY_LENGTH(supported), supported);
1758
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001759 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760
1761 xcb_flush(wm->conn);
1762
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001763 wm->activate_listener.notify = weston_wm_window_activate;
1764 wl_signal_add(&wxs->compositor->activate_signal,
1765 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001766 wm->transform_listener.notify = weston_wm_window_transform;
1767 wl_signal_add(&wxs->compositor->transform_signal,
1768 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001769 wm->kill_listener.notify = weston_wm_kill_client;
1770 wl_signal_add(&wxs->compositor->kill_signal,
1771 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001772
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001773 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001774 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001775
Martin Minarik6d118362012-06-07 18:01:59 +02001776 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001777
1778 return wm;
1779}
1780
1781void
1782weston_wm_destroy(struct weston_wm *wm)
1783{
1784 /* FIXME: Free windows in hash. */
1785 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001786 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001787 xcb_disconnect(wm->conn);
1788 wl_event_source_remove(wm->source);
1789 wl_list_remove(&wm->selection_listener.link);
1790 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001791 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001792 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001793
1794 free(wm);
1795}
1796
1797static void
1798surface_destroy(struct wl_listener *listener, void *data)
1799{
1800 struct weston_wm_window *window =
1801 container_of(listener,
1802 struct weston_wm_window, surface_destroy_listener);
1803
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001804 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805}
1806
1807static struct weston_wm_window *
1808get_wm_window(struct weston_surface *surface)
1809{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001810 struct wl_listener *listener;
1811
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001812 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001813 if (listener)
1814 return container_of(listener, struct weston_wm_window,
1815 surface_destroy_listener);
1816
1817 return NULL;
1818}
1819
1820static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001821weston_wm_window_configure(void *data)
1822{
1823 struct weston_wm_window *window = data;
1824 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001825 uint32_t values[4];
1826 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001827
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001828 weston_wm_window_get_child_position(window, &x, &y);
1829 values[0] = x;
1830 values[1] = y;
1831 values[2] = window->width;
1832 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001833 xcb_configure_window(wm->conn,
1834 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001835 XCB_CONFIG_WINDOW_X |
1836 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001837 XCB_CONFIG_WINDOW_WIDTH |
1838 XCB_CONFIG_WINDOW_HEIGHT,
1839 values);
1840
1841 weston_wm_window_get_frame_size(window, &width, &height);
1842 values[0] = width;
1843 values[1] = height;
1844 xcb_configure_window(wm->conn,
1845 window->frame_id,
1846 XCB_CONFIG_WINDOW_WIDTH |
1847 XCB_CONFIG_WINDOW_HEIGHT,
1848 values);
1849
1850 window->configure_source = NULL;
1851
1852 weston_wm_window_schedule_repaint(window);
1853}
1854
1855static void
1856send_configure(struct weston_surface *surface,
1857 uint32_t edges, int32_t width, int32_t height)
1858{
1859 struct weston_wm_window *window = get_wm_window(surface);
1860 struct weston_wm *wm = window->wm;
1861 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001862 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001863
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001864 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001865 hborder = 0;
1866 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001867 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001868 hborder = 2 * (t->margin + t->width);
1869 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001870 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001871 hborder = 2 * t->margin;
1872 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001873 }
1874
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001875 if (width > hborder)
1876 window->width = width - hborder;
1877 else
1878 window->width = 1;
1879
1880 if (height > vborder)
1881 window->height = height - vborder;
1882 else
1883 window->height = 1;
1884
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001885 if (window->configure_source)
1886 return;
1887
1888 window->configure_source =
1889 wl_event_loop_add_idle(wm->server->loop,
1890 weston_wm_window_configure, window);
1891}
1892
1893static const struct weston_shell_client shell_client = {
1894 send_configure
1895};
1896
1897static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001898xserver_map_shell_surface(struct weston_wm *wm,
1899 struct weston_wm_window *window)
1900{
1901 struct weston_shell_interface *shell_interface =
1902 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001903
1904 if (!shell_interface->create_shell_surface)
1905 return;
1906
1907 window->shsurf =
1908 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001909 window->surface,
1910 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001911
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001912 if (window->fullscreen) {
1913 window->saved_width = window->width;
1914 window->saved_height = window->height;
1915 shell_interface->set_fullscreen(window->shsurf,
1916 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1917 0, NULL);
1918 return;
1919 } else if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001920 shell_interface->set_toplevel(window->shsurf);
1921 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001922 } else {
1923 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07001924 window->x,
1925 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001926 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001928}
1929
1930static void
1931xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1932 struct wl_resource *surface_resource, uint32_t id)
1933{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001934 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001935 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001936 struct weston_surface *surface =
1937 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001938 struct weston_wm_window *window;
1939
1940 if (client != wxs->client)
1941 return;
1942
1943 window = hash_table_lookup(wm->window_hash, id);
1944 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001945 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001946 return;
1947 }
1948
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001949 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001950
1951 weston_wm_window_read_properties(window);
1952
1953 window->surface = (struct weston_surface *) surface;
1954 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001955 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001956 &window->surface_destroy_listener);
1957
1958 weston_wm_window_schedule_repaint(window);
1959 xserver_map_shell_surface(wm, window);
1960}
1961
1962const struct xserver_interface xserver_implementation = {
1963 xserver_set_window_id
1964};