blob: 4d3611f23ca7edd676b3831224b1514ef6a636f3 [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>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050035#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040036
37#include "xwayland.h"
38
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080039#include "cairo-util.h"
40#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041#include "hash.h"
42
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070043struct wm_size_hints {
44 uint32_t flags;
45 int32_t x, y;
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
50 struct {
51 int32_t x;
52 int32_t y;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
55 int32_t win_gravity;
56};
57
58#define USPosition (1L << 0)
59#define USSize (1L << 1)
60#define PPosition (1L << 2)
61#define PSize (1L << 3)
62#define PMinSize (1L << 4)
63#define PMaxSize (1L << 5)
64#define PResizeInc (1L << 6)
65#define PAspect (1L << 7)
66#define PBaseSize (1L << 8)
67#define PWinGravity (1L << 9)
68
Kristian Høgsberg380deee2012-05-21 17:12:41 -040069struct motif_wm_hints {
70 uint32_t flags;
71 uint32_t functions;
72 uint32_t decorations;
73 int32_t input_mode;
74 uint32_t status;
75};
76
77#define MWM_HINTS_FUNCTIONS (1L << 0)
78#define MWM_HINTS_DECORATIONS (1L << 1)
79#define MWM_HINTS_INPUT_MODE (1L << 2)
80#define MWM_HINTS_STATUS (1L << 3)
81
82#define MWM_FUNC_ALL (1L << 0)
83#define MWM_FUNC_RESIZE (1L << 1)
84#define MWM_FUNC_MOVE (1L << 2)
85#define MWM_FUNC_MINIMIZE (1L << 3)
86#define MWM_FUNC_MAXIMIZE (1L << 4)
87#define MWM_FUNC_CLOSE (1L << 5)
88
89#define MWM_DECOR_ALL (1L << 0)
90#define MWM_DECOR_BORDER (1L << 1)
91#define MWM_DECOR_RESIZEH (1L << 2)
92#define MWM_DECOR_TITLE (1L << 3)
93#define MWM_DECOR_MENU (1L << 4)
94#define MWM_DECOR_MINIMIZE (1L << 5)
95#define MWM_DECOR_MAXIMIZE (1L << 6)
96
97#define MWM_INPUT_MODELESS 0
98#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99#define MWM_INPUT_SYSTEM_MODAL 2
100#define MWM_INPUT_FULL_APPLICATION_MODAL 3
101#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
102
103#define MWM_TEAROFF_WINDOW (1L<<0)
104
105#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106#define _NET_WM_MOVERESIZE_SIZE_TOP 1
107#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
117
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400118struct weston_wm_window {
119 struct weston_wm *wm;
120 xcb_window_t id;
121 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500122 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400123 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700124 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400125 struct weston_surface *surface;
126 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128 struct wl_listener surface_destroy_listener;
129 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400130 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300132 int pid;
133 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134 char *class;
135 char *name;
136 struct weston_wm_window *transient_for;
137 uint32_t protocols;
138 xcb_atom_t type;
139 int width, height;
140 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500141 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300143 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500144 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500145 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700146 int delete_window;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700147 struct wm_size_hints size_hints;
148 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700149 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150};
151
152static struct weston_wm_window *
153get_wm_window(struct weston_surface *surface);
154
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400155static void
156weston_wm_window_schedule_repaint(struct weston_wm_window *window);
157
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700158static void
159xserver_map_shell_surface(struct weston_wm_window *window,
160 struct weston_surface *surface);
161
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400162static int __attribute__ ((format (printf, 1, 2)))
163wm_log(const char *fmt, ...)
164{
165#ifdef WM_DEBUG
166 int l;
167 va_list argp;
168
169 va_start(argp, fmt);
170 l = weston_vlog(fmt, argp);
171 va_end(argp);
172
173 return l;
174#else
175 return 0;
176#endif
177}
178
179static int __attribute__ ((format (printf, 1, 2)))
180wm_log_continue(const char *fmt, ...)
181{
182#ifdef WM_DEBUG
183 int l;
184 va_list argp;
185
186 va_start(argp, fmt);
187 l = weston_vlog_continue(fmt, argp);
188 va_end(argp);
189
190 return l;
191#else
192 return 0;
193#endif
194}
195
196
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400197const char *
198get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
199{
200 xcb_get_atom_name_cookie_t cookie;
201 xcb_get_atom_name_reply_t *reply;
202 xcb_generic_error_t *e;
203 static char buffer[64];
204
205 if (atom == XCB_ATOM_NONE)
206 return "None";
207
208 cookie = xcb_get_atom_name (c, atom);
209 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500210
211 if(reply) {
212 snprintf(buffer, sizeof buffer, "%.*s",
213 xcb_get_atom_name_name_length (reply),
214 xcb_get_atom_name_name (reply));
215 } else {
216 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
217 }
218
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400219 free(reply);
220
221 return buffer;
222}
223
Tiago Vignatti90fada42012-07-16 12:02:08 -0400224static xcb_cursor_t
225xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
226{
227 xcb_connection_t *c = wm->conn;
228 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
229 xcb_screen_t *screen = s.data;
230 xcb_gcontext_t gc;
231 xcb_pixmap_t pix;
232 xcb_render_picture_t pic;
233 xcb_cursor_t cursor;
234 int stride = img->width * 4;
235
236 pix = xcb_generate_id(c);
237 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
238
239 pic = xcb_generate_id(c);
240 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
241
242 gc = xcb_generate_id(c);
243 xcb_create_gc(c, gc, pix, 0, 0);
244
245 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
246 img->width, img->height, 0, 0, 0, 32,
247 stride * img->height, (uint8_t *) img->pixels);
248 xcb_free_gc(c, gc);
249
250 cursor = xcb_generate_id(c);
251 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
252
253 xcb_render_free_picture(c, pic);
254 xcb_free_pixmap(c, pix);
255
256 return cursor;
257}
258
259static xcb_cursor_t
260xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
261{
262 /* TODO: treat animated cursors as well */
263 if (images->nimage != 1)
264 return -1;
265
266 return xcb_cursor_image_load_cursor(wm, images->images[0]);
267}
268
269static xcb_cursor_t
270xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
271{
272 xcb_cursor_t cursor;
273 XcursorImages *images;
274 char *v = NULL;
275 int size = 0;
276
277 if (!file)
278 return 0;
279
280 v = getenv ("XCURSOR_SIZE");
281 if (v)
282 size = atoi(v);
283
284 if (!size)
285 size = 32;
286
287 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300288 if (!images)
289 return -1;
290
Tiago Vignatti90fada42012-07-16 12:02:08 -0400291 cursor = xcb_cursor_images_load_cursor (wm, images);
292 XcursorImagesDestroy (images);
293
294 return cursor;
295}
296
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400297void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400298dump_property(struct weston_wm *wm,
299 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400300{
301 int32_t *incr_value;
302 const char *text_value, *name;
303 xcb_atom_t *atom_value;
304 int width, len;
305 uint32_t i;
306
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400307 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400308 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400309 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400310 return;
311 }
312
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400313 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
314 get_atom_name(wm->conn, reply->type),
315 reply->format,
316 xcb_get_property_value_length(reply),
317 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400318
319 if (reply->type == wm->atom.incr) {
320 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400321 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400322 } else if (reply->type == wm->atom.utf8_string ||
323 reply->type == wm->atom.string) {
324 text_value = xcb_get_property_value(reply);
325 if (reply->value_len > 40)
326 len = 40;
327 else
328 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 } else if (reply->type == XCB_ATOM_ATOM) {
331 atom_value = xcb_get_property_value(reply);
332 for (i = 0; i < reply->value_len; i++) {
333 name = get_atom_name(wm->conn, atom_value[i]);
334 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400335 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400336 width = 4;
337 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400338 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400339 }
340
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400341 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400342 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400343 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400344 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400345 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346 }
347}
348
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200349static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400350read_and_dump_property(struct weston_wm *wm,
351 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400353 xcb_get_property_reply_t *reply;
354 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400355
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400356 cookie = xcb_get_property(wm->conn, 0, window,
357 property, XCB_ATOM_ANY, 0, 2048);
358 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400359
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400360 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400362 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363}
364
365/* We reuse some predefined, but otherwise useles atoms */
366#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
367#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500368#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700369#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370
371static void
372weston_wm_window_read_properties(struct weston_wm_window *window)
373{
374 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200375 struct weston_shell_interface *shell_interface =
376 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400377
378#define F(field) offsetof(struct weston_wm_window, field)
379 const struct {
380 xcb_atom_t atom;
381 xcb_atom_t type;
382 int offset;
383 } props[] = {
384 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
385 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
386 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
387 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700388 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500389 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400390 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
391 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300392 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400393 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300394 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395 };
396#undef F
397
398 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
399 xcb_get_property_reply_t *reply;
400 void *p;
401 uint32_t *xid;
402 xcb_atom_t *atom;
403 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400404
405 if (!window->properties_dirty)
406 return;
407 window->properties_dirty = 0;
408
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400409 for (i = 0; i < ARRAY_LENGTH(props); i++)
410 cookie[i] = xcb_get_property(wm->conn,
411 0, /* delete */
412 window->id,
413 props[i].atom,
414 XCB_ATOM_ANY, 0, 2048);
415
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300416 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700417 window->size_hints.flags = 0;
418 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700419 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700420
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400421 for (i = 0; i < ARRAY_LENGTH(props); i++) {
422 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
423 if (!reply)
424 /* Bad window, typically */
425 continue;
426 if (reply->type == XCB_ATOM_NONE) {
427 /* No such property */
428 free(reply);
429 continue;
430 }
431
432 p = ((char *) window + props[i].offset);
433
434 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300435 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436 case XCB_ATOM_STRING:
437 /* FIXME: We're using this for both string and
438 utf8_string */
439 if (*(char **) p)
440 free(*(char **) p);
441
442 *(char **) p =
443 strndup(xcb_get_property_value(reply),
444 xcb_get_property_value_length(reply));
445 break;
446 case XCB_ATOM_WINDOW:
447 xid = xcb_get_property_value(reply);
448 *(struct weston_wm_window **) p =
449 hash_table_lookup(wm->window_hash, *xid);
450 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300451 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400452 case XCB_ATOM_ATOM:
453 atom = xcb_get_property_value(reply);
454 *(xcb_atom_t *) p = *atom;
455 break;
456 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700457 atom = xcb_get_property_value(reply);
458 for (i = 0; i < reply->value_len; i++)
459 if (atom[i] == wm->atom.wm_delete_window)
460 window->delete_window = 1;
461 break;
462
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400463 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700464 case TYPE_WM_NORMAL_HINTS:
465 memcpy(&window->size_hints,
466 xcb_get_property_value(reply),
467 sizeof window->size_hints);
468 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500469 case TYPE_NET_WM_STATE:
470 window->fullscreen = 0;
471 atom = xcb_get_property_value(reply);
472 for (i = 0; i < reply->value_len; i++)
473 if (atom[i] == wm->atom.net_wm_state_fullscreen)
474 window->fullscreen = 1;
475 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700477 memcpy(&window->motif_hints,
478 xcb_get_property_value(reply),
479 sizeof window->motif_hints);
480 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
481 window->decorate =
482 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400483 break;
484 default:
485 break;
486 }
487 free(reply);
488 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200489
490 if (window->shsurf && window->name)
491 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500492 if (window->frame && window->name)
493 frame_set_title(window->frame, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400494}
495
496static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400497weston_wm_window_get_frame_size(struct weston_wm_window *window,
498 int *width, int *height)
499{
500 struct theme *t = window->wm->theme;
501
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500502 if (window->fullscreen) {
503 *width = window->width;
504 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800505 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500506 *width = frame_width(window->frame);
507 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400508 } else {
509 *width = window->width + t->margin * 2;
510 *height = window->height + t->margin * 2;
511 }
512}
513
514static void
515weston_wm_window_get_child_position(struct weston_wm_window *window,
516 int *x, int *y)
517{
518 struct theme *t = window->wm->theme;
519
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500520 if (window->fullscreen) {
521 *x = 0;
522 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800523 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500524 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400525 } else {
526 *x = t->margin;
527 *y = t->margin;
528 }
529}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400530
531static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500532weston_wm_window_send_configure_notify(struct weston_wm_window *window)
533{
534 xcb_configure_notify_event_t configure_notify;
535 struct weston_wm *wm = window->wm;
536 int x, y;
537
538 weston_wm_window_get_child_position(window, &x, &y);
539 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
540 configure_notify.pad0 = 0;
541 configure_notify.event = window->id;
542 configure_notify.window = window->id;
543 configure_notify.above_sibling = XCB_WINDOW_NONE;
544 configure_notify.x = x;
545 configure_notify.y = y;
546 configure_notify.width = window->width;
547 configure_notify.height = window->height;
548 configure_notify.border_width = 0;
549 configure_notify.override_redirect = 0;
550 configure_notify.pad1 = 0;
551
552 xcb_send_event(wm->conn, 0, window->id,
553 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
554 (char *) &configure_notify);
555}
556
557static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400558weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
559{
560 xcb_configure_request_event_t *configure_request =
561 (xcb_configure_request_event_t *) event;
562 struct weston_wm_window *window;
563 uint32_t mask, values[16];
564 int x, y, width, height, i = 0;
565
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400566 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
567 configure_request->window,
568 configure_request->x, configure_request->y,
569 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400570
571 window = hash_table_lookup(wm->window_hash, configure_request->window);
572
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500573 if (window->fullscreen) {
574 weston_wm_window_send_configure_notify(window);
575 return;
576 }
577
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400578 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
579 window->width = configure_request->width;
580 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
581 window->height = configure_request->height;
582
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500583 if (window->frame)
584 frame_resize_inside(window->frame, window->width, window->height);
585
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400586 weston_wm_window_get_child_position(window, &x, &y);
587 values[i++] = x;
588 values[i++] = y;
589 values[i++] = window->width;
590 values[i++] = window->height;
591 values[i++] = 0;
592 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
593 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
594 XCB_CONFIG_WINDOW_BORDER_WIDTH;
595 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
596 values[i++] = configure_request->sibling;
597 mask |= XCB_CONFIG_WINDOW_SIBLING;
598 }
599 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
600 values[i++] = configure_request->stack_mode;
601 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
602 }
603
604 xcb_configure_window(wm->conn, window->id, mask, values);
605
606 weston_wm_window_get_frame_size(window, &width, &height);
607 values[0] = width;
608 values[1] = height;
609 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
610 xcb_configure_window(wm->conn, window->frame_id, mask, values);
611
612 weston_wm_window_schedule_repaint(window);
613}
614
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400615static int
616our_resource(struct weston_wm *wm, uint32_t id)
617{
618 const xcb_setup_t *setup;
619
620 setup = xcb_get_setup(wm->conn);
621
622 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
623}
624
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400625static void
626weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
627{
628 xcb_configure_notify_event_t *configure_notify =
629 (xcb_configure_notify_event_t *) event;
630 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400631
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400632 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400633 configure_notify->window,
634 configure_notify->x, configure_notify->y,
635 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300636
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400637 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700638 window->x = configure_notify->x;
639 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700640 if (window->override_redirect) {
641 window->width = configure_notify->width;
642 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500643 if (window->frame)
644 frame_resize_inside(window->frame,
645 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700646 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400647}
648
649static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300650weston_wm_kill_client(struct wl_listener *listener, void *data)
651{
652 struct weston_surface *surface = data;
653 struct weston_wm_window *window = get_wm_window(surface);
654 char name[1024];
655
656 if (!window)
657 return;
658
659 gethostname(name, 1024);
660
661 /* this is only one heuristic to guess the PID of a client is valid,
662 * assuming it's compliant with icccm and ewmh. Non-compliants and
663 * remote applications of course fail. */
664 if (!strcmp(window->machine, name) && window->pid != 0)
665 kill(window->pid, SIGKILL);
666}
667
668static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700669weston_wm_create_surface(struct wl_listener *listener, void *data)
670{
671 struct weston_surface *surface = data;
672 struct weston_wm *wm =
673 container_of(listener,
674 struct weston_wm, create_surface_listener);
675 struct weston_wm_window *window;
676
677 if (wl_resource_get_client(surface->resource) != wm->server->client)
678 return;
679
680 wl_list_for_each(window, &wm->unpaired_window_list, link)
681 if (window->surface_id ==
682 wl_resource_get_id(surface->resource)) {
683 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700684 window->surface_id = 0;
685 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700686 break;
687 }
688}
689
690static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400691weston_wm_window_activate(struct wl_listener *listener, void *data)
692{
693 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200694 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600695 struct weston_wm *wm =
696 container_of(listener, struct weston_wm, activate_listener);
697 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400698
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200699 if (surface) {
700 window = get_wm_window(surface);
701 }
702
Scott Moreau85ecac02012-05-21 15:49:14 -0600703 if (window) {
Boyan Dingb9f863c2014-08-30 10:33:23 +0800704 if (window->override_redirect)
705 return;
706
Scott Moreau85ecac02012-05-21 15:49:14 -0600707 client_message.response_type = XCB_CLIENT_MESSAGE;
708 client_message.format = 32;
709 client_message.window = window->id;
710 client_message.type = wm->atom.wm_protocols;
711 client_message.data.data32[0] = wm->atom.wm_take_focus;
712 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
713
714 xcb_send_event(wm->conn, 0, window->id,
715 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
716 (char *) &client_message);
717
718 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
719 window->id, XCB_TIME_CURRENT_TIME);
720 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400721 xcb_set_input_focus (wm->conn,
722 XCB_INPUT_FOCUS_POINTER_ROOT,
723 XCB_NONE,
724 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600725 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400726
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500727 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800728 if (wm->focus_window->frame)
729 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400730 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500731 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500733 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800734 if (wm->focus_window->frame)
735 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400736 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500737 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400738}
739
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300740static void
741weston_wm_window_transform(struct wl_listener *listener, void *data)
742{
743 struct weston_surface *surface = data;
744 struct weston_wm_window *window = get_wm_window(surface);
745 struct weston_wm *wm =
746 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300747 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300748
749 if (!window || !wm)
750 return;
751
Jason Ekstranda7af7042013-10-12 22:38:11 -0500752 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300753 return;
754
Jason Ekstranda7af7042013-10-12 22:38:11 -0500755 if (window->x != window->view->geometry.x ||
756 window->y != window->view->geometry.y) {
757 values[0] = window->view->geometry.x;
758 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700759 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300760
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700761 xcb_configure_window(wm->conn, window->frame_id, mask, values);
762 xcb_flush(wm->conn);
763 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300764}
765
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400766#define ICCCM_WITHDRAWN_STATE 0
767#define ICCCM_NORMAL_STATE 1
768#define ICCCM_ICONIC_STATE 3
769
770static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500771weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400772{
773 struct weston_wm *wm = window->wm;
774 uint32_t property[2];
775
776 property[0] = state;
777 property[1] = XCB_WINDOW_NONE;
778
779 xcb_change_property(wm->conn,
780 XCB_PROP_MODE_REPLACE,
781 window->id,
782 wm->atom.wm_state,
783 wm->atom.wm_state,
784 32, /* format */
785 2, property);
786}
787
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400788static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500789weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
790{
791 struct weston_wm *wm = window->wm;
792 uint32_t property[1];
793 int i;
794
795 i = 0;
796 if (window->fullscreen)
797 property[i++] = wm->atom.net_wm_state_fullscreen;
798
799 xcb_change_property(wm->conn,
800 XCB_PROP_MODE_REPLACE,
801 window->id,
802 wm->atom.net_wm_state,
803 XCB_ATOM_ATOM,
804 32, /* format */
805 i, property);
806}
807
808static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700809weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400810{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700811 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400812 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400813 int x, y, width, height;
814
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500815 window->frame = frame_create(window->wm->theme,
816 window->width, window->height,
817 FRAME_BUTTON_CLOSE, window->name);
818 frame_resize_inside(window->frame, window->width, window->height);
819
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400820 weston_wm_window_get_frame_size(window, &width, &height);
821 weston_wm_window_get_child_position(window, &x, &y);
822
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400823 values[0] = wm->screen->black_pixel;
824 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400825 XCB_EVENT_MASK_KEY_PRESS |
826 XCB_EVENT_MASK_KEY_RELEASE |
827 XCB_EVENT_MASK_BUTTON_PRESS |
828 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400829 XCB_EVENT_MASK_POINTER_MOTION |
830 XCB_EVENT_MASK_ENTER_WINDOW |
831 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400832 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400833 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400834 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400835
836 window->frame_id = xcb_generate_id(wm->conn);
837 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400838 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839 window->frame_id,
840 wm->screen->root,
841 0, 0,
842 width, height,
843 0,
844 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400845 wm->visual_id,
846 XCB_CW_BORDER_PIXEL |
847 XCB_CW_EVENT_MASK |
848 XCB_CW_COLORMAP, values);
849
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400850 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
851
852 values[0] = 0;
853 xcb_configure_window(wm->conn, window->id,
854 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
855
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400856 window->cairo_surface =
857 cairo_xcb_surface_create_with_xrender_format(wm->conn,
858 wm->screen,
859 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400860 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400861 width, height);
862
863 hash_table_insert(wm->window_hash, window->frame_id, window);
864}
865
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200866/*
867 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
868 * Passing a <0 desktop value deletes the property.
869 */
870static void
871weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
872 int desktop)
873{
874 if (desktop >= 0) {
875 xcb_change_property(window->wm->conn,
876 XCB_PROP_MODE_REPLACE,
877 window->id,
878 window->wm->atom.net_wm_desktop,
879 XCB_ATOM_CARDINAL,
880 32, /* format */
881 1, &desktop);
882 } else {
883 xcb_delete_property(window->wm->conn,
884 window->id,
885 window->wm->atom.net_wm_desktop);
886 }
887}
888
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400889static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700890weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
891{
892 xcb_map_request_event_t *map_request =
893 (xcb_map_request_event_t *) event;
894 struct weston_wm_window *window;
895
896 if (our_resource(wm, map_request->window)) {
897 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
898 map_request->window);
899 return;
900 }
901
902 window = hash_table_lookup(wm->window_hash, map_request->window);
903
904 weston_wm_window_read_properties(window);
905
906 if (window->frame_id == XCB_WINDOW_NONE)
907 weston_wm_window_create_frame(window);
908
909 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
910 window->id, window, window->frame_id);
911
912 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
913 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200914 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700915
916 xcb_map_window(wm->conn, map_request->window);
917 xcb_map_window(wm->conn, window->frame_id);
918}
919
920static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400921weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
922{
923 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
924
925 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400926 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
927 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400928 return;
929 }
930
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400931 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400932}
933
934static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400935weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
936{
937 xcb_unmap_notify_event_t *unmap_notify =
938 (xcb_unmap_notify_event_t *) event;
939 struct weston_wm_window *window;
940
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400941 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
942 unmap_notify->window,
943 unmap_notify->event,
944 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400945
946 if (our_resource(wm, unmap_notify->window))
947 return;
948
MoD31700122013-06-11 19:58:55 -0500949 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400950 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
951 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400952 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400953
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400954 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400955 if (wm->focus_window == window)
956 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400957 if (window->surface)
958 wl_list_remove(&window->surface_destroy_listener.link);
959 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200960 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -0800961 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200962
963 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
964 weston_wm_window_set_virtual_desktop(window, -1);
965
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700966 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400967}
968
969static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400970weston_wm_window_draw_decoration(void *data)
971{
972 struct weston_wm_window *window = data;
973 struct weston_wm *wm = window->wm;
974 struct theme *t = wm->theme;
975 cairo_t *cr;
976 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500977 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -0700978 struct weston_shell_interface *shell_interface =
979 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400980 uint32_t flags = 0;
981
982 weston_wm_window_read_properties(window);
983
984 window->repaint_source = NULL;
985
986 weston_wm_window_get_frame_size(window, &width, &height);
987 weston_wm_window_get_child_position(window, &x, &y);
988
989 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
990 cr = cairo_create(window->cairo_surface);
991
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500992 if (window->fullscreen) {
993 /* nothing */
994 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400995 if (wm->focus_window == window)
996 flags |= THEME_FRAME_ACTIVE;
997
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500998 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400999 } else {
1000 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1001 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1002 cairo_paint(cr);
1003
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001004 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005 }
1006
1007 cairo_destroy(cr);
1008
1009 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001010 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001011 if(window->has_alpha) {
1012 pixman_region32_init(&window->surface->pending.opaque);
1013 } else {
1014 /* We leave an extra pixel around the X window area to
1015 * make sure we don't sample from the undefined alpha
1016 * channel when filtering. */
1017 pixman_region32_init_rect(&window->surface->pending.opaque,
1018 x - 1, y - 1,
1019 window->width + 2,
1020 window->height + 2);
1021 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 if (window->view)
1023 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001024
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001025 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001026
1027 if (window->fullscreen) {
1028 input_x = 0;
1029 input_y = 0;
1030 input_w = window->width;
1031 input_h = window->height;
1032 } else if (window->decorate) {
1033 frame_input_rect(window->frame, &input_x, &input_y,
1034 &input_w, &input_h);
1035 }
1036
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001037 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001038 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001039
1040 shell_interface->set_window_geometry(window->shsurf,
1041 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001042 }
1043}
1044
1045static void
1046weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1047{
1048 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001049 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001051 if (window->frame_id == XCB_WINDOW_NONE) {
1052 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001053 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001054 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001055 if(window->has_alpha) {
1056 pixman_region32_init(&window->surface->pending.opaque);
1057 } else {
1058 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1059 width, height);
1060 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001061 if (window->view)
1062 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001063 }
1064 return;
1065 }
1066
1067 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001068 return;
1069
1070 window->repaint_source =
1071 wl_event_loop_add_idle(wm->server->loop,
1072 weston_wm_window_draw_decoration,
1073 window);
1074}
1075
1076static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001077weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1078{
1079 xcb_property_notify_event_t *property_notify =
1080 (xcb_property_notify_event_t *) event;
1081 struct weston_wm_window *window;
1082
1083 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001084 if (!window)
1085 return;
1086
1087 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001088
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001089 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001090 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001091 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001092 else
1093 read_and_dump_property(wm, property_notify->window,
1094 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001095
1096 if (property_notify->atom == wm->atom.net_wm_name ||
1097 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001098 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001099}
1100
1101static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001102weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001103 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001104{
1105 struct weston_wm_window *window;
1106 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001107 xcb_get_geometry_cookie_t geometry_cookie;
1108 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001109
Peter Huttererf3d62272013-08-08 11:57:05 +10001110 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001111 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001112 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001113 return;
1114 }
1115
MoD384a11a2013-06-22 11:04:21 -05001116 geometry_cookie = xcb_get_geometry(wm->conn, id);
1117
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001118 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1119 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1120
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001121 window->wm = wm;
1122 window->id = id;
1123 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001124 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001125 window->width = width;
1126 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001127 window->x = x;
1128 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001129
MoD384a11a2013-06-22 11:04:21 -05001130 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1131 /* technically we should use XRender and check the visual format's
1132 alpha_mask, but checking depth is simpler and works in all known cases */
1133 if(geometry_reply != NULL)
1134 window->has_alpha = geometry_reply->depth == 32;
1135 free(geometry_reply);
1136
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001137 hash_table_insert(wm->window_hash, id, window);
1138}
1139
1140static void
1141weston_wm_window_destroy(struct weston_wm_window *window)
1142{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001143 struct weston_wm *wm = window->wm;
1144
1145 if (window->repaint_source)
1146 wl_event_source_remove(window->repaint_source);
1147 if (window->cairo_surface)
1148 cairo_surface_destroy(window->cairo_surface);
1149
1150 if (window->frame_id) {
1151 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1152 xcb_destroy_window(wm->conn, window->frame_id);
1153 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001154 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001155 hash_table_remove(wm->window_hash, window->frame_id);
1156 window->frame_id = XCB_WINDOW_NONE;
1157 }
1158
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001159 if (window->surface_id)
1160 wl_list_remove(&window->link);
1161
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001162 hash_table_remove(window->wm->window_hash, window->id);
1163 free(window);
1164}
1165
1166static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001167weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1168{
1169 xcb_create_notify_event_t *create_notify =
1170 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001171
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001172 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1173 create_notify->window,
1174 create_notify->width, create_notify->height,
1175 create_notify->override_redirect ? ", override" : "",
1176 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001177
1178 if (our_resource(wm, create_notify->window))
1179 return;
1180
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001181 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001182 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001183 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001184 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001185}
1186
1187static void
1188weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1189{
1190 xcb_destroy_notify_event_t *destroy_notify =
1191 (xcb_destroy_notify_event_t *) event;
1192 struct weston_wm_window *window;
1193
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001194 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1195 destroy_notify->window,
1196 destroy_notify->event,
1197 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001198
1199 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001200 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001201
1202 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001203 weston_wm_window_destroy(window);
1204}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001205
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001206static void
1207weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1208{
1209 xcb_reparent_notify_event_t *reparent_notify =
1210 (xcb_reparent_notify_event_t *) event;
1211 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001212
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001213 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1214 reparent_notify->window,
1215 reparent_notify->parent,
1216 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001217
1218 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001219 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001220 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001221 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001222 } else if (!our_resource(wm, reparent_notify->parent)) {
1223 window = hash_table_lookup(wm->window_hash,
1224 reparent_notify->window);
1225 weston_wm_window_destroy(window);
1226 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001227}
1228
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001229struct weston_seat *
1230weston_wm_pick_seat(struct weston_wm *wm)
1231{
1232 return container_of(wm->server->compositor->seat_list.next,
1233 struct weston_seat, link);
1234}
1235
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001236static struct weston_seat *
1237weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1238{
1239 struct weston_wm *wm = window->wm;
1240 struct weston_seat *seat, *s;
1241
1242 seat = NULL;
1243 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001244 if (s->pointer != NULL && s->pointer->focus &&
1245 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001246 s->pointer->button_count > 0 &&
1247 (seat == NULL ||
1248 s->pointer->grab_serial -
1249 seat->pointer->grab_serial < (1 << 30)))
1250 seat = s;
1251 }
1252
1253 return seat;
1254}
1255
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001256static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001257weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1258 xcb_client_message_event_t *client_message)
1259{
1260 static const int map[] = {
1261 THEME_LOCATION_RESIZING_TOP_LEFT,
1262 THEME_LOCATION_RESIZING_TOP,
1263 THEME_LOCATION_RESIZING_TOP_RIGHT,
1264 THEME_LOCATION_RESIZING_RIGHT,
1265 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1266 THEME_LOCATION_RESIZING_BOTTOM,
1267 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1268 THEME_LOCATION_RESIZING_LEFT
1269 };
1270
1271 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001272 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001273 int detail;
1274 struct weston_shell_interface *shell_interface =
1275 &wm->server->compositor->shell_interface;
1276
Boyan Dingc06a1802014-07-06 11:44:58 +08001277 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001278 || !seat->pointer->focus
1279 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001280 return;
1281
1282 detail = client_message->data.data32[2];
1283 switch (detail) {
1284 case _NET_WM_MOVERESIZE_MOVE:
1285 shell_interface->move(window->shsurf, seat);
1286 break;
1287 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1288 case _NET_WM_MOVERESIZE_SIZE_TOP:
1289 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1290 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1291 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1292 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1293 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1294 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1295 shell_interface->resize(window->shsurf, seat, map[detail]);
1296 break;
1297 case _NET_WM_MOVERESIZE_CANCEL:
1298 break;
1299 }
1300}
1301
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001302#define _NET_WM_STATE_REMOVE 0
1303#define _NET_WM_STATE_ADD 1
1304#define _NET_WM_STATE_TOGGLE 2
1305
1306static int
1307update_state(int action, int *state)
1308{
1309 int new_state, changed;
1310
1311 switch (action) {
1312 case _NET_WM_STATE_REMOVE:
1313 new_state = 0;
1314 break;
1315 case _NET_WM_STATE_ADD:
1316 new_state = 1;
1317 break;
1318 case _NET_WM_STATE_TOGGLE:
1319 new_state = !*state;
1320 break;
1321 default:
1322 return 0;
1323 }
1324
1325 changed = (*state != new_state);
1326 *state = new_state;
1327
1328 return changed;
1329}
1330
1331static void
1332weston_wm_window_configure(void *data);
1333
1334static void
1335weston_wm_window_handle_state(struct weston_wm_window *window,
1336 xcb_client_message_event_t *client_message)
1337{
1338 struct weston_wm *wm = window->wm;
1339 struct weston_shell_interface *shell_interface =
1340 &wm->server->compositor->shell_interface;
1341 uint32_t action, property;
1342
1343 action = client_message->data.data32[0];
1344 property = client_message->data.data32[1];
1345
1346 if (property == wm->atom.net_wm_state_fullscreen &&
1347 update_state(action, &window->fullscreen)) {
1348 weston_wm_window_set_net_wm_state(window);
1349 if (window->fullscreen) {
1350 window->saved_width = window->width;
1351 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001352
1353 if (window->shsurf)
1354 shell_interface->set_fullscreen(window->shsurf,
1355 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1356 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001357 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001358 if (window->shsurf)
1359 shell_interface->set_toplevel(window->shsurf);
1360
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001361 window->width = window->saved_width;
1362 window->height = window->saved_height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001363 if (window->frame)
1364 frame_resize_inside(window->frame,
1365 window->width,
1366 window->height);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001367 weston_wm_window_configure(window);
1368 }
1369 }
1370}
1371
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001372static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001373surface_destroy(struct wl_listener *listener, void *data)
1374{
1375 struct weston_wm_window *window =
1376 container_of(listener,
1377 struct weston_wm_window, surface_destroy_listener);
1378
1379 wm_log("surface for xid %d destroyed\n", window->id);
1380
1381 /* This should have been freed by the shell.
1382 * Don't try to use it later. */
1383 window->shsurf = NULL;
1384 window->surface = NULL;
1385 window->view = NULL;
1386}
1387
1388static void
1389weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1390 xcb_client_message_event_t *client_message)
1391{
1392 struct weston_wm *wm = window->wm;
1393 struct wl_resource *resource;
1394
1395 if (window->surface_id != 0) {
1396 wm_log("already have surface id for window %d\n", window->id);
1397 return;
1398 }
1399
1400 /* Xwayland will send the wayland requests to create the
1401 * wl_surface before sending this client message. Even so, we
1402 * can end up handling the X event before the wayland requests
1403 * and thus when we try to look up the surface ID, the surface
1404 * hasn't been created yet. In that case put the window on
1405 * the unpaired window list and continue when the surface gets
1406 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001407 uint32_t id = client_message->data.data32[0];
1408 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001409 if (resource) {
1410 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001411 xserver_map_shell_surface(window,
1412 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001413 }
1414 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001415 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001416 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001417 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001418}
1419
1420static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001421weston_wm_handle_client_message(struct weston_wm *wm,
1422 xcb_generic_event_t *event)
1423{
1424 xcb_client_message_event_t *client_message =
1425 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001426 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001427
1428 window = hash_table_lookup(wm->window_hash, client_message->window);
1429
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001430 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1431 get_atom_name(wm->conn, client_message->type),
1432 client_message->data.data32[0],
1433 client_message->data.data32[1],
1434 client_message->data.data32[2],
1435 client_message->data.data32[3],
1436 client_message->data.data32[4],
1437 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001438
Jason Ekstrand0250a742014-07-24 13:17:47 -07001439 /* The window may get created and destroyed before we actually
1440 * handle the message. If it doesn't exist, bail.
1441 */
1442 if (!window)
1443 return;
1444
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001445 if (client_message->type == wm->atom.net_wm_moveresize)
1446 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001447 else if (client_message->type == wm->atom.net_wm_state)
1448 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001449 else if (client_message->type == wm->atom.wl_surface_id)
1450 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001451}
1452
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001453enum cursor_type {
1454 XWM_CURSOR_TOP,
1455 XWM_CURSOR_BOTTOM,
1456 XWM_CURSOR_LEFT,
1457 XWM_CURSOR_RIGHT,
1458 XWM_CURSOR_TOP_LEFT,
1459 XWM_CURSOR_TOP_RIGHT,
1460 XWM_CURSOR_BOTTOM_LEFT,
1461 XWM_CURSOR_BOTTOM_RIGHT,
1462 XWM_CURSOR_LEFT_PTR,
1463};
1464
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001465/*
1466 * The following correspondences between file names and cursors was copied
1467 * from: https://bugs.kde.org/attachment.cgi?id=67313
1468 */
1469
1470static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001471 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001472 "sw-resize",
1473 "size_bdiag"
1474};
1475
1476static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001477 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001478 "se-resize",
1479 "size_fdiag"
1480};
1481
1482static const char *bottom_sides[] = {
1483 "bottom_side",
1484 "s-resize",
1485 "size_ver"
1486};
1487
1488static const char *left_ptrs[] = {
1489 "left_ptr",
1490 "default",
1491 "top_left_arrow",
1492 "left-arrow"
1493};
1494
1495static const char *left_sides[] = {
1496 "left_side",
1497 "w-resize",
1498 "size_hor"
1499};
1500
1501static const char *right_sides[] = {
1502 "right_side",
1503 "e-resize",
1504 "size_hor"
1505};
1506
1507static const char *top_left_corners[] = {
1508 "top_left_corner",
1509 "nw-resize",
1510 "size_fdiag"
1511};
1512
1513static const char *top_right_corners[] = {
1514 "top_right_corner",
1515 "ne-resize",
1516 "size_bdiag"
1517};
1518
1519static const char *top_sides[] = {
1520 "top_side",
1521 "n-resize",
1522 "size_ver"
1523};
1524
1525struct cursor_alternatives {
1526 const char **names;
1527 size_t count;
1528};
1529
1530static const struct cursor_alternatives cursors[] = {
1531 {top_sides, ARRAY_LENGTH(top_sides)},
1532 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1533 {left_sides, ARRAY_LENGTH(left_sides)},
1534 {right_sides, ARRAY_LENGTH(right_sides)},
1535 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1536 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1537 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1538 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1539 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001540};
1541
1542static void
1543weston_wm_create_cursors(struct weston_wm *wm)
1544{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001545 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001546 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001547 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001548
1549 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1550 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001551 for (j = 0; j < cursors[i].count; j++) {
1552 name = cursors[i].names[j];
1553 wm->cursors[i] =
1554 xcb_cursor_library_load_cursor(wm, name);
1555 if (wm->cursors[i] != (xcb_cursor_t)-1)
1556 break;
1557 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001558 }
1559
1560 wm->last_cursor = -1;
1561}
1562
1563static void
1564weston_wm_destroy_cursors(struct weston_wm *wm)
1565{
1566 uint8_t i;
1567
1568 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1569 xcb_free_cursor(wm->conn, wm->cursors[i]);
1570
1571 free(wm->cursors);
1572}
1573
1574static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001575get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001576{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001577 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001578
1579 switch (location) {
1580 case THEME_LOCATION_RESIZING_TOP:
1581 return XWM_CURSOR_TOP;
1582 case THEME_LOCATION_RESIZING_BOTTOM:
1583 return XWM_CURSOR_BOTTOM;
1584 case THEME_LOCATION_RESIZING_LEFT:
1585 return XWM_CURSOR_LEFT;
1586 case THEME_LOCATION_RESIZING_RIGHT:
1587 return XWM_CURSOR_RIGHT;
1588 case THEME_LOCATION_RESIZING_TOP_LEFT:
1589 return XWM_CURSOR_TOP_LEFT;
1590 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1591 return XWM_CURSOR_TOP_RIGHT;
1592 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1593 return XWM_CURSOR_BOTTOM_LEFT;
1594 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1595 return XWM_CURSOR_BOTTOM_RIGHT;
1596 case THEME_LOCATION_EXTERIOR:
1597 case THEME_LOCATION_TITLEBAR:
1598 default:
1599 return XWM_CURSOR_LEFT_PTR;
1600 }
1601}
1602
1603static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001604weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1605 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001606{
1607 uint32_t cursor_value_list;
1608
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001609 if (wm->last_cursor == cursor)
1610 return;
1611
1612 wm->last_cursor = cursor;
1613
1614 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001615 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001616 XCB_CW_CURSOR, &cursor_value_list);
1617 xcb_flush(wm->conn);
1618}
1619
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001620static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001621weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001622{
1623 xcb_client_message_event_t client_message;
1624
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001625 if (window->delete_window) {
1626 client_message.response_type = XCB_CLIENT_MESSAGE;
1627 client_message.format = 32;
1628 client_message.window = window->id;
1629 client_message.type = window->wm->atom.wm_protocols;
1630 client_message.data.data32[0] =
1631 window->wm->atom.wm_delete_window;
1632 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001633
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001634 xcb_send_event(window->wm->conn, 0, window->id,
1635 XCB_EVENT_MASK_NO_EVENT,
1636 (char *) &client_message);
1637 } else {
1638 xcb_kill_client(window->wm->conn, window->id);
1639 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001640}
1641
1642static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001643weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1644{
1645 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1646 struct weston_shell_interface *shell_interface =
1647 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001648 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001649 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001650 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001651 enum frame_button_state button_state;
1652 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001653
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001654 wm_log("XCB_BUTTON_%s (detail %d)\n",
1655 button->response_type == XCB_BUTTON_PRESS ?
1656 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001657
1658 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001659 if (!window || !window->decorate)
1660 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001661
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001662 if (button->detail != 1 && button->detail != 2)
1663 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001664
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001665 seat = weston_wm_pick_seat_for_window(window);
1666
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001667 button_state = button->response_type == XCB_BUTTON_PRESS ?
1668 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1669 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1670
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001671 /* Make sure we're looking at the right location. The frame
1672 * could have received a motion event from a pointer from a
1673 * different wl_seat, but under X it looks like our core
1674 * pointer moved. Move the frame pointer to the button press
1675 * location before deciding what to do. */
1676 location = frame_pointer_motion(window->frame, NULL,
1677 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001678 location = frame_pointer_button(window->frame, NULL,
1679 button_id, button_state);
1680 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1681 weston_wm_window_schedule_repaint(window);
1682
1683 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001684 if (seat != NULL)
1685 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001686 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1687 }
1688
1689 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001690 if (seat != NULL)
1691 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001692 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1693 }
1694
1695 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001696 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001697 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001698 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001699}
1700
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001701static void
1702weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1703{
1704 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1705 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001706 enum theme_location location;
1707 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001708
1709 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001710 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001711 return;
1712
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001713 location = frame_pointer_motion(window->frame, NULL,
1714 motion->event_x, motion->event_y);
1715 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1716 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001717
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001718 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001719 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001720}
1721
1722static void
1723weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1724{
1725 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1726 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001727 enum theme_location location;
1728 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001729
1730 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001731 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001732 return;
1733
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001734 location = frame_pointer_enter(window->frame, NULL,
1735 enter->event_x, enter->event_y);
1736 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1737 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001738
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001739 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001740 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001741}
1742
1743static void
1744weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1745{
1746 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1747 struct weston_wm_window *window;
1748
1749 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001750 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001751 return;
1752
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001753 frame_pointer_leave(window->frame, NULL);
1754 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1755 weston_wm_window_schedule_repaint(window);
1756
Tiago Vignattic1903232012-07-16 12:15:37 -04001757 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001758}
1759
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760static int
1761weston_wm_handle_event(int fd, uint32_t mask, void *data)
1762{
1763 struct weston_wm *wm = data;
1764 xcb_generic_event_t *event;
1765 int count = 0;
1766
1767 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1768 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001769 free(event);
1770 count++;
1771 continue;
1772 }
1773
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001774 if (weston_wm_handle_dnd_event(wm, event)) {
1775 free(event);
1776 count++;
1777 continue;
1778 }
1779
MoD31700122013-06-11 19:58:55 -05001780 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001781 case XCB_BUTTON_PRESS:
1782 case XCB_BUTTON_RELEASE:
1783 weston_wm_handle_button(wm, event);
1784 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001785 case XCB_ENTER_NOTIFY:
1786 weston_wm_handle_enter(wm, event);
1787 break;
1788 case XCB_LEAVE_NOTIFY:
1789 weston_wm_handle_leave(wm, event);
1790 break;
1791 case XCB_MOTION_NOTIFY:
1792 weston_wm_handle_motion(wm, event);
1793 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001794 case XCB_CREATE_NOTIFY:
1795 weston_wm_handle_create_notify(wm, event);
1796 break;
1797 case XCB_MAP_REQUEST:
1798 weston_wm_handle_map_request(wm, event);
1799 break;
1800 case XCB_MAP_NOTIFY:
1801 weston_wm_handle_map_notify(wm, event);
1802 break;
1803 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001804 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001806 case XCB_REPARENT_NOTIFY:
1807 weston_wm_handle_reparent_notify(wm, event);
1808 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001809 case XCB_CONFIGURE_REQUEST:
1810 weston_wm_handle_configure_request(wm, event);
1811 break;
1812 case XCB_CONFIGURE_NOTIFY:
1813 weston_wm_handle_configure_notify(wm, event);
1814 break;
1815 case XCB_DESTROY_NOTIFY:
1816 weston_wm_handle_destroy_notify(wm, event);
1817 break;
1818 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001819 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001820 break;
1821 case XCB_PROPERTY_NOTIFY:
1822 weston_wm_handle_property_notify(wm, event);
1823 break;
1824 case XCB_CLIENT_MESSAGE:
1825 weston_wm_handle_client_message(wm, event);
1826 break;
1827 }
1828
1829 free(event);
1830 count++;
1831 }
1832
1833 xcb_flush(wm->conn);
1834
1835 return count;
1836}
1837
1838static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001839weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1840{
1841 xcb_depth_iterator_t d_iter;
1842 xcb_visualtype_iterator_t vt_iter;
1843 xcb_visualtype_t *visualtype;
1844
1845 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1846 visualtype = NULL;
1847 while (d_iter.rem > 0) {
1848 if (d_iter.data->depth == 32) {
1849 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1850 visualtype = vt_iter.data;
1851 break;
1852 }
1853
1854 xcb_depth_next(&d_iter);
1855 }
1856
1857 if (visualtype == NULL) {
1858 weston_log("no 32 bit visualtype\n");
1859 return;
1860 }
1861
1862 wm->visual_id = visualtype->visual_id;
1863 wm->colormap = xcb_generate_id(wm->conn);
1864 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1865 wm->colormap, wm->screen->root, wm->visual_id);
1866}
1867
1868static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001869weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001870{
1871
1872#define F(field) offsetof(struct weston_wm, field)
1873
1874 static const struct { const char *name; int offset; } atoms[] = {
1875 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001876 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001877 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1878 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001879 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001880 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001881 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001882 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001884 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001885 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1886 { "_NET_WM_STATE", F(atom.net_wm_state) },
1887 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1888 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1889 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001890 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001891 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1892
1893 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1894 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1895 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1896 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1897 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1898 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1899 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001900 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1901 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001902 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1903 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1904 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1905 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1906 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1907
1908 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1909 { "_NET_SUPPORTING_WM_CHECK",
1910 F(atom.net_supporting_wm_check) },
1911 { "_NET_SUPPORTED", F(atom.net_supported) },
1912 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1913 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001914 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001915 { "TARGETS", F(atom.targets) },
1916 { "UTF8_STRING", F(atom.utf8_string) },
1917 { "_WL_SELECTION", F(atom.wl_selection) },
1918 { "INCR", F(atom.incr) },
1919 { "TIMESTAMP", F(atom.timestamp) },
1920 { "MULTIPLE", F(atom.multiple) },
1921 { "UTF8_STRING" , F(atom.utf8_string) },
1922 { "COMPOUND_TEXT", F(atom.compound_text) },
1923 { "TEXT", F(atom.text) },
1924 { "STRING", F(atom.string) },
1925 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1926 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001927 { "XdndSelection", F(atom.xdnd_selection) },
1928 { "XdndAware", F(atom.xdnd_aware) },
1929 { "XdndEnter", F(atom.xdnd_enter) },
1930 { "XdndLeave", F(atom.xdnd_leave) },
1931 { "XdndDrop", F(atom.xdnd_drop) },
1932 { "XdndStatus", F(atom.xdnd_status) },
1933 { "XdndFinished", F(atom.xdnd_finished) },
1934 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001935 { "XdndActionCopy", F(atom.xdnd_action_copy) },
1936 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001937 };
1938#undef F
1939
1940 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1941 xcb_xfixes_query_version_reply_t *xfixes_reply;
1942 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1943 xcb_intern_atom_reply_t *reply;
1944 xcb_render_query_pict_formats_reply_t *formats_reply;
1945 xcb_render_query_pict_formats_cookie_t formats_cookie;
1946 xcb_render_pictforminfo_t *formats;
1947 uint32_t i;
1948
1949 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001950 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001951
1952 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1953
1954 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1955 cookies[i] = xcb_intern_atom (wm->conn, 0,
1956 strlen(atoms[i].name),
1957 atoms[i].name);
1958
1959 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1960 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1961 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1962 free(reply);
1963 }
1964
1965 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1966 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001967 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001968
1969 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1970 XCB_XFIXES_MAJOR_VERSION,
1971 XCB_XFIXES_MINOR_VERSION);
1972 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1973 xfixes_cookie, NULL);
1974
Martin Minarik6d118362012-06-07 18:01:59 +02001975 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001976 xfixes_reply->major_version, xfixes_reply->minor_version);
1977
1978 free(xfixes_reply);
1979
1980 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1981 formats_cookie, 0);
1982 if (formats_reply == NULL)
1983 return;
1984
1985 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001986 for (i = 0; i < formats_reply->num_formats; i++) {
1987 if (formats[i].direct.red_mask != 0xff &&
1988 formats[i].direct.red_shift != 16)
1989 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001990 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1991 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001992 wm->format_rgb = formats[i];
1993 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1994 formats[i].depth == 32 &&
1995 formats[i].direct.alpha_mask == 0xff &&
1996 formats[i].direct.alpha_shift == 24)
1997 wm->format_rgba = formats[i];
1998 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001999
2000 free(formats_reply);
2001}
2002
2003static void
2004weston_wm_create_wm_window(struct weston_wm *wm)
2005{
2006 static const char name[] = "Weston WM";
2007
2008 wm->wm_window = xcb_generate_id(wm->conn);
2009 xcb_create_window(wm->conn,
2010 XCB_COPY_FROM_PARENT,
2011 wm->wm_window,
2012 wm->screen->root,
2013 0, 0,
2014 10, 10,
2015 0,
2016 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2017 wm->screen->root_visual,
2018 0, NULL);
2019
2020 xcb_change_property(wm->conn,
2021 XCB_PROP_MODE_REPLACE,
2022 wm->wm_window,
2023 wm->atom.net_supporting_wm_check,
2024 XCB_ATOM_WINDOW,
2025 32, /* format */
2026 1, &wm->wm_window);
2027
2028 xcb_change_property(wm->conn,
2029 XCB_PROP_MODE_REPLACE,
2030 wm->wm_window,
2031 wm->atom.net_wm_name,
2032 wm->atom.utf8_string,
2033 8, /* format */
2034 strlen(name), name);
2035
2036 xcb_change_property(wm->conn,
2037 XCB_PROP_MODE_REPLACE,
2038 wm->screen->root,
2039 wm->atom.net_supporting_wm_check,
2040 XCB_ATOM_WINDOW,
2041 32, /* format */
2042 1, &wm->wm_window);
2043
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002044 /* Claim the WM_S0 selection even though we don't suport
2045 * the --replace functionality. */
2046 xcb_set_selection_owner(wm->conn,
2047 wm->wm_window,
2048 wm->atom.wm_s0,
2049 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002050
2051 xcb_set_selection_owner(wm->conn,
2052 wm->wm_window,
2053 wm->atom.net_wm_cm_s0,
2054 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002055}
2056
2057struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002058weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002059{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002060 struct weston_wm *wm;
2061 struct wl_event_loop *loop;
2062 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002063 uint32_t values[1];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002064 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002065
Peter Huttererf3d62272013-08-08 11:57:05 +10002066 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002067 if (wm == NULL)
2068 return NULL;
2069
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002070 wm->server = wxs;
2071 wm->window_hash = hash_table_create();
2072 if (wm->window_hash == NULL) {
2073 free(wm);
2074 return NULL;
2075 }
2076
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002077 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002078 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002079 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002080 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002081 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002082 hash_table_destroy(wm->window_hash);
2083 free(wm);
2084 return NULL;
2085 }
2086
2087 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2088 wm->screen = s.data;
2089
2090 loop = wl_display_get_event_loop(wxs->wl_display);
2091 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002092 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002093 WL_EVENT_READABLE,
2094 weston_wm_handle_event, wm);
2095 wl_event_source_check(wm->source);
2096
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002097 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002098 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002099
2100 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002101 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2102 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2103 XCB_EVENT_MASK_PROPERTY_CHANGE;
2104 xcb_change_window_attributes(wm->conn, wm->screen->root,
2105 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002106
2107 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2108 XCB_COMPOSITE_REDIRECT_MANUAL);
2109
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002110 wm->theme = theme_create();
2111
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002112 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002113 supported[1] = wm->atom.net_wm_state;
2114 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002115 xcb_change_property(wm->conn,
2116 XCB_PROP_MODE_REPLACE,
2117 wm->screen->root,
2118 wm->atom.net_supported,
2119 XCB_ATOM_ATOM,
2120 32, /* format */
2121 ARRAY_LENGTH(supported), supported);
2122
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002123 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002124
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002125 weston_wm_dnd_init(wm);
2126
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002127 xcb_flush(wm->conn);
2128
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002129 wm->create_surface_listener.notify = weston_wm_create_surface;
2130 wl_signal_add(&wxs->compositor->create_surface_signal,
2131 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002132 wm->activate_listener.notify = weston_wm_window_activate;
2133 wl_signal_add(&wxs->compositor->activate_signal,
2134 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002135 wm->transform_listener.notify = weston_wm_window_transform;
2136 wl_signal_add(&wxs->compositor->transform_signal,
2137 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002138 wm->kill_listener.notify = weston_wm_kill_client;
2139 wl_signal_add(&wxs->compositor->kill_signal,
2140 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002141 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002143 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002144 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002145
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002146 /* Create wm window and take WM_S0 selection last, which
2147 * signals to Xwayland that we're done with setup. */
2148 weston_wm_create_wm_window(wm);
2149
2150 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002151
2152 return wm;
2153}
2154
2155void
2156weston_wm_destroy(struct weston_wm *wm)
2157{
2158 /* FIXME: Free windows in hash. */
2159 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002160 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002161 xcb_disconnect(wm->conn);
2162 wl_event_source_remove(wm->source);
2163 wl_list_remove(&wm->selection_listener.link);
2164 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002165 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002166 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002167
2168 free(wm);
2169}
2170
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002171static struct weston_wm_window *
2172get_wm_window(struct weston_surface *surface)
2173{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002174 struct wl_listener *listener;
2175
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002176 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002177 if (listener)
2178 return container_of(listener, struct weston_wm_window,
2179 surface_destroy_listener);
2180
2181 return NULL;
2182}
2183
2184static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002185weston_wm_window_configure(void *data)
2186{
2187 struct weston_wm_window *window = data;
2188 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002189 uint32_t values[4];
2190 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002191
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002192 weston_wm_window_get_child_position(window, &x, &y);
2193 values[0] = x;
2194 values[1] = y;
2195 values[2] = window->width;
2196 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002197 xcb_configure_window(wm->conn,
2198 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002199 XCB_CONFIG_WINDOW_X |
2200 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002201 XCB_CONFIG_WINDOW_WIDTH |
2202 XCB_CONFIG_WINDOW_HEIGHT,
2203 values);
2204
2205 weston_wm_window_get_frame_size(window, &width, &height);
2206 values[0] = width;
2207 values[1] = height;
2208 xcb_configure_window(wm->conn,
2209 window->frame_id,
2210 XCB_CONFIG_WINDOW_WIDTH |
2211 XCB_CONFIG_WINDOW_HEIGHT,
2212 values);
2213
2214 window->configure_source = NULL;
2215
2216 weston_wm_window_schedule_repaint(window);
2217}
2218
2219static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002220send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002221{
2222 struct weston_wm_window *window = get_wm_window(surface);
2223 struct weston_wm *wm = window->wm;
2224 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002225 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002226
Marek Chalupae9fe4672014-10-29 13:44:44 +01002227 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002228 hborder = 2 * t->width;
2229 vborder = t->titlebar_height + t->width;
2230 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002231 hborder = 0;
2232 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002233 }
2234
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002235 if (width > hborder)
2236 window->width = width - hborder;
2237 else
2238 window->width = 1;
2239
2240 if (height > vborder)
2241 window->height = height - vborder;
2242 else
2243 window->height = 1;
2244
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002245 if (window->frame)
2246 frame_resize_inside(window->frame, window->width, window->height);
2247
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002248 if (window->configure_source)
2249 return;
2250
2251 window->configure_source =
2252 wl_event_loop_add_idle(wm->server->loop,
2253 weston_wm_window_configure, window);
2254}
2255
2256static const struct weston_shell_client shell_client = {
2257 send_configure
2258};
2259
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002260static int
2261legacy_fullscreen(struct weston_wm *wm,
2262 struct weston_wm_window *window,
2263 struct weston_output **output_ret)
2264{
2265 struct weston_compositor *compositor = wm->server->compositor;
2266 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002267 uint32_t minmax = PMinSize | PMaxSize;
2268 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002269
2270 /* Heuristics for detecting legacy fullscreen windows... */
2271
2272 wl_list_for_each(output, &compositor->output_list, link) {
2273 if (output->x == window->x &&
2274 output->y == window->y &&
2275 output->width == window->width &&
2276 output->height == window->height &&
2277 window->override_redirect) {
2278 *output_ret = output;
2279 return 1;
2280 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002281
2282 matching_size = 0;
2283 if ((window->size_hints.flags & (USSize |PSize)) &&
2284 window->size_hints.width == output->width &&
2285 window->size_hints.height == output->height)
2286 matching_size = 1;
2287 if ((window->size_hints.flags & minmax) == minmax &&
2288 window->size_hints.min_width == output->width &&
2289 window->size_hints.min_height == output->height &&
2290 window->size_hints.max_width == output->width &&
2291 window->size_hints.max_height == output->height)
2292 matching_size = 1;
2293
2294 if (matching_size && !window->decorate &&
2295 (window->size_hints.flags & (USPosition | PPosition)) &&
2296 window->size_hints.x == output->x &&
2297 window->size_hints.y == output->y) {
2298 *output_ret = output;
2299 return 1;
2300 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002301 }
2302
2303 return 0;
2304}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002305
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002306static bool
2307weston_wm_window_type_inactive(struct weston_wm_window *window)
2308{
2309 struct weston_wm *wm = window->wm;
2310
2311 return window->type == wm->atom.net_wm_window_type_tooltip ||
2312 window->type == wm->atom.net_wm_window_type_dropdown ||
2313 window->type == wm->atom.net_wm_window_type_dnd ||
2314 window->type == wm->atom.net_wm_window_type_combo ||
2315 window->type == wm->atom.net_wm_window_type_popup;
2316}
2317
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002318static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002319xserver_map_shell_surface(struct weston_wm_window *window,
2320 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002321{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002322 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002323 struct weston_shell_interface *shell_interface =
2324 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002325 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002326 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002327 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002328
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002329 weston_wm_window_read_properties(window);
2330
2331 /* A weston_wm_window may have many different surfaces assigned
2332 * throughout its life, so we must make sure to remove the listener
2333 * from the old surface signal list. */
2334 if (window->surface)
2335 wl_list_remove(&window->surface_destroy_listener.link);
2336
2337 window->surface = surface;
2338 window->surface_destroy_listener.notify = surface_destroy;
2339 wl_signal_add(&window->surface->destroy_signal,
2340 &window->surface_destroy_listener);
2341
2342 weston_wm_window_schedule_repaint(window);
2343
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002344 if (!shell_interface->create_shell_surface)
2345 return;
2346
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 if (!shell_interface->get_primary_view)
2348 return;
2349
Pekka Paalanen50b67472014-10-01 15:02:41 +03002350 if (window->surface->configure) {
2351 weston_log("warning, unexpected in %s: "
2352 "surface's configure hook is already set.\n",
2353 __func__);
2354 return;
2355 }
2356
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002357 window->shsurf =
2358 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002359 window->surface,
2360 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 window->view = shell_interface->get_primary_view(shell_interface->shell,
2362 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002363
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002364 if (window->name)
2365 shell_interface->set_title(window->shsurf, window->name);
2366
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002367 if (window->fullscreen) {
2368 window->saved_width = window->width;
2369 window->saved_height = window->height;
2370 shell_interface->set_fullscreen(window->shsurf,
2371 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2372 0, NULL);
2373 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002374 } else if (legacy_fullscreen(wm, window, &output)) {
2375 window->fullscreen = 1;
2376 shell_interface->set_fullscreen(window->shsurf,
2377 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2378 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002379 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002380 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002381 window->x,
2382 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002383 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002384 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002385 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002386 if (weston_wm_window_type_inactive(window))
2387 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002388 shell_interface->set_transient(window->shsurf,
2389 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002390 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002391 window->y - parent->y, flags);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002392 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002393 if (weston_wm_window_type_inactive(window)) {
2394 shell_interface->set_xwayland(window->shsurf,
2395 window->x,
2396 window->y,
2397 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2398 } else {
2399 shell_interface->set_toplevel(window->shsurf);
2400 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002401 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002402}