blob: 0619eca8aa65c47fd9916eae3805d83e694c4620 [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;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200613 struct weston_wm_window *window = NULL;
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
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200618 if (surface) {
619 window = get_wm_window(surface);
620 }
621
Scott Moreau85ecac02012-05-21 15:49:14 -0600622 if (window) {
623 client_message.response_type = XCB_CLIENT_MESSAGE;
624 client_message.format = 32;
625 client_message.window = window->id;
626 client_message.type = wm->atom.wm_protocols;
627 client_message.data.data32[0] = wm->atom.wm_take_focus;
628 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
629
630 xcb_send_event(wm->conn, 0, window->id,
631 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
632 (char *) &client_message);
633
634 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
635 window->id, XCB_TIME_CURRENT_TIME);
636 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400637 xcb_set_input_focus (wm->conn,
638 XCB_INPUT_FOCUS_POINTER_ROOT,
639 XCB_NONE,
640 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600641 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400642
643 if (wm->focus_window)
644 weston_wm_window_schedule_repaint(wm->focus_window);
645 wm->focus_window = window;
646 if (wm->focus_window)
647 weston_wm_window_schedule_repaint(wm->focus_window);
648}
649
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300650static void
651weston_wm_window_transform(struct wl_listener *listener, void *data)
652{
653 struct weston_surface *surface = data;
654 struct weston_wm_window *window = get_wm_window(surface);
655 struct weston_wm *wm =
656 container_of(listener, struct weston_wm, transform_listener);
657 struct weston_output *output = surface->output;
658 uint32_t mask, values[2];
659 float sxf, syf;
660 int sx, sy;
661 static int old_sx = -1, old_sy = -1;
662
663 if (!window || !wm)
664 return;
665
666 if (!weston_surface_is_mapped(surface))
667 return;
668
669 weston_surface_to_global_float(surface, output->x, output->y,
670 &sxf, &syf);
671
672 sx = (int) sxf;
673 sy = (int) syf;
674
675 if (old_sx == sx && old_sy == sy)
676 return;
677
678 values[0] = sx;
679 values[1] = sy;
680 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
681
682 xcb_configure_window(wm->conn, window->frame_id, mask, values);
683 xcb_flush(wm->conn);
684
685 old_sx = sx;
686 old_sy = sy;
687}
688
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400689#define ICCCM_WITHDRAWN_STATE 0
690#define ICCCM_NORMAL_STATE 1
691#define ICCCM_ICONIC_STATE 3
692
693static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500694weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400695{
696 struct weston_wm *wm = window->wm;
697 uint32_t property[2];
698
699 property[0] = state;
700 property[1] = XCB_WINDOW_NONE;
701
702 xcb_change_property(wm->conn,
703 XCB_PROP_MODE_REPLACE,
704 window->id,
705 wm->atom.wm_state,
706 wm->atom.wm_state,
707 32, /* format */
708 2, property);
709}
710
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400711static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500712weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
713{
714 struct weston_wm *wm = window->wm;
715 uint32_t property[1];
716 int i;
717
718 i = 0;
719 if (window->fullscreen)
720 property[i++] = wm->atom.net_wm_state_fullscreen;
721
722 xcb_change_property(wm->conn,
723 XCB_PROP_MODE_REPLACE,
724 window->id,
725 wm->atom.net_wm_state,
726 XCB_ATOM_ATOM,
727 32, /* format */
728 i, property);
729}
730
731static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
733{
734 xcb_map_request_event_t *map_request =
735 (xcb_map_request_event_t *) event;
736 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400737 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400738 int x, y, width, height;
739
740 if (our_resource(wm, map_request->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400741 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
742 map_request->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743 return;
744 }
745
746 window = hash_table_lookup(wm->window_hash, map_request->window);
747
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400748 if (window->frame_id)
749 return;
750
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400751 weston_wm_window_read_properties(window);
752
753 weston_wm_window_get_frame_size(window, &width, &height);
754 weston_wm_window_get_child_position(window, &x, &y);
755
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400756 values[0] = wm->screen->black_pixel;
757 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400758 XCB_EVENT_MASK_KEY_PRESS |
759 XCB_EVENT_MASK_KEY_RELEASE |
760 XCB_EVENT_MASK_BUTTON_PRESS |
761 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400762 XCB_EVENT_MASK_POINTER_MOTION |
763 XCB_EVENT_MASK_ENTER_WINDOW |
764 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400765 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400766 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400767 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400768
769 window->frame_id = xcb_generate_id(wm->conn);
770 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400771 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400772 window->frame_id,
773 wm->screen->root,
774 0, 0,
775 width, height,
776 0,
777 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400778 wm->visual_id,
779 XCB_CW_BORDER_PIXEL |
780 XCB_CW_EVENT_MASK |
781 XCB_CW_COLORMAP, values);
782
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400783 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
784
785 values[0] = 0;
786 xcb_configure_window(wm->conn, window->id,
787 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
788
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400789 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
790 window->id, window, window->frame_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400791
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500792 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
793 weston_wm_window_set_net_wm_state(window);
794
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400795 xcb_map_window(wm->conn, map_request->window);
796 xcb_map_window(wm->conn, window->frame_id);
797
798 window->cairo_surface =
799 cairo_xcb_surface_create_with_xrender_format(wm->conn,
800 wm->screen,
801 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400802 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803 width, height);
804
805 hash_table_insert(wm->window_hash, window->frame_id, window);
806}
807
808static void
809weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
810{
811 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
812
813 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400814 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
815 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400816 return;
817 }
818
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400819 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400820}
821
822static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400823weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
824{
825 xcb_unmap_notify_event_t *unmap_notify =
826 (xcb_unmap_notify_event_t *) event;
827 struct weston_wm_window *window;
828
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400829 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
830 unmap_notify->window,
831 unmap_notify->event,
832 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400833
834 if (our_resource(wm, unmap_notify->window))
835 return;
836
MoD31700122013-06-11 19:58:55 -0500837 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400838 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
839 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400840 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400841
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400842 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400843 if (window->repaint_source)
844 wl_event_source_remove(window->repaint_source);
845 if (window->cairo_surface)
846 cairo_surface_destroy(window->cairo_surface);
847
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400848 if (window->frame_id) {
849 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
850 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500851 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400852 hash_table_remove(wm->window_hash, window->frame_id);
853 window->frame_id = XCB_WINDOW_NONE;
854 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400855
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400856 if (wm->focus_window == window)
857 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400858 if (window->surface)
859 wl_list_remove(&window->surface_destroy_listener.link);
860 window->surface = NULL;
861}
862
863static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400864weston_wm_window_draw_decoration(void *data)
865{
866 struct weston_wm_window *window = data;
867 struct weston_wm *wm = window->wm;
868 struct theme *t = wm->theme;
869 cairo_t *cr;
870 int x, y, width, height;
871 const char *title;
872 uint32_t flags = 0;
873
874 weston_wm_window_read_properties(window);
875
876 window->repaint_source = NULL;
877
878 weston_wm_window_get_frame_size(window, &width, &height);
879 weston_wm_window_get_child_position(window, &x, &y);
880
881 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
882 cr = cairo_create(window->cairo_surface);
883
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500884 if (window->fullscreen) {
885 /* nothing */
886 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400887 if (wm->focus_window == window)
888 flags |= THEME_FRAME_ACTIVE;
889
890 if (window->name)
891 title = window->name;
892 else
893 title = "untitled";
894
895 theme_render_frame(t, cr, width, height, title, flags);
896 } else {
897 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
898 cairo_set_source_rgba(cr, 0, 0, 0, 0);
899 cairo_paint(cr);
900
901 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
902 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
903 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
904 }
905
906 cairo_destroy(cr);
907
908 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700909 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500910 if(window->has_alpha) {
911 pixman_region32_init(&window->surface->pending.opaque);
912 } else {
913 /* We leave an extra pixel around the X window area to
914 * make sure we don't sample from the undefined alpha
915 * channel when filtering. */
916 pixman_region32_init_rect(&window->surface->pending.opaque,
917 x - 1, y - 1,
918 window->width + 2,
919 window->height + 2);
920 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200921 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500922 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400923
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500924 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500925 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500926 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400927 t->margin, t->margin,
928 width - 2 * t->margin,
929 height - 2 * t->margin);
930 }
931}
932
933static void
934weston_wm_window_schedule_repaint(struct weston_wm_window *window)
935{
936 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300937 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400938
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400939 if (window->frame_id == XCB_WINDOW_NONE) {
940 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300941 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700942 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500943 if(window->has_alpha) {
944 pixman_region32_init(&window->surface->pending.opaque);
945 } else {
946 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
947 width, height);
948 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200949 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400950 }
951 return;
952 }
953
954 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955 return;
956
957 window->repaint_source =
958 wl_event_loop_add_idle(wm->server->loop,
959 weston_wm_window_draw_decoration,
960 window);
961}
962
963static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400964weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
965{
966 xcb_property_notify_event_t *property_notify =
967 (xcb_property_notify_event_t *) event;
968 struct weston_wm_window *window;
969
970 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000971 if (!window)
972 return;
973
974 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400975
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400976 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400977 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400978 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400979 else
980 read_and_dump_property(wm, property_notify->window,
981 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400982
983 if (property_notify->atom == wm->atom.net_wm_name ||
984 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400986}
987
988static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400989weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300990 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400991{
992 struct weston_wm_window *window;
993 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -0500994 xcb_get_geometry_cookie_t geometry_cookie;
995 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400996
Peter Huttererf3d62272013-08-08 11:57:05 +1000997 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400998 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400999 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001000 return;
1001 }
1002
MoD384a11a2013-06-22 11:04:21 -05001003 geometry_cookie = xcb_get_geometry(wm->conn, id);
1004
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001005 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1006 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1007
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001008 window->wm = wm;
1009 window->id = id;
1010 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001011 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001012 window->width = width;
1013 window->height = height;
1014
MoD384a11a2013-06-22 11:04:21 -05001015 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1016 /* technically we should use XRender and check the visual format's
1017 alpha_mask, but checking depth is simpler and works in all known cases */
1018 if(geometry_reply != NULL)
1019 window->has_alpha = geometry_reply->depth == 32;
1020 free(geometry_reply);
1021
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001022 hash_table_insert(wm->window_hash, id, window);
1023}
1024
1025static void
1026weston_wm_window_destroy(struct weston_wm_window *window)
1027{
1028 hash_table_remove(window->wm->window_hash, window->id);
1029 free(window);
1030}
1031
1032static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001033weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1034{
1035 xcb_create_notify_event_t *create_notify =
1036 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001037
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001038 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1039 create_notify->window,
1040 create_notify->width, create_notify->height,
1041 create_notify->override_redirect ? ", override" : "",
1042 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001043
1044 if (our_resource(wm, create_notify->window))
1045 return;
1046
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001047 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001048 create_notify->width, create_notify->height,
1049 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050}
1051
1052static void
1053weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1054{
1055 xcb_destroy_notify_event_t *destroy_notify =
1056 (xcb_destroy_notify_event_t *) event;
1057 struct weston_wm_window *window;
1058
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001059 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1060 destroy_notify->window,
1061 destroy_notify->event,
1062 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001063
1064 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001065 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001066
1067 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001068 weston_wm_window_destroy(window);
1069}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001071static void
1072weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1073{
1074 xcb_reparent_notify_event_t *reparent_notify =
1075 (xcb_reparent_notify_event_t *) event;
1076 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001077
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001078 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1079 reparent_notify->window,
1080 reparent_notify->parent,
1081 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001082
1083 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001084 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1085 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001086 } else if (!our_resource(wm, reparent_notify->parent)) {
1087 window = hash_table_lookup(wm->window_hash,
1088 reparent_notify->window);
1089 weston_wm_window_destroy(window);
1090 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001091}
1092
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001093struct weston_seat *
1094weston_wm_pick_seat(struct weston_wm *wm)
1095{
1096 return container_of(wm->server->compositor->seat_list.next,
1097 struct weston_seat, link);
1098}
1099
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001101weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1102 xcb_client_message_event_t *client_message)
1103{
1104 static const int map[] = {
1105 THEME_LOCATION_RESIZING_TOP_LEFT,
1106 THEME_LOCATION_RESIZING_TOP,
1107 THEME_LOCATION_RESIZING_TOP_RIGHT,
1108 THEME_LOCATION_RESIZING_RIGHT,
1109 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1110 THEME_LOCATION_RESIZING_BOTTOM,
1111 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1112 THEME_LOCATION_RESIZING_LEFT
1113 };
1114
1115 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001116 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001117 int detail;
1118 struct weston_shell_interface *shell_interface =
1119 &wm->server->compositor->shell_interface;
1120
Kristian Høgsberge3148752013-05-06 23:19:49 -04001121 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001122 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001123 return;
1124
1125 detail = client_message->data.data32[2];
1126 switch (detail) {
1127 case _NET_WM_MOVERESIZE_MOVE:
1128 shell_interface->move(window->shsurf, seat);
1129 break;
1130 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1131 case _NET_WM_MOVERESIZE_SIZE_TOP:
1132 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1133 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1134 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1135 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1136 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1137 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1138 shell_interface->resize(window->shsurf, seat, map[detail]);
1139 break;
1140 case _NET_WM_MOVERESIZE_CANCEL:
1141 break;
1142 }
1143}
1144
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001145#define _NET_WM_STATE_REMOVE 0
1146#define _NET_WM_STATE_ADD 1
1147#define _NET_WM_STATE_TOGGLE 2
1148
1149static int
1150update_state(int action, int *state)
1151{
1152 int new_state, changed;
1153
1154 switch (action) {
1155 case _NET_WM_STATE_REMOVE:
1156 new_state = 0;
1157 break;
1158 case _NET_WM_STATE_ADD:
1159 new_state = 1;
1160 break;
1161 case _NET_WM_STATE_TOGGLE:
1162 new_state = !*state;
1163 break;
1164 default:
1165 return 0;
1166 }
1167
1168 changed = (*state != new_state);
1169 *state = new_state;
1170
1171 return changed;
1172}
1173
1174static void
1175weston_wm_window_configure(void *data);
1176
1177static void
1178weston_wm_window_handle_state(struct weston_wm_window *window,
1179 xcb_client_message_event_t *client_message)
1180{
1181 struct weston_wm *wm = window->wm;
1182 struct weston_shell_interface *shell_interface =
1183 &wm->server->compositor->shell_interface;
1184 uint32_t action, property;
1185
1186 action = client_message->data.data32[0];
1187 property = client_message->data.data32[1];
1188
1189 if (property == wm->atom.net_wm_state_fullscreen &&
1190 update_state(action, &window->fullscreen)) {
1191 weston_wm_window_set_net_wm_state(window);
1192 if (window->fullscreen) {
1193 window->saved_width = window->width;
1194 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001195
1196 if (window->shsurf)
1197 shell_interface->set_fullscreen(window->shsurf,
1198 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1199 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001200 } else {
1201 shell_interface->set_toplevel(window->shsurf);
1202 window->width = window->saved_width;
1203 window->height = window->saved_height;
1204 weston_wm_window_configure(window);
1205 }
1206 }
1207}
1208
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001209static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001210weston_wm_handle_client_message(struct weston_wm *wm,
1211 xcb_generic_event_t *event)
1212{
1213 xcb_client_message_event_t *client_message =
1214 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001215 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001216
1217 window = hash_table_lookup(wm->window_hash, client_message->window);
1218
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001219 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1220 get_atom_name(wm->conn, client_message->type),
1221 client_message->data.data32[0],
1222 client_message->data.data32[1],
1223 client_message->data.data32[2],
1224 client_message->data.data32[3],
1225 client_message->data.data32[4],
1226 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001227
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001228 if (client_message->type == wm->atom.net_wm_moveresize)
1229 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001230 else if (client_message->type == wm->atom.net_wm_state)
1231 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001232}
1233
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001234enum cursor_type {
1235 XWM_CURSOR_TOP,
1236 XWM_CURSOR_BOTTOM,
1237 XWM_CURSOR_LEFT,
1238 XWM_CURSOR_RIGHT,
1239 XWM_CURSOR_TOP_LEFT,
1240 XWM_CURSOR_TOP_RIGHT,
1241 XWM_CURSOR_BOTTOM_LEFT,
1242 XWM_CURSOR_BOTTOM_RIGHT,
1243 XWM_CURSOR_LEFT_PTR,
1244};
1245
1246static const char *cursors[] = {
1247 "top_side",
1248 "bottom_side",
1249 "left_side",
1250 "right_side",
1251 "top_left_corner",
1252 "top_right_corner",
1253 "bottom_left_corner",
1254 "bottom_right_corner",
1255 "left_ptr"
1256};
1257
1258static void
1259weston_wm_create_cursors(struct weston_wm *wm)
1260{
1261 int i, count = ARRAY_LENGTH(cursors);
1262
1263 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1264 for (i = 0; i < count; i++) {
1265 wm->cursors[i] =
1266 xcb_cursor_library_load_cursor(wm, cursors[i]);
1267 }
1268
1269 wm->last_cursor = -1;
1270}
1271
1272static void
1273weston_wm_destroy_cursors(struct weston_wm *wm)
1274{
1275 uint8_t i;
1276
1277 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1278 xcb_free_cursor(wm->conn, wm->cursors[i]);
1279
1280 free(wm->cursors);
1281}
1282
1283static int
1284get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1285{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001286 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001287
1288 switch (location) {
1289 case THEME_LOCATION_RESIZING_TOP:
1290 return XWM_CURSOR_TOP;
1291 case THEME_LOCATION_RESIZING_BOTTOM:
1292 return XWM_CURSOR_BOTTOM;
1293 case THEME_LOCATION_RESIZING_LEFT:
1294 return XWM_CURSOR_LEFT;
1295 case THEME_LOCATION_RESIZING_RIGHT:
1296 return XWM_CURSOR_RIGHT;
1297 case THEME_LOCATION_RESIZING_TOP_LEFT:
1298 return XWM_CURSOR_TOP_LEFT;
1299 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1300 return XWM_CURSOR_TOP_RIGHT;
1301 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1302 return XWM_CURSOR_BOTTOM_LEFT;
1303 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1304 return XWM_CURSOR_BOTTOM_RIGHT;
1305 case THEME_LOCATION_EXTERIOR:
1306 case THEME_LOCATION_TITLEBAR:
1307 default:
1308 return XWM_CURSOR_LEFT_PTR;
1309 }
1310}
1311
1312static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001313weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1314 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001315{
1316 uint32_t cursor_value_list;
1317
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001318 if (wm->last_cursor == cursor)
1319 return;
1320
1321 wm->last_cursor = cursor;
1322
1323 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001324 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001325 XCB_CW_CURSOR, &cursor_value_list);
1326 xcb_flush(wm->conn);
1327}
1328
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001329static void
1330weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1331{
1332 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1333 struct weston_shell_interface *shell_interface =
1334 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001335 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001336 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001337 enum theme_location location;
1338 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001339 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001340
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001341 wm_log("XCB_BUTTON_%s (detail %d)\n",
1342 button->response_type == XCB_BUTTON_PRESS ?
1343 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001344
1345 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001346 weston_wm_window_get_frame_size(window, &width, &height);
1347
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001348 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001349 button->detail == 1) {
1350 location = theme_get_location(t,
1351 button->event_x,
1352 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001353 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001354
1355 switch (location) {
1356 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001357 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001358 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001359 case THEME_LOCATION_RESIZING_TOP:
1360 case THEME_LOCATION_RESIZING_BOTTOM:
1361 case THEME_LOCATION_RESIZING_LEFT:
1362 case THEME_LOCATION_RESIZING_RIGHT:
1363 case THEME_LOCATION_RESIZING_TOP_LEFT:
1364 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1365 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1366 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1367 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001368 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001369 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001370 default:
1371 break;
1372 }
1373 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374}
1375
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001376static void
1377weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1378{
1379 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1380 struct weston_wm_window *window;
1381 int cursor, width, height;
1382
1383 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001384 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001385 return;
1386
1387 weston_wm_window_get_frame_size(window, &width, &height);
1388 cursor = get_cursor_for_location(wm->theme, width, height,
1389 motion->event_x, motion->event_y);
1390
Tiago Vignattic1903232012-07-16 12:15:37 -04001391 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001392}
1393
1394static void
1395weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1396{
1397 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1398 struct weston_wm_window *window;
1399 int cursor, width, height;
1400
1401 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001402 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001403 return;
1404
1405 weston_wm_window_get_frame_size(window, &width, &height);
1406 cursor = get_cursor_for_location(wm->theme, width, height,
1407 enter->event_x, enter->event_y);
1408
Tiago Vignattic1903232012-07-16 12:15:37 -04001409 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001410}
1411
1412static void
1413weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1414{
1415 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1416 struct weston_wm_window *window;
1417
1418 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001419 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001420 return;
1421
Tiago Vignattic1903232012-07-16 12:15:37 -04001422 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001423}
1424
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001425static int
1426weston_wm_handle_event(int fd, uint32_t mask, void *data)
1427{
1428 struct weston_wm *wm = data;
1429 xcb_generic_event_t *event;
1430 int count = 0;
1431
1432 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1433 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001434 free(event);
1435 count++;
1436 continue;
1437 }
1438
MoD31700122013-06-11 19:58:55 -05001439 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001440 case XCB_BUTTON_PRESS:
1441 case XCB_BUTTON_RELEASE:
1442 weston_wm_handle_button(wm, event);
1443 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001444 case XCB_ENTER_NOTIFY:
1445 weston_wm_handle_enter(wm, event);
1446 break;
1447 case XCB_LEAVE_NOTIFY:
1448 weston_wm_handle_leave(wm, event);
1449 break;
1450 case XCB_MOTION_NOTIFY:
1451 weston_wm_handle_motion(wm, event);
1452 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001453 case XCB_CREATE_NOTIFY:
1454 weston_wm_handle_create_notify(wm, event);
1455 break;
1456 case XCB_MAP_REQUEST:
1457 weston_wm_handle_map_request(wm, event);
1458 break;
1459 case XCB_MAP_NOTIFY:
1460 weston_wm_handle_map_notify(wm, event);
1461 break;
1462 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001463 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001465 case XCB_REPARENT_NOTIFY:
1466 weston_wm_handle_reparent_notify(wm, event);
1467 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001468 case XCB_CONFIGURE_REQUEST:
1469 weston_wm_handle_configure_request(wm, event);
1470 break;
1471 case XCB_CONFIGURE_NOTIFY:
1472 weston_wm_handle_configure_notify(wm, event);
1473 break;
1474 case XCB_DESTROY_NOTIFY:
1475 weston_wm_handle_destroy_notify(wm, event);
1476 break;
1477 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001478 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001479 break;
1480 case XCB_PROPERTY_NOTIFY:
1481 weston_wm_handle_property_notify(wm, event);
1482 break;
1483 case XCB_CLIENT_MESSAGE:
1484 weston_wm_handle_client_message(wm, event);
1485 break;
1486 }
1487
1488 free(event);
1489 count++;
1490 }
1491
1492 xcb_flush(wm->conn);
1493
1494 return count;
1495}
1496
1497static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001498weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1499{
1500 xcb_depth_iterator_t d_iter;
1501 xcb_visualtype_iterator_t vt_iter;
1502 xcb_visualtype_t *visualtype;
1503
1504 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1505 visualtype = NULL;
1506 while (d_iter.rem > 0) {
1507 if (d_iter.data->depth == 32) {
1508 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1509 visualtype = vt_iter.data;
1510 break;
1511 }
1512
1513 xcb_depth_next(&d_iter);
1514 }
1515
1516 if (visualtype == NULL) {
1517 weston_log("no 32 bit visualtype\n");
1518 return;
1519 }
1520
1521 wm->visual_id = visualtype->visual_id;
1522 wm->colormap = xcb_generate_id(wm->conn);
1523 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1524 wm->colormap, wm->screen->root, wm->visual_id);
1525}
1526
1527static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001528weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001529{
1530
1531#define F(field) offsetof(struct weston_wm, field)
1532
1533 static const struct { const char *name; int offset; } atoms[] = {
1534 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1535 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1536 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001537 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001538 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001539 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001540 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001541 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001542 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001543 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1544 { "_NET_WM_STATE", F(atom.net_wm_state) },
1545 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1546 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1547 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1548 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1549
1550 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1551 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1552 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1553 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1554 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1555 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1556 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001557 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1558 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001559 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1560 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1561 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1562 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1563 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1564
1565 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1566 { "_NET_SUPPORTING_WM_CHECK",
1567 F(atom.net_supporting_wm_check) },
1568 { "_NET_SUPPORTED", F(atom.net_supported) },
1569 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1570 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001571 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001572 { "TARGETS", F(atom.targets) },
1573 { "UTF8_STRING", F(atom.utf8_string) },
1574 { "_WL_SELECTION", F(atom.wl_selection) },
1575 { "INCR", F(atom.incr) },
1576 { "TIMESTAMP", F(atom.timestamp) },
1577 { "MULTIPLE", F(atom.multiple) },
1578 { "UTF8_STRING" , F(atom.utf8_string) },
1579 { "COMPOUND_TEXT", F(atom.compound_text) },
1580 { "TEXT", F(atom.text) },
1581 { "STRING", F(atom.string) },
1582 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1583 { "text/plain", F(atom.text_plain) },
1584 };
1585#undef F
1586
1587 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1588 xcb_xfixes_query_version_reply_t *xfixes_reply;
1589 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1590 xcb_intern_atom_reply_t *reply;
1591 xcb_render_query_pict_formats_reply_t *formats_reply;
1592 xcb_render_query_pict_formats_cookie_t formats_cookie;
1593 xcb_render_pictforminfo_t *formats;
1594 uint32_t i;
1595
1596 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1597
1598 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1599
1600 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1601 cookies[i] = xcb_intern_atom (wm->conn, 0,
1602 strlen(atoms[i].name),
1603 atoms[i].name);
1604
1605 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1606 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1607 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1608 free(reply);
1609 }
1610
1611 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1612 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001613 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001614
1615 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1616 XCB_XFIXES_MAJOR_VERSION,
1617 XCB_XFIXES_MINOR_VERSION);
1618 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1619 xfixes_cookie, NULL);
1620
Martin Minarik6d118362012-06-07 18:01:59 +02001621 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001622 xfixes_reply->major_version, xfixes_reply->minor_version);
1623
1624 free(xfixes_reply);
1625
1626 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1627 formats_cookie, 0);
1628 if (formats_reply == NULL)
1629 return;
1630
1631 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001632 for (i = 0; i < formats_reply->num_formats; i++) {
1633 if (formats[i].direct.red_mask != 0xff &&
1634 formats[i].direct.red_shift != 16)
1635 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001636 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1637 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001638 wm->format_rgb = formats[i];
1639 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1640 formats[i].depth == 32 &&
1641 formats[i].direct.alpha_mask == 0xff &&
1642 formats[i].direct.alpha_shift == 24)
1643 wm->format_rgba = formats[i];
1644 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001645
1646 free(formats_reply);
1647}
1648
1649static void
1650weston_wm_create_wm_window(struct weston_wm *wm)
1651{
1652 static const char name[] = "Weston WM";
1653
1654 wm->wm_window = xcb_generate_id(wm->conn);
1655 xcb_create_window(wm->conn,
1656 XCB_COPY_FROM_PARENT,
1657 wm->wm_window,
1658 wm->screen->root,
1659 0, 0,
1660 10, 10,
1661 0,
1662 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1663 wm->screen->root_visual,
1664 0, NULL);
1665
1666 xcb_change_property(wm->conn,
1667 XCB_PROP_MODE_REPLACE,
1668 wm->wm_window,
1669 wm->atom.net_supporting_wm_check,
1670 XCB_ATOM_WINDOW,
1671 32, /* format */
1672 1, &wm->wm_window);
1673
1674 xcb_change_property(wm->conn,
1675 XCB_PROP_MODE_REPLACE,
1676 wm->wm_window,
1677 wm->atom.net_wm_name,
1678 wm->atom.utf8_string,
1679 8, /* format */
1680 strlen(name), name);
1681
1682 xcb_change_property(wm->conn,
1683 XCB_PROP_MODE_REPLACE,
1684 wm->screen->root,
1685 wm->atom.net_supporting_wm_check,
1686 XCB_ATOM_WINDOW,
1687 32, /* format */
1688 1, &wm->wm_window);
1689
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001690 /* Claim the WM_S0 selection even though we don't suport
1691 * the --replace functionality. */
1692 xcb_set_selection_owner(wm->conn,
1693 wm->wm_window,
1694 wm->atom.wm_s0,
1695 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001696
1697 xcb_set_selection_owner(wm->conn,
1698 wm->wm_window,
1699 wm->atom.net_wm_cm_s0,
1700 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001701}
1702
1703struct weston_wm *
1704weston_wm_create(struct weston_xserver *wxs)
1705{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001706 struct weston_wm *wm;
1707 struct wl_event_loop *loop;
1708 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001709 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001711 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001712
Peter Huttererf3d62272013-08-08 11:57:05 +10001713 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001714 if (wm == NULL)
1715 return NULL;
1716
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001717 wm->server = wxs;
1718 wm->window_hash = hash_table_create();
1719 if (wm->window_hash == NULL) {
1720 free(wm);
1721 return NULL;
1722 }
1723
1724 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001725 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001726 hash_table_destroy(wm->window_hash);
1727 free(wm);
1728 return NULL;
1729 }
1730
1731 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001732 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001733 close(sv[1]);
1734
1735 /* xcb_connect_to_fd takes ownership of the fd. */
1736 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1737 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001738 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001739 close(sv[0]);
1740 hash_table_destroy(wm->window_hash);
1741 free(wm);
1742 return NULL;
1743 }
1744
1745 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1746 wm->screen = s.data;
1747
1748 loop = wl_display_get_event_loop(wxs->wl_display);
1749 wm->source =
1750 wl_event_loop_add_fd(loop, sv[0],
1751 WL_EVENT_READABLE,
1752 weston_wm_handle_event, wm);
1753 wl_event_source_check(wm->source);
1754
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001755 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001756 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001757
1758 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001759 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1760 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1761 XCB_EVENT_MASK_PROPERTY_CHANGE;
1762 xcb_change_window_attributes(wm->conn, wm->screen->root,
1763 XCB_CW_EVENT_MASK, values);
1764 wm->theme = theme_create();
1765
1766 weston_wm_create_wm_window(wm);
1767
1768 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001769 supported[1] = wm->atom.net_wm_state;
1770 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001771 xcb_change_property(wm->conn,
1772 XCB_PROP_MODE_REPLACE,
1773 wm->screen->root,
1774 wm->atom.net_supported,
1775 XCB_ATOM_ATOM,
1776 32, /* format */
1777 ARRAY_LENGTH(supported), supported);
1778
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001779 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001780
1781 xcb_flush(wm->conn);
1782
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001783 wm->activate_listener.notify = weston_wm_window_activate;
1784 wl_signal_add(&wxs->compositor->activate_signal,
1785 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001786 wm->transform_listener.notify = weston_wm_window_transform;
1787 wl_signal_add(&wxs->compositor->transform_signal,
1788 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001789 wm->kill_listener.notify = weston_wm_kill_client;
1790 wl_signal_add(&wxs->compositor->kill_signal,
1791 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001792
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001793 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001794 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001795
Martin Minarik6d118362012-06-07 18:01:59 +02001796 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001797
1798 return wm;
1799}
1800
1801void
1802weston_wm_destroy(struct weston_wm *wm)
1803{
1804 /* FIXME: Free windows in hash. */
1805 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001806 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001807 xcb_disconnect(wm->conn);
1808 wl_event_source_remove(wm->source);
1809 wl_list_remove(&wm->selection_listener.link);
1810 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001811 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001812 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001813
1814 free(wm);
1815}
1816
1817static void
1818surface_destroy(struct wl_listener *listener, void *data)
1819{
1820 struct weston_wm_window *window =
1821 container_of(listener,
1822 struct weston_wm_window, surface_destroy_listener);
1823
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001824 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001825}
1826
1827static struct weston_wm_window *
1828get_wm_window(struct weston_surface *surface)
1829{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001830 struct wl_listener *listener;
1831
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001832 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001833 if (listener)
1834 return container_of(listener, struct weston_wm_window,
1835 surface_destroy_listener);
1836
1837 return NULL;
1838}
1839
1840static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001841weston_wm_window_configure(void *data)
1842{
1843 struct weston_wm_window *window = data;
1844 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001845 uint32_t values[4];
1846 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001847
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001848 weston_wm_window_get_child_position(window, &x, &y);
1849 values[0] = x;
1850 values[1] = y;
1851 values[2] = window->width;
1852 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001853 xcb_configure_window(wm->conn,
1854 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001855 XCB_CONFIG_WINDOW_X |
1856 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001857 XCB_CONFIG_WINDOW_WIDTH |
1858 XCB_CONFIG_WINDOW_HEIGHT,
1859 values);
1860
1861 weston_wm_window_get_frame_size(window, &width, &height);
1862 values[0] = width;
1863 values[1] = height;
1864 xcb_configure_window(wm->conn,
1865 window->frame_id,
1866 XCB_CONFIG_WINDOW_WIDTH |
1867 XCB_CONFIG_WINDOW_HEIGHT,
1868 values);
1869
1870 window->configure_source = NULL;
1871
1872 weston_wm_window_schedule_repaint(window);
1873}
1874
1875static void
1876send_configure(struct weston_surface *surface,
1877 uint32_t edges, int32_t width, int32_t height)
1878{
1879 struct weston_wm_window *window = get_wm_window(surface);
1880 struct weston_wm *wm = window->wm;
1881 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001882 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001883
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001884 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001885 hborder = 0;
1886 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001887 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001888 hborder = 2 * (t->margin + t->width);
1889 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001890 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001891 hborder = 2 * t->margin;
1892 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001893 }
1894
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001895 if (width > hborder)
1896 window->width = width - hborder;
1897 else
1898 window->width = 1;
1899
1900 if (height > vborder)
1901 window->height = height - vborder;
1902 else
1903 window->height = 1;
1904
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001905 if (window->configure_source)
1906 return;
1907
1908 window->configure_source =
1909 wl_event_loop_add_idle(wm->server->loop,
1910 weston_wm_window_configure, window);
1911}
1912
1913static const struct weston_shell_client shell_client = {
1914 send_configure
1915};
1916
1917static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001918xserver_map_shell_surface(struct weston_wm *wm,
1919 struct weston_wm_window *window)
1920{
1921 struct weston_shell_interface *shell_interface =
1922 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001923 struct theme *t = window->wm->theme;
1924
1925 if (!shell_interface->create_shell_surface)
1926 return;
1927
1928 window->shsurf =
1929 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001930 window->surface,
1931 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001932
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001933 if (window->fullscreen) {
1934 window->saved_width = window->width;
1935 window->saved_height = window->height;
1936 shell_interface->set_fullscreen(window->shsurf,
1937 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1938 0, NULL);
1939 return;
1940 } else if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001941 shell_interface->set_toplevel(window->shsurf);
1942 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001943 } else {
1944 shell_interface->set_xwayland(window->shsurf,
1945 window->x + t->margin,
1946 window->y + t->margin,
1947 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001948 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001949}
1950
1951static void
1952xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1953 struct wl_resource *surface_resource, uint32_t id)
1954{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001955 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001956 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001957 struct weston_surface *surface =
1958 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001959 struct weston_wm_window *window;
1960
1961 if (client != wxs->client)
1962 return;
1963
1964 window = hash_table_lookup(wm->window_hash, id);
1965 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001966 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001967 return;
1968 }
1969
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001970 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001971
1972 weston_wm_window_read_properties(window);
1973
1974 window->surface = (struct weston_surface *) surface;
1975 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001976 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001977 &window->surface_destroy_listener);
1978
1979 weston_wm_window_schedule_repaint(window);
1980 xserver_map_shell_surface(wm, window);
1981}
1982
1983const struct xserver_interface xserver_implementation = {
1984 xserver_set_window_id
1985};