blob: 257c1088e81ed1a9efe1a0fe48aa1fd66e21992d [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 if (our_resource(wm, configure_notify->window))
578 return;
579
580 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300581 /* resize falls here */
582 if (configure_notify->window != window->id)
583 return;
584
585 weston_wm_window_get_child_position(window, &x, &y);
586 window->x = configure_notify->x - x;
587 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400588}
589
590static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300591weston_wm_kill_client(struct wl_listener *listener, void *data)
592{
593 struct weston_surface *surface = data;
594 struct weston_wm_window *window = get_wm_window(surface);
595 char name[1024];
596
597 if (!window)
598 return;
599
600 gethostname(name, 1024);
601
602 /* this is only one heuristic to guess the PID of a client is valid,
603 * assuming it's compliant with icccm and ewmh. Non-compliants and
604 * remote applications of course fail. */
605 if (!strcmp(window->machine, name) && window->pid != 0)
606 kill(window->pid, SIGKILL);
607}
608
609static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400610weston_wm_window_activate(struct wl_listener *listener, void *data)
611{
612 struct weston_surface *surface = data;
613 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600614 struct weston_wm *wm =
615 container_of(listener, struct weston_wm, activate_listener);
616 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400617
Scott Moreau85ecac02012-05-21 15:49:14 -0600618 if (window) {
619 client_message.response_type = XCB_CLIENT_MESSAGE;
620 client_message.format = 32;
621 client_message.window = window->id;
622 client_message.type = wm->atom.wm_protocols;
623 client_message.data.data32[0] = wm->atom.wm_take_focus;
624 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
625
626 xcb_send_event(wm->conn, 0, window->id,
627 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
628 (char *) &client_message);
629
630 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
631 window->id, XCB_TIME_CURRENT_TIME);
632 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400633 xcb_set_input_focus (wm->conn,
634 XCB_INPUT_FOCUS_POINTER_ROOT,
635 XCB_NONE,
636 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600637 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400638
639 if (wm->focus_window)
640 weston_wm_window_schedule_repaint(wm->focus_window);
641 wm->focus_window = window;
642 if (wm->focus_window)
643 weston_wm_window_schedule_repaint(wm->focus_window);
644}
645
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300646static void
647weston_wm_window_transform(struct wl_listener *listener, void *data)
648{
649 struct weston_surface *surface = data;
650 struct weston_wm_window *window = get_wm_window(surface);
651 struct weston_wm *wm =
652 container_of(listener, struct weston_wm, transform_listener);
653 struct weston_output *output = surface->output;
654 uint32_t mask, values[2];
655 float sxf, syf;
656 int sx, sy;
657 static int old_sx = -1, old_sy = -1;
658
659 if (!window || !wm)
660 return;
661
662 if (!weston_surface_is_mapped(surface))
663 return;
664
665 weston_surface_to_global_float(surface, output->x, output->y,
666 &sxf, &syf);
667
668 sx = (int) sxf;
669 sy = (int) syf;
670
671 if (old_sx == sx && old_sy == sy)
672 return;
673
674 values[0] = sx;
675 values[1] = sy;
676 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
677
678 xcb_configure_window(wm->conn, window->frame_id, mask, values);
679 xcb_flush(wm->conn);
680
681 old_sx = sx;
682 old_sy = sy;
683}
684
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400685#define ICCCM_WITHDRAWN_STATE 0
686#define ICCCM_NORMAL_STATE 1
687#define ICCCM_ICONIC_STATE 3
688
689static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500690weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400691{
692 struct weston_wm *wm = window->wm;
693 uint32_t property[2];
694
695 property[0] = state;
696 property[1] = XCB_WINDOW_NONE;
697
698 xcb_change_property(wm->conn,
699 XCB_PROP_MODE_REPLACE,
700 window->id,
701 wm->atom.wm_state,
702 wm->atom.wm_state,
703 32, /* format */
704 2, property);
705}
706
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400707static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500708weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
709{
710 struct weston_wm *wm = window->wm;
711 uint32_t property[1];
712 int i;
713
714 i = 0;
715 if (window->fullscreen)
716 property[i++] = wm->atom.net_wm_state_fullscreen;
717
718 xcb_change_property(wm->conn,
719 XCB_PROP_MODE_REPLACE,
720 window->id,
721 wm->atom.net_wm_state,
722 XCB_ATOM_ATOM,
723 32, /* format */
724 i, property);
725}
726
727static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400728weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
729{
730 xcb_map_request_event_t *map_request =
731 (xcb_map_request_event_t *) event;
732 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400733 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400734 int x, y, width, height;
735
736 if (our_resource(wm, map_request->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400737 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
738 map_request->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400739 return;
740 }
741
742 window = hash_table_lookup(wm->window_hash, map_request->window);
743
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400744 if (window->frame_id)
745 return;
746
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400747 weston_wm_window_read_properties(window);
748
749 weston_wm_window_get_frame_size(window, &width, &height);
750 weston_wm_window_get_child_position(window, &x, &y);
751
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400752 values[0] = wm->screen->black_pixel;
753 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400754 XCB_EVENT_MASK_KEY_PRESS |
755 XCB_EVENT_MASK_KEY_RELEASE |
756 XCB_EVENT_MASK_BUTTON_PRESS |
757 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400758 XCB_EVENT_MASK_POINTER_MOTION |
759 XCB_EVENT_MASK_ENTER_WINDOW |
760 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400761 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400762 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400763 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764
765 window->frame_id = xcb_generate_id(wm->conn);
766 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400767 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400768 window->frame_id,
769 wm->screen->root,
770 0, 0,
771 width, height,
772 0,
773 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400774 wm->visual_id,
775 XCB_CW_BORDER_PIXEL |
776 XCB_CW_EVENT_MASK |
777 XCB_CW_COLORMAP, values);
778
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400779 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
780
781 values[0] = 0;
782 xcb_configure_window(wm->conn, window->id,
783 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
784
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400785 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
786 window->id, window, window->frame_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400787
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500788 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
789 weston_wm_window_set_net_wm_state(window);
790
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400791 xcb_map_window(wm->conn, map_request->window);
792 xcb_map_window(wm->conn, window->frame_id);
793
794 window->cairo_surface =
795 cairo_xcb_surface_create_with_xrender_format(wm->conn,
796 wm->screen,
797 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400798 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400799 width, height);
800
801 hash_table_insert(wm->window_hash, window->frame_id, window);
802}
803
804static void
805weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
806{
807 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
808
809 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400810 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
811 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400812 return;
813 }
814
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400815 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400816}
817
818static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400819weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
820{
821 xcb_unmap_notify_event_t *unmap_notify =
822 (xcb_unmap_notify_event_t *) event;
823 struct weston_wm_window *window;
824
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400825 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
826 unmap_notify->window,
827 unmap_notify->event,
828 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400829
830 if (our_resource(wm, unmap_notify->window))
831 return;
832
MoD31700122013-06-11 19:58:55 -0500833 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400834 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
835 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400836 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400837
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400838 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400839 if (window->repaint_source)
840 wl_event_source_remove(window->repaint_source);
841 if (window->cairo_surface)
842 cairo_surface_destroy(window->cairo_surface);
843
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400844 if (window->frame_id) {
845 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
846 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500847 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400848 hash_table_remove(wm->window_hash, window->frame_id);
849 window->frame_id = XCB_WINDOW_NONE;
850 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400851
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400852 if (wm->focus_window == window)
853 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400854 if (window->surface)
855 wl_list_remove(&window->surface_destroy_listener.link);
856 window->surface = NULL;
857}
858
859static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400860weston_wm_window_draw_decoration(void *data)
861{
862 struct weston_wm_window *window = data;
863 struct weston_wm *wm = window->wm;
864 struct theme *t = wm->theme;
865 cairo_t *cr;
866 int x, y, width, height;
867 const char *title;
868 uint32_t flags = 0;
869
870 weston_wm_window_read_properties(window);
871
872 window->repaint_source = NULL;
873
874 weston_wm_window_get_frame_size(window, &width, &height);
875 weston_wm_window_get_child_position(window, &x, &y);
876
877 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
878 cr = cairo_create(window->cairo_surface);
879
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500880 if (window->fullscreen) {
881 /* nothing */
882 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883 if (wm->focus_window == window)
884 flags |= THEME_FRAME_ACTIVE;
885
886 if (window->name)
887 title = window->name;
888 else
889 title = "untitled";
890
891 theme_render_frame(t, cr, width, height, title, flags);
892 } else {
893 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
894 cairo_set_source_rgba(cr, 0, 0, 0, 0);
895 cairo_paint(cr);
896
897 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
898 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
899 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
900 }
901
902 cairo_destroy(cr);
903
904 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700905 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500906 if(window->has_alpha) {
907 pixman_region32_init(&window->surface->pending.opaque);
908 } else {
909 /* We leave an extra pixel around the X window area to
910 * make sure we don't sample from the undefined alpha
911 * channel when filtering. */
912 pixman_region32_init_rect(&window->surface->pending.opaque,
913 x - 1, y - 1,
914 window->width + 2,
915 window->height + 2);
916 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200917 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500918 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400919
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500920 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500921 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500922 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400923 t->margin, t->margin,
924 width - 2 * t->margin,
925 height - 2 * t->margin);
926 }
927}
928
929static void
930weston_wm_window_schedule_repaint(struct weston_wm_window *window)
931{
932 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300933 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400934
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400935 if (window->frame_id == XCB_WINDOW_NONE) {
936 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300937 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700938 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500939 if(window->has_alpha) {
940 pixman_region32_init(&window->surface->pending.opaque);
941 } else {
942 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
943 width, height);
944 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200945 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400946 }
947 return;
948 }
949
950 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400951 return;
952
953 window->repaint_source =
954 wl_event_loop_add_idle(wm->server->loop,
955 weston_wm_window_draw_decoration,
956 window);
957}
958
959static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400960weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
961{
962 xcb_property_notify_event_t *property_notify =
963 (xcb_property_notify_event_t *) event;
964 struct weston_wm_window *window;
965
966 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000967 if (!window)
968 return;
969
970 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400971
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400972 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400973 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400974 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400975 else
976 read_and_dump_property(wm, property_notify->window,
977 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400978
979 if (property_notify->atom == wm->atom.net_wm_name ||
980 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400981 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400982}
983
984static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400985weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300986 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400987{
988 struct weston_wm_window *window;
989 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -0500990 xcb_get_geometry_cookie_t geometry_cookie;
991 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400992
993 window = malloc(sizeof *window);
994 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400995 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400996 return;
997 }
998
MoD384a11a2013-06-22 11:04:21 -0500999 geometry_cookie = xcb_get_geometry(wm->conn, id);
1000
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001001 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1002 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1003
1004 memset(window, 0, sizeof *window);
1005 window->wm = wm;
1006 window->id = id;
1007 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001008 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001009 window->width = width;
1010 window->height = height;
1011
MoD384a11a2013-06-22 11:04:21 -05001012 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1013 /* technically we should use XRender and check the visual format's
1014 alpha_mask, but checking depth is simpler and works in all known cases */
1015 if(geometry_reply != NULL)
1016 window->has_alpha = geometry_reply->depth == 32;
1017 free(geometry_reply);
1018
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001019 hash_table_insert(wm->window_hash, id, window);
1020}
1021
1022static void
1023weston_wm_window_destroy(struct weston_wm_window *window)
1024{
1025 hash_table_remove(window->wm->window_hash, window->id);
1026 free(window);
1027}
1028
1029static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001030weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1031{
1032 xcb_create_notify_event_t *create_notify =
1033 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001034
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001035 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1036 create_notify->window,
1037 create_notify->width, create_notify->height,
1038 create_notify->override_redirect ? ", override" : "",
1039 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001040
1041 if (our_resource(wm, create_notify->window))
1042 return;
1043
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001044 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001045 create_notify->width, create_notify->height,
1046 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001047}
1048
1049static void
1050weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1051{
1052 xcb_destroy_notify_event_t *destroy_notify =
1053 (xcb_destroy_notify_event_t *) event;
1054 struct weston_wm_window *window;
1055
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001056 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1057 destroy_notify->window,
1058 destroy_notify->event,
1059 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001060
1061 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063
1064 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001065 weston_wm_window_destroy(window);
1066}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001067
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001068static void
1069weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1070{
1071 xcb_reparent_notify_event_t *reparent_notify =
1072 (xcb_reparent_notify_event_t *) event;
1073 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001074
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001075 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1076 reparent_notify->window,
1077 reparent_notify->parent,
1078 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001079
1080 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001081 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1082 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001083 } else if (!our_resource(wm, reparent_notify->parent)) {
1084 window = hash_table_lookup(wm->window_hash,
1085 reparent_notify->window);
1086 weston_wm_window_destroy(window);
1087 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001088}
1089
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001090struct weston_seat *
1091weston_wm_pick_seat(struct weston_wm *wm)
1092{
1093 return container_of(wm->server->compositor->seat_list.next,
1094 struct weston_seat, link);
1095}
1096
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001097static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001098weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1099 xcb_client_message_event_t *client_message)
1100{
1101 static const int map[] = {
1102 THEME_LOCATION_RESIZING_TOP_LEFT,
1103 THEME_LOCATION_RESIZING_TOP,
1104 THEME_LOCATION_RESIZING_TOP_RIGHT,
1105 THEME_LOCATION_RESIZING_RIGHT,
1106 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1107 THEME_LOCATION_RESIZING_BOTTOM,
1108 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1109 THEME_LOCATION_RESIZING_LEFT
1110 };
1111
1112 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001113 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001114 int detail;
1115 struct weston_shell_interface *shell_interface =
1116 &wm->server->compositor->shell_interface;
1117
Kristian Høgsberge3148752013-05-06 23:19:49 -04001118 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001119 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001120 return;
1121
1122 detail = client_message->data.data32[2];
1123 switch (detail) {
1124 case _NET_WM_MOVERESIZE_MOVE:
1125 shell_interface->move(window->shsurf, seat);
1126 break;
1127 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1128 case _NET_WM_MOVERESIZE_SIZE_TOP:
1129 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1130 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1131 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1132 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1133 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1134 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1135 shell_interface->resize(window->shsurf, seat, map[detail]);
1136 break;
1137 case _NET_WM_MOVERESIZE_CANCEL:
1138 break;
1139 }
1140}
1141
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001142#define _NET_WM_STATE_REMOVE 0
1143#define _NET_WM_STATE_ADD 1
1144#define _NET_WM_STATE_TOGGLE 2
1145
1146static int
1147update_state(int action, int *state)
1148{
1149 int new_state, changed;
1150
1151 switch (action) {
1152 case _NET_WM_STATE_REMOVE:
1153 new_state = 0;
1154 break;
1155 case _NET_WM_STATE_ADD:
1156 new_state = 1;
1157 break;
1158 case _NET_WM_STATE_TOGGLE:
1159 new_state = !*state;
1160 break;
1161 default:
1162 return 0;
1163 }
1164
1165 changed = (*state != new_state);
1166 *state = new_state;
1167
1168 return changed;
1169}
1170
1171static void
1172weston_wm_window_configure(void *data);
1173
1174static void
1175weston_wm_window_handle_state(struct weston_wm_window *window,
1176 xcb_client_message_event_t *client_message)
1177{
1178 struct weston_wm *wm = window->wm;
1179 struct weston_shell_interface *shell_interface =
1180 &wm->server->compositor->shell_interface;
1181 uint32_t action, property;
1182
1183 action = client_message->data.data32[0];
1184 property = client_message->data.data32[1];
1185
1186 if (property == wm->atom.net_wm_state_fullscreen &&
1187 update_state(action, &window->fullscreen)) {
1188 weston_wm_window_set_net_wm_state(window);
1189 if (window->fullscreen) {
1190 window->saved_width = window->width;
1191 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001192
1193 if (window->shsurf)
1194 shell_interface->set_fullscreen(window->shsurf,
1195 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1196 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001197 } else {
1198 shell_interface->set_toplevel(window->shsurf);
1199 window->width = window->saved_width;
1200 window->height = window->saved_height;
1201 weston_wm_window_configure(window);
1202 }
1203 }
1204}
1205
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001206static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001207weston_wm_handle_client_message(struct weston_wm *wm,
1208 xcb_generic_event_t *event)
1209{
1210 xcb_client_message_event_t *client_message =
1211 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001212 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001213
1214 window = hash_table_lookup(wm->window_hash, client_message->window);
1215
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001216 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1217 get_atom_name(wm->conn, client_message->type),
1218 client_message->data.data32[0],
1219 client_message->data.data32[1],
1220 client_message->data.data32[2],
1221 client_message->data.data32[3],
1222 client_message->data.data32[4],
1223 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001224
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001225 if (client_message->type == wm->atom.net_wm_moveresize)
1226 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001227 else if (client_message->type == wm->atom.net_wm_state)
1228 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001229}
1230
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001231enum cursor_type {
1232 XWM_CURSOR_TOP,
1233 XWM_CURSOR_BOTTOM,
1234 XWM_CURSOR_LEFT,
1235 XWM_CURSOR_RIGHT,
1236 XWM_CURSOR_TOP_LEFT,
1237 XWM_CURSOR_TOP_RIGHT,
1238 XWM_CURSOR_BOTTOM_LEFT,
1239 XWM_CURSOR_BOTTOM_RIGHT,
1240 XWM_CURSOR_LEFT_PTR,
1241};
1242
1243static const char *cursors[] = {
1244 "top_side",
1245 "bottom_side",
1246 "left_side",
1247 "right_side",
1248 "top_left_corner",
1249 "top_right_corner",
1250 "bottom_left_corner",
1251 "bottom_right_corner",
1252 "left_ptr"
1253};
1254
1255static void
1256weston_wm_create_cursors(struct weston_wm *wm)
1257{
1258 int i, count = ARRAY_LENGTH(cursors);
1259
1260 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1261 for (i = 0; i < count; i++) {
1262 wm->cursors[i] =
1263 xcb_cursor_library_load_cursor(wm, cursors[i]);
1264 }
1265
1266 wm->last_cursor = -1;
1267}
1268
1269static void
1270weston_wm_destroy_cursors(struct weston_wm *wm)
1271{
1272 uint8_t i;
1273
1274 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1275 xcb_free_cursor(wm->conn, wm->cursors[i]);
1276
1277 free(wm->cursors);
1278}
1279
1280static int
1281get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1282{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001283 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001284
1285 switch (location) {
1286 case THEME_LOCATION_RESIZING_TOP:
1287 return XWM_CURSOR_TOP;
1288 case THEME_LOCATION_RESIZING_BOTTOM:
1289 return XWM_CURSOR_BOTTOM;
1290 case THEME_LOCATION_RESIZING_LEFT:
1291 return XWM_CURSOR_LEFT;
1292 case THEME_LOCATION_RESIZING_RIGHT:
1293 return XWM_CURSOR_RIGHT;
1294 case THEME_LOCATION_RESIZING_TOP_LEFT:
1295 return XWM_CURSOR_TOP_LEFT;
1296 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1297 return XWM_CURSOR_TOP_RIGHT;
1298 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1299 return XWM_CURSOR_BOTTOM_LEFT;
1300 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1301 return XWM_CURSOR_BOTTOM_RIGHT;
1302 case THEME_LOCATION_EXTERIOR:
1303 case THEME_LOCATION_TITLEBAR:
1304 default:
1305 return XWM_CURSOR_LEFT_PTR;
1306 }
1307}
1308
1309static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001310weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1311 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001312{
1313 uint32_t cursor_value_list;
1314
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001315 if (wm->last_cursor == cursor)
1316 return;
1317
1318 wm->last_cursor = cursor;
1319
1320 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001321 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001322 XCB_CW_CURSOR, &cursor_value_list);
1323 xcb_flush(wm->conn);
1324}
1325
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001326static void
1327weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1328{
1329 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1330 struct weston_shell_interface *shell_interface =
1331 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001332 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001333 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001334 enum theme_location location;
1335 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001336 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001337
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001338 wm_log("XCB_BUTTON_%s (detail %d)\n",
1339 button->response_type == XCB_BUTTON_PRESS ?
1340 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001341
1342 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001343 weston_wm_window_get_frame_size(window, &width, &height);
1344
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001345 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001346 button->detail == 1) {
1347 location = theme_get_location(t,
1348 button->event_x,
1349 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001350 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001351
1352 switch (location) {
1353 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001354 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001355 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001356 case THEME_LOCATION_RESIZING_TOP:
1357 case THEME_LOCATION_RESIZING_BOTTOM:
1358 case THEME_LOCATION_RESIZING_LEFT:
1359 case THEME_LOCATION_RESIZING_RIGHT:
1360 case THEME_LOCATION_RESIZING_TOP_LEFT:
1361 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1362 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1363 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1364 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001365 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001366 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001367 default:
1368 break;
1369 }
1370 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001371}
1372
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001373static void
1374weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1375{
1376 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1377 struct weston_wm_window *window;
1378 int cursor, width, height;
1379
1380 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001381 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001382 return;
1383
1384 weston_wm_window_get_frame_size(window, &width, &height);
1385 cursor = get_cursor_for_location(wm->theme, width, height,
1386 motion->event_x, motion->event_y);
1387
Tiago Vignattic1903232012-07-16 12:15:37 -04001388 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001389}
1390
1391static void
1392weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1393{
1394 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1395 struct weston_wm_window *window;
1396 int cursor, width, height;
1397
1398 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001399 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001400 return;
1401
1402 weston_wm_window_get_frame_size(window, &width, &height);
1403 cursor = get_cursor_for_location(wm->theme, width, height,
1404 enter->event_x, enter->event_y);
1405
Tiago Vignattic1903232012-07-16 12:15:37 -04001406 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001407}
1408
1409static void
1410weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1411{
1412 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1413 struct weston_wm_window *window;
1414
1415 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001416 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001417 return;
1418
Tiago Vignattic1903232012-07-16 12:15:37 -04001419 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001420}
1421
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001422static int
1423weston_wm_handle_event(int fd, uint32_t mask, void *data)
1424{
1425 struct weston_wm *wm = data;
1426 xcb_generic_event_t *event;
1427 int count = 0;
1428
1429 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1430 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001431 free(event);
1432 count++;
1433 continue;
1434 }
1435
MoD31700122013-06-11 19:58:55 -05001436 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001437 case XCB_BUTTON_PRESS:
1438 case XCB_BUTTON_RELEASE:
1439 weston_wm_handle_button(wm, event);
1440 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001441 case XCB_ENTER_NOTIFY:
1442 weston_wm_handle_enter(wm, event);
1443 break;
1444 case XCB_LEAVE_NOTIFY:
1445 weston_wm_handle_leave(wm, event);
1446 break;
1447 case XCB_MOTION_NOTIFY:
1448 weston_wm_handle_motion(wm, event);
1449 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001450 case XCB_CREATE_NOTIFY:
1451 weston_wm_handle_create_notify(wm, event);
1452 break;
1453 case XCB_MAP_REQUEST:
1454 weston_wm_handle_map_request(wm, event);
1455 break;
1456 case XCB_MAP_NOTIFY:
1457 weston_wm_handle_map_notify(wm, event);
1458 break;
1459 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001460 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001461 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001462 case XCB_REPARENT_NOTIFY:
1463 weston_wm_handle_reparent_notify(wm, event);
1464 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001465 case XCB_CONFIGURE_REQUEST:
1466 weston_wm_handle_configure_request(wm, event);
1467 break;
1468 case XCB_CONFIGURE_NOTIFY:
1469 weston_wm_handle_configure_notify(wm, event);
1470 break;
1471 case XCB_DESTROY_NOTIFY:
1472 weston_wm_handle_destroy_notify(wm, event);
1473 break;
1474 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001475 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001476 break;
1477 case XCB_PROPERTY_NOTIFY:
1478 weston_wm_handle_property_notify(wm, event);
1479 break;
1480 case XCB_CLIENT_MESSAGE:
1481 weston_wm_handle_client_message(wm, event);
1482 break;
1483 }
1484
1485 free(event);
1486 count++;
1487 }
1488
1489 xcb_flush(wm->conn);
1490
1491 return count;
1492}
1493
1494static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001495weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1496{
1497 xcb_depth_iterator_t d_iter;
1498 xcb_visualtype_iterator_t vt_iter;
1499 xcb_visualtype_t *visualtype;
1500
1501 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1502 visualtype = NULL;
1503 while (d_iter.rem > 0) {
1504 if (d_iter.data->depth == 32) {
1505 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1506 visualtype = vt_iter.data;
1507 break;
1508 }
1509
1510 xcb_depth_next(&d_iter);
1511 }
1512
1513 if (visualtype == NULL) {
1514 weston_log("no 32 bit visualtype\n");
1515 return;
1516 }
1517
1518 wm->visual_id = visualtype->visual_id;
1519 wm->colormap = xcb_generate_id(wm->conn);
1520 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1521 wm->colormap, wm->screen->root, wm->visual_id);
1522}
1523
1524static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001525weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001526{
1527
1528#define F(field) offsetof(struct weston_wm, field)
1529
1530 static const struct { const char *name; int offset; } atoms[] = {
1531 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1532 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1533 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001534 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001535 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001536 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001537 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001538 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001539 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1540 { "_NET_WM_STATE", F(atom.net_wm_state) },
1541 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1542 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1543 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1544 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1545
1546 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1547 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1548 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1549 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1550 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1551 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1552 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001553 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1554 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001555 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1556 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1557 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1558 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1559 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1560
1561 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1562 { "_NET_SUPPORTING_WM_CHECK",
1563 F(atom.net_supporting_wm_check) },
1564 { "_NET_SUPPORTED", F(atom.net_supported) },
1565 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1566 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001567 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001568 { "TARGETS", F(atom.targets) },
1569 { "UTF8_STRING", F(atom.utf8_string) },
1570 { "_WL_SELECTION", F(atom.wl_selection) },
1571 { "INCR", F(atom.incr) },
1572 { "TIMESTAMP", F(atom.timestamp) },
1573 { "MULTIPLE", F(atom.multiple) },
1574 { "UTF8_STRING" , F(atom.utf8_string) },
1575 { "COMPOUND_TEXT", F(atom.compound_text) },
1576 { "TEXT", F(atom.text) },
1577 { "STRING", F(atom.string) },
1578 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1579 { "text/plain", F(atom.text_plain) },
1580 };
1581#undef F
1582
1583 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1584 xcb_xfixes_query_version_reply_t *xfixes_reply;
1585 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1586 xcb_intern_atom_reply_t *reply;
1587 xcb_render_query_pict_formats_reply_t *formats_reply;
1588 xcb_render_query_pict_formats_cookie_t formats_cookie;
1589 xcb_render_pictforminfo_t *formats;
1590 uint32_t i;
1591
1592 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1593
1594 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1595
1596 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1597 cookies[i] = xcb_intern_atom (wm->conn, 0,
1598 strlen(atoms[i].name),
1599 atoms[i].name);
1600
1601 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1602 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1603 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1604 free(reply);
1605 }
1606
1607 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1608 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001609 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001610
1611 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1612 XCB_XFIXES_MAJOR_VERSION,
1613 XCB_XFIXES_MINOR_VERSION);
1614 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1615 xfixes_cookie, NULL);
1616
Martin Minarik6d118362012-06-07 18:01:59 +02001617 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001618 xfixes_reply->major_version, xfixes_reply->minor_version);
1619
1620 free(xfixes_reply);
1621
1622 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1623 formats_cookie, 0);
1624 if (formats_reply == NULL)
1625 return;
1626
1627 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001628 for (i = 0; i < formats_reply->num_formats; i++) {
1629 if (formats[i].direct.red_mask != 0xff &&
1630 formats[i].direct.red_shift != 16)
1631 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001632 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1633 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001634 wm->format_rgb = formats[i];
1635 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1636 formats[i].depth == 32 &&
1637 formats[i].direct.alpha_mask == 0xff &&
1638 formats[i].direct.alpha_shift == 24)
1639 wm->format_rgba = formats[i];
1640 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001641
1642 free(formats_reply);
1643}
1644
1645static void
1646weston_wm_create_wm_window(struct weston_wm *wm)
1647{
1648 static const char name[] = "Weston WM";
1649
1650 wm->wm_window = xcb_generate_id(wm->conn);
1651 xcb_create_window(wm->conn,
1652 XCB_COPY_FROM_PARENT,
1653 wm->wm_window,
1654 wm->screen->root,
1655 0, 0,
1656 10, 10,
1657 0,
1658 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1659 wm->screen->root_visual,
1660 0, NULL);
1661
1662 xcb_change_property(wm->conn,
1663 XCB_PROP_MODE_REPLACE,
1664 wm->wm_window,
1665 wm->atom.net_supporting_wm_check,
1666 XCB_ATOM_WINDOW,
1667 32, /* format */
1668 1, &wm->wm_window);
1669
1670 xcb_change_property(wm->conn,
1671 XCB_PROP_MODE_REPLACE,
1672 wm->wm_window,
1673 wm->atom.net_wm_name,
1674 wm->atom.utf8_string,
1675 8, /* format */
1676 strlen(name), name);
1677
1678 xcb_change_property(wm->conn,
1679 XCB_PROP_MODE_REPLACE,
1680 wm->screen->root,
1681 wm->atom.net_supporting_wm_check,
1682 XCB_ATOM_WINDOW,
1683 32, /* format */
1684 1, &wm->wm_window);
1685
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001686 /* Claim the WM_S0 selection even though we don't suport
1687 * the --replace functionality. */
1688 xcb_set_selection_owner(wm->conn,
1689 wm->wm_window,
1690 wm->atom.wm_s0,
1691 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001692}
1693
1694struct weston_wm *
1695weston_wm_create(struct weston_xserver *wxs)
1696{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001697 struct weston_wm *wm;
1698 struct wl_event_loop *loop;
1699 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001700 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001701 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001702 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001703
1704 wm = malloc(sizeof *wm);
1705 if (wm == NULL)
1706 return NULL;
1707
1708 memset(wm, 0, sizeof *wm);
1709 wm->server = wxs;
1710 wm->window_hash = hash_table_create();
1711 if (wm->window_hash == NULL) {
1712 free(wm);
1713 return NULL;
1714 }
1715
1716 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001717 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718 hash_table_destroy(wm->window_hash);
1719 free(wm);
1720 return NULL;
1721 }
1722
1723 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001724 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001725 close(sv[1]);
1726
1727 /* xcb_connect_to_fd takes ownership of the fd. */
1728 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1729 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001730 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001731 close(sv[0]);
1732 hash_table_destroy(wm->window_hash);
1733 free(wm);
1734 return NULL;
1735 }
1736
1737 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1738 wm->screen = s.data;
1739
1740 loop = wl_display_get_event_loop(wxs->wl_display);
1741 wm->source =
1742 wl_event_loop_add_fd(loop, sv[0],
1743 WL_EVENT_READABLE,
1744 weston_wm_handle_event, wm);
1745 wl_event_source_check(wm->source);
1746
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001747 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001748 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001749
1750 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1752 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1753 XCB_EVENT_MASK_PROPERTY_CHANGE;
1754 xcb_change_window_attributes(wm->conn, wm->screen->root,
1755 XCB_CW_EVENT_MASK, values);
1756 wm->theme = theme_create();
1757
1758 weston_wm_create_wm_window(wm);
1759
1760 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001761 supported[1] = wm->atom.net_wm_state;
1762 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001763 xcb_change_property(wm->conn,
1764 XCB_PROP_MODE_REPLACE,
1765 wm->screen->root,
1766 wm->atom.net_supported,
1767 XCB_ATOM_ATOM,
1768 32, /* format */
1769 ARRAY_LENGTH(supported), supported);
1770
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001771 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001772
1773 xcb_flush(wm->conn);
1774
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001775 wm->activate_listener.notify = weston_wm_window_activate;
1776 wl_signal_add(&wxs->compositor->activate_signal,
1777 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001778 wm->transform_listener.notify = weston_wm_window_transform;
1779 wl_signal_add(&wxs->compositor->transform_signal,
1780 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001781 wm->kill_listener.notify = weston_wm_kill_client;
1782 wl_signal_add(&wxs->compositor->kill_signal,
1783 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001784
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001785 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001786 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001787
Martin Minarik6d118362012-06-07 18:01:59 +02001788 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001789
1790 return wm;
1791}
1792
1793void
1794weston_wm_destroy(struct weston_wm *wm)
1795{
1796 /* FIXME: Free windows in hash. */
1797 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001798 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001799 xcb_disconnect(wm->conn);
1800 wl_event_source_remove(wm->source);
1801 wl_list_remove(&wm->selection_listener.link);
1802 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001803 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001804 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805
1806 free(wm);
1807}
1808
1809static void
1810surface_destroy(struct wl_listener *listener, void *data)
1811{
1812 struct weston_wm_window *window =
1813 container_of(listener,
1814 struct weston_wm_window, surface_destroy_listener);
1815
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001816 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001817}
1818
1819static struct weston_wm_window *
1820get_wm_window(struct weston_surface *surface)
1821{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001822 struct wl_listener *listener;
1823
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001824 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001825 if (listener)
1826 return container_of(listener, struct weston_wm_window,
1827 surface_destroy_listener);
1828
1829 return NULL;
1830}
1831
1832static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001833weston_wm_window_configure(void *data)
1834{
1835 struct weston_wm_window *window = data;
1836 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001837 uint32_t values[4];
1838 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001839
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001840 weston_wm_window_get_child_position(window, &x, &y);
1841 values[0] = x;
1842 values[1] = y;
1843 values[2] = window->width;
1844 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001845 xcb_configure_window(wm->conn,
1846 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001847 XCB_CONFIG_WINDOW_X |
1848 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001849 XCB_CONFIG_WINDOW_WIDTH |
1850 XCB_CONFIG_WINDOW_HEIGHT,
1851 values);
1852
1853 weston_wm_window_get_frame_size(window, &width, &height);
1854 values[0] = width;
1855 values[1] = height;
1856 xcb_configure_window(wm->conn,
1857 window->frame_id,
1858 XCB_CONFIG_WINDOW_WIDTH |
1859 XCB_CONFIG_WINDOW_HEIGHT,
1860 values);
1861
1862 window->configure_source = NULL;
1863
1864 weston_wm_window_schedule_repaint(window);
1865}
1866
1867static void
1868send_configure(struct weston_surface *surface,
1869 uint32_t edges, int32_t width, int32_t height)
1870{
1871 struct weston_wm_window *window = get_wm_window(surface);
1872 struct weston_wm *wm = window->wm;
1873 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001874 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001875
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001876 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001877 hborder = 0;
1878 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001879 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001880 hborder = 2 * (t->margin + t->width);
1881 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001882 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001883 hborder = 2 * t->margin;
1884 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001885 }
1886
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001887 if (width > hborder)
1888 window->width = width - hborder;
1889 else
1890 window->width = 1;
1891
1892 if (height > vborder)
1893 window->height = height - vborder;
1894 else
1895 window->height = 1;
1896
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001897 if (window->configure_source)
1898 return;
1899
1900 window->configure_source =
1901 wl_event_loop_add_idle(wm->server->loop,
1902 weston_wm_window_configure, window);
1903}
1904
1905static const struct weston_shell_client shell_client = {
1906 send_configure
1907};
1908
1909static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001910xserver_map_shell_surface(struct weston_wm *wm,
1911 struct weston_wm_window *window)
1912{
1913 struct weston_shell_interface *shell_interface =
1914 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001915 struct theme *t = window->wm->theme;
1916
1917 if (!shell_interface->create_shell_surface)
1918 return;
1919
1920 window->shsurf =
1921 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001922 window->surface,
1923 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001924
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001925 if (window->fullscreen) {
1926 window->saved_width = window->width;
1927 window->saved_height = window->height;
1928 shell_interface->set_fullscreen(window->shsurf,
1929 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1930 0, NULL);
1931 return;
1932 } else if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001933 shell_interface->set_toplevel(window->shsurf);
1934 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001935 } else {
1936 shell_interface->set_xwayland(window->shsurf,
1937 window->x + t->margin,
1938 window->y + t->margin,
1939 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001940 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001941}
1942
1943static void
1944xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1945 struct wl_resource *surface_resource, uint32_t id)
1946{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001947 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001948 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001949 struct weston_surface *surface =
1950 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001951 struct weston_wm_window *window;
1952
1953 if (client != wxs->client)
1954 return;
1955
1956 window = hash_table_lookup(wm->window_hash, id);
1957 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001958 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001959 return;
1960 }
1961
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001962 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963
1964 weston_wm_window_read_properties(window);
1965
1966 window->surface = (struct weston_surface *) surface;
1967 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001968 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001969 &window->surface_destroy_listener);
1970
1971 weston_wm_window_schedule_repaint(window);
1972 xserver_map_shell_surface(wm, window);
1973}
1974
1975const struct xserver_interface xserver_implementation = {
1976 xserver_set_window_id
1977};